author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Widgets Administration API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Display list of the available widgets. |
|
11 |
* |
|
12 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* @global array $wp_registered_widget_controls |
0 | 16 |
*/ |
17 |
function wp_list_widgets() { |
|
5 | 18 |
global $wp_registered_widgets, $wp_registered_widget_controls; |
0 | 19 |
|
20 |
$sort = $wp_registered_widgets; |
|
21 |
usort( $sort, '_sort_name_callback' ); |
|
22 |
$done = array(); |
|
23 |
||
24 |
foreach ( $sort as $widget ) { |
|
25 |
if ( in_array( $widget['callback'], $done, true ) ) // We already showed this multi-widget |
|
26 |
continue; |
|
27 |
||
28 |
$sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false ); |
|
29 |
$done[] = $widget['callback']; |
|
30 |
||
31 |
if ( ! isset( $widget['params'][0] ) ) |
|
32 |
$widget['params'][0] = array(); |
|
33 |
||
34 |
$args = array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' ); |
|
35 |
||
36 |
if ( isset($wp_registered_widget_controls[$widget['id']]['id_base']) && isset($widget['params'][0]['number']) ) { |
|
37 |
$id_base = $wp_registered_widget_controls[$widget['id']]['id_base']; |
|
38 |
$args['_temp_id'] = "$id_base-__i__"; |
|
39 |
$args['_multi_num'] = next_widget_id_number($id_base); |
|
40 |
$args['_add'] = 'multi'; |
|
41 |
} else { |
|
42 |
$args['_add'] = 'single'; |
|
43 |
if ( $sidebar ) |
|
44 |
$args['_hide'] = '1'; |
|
45 |
} |
|
46 |
||
47 |
$args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) ); |
|
48 |
call_user_func_array( 'wp_widget_control', $args ); |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
/** |
|
53 |
* Callback to sort array by a 'name' key. |
|
54 |
* |
|
55 |
* @since 3.1.0 |
|
56 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* @return int |
0 | 59 |
*/ |
60 |
function _sort_name_callback( $a, $b ) { |
|
61 |
return strnatcasecmp( $a['name'], $b['name'] ); |
|
62 |
} |
|
63 |
||
64 |
/** |
|
65 |
* Show the widgets and their settings for a sidebar. |
|
66 |
* Used in the admin widget config screen. |
|
67 |
* |
|
68 |
* @since 2.5.0 |
|
69 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* @param string $sidebar Sidebar ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* @param string $sidebar_name Optional. Sidebar name. Default empty. |
0 | 72 |
*/ |
5 | 73 |
function wp_list_widget_controls( $sidebar, $sidebar_name = '' ) { |
0 | 74 |
add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' ); |
75 |
||
76 |
$description = wp_sidebar_description( $sidebar ); |
|
77 |
||
5 | 78 |
echo '<div id="' . esc_attr( $sidebar ) . '" class="widgets-sortables">'; |
79 |
||
80 |
if ( $sidebar_name ) { |
|
81 |
?> |
|
82 |
<div class="sidebar-name"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
<button type="button" class="handlediv hide-if-no-js" aria-expanded="true"> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
<span class="screen-reader-text"><?php echo esc_html( $sidebar_name ); ?></span> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
<span class="toggle-indicator" aria-hidden="true"></span> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
</button> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
<h2><?php echo esc_html( $sidebar_name ); ?> <span class="spinner"></span></h2> |
5 | 88 |
</div> |
89 |
<?php |
|
0 | 90 |
} |
91 |
||
5 | 92 |
if ( ! empty( $description ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
<div class="sidebar-description"> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
<p class="description"><?php echo $description; ?></p> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
</div> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
<?php |
5 | 98 |
} |
99 |
||
0 | 100 |
dynamic_sidebar( $sidebar ); |
5 | 101 |
|
102 |
echo '</div>'; |
|
0 | 103 |
} |
104 |
||
105 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
* Retrieves the widget control arguments. |
0 | 107 |
* |
108 |
* @since 2.5.0 |
|
109 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
* @staticvar int $i |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
* |
0 | 114 |
* @param array $params |
115 |
* @return array |
|
116 |
*/ |
|
117 |
function wp_list_widget_controls_dynamic_sidebar( $params ) { |
|
118 |
global $wp_registered_widgets; |
|
119 |
static $i = 0; |
|
120 |
$i++; |
|
121 |
||
122 |
$widget_id = $params[0]['widget_id']; |
|
123 |
$id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id; |
|
124 |
$hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : ''; |
|
125 |
||
126 |
$params[0]['before_widget'] = "<div id='widget-{$i}_{$id}' class='widget'$hidden>"; |
|
127 |
$params[0]['after_widget'] = "</div>"; |
|
128 |
$params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated |
|
129 |
$params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated |
|
130 |
if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) { |
|
131 |
$wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback']; |
|
132 |
$wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control'; |
|
133 |
} |
|
134 |
||
135 |
return $params; |
|
136 |
} |
|
137 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* @param string $id_base |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
* @return int |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
function next_widget_id_number( $id_base ) { |
0 | 146 |
global $wp_registered_widgets; |
147 |
$number = 1; |
|
148 |
||
149 |
foreach ( $wp_registered_widgets as $widget_id => $widget ) { |
|
150 |
if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) ) |
|
151 |
$number = max($number, $matches[1]); |
|
152 |
} |
|
153 |
$number++; |
|
154 |
||
155 |
return $number; |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Meta widget used to display the control form for a widget. |
|
160 |
* |
|
161 |
* Called from dynamic_sidebar(). |
|
162 |
* |
|
163 |
* @since 2.5.0 |
|
164 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* @global array $wp_registered_widget_controls |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* @global array $sidebars_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
* |
0 | 169 |
* @param array $sidebar_args |
170 |
* @return array |
|
171 |
*/ |
|
172 |
function wp_widget_control( $sidebar_args ) { |
|
173 |
global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets; |
|
174 |
||
175 |
$widget_id = $sidebar_args['widget_id']; |
|
176 |
$sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false; |
|
177 |
$key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar |
|
178 |
$control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array(); |
|
179 |
$widget = $wp_registered_widgets[$widget_id]; |
|
180 |
||
181 |
$id_format = $widget['id']; |
|
182 |
$widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : ''; |
|
183 |
$id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id; |
|
184 |
$multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : ''; |
|
185 |
$add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : ''; |
|
186 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
187 |
$before_form = isset( $sidebar_args['before_form'] ) ? $sidebar_args['before_form'] : '<form method="post">'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
$after_form = isset( $sidebar_args['after_form'] ) ? $sidebar_args['after_form'] : '</form>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
$before_widget_content = isset( $sidebar_args['before_widget_content'] ) ? $sidebar_args['before_widget_content'] : '<div class="widget-content">'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
$after_widget_content = isset( $sidebar_args['after_widget_content'] ) ? $sidebar_args['after_widget_content'] : '</div>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
|
0 | 192 |
$query_arg = array( 'editwidget' => $widget['id'] ); |
193 |
if ( $add_new ) { |
|
194 |
$query_arg['addnew'] = 1; |
|
195 |
if ( $multi_number ) { |
|
196 |
$query_arg['num'] = $multi_number; |
|
197 |
$query_arg['base'] = $id_base; |
|
198 |
} |
|
199 |
} else { |
|
200 |
$query_arg['sidebar'] = $sidebar_id; |
|
201 |
$query_arg['key'] = $key; |
|
202 |
} |
|
203 |
||
5 | 204 |
/* |
205 |
* We aren't showing a widget control, we're outputting a template |
|
206 |
* for a multi-widget control. |
|
207 |
*/ |
|
0 | 208 |
if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) { |
209 |
// number == -1 implies a template where id numbers are replaced by a generic '__i__' |
|
210 |
$control['params'][0]['number'] = -1; |
|
5 | 211 |
// With id_base widget id's are constructed like {$id_base}-{$id_number}. |
0 | 212 |
if ( isset($control['id_base']) ) |
213 |
$id_format = $control['id_base'] . '-__i__'; |
|
214 |
} |
|
215 |
||
216 |
$wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback']; |
|
217 |
unset($wp_registered_widgets[$widget_id]['_callback']); |
|
218 |
||
219 |
$widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) ); |
|
220 |
$has_form = 'noform'; |
|
221 |
||
222 |
echo $sidebar_args['before_widget']; ?> |
|
223 |
<div class="widget-top"> |
|
224 |
<div class="widget-title-action"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
<button type="button" class="widget-action hide-if-no-js" aria-expanded="false"> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
<span class="screen-reader-text"><?php printf( __( 'Edit widget: %s' ), $widget_title ); ?></span> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
<span class="toggle-indicator" aria-hidden="true"></span> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
</button> |
0 | 229 |
<a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>"> |
230 |
<span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span> |
|
231 |
<span class="add"><?php _ex( 'Add', 'widget' ); ?></span> |
|
232 |
<span class="screen-reader-text"><?php echo $widget_title; ?></span> |
|
233 |
</a> |
|
234 |
</div> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
<div class="widget-title"><h3><?php echo $widget_title; ?><span class="in-widget-title"></span></h3></div> |
0 | 236 |
</div> |
237 |
||
238 |
<div class="widget-inside"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
<?php echo $before_form; ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
<?php echo $before_widget_content; ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
if ( isset( $control['callback'] ) ) { |
0 | 243 |
$has_form = call_user_func_array( $control['callback'], $control['params'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
<?php echo $after_widget_content; ?> |
0 | 249 |
<input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" /> |
250 |
<input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" /> |
|
251 |
<input type="hidden" name="widget-width" class="widget-width" value="<?php if (isset( $control['width'] )) echo esc_attr($control['width']); ?>" /> |
|
252 |
<input type="hidden" name="widget-height" class="widget-height" value="<?php if (isset( $control['height'] )) echo esc_attr($control['height']); ?>" /> |
|
253 |
<input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" /> |
|
254 |
<input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" /> |
|
255 |
<input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" /> |
|
256 |
||
257 |
<div class="widget-control-actions"> |
|
258 |
<div class="alignleft"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
<button type="button" class="button-link button-link-delete widget-control-remove"><?php _e( 'Delete' ); ?></button> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
<span class="widget-control-close-wrapper"> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
| |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
<button type="button" class="button-link widget-control-close"><?php _e( 'Done' ); ?></button> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
</span> |
0 | 264 |
</div> |
265 |
<div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
<?php submit_button( __( 'Save' ), 'primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $id_format ) . '-savewidget' ) ); ?> |
0 | 267 |
<span class="spinner"></span> |
268 |
</div> |
|
269 |
<br class="clear" /> |
|
270 |
</div> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
<?php echo $after_form; ?> |
0 | 272 |
</div> |
273 |
||
274 |
<div class="widget-description"> |
|
275 |
<?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?> |
|
276 |
</div> |
|
277 |
<?php |
|
278 |
echo $sidebar_args['after_widget']; |
|
5 | 279 |
|
0 | 280 |
return $sidebar_args; |
281 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
* @param string $classes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* @return string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
function wp_widgets_access_body_class($classes) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
return "$classes widgets_access "; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
} |