Jake Vanderwerf
2026-03-03 772462eeca3002a1d52508aeba485aab2b4742ad
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
<?php
namespace JVBase\managers\SEO\render\Traits\_Helpers;
 
if (!defined('ABSPATH')) {
    exit;
}
trait arrayHelper {
    protected function classArray(string $property, array $array, string $className):array
    {
        return array_filter($array, function ($item) use ($property, $className) {
            $test = class_exists($className) && is_a($item, $className);
            if (!$test) {
                error_log('[SEO]Item property '.$property.' is not an instance of '.$className);
            }
            return $test;
        });
    }
    protected function mixedArray(string $property, array $array, array $allowedTypes):array
    {
        return array_filter($array, function($item) use ($property, $allowedTypes) {
            $test = in_array(gettype($item), $allowedTypes) || $this->testClasses($item, $allowedTypes);
            if (!$test) {
                error_log('[SEO]Item property '.$property.' is not an allowed type: '.print_r($item, true));
            }
            return $test;
        });
    }
    protected function testClasses(mixed $item, array $allowedTypes):bool
    {
        if (gettype($item) !== 'object') return false;
        foreach ($allowedTypes as $type) {
            if (!class_exists($type)) continue;
            $test = is_a($item, $type);
            if ($test){
                return true;
            }
        }
        return false;
    }
    protected function stringArray(string $property, array $array):array
    {
        return array_filter($array, function($item) use($property) {
            if (!is_string($item)) {
                error_log('[SEO]Item property '.$property.' is not string: '.print_r($item, true));
            }
            return is_string($item);
        });
    }
    protected function urlArray(string $property, array $array):array
    {
        return array_filter($array, function($item) use($property) {
            $test = filter_var($item, FILTER_VALIDATE_URL);
            if (!$test) {
                error_log('[SEO]Item property '.$property.' is not URL: '.print_r($item, true));
            }
            return $test;
        });
    }
    protected function intArray(string $property, array $array):array
    {
        return array_filter($array, function($item) use($property) {
            if (!is_int($item)) {
                error_log('[SEO]Item property '.$property.' is not int: '.print_r($item, true));
            }
            return is_int($item);
        });
    }
    protected function floatArray(string $property, array $array):array
    {
        return array_filter($array, function($item) use($property) {
            if (!is_float($item)) {
                error_log('[SEO]Item property '.$property.' is not float: '.print_r($item, true));
            }
            return is_float($item);
        });
    }
 
    protected function testAllowed(string $property, array $mappedTypes, array $testTypes):array
    {
        return array_map(
            function($item) use ($mappedTypes) {
                return $mappedTypes[strtolower($item)];
            },
            array_filter($testTypes, function($item) use($property, $mappedTypes) {
                $test = array_key_exists(strtolower($item), $mappedTypes);
                if (!$test) {
                    error_log('[SEO]Item property '.$property.' has invalid type: '.$item);
                }
                return $test;
            })
        );
    }
}