mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress Widgets Administration API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Administration
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 /**
mp-wp_genesis 10 * Display list of widgets, either all or matching search.
mp-wp_genesis 11 *
mp-wp_genesis 12 * The search parameter are search terms separated by spaces.
mp-wp_genesis 13 *
mp-wp_genesis 14 * @since unknown
mp-wp_genesis 15 *
mp-wp_genesis 16 * @param string $show Optional, default is all. What to display, can be 'all', 'unused', or 'used'.
mp-wp_genesis 17 * @param string $_search Optional. Search for widgets. Should be unsanitized.
mp-wp_genesis 18 */
mp-wp_genesis 19 function wp_list_widgets( $show = 'all', $_search = false ) {
mp-wp_genesis 20 global $wp_registered_widgets, $sidebars_widgets, $wp_registered_widget_controls;
mp-wp_genesis 21 if ( $_search ) {
mp-wp_genesis 22 // sanitize
mp-wp_genesis 23 $search = preg_replace( '/[^\w\s]/', '', $_search );
mp-wp_genesis 24 // array of terms
mp-wp_genesis 25 $search_terms = preg_split( '/[\s]/', $search, -1, PREG_SPLIT_NO_EMPTY );
mp-wp_genesis 26 } else {
mp-wp_genesis 27 $search_terms = array();
mp-wp_genesis 28 }
mp-wp_genesis 29
mp-wp_genesis 30 if ( !in_array( $show, array( 'all', 'unused', 'used' ) ) )
mp-wp_genesis 31 $show = 'all';
mp-wp_genesis 32 ?>
mp-wp_genesis 33
mp-wp_genesis 34 <ul id='widget-list'>
mp-wp_genesis 35 <?php
mp-wp_genesis 36 $no_widgets_shown = true;
mp-wp_genesis 37 $already_shown = array();
mp-wp_genesis 38 foreach ( $wp_registered_widgets as $name => $widget ) :
mp-wp_genesis 39 if ( 'all' == $show && in_array( $widget['callback'], $already_shown ) ) // We already showed this multi-widget
mp-wp_genesis 40 continue;
mp-wp_genesis 41
mp-wp_genesis 42 if ( $search_terms ) {
mp-wp_genesis 43 $hit = false;
mp-wp_genesis 44 // Simple case-insensitive search. Boolean OR.
mp-wp_genesis 45 $search_text = preg_replace( '/[^\w]/', '', $widget['name'] );
mp-wp_genesis 46 if ( isset($widget['description']) )
mp-wp_genesis 47 $search_text .= preg_replace( '/[^\w]/', '', $widget['description'] );
mp-wp_genesis 48
mp-wp_genesis 49 foreach ( $search_terms as $search_term ) {
mp-wp_genesis 50 if ( stristr( $search_text, $search_term ) ) {
mp-wp_genesis 51 $hit = true;
mp-wp_genesis 52 break;
mp-wp_genesis 53 }
mp-wp_genesis 54 }
mp-wp_genesis 55 if ( !$hit )
mp-wp_genesis 56 continue;
mp-wp_genesis 57 }
mp-wp_genesis 58
mp-wp_genesis 59 $sidebar = is_active_widget( $widget['callback'], $widget['id'] );
mp-wp_genesis 60
mp-wp_genesis 61 if ( ( 'unused' == $show && $sidebar ) || ( 'used' == $show && !$sidebar ) )
mp-wp_genesis 62 continue;
mp-wp_genesis 63
mp-wp_genesis 64 if ( ! isset( $widget['params'][0] ) )
mp-wp_genesis 65 $widget['params'][0] = array();
mp-wp_genesis 66 ob_start();
mp-wp_genesis 67 $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template', '_show' => $show ), 1 => $widget['params'][0] ) );
mp-wp_genesis 68 $sidebar_args = call_user_func_array( 'wp_widget_control', $args );
mp-wp_genesis 69 $widget_control_template = ob_get_contents();
mp-wp_genesis 70 ob_end_clean();
mp-wp_genesis 71
mp-wp_genesis 72 $widget_id = $widget['id']; // save this for later in case we mess with $widget['id']
mp-wp_genesis 73
mp-wp_genesis 74 $is_multi = false !== strpos( $widget_control_template, '%i%' );
mp-wp_genesis 75 if ( !$sidebar || $is_multi ) {
mp-wp_genesis 76 $add_query = array(
mp-wp_genesis 77 'sidebar' => $sidebar,
mp-wp_genesis 78 'key' => false,
mp-wp_genesis 79 'edit' => false
mp-wp_genesis 80 );
mp-wp_genesis 81 if ( 'all' == $show && $is_multi ) {
mp-wp_genesis 82 // it's a multi-widget. We only need to show it in the list once.
mp-wp_genesis 83 $already_shown[] = $widget['callback'];
mp-wp_genesis 84 $num = (int) array_pop( $ids = explode( '-', $widget['id'] ) );
mp-wp_genesis 85 $id_base = $wp_registered_widget_controls[$widget['id']]['id_base'];
mp-wp_genesis 86 // so that we always add a new one when clicking "add"
mp-wp_genesis 87 while ( isset($wp_registered_widgets["$id_base-$num"]) )
mp-wp_genesis 88 $num++;
mp-wp_genesis 89 $widget['id'] = "$id_base-$num";
mp-wp_genesis 90 $add_query['base'] = $id_base;
mp-wp_genesis 91 $add_query['key'] = $num;
mp-wp_genesis 92 $add_query['sidebar'] = $GLOBALS['sidebar'];
mp-wp_genesis 93 }
mp-wp_genesis 94 $add_query['add'] = $widget['id'];
mp-wp_genesis 95 $action = 'add';
mp-wp_genesis 96 $add_url = clean_url( wp_nonce_url( add_query_arg( $add_query ), "add-widget_$widget[id]" ) );
mp-wp_genesis 97 } else {
mp-wp_genesis 98 $action = 'edit';
mp-wp_genesis 99 $edit_url = clean_url( add_query_arg( array(
mp-wp_genesis 100 'sidebar' => $sidebar,
mp-wp_genesis 101 'edit' => $widget['id'],
mp-wp_genesis 102 'key' => array_search( $widget['id'], $sidebars_widgets[$sidebar] ),
mp-wp_genesis 103 ) ) );
mp-wp_genesis 104
mp-wp_genesis 105 $widget_control_template = '<textarea rows="1" cols="1">' . htmlspecialchars( $widget_control_template ) . '</textarea>';
mp-wp_genesis 106 }
mp-wp_genesis 107
mp-wp_genesis 108 $widget_control_template = $sidebar_args['before_widget'] . $widget_control_template . $sidebar_args['after_widget'];
mp-wp_genesis 109
mp-wp_genesis 110 $no_widgets_shown = false;
mp-wp_genesis 111
mp-wp_genesis 112
mp-wp_genesis 113 if ( 'all' != $show && $sidebar_args['_widget_title'] )
mp-wp_genesis 114 $widget_title = $sidebar_args['_widget_title'];
mp-wp_genesis 115 else
mp-wp_genesis 116 $widget_title = $widget['name'];
mp-wp_genesis 117 ?>
mp-wp_genesis 118
mp-wp_genesis 119 <li id="widget-list-item-<?php echo attribute_escape( $widget['id'] ); ?>" class="widget-list-item">
mp-wp_genesis 120 <h4 class="widget-title widget-draggable">
mp-wp_genesis 121
mp-wp_genesis 122 <span><?php echo $widget_title; ?></span>
mp-wp_genesis 123
mp-wp_genesis 124 <?php if ( 'add' == $action ) : ?>
mp-wp_genesis 125
mp-wp_genesis 126 <a class="widget-action widget-control-add" href="<?php echo $add_url; ?>"><?php _e( 'Add' ); ?></a>
mp-wp_genesis 127
mp-wp_genesis 128 <?php elseif ( 'edit' == $action ) :
mp-wp_genesis 129 // We echo a hidden edit link for the sake of the JS. Edit links are shown (needlessly?) after a widget is added.
mp-wp_genesis 130 ?>
mp-wp_genesis 131
mp-wp_genesis 132 <a class="widget-action widget-control-edit" href="<?php echo $edit_url; ?>" style="display: none;"><?php _e( 'Edit' ); ?></a>
mp-wp_genesis 133
mp-wp_genesis 134 <?php endif; ?>
mp-wp_genesis 135
mp-wp_genesis 136 <br class="clear" />
mp-wp_genesis 137
mp-wp_genesis 138 </h4>
mp-wp_genesis 139
mp-wp_genesis 140
mp-wp_genesis 141 <ul id="widget-control-info-<?php echo $widget['id']; ?>" class="widget-control-info">
mp-wp_genesis 142
mp-wp_genesis 143 <?php echo $widget_control_template; ?>
mp-wp_genesis 144
mp-wp_genesis 145 </ul>
mp-wp_genesis 146
mp-wp_genesis 147 <?php if ( 'add' == $action ) : ?>
mp-wp_genesis 148 <?php endif; ?>
mp-wp_genesis 149
mp-wp_genesis 150 <div class="widget-description">
mp-wp_genesis 151 <?php echo ( $widget_description = wp_widget_description( $widget_id ) ) ? $widget_description : ' '; ?>
mp-wp_genesis 152 </div>
mp-wp_genesis 153
mp-wp_genesis 154 <br class="clear" />
mp-wp_genesis 155
mp-wp_genesis 156 </li>
mp-wp_genesis 157
mp-wp_genesis 158 <?php endforeach; if ( $no_widgets_shown ) : ?>
mp-wp_genesis 159
mp-wp_genesis 160 <li><?php _e( 'No matching widgets' ); ?></li>
mp-wp_genesis 161
mp-wp_genesis 162 <?php endif; ?>
mp-wp_genesis 163
mp-wp_genesis 164 </ul>
mp-wp_genesis 165 <?php
mp-wp_genesis 166 }
mp-wp_genesis 167
mp-wp_genesis 168 /**
mp-wp_genesis 169 * {@internal Missing Short Description}}
mp-wp_genesis 170 *
mp-wp_genesis 171 * @since unknown
mp-wp_genesis 172 *
mp-wp_genesis 173 * @param string $sidebar
mp-wp_genesis 174 */
mp-wp_genesis 175 function wp_list_widget_controls( $sidebar ) {
mp-wp_genesis 176 add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
mp-wp_genesis 177 ?>
mp-wp_genesis 178
mp-wp_genesis 179 <ul class="widget-control-list">
mp-wp_genesis 180
mp-wp_genesis 181 <?php if ( !dynamic_sidebar( $sidebar ) ) echo "<li />"; ?>
mp-wp_genesis 182
mp-wp_genesis 183 </ul>
mp-wp_genesis 184
mp-wp_genesis 185 <?php
mp-wp_genesis 186 }
mp-wp_genesis 187
mp-wp_genesis 188 /**
mp-wp_genesis 189 * {@internal Missing Short Description}}
mp-wp_genesis 190 *
mp-wp_genesis 191 * @since unknown
mp-wp_genesis 192 *
mp-wp_genesis 193 * @param array $params
mp-wp_genesis 194 * @return array
mp-wp_genesis 195 */
mp-wp_genesis 196 function wp_list_widget_controls_dynamic_sidebar( $params ) {
mp-wp_genesis 197 global $wp_registered_widgets;
mp-wp_genesis 198 static $i = 0;
mp-wp_genesis 199 $i++;
mp-wp_genesis 200
mp-wp_genesis 201 $widget_id = $params[0]['widget_id'];
mp-wp_genesis 202
mp-wp_genesis 203 $params[0]['before_widget'] = "<li id='widget-list-control-item-$i-$widget_id' class='widget-list-control-item widget-sortable'>\n";
mp-wp_genesis 204 $params[0]['after_widget'] = "</li>";
mp-wp_genesis 205 $params[0]['before_title'] = "%BEG_OF_TITLE%";
mp-wp_genesis 206 $params[0]['after_title'] = "%END_OF_TITLE%";
mp-wp_genesis 207 if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
mp-wp_genesis 208 $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
mp-wp_genesis 209 $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
mp-wp_genesis 210 }
mp-wp_genesis 211 return $params;
mp-wp_genesis 212 }
mp-wp_genesis 213
mp-wp_genesis 214 /**
mp-wp_genesis 215 * Meta widget used to display the control form for a widget.
mp-wp_genesis 216 *
mp-wp_genesis 217 * Called from dynamic_sidebar().
mp-wp_genesis 218 *
mp-wp_genesis 219 * @since unknown
mp-wp_genesis 220 *
mp-wp_genesis 221 * @param array $sidebar_args
mp-wp_genesis 222 * @return array
mp-wp_genesis 223 */
mp-wp_genesis 224 function wp_widget_control( $sidebar_args ) {
mp-wp_genesis 225 global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets, $edit_widget;
mp-wp_genesis 226 $widget_id = $sidebar_args['widget_id'];
mp-wp_genesis 227 $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
mp-wp_genesis 228
mp-wp_genesis 229 $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : 0;
mp-wp_genesis 230 $widget = $wp_registered_widgets[$widget_id];
mp-wp_genesis 231
mp-wp_genesis 232 $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : 'no-key'; // position of widget in sidebar
mp-wp_genesis 233
mp-wp_genesis 234 $edit = -1 < $edit_widget && is_numeric($key) && $edit_widget === $key; // (bool) are we currently editing this widget
mp-wp_genesis 235
mp-wp_genesis 236 $id_format = $widget['id'];
mp-wp_genesis 237
mp-wp_genesis 238 if ( ! isset( $sidebar_args['_show'] ) )
mp-wp_genesis 239 $sidebar_args['_show'] = '';
mp-wp_genesis 240
mp-wp_genesis 241 if ( ! isset( $sidebar_args['_display'] ) )
mp-wp_genesis 242 $sidebar_args['_display'] = '';
mp-wp_genesis 243
mp-wp_genesis 244 // We aren't showing a widget control, we're outputing a template for a mult-widget control
mp-wp_genesis 245 if ( 'all' == $sidebar_args['_show'] && 'template' == $sidebar_args['_display'] && isset($control['params'][0]['number']) ) {
mp-wp_genesis 246 // number == -1 implies a template where id numbers are replaced by a generic '%i%'
mp-wp_genesis 247 $control['params'][0]['number'] = -1;
mp-wp_genesis 248 // if given, id_base means widget id's should be constructed like {$id_base}-{$id_number}
mp-wp_genesis 249 if ( isset($control['id_base']) )
mp-wp_genesis 250 $id_format = $control['id_base'] . '-%i%';
mp-wp_genesis 251 }
mp-wp_genesis 252
mp-wp_genesis 253 $widget_title = '';
mp-wp_genesis 254 // We grab the normal widget output to find the widget's title
mp-wp_genesis 255 if ( ( 'all' != $sidebar_args['_show'] || 'template' != $sidebar_args['_display'] ) && is_callable( $widget['_callback'] ) ) {
mp-wp_genesis 256 ob_start();
mp-wp_genesis 257 $args = func_get_args();
mp-wp_genesis 258 call_user_func_array( $widget['_callback'], $args );
mp-wp_genesis 259 $widget_title = ob_get_clean();
mp-wp_genesis 260 $widget_title = wp_widget_control_ob_filter( $widget_title );
mp-wp_genesis 261 }
mp-wp_genesis 262 $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
mp-wp_genesis 263 unset($wp_registered_widgets[$widget_id]['_callback']);
mp-wp_genesis 264
mp-wp_genesis 265 if ( $widget_title && $widget_title != $sidebar_args['widget_name'] )
mp-wp_genesis 266 $widget_title = sprintf( _c('%1$s: %2$s|1: widget name, 2: widget title' ), $sidebar_args['widget_name'], $widget_title );
mp-wp_genesis 267 else
mp-wp_genesis 268 $widget_title = wp_specialchars( strip_tags( $sidebar_args['widget_name'] ) );
mp-wp_genesis 269
mp-wp_genesis 270 $sidebar_args['_widget_title'] = $widget_title;
mp-wp_genesis 271
mp-wp_genesis 272 if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
mp-wp_genesis 273 echo $sidebar_args['before_widget'];
mp-wp_genesis 274 ?>
mp-wp_genesis 275 <div class="widget-top">
mp-wp_genesis 276 <h4 class="widget-title"><span><?php echo $widget_title ?></span>
mp-wp_genesis 277
mp-wp_genesis 278 <?php if ( $edit ) : ?>
mp-wp_genesis 279
mp-wp_genesis 280 <a class="widget-action widget-control-edit" href="<?php echo clean_url( remove_query_arg( array( 'edit', 'key' ) ) ); ?>"><?php _e('Cancel'); ?></a>
mp-wp_genesis 281
mp-wp_genesis 282 <?php else : ?>
mp-wp_genesis 283
mp-wp_genesis 284 <a class="widget-action widget-control-edit" href="<?php echo clean_url( add_query_arg( array( 'edit' => $id_format, 'key' => $key ) ) ); ?>"><?php _e('Edit'); ?></a>
mp-wp_genesis 285
mp-wp_genesis 286 <?php endif; ?>
mp-wp_genesis 287
mp-wp_genesis 288 <br class="clear" />
mp-wp_genesis 289
mp-wp_genesis 290 </h4></div>
mp-wp_genesis 291
mp-wp_genesis 292 <div class="widget-control"<?php if ( $edit ) echo ' style="display: block;"'; ?>>
mp-wp_genesis 293
mp-wp_genesis 294 <?php
mp-wp_genesis 295 if ( $control )
mp-wp_genesis 296 call_user_func_array( $control['callback'], $control['params'] );
mp-wp_genesis 297 else
mp-wp_genesis 298 echo '<p>' . __('There are no options for this widget.') . '</p>';
mp-wp_genesis 299 ?>
mp-wp_genesis 300
mp-wp_genesis 301 <input type="hidden" name="widget-id[]" value="<?php echo $id_format; ?>" />
mp-wp_genesis 302 <input type="hidden" class="widget-width" value="<?php echo $control['width']; ?>" />
mp-wp_genesis 303
mp-wp_genesis 304 <div class="widget-control-actions">
mp-wp_genesis 305 <a class="button widget-action widget-control-remove alignright" href="<?php echo clean_url( wp_nonce_url( add_query_arg( array( 'remove' => $id_format, 'key' => $key ) ), "remove-widget_$widget[id]" ) ); ?>"><?php _e('Remove'); ?></a>
mp-wp_genesis 306 <br class="clear" />
mp-wp_genesis 307 </div>
mp-wp_genesis 308 </div>
mp-wp_genesis 309 <?php
mp-wp_genesis 310 if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
mp-wp_genesis 311 echo $sidebar_args['after_widget'];
mp-wp_genesis 312 return $sidebar_args;
mp-wp_genesis 313 }
mp-wp_genesis 314
mp-wp_genesis 315 /**
mp-wp_genesis 316 * {@internal Missing Short Description}}
mp-wp_genesis 317 *
mp-wp_genesis 318 * @since unknown
mp-wp_genesis 319 *
mp-wp_genesis 320 * @param string $string
mp-wp_genesis 321 * @return string
mp-wp_genesis 322 */
mp-wp_genesis 323 function wp_widget_control_ob_filter( $string ) {
mp-wp_genesis 324 if ( false === $beg = strpos( $string, '%BEG_OF_TITLE%' ) )
mp-wp_genesis 325 return '';
mp-wp_genesis 326 if ( false === $end = strpos( $string, '%END_OF_TITLE%' ) )
mp-wp_genesis 327 return '';
mp-wp_genesis 328 $string = substr( $string, $beg + 14 , $end - $beg - 14);
mp-wp_genesis 329 $string = str_replace( ' ', ' ', $string );
mp-wp_genesis 330 return trim( wp_specialchars( strip_tags( $string ) ) );
mp-wp_genesis 331 }
mp-wp_genesis 332