WordPress 4.0
[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 function wp_list_widgets() {
15         global $wp_registered_widgets, $wp_registered_widget_controls;
16
17         $sort = $wp_registered_widgets;
18         usort( $sort, '_sort_name_callback' );
19         $done = array();
20
21         foreach ( $sort as $widget ) {
22                 if ( in_array( $widget['callback'], $done, true ) ) // We already showed this multi-widget
23                         continue;
24
25                 $sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false );
26                 $done[] = $widget['callback'];
27
28                 if ( ! isset( $widget['params'][0] ) )
29                         $widget['params'][0] = array();
30
31                 $args = array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' );
32
33                 if ( isset($wp_registered_widget_controls[$widget['id']]['id_base']) && isset($widget['params'][0]['number']) ) {
34                         $id_base = $wp_registered_widget_controls[$widget['id']]['id_base'];
35                         $args['_temp_id'] = "$id_base-__i__";
36                         $args['_multi_num'] = next_widget_id_number($id_base);
37                         $args['_add'] = 'multi';
38                 } else {
39                         $args['_add'] = 'single';
40                         if ( $sidebar )
41                                 $args['_hide'] = '1';
42                 }
43
44                 $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
45                 call_user_func_array( 'wp_widget_control', $args );
46         }
47 }
48
49 /**
50  * Callback to sort array by a 'name' key.
51  *
52  * @since 3.1.0
53  * @access private
54  */
55 function _sort_name_callback( $a, $b ) {
56         return strnatcasecmp( $a['name'], $b['name'] );
57 }
58
59 /**
60  * Show the widgets and their settings for a sidebar.
61  * Used in the admin widget config screen.
62  *
63  * @since 2.5.0
64  *
65  * @param string $sidebar id slug of the sidebar
66  * @param string optional $sidebar_name Include the HTML for the sidebar name
67  */
68 function wp_list_widget_controls( $sidebar, $sidebar_name = '' ) {
69         add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
70
71         $description = wp_sidebar_description( $sidebar );
72
73         echo '<div id="' . esc_attr( $sidebar ) . '" class="widgets-sortables">';
74
75         if ( $sidebar_name ) {
76                 ?>
77                 <div class="sidebar-name">
78                         <div class="sidebar-name-arrow"><br /></div>
79                         <h3><?php echo esc_html( $sidebar_name ); ?> <span class="spinner"></span></h3>
80                 </div>
81                 <?php
82         }
83
84         echo '<div class="sidebar-description">';
85
86         if ( ! empty( $description ) ) {
87                 echo '<p class="description">' . $description . '</p>';
88         }
89
90         echo '</div>';
91
92         dynamic_sidebar( $sidebar );
93
94         echo '</div>';
95 }
96
97 /**
98  * {@internal Missing Short Description}}
99  *
100  * @since 2.5.0
101  *
102  * @param array $params
103  * @return array
104  */
105 function wp_list_widget_controls_dynamic_sidebar( $params ) {
106         global $wp_registered_widgets;
107         static $i = 0;
108         $i++;
109
110         $widget_id = $params[0]['widget_id'];
111         $id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id;
112         $hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : '';
113
114         $params[0]['before_widget'] = "<div id='widget-{$i}_{$id}' class='widget'$hidden>";
115         $params[0]['after_widget'] = "</div>";
116         $params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated
117         $params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated
118         if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
119                 $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
120                 $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
121         }
122
123         return $params;
124 }
125
126 function next_widget_id_number($id_base) {
127         global $wp_registered_widgets;
128         $number = 1;
129
130         foreach ( $wp_registered_widgets as $widget_id => $widget ) {
131                 if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) )
132                         $number = max($number, $matches[1]);
133         }
134         $number++;
135
136         return $number;
137 }
138
139 /**
140  * Meta widget used to display the control form for a widget.
141  *
142  * Called from dynamic_sidebar().
143  *
144  * @since 2.5.0
145  *
146  * @param array $sidebar_args
147  * @return array
148  */
149 function wp_widget_control( $sidebar_args ) {
150         global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
151
152         $widget_id = $sidebar_args['widget_id'];
153         $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
154         $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar
155         $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array();
156         $widget = $wp_registered_widgets[$widget_id];
157
158         $id_format = $widget['id'];
159         $widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
160         $id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id;
161         $multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : '';
162         $add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : '';
163
164         $query_arg = array( 'editwidget' => $widget['id'] );
165         if ( $add_new ) {
166                 $query_arg['addnew'] = 1;
167                 if ( $multi_number ) {
168                         $query_arg['num'] = $multi_number;
169                         $query_arg['base'] = $id_base;
170                 }
171         } else {
172                 $query_arg['sidebar'] = $sidebar_id;
173                 $query_arg['key'] = $key;
174         }
175
176         /*
177          * We aren't showing a widget control, we're outputting a template
178          * for a multi-widget control.
179          */
180         if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) {
181                 // number == -1 implies a template where id numbers are replaced by a generic '__i__'
182                 $control['params'][0]['number'] = -1;
183                 // With id_base widget id's are constructed like {$id_base}-{$id_number}.
184                 if ( isset($control['id_base']) )
185                         $id_format = $control['id_base'] . '-__i__';
186         }
187
188         $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
189         unset($wp_registered_widgets[$widget_id]['_callback']);
190
191         $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
192         $has_form = 'noform';
193
194         echo $sidebar_args['before_widget']; ?>
195         <div class="widget-top">
196         <div class="widget-title-action">
197                 <a class="widget-action hide-if-no-js" href="#available-widgets"></a>
198                 <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>">
199                         <span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span>
200                         <span class="add"><?php _ex( 'Add', 'widget' ); ?></span>
201                         <span class="screen-reader-text"><?php echo $widget_title; ?></span>
202                 </a>
203         </div>
204         <div class="widget-title"><h4><?php echo $widget_title ?><span class="in-widget-title"></span></h4></div>
205         </div>
206
207         <div class="widget-inside">
208         <form action="" method="post">
209         <div class="widget-content">
210 <?php
211         if ( isset($control['callback']) )
212                 $has_form = call_user_func_array( $control['callback'], $control['params'] );
213         else
214                 echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
215         </div>
216         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" />
217         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
218         <input type="hidden" name="widget-width" class="widget-width" value="<?php if (isset( $control['width'] )) echo esc_attr($control['width']); ?>" />
219         <input type="hidden" name="widget-height" class="widget-height" value="<?php if (isset( $control['height'] )) echo esc_attr($control['height']); ?>" />
220         <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" />
221         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
222         <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" />
223
224         <div class="widget-control-actions">
225                 <div class="alignleft">
226                 <a class="widget-control-remove" href="#remove"><?php _e('Delete'); ?></a> |
227                 <a class="widget-control-close" href="#close"><?php _e('Close'); ?></a>
228                 </div>
229                 <div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>">
230                         <?php submit_button( __( 'Save' ), 'button-primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $id_format ) . '-savewidget' ) ); ?>
231                         <span class="spinner"></span>
232                 </div>
233                 <br class="clear" />
234         </div>
235         </form>
236         </div>
237
238         <div class="widget-description">
239 <?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
240         </div>
241 <?php
242         echo $sidebar_args['after_widget'];
243
244         return $sidebar_args;
245 }