]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/widgets.php
WordPress 4.3-scripts
[autoinstalls/wordpress.git] / wp-admin / widgets.php
1 <?php
2 /**
3  * Widgets administration panel.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /** WordPress Administration Bootstrap */
10 require_once( dirname( __FILE__ ) . '/admin.php' );
11
12 /** WordPress Administration Widgets API */
13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
14
15 if ( ! current_user_can('edit_theme_options') )
16         wp_die( __( 'Cheatin&#8217; uh?' ), 403 );
17
18 $widgets_access = get_user_setting( 'widgets_access' );
19 if ( isset($_GET['widgets-access']) ) {
20         $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
21         set_user_setting( 'widgets_access', $widgets_access );
22 }
23
24 /**
25  *
26  * @param string $classes
27  * @return string
28  */
29 function wp_widgets_access_body_class($classes) {
30         return "$classes widgets_access ";
31 }
32
33 if ( 'on' == $widgets_access ) {
34         add_filter( 'admin_body_class', 'wp_widgets_access_body_class' );
35 } else {
36         wp_enqueue_script('admin-widgets');
37
38         if ( wp_is_mobile() )
39                 wp_enqueue_script( 'jquery-touch-punch' );
40 }
41
42 /**
43  * Fires early before the Widgets administration screen loads,
44  * after scripts are enqueued.
45  *
46  * @since 2.2.0
47  */
48 do_action( 'sidebar_admin_setup' );
49
50 $title = __( 'Widgets' );
51 $parent_file = 'themes.php';
52
53 get_current_screen()->add_help_tab( array(
54 'id'            => 'overview',
55 'title'         => __('Overview'),
56 'content'       =>
57         '<p>' . __('Widgets are independent sections of content that can be placed into any widgetized area provided by your theme (commonly called sidebars). To populate your sidebars/widget areas with individual widgets, drag and drop the title bars into the desired area. By default, only the first widget area is expanded. To populate additional widget areas, click on their title bars to expand them.') . '</p>
58         <p>' . __('The Available Widgets section contains all the widgets you can choose from. Once you drag a widget into a sidebar, it will open to allow you to configure its settings. When you are happy with the widget settings, click the Save button and the widget will go live on your site. If you click Delete, it will remove the widget.') . '</p>'
59 ) );
60 get_current_screen()->add_help_tab( array(
61 'id'            => 'removing-reusing',
62 'title'         => __('Removing and Reusing'),
63 'content'       =>
64         '<p>' . __('If you want to remove the widget but save its setting for possible future use, just drag it into the Inactive Widgets area. You can add them back anytime from there. This is especially helpful when you switch to a theme with fewer or different widget areas.') . '</p>
65         <p>' . __('Widgets may be used multiple times. You can give each widget a title, to display on your site, but it&#8217;s not required.') . '</p>
66         <p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>'
67 ) );
68 get_current_screen()->add_help_tab( array(
69 'id'            => 'missing-widgets',
70 'title'         => __('Missing Widgets'),
71 'content'       =>
72         '<p>' . __('Many themes show some sidebar widgets by default until you edit your sidebars, but they are not automatically displayed in your sidebar management tool. After you make your first widget change, you can re-add the default widgets by adding them from the Available Widgets area.') . '</p>' .
73                 '<p>' . __('When changing themes, there is often some variation in the number and setup of widget areas/sidebars and sometimes these conflicts make the transition a bit less smooth. If you changed themes and seem to be missing widgets, scroll down on this screen to the Inactive Widgets area, where all of your widgets and their settings will have been saved.') . '</p>'
74 ) );
75
76 get_current_screen()->set_help_sidebar(
77         '<p><strong>' . __('For more information:') . '</strong></p>' .
78         '<p>' . __('<a href="https://codex.wordpress.org/Appearance_Widgets_Screen" target="_blank">Documentation on Widgets</a>') . '</p>' .
79         '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
80 );
81
82 if ( ! current_theme_supports( 'widgets' ) ) {
83         wp_die( __( 'The theme you are currently using isn&#8217;t widget-aware, meaning that it has no sidebars that you are able to change. For information on making your theme widget-aware, please <a href="https://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ) );
84 }
85
86 // These are the widgets grouped by sidebar
87 $sidebars_widgets = wp_get_sidebars_widgets();
88
89 if ( empty( $sidebars_widgets ) )
90         $sidebars_widgets = wp_get_widget_defaults();
91
92 foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
93         if ( 'wp_inactive_widgets' == $sidebar_id )
94                 continue;
95
96         if ( !isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
97                 if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar
98                         register_sidebar(array(
99                                 'name' => __( 'Inactive Sidebar (not used)' ),
100                                 'id' => $sidebar_id,
101                                 'class' => 'inactive-sidebar orphan-sidebar',
102                                 'description' => __( 'This sidebar is no longer available and does not show anywhere on your site. Remove each of the widgets below to fully remove this inactive sidebar.' ),
103                                 'before_widget' => '',
104                                 'after_widget' => '',
105                                 'before_title' => '',
106                                 'after_title' => '',
107                         ));
108                 } else {
109                         unset( $sidebars_widgets[ $sidebar_id ] );
110                 }
111         }
112 }
113
114 // register the inactive_widgets area as sidebar
115 register_sidebar(array(
116         'name' => __('Inactive Widgets'),
117         'id' => 'wp_inactive_widgets',
118         'class' => 'inactive-sidebar',
119         'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
120         'before_widget' => '',
121         'after_widget' => '',
122         'before_title' => '',
123         'after_title' => '',
124 ));
125
126 retrieve_widgets();
127
128 // We're saving a widget without js
129 if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
130         $widget_id = $_POST['widget-id'];
131         check_admin_referer("save-delete-widget-$widget_id");
132
133         $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
134         if ( $number ) {
135                 foreach ( $_POST as $key => $val ) {
136                         if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
137                                 $_POST[$key] = array( $number => array_shift($val) );
138                                 break;
139                         }
140                 }
141         }
142
143         $sidebar_id = $_POST['sidebar'];
144         $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
145
146         $id_base = $_POST['id_base'];
147         $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
148
149         // Delete.
150         if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
151
152                 if ( !in_array($widget_id, $sidebar, true) ) {
153                         wp_redirect( admin_url('widgets.php?error=0') );
154                         exit;
155                 }
156
157                 $sidebar = array_diff( $sidebar, array($widget_id) );
158                 $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
159         }
160
161         $_POST['widget-id'] = $sidebar;
162
163         foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
164                 if ( $name != $id_base || !is_callable($control['callback']) )
165                         continue;
166
167                 ob_start();
168                         call_user_func_array( $control['callback'], $control['params'] );
169                 ob_end_clean();
170
171                 break;
172         }
173
174         $sidebars_widgets[$sidebar_id] = $sidebar;
175
176         // Remove old position.
177         if ( !isset($_POST['delete_widget']) ) {
178                 foreach ( $sidebars_widgets as $key => $sb ) {
179                         if ( is_array($sb) )
180                                 $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
181                 }
182                 array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
183         }
184
185         wp_set_sidebars_widgets($sidebars_widgets);
186         wp_redirect( admin_url('widgets.php?message=0') );
187         exit;
188 }
189
190 // Output the widget form without js
191 if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
192         $widget_id = $_GET['editwidget'];
193
194         if ( isset($_GET['addnew']) ) {
195                 // Default to the first sidebar
196                 $keys = array_keys( $wp_registered_sidebars );
197                 $sidebar = reset( $keys );
198
199                 if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
200                         // Copy minimal info from an existing instance of this widget to a new instance
201                         foreach ( $wp_registered_widget_controls as $control ) {
202                                 if ( $_GET['base'] === $control['id_base'] ) {
203                                         $control_callback = $control['callback'];
204                                         $multi_number = (int) $_GET['num'];
205                                         $control['params'][0]['number'] = -1;
206                                         $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
207                                         $wp_registered_widget_controls[$control['id']] = $control;
208                                         break;
209                                 }
210                         }
211                 }
212         }
213
214         if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
215                 $control = $wp_registered_widget_controls[$widget_id];
216                 $control_callback = $control['callback'];
217         } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
218                 $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
219         }
220
221         if ( !isset($name) )
222                 $name = esc_html( strip_tags($control['name']) );
223
224         if ( !isset($sidebar) )
225                 $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
226
227         if ( !isset($multi_number) )
228                 $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
229
230         $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
231
232         // Show the widget form.
233         $width = ' style="width:' . max($control['width'], 350) . 'px"';
234         $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
235
236         require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
237         <div class="wrap">
238         <h1><?php echo esc_html( $title ); ?></h1>
239         <div class="editwidget"<?php echo $width; ?>>
240         <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
241
242         <form action="widgets.php" method="post">
243         <div class="widget-inside">
244 <?php
245         if ( is_callable( $control_callback ) )
246                 call_user_func_array( $control_callback, $control['params'] );
247         else
248                 echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
249         </div>
250
251         <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
252         <div class="widget-position">
253         <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
254 <?php
255         foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
256                 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
257                 if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
258                         echo '&nbsp;';
259                 } else {
260                         if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
261                                 $j = 1;
262                                 $sidebars_widgets[$sbname] = array();
263                         } else {
264                                 $j = count($sidebars_widgets[$sbname]);
265                                 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
266                                         $j++;
267                         }
268                         $selected = '';
269                         echo "\t\t<select name='{$sbname}_position'>\n";
270                         echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
271                         for ( $i = 1; $i <= $j; $i++ ) {
272                                 if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
273                                         $selected = selected( $i, $key + 1, false );
274                                 echo "\t\t<option value='$i'$selected> $i </option>\n";
275                         }
276                         echo "\t\t</select>\n";
277                 }
278                 echo "</td></tr>\n";
279         } ?>
280         </tbody></table>
281         </div>
282
283         <div class="widget-control-actions">
284 <?php
285         if ( isset($_GET['addnew']) ) { ?>
286         <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
287 <?php
288         } else {
289                 submit_button( __( 'Delete' ), 'button alignleft', 'removewidget', false );
290         }
291         submit_button( __( 'Save Widget' ), 'button-primary alignright', 'savewidget', false ); ?>
292         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
293         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
294         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
295 <?php   wp_nonce_field("save-delete-widget-$widget_id"); ?>
296         <br class="clear" />
297         </div>
298         </form>
299         </div>
300         </div>
301 <?php
302         require_once( ABSPATH . 'wp-admin/admin-footer.php' );
303         exit;
304 }
305
306 $messages = array(
307         __('Changes saved.')
308 );
309
310 $errors = array(
311         __('Error while saving.'),
312         __('Error in displaying the widget settings form.')
313 );
314
315 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
316
317 <div class="wrap">
318 <h1>
319 <?php
320         echo esc_html( $title );
321         if ( current_user_can( 'customize' ) ) {
322                 printf(
323                         ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
324                         esc_url( add_query_arg(
325                                 array(
326                                         array( 'autofocus' => array( 'panel' => 'widgets' ) ),
327                                         'return' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) )
328                                 ),
329                                 admin_url( 'customize.php' )
330                         ) ),
331                         __( 'Manage in Customizer' )
332                 );
333         }
334 ?>
335 </h1>
336
337 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
338 <div id="message" class="updated notice is-dismissible"><p><?php echo $messages[$_GET['message']]; ?></p></div>
339 <?php } ?>
340 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
341 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
342 <?php } ?>
343
344 <?php
345 /**
346  * Fires before the Widgets administration page content loads.
347  *
348  * @since 3.0.0
349  */
350 do_action( 'widgets_admin_page' ); ?>
351
352 <div class="widget-liquid-left">
353 <div id="widgets-left">
354         <div id="available-widgets" class="widgets-holder-wrap">
355                 <div class="sidebar-name">
356                         <div class="sidebar-name-arrow"><br /></div>
357                         <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _ex('Deactivate', 'removing-widget'); ?> <span></span></span></h3>
358                 </div>
359                 <div class="widget-holder">
360                         <div class="sidebar-description">
361                                 <p class="description"><?php _e('To activate a widget drag it to a sidebar or click on it. To deactivate a widget and delete its settings, drag it back.'); ?></p>
362                         </div>
363                         <div id="widget-list">
364                                 <?php wp_list_widgets(); ?>
365                         </div>
366                         <br class='clear' />
367                 </div>
368                 <br class="clear" />
369         </div>
370
371 <?php
372
373 $theme_sidebars = array();
374 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
375         if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
376                 $wrap_class = 'widgets-holder-wrap';
377                 if ( !empty( $registered_sidebar['class'] ) )
378                         $wrap_class .= ' ' . $registered_sidebar['class'];
379
380                 ?>
381                 <div class="<?php echo esc_attr( $wrap_class ); ?>">
382                         <div class="widget-holder inactive">
383                                 <?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?>
384                                 <div class="clear"></div>
385                         </div>
386                 </div>
387                 <?php
388
389         } else {
390                 $theme_sidebars[$sidebar] = $registered_sidebar;
391         }
392 }
393
394 ?>
395 </div>
396 </div>
397 <?php
398
399 $i = $split = 0;
400 $single_sidebar_class = '';
401 $sidebars_count = count( $theme_sidebars );
402
403 if ( $sidebars_count > 1 ) {
404         $split = ceil( $sidebars_count / 2 );
405 } else {
406         $single_sidebar_class = ' class="single-sidebar"';
407 }
408
409 ?>
410 <div class="widget-liquid-right">
411 <div id="widgets-right"<?php echo $single_sidebar_class; ?>>
412 <div class="sidebars-column-1">
413 <?php
414
415 foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) {
416         $wrap_class = 'widgets-holder-wrap';
417         if ( !empty( $registered_sidebar['class'] ) )
418                 $wrap_class .= ' sidebar-' . $registered_sidebar['class'];
419
420         if ( $i > 0 )
421                 $wrap_class .= ' closed';
422
423         if ( $split && $i == $split ) {
424                 ?>
425                 </div><div class="sidebars-column-2">
426                 <?php
427         }
428
429         ?>
430         <div class="<?php echo esc_attr( $wrap_class ); ?>">
431                 <?php wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); // Show the control forms for each of the widgets in this sidebar ?>
432         </div>
433         <?php
434
435         $i++;
436 }
437
438 ?>
439 </div>
440 </div>
441 </div>
442 <form method="post">
443 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
444 </form>
445 <br class="clear" />
446 </div>
447
448 <div class="widgets-chooser">
449         <ul class="widgets-chooser-sidebars"></ul>
450         <div class="widgets-chooser-actions">
451                 <button class="button-secondary"><?php _e( 'Cancel' ); ?></button>
452                 <button class="button-primary"><?php _e( 'Add Widget' ); ?></button>
453         </div>
454 </div>
455
456 <?php
457
458 /**
459  * Fires after the available widgets and sidebars have loaded, before the admin footer.
460  *
461  * @since 2.2.0
462  */
463 do_action( 'sidebar_admin_page' );
464 require_once( ABSPATH . 'wp-admin/admin-footer.php' );