1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Widgets; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
6
|
|
|
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract; |
7
|
|
|
use GeminiLabs\SiteReviews\Database; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\WidgetBuilder; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\WidgetField; |
12
|
|
|
|
13
|
|
|
abstract class Widget extends \WP_Widget |
14
|
|
|
{ |
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$className = (new \ReflectionClass($this))->getShortName(); |
18
|
|
|
$className = str_replace('Widget', '', $className); |
19
|
|
|
$baseId = glsr()->prefix.Str::dashCase($className); |
20
|
|
|
parent::__construct($baseId, $this->shortcode()->name, [ |
21
|
|
|
'description' => sprintf('%s: %s', glsr()->name, $this->shortcode()->description), |
22
|
|
|
'name' => $this->shortcode()->name, |
23
|
|
|
'show_instance_in_rest' => true, |
24
|
|
|
]); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $instance |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function form($instance) |
33
|
|
|
{ |
34
|
|
|
$instance = $this->normalizeInstance($instance); |
35
|
|
|
$notice = _x('This is a legacy widget with limited options, consider switching to the shortcode or block.', 'admin-text', 'site-reviews'); |
36
|
|
|
echo glsr(WidgetBuilder::class)->div([ |
37
|
|
|
'class' => 'notice notice-alt notice-warning inline', |
38
|
|
|
'style' => 'margin:1em 0;', |
39
|
|
|
'text' => glsr(WidgetBuilder::class)->p($notice), |
40
|
|
|
]); |
41
|
|
|
$config = wp_parse_args($this->widgetConfig(), [ |
42
|
|
|
'title' => [ |
43
|
|
|
'label' => _x('Widget Title', 'admin-text', 'site-reviews'), |
44
|
|
|
'type' => 'text', |
45
|
|
|
], |
46
|
|
|
]); |
47
|
|
|
foreach ($config as $name => $args) { |
48
|
|
|
if ('type' === $name && empty($args['options'])) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
$this->renderField($name, $args, $instance); |
52
|
|
|
} |
53
|
|
|
return ''; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $oldInstance |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function update($instance, $oldInstance) |
62
|
|
|
{ |
63
|
|
|
return $this->normalizeInstance($instance); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $args |
68
|
|
|
* @param array $instance |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function widget($args, $instance) |
73
|
|
|
{ |
74
|
|
|
$shortcode = $this->shortcode(); |
75
|
|
|
$args = $this->normalizeArgs($args); |
76
|
|
|
$html = $shortcode->build($instance, 'widget'); |
77
|
|
|
$title = !empty($shortcode->args['title']) |
78
|
|
|
? $args->before_title.$shortcode->args['title'].$args->after_title |
79
|
|
|
: ''; |
80
|
|
|
echo $args->before_widget.$title.$html.$args->after_widget; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function fieldAssignedTermsOptions(): array |
84
|
|
|
{ |
85
|
|
|
return Arr::prepend( |
86
|
|
|
glsr(Database::class)->terms(), |
87
|
|
|
esc_html_x('— Select —', 'admin-text', 'site-reviews'), |
88
|
|
|
'' |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function fieldTypeOptions(): array |
93
|
|
|
{ |
94
|
|
|
$types = glsr()->retrieveAs('array', 'review_types'); |
95
|
|
|
if (count($types) > 1) { |
96
|
|
|
return $types; |
97
|
|
|
} |
98
|
|
|
return []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array|string $args |
103
|
|
|
*/ |
104
|
|
|
protected function normalizeArgs($args): Arguments |
105
|
|
|
{ |
106
|
|
|
$args = wp_parse_args($args, [ |
107
|
|
|
'before_widget' => '', |
108
|
|
|
'after_widget' => '', |
109
|
|
|
'before_title' => '<h2 class="glsr-title">', |
110
|
|
|
'after_title' => '</h2>', |
111
|
|
|
]); |
112
|
|
|
$args = glsr()->filterArray('widget/args', $args, $this->shortcode()->shortcode); |
113
|
|
|
return glsr()->args($args); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function normalizeInstance(array $instance): array |
117
|
|
|
{ |
118
|
|
|
$pairs = array_fill_keys(array_keys($instance), ''); |
119
|
|
|
$title = $instance['title'] ?? ''; |
120
|
|
|
$instance = $this->shortcode()->defaults()->merge($instance); |
|
|
|
|
121
|
|
|
$instance['title'] = sanitize_text_field($title); |
122
|
|
|
return shortcode_atts($pairs, $instance); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function renderField($name, array $args, array $instance = []): void |
126
|
|
|
{ |
127
|
|
|
if (isset($args['name']) && !isset($args['type'])) { |
128
|
|
|
$args['type'] = $name; // @todo remove in v8.0 |
129
|
|
|
} |
130
|
|
|
$field = new WidgetField(wp_parse_args($args, compact('name'))); |
131
|
|
|
if (!$field->isValid()) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
$value = Arr::get($instance, $field->original_name); |
135
|
|
|
if ('' !== $value) { |
136
|
|
|
$field->value = $value; |
137
|
|
|
} |
138
|
|
|
$field->id = $this->get_field_id($field->name); |
139
|
|
|
$field->name = $this->get_field_name($field->name); |
140
|
|
|
$field->render(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
abstract protected function shortcode(): ShortcodeContract; |
144
|
|
|
|
145
|
|
|
protected function widgetConfig(): array |
146
|
|
|
{ |
147
|
|
|
return []; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|