Wordpress 2.8.2
[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, either all or matching search.
11  *
12  * The search parameter are search terms separated by spaces.
13  *
14  * @since unknown
15  *
16  * @param string $show Optional, default is all. What to display, can be 'all', 'unused', or 'used'.
17  * @param string $_search Optional. Search for widgets. Should be unsanitized.
18  */
19 function wp_list_widgets() {
20         global $wp_registered_widgets, $sidebars_widgets, $wp_registered_widget_controls;
21
22         $sort = $wp_registered_widgets;
23         usort( $sort, create_function( '$a, $b', 'return strnatcasecmp( $a["name"], $b["name"] );' ) );
24         $done = array();
25
26         foreach ( $sort as $widget ) {
27                 if ( in_array( $widget['callback'], $done, true ) ) // We already showed this multi-widget
28                         continue;
29
30                 $sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false );
31                 $done[] = $widget['callback'];
32
33                 if ( ! isset( $widget['params'][0] ) )
34                         $widget['params'][0] = array();
35
36                 $args = array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' );
37
38                 if ( isset($wp_registered_widget_controls[$widget['id']]['id_base']) && isset($widget['params'][0]['number']) ) {
39                         $id_base = $wp_registered_widget_controls[$widget['id']]['id_base'];
40                         $args['_temp_id'] = "$id_base-__i__";
41                         $args['_multi_num'] = next_widget_id_number($id_base);
42                         $args['_add'] = 'multi';
43                 } else {
44                         $args['_add'] = 'single';
45                         if ( $sidebar )
46                                 $args['_hide'] = '1';
47                 }
48
49                 $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
50                 call_user_func_array( 'wp_widget_control', $args );
51         }
52 }
53
54 /**
55  * {@internal Missing Short Description}}
56  *
57  * @since unknown
58  *
59  * @param string $sidebar
60  */
61 function wp_list_widget_controls( $sidebar ) {
62         add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
63
64         echo "\t<div id='$sidebar' class='widgets-sortables'>\n";
65         dynamic_sidebar( $sidebar );
66         echo "\t</div>\n";
67 }
68
69 /**
70  * {@internal Missing Short Description}}
71  *
72  * @since unknown
73  *
74  * @param array $params
75  * @return array
76  */
77 function wp_list_widget_controls_dynamic_sidebar( $params ) {
78         global $wp_registered_widgets;
79         static $i = 0;
80         $i++;
81
82         $widget_id = $params[0]['widget_id'];
83         $id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id;
84         $hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : '';
85
86         $params[0]['before_widget'] = "<div id='widget-${i}_$id' class='widget'$hidden>";
87         $params[0]['after_widget'] = "</div>";
88         $params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated
89         $params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated
90         if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
91                 $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
92                 $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
93         }
94
95         return $params;
96 }
97
98 function next_widget_id_number($id_base) {
99         global $wp_registered_widgets;
100         $number = 1;
101
102         foreach ( $wp_registered_widgets as $widget_id => $widget ) {
103                 if ( preg_match( '/' . $id_base . '-([0-9]+)$/', $widget_id, $matches ) )
104                         $number = max($number, $matches[1]);
105         }
106         $number++;
107
108         return $number;
109 }
110
111 /**
112  * Meta widget used to display the control form for a widget.
113  *
114  * Called from dynamic_sidebar().
115  *
116  * @since unknown
117  *
118  * @param array $sidebar_args
119  * @return array
120  */
121 function wp_widget_control( $sidebar_args ) {
122         global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
123
124         $widget_id = $sidebar_args['widget_id'];
125         $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
126         $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : '-1'; // position of widget in sidebar
127         $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array();
128         $widget = $wp_registered_widgets[$widget_id];
129
130         $id_format = $widget['id'];
131         $widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
132         $id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id;
133         $multi_number = isset($sidebar_args['_multi_num']) ? $sidebar_args['_multi_num'] : '';
134         $add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : '';
135
136         $query_arg = array( 'editwidget' => $widget['id'] );
137         if ( $add_new ) {
138                 $query_arg['addnew'] = 1;
139                 if ( $multi_number ) {
140                         $query_arg['num'] = $multi_number;
141                         $query_arg['base'] = $id_base;
142                 }
143         } else {
144                 $query_arg['sidebar'] = $sidebar_id;
145                 $query_arg['key'] = $key;
146         }
147
148         // We aren't showing a widget control, we're outputing a template for a mult-widget control
149         if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) {
150                 // number == -1 implies a template where id numbers are replaced by a generic '__i__'
151                 $control['params'][0]['number'] = -1;
152                 // with id_base widget id's are constructed like {$id_base}-{$id_number}
153                 if ( isset($control['id_base']) )
154                         $id_format = $control['id_base'] . '-__i__';
155         }
156
157         $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
158         unset($wp_registered_widgets[$widget_id]['_callback']);
159
160         $widget_title = esc_html( strip_tags( $sidebar_args['widget_name'] ) );
161         $has_form = 'noform';
162
163         echo $sidebar_args['before_widget']; ?>
164         <div class="widget-top">
165         <div class="widget-title-action">
166                 <a class="widget-action hide-if-no-js" href="#available-widgets"></a>
167                 <a class="widget-control-edit hide-if-js" href="<?php echo esc_url( add_query_arg( $query_arg ) ); ?>"><span class="edit"><?php _e('Edit'); ?></span><span class="add"><?php _e('Add'); ?></span></a>
168         </div>
169         <div class="widget-title"><h4><?php echo $widget_title ?><span class="in-widget-title"></span></h4></div>
170         </div>
171
172         <div class="widget-inside">
173         <form action="" method="post">
174         <div class="widget-content">
175 <?php
176         if ( isset($control['callback']) )
177                 $has_form = call_user_func_array( $control['callback'], $control['params'] );
178         else
179                 echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
180         </div>
181         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" />
182         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
183         <input type="hidden" name="widget-width" class="widget-width" value="<?php echo esc_attr($control['width']); ?>" />
184         <input type="hidden" name="widget-height" class="widget-height" value="<?php echo esc_attr($control['height']); ?>" />
185         <input type="hidden" name="widget_number" class="widget_number" value="<?php echo esc_attr($widget_number); ?>" />
186         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
187         <input type="hidden" name="add_new" class="add_new" value="<?php echo esc_attr($add_new); ?>" />
188
189         <div class="widget-control-actions">
190                 <div class="alignleft">
191                 <a class="widget-control-remove" href="#remove"><?php _e('Remove'); ?></a> |
192                 <a class="widget-control-close" href="#close"><?php _e('Close'); ?></a>
193                 </div>
194                 <div class="alignright<?php if ( 'noform' === $has_form ) echo ' widget-control-noform'; ?>">
195                 <img src="images/wpspin_light.gif" class="ajax-feedback " title="" alt="" />
196                 <input type="submit" name="savewidget" class="button-primary widget-control-save" value="<?php esc_attr_e('Save'); ?>" />
197                 </div>
198                 <br class="clear" />
199         </div>
200         </form>
201         </div>
202
203         <div class="widget-description">
204 <?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
205         </div>
206 <?php
207         echo $sidebar_args['after_widget'];
208         return $sidebar_args;
209 }
210