|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\WPBakery; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Integrations\WPBakery\Defaults\ControlDefaults; |
|
8
|
|
|
|
|
9
|
|
|
class Transformer |
|
10
|
|
|
{ |
|
11
|
|
|
public string $name; |
|
12
|
|
|
public string $shortcode; |
|
13
|
|
|
|
|
14
|
|
|
protected array $control; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(string $name, array $control, string $shortcode = '') |
|
17
|
|
|
{ |
|
18
|
|
|
$this->name = $name; |
|
19
|
|
|
$this->shortcode = $shortcode; |
|
20
|
|
|
$control = wp_parse_args(compact('name'), $control); |
|
21
|
|
|
$control = glsr(ControlDefaults::class)->merge($control); |
|
22
|
|
|
$method = Helper::buildMethodName('transform', $control['type']); |
|
23
|
|
|
$this->control = method_exists($this, $method) |
|
24
|
|
|
? call_user_func([$this, $method], $control) |
|
25
|
|
|
: $control; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function control(): array |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->control; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function transformCheckbox(array $control): array |
|
34
|
|
|
{ |
|
35
|
|
|
if ('hide' === $this->name) { |
|
36
|
|
|
$control['heading'] = _x('Hide', 'admin-text', 'site-reviews'); |
|
37
|
|
|
} |
|
38
|
|
|
if (!empty($control['options'])) { |
|
39
|
|
|
$control['value'] = array_flip($control['options']); |
|
40
|
|
|
} elseif (empty($control['value'])) { |
|
41
|
|
|
$control['value'] = [esc_html_x('Yes', 'admin-text', 'site-reviews') => 'true']; |
|
42
|
|
|
} |
|
43
|
|
|
unset($control['options']); |
|
44
|
|
|
return $control; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function transformNumber(array $control): array |
|
48
|
|
|
{ |
|
49
|
|
|
$control['type'] = 'glsr_type_range'; |
|
50
|
|
|
return $control; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function transformSelect(array $control): array |
|
54
|
|
|
{ |
|
55
|
|
|
$control['type'] = 'dropdown'; |
|
56
|
|
|
if (!isset($values['options']) && !isset($values['value'])) { |
|
|
|
|
|
|
57
|
|
|
$control['type'] = 'autocomplete'; |
|
58
|
|
|
$control['settings'] = [ |
|
59
|
|
|
'multiple' => true, |
|
60
|
|
|
'sortable' => true, |
|
61
|
|
|
]; |
|
62
|
|
|
return $control; |
|
63
|
|
|
} |
|
64
|
|
|
if ($options = Arr::consolidate($control['value'] ?? [])) { |
|
|
|
|
|
|
65
|
|
|
unset($control['options']); |
|
66
|
|
|
return $control; |
|
67
|
|
|
} |
|
68
|
|
|
if ($options = Arr::consolidate($control['options'] ?? [])) { |
|
69
|
|
|
if (!empty($control['placeholder'])) { |
|
70
|
|
|
$options = ['' => $control['placeholder']] + $options; |
|
71
|
|
|
} |
|
72
|
|
|
$control['value'] = array_flip($options); |
|
73
|
|
|
unset($control['options']); |
|
74
|
|
|
return $control; |
|
75
|
|
|
} |
|
76
|
|
|
return []; // invalid control |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function transformText(array $control): array |
|
80
|
|
|
{ |
|
81
|
|
|
$control['type'] = 'textfield'; |
|
82
|
|
|
return $control; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|