Passed
Push — develop ( 70fb5e...391f8f )
by Paul
13:21
created

ElementControlsTrait::contentControls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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