Test Failed
Push — develop ( c74868...df43c9 )
by Paul
09:13
created

ElementControlsTrait::defaultProperties()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 14
c 2
b 0
f 2
dl 0
loc 19
rs 9.4888
cc 5
nc 6
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
    abstract public static function bdShortcode(): ShortcodeContract;
12
13
    /**
14
     * @return array[]
15
     */
16
    public static function contentControls()
17
    {
18
        $controls = [];
19
        $settings = [];
20
        $shortcode = static::bdShortcode();
21
        $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...
22
        foreach ($transformer as $item) {
23
            $controls[$item['path']] = $item['control'];
24
        }
25
        $controls = Arr::unflatten(array_filter($controls));
26
        foreach ($controls as $slug => $children) {
27
            $section = $transformer->section($slug);
28
            foreach ($children as $key => $child) {
29
                if (!str_starts_with($key, 'popout_')) {
30
                    $section['children'][] = $child;
31
                    continue;
32
                }
33
                $slug = Str::removePrefix($key, 'popout_');
34
                $section['children'][] = $transformer->popout($slug, array_values($child));
35
            }
36
            $settings[] = $section;
37
        }
38
        return $settings;
39
    }
40
41
    /**
42
     * This must return false if the element has no default properties
43
     * otherwise SSR will not trigger when control values are changed.
44
     *
45
     * @return array|false
46
     */
47
    public static function defaultProperties()
48
    {
49
        $properties = [];
50
        $shortcode = static::bdShortcode();
51
        $config = $shortcode->settings();
52
        $defaults = array_filter($config, fn ($args) => isset($args['default']));
53
        $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...
54
        foreach ($transformer as $item) {
55
            $slug = $item['control']['slug'] ?? '';
56
            if (empty($slug) || !array_key_exists($slug, $defaults)) {
57
                continue;
58
            }
59
            $path = "content.{$item['path']}";
60
            $properties[$path] = $defaults[$slug]['default'];
61
        }
62
        if (empty($properties)) {
63
            return false;
64
        }
65
        return Arr::unflatten($defaults);
66
    }
67
68
    /**
69
     * Return an array of saved keyed values.
70
     */
71
    public static function ssrArgs(array $data): array
72
    {
73
        $args = [];
74
        $replacements = [ // the post_chooser control requires integer keys
75
            -10 => 'post_id',
76
            -20 => 'parent_id',
77
            -30 => 'user_id',
78
            -40 => 'author_id',
79
            -50 => 'profile_id',
80
        ];
81
        $arrayIterator = new \RecursiveArrayIterator($data);
82
        $iterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
83
        $keys = array_keys(static::bdShortcode()->settings());
84
        foreach ($iterator as $key => $value) {
85
            if (in_array($key, $keys)) {
86
                $args[$key] = $value;
87
            }
88
        }
89
        foreach ($args as $key => $value) {
90
            if (!is_array($value)) {
91
                continue;
92
            }
93
            if (wp_is_numeric_array($value)) {
94
                $args[$key] = array_map(fn ($id) => $replacements[$id] ?? $id, $value);
95
            } else {
96
                $args[$key] = array_keys(array_filter($value));
97
            }
98
        }
99
        $args = glsr()->filterArray('breakdance/ssr', $args, $data);
100
        return $args;
101
    }
102
}
103