WordPress 4.7
[autoinstalls/wordpress.git] / wp-admin / includes / widgets.php
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
13  *
14  * @global array $wp_registered_widgets
15  * @global array $wp_registered_widget_controls
16  */
17 function wp_list_widgets() {
18         global $wp_registered_widgets, $wp_registered_widget_controls;
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
57  *
58  * @return int
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  *
70  * @param string $sidebar      Sidebar ID.
71  * @param string $sidebar_name Optional. Sidebar name. Default empty.
72  */
73 function wp_list_widget_controls( $sidebar, $sidebar_name = '' ) {
74         add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
75
76         $description = wp_sidebar_description( $sidebar );
77
78         echo '<div id="' . esc_attr( $sidebar ) . '" class="widgets-sortables">';
79
80         if ( $sidebar_name ) {
81                 ?>
82                 <div class="sidebar-name">
83                         <div class="sidebar-name-arrow"><br /></div>
84                         <h2><?php echo esc_html( $sidebar_name ); ?> <span class="spinner"></span></h2>
85                 </div>
86                 <?php
87         }
88
89         if ( ! empty( $description ) ) {
90                 ?>
91                 <div class="sidebar-description">
92                         <p class="description"><?php echo $description; ?></p>
93                 </div>
94                 <?php
95         }
96
97         dynamic_sidebar( $sidebar );
98
99         echo '</div>';
100 }
101
102 /**
103  * Retrieves the widget control arguments.
104  *
105  * @since 2.5.0
106  *
107  * @global array $wp_registered_widgets
108  *
109  * @staticvar int $i
110  *
111  * @param array $params
112  * @return array
113  */
114 function wp_list_widget_controls_dynamic_sidebar( $params ) {
115         global $wp_registered_widgets;
116         static $i = 0;
117         $i++;
118
119         $widget_id = $params[0]['widget_id'];
120         $id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id;
121         $hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : '';
122
123         $params[0]['before_widget'] = "<div id='widget-{$i}_{$id}' class='widget'$hidden>";
124         $params[0]['after_widget'] = "</div>";
125         $params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated
126         $params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated
127         if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
128                 $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
129                 $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
130         }
131
132         return $params;
133 }
134
135 /**
136  *
137  * @global array $wp_registered_widgets
138  *
139  * @param string $id_base
140  * @return int
141  */
142 function next_widget_id_number( $id_base ) {
143         global $wp_registered_widgets;
144         $number = 1;
145
146         foreach ( $wp_registered_widgets as $widget_id => $widget ) {
147                 if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) )
148                         $number = max($number, $matches[1]);
149         }
150         $number++;
151
152         return $number;
153 }
154
155 /**
156  * Meta widget used to display the control form for a widget.
157  *
158  * Called from dynamic_sidebar().
159  *
160  * @since 2.5.0
161  *
162  * @global array $wp_registered_widgets
163  * @global array $wp_registered_widget_controls
164  * @global array $sidebars_widgets
165  *
166  * @param array $sidebar_args
167  * @return array
168  */
169 function wp_widget_control( $sidebar_args ) {
170         global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
171
172         $widget_id = $sidebar_args['widget_id'];
173         $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
174         $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar
175         $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array();
176         $widget = $wp_registered_widgets[$widget_id];
177
178         $id_format = $widget['id'];
179         $widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
180         $id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id;
181         $multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : '';
182         $add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : '';
183
184         $before_form = isset( $sidebar_args['before_form'] ) ? $sidebar_args['before_form'] : '<form method="post">';
185         $after_form = isset( $sidebar_args['after_form'] ) ? $sidebar_args['after_form'] : '</form>';
186         $before_widget_content = isset( $sidebar_args['before_widget_content'] ) ? $sidebar_args['before_widget_content'] : '<div class="widget-content">';
187         $after_widget_content = isset( $sidebar_args['after_widget_content'] ) ? $sidebar_args['after_widget_content'] : '</div>';
188
189         $query_arg = array( 'editwidget' => $widget['id'] );
190         if ( $add_new ) {
191                 $query_arg['addnew'] = 1;
192                 if ( $multi_number ) {
193                         $query_arg['num'] = $multi_number;
194                         $query_arg['base'] = $id_base;
195                 }
196         } else {
197                 $query_arg['sidebar'] = $sidebar_id;
198                 $query_arg['key'] = $key;
199         }
200
201         /*
202          * We aren't showing a widget control, we're outputting a template
203          * for a multi-widget control.
204          */
205         if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) {
206                 // number == -1 implies a template where id numbers are replaced by a generic '__i__'
207                 $control['params'][0]['number'] = -1;
208                 // With id_base widget id's are constructed like {$id_base}-{$id_number}.
209                 if ( isset($control['id_base']) )
210                         $id_format = $control['id_base'] . '-__i__';
211         }
212
213         $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
214         unset($wp_registered_widgets[$widget_id]['_callback']);
215
216         $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
217         $has_form = 'noform';
218
219         echo $sidebar_args['before_widget']; ?>
220         <div class="widget-top">
221         <div class="widget-title-action">
222                 <a class="widget-action hide-if-no-js" href="#available-widgets"></a>
223                 <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>">
224                         <span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span>
225                         <span class="add"><?php _ex( 'Add', 'widget' ); ?></span>
226                         <span class="screen-reader-text"><?php echo $widget_title; ?></span>
227                 </a>
228         </div>
229         <div class="widget-title"><h3><?php echo $widget_title; ?><span class="in-widget-title"></span></h3></div>
230         </div>
231
232         <div class="widget-inside">
233         <?php echo $before_form; ?>
234         <?php echo $before_widget_content; ?>
235         <?php
236         if ( isset( $control['callback'] ) ) {
237                 $has_form = call_user_func_array( $control['callback'], $control['params'] );
238         } else {
239                 echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n";
240         }
241         ?>
242         <?php echo $after_widget_content; ?>
243         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" />
244         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
245         <input type="hidden" name="widget-width" class="widget-width" value="<?php if (isset( $control['width'] )) echo esc_attr($control['width']); ?>" />
246         <input type="hidden" name="widget-height" class="widget-height" value="<?php if (isset( $control['height'] )) echo esc_attr($control['height']); ?>" />
247         <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" />
248         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
249         <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" />
250
251         <div class="widget-control-actions">
252                 <div class="alignleft">
253                 <a class="widget-control-remove" href="#remove"><?php _e('Delete'); ?></a> |
254                 <a class="widget-control-close" href="#close"><?php _e('Close'); ?></a>
255                 </div>
256                 <div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>">
257                         <?php submit_button( __( 'Save' ), 'primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $id_format ) . '-savewidget' ) ); ?>
258                         <span class="spinner"></span>
259                 </div>
260                 <br class="clear" />
261         </div>
262         <?php echo $after_form; ?>
263         </div>
264
265         <div class="widget-description">
266 <?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
267         </div>
268 <?php
269         echo $sidebar_args['after_widget'];
270
271         return $sidebar_args;
272 }
273
274 /**
275  *
276  * @param string $classes
277  * @return string
278  */
279 function wp_widgets_access_body_class($classes) {
280         return "$classes widgets_access ";
281 }