|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Amarkal\Widget; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AbstractWidget extends \WP_Widget |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var array The configuration array |
|
9
|
|
|
*/ |
|
10
|
|
|
private $config; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \Amarkal\UI\Form Amarkal UI for data processing |
|
14
|
|
|
*/ |
|
15
|
|
|
private $form; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get the user config and call the parent constructor |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$config = $this->get_config(); |
|
23
|
|
|
|
|
24
|
|
|
parent::__construct( |
|
25
|
|
|
$config['id'], |
|
26
|
|
|
$config['name'], |
|
27
|
|
|
$config['widget_options'], |
|
28
|
|
|
$config['control_options'] |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Generates the administration form for the widget |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $instance The array of keys and values for the widget |
|
36
|
|
|
*/ |
|
37
|
|
|
public function form( $instance ) |
|
38
|
|
|
{ |
|
39
|
|
|
$form = $this->get_form(); |
|
40
|
|
|
$cl = $form->get_component_list(); |
|
41
|
|
|
|
|
42
|
|
|
$form->update($instance); |
|
43
|
|
|
|
|
44
|
|
|
// Set the widget-specific names and ids |
|
45
|
|
|
foreach( $cl->get_value_components() as $component ) |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$component->original_name = $component->name; |
|
48
|
|
|
$component->id = $this->get_field_id($component->name); |
|
49
|
|
|
$component->name = $this->get_field_name($component->name); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
include __DIR__.'/Form.phtml'; |
|
53
|
|
|
|
|
54
|
|
|
// Use the original names again (for when the components update) |
|
55
|
|
|
foreach( $cl->get_value_components() as $component ) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$component->id = $component->original_name; |
|
58
|
|
|
$component->name = $component->original_name; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Process the widget's options before they are saved into the db |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $new_instance The previous instance of values before the update. |
|
66
|
|
|
* @param array $old_instance The new instance of values to be generated via the update. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function update( $new_instance, $old_instance ) |
|
69
|
|
|
{ |
|
70
|
|
|
$form = $this->get_form(); |
|
71
|
|
|
return $form->update($new_instance, $old_instance); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the Amarkal UI form. |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Amarkal\UI\Form |
|
78
|
|
|
*/ |
|
79
|
|
|
private function get_form() |
|
80
|
|
|
{ |
|
81
|
|
|
if( !isset($this->form) ) |
|
82
|
|
|
{ |
|
83
|
|
|
$config = $this->get_config(); |
|
84
|
|
|
$this->form = new \Amarkal\UI\Form( |
|
85
|
|
|
new \Amarkal\UI\ComponentList($config['fields']) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
return $this->form; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the default widget configuration. |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
private function default_config() |
|
97
|
|
|
{ |
|
98
|
|
|
return array( |
|
99
|
|
|
'id' => null, |
|
100
|
|
|
'name' => null, |
|
101
|
|
|
'widget_options' => array(), |
|
102
|
|
|
'control_options' => array(), |
|
103
|
|
|
'fields' => array() |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the configuraiton array, merging between the user and the default |
|
109
|
|
|
* configuration values. |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
* @throws \RuntimeException if the configuration is missing the ID argument |
|
113
|
|
|
*/ |
|
114
|
|
|
private function get_config() |
|
115
|
|
|
{ |
|
116
|
|
|
if( !isset($this->config) ) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->config = array_merge( |
|
119
|
|
|
$this->default_config(), |
|
120
|
|
|
$this->config() |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
if( null === $this->config['id'] ) |
|
124
|
|
|
{ |
|
125
|
|
|
throw new \RuntimeException('No \'id\' was sepcified in the widget configuration'); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
return $this->config; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* The user configuration array. Must be implemented in the child class. |
|
133
|
|
|
*/ |
|
134
|
|
|
abstract public function config(); |
|
135
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.