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); |
|
|
|
|
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); |
|
|
|
|
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)) { |
|
|
|
|
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); |
|
|
|
|
120
|
|
|
return $args; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|