Make WordPress Core

Ticket #8441: widgets.php

File widgets.php, 32.8 KB (added by ceenz, 17 years ago)
Line 
1<?php
2/**
3 * API for creating dynamic sidebar without hardcoding functionality into
4 * themes. Includes both internal WordPress routines and theme use routines.
5 *
6 * This functionality was found in a plugin before WordPress 2.2 release which
7 * included it in the core from that point on.
8 *
9 * @link http://codex.wordpress.org/Plugins/WordPress_Widgets WordPress Widgets
10 * @link http://codex.wordpress.org/Plugins/WordPress_Widgets_Api Widgets API
11 *
12 * @package WordPress
13 * @subpackage Widgets
14 */
15
16/**
17 * This class must be extended for each widget and WP_Widget::widget(), WP_Widget::update()
18 * and WP_Widget::form() need to be over-ridden.
19 *
20 * @package WordPress
21 * @subpackage Widgets
22 * @since 2.8
23 */
24class WP_Widget {
25
26 var $id_base; // Root id for all widgets of this type.
27 var $name; // Name for this widget type.
28 var $widget_options; // Option array passed to wp_register_sidebar_widget()
29 var $control_options; // Option array passed to wp_register_widget_control()
30
31 var $number = false; // Unique ID number of the current instance.
32 var $id = false; // Unique ID string of the current instance (id_base-number)
33 var $updated = false; // Set true when we update the data after a POST submit - makes sure we don't do it twice.
34
35 // Member functions that you must over-ride.
36
37 /** Echo the widget content.
38 *
39 * Subclasses should over-ride this function to generate their widget code.
40 *
41 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
42 * @param array $instance The settings for the particular instance of the widget
43 */
44 function widget($args, $instance) {
45 die('function WP_Widget::widget() must be over-ridden in a sub-class.');
46 }
47
48 /** Update a particular instance.
49 *
50 * This function should check that $new_instance is set correctly.
51 * The newly calculated value of $instance should be returned.
52 * If "false" is returned, the instance won't be saved/updated.
53 *
54 * @param array $new_instance New settings for this instance as input by the user via form()
55 * @param array $old_instance Old settings for this instance
56 * @return array Settings to save or bool false to cancel saving
57 */
58 function update($new_instance, $old_instance) {
59 return $new_instance;
60 }
61
62 /** Echo the settings update form
63 *
64 * @param array $instance Current settings
65 */
66 function form($instance) {
67 echo '<p>' . __('There are no options for this widget.') . '</p>';
68 }
69
70 // Functions you'll need to call.
71
72 /**
73 * PHP4 constructor
74 */
75 function WP_Widget( $id_base, $name, $widget_options = array(), $control_options = array() ) {
76 $this->__construct( $id_base, $name, $widget_options, $control_options );
77 }
78
79 /**
80 * PHP5 constructor
81 * widget_options: passed to wp_register_sidebar_widget()
82 * - description
83 * - classname
84 * control_options: passed to wp_register_widget_control()
85 * - width
86 * - height
87 */
88 function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
89 $this->id_base = $id_base;
90 $this->name = $name;
91 $this->option_name = 'widget_' . $id_base;
92 $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
93 $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
94
95 //add_action( 'widgets_init', array( &$this, '_register' ) );
96 }
97
98 /** Constructs name attributes for use in form() fields
99 *
100 * This function should be used in form() methods to create name attributes for fields to be saved by update()
101 *
102 * @param string $field_name Field name
103 * @return string Name attribute for $field_name
104 */
105 function get_field_name($field_name) {
106 return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
107 }
108
109 /** Constructs id attributes for use in form() fields
110 *
111 * This function should be used in form() methods to create id attributes for fields to be saved by update()
112 *
113 * @param string $field_name Field name
114 * @return string ID attribute for $field_name
115 */
116 function get_field_id($field_name) {
117 return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
118 }
119
120 // Private Functions. Don't worry about these.
121
122 function _register() {
123 $settings = $this->get_settings();
124
125 if ( empty($settings) ) {
126 // If there are none, we register the widget's existance with a
127 // generic template
128 $this->_set(1);
129 $this->_register_one();
130 } elseif ( is_array($settings) ) {
131 foreach ( array_keys($settings) as $number ) {
132 if ( is_numeric($number) ) {
133 $this->_set($number);
134 $this->_register_one($number);
135 }
136 }
137 }
138 }
139
140 function _set($number) {
141 $this->number = $number;
142 $this->id = $this->id_base . '-' . $number;
143 }
144
145 function _get_display_callback() {
146 return array(&$this, 'display_callback');
147 }
148
149 function _get_update_callback() {
150 return array(&$this, 'update_callback');
151 }
152
153 function _get_form_callback() {
154 return array(&$this, 'form_callback');
155 }
156
157 /** Generate the actual widget content.
158 * Just finds the instance and calls widget().
159 * Do NOT over-ride this function. */
160 function display_callback( $args, $widget_args = 1 ) {
161 if ( is_numeric($widget_args) )
162 $widget_args = array( 'number' => $widget_args );
163
164 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
165 $this->_set( $widget_args['number'] );
166 $settings = $this->get_settings();
167
168 if ( array_key_exists( $this->number, $settings ) ) {
169 $hide_widget = apply_filters('widget_display_test', $settings[$this->number]); //Add hook to test if widget should be displayed.
170 if (!$hide_widget) $this->widget($args, $settings[$this->number]); //Test and display widget.
171 $this->widget($args, $settings[$this->number]);
172 }
173 }
174
175 /** Deal with changed settings.
176 * Do NOT over-ride this function. */
177 function update_callback( $widget_args = 1 ) {
178 global $wp_registered_widgets;
179
180 if ( is_numeric($widget_args) )
181 $widget_args = array( 'number' => $widget_args );
182
183 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
184 $all_instances = $this->get_settings();
185
186 // We need to update the data
187 if ( !$this->updated && !empty($_POST['sidebar']) ) {
188
189 // Tells us what sidebar to put the data in
190 $sidebar = (string) $_POST['sidebar'];
191
192 $sidebars_widgets = wp_get_sidebars_widgets();
193 if ( isset($sidebars_widgets[$sidebar]) )
194 $this_sidebar =& $sidebars_widgets[$sidebar];
195 else
196 $this_sidebar = array();
197
198 if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
199 // Delete the settings for this instance of the widget
200 if ( isset($_POST['widget-id']) )
201 $del_id = $_POST['widget-id'];
202 else
203 return;
204
205 if ( $this->_get_display_callback() == $wp_registered_widgets[$del_id]['callback'] && isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
206 $number = $wp_registered_widgets[$del_id]['params'][0]['number'];
207
208 if ( $this->id_base . '-' . $number == $del_id ) {
209 unset($all_instances[$number]);
210 }
211 }
212 } else {
213 foreach ( (array) $_POST['widget-' . $this->id_base] as $number => $new_instance ) {
214 $new_instance = stripslashes_deep($new_instance);
215 $this->_set($number);
216
217 if ( isset($all_instances[$number]) )
218 $instance = $this->update($new_instance, $all_instances[$number]);
219 else
220 $instance = $this->update($new_instance, array());
221
222 if ( false !== $instance ) {
223 $widget_details_compare = array($new_instance,$instance);
224 $instance = apply_filters('default_widget_options_update',$widget_details_compare); //Callback to apply default or other options to be added to list of widget options to store in database
225 $all_instances[$number] = $instance;
226
227 }
228 }
229 }
230
231 $this->save_settings($all_instances);
232 $this->updated = true;
233 }
234 }
235
236 /** Generate the control form.
237 * Do NOT over-ride this function. */
238 function form_callback( $widget_args = 1 ) {
239 if ( is_numeric($widget_args) )
240 $widget_args = array( 'number' => $widget_args );
241
242 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
243 $all_instances = $this->get_settings();
244
245 if ( -1 == $widget_args['number'] ) {
246 // We echo out a form where 'number' can be set later
247 $this->_set('__i__');
248 $instance = array();
249 } else {
250 $this->_set($widget_args['number']);
251 $instance = $all_instances[ $widget_args['number'] ];
252 }
253
254 $this->form($instance);
255 $widget_details = array($this->id_base,$this->number,$instance);
256 do_action('default_widget_options_display', $widget_details); //Add action hook to display default widget information or options
257 }
258
259 /** Helper function: Registers a single instance. */
260 function _register_one($number = -1) {
261 wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
262 _register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
263 _register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
264 }
265
266 function save_settings($settings) {
267 $settings['_multiwidget'] = 1;
268 update_option( $this->option_name, $settings );
269 }
270
271 function get_settings() {
272 $settings = get_option($this->option_name);
273
274 if ( !is_array($settings) )
275 $settings = array();
276
277 if ( !array_key_exists('_multiwidget', $settings) ) {
278 // old format, conver if single widget
279 $settings = wp_convert_widget_settings($this->id_base, $this->option_name, $settings);
280 }
281
282 unset($settings['_multiwidget']);
283 return $settings;
284 }
285}
286
287/**
288 * Singleton that registers and instantiates WP_Widget classes.
289 *
290 * @package WordPress
291 * @subpackage Widgets
292 * @since 2.8
293 */
294class WP_Widget_Factory {
295 var $widgets = array();
296
297 function WP_Widget_Factory() {
298 add_action( 'widgets_init', array( &$this, '_register_widgets' ), 100 );
299 }
300
301 function register($widget_class) {
302 $this->widgets[$widget_class] = new $widget_class();
303 }
304
305 function unregister($widget_class) {
306 if ( isset($this->widgets[$widget_class]) )
307 unset($this->widgets[$widget_class]);
308 }
309
310 function _register_widgets() {
311 foreach ( $this->widgets as $widget )
312 $widget->_register();
313 }
314}
315
316/* Global Variables */
317
318/** @ignore */
319global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
320
321/**
322 * Stores the sidebars, since many themes can have more than one.
323 *
324 * @global array $wp_registered_sidebars
325 * @since 2.2.0
326 */
327$wp_registered_sidebars = array();
328
329/**
330 * Stores the registered widgets.
331 *
332 * @global array $wp_registered_widgets
333 * @since 2.2.0
334 */
335$wp_registered_widgets = array();
336
337/**
338 * Stores the registered widget control (options).
339 *
340 * @global array $wp_registered_widget_controls
341 * @since 2.2.0
342 */
343$wp_registered_widget_controls = array();
344$wp_registered_widget_updates = array();
345
346/* Template tags & API functions */
347
348/**
349 * Register a widget
350 *
351 * Registers a WP_Widget widget
352 *
353 * @since 2.8.0
354 *
355 * @see WP_Widget
356 * @see WP_Widget_Factory
357 * @uses WP_Widget_Factory
358 *
359 * @param string $widget_class The name of a class that extends WP_Widget
360 */
361function register_widget($widget_class) {
362 global $wp_widget_factory;
363
364 $wp_widget_factory->register($widget_class);
365}
366
367/**
368 * Unregister a widget
369 *
370 * Unregisters a WP_Widget widget. Useful for unregistering default widgets.
371 * Run within a function hooked to the widgets_init action.
372 *
373 * @since 2.8.0
374 *
375 * @see WP_Widget
376 * @see WP_Widget_Factory
377 * @uses WP_Widget_Factory
378 *
379 * @param string $widget_class The name of a class that extends WP_Widget
380 */
381function unregister_widget($widget_class) {
382 global $wp_widget_factory;
383
384 $wp_widget_factory->unregister($widget_class);
385}
386
387/**
388 * Creates multiple sidebars.
389 *
390 * If you wanted to quickly create multiple sidebars for a theme or internally.
391 * This function will allow you to do so. If you don't pass the 'name' and/or
392 * 'id' in $args, then they will be built for you.
393 *
394 * The default for the name is "Sidebar #", with '#' being replaced with the
395 * number the sidebar is currently when greater than one. If first sidebar, the
396 * name will be just "Sidebar". The default for id is "sidebar-" followed by the
397 * number the sidebar creation is currently at.
398 *
399 * @since 2.2.0
400 *
401 * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
402 * @uses parse_str() Converts a string to an array to be used in the rest of the function.
403 * @uses register_sidebar() Sends single sidebar information [name, id] to this
404 * function to handle building the sidebar.
405 *
406 * @param int $number Number of sidebars to create.
407 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values.
408 */
409function register_sidebars($number = 1, $args = array()) {
410 global $wp_registered_sidebars;
411 $number = (int) $number;
412
413 if ( is_string($args) )
414 parse_str($args, $args);
415
416 for ( $i=1; $i <= $number; $i++ ) {
417 $_args = $args;
418
419 if ( $number > 1 ) {
420 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
421 } else {
422 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
423 }
424
425 if (isset($args['id'])) {
426 $_args['id'] = $args['id'];
427 } else {
428 $n = count($wp_registered_sidebars);
429 do {
430 $n++;
431 $_args['id'] = "sidebar-$n";
432 } while (isset($wp_registered_sidebars[$_args['id']]));
433 }
434
435 register_sidebar($_args);
436 }
437}
438
439/**
440 * Builds the definition for a single sidebar and returns the ID.
441 *
442 * The $args parameter takes either a string or an array with 'name' and 'id'
443 * contained in either usage. It will be noted that the values will be applied
444 * to all sidebars, so if creating more than one, it will be advised to allow
445 * for WordPress to create the defaults for you.
446 *
447 * Example for string would be <code>'name=whatever;id=whatever1'</code> and for
448 * the array it would be <code>array(
449 * 'name' => 'whatever',
450 * 'id' => 'whatever1')</code>.
451 *
452 * name - The name of the sidebar, which presumably the title which will be
453 * displayed.
454 * id - The unique identifier by which the sidebar will be called by.
455 * before_widget - The content that will prepended to the widgets when they are
456 * displayed.
457 * after_widget - The content that will be appended to the widgets when they are
458 * displayed.
459 * before_title - The content that will be prepended to the title when displayed.
460 * after_title - the content that will be appended to the title when displayed.
461 *
462 * <em>Content</em> is assumed to be HTML and should be formatted as such, but
463 * doesn't have to be.
464 *
465 * @since 2.2.0
466 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
467 * @uses parse_str() Converts a string to an array to be used in the rest of the function.
468 * @usedby register_sidebars()
469 *
470 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values
471 * @return string The sidebar id that was added.
472 */
473function register_sidebar($args = array()) {
474 global $wp_registered_sidebars;
475
476 if ( is_string($args) )
477 parse_str($args, $args);
478
479 $i = count($wp_registered_sidebars) + 1;
480
481 $defaults = array(
482 'name' => sprintf(__('Sidebar %d'), $i ),
483 'id' => "sidebar-$i",
484 'before_widget' => '<li id="%1$s" class="widget %2$s">',
485 'after_widget' => "</li>\n",
486 'before_title' => '<h2 class="widgettitle">',
487 'after_title' => "</h2>\n",
488 );
489
490 $sidebar = array_merge($defaults, (array) $args);
491
492 $wp_registered_sidebars[$sidebar['id']] = $sidebar;
493
494 return $sidebar['id'];
495}
496
497/**
498 * Removes a sidebar from the list.
499 *
500 * @since 2.2.0
501 *
502 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
503 *
504 * @param string $name The ID of the sidebar when it was added.
505 */
506function unregister_sidebar( $name ) {
507 global $wp_registered_sidebars;
508
509 if ( isset( $wp_registered_sidebars[$name] ) )
510 unset( $wp_registered_sidebars[$name] );
511}
512
513/**
514 * Register widget for use in sidebars.
515 *
516 * The default widget option is 'classname' that can be override.
517 *
518 * The function can also be used to unregister widgets when $output_callback
519 * parameter is an empty string.
520 *
521 * @since 2.2.0
522 *
523 * @uses $wp_registered_widgets Uses stored registered widgets.
524 * @uses $wp_register_widget_defaults Retrieves widget defaults.
525 *
526 * @param int|string $id Widget ID.
527 * @param string $name Widget display title.
528 * @param callback $output_callback Run when widget is called.
529 * @param array|string Optional. $options Widget Options.
530 * @param mixed $params,... Widget parameters to add to widget.
531 * @return null Will return if $output_callback is empty after removing widget.
532 */
533function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
534 global $wp_registered_widgets;
535
536 $id = strtolower($id);
537
538 if ( empty($output_callback) ) {
539 unset($wp_registered_widgets[$id]);
540 return;
541 }
542
543 $defaults = array('classname' => $output_callback);
544 $options = wp_parse_args($options, $defaults);
545 $widget = array(
546 'name' => $name,
547 'id' => $id,
548 'callback' => $output_callback,
549 'params' => array_slice(func_get_args(), 4)
550 );
551 $widget = array_merge($widget, $options);
552
553 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) )
554 $wp_registered_widgets[$id] = $widget;
555}
556
557/**
558 * Retrieve description for widget.
559 *
560 * When registering widgets, the options can also include 'description' that
561 * describes the widget for display on the widget administration panel or
562 * in the theme.
563 *
564 * @since 2.5.0
565 *
566 * @param int|string $id Widget ID.
567 * @return string Widget description, if available. Null on failure to retrieve description.
568 */
569function wp_widget_description( $id ) {
570 if ( !is_scalar($id) )
571 return;
572
573 global $wp_registered_widgets;
574
575 if ( isset($wp_registered_widgets[$id]['description']) )
576 return wp_specialchars( $wp_registered_widgets[$id]['description'] );
577}
578
579/**
580 * Remove widget from sidebar.
581 *
582 * @since 2.2.0
583 *
584 * @param int|string $id Widget ID.
585 */
586function wp_unregister_sidebar_widget($id) {
587 wp_register_sidebar_widget($id, '', '');
588 wp_unregister_widget_control($id);
589}
590
591/**
592 * Registers widget control callback for customizing options.
593 *
594 * The options contains the 'height', 'width', and 'id_base' keys. The 'height'
595 * option is never used. The 'width' option is the width of the fully expanded
596 * control form, but try hard to use the default width. The 'id_base' is for
597 * multi-widgets (widgets which allow multiple instances such as the text
598 * widget), an id_base must be provided. The widget id will end up looking like
599 * {$id_base}-{$unique_number}.
600 *
601 * @since 2.2.0
602 *
603 * @param int|string $id Sidebar ID.
604 * @param string $name Sidebar display name.
605 * @param callback $control_callback Run when sidebar is displayed.
606 * @param array|string $options Optional. Widget options. See above long description.
607 * @param mixed $params,... Optional. Additional parameters to add to widget.
608 */
609function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
610 global $wp_registered_widget_controls, $wp_registered_widget_updates;
611
612 $id = strtolower($id);
613 $update_id = preg_replace( '/-[0-9]+$/', '', $id );
614
615 if ( empty($control_callback) ) {
616 unset($wp_registered_widget_controls[$id]);
617 unset($wp_registered_widget_updates[$update_id]);
618 return;
619 }
620
621 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
622 return;
623
624 $defaults = array('width' => 250, 'height' => 200 ); // height is never used
625 $options = wp_parse_args($options, $defaults);
626 $options['width'] = (int) $options['width'];
627 $options['height'] = (int) $options['height'];
628
629 $widget = array(
630 'name' => $name,
631 'id' => $id,
632 'callback' => $control_callback,
633 'params' => array_slice(func_get_args(), 4)
634 );
635 $widget = array_merge($widget, $options);
636
637 $wp_registered_widget_controls[$id] = $widget;
638
639 if ( isset($wp_registered_widget_updates[$update_id]) )
640 return;
641
642 if ( isset($widget['params'][0]['number']) )
643 $widget['params'][0]['number'] = -1;
644
645 unset($widget['width'], $widget['height'], $widget['name'], $widget['id']);
646 $wp_registered_widget_updates[$update_id] = $widget;
647}
648
649function _register_widget_update_callback($id_base, $update_callback, $options = array()) {
650 global $wp_registered_widget_updates;
651
652 if ( isset($wp_registered_widget_updates[$id_base]) ) {
653 if ( empty($update_callback) )
654 unset($wp_registered_widget_updates[$id_base]);
655 return;
656 }
657
658 $widget = array(
659 'callback' => $update_callback,
660 'params' => array_slice(func_get_args(), 3)
661 );
662
663 $widget = array_merge($widget, $options);
664 $wp_registered_widget_updates[$id_base] = $widget;
665}
666
667function _register_widget_form_callback($id, $name, $form_callback, $options = array()) {
668 global $wp_registered_widget_controls;
669
670 $id = strtolower($id);
671
672 if ( empty($form_callback) ) {
673 unset($wp_registered_widget_controls[$id]);
674 return;
675 }
676
677 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
678 return;
679
680 $defaults = array('width' => 250, 'height' => 200 );
681 $options = wp_parse_args($options, $defaults);
682 $options['width'] = (int) $options['width'];
683 $options['height'] = (int) $options['height'];
684
685 $widget = array(
686 'name' => $name,
687 'id' => $id,
688 'callback' => $form_callback,
689 'params' => array_slice(func_get_args(), 4)
690 );
691 $widget = array_merge($widget, $options);
692
693 $wp_registered_widget_controls[$id] = $widget;
694}
695
696/**
697 * Remove control callback for widget.
698 *
699 * @since 2.2.0
700 * @uses wp_register_widget_control() Unregisters by using empty callback.
701 *
702 * @param int|string $id Widget ID.
703 */
704function wp_unregister_widget_control($id) {
705 return wp_register_widget_control($id, '', '');
706}
707
708/**
709 * Display dynamic sidebar.
710 *
711 * By default it displays the default sidebar or 'sidebar-1'. The 'sidebar-1' is
712 * not named by the theme, the actual name is '1', but 'sidebar-' is added to
713 * the registered sidebars for the name. If you named your sidebar 'after-post',
714 * then the parameter $index will still be 'after-post', but the lookup will be
715 * for 'sidebar-after-post'.
716 *
717 * It is confusing for the $index parameter, but just know that it should just
718 * work. When you register the sidebar in the theme, you will use the same name
719 * for this function or "Pay no heed to the man behind the curtain." Just accept
720 * it as an oddity of WordPress sidebar register and display.
721 *
722 * @since 2.2.0
723 *
724 * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
725 * @return bool True, if widget sidebar was found and called. False if not found or not called.
726 */
727function dynamic_sidebar($index = 1) {
728 global $wp_registered_sidebars, $wp_registered_widgets;
729
730 if ( is_int($index) ) {
731 $index = "sidebar-$index";
732 } else {
733 $index = sanitize_title($index);
734 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
735 if ( sanitize_title($value['name']) == $index ) {
736 $index = $key;
737 break;
738 }
739 }
740 }
741
742 $sidebars_widgets = wp_get_sidebars_widgets();
743
744 if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
745 return false;
746
747 $sidebar = $wp_registered_sidebars[$index];
748
749 $did_one = false;
750 foreach ( (array) $sidebars_widgets[$index] as $id ) {
751 $params = array_merge(
752 array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
753 (array) $wp_registered_widgets[$id]['params']
754 );
755
756 // Substitute HTML id and class attributes into before_widget
757 $classname_ = '';
758 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
759 if ( is_string($cn) )
760 $classname_ .= '_' . $cn;
761 elseif ( is_object($cn) )
762 $classname_ .= '_' . get_class($cn);
763 }
764 $classname_ = ltrim($classname_, '_');
765 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
766
767 $params = apply_filters( 'dynamic_sidebar_params', $params );
768
769 $callback = $wp_registered_widgets[$id]['callback'];
770
771 if ( is_callable($callback) ) {
772 call_user_func_array($callback, $params);
773 $did_one = true;
774 }
775 }
776
777 return $did_one;
778}
779
780/**
781 * Whether widget is registered using callback with widget ID.
782 *
783 * Without the optional $widget_id parameter, returns the ID of the first sidebar in which the first instance of the widget with the given callback is found.
784 * With the $widget_id parameter, returns the ID of the sidebar in which the widget with that callback AND that ID is found.
785 *
786 * @since 2.2.0
787 *
788 * @param callback $callback Widget callback to check.
789 * @param int $widget_id Optional, but needed for checking. Widget ID.
790/* @return mixed false if widget is not active or id of sidebar in which the widget is active.
791 */
792function is_active_widget($callback, $widget_id = false) {
793 global $wp_registered_widgets;
794
795 $sidebars_widgets = wp_get_sidebars_widgets(false);
796
797 if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
798 if ( is_array($widgets) ) foreach ( $widgets as $widget )
799 if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
800 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
801 return $sidebar;
802
803
804 return false;
805}
806
807/**
808 * Whether the dynamic sidebar is enabled and used by theme.
809 *
810 * @since 2.2.0
811 *
812 * @return bool True, if using widgets. False, if not using widgets.
813 */
814function is_dynamic_sidebar() {
815 global $wp_registered_widgets, $wp_registered_sidebars;
816 $sidebars_widgets = get_option('sidebars_widgets');
817 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
818 if ( count($sidebars_widgets[$index]) ) {
819 foreach ( (array) $sidebars_widgets[$index] as $widget )
820 if ( array_key_exists($widget, $wp_registered_widgets) )
821 return true;
822 }
823 }
824 return false;
825}
826
827/* Internal Functions */
828
829/**
830 * Retrieve full list of sidebars and their widgets.
831 *
832 * Will upgrade sidebar widget list, if needed. Will also save updated list, if
833 * needed.
834 *
835 * @since 2.2.0
836 * @access private
837 *
838 * @param bool $update Optional, default is true. Whether to save upgrade of widget array list.
839 * @return array Upgraded list of widgets to version 2 array format.
840 */
841function wp_get_sidebars_widgets($update = true) {
842 global $wp_registered_widgets, $wp_registered_sidebars;
843
844 $sidebars_widgets = get_option('sidebars_widgets', array());
845 $_sidebars_widgets = array();
846
847 if ( !isset($sidebars_widgets['array_version']) )
848 $sidebars_widgets['array_version'] = 1;
849
850 switch ( $sidebars_widgets['array_version'] ) {
851 case 1 :
852 foreach ( (array) $sidebars_widgets as $index => $sidebar )
853 if ( is_array($sidebar) )
854 foreach ( (array) $sidebar as $i => $name ) {
855 $id = strtolower($name);
856 if ( isset($wp_registered_widgets[$id]) ) {
857 $_sidebars_widgets[$index][$i] = $id;
858 continue;
859 }
860 $id = sanitize_title($name);
861 if ( isset($wp_registered_widgets[$id]) ) {
862 $_sidebars_widgets[$index][$i] = $id;
863 continue;
864 }
865
866 $found = false;
867
868 foreach ( $wp_registered_widgets as $widget_id => $widget ) {
869 if ( strtolower($widget['name']) == strtolower($name) ) {
870 $_sidebars_widgets[$index][$i] = $widget['id'];
871 $found = true;
872 break;
873 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
874 $_sidebars_widgets[$index][$i] = $widget['id'];
875 $found = true;
876 break;
877 }
878 }
879
880 if ( $found )
881 continue;
882
883 unset($_sidebars_widgets[$index][$i]);
884 }
885 $_sidebars_widgets['array_version'] = 2;
886 $sidebars_widgets = $_sidebars_widgets;
887 unset($_sidebars_widgets);
888
889 case 2 :
890 $sidebars = array_keys( $wp_registered_sidebars );
891 if ( !empty( $sidebars ) ) {
892 // Move the known-good ones first
893 foreach ( (array) $sidebars as $id ) {
894 if ( array_key_exists( $id, $sidebars_widgets ) ) {
895 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
896 unset($sidebars_widgets[$id], $sidebars[$id]);
897 }
898 }
899
900 // Assign to each unmatched registered sidebar the first available orphan
901 unset( $sidebars_widgets[ 'array_version' ] );
902 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
903 $_sidebars_widgets[ $sidebar ] = $widgets;
904
905 $_sidebars_widgets['array_version'] = 3;
906 $sidebars_widgets = $_sidebars_widgets;
907 unset($_sidebars_widgets);
908 }
909
910 if ( $update )
911 update_option('sidebars_widgets', $sidebars_widgets);
912 }
913
914 if ( isset($sidebars_widgets['array_version']) )
915 unset($sidebars_widgets['array_version']);
916
917 $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets);
918 return $sidebars_widgets;
919}
920
921/**
922 * Set the sidebar widget option to update sidebars.
923 *
924 * @since 2.2.0
925 * @access private
926 *
927 * @param array $sidebars_widgets Sidebar widgets and their settings.
928 */
929function wp_set_sidebars_widgets( $sidebars_widgets ) {
930 if ( !isset( $sidebars_widgets['array_version'] ) )
931 $sidebars_widgets['array_version'] = 3;
932 update_option( 'sidebars_widgets', $sidebars_widgets );
933}
934
935/**
936 * Retrieve default registered sidebars list.
937 *
938 * @since 2.2.0
939 * @access private
940 *
941 * @return array
942 */
943function wp_get_widget_defaults() {
944 global $wp_registered_sidebars;
945
946 $defaults = array();
947
948 foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
949 $defaults[$index] = array();
950
951 return $defaults;
952}
953
954/**
955 * Convert the widget settings from single to multi-widget format.
956 *
957 * @since 2.8.0
958 *
959 * @return array
960 */
961function wp_convert_widget_settings($base_name, $option_name, $settings) {
962 // This test may need expanding.
963 $single = false;
964 if ( empty($settings) ) {
965 $single = true;
966 } else {
967 foreach ( array_keys($settings) as $number ) {
968 if ( !is_numeric($number) ) {
969 $single = true;
970 break;
971 }
972 }
973 }
974
975 if ( $single ) {
976 $settings = array( 2 => $settings );
977
978 $sidebars_widgets = get_option('sidebars_widgets');
979 foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
980 if ( is_array($sidebar) ) {
981 foreach ( $sidebar as $i => $name ) {
982 if ( $base_name == $name ) {
983 $sidebars_widgets[$index][$i] = "$name-2";
984 break 2;
985 }
986 }
987 }
988 }
989
990 update_option('sidebars_widgets', $sidebars_widgets);
991 }
992
993 $settings['_multiwidget'] = 1;
994 update_option( $option_name, $settings );
995
996 return $settings;
997}
998
999/**
1000 * Deprecated API
1001 */
1002
1003/**
1004 * Register widget for sidebar with backwards compatibility.
1005 *
1006 * Allows $name to be an array that accepts either three elements to grab the
1007 * first element and the third for the name or just uses the first element of
1008 * the array for the name.
1009 *
1010 * Passes to {@link wp_register_sidebar_widget()} after argument list and
1011 * backwards compatibility is complete.
1012 *
1013 * @since 2.2.0
1014 * @uses wp_register_sidebar_widget() Passes the compiled arguments.
1015 *
1016 * @param string|int $name Widget ID.
1017 * @param callback $output_callback Run when widget is called.
1018 * @param string $classname Classname widget option.
1019 * @param mixed $params,... Widget parameters.
1020 */
1021function register_sidebar_widget($name, $output_callback, $classname = '') {
1022 // Compat
1023 if ( is_array($name) ) {
1024 if ( count($name) == 3 )
1025 $name = sprintf($name[0], $name[2]);
1026 else
1027 $name = $name[0];
1028 }
1029
1030 $id = sanitize_title($name);
1031 $options = array();
1032 if ( !empty($classname) && is_string($classname) )
1033 $options['classname'] = $classname;
1034 $params = array_slice(func_get_args(), 2);
1035 $args = array($id, $name, $output_callback, $options);
1036 if ( !empty($params) )
1037 $args = array_merge($args, $params);
1038
1039 call_user_func_array('wp_register_sidebar_widget', $args);
1040}
1041
1042/**
1043 * Alias of {@link wp_unregister_sidebar_widget()}.
1044 *
1045 * @see wp_unregister_sidebar_widget()
1046 *
1047 * @since 2.2.0
1048 *
1049 * @param int|string $id Widget ID.
1050 */
1051function unregister_sidebar_widget($id) {
1052 return wp_unregister_sidebar_widget($id);
1053}
1054
1055/**
1056 * Registers widget control callback for customizing options.
1057 *
1058 * Allows $name to be an array that accepts either three elements to grab the
1059 * first element and the third for the name or just uses the first element of
1060 * the array for the name.
1061 *
1062 * Passes to {@link wp_register_widget_control()} after the argument list has
1063 * been compiled.
1064 *
1065 * @since 2.2.0
1066 *
1067 * @param int|string $name Sidebar ID.
1068 * @param callback $control_callback Widget control callback to display and process form.
1069 * @param int $width Widget width.
1070 * @param int $height Widget height.
1071 */
1072function register_widget_control($name, $control_callback, $width = '', $height = '') {
1073 // Compat
1074 if ( is_array($name) ) {
1075 if ( count($name) == 3 )
1076 $name = sprintf($name[0], $name[2]);
1077 else
1078 $name = $name[0];
1079 }
1080
1081 $id = sanitize_title($name);
1082 $options = array();
1083 if ( !empty($width) )
1084 $options['width'] = $width;
1085 if ( !empty($height) )
1086 $options['height'] = $height;
1087 $params = array_slice(func_get_args(), 4);
1088 $args = array($id, $name, $control_callback, $options);
1089 if ( !empty($params) )
1090 $args = array_merge($args, $params);
1091
1092 call_user_func_array('wp_register_widget_control', $args);
1093}
1094
1095/**
1096 * Alias of {@link wp_unregister_widget_control()}.
1097 *
1098 * @since 2.2.0
1099 * @see wp_unregister_widget_control()
1100 *
1101 * @param int|string $id Widget ID.
1102 */
1103function unregister_widget_control($id) {
1104 return wp_unregister_widget_control($id);
1105}