1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Widgets; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\WidgetBuilder; |
8
|
|
|
use WP_Widget; |
9
|
|
|
|
10
|
|
|
abstract class Widget extends WP_Widget |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $mapped = [ // @compat 4.0 |
16
|
|
|
'assign_to' => 'assigned_posts', |
17
|
|
|
'assigned_to' => 'assigned_posts', |
18
|
|
|
'category' => 'assigned_terms', |
19
|
|
|
'per_page' => 'display', |
20
|
|
|
'user' => 'assigned_users', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $widgetArgs; |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$className = (new \ReflectionClass($this))->getShortName(); |
31
|
|
|
$className = str_replace('Widget', '', $className); |
32
|
|
|
$baseId = glsr()->prefix.Str::dashCase($className); |
33
|
|
|
parent::__construct($baseId, $this->widgetName(), $this->widgetOptions()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $args |
38
|
|
|
* @param array $instance |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function widget($args, $instance) |
42
|
|
|
{ |
43
|
|
|
echo $this->shortcode()->build($instance, $args, 'widget'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $key |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
protected function mapped($key) |
51
|
|
|
{ |
52
|
|
|
$key = Arr::get($this->mapped, $key, $key); |
53
|
|
|
return Arr::get($this->widgetArgs, $key); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $tag |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
protected function normalizeFieldAttributes($tag, array $args) |
61
|
|
|
{ |
62
|
|
|
if (empty($args['value'])) { |
63
|
|
|
$args['value'] = $this->mapped($args['name']); |
64
|
|
|
} |
65
|
|
|
if (empty($this->mapped('options')) && in_array($tag, ['checkbox', 'radio'])) { |
66
|
|
|
$args['checked'] = in_array($args['value'], (array) $this->mapped($args['name'])); |
67
|
|
|
} |
68
|
|
|
$args['id'] = $this->get_field_id($args['name']); |
69
|
|
|
$args['name'] = $this->get_field_name($args['name']); |
70
|
|
|
return $args; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $tag |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected function renderField($tag, array $args = []) |
78
|
|
|
{ |
79
|
|
|
$args = $this->normalizeFieldAttributes($tag, $args); |
80
|
|
|
echo glsr(WidgetBuilder::class)->p([ |
81
|
|
|
'text' => glsr(WidgetBuilder::class)->$tag($args), |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return \GeminiLabs\SiteReviews\Shortcodes\Shortcode |
87
|
|
|
*/ |
88
|
|
|
abstract protected function shortcode(); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function widgetDescription() |
94
|
|
|
{ |
95
|
|
|
return ''; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function widgetName() |
102
|
|
|
{ |
103
|
|
|
return _x('Site Reviews: Unknown Widget', 'admin-text', 'site-reviews'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
protected function widgetOptions() |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
'description' => $this->widgetDescription(), |
113
|
|
|
'name' => $this->widgetName(), |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|