Passed
Push — develop ( 0b5f25...28eec5 )
by Paul
13:25
created

ElementControlsTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 17
eloc 58
c 2
b 0
f 2
dl 0
loc 120
ccs 0
cts 63
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bdShortcode() 0 7 2
A contentControls() 0 5 1
B ssrArgs() 0 42 8
A designControls() 0 5 1
A defaultProperties() 0 21 5
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
        $shortcode = static::bdShortcode();
28
        $transformer = new Transformer('content', $shortcode->settings(), $shortcode->tag);
29
        return $transformer->controls();
30
    }
31
32
    /**
33
     * This must return false if the element has no default properties
34
     * otherwise SSR will not trigger when control values are changed.
35
     *
36
     * return [
37
     *     'content' => [
38
     *         'general' => [
39
     *             'rating' => 0,
40
     *         ],
41
     *     ],
42
     *     'design' => [
43
     *         'general' => [
44
     *             'preset' => 0,
45
     *         ],
46
     *     ],
47
     * ];
48
     * 
49
     * @return array|false
50
     */
51
    public static function defaultProperties()
52
    {
53
        $properties = [];
54
        $shortcode = static::bdShortcode();
55
        $config = $shortcode->settings();
56
        $defaults = array_filter($config, fn ($args) => isset($args['default']));
57
        $transformer = new Transformer('content', $config, $shortcode->tag);
58
        foreach ($transformer as $item) {
59
            $slug = $item['control']['slug'] ?? '';
60
            if (empty($slug) || !array_key_exists($slug, $defaults)) {
61
                continue;
62
            }
63
            $path = "content.{$item['path']}";
64
            $properties[$path] = $defaults[$slug]['default'];
65
        }
66
        $properties = Arr::unflatten($properties);
67
        $properties = glsr()->filterArray('breakdance/default-properties', $properties, $shortcode);
68
        if (empty($properties)) {
69
            return false;
70
        }
71
        return $properties;
72
    }
73
74
    /**
75
     * @return array[]
76
     */
77
    public static function designControls()
78
    {
79
        $shortcode = static::bdShortcode();
80
        $transformer = new Transformer('design', [], $shortcode->tag);
81
        return $transformer->controls();
82
    }
83
84
    /**
85
     * Return an array of saved keyed values.
86
     */
87
    public static function ssrArgs(array $data): array
88
    {
89
        $args = [];
90
        $arrayIterator = new \RecursiveArrayIterator($data);
91
        $iterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
92
        $keys = array_keys(static::bdShortcode()->settings());
93
        $mappedKeys = [
94
            'attr_class' => 'class',
95
            'attr_id' => 'id',
96
        ];
97
        foreach ($iterator as $key => $value) {
98
            if (array_key_exists((string) $key, $mappedKeys)) {
99
                $args[$mappedKeys[$key]] = $value;
100
                continue;
101
            }
102
            if (in_array($key, $mappedKeys)) {
103
                continue;
104
            }
105
            if (in_array($key, $keys)) {
106
                $args[$key] = $value;
107
            }
108
        }
109
        $replacements = [ // the post_chooser control requires integer keys
110
            -10 => 'post_id',
111
            -20 => 'parent_id',
112
            -30 => 'user_id',
113
            -40 => 'author_id',
114
            -50 => 'profile_id',
115
        ];
116
        foreach ($args as $key => $value) {
117
            if (!is_array($value)) {
118
                continue;
119
            }
120
            if (wp_is_numeric_array($value)) {
121
                $args[$key] = array_map(fn ($id) => $replacements[$id] ?? $id, $value);
122
            } else {
123
                $args[$key] = array_keys(array_filter($value));
124
            }
125
        }
126
        $shortcode = static::bdShortcode();
127
        $args = glsr()->filterArray('breakdance/ssr', $args, $data, $shortcode);
128
        return $args;
129
    }
130
}
131