Wordpress 3.5
[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, $sidebars_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 the admin widget config screen.
62  *
63  * @since 2.5.0
64  *
65  * @param string $sidebar id slug of the sidebar
66  */
67 function wp_list_widget_controls( $sidebar ) {
68         add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
69
70         echo "<div id='$sidebar' class='widgets-sortables'>\n";
71
72         $description = wp_sidebar_description( $sidebar );
73
74         if ( !empty( $description ) ) {
75                 echo "<div class='sidebar-description'>\n";
76                 echo "\t<p class='description'>$description</p>";
77                 echo "</div>\n";
78         }
79
80         dynamic_sidebar( $sidebar );
81         echo "</div>\n";
82 }
83
84 /**
85  * {@internal Missing Short Description}}
86  *
87  * @since 2.5.0
88  *
89  * @param array $params
90  * @return array
91  */
92 function wp_list_widget_controls_dynamic_sidebar( $params ) {
93         global $wp_registered_widgets;
94         static $i = 0;
95         $i++;
96
97         $widget_id = $params[0]['widget_id'];
98         $id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id;
99         $hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : '';
100
101         $params[0]['before_widget'] = "<div id='widget-{$i}_{$id}' class='widget'$hidden>";
102         $params[0]['after_widget'] = "</div>";
103         $params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated
104         $params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated
105         if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
106                 $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
107                 $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
108         }
109
110         return $params;
111 }
112
113 function next_widget_id_number($id_base) {
114         global $wp_registered_widgets;
115         $number = 1;
116
117         foreach ( $wp_registered_widgets as $widget_id => $widget ) {
118                 if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) )
119                         $number = max($number, $matches[1]);
120         }
121         $number++;
122
123         return $number;
124 }
125
126 /**
127  * Meta widget used to display the control form for a widget.
128  *
129  * Called from dynamic_sidebar().
130  *
131  * @since 2.5.0
132  *
133  * @param array $sidebar_args
134  * @return array
135  */
136 function wp_widget_control( $sidebar_args ) {
137         global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
138
139         $widget_id = $sidebar_args['widget_id'];
140         $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
141         $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar
142         $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array();
143         $widget = $wp_registered_widgets[$widget_id];
144
145         $id_format = $widget['id'];
146         $widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
147         $id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id;
148         $multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : '';
149         $add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : '';
150
151         $query_arg = array( 'editwidget' => $widget['id'] );
152         if ( $add_new ) {
153                 $query_arg['addnew'] = 1;
154                 if ( $multi_number ) {
155                         $query_arg['num'] = $multi_number;
156                         $query_arg['base'] = $id_base;
157                 }
158         } else {
159                 $query_arg['sidebar'] = $sidebar_id;
160                 $query_arg['key'] = $key;
161         }
162
163         // We aren't showing a widget control, we're outputting a template for a multi-widget control
164         if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) {
165                 // number == -1 implies a template where id numbers are replaced by a generic '__i__'
166                 $control['params'][0]['number'] = -1;
167                 // with id_base widget id's are constructed like {$id_base}-{$id_number}
168                 if ( isset($control['id_base']) )
169                         $id_format = $control['id_base'] . '-__i__';
170         }
171
172         $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
173         unset($wp_registered_widgets[$widget_id]['_callback']);
174
175         $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
176         $has_form = 'noform';
177
178         echo $sidebar_args['before_widget']; ?>
179         <div class="widget-top">
180         <div class="widget-title-action">
181                 <a class="widget-action hide-if-no-js" href="#available-widgets"></a>
182                 <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>">
183                         <span class="edit"><?php _ex( 'Edit', 'widget' ); ?></span>
184                         <span class="add"><?php _ex( 'Add', 'widget' ); ?></span>
185                         <span class="screen-reader-text"><?php echo $widget_title; ?></span>
186                 </a>
187         </div>
188         <div class="widget-title"><h4><?php echo $widget_title ?><span class="in-widget-title"></span></h4></div>
189         </div>
190
191         <div class="widget-inside">
192         <form action="" method="post">
193         <div class="widget-content">
194 <?php
195         if ( isset($control['callback']) )
196                 $has_form = call_user_func_array( $control['callback'], $control['params'] );
197         else
198                 echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
199         </div>
200         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" />
201         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
202         <input type="hidden" name="widget-width" class="widget-width" value="<?php if (isset( $control['width'] )) echo esc_attr($control['width']); ?>" />
203         <input type="hidden" name="widget-height" class="widget-height" value="<?php if (isset( $control['height'] )) echo esc_attr($control['height']); ?>" />
204         <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" />
205         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
206         <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" />
207
208         <div class="widget-control-actions">
209                 <div class="alignleft">
210                 <a class="widget-control-remove" href="#remove"><?php _e('Delete'); ?></a> |
211                 <a class="widget-control-close" href="#close"><?php _e('Close'); ?></a>
212                 </div>
213                 <div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>">
214                         <?php submit_button( __( 'Save' ), 'button-primary widget-control-save right', 'savewidget', false, array( 'id' => 'widget-' . esc_attr( $id_format ) . '-savewidget' ) ); ?>
215                         <span class="spinner"></span>
216                 </div>
217                 <br class="clear" />
218         </div>
219         </form>
220         </div>
221
222         <div class="widget-description">
223 <?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
224         </div>
225 <?php
226         echo $sidebar_args['after_widget'];
227         return $sidebar_args;
228 }