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