This repository has been archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget_base.php
224 lines (210 loc) · 6.49 KB
/
widget_base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Contains a widget base class for handling configurable widgets.
*
* PHP Version 5.3+
*
* @category Wordpress
* @package Widgets
* @author Kevin Fodness <[email protected]
* @license GPLv3
* @link http://www.kevinfodness.com
*/
/**
* A widget base class for handling configurable widgets.
*
* @category Wordpress
* @package Widgets
* @author Kevin Fodness <[email protected]
* @license GPLv3
* @link http://www.kevinfodness.com
*/
class Widget_Base extends WP_Widget
{
/**
* Fields listing. To be overriden by child class.
*
* @access protected
* @var array
*/
protected $fields = array();
/**
* Constructor function. Registers the widget by calling the parent constructor.
*
* @param string $id The ID of the widget.
* @param string $name The name of the widget.
* @param string $description The description for the widget.
*
* @access public
* @return void
*/
public function __construct($id, $name, $description)
{
parent::__construct(
$id,
__($name, 'text_domain'),
array('description' => __($description, 'text_domain'))
);
/* Add action hooks. */
add_action('admin_print_scripts', array($this, 'admin_print_scripts'));
add_action('admin_print_styles', array($this, 'admin_print_styles'));
}
/**
* Function to retrieve form data values from the instance based on defined fields.
*
* @param array $instance The widget instance from WP.
*
* @access protected
* @return array
*/
protected function get_data($instance)
{
$data = array();
foreach ($this->fields as $key => $field) {
/* Construct field data from instance data for each field. */
$data[$key]['title'] = ucwords(str_replace('-', ' ', $key)) . ':';
$data[$key]['value'] = (isset($instance[$key])) ? esc_attr($instance[$key]) : '';
$data[$key]['field_id'] = $this->get_field_id($key);
$data[$key]['field_name'] = $this->get_field_name($key);
/* Sanitize field value. */
switch ($field['type']) {
case 'select':
case 'text':
$data[$key]['value'] = esc_attr($data[$key]['value']);
break;
case 'textarea':
$data[$key]['value'] = esc_textarea($data[$key]['value']);
break;
case 'url':
$data[$key]['value'] = esc_url($data[$key]['value']);
break;
}
}
return $data;
}
/**
* A function to print a field on the edit form.
*
* @param string $key The key from the fields definition for this field.
* @param array $data The data array for this field.
*
* @access protected
* @return void
*/
protected function print_field($key, $data)
{
/* Switch to construct HTML for the field itself. */
switch ($this->fields[$key]['type']) {
case 'select':
$options = '';
foreach ($this->fields[$key]['values'] as $value => $name) {
$options .= '<option value="' . esc_attr($value) . '"';
if ($data['value'] === esc_attr($value)) {
$options .= ' selected="selected"';
}
$options .= '>' . esc_attr($name) . '</option>';
}
$field = <<<HTML
<select id="{$data['field_id']}" name="{$data['field_name']}">
<option value="">-- Select --</option>
{$options}
</select>
HTML;
break;
case 'text':
case 'url':
$field = <<<HTML
<input id="{$data['field_id']}" name="{$data['field_name']}" type="text" value="{$data['value']}" />
HTML;
break;
case 'textarea':
$field = <<<HTML
<textarea id="{$data['field_id']}" name="{$data['field_name']}">{$data['value']}</textarea>
HTML;
break;
case 'file':
$field = <<<HTML
<input id="{$data['field_id']}" name="{$data['field_name']}" type="hidden" value="{$data['value']}" />
HTML;
if (!empty($data['value'])) {
$media = wp_get_attachment_image($data['value']);
$field .= <<<HTML
{$media}<br />
<a href="#" class="widget-base-remove-media">Remove media</a>
HTML;
} else {
$field .= <<<HTML
<input class="button widget-base-add-media" type="button" value="Add Media" />
HTML;
}
}
/* Print the field. */
echo <<<HTML
<p>
<label for="{$data['field_id']}">{$data['title']}</label><br/>
{$field}
</p>
HTML;
}
/**
* Action hook function to enqueue admin scripts used by this widget.
*
* @access public
* @return void
*/
public function admin_print_scripts()
{
/* Compute URL of supplemental JS file. */
$js_url = get_template_directory_uri();
$template_base = substr($js_url, strpos($js_url, '/', 8));
$js_url .= substr(__DIR__, strpos(__DIR__, $template_base) + strlen($template_base)) . '/widget_base.js';
/* Register and enqueue scripts. */
wp_enqueue_media();
wp_register_script('widget-base', $js_url);
wp_enqueue_script('widget-base');
}
/**
* Action hook function to enqueue admin styles used by this widget.
*
* @access public
* @return void
*/
public function admin_print_styles()
{
wp_enqueue_style('thickbox');
}
/**
* A function to print the edit form on the back-end.
*
* @param array $instance The instance variables, as reported by WP.
*
* @access public
* @return void
*/
public function form($instance)
{
$data = $this->get_data($instance);
foreach ($data as $key => $field) {
$this->print_field($key, $field);
}
}
/**
* A function to process and sanitize inputs on save.
*
* @param array $new_instance The new save data.
* @param array $old_instance The old save data.
*
* @access public
* @return array The modified instance data.
*/
public function update($new_instance, $old_instance)
{
$instance = array();
foreach ($this->fields as $key => $field) {
if (!empty($new_instance[$key])) {
$instance[$key] = strip_tags($new_instance[$key]);
}
}
return $instance;
}
}