Jake Vanderwerf
yesterday f4be611c51473359e6d41780f0313c446079e9d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace JVBase\registrar\config\seo;
 
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class Meta {
    protected string $slug;
 
    protected string $title;
    protected array $titleConfig = [
        'maxLength' => 60,
        'alts'  => []
    ];
    protected string $description;
    protected array $descriptionConfig = [
        'maxLength' => 120,
        'alts'  => []
    ];
    protected int $image;
    protected int $twitterImage;
 
 
    public function __construct(string $slug) {
        $this->slug = $slug;
 
        $this->titleConfig['alts'] = apply_filters(BASE.jvbNoBase($slug).'TitleAlts', []);
        add_action('init', [$this, 'init']);
    }
 
    public function init():void {
        if (!function_exists('tsf')){
            return;
        }
 
        if ($this->hasTitle()){
            add_filter('the_seo_framework_title_from_generation', [$this, 'filterTitle'], 10, 2);
        }
        if ($this->hasDescription()){
            add_filter('the_seo_framework_generated_description', [$this, 'filterDescription'], 10, 3);
        }
        if ($this->hasImage()){
            add_filter('the_seo_framework_image_generation_params', [$this, 'filterImage'], 10, 3);
        }
    }
 
    public function setAlts(array $alts, string $type = 'title'):void
    {
        foreach (array_values($alts) as $i => $alt) {
            if (!is_string($alt)) {
                error_log('[Registrar.config.Meta]:setAlts invalid string attempted: '.print_r($alt, true));
                unset($alts[$i]);
            }
        }
        switch ($type) {
            case 'title':
                $this->titleConfig['alts'] = $alts;
                break;
            case 'description':
                $this->descriptionConfig['alts'] = $alts;
                break;
            default:
                error_log('[Registrar.config.Meta]:setAlts invalid type attempted: '.print_r($type, true));
 
        }
    }
 
    public function hasTitle():bool
    {
        return !empty($this->title);
    }
    public function hasDescription():bool
    {
        return !empty($this->description);
    }
    public function hasImage():bool
    {
        return !empty($this->image);
    }
    public function hasTwitterImage():bool
    {
        return !empty($this->twitterImage);
    }
 
    public function testLength(int $ID, string $result, string $type):string
    {
        $maxLength = match($type) {
            'title' => $this->titleConfig['maxLength'],
            'description'   => $this->descriptionConfig['alts'],
            default => false
        };
        if (!$maxLength) {
            error_log('[Registrar.config.Meta]:testLength invalid type: '.$type);
            return $result;
        }
        $alts = match($type) {
            'title' => $this->titleConfig['alts'],
            'description' => $this->descriptionConfig['alts'],
        };
 
 
        usort($alts, fn($a, $b) => mb_strlen($b) <=> mb_strlen($a));
 
 
        $meta = new \JVBase\meta\Meta($ID, Registrar::getInstance($this->slug)->getType());
        foreach ($alts as $alt) {
            $alt = Resolver::resolve($alt, $meta);
            $r = $result.' '.trim($alt);
            if (mb_strlen($r) <= $maxLength) {
                return $r;
            }
        }
        return rtrim($result, $maxLength - 3).'...';
    }
 
    public function filterTitle(string $title, ?array $args): string
    {
        error_log('Filtering title...');
        if (jvbTSFDoIt($this->slug, $args)){
            return $title;
        }
        $ID = jvbTSFGetID($args);
 
 
        $meta = new \JVBase\meta\Meta($ID, Registrar::getInstance($this->slug)->getType());
        $resolved = Resolver::resolve($this->title, $meta);
        $resolved = $this->testLength($ID, $resolved, 'title');
        return $resolved;
    }
 
    public function filterDescription(string $description, ?array $args): string
    {
        if (jvbTSFDoIt($this->slug, $args)){
            return $description;
        }
        $ID = jvbTSFGetID($args);
 
        $meta = new \JVBase\meta\Meta($ID, Registrar::getInstance($this->slug)->getType());
        return Resolver::resolve($this->title, $meta);
    }
    public function filterImage(string $image, ?array $args): string
    {
        if (jvbTSFDoIt($this->slug, $args)){
            return $image;
        }
        $ID = jvbTSFGetID($args);
 
        $meta = new \JVBase\meta\Meta($ID, Registrar::getInstance($this->slug)->getType());
        return Resolver::resolve($this->title, $meta);
    }
}