Test Failed
Push — develop ( 76157d...9702e2 )
by Paul
08:08
created

ElementControlsTrait::bdShortcode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 4
c 2
b 0
f 2
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Breakdance;
4
5
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
9
trait ElementControlsTrait
10
{
11
    public static function bdShortcode(): ShortcodeContract
12
    {
13
        static $shortcode;
14
        if (empty($shortcode)) {
15
            $shortcode = glsr(static::bdShortcodeClass());
16
        }
17
        return $shortcode;
18
    }
19
20
    abstract public static function bdShortcodeClass(): string;
21
22
    /**
23
     * @return array[]
24
     */
25
    public static function contentControls()
26
    {
27
        $controls = [];
28
        $settings = [];
29
        $shortcode = static::bdShortcode();
30
        $transformer = new Transformer($shortcode->settings(), $shortcode->tag);
0 ignored issues
show
Bug introduced by
Accessing tag on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
        foreach ($transformer as $item) {
32
            $controls[$item['path']] = $item['control'];
33
        }
34
        $controls = Arr::unflatten(array_filter($controls));
35
        foreach ($controls as $slug => $children) {
36
            $section = $transformer->section($slug);
37
            foreach ($children as $key => $child) {
38
                if (!str_starts_with($key, 'popout_')) {
39
                    $section['children'][] = $child;
40
                    continue;
41
                }
42
                $slug = Str::removePrefix($key, 'popout_');
43
                $section['children'][] = $transformer->popout($slug, array_values($child));
44
            }
45
            $settings[] = $section;
46
        }
47
        return $settings;
48
    }
49
50
    /**
51
     * This must return false if the element has no default properties
52
     * otherwise SSR will not trigger when control values are changed.
53
     *
54
     * @return array|false
55
     */
56
    public static function defaultProperties()
57
    {
58
        $properties = [];
59
        $shortcode = static::bdShortcode();
60
        $config = $shortcode->settings();
61
        $defaults = array_filter($config, fn ($args) => isset($args['default']));
62
        $transformer = new Transformer($config, $shortcode->tag);
0 ignored issues
show
Bug introduced by
Accessing tag on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
63
        foreach ($transformer as $item) {
64
            $slug = $item['control']['slug'] ?? '';
65
            if (empty($slug) || !array_key_exists($slug, $defaults)) {
66
                continue;
67
            }
68
            $path = "content.{$item['path']}";
69
            $properties[$path] = $defaults[$slug]['default'];
70
        }
71
        if (empty($properties)) {
72
            return false;
73
        }
74
        return Arr::unflatten($defaults);
75
    }
76
77
    /**
78
     * Return an array of saved keyed values.
79
     */
80
    public static function ssrArgs(array $data): array
81
    {
82
        $args = [];
83
        $arrayIterator = new \RecursiveArrayIterator($data);
84
        $iterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
85
        $keys = array_keys(static::bdShortcode()->settings());
86
        $mappedKeys = [
87
            'attr_class' => 'class',
88
            'attr_id' => 'id',
89
        ];
90
        foreach ($iterator as $key => $value) {
91
            if (array_key_exists($key, $mappedKeys)) {
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type true; however, parameter $key of array_key_exists() does only seem to accept integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            if (array_key_exists(/** @scrutinizer ignore-type */ $key, $mappedKeys)) {
Loading history...
92
                $args[$mappedKeys[$key]] = $value;
93
                continue;
94
            }
95
            if (in_array($key, $mappedKeys)) {
96
                continue;
97
            }
98
            if (in_array($key, $keys)) {
99
                $args[$key] = $value;
100
            }
101
        }
102
        $replacements = [ // the post_chooser control requires integer keys
103
            -10 => 'post_id',
104
            -20 => 'parent_id',
105
            -30 => 'user_id',
106
            -40 => 'author_id',
107
            -50 => 'profile_id',
108
        ];
109
        foreach ($args as $key => $value) {
110
            if (!is_array($value)) {
111
                continue;
112
            }
113
            if (wp_is_numeric_array($value)) {
114
                $args[$key] = array_map(fn ($id) => $replacements[$id] ?? $id, $value);
115
            } else {
116
                $args[$key] = array_keys(array_filter($value));
117
            }
118
        }
119
        $args = glsr()->filterArray('breakdance/ssr', $args, $data, static::bdShortcode()->tag);
0 ignored issues
show
Bug introduced by
Accessing tag on the interface GeminiLabs\SiteReviews\Contracts\ShortcodeContract suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
120
        return $args;
121
    }
122
}
123