Projects : mp-wp : mp-wp_genesis

mp-wp/wp-includes/widgets.php

Dir - Raw

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/* Global Variables */
17
18/** @ignore */
19global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
20
21/**
22 * Stores the sidebars, since many themes can have more than one.
23 *
24 * @global array $wp_registered_sidebars
25 * @since 2.2.0
26 */
27$wp_registered_sidebars = array();
28
29/**
30 * Stores the registered widgets.
31 *
32 * @global array $wp_registered_widgets
33 * @since 2.2.0
34 */
35$wp_registered_widgets = array();
36
37/**
38 * Stores the registered widget control (options).
39 *
40 * @global array $wp_registered_widget_controls
41 * @since 2.2.0
42 */
43$wp_registered_widget_controls = array();
44
45/* Template tags & API functions */
46
47/**
48 * Creates multiple sidebars.
49 *
50 * If you wanted to quickly create multiple sidebars for a theme or internally.
51 * This function will allow you to do so. If you don't pass the 'name' and/or
52 * 'id' in $args, then they will be built for you.
53 *
54 * The default for the name is "Sidebar #", with '#' being replaced with the
55 * number the sidebar is currently when greater than one. If first sidebar, the
56 * name will be just "Sidebar". The default for id is "sidebar-" followed by the
57 * number the sidebar creation is currently at.
58 *
59 * @since 2.2.0
60 *
61 * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
62 * @uses parse_str() Converts a string to an array to be used in the rest of the function.
63 * @uses register_sidebar() Sends single sidebar information [name, id] to this
64 * function to handle building the sidebar.
65 *
66 * @param int $number Number of sidebars to create.
67 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values.
68 */
69function register_sidebars($number = 1, $args = array()) {
70 global $wp_registered_sidebars;
71 $number = (int) $number;
72
73 if ( is_string($args) )
74 parse_str($args, $args);
75
76 for ( $i=1; $i <= $number; $i++ ) {
77 $_args = $args;
78
79 if ( $number > 1 ) {
80 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
81 } else {
82 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
83 }
84
85 if (isset($args['id'])) {
86 $_args['id'] = $args['id'];
87 } else {
88 $n = count($wp_registered_sidebars);
89 do {
90 $n++;
91 $_args['id'] = "sidebar-$n";
92 } while (isset($wp_registered_sidebars[$_args['id']]));
93 }
94
95 register_sidebar($_args);
96 }
97}
98
99/**
100 * Builds the definition for a single sidebar and returns the ID.
101 *
102 * The $args parameter takes either a string or an array with 'name' and 'id'
103 * contained in either usage. It will be noted that the values will be applied
104 * to all sidebars, so if creating more than one, it will be advised to allow
105 * for WordPress to create the defaults for you.
106 *
107 * Example for string would be <code>'name=whatever;id=whatever1'</code> and for
108 * the array it would be <code>array(
109 * 'name' => 'whatever',
110 * 'id' => 'whatever1')</code>.
111 *
112 * name - The name of the sidebar, which presumably the title which will be
113 * displayed.
114 * id - The unique identifier by which the sidebar will be called by.
115 * before_widget - The content that will prepended to the widgets when they are
116 * displayed.
117 * after_widget - The content that will be appended to the widgets when they are
118 * displayed.
119 * before_title - The content that will be prepended to the title when displayed.
120 * after_title - the content that will be appended to the title when displayed.
121 *
122 * <em>Content</em> is assumed to be HTML and should be formatted as such, but
123 * doesn't have to be.
124 *
125 * @since 2.2.0
126 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
127 * @uses parse_str() Converts a string to an array to be used in the rest of the function.
128 * @usedby register_sidebars()
129 *
130 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values
131 * @return string The sidebar id that was added.
132 */
133function register_sidebar($args = array()) {
134 global $wp_registered_sidebars;
135
136 if ( is_string($args) )
137 parse_str($args, $args);
138
139 $i = count($wp_registered_sidebars) + 1;
140
141 $defaults = array(
142 'name' => sprintf(__('Sidebar %d'), $i ),
143 'id' => "sidebar-$i",
144 'before_widget' => '<li id="%1$s" class="widget %2$s">',
145 'after_widget' => "</li>\n",
146 'before_title' => '<h2 class="widgettitle">',
147 'after_title' => "</h2>\n",
148 );
149
150 $sidebar = array_merge($defaults, (array) $args);
151
152 $wp_registered_sidebars[$sidebar['id']] = $sidebar;
153
154 return $sidebar['id'];
155}
156
157/**
158 * Removes a sidebar from the list.
159 *
160 * @since 2.2.0
161 *
162 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
163 *
164 * @param string $name The ID of the sidebar when it was added.
165 */
166function unregister_sidebar( $name ) {
167 global $wp_registered_sidebars;
168
169 if ( isset( $wp_registered_sidebars[$name] ) )
170 unset( $wp_registered_sidebars[$name] );
171}
172
173/**
174 * Register widget for sidebar with backwards compatibility.
175 *
176 * Allows $name to be an array that accepts either three elements to grab the
177 * first element and the third for the name or just uses the first element of
178 * the array for the name.
179 *
180 * Passes to {@link wp_register_sidebar_widget()} after argument list and
181 * backwards compatibility is complete.
182 *
183 * @since 2.2.0
184 * @uses wp_register_sidebar_widget() Passes the compiled arguments.
185 *
186 * @param string|int $name Widget ID.
187 * @param callback $output_callback Run when widget is called.
188 * @param string $classname Classname widget option.
189 * @param mixed $params,... Widget parameters.
190 */
191function register_sidebar_widget($name, $output_callback, $classname = '') {
192 // Compat
193 if ( is_array($name) ) {
194 if ( count($name) == 3 )
195 $name = sprintf($name[0], $name[2]);
196 else
197 $name = $name[0];
198 }
199
200 $id = sanitize_title($name);
201 $options = array();
202 if ( !empty($classname) && is_string($classname) )
203 $options['classname'] = $classname;
204 $params = array_slice(func_get_args(), 2);
205 $args = array($id, $name, $output_callback, $options);
206 if ( !empty($params) )
207 $args = array_merge($args, $params);
208
209 call_user_func_array('wp_register_sidebar_widget', $args);
210}
211
212/**
213 * Register widget for use in sidebars.
214 *
215 * The default widget option is 'classname' that can be override.
216 *
217 * The function can also be used to unregister widgets when $output_callback
218 * parameter is an empty string.
219 *
220 * @since 2.2.0
221 *
222 * @uses $wp_registered_widgets Uses stored registered widgets.
223 * @uses $wp_register_widget_defaults Retrieves widget defaults.
224 *
225 * @param int|string $id Widget ID.
226 * @param string $name Widget display title.
227 * @param callback $output_callback Run when widget is called.
228 * @param array|string Optional. $options Widget Options.
229 * @param mixed $params,... Widget parameters to add to widget.
230 * @return null Will return if $output_callback is empty after removing widget.
231 */
232function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
233 global $wp_registered_widgets;
234
235 $id = strtolower($id);
236
237 if ( empty($output_callback) ) {
238 unset($wp_registered_widgets[$id]);
239 return;
240 }
241
242 $defaults = array('classname' => $output_callback);
243 $options = wp_parse_args($options, $defaults);
244 $widget = array(
245 'name' => $name,
246 'id' => $id,
247 'callback' => $output_callback,
248 'params' => array_slice(func_get_args(), 4)
249 );
250 $widget = array_merge($widget, $options);
251
252 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) )
253 $wp_registered_widgets[$id] = $widget;
254}
255
256/**
257 * Retrieve description for widget.
258 *
259 * When registering widgets, the options can also include 'description' that
260 * describes the widget for display on the widget administration panel or
261 * in the theme.
262 *
263 * @since 2.5.0
264 *
265 * @param int|string $id Widget ID.
266 * @return string Widget description, if available. Null on failure to retrieve description.
267 */
268function wp_widget_description( $id ) {
269 if ( !is_scalar($id) )
270 return;
271
272 global $wp_registered_widgets;
273
274 if ( isset($wp_registered_widgets[$id]['description']) )
275 return wp_specialchars( $wp_registered_widgets[$id]['description'] );
276}
277
278/**
279 * Alias of {@link wp_unregister_sidebar_widget()}.
280 *
281 * @see wp_unregister_sidebar_widget()
282 *
283 * @since 2.2.0
284 *
285 * @param int|string $id Widget ID.
286 */
287function unregister_sidebar_widget($id) {
288 return wp_unregister_sidebar_widget($id);
289}
290
291/**
292 * Remove widget from sidebar.
293 *
294 * @since 2.2.0
295 *
296 * @param int|string $id Widget ID.
297 */
298function wp_unregister_sidebar_widget($id) {
299 wp_register_sidebar_widget($id, '', '');
300 wp_unregister_widget_control($id);
301}
302
303/**
304 * Registers widget control callback for customizing options.
305 *
306 * Allows $name to be an array that accepts either three elements to grab the
307 * first element and the third for the name or just uses the first element of
308 * the array for the name.
309 *
310 * Passes to {@link wp_register_widget_control()} after the argument list has
311 * been compiled.
312 *
313 * @since 2.2.0
314 *
315 * @param int|string $name Sidebar ID.
316 * @param callback $control_callback Widget control callback to display and process form.
317 * @param int $width Widget width.
318 * @param int $height Widget height.
319 */
320function register_widget_control($name, $control_callback, $width = '', $height = '') {
321 // Compat
322 if ( is_array($name) ) {
323 if ( count($name) == 3 )
324 $name = sprintf($name[0], $name[2]);
325 else
326 $name = $name[0];
327 }
328
329 $id = sanitize_title($name);
330 $options = array();
331 if ( !empty($width) )
332 $options['width'] = $width;
333 if ( !empty($height) )
334 $options['height'] = $height;
335 $params = array_slice(func_get_args(), 4);
336 $args = array($id, $name, $control_callback, $options);
337 if ( !empty($params) )
338 $args = array_merge($args, $params);
339
340 call_user_func_array('wp_register_widget_control', $args);
341}
342
343/**
344 * Registers widget control callback for customizing options.
345 *
346 * The options contains the 'height', 'width', and 'id_base' keys. The 'height'
347 * option is never used. The 'width' option is the width of the fully expanded
348 * control form, but try hard to use the default width. The 'id_base' is for
349 * multi-widgets (widgets which allow multiple instances such as the text
350 * widget), an id_base must be provided. The widget id will end up looking like
351 * {$id_base}-{$unique_number}.
352 *
353 * @since 2.2.0
354 *
355 * @param int|string $id Sidebar ID.
356 * @param string $name Sidebar display name.
357 * @param callback $control_callback Run when sidebar is displayed.
358 * @param array|string $options Optional. Widget options. See above long description.
359 * @param mixed $params,... Optional. Additional parameters to add to widget.
360 */
361function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
362 global $wp_registered_widget_controls;
363
364 $id = strtolower($id);
365
366 if ( empty($control_callback) ) {
367 unset($wp_registered_widget_controls[$id]);
368 return;
369 }
370
371 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
372 return;
373
374 $defaults = array('width' => 250, 'height' => 200 ); // height is never used
375 $options = wp_parse_args($options, $defaults);
376 $options['width'] = (int) $options['width'];
377 $options['height'] = (int) $options['height'];
378
379 $widget = array(
380 'name' => $name,
381 'id' => $id,
382 'callback' => $control_callback,
383 'params' => array_slice(func_get_args(), 4)
384 );
385 $widget = array_merge($widget, $options);
386
387 $wp_registered_widget_controls[$id] = $widget;
388}
389
390/**
391 * Alias of {@link wp_unregister_widget_control()}.
392 *
393 * @since 2.2.0
394 * @see wp_unregister_widget_control()
395 *
396 * @param int|string $id Widget ID.
397 */
398function unregister_widget_control($id) {
399 return wp_unregister_widget_control($id);
400}
401
402/**
403 * Remove control callback for widget.
404 *
405 * @since 2.2.0
406 * @uses wp_register_widget_control() Unregisters by using empty callback.
407 *
408 * @param int|string $id Widget ID.
409 */
410function wp_unregister_widget_control($id) {
411 return wp_register_widget_control($id, '', '');
412}
413
414/**
415 * Display dynamic sidebar.
416 *
417 * By default it displays the default sidebar or 'sidebar-1'. The 'sidebar-1' is
418 * not named by the theme, the actual name is '1', but 'sidebar-' is added to
419 * the registered sidebars for the name. If you named your sidebar 'after-post',
420 * then the parameter $index will still be 'after-post', but the lookup will be
421 * for 'sidebar-after-post'.
422 *
423 * It is confusing for the $index parameter, but just know that it should just
424 * work. When you register the sidebar in the theme, you will use the same name
425 * for this function or "Pay no heed to the man behind the curtain." Just accept
426 * it as an oddity of WordPress sidebar register and display.
427 *
428 * @since 2.2.0
429 *
430 * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
431 * @return bool True, if widget sidebar was found and called. False if not found or not called.
432 */
433function dynamic_sidebar($index = 1) {
434 global $wp_registered_sidebars, $wp_registered_widgets;
435
436 if ( is_int($index) ) {
437 $index = "sidebar-$index";
438 } else {
439 $index = sanitize_title($index);
440 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
441 if ( sanitize_title($value['name']) == $index ) {
442 $index = $key;
443 break;
444 }
445 }
446 }
447
448 $sidebars_widgets = wp_get_sidebars_widgets();
449
450 if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
451 return false;
452
453 $sidebar = $wp_registered_sidebars[$index];
454
455 $did_one = false;
456 foreach ( (array) $sidebars_widgets[$index] as $id ) {
457 $params = array_merge(
458 array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
459 (array) $wp_registered_widgets[$id]['params']
460 );
461
462 // Substitute HTML id and class attributes into before_widget
463 $classname_ = '';
464 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
465 if ( is_string($cn) )
466 $classname_ .= '_' . $cn;
467 elseif ( is_object($cn) )
468 $classname_ .= '_' . get_class($cn);
469 }
470 $classname_ = ltrim($classname_, '_');
471 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
472
473 $params = apply_filters( 'dynamic_sidebar_params', $params );
474
475 $callback = $wp_registered_widgets[$id]['callback'];
476
477 if ( is_callable($callback) ) {
478 call_user_func_array($callback, $params);
479 $did_one = true;
480 }
481 }
482
483 return $did_one;
484}
485
486/**
487 * Whether widget is registered using callback with widget ID.
488 *
489 * Will only check if both parameters are used. Used to find which sidebar the
490 * widget is located in, but requires that both the callback and the widget ID
491 * be known.
492 *
493 * @since 2.2.0
494 *
495 * @param callback $callback Widget callback to check.
496 * @param int $widget_id Optional, but needed for checking. Widget ID.
497/* @return mixed false if widget is not active or id of sidebar in which the widget is active.
498 */
499function is_active_widget($callback, $widget_id = false) {
500 global $wp_registered_widgets;
501
502 $sidebars_widgets = wp_get_sidebars_widgets(false);
503
504 if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
505 if ( is_array($widgets) ) foreach ( $widgets as $widget )
506 if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
507 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
508 return $sidebar;
509
510
511 return false;
512}
513
514/**
515 * Whether the dynamic sidebar is enabled and used by theme.
516 *
517 * @since 2.2.0
518 *
519 * @return bool True, if using widgets. False, if not using widgets.
520 */
521function is_dynamic_sidebar() {
522 global $wp_registered_widgets, $wp_registered_sidebars;
523 $sidebars_widgets = get_option('sidebars_widgets');
524 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
525 if ( count($sidebars_widgets[$index]) ) {
526 foreach ( (array) $sidebars_widgets[$index] as $widget )
527 if ( array_key_exists($widget, $wp_registered_widgets) )
528 return true;
529 }
530 }
531 return false;
532}
533
534/* Internal Functions */
535
536/**
537 * Retrieve full list of sidebars and their widgets.
538 *
539 * Will upgrade sidebar widget list, if needed. Will also save updated list, if
540 * needed.
541 *
542 * @since 2.2.0
543 * @access private
544 *
545 * @param bool $update Optional, default is true. Whether to save upgrade of widget array list.
546 * @return array Upgraded list of widgets to version 2 array format.
547 */
548function wp_get_sidebars_widgets($update = true) {
549 global $wp_registered_widgets, $wp_registered_sidebars;
550
551 $sidebars_widgets = get_option('sidebars_widgets');
552 $_sidebars_widgets = array();
553
554 if ( !isset($sidebars_widgets['array_version']) )
555 $sidebars_widgets['array_version'] = 1;
556
557 switch ( $sidebars_widgets['array_version'] ) {
558 case 1 :
559 foreach ( (array) $sidebars_widgets as $index => $sidebar )
560 if ( is_array($sidebar) )
561 foreach ( (array) $sidebar as $i => $name ) {
562 $id = strtolower($name);
563 if ( isset($wp_registered_widgets[$id]) ) {
564 $_sidebars_widgets[$index][$i] = $id;
565 continue;
566 }
567 $id = sanitize_title($name);
568 if ( isset($wp_registered_widgets[$id]) ) {
569 $_sidebars_widgets[$index][$i] = $id;
570 continue;
571 }
572
573 $found = false;
574
575 foreach ( $wp_registered_widgets as $widget_id => $widget ) {
576 if ( strtolower($widget['name']) == strtolower($name) ) {
577 $_sidebars_widgets[$index][$i] = $widget['id'];
578 $found = true;
579 break;
580 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
581 $_sidebars_widgets[$index][$i] = $widget['id'];
582 $found = true;
583 break;
584 }
585 }
586
587 if ( $found )
588 continue;
589
590 unset($_sidebars_widgets[$index][$i]);
591 }
592 $_sidebars_widgets['array_version'] = 2;
593 $sidebars_widgets = $_sidebars_widgets;
594 unset($_sidebars_widgets);
595
596 case 2 :
597 $sidebars = array_keys( $wp_registered_sidebars );
598 if ( !empty( $sidebars ) ) {
599 // Move the known-good ones first
600 foreach ( (array) $sidebars as $id ) {
601 if ( array_key_exists( $id, $sidebars_widgets ) ) {
602 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
603 unset($sidebars_widgets[$id], $sidebars[$id]);
604 }
605 }
606
607 // Assign to each unmatched registered sidebar the first available orphan
608 unset( $sidebars_widgets[ 'array_version' ] );
609 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
610 $_sidebars_widgets[ $sidebar ] = $widgets;
611
612 $_sidebars_widgets['array_version'] = 3;
613 $sidebars_widgets = $_sidebars_widgets;
614 unset($_sidebars_widgets);
615 }
616
617 if ( $update )
618 update_option('sidebars_widgets', $sidebars_widgets);
619 }
620
621 unset($sidebars_widgets['array_version']);
622
623 $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets);
624 return $sidebars_widgets;
625}
626
627/**
628 * Set the sidebar widget option to update sidebars.
629 *
630 * @since 2.2.0
631 * @access private
632 *
633 * @param array $sidebars_widgets Sidebar widgets and their settings.
634 */
635function wp_set_sidebars_widgets( $sidebars_widgets ) {
636 if ( !isset( $sidebars_widgets['array_version'] ) )
637 $sidebars_widgets['array_version'] = 3;
638 update_option( 'sidebars_widgets', $sidebars_widgets );
639}
640
641/**
642 * Retrieve default registered sidebars list.
643 *
644 * @since 2.2.0
645 * @access private
646 *
647 * @return array
648 */
649function wp_get_widget_defaults() {
650 global $wp_registered_sidebars;
651
652 $defaults = array();
653
654 foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
655 $defaults[$index] = array();
656
657 return $defaults;
658}
659
660/* Default Widgets */
661
662/**
663 * Display pages widget.
664 *
665 * @since 2.2.0
666 *
667 * @param array $args Widget arguments.
668 */
669function wp_widget_pages( $args ) {
670 extract( $args );
671 $options = get_option( 'widget_pages' );
672
673 $title = empty( $options['title'] ) ? __( 'Pages' ) : apply_filters('widget_title', $options['title']);
674 $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
675 $exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
676
677 if ( $sortby == 'menu_order' ) {
678 $sortby = 'menu_order, post_title';
679 }
680
681 $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
682
683 if ( !empty( $out ) ) {
684?>
685 <?php echo $before_widget; ?>
686 <?php echo $before_title . $title . $after_title; ?>
687 <ul>
688 <?php echo $out; ?>
689 </ul>
690 <?php echo $after_widget; ?>
691<?php
692 }
693}
694
695/**
696 * Display and process pages widget options form.
697 *
698 * @since 2.2.0
699 */
700function wp_widget_pages_control() {
701 $options = $newoptions = get_option('widget_pages');
702 if ( isset($_POST['pages-submit']) ) {
703 $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));
704
705 $sortby = stripslashes( $_POST['pages-sortby'] );
706
707 if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
708 $newoptions['sortby'] = $sortby;
709 } else {
710 $newoptions['sortby'] = 'menu_order';
711 }
712
713 $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
714 }
715 if ( $options != $newoptions ) {
716 $options = $newoptions;
717 update_option('widget_pages', $options);
718 }
719 $title = attribute_escape($options['title']);
720 $exclude = attribute_escape( $options['exclude'] );
721?>
722 <p><label for="pages-title"><?php _e('Title:'); ?> <input class="widefat" id="pages-title" name="pages-title" type="text" value="<?php echo $title; ?>" /></label></p>
723 <p>
724 <label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
725 <select name="pages-sortby" id="pages-sortby" class="widefat">
726 <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
727 <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
728 <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
729 </select>
730 </label>
731 </p>
732 <p>
733 <label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
734 <br />
735 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
736 </p>
737 <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
738<?php
739}
740
741/**
742 * Display links widget.
743 *
744 * @since 2.2.0
745 *
746 * @param array $args Widget arguments.
747 */
748function wp_widget_links($args) {
749 extract($args, EXTR_SKIP);
750
751 $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
752 wp_list_bookmarks(apply_filters('widget_links_args', array(
753 'title_before' => $before_title, 'title_after' => $after_title,
754 'category_before' => $before_widget, 'category_after' => $after_widget,
755 'show_images' => true, 'class' => 'linkcat widget'
756 )));
757}
758
759/**
760 * Display search widget.
761 *
762 * @since 2.2.0
763 *
764 * @param array $args Widget arguments.
765 */
766function wp_widget_search($args) {
767 extract($args);
768 echo $before_widget;
769
770 // Use current theme search form if it exists
771 get_search_form();
772
773 echo $after_widget;
774}
775
776/**
777 * Display archives widget.
778 *
779 * @since 2.2.0
780 *
781 * @param array $args Widget arguments.
782 */
783function wp_widget_archives($args) {
784 extract($args);
785 $options = get_option('widget_archives');
786 $c = $options['count'] ? '1' : '0';
787 $d = $options['dropdown'] ? '1' : '0';
788 $title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);
789
790 echo $before_widget;
791 echo $before_title . $title . $after_title;
792
793 if($d) {
794?>
795 <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
796<?php
797 } else {
798?>
799 <ul>
800 <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
801 </ul>
802<?php
803 }
804
805 echo $after_widget;
806}
807
808/**
809 * Display and process archives widget options form.
810 *
811 * @since 2.2.0
812 */
813function wp_widget_archives_control() {
814 $options = $newoptions = get_option('widget_archives');
815 if ( isset($_POST["archives-submit"]) ) {
816 $newoptions['count'] = isset($_POST['archives-count']);
817 $newoptions['dropdown'] = isset($_POST['archives-dropdown']);
818 $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
819 }
820 if ( $options != $newoptions ) {
821 $options = $newoptions;
822 update_option('widget_archives', $options);
823 }
824 $count = $options['count'] ? 'checked="checked"' : '';
825 $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
826 $title = attribute_escape($options['title']);
827?>
828 <p><label for="archives-title"><?php _e('Title:'); ?> <input class="widefat" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
829 <p>
830 <label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
831 <br />
832 <label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
833 </p>
834 <input type="hidden" id="archives-submit" name="archives-submit" value="1" />
835<?php
836}
837
838/**
839 * Display meta widget.
840 *
841 * Displays log in/out, RSS feed links, etc.
842 *
843 * @since 2.2.0
844 *
845 * @param array $args Widget arguments.
846 */
847function wp_widget_meta($args) {
848 extract($args);
849 $options = get_option('widget_meta');
850 $title = empty($options['title']) ? __('Meta') : apply_filters('widget_title', $options['title']);
851?>
852 <?php echo $before_widget; ?>
853 <?php echo $before_title . $title . $after_title; ?>
854 <ul>
855 <?php wp_register(); ?>
856 <li><?php wp_loginout(); ?></li>
857 <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo attribute_escape(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
858 <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo attribute_escape(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
859 <li><a href="http://wordpress.org/" title="<?php echo attribute_escape(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>">WordPress.org</a></li>
860 <?php wp_meta(); ?>
861 </ul>
862 <?php echo $after_widget; ?>
863<?php
864}
865
866/**
867 * Display and process meta widget options form.
868 *
869 * @since 2.2.0
870 */
871function wp_widget_meta_control() {
872 $options = $newoptions = get_option('widget_meta');
873 if ( isset($_POST["meta-submit"]) ) {
874 $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"]));
875 }
876 if ( $options != $newoptions ) {
877 $options = $newoptions;
878 update_option('widget_meta', $options);
879 }
880 $title = attribute_escape($options['title']);
881?>
882 <p><label for="meta-title"><?php _e('Title:'); ?> <input class="widefat" id="meta-title" name="meta-title" type="text" value="<?php echo $title; ?>" /></label></p>
883 <input type="hidden" id="meta-submit" name="meta-submit" value="1" />
884<?php
885}
886
887/**
888 * Display calendar widget.
889 *
890 * @since 2.2.0
891 *
892 * @param array $args Widget arguments.
893 */
894function wp_widget_calendar($args) {
895 extract($args);
896 $options = get_option('widget_calendar');
897 $title = apply_filters('widget_title', $options['title']);
898 if ( empty($title) )
899 $title = '&nbsp;';
900 echo $before_widget . $before_title . $title . $after_title;
901 echo '<div id="calendar_wrap">';
902 get_calendar();
903 echo '</div>';
904 echo $after_widget;
905}
906
907/**
908 * Display and process calendar widget options form.
909 *
910 * @since 2.2.0
911 */
912function wp_widget_calendar_control() {
913 $options = $newoptions = get_option('widget_calendar');
914 if ( isset($_POST["calendar-submit"]) ) {
915 $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
916 }
917 if ( $options != $newoptions ) {
918 $options = $newoptions;
919 update_option('widget_calendar', $options);
920 }
921 $title = attribute_escape($options['title']);
922?>
923 <p><label for="calendar-title"><?php _e('Title:'); ?> <input class="widefat" id="calendar-title" name="calendar-title" type="text" value="<?php echo $title; ?>" /></label></p>
924 <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" />
925<?php
926}
927
928/**
929 * Display the Text widget, depending on the widget number.
930 *
931 * Supports multiple text widgets and keeps track of the widget number by using
932 * the $widget_args parameter. The option 'widget_text' is used to store the
933 * content for the widgets. The content and title are passed through the
934 * 'widget_text' and 'widget_title' filters respectively.
935 *
936 * @since 2.2.0
937 *
938 * @param array $args Widget arguments.
939 * @param int $number Widget number.
940 */
941function wp_widget_text($args, $widget_args = 1) {
942 extract( $args, EXTR_SKIP );
943 if ( is_numeric($widget_args) )
944 $widget_args = array( 'number' => $widget_args );
945 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
946 extract( $widget_args, EXTR_SKIP );
947
948 $options = get_option('widget_text');
949 if ( !isset($options[$number]) )
950 return;
951
952 $title = apply_filters('widget_title', $options[$number]['title']);
953 $text = apply_filters( 'widget_text', $options[$number]['text'] );
954?>
955 <?php echo $before_widget; ?>
956 <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
957 <div class="textwidget"><?php echo $text; ?></div>
958 <?php echo $after_widget; ?>
959<?php
960}
961
962/**
963 * Display and process text widget options form.
964 *
965 * @since 2.2.0
966 *
967 * @param int $widget_args Widget number.
968 */
969function wp_widget_text_control($widget_args) {
970 global $wp_registered_widgets;
971 static $updated = false;
972
973 if ( is_numeric($widget_args) )
974 $widget_args = array( 'number' => $widget_args );
975 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
976 extract( $widget_args, EXTR_SKIP );
977
978 $options = get_option('widget_text');
979 if ( !is_array($options) )
980 $options = array();
981
982 if ( !$updated && !empty($_POST['sidebar']) ) {
983 $sidebar = (string) $_POST['sidebar'];
984
985 $sidebars_widgets = wp_get_sidebars_widgets();
986 if ( isset($sidebars_widgets[$sidebar]) )
987 $this_sidebar =& $sidebars_widgets[$sidebar];
988 else
989 $this_sidebar = array();
990
991 foreach ( (array) $this_sidebar as $_widget_id ) {
992 if ( 'wp_widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
993 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
994 if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
995 unset($options[$widget_number]);
996 }
997 }
998
999 foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
1000 if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
1001 continue;
1002 $title = strip_tags(stripslashes($widget_text['title']));
1003 if ( current_user_can('unfiltered_html') )
1004 $text = stripslashes( $widget_text['text'] );
1005 else
1006 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
1007 $options[$widget_number] = compact( 'title', 'text' );
1008 }
1009
1010 update_option('widget_text', $options);
1011 $updated = true;
1012 }
1013
1014 if ( -1 == $number ) {
1015 $title = '';
1016 $text = '';
1017 $number = '%i%';
1018 } else {
1019 $title = attribute_escape($options[$number]['title']);
1020 $text = format_to_edit($options[$number]['text']);
1021 }
1022?>
1023 <p>
1024 <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
1025 <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
1026 <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
1027 </p>
1028<?php
1029}
1030
1031/**
1032 * Register text widget on startup.
1033 *
1034 * @since 2.2.0
1035 */
1036function wp_widget_text_register() {
1037 if ( !$options = get_option('widget_text') )
1038 $options = array();
1039 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
1040 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
1041 $name = __('Text');
1042
1043 $id = false;
1044 foreach ( (array) array_keys($options) as $o ) {
1045 // Old widgets can have null values for some reason
1046 if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
1047 continue;
1048 $id = "text-$o"; // Never never never translate an id
1049 wp_register_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
1050 wp_register_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
1051 }
1052
1053 // If there are none, we register the widget's existance with a generic template
1054 if ( !$id ) {
1055 wp_register_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
1056 wp_register_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
1057 }
1058}
1059
1060/**
1061 * Display categories widget.
1062 *
1063 * Allows multiple category widgets.
1064 *
1065 * @since 2.2.0
1066 *
1067 * @param array $args Widget arguments.
1068 * @param int $number Widget number.
1069 */
1070function wp_widget_categories($args, $widget_args = 1) {
1071 extract($args, EXTR_SKIP);
1072 if ( is_numeric($widget_args) )
1073 $widget_args = array( 'number' => $widget_args );
1074 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1075 extract($widget_args, EXTR_SKIP);
1076
1077 $options = get_option('widget_categories');
1078 if ( !isset($options[$number]) )
1079 return;
1080
1081 $c = $options[$number]['count'] ? '1' : '0';
1082 $h = $options[$number]['hierarchical'] ? '1' : '0';
1083 $d = $options[$number]['dropdown'] ? '1' : '0';
1084
1085 $title = empty($options[$number]['title']) ? __('Categories') : apply_filters('widget_title', $options[$number]['title']);
1086
1087 echo $before_widget;
1088 echo $before_title . $title . $after_title;
1089
1090 $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
1091
1092 if ( $d ) {
1093 $cat_args['show_option_none'] = __('Select Category');
1094 wp_dropdown_categories($cat_args);
1095?>
1096
1097<script type='text/javascript'>
1098/* <![CDATA[ */
1099 var dropdown = document.getElementById("cat");
1100 function onCatChange() {
1101 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
1102 location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
1103 }
1104 }
1105 dropdown.onchange = onCatChange;
1106/* ]]> */
1107</script>
1108
1109<?php
1110 } else {
1111?>
1112 <ul>
1113 <?php
1114 $cat_args['title_li'] = '';
1115 wp_list_categories($cat_args);
1116 ?>
1117 </ul>
1118<?php
1119 }
1120
1121 echo $after_widget;
1122}
1123
1124/**
1125 * Display and process categories widget options form.
1126 *
1127 * @since 2.2.0
1128 *
1129 * @param int $widget_args Widget number.
1130 */
1131function wp_widget_categories_control( $widget_args ) {
1132 global $wp_registered_widgets;
1133 static $updated = false;
1134
1135 if ( is_numeric($widget_args) )
1136 $widget_args = array( 'number' => $widget_args );
1137 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1138 extract($widget_args, EXTR_SKIP);
1139
1140 $options = get_option('widget_categories');
1141
1142 if ( !is_array( $options ) )
1143 $options = array();
1144
1145 if ( !$updated && !empty($_POST['sidebar']) ) {
1146 $sidebar = (string) $_POST['sidebar'];
1147
1148 $sidebars_widgets = wp_get_sidebars_widgets();
1149 if ( isset($sidebars_widgets[$sidebar]) )
1150 $this_sidebar =& $sidebars_widgets[$sidebar];
1151 else
1152 $this_sidebar = array();
1153
1154 foreach ( (array) $this_sidebar as $_widget_id ) {
1155 if ( 'wp_widget_categories' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1156 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1157 if ( !in_array( "categories-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
1158 unset($options[$widget_number]);
1159 }
1160 }
1161
1162 foreach ( (array) $_POST['widget-categories'] as $widget_number => $widget_cat ) {
1163 if ( !isset($widget_cat['title']) && isset($options[$widget_number]) ) // user clicked cancel
1164 continue;
1165 $title = trim(strip_tags(stripslashes($widget_cat['title'])));
1166 $count = isset($widget_cat['count']);
1167 $hierarchical = isset($widget_cat['hierarchical']);
1168 $dropdown = isset($widget_cat['dropdown']);
1169 $options[$widget_number] = compact( 'title', 'count', 'hierarchical', 'dropdown' );
1170 }
1171
1172 update_option('widget_categories', $options);
1173 $updated = true;
1174 }
1175
1176 if ( -1 == $number ) {
1177 $title = '';
1178 $count = false;
1179 $hierarchical = false;
1180 $dropdown = false;
1181 $number = '%i%';
1182 } else {
1183 $title = attribute_escape( $options[$number]['title'] );
1184 $count = (bool) $options[$number]['count'];
1185 $hierarchical = (bool) $options[$number]['hierarchical'];
1186 $dropdown = (bool) $options[$number]['dropdown'];
1187 }
1188?>
1189 <p>
1190 <label for="categories-title-<?php echo $number; ?>">
1191 <?php _e( 'Title:' ); ?>
1192 <input class="widefat" id="categories-title-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
1193 </label>
1194 </p>
1195
1196 <p>
1197 <label for="categories-dropdown-<?php echo $number; ?>">
1198 <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][dropdown]"<?php checked( $dropdown, true ); ?> />
1199 <?php _e( 'Show as dropdown' ); ?>
1200 </label>
1201 <br />
1202 <label for="categories-count-<?php echo $number; ?>">
1203 <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][count]"<?php checked( $count, true ); ?> />
1204 <?php _e( 'Show post counts' ); ?>
1205 </label>
1206 <br />
1207 <label for="categories-hierarchical-<?php echo $number; ?>">
1208 <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][hierarchical]"<?php checked( $hierarchical, true ); ?> />
1209 <?php _e( 'Show hierarchy' ); ?>
1210 </label>
1211 </p>
1212
1213 <input type="hidden" name="widget-categories[<?php echo $number; ?>][submit]" value="1" />
1214<?php
1215}
1216
1217/**
1218 * Register categories widget on startup.
1219 *
1220 * @since 2.3.0
1221 */
1222function wp_widget_categories_register() {
1223 if ( !$options = get_option( 'widget_categories' ) )
1224 $options = array();
1225
1226 if ( isset($options['title']) )
1227 $options = wp_widget_categories_upgrade();
1228
1229 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
1230
1231 $name = __( 'Categories' );
1232
1233 $id = false;
1234 foreach ( (array) array_keys($options) as $o ) {
1235 // Old widgets can have null values for some reason
1236 if ( !isset($options[$o]['title']) )
1237 continue;
1238 $id = "categories-$o";
1239 wp_register_sidebar_widget( $id, $name, 'wp_widget_categories', $widget_ops, array( 'number' => $o ) );
1240 wp_register_widget_control( $id, $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => $o ) );
1241 }
1242
1243 // If there are none, we register the widget's existance with a generic template
1244 if ( !$id ) {
1245 wp_register_sidebar_widget( 'categories-1', $name, 'wp_widget_categories', $widget_ops, array( 'number' => -1 ) );
1246 wp_register_widget_control( 'categories-1', $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => -1 ) );
1247 }
1248}
1249
1250/**
1251 * Upgrade previous category widget to current version.
1252 *
1253 * @since 2.3.0
1254 *
1255 * @return array
1256 */
1257function wp_widget_categories_upgrade() {
1258 $options = get_option( 'widget_categories' );
1259
1260 if ( !isset( $options['title'] ) )
1261 return $options;
1262
1263 $newoptions = array( 1 => $options );
1264
1265 update_option( 'widget_categories', $newoptions );
1266
1267 $sidebars_widgets = get_option( 'sidebars_widgets' );
1268 if ( is_array( $sidebars_widgets ) ) {
1269 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
1270 if ( is_array( $widgets ) ) {
1271 foreach ( $widgets as $widget )
1272 $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
1273 } else {
1274 $new_widgets[$sidebar] = $widgets;
1275 }
1276 }
1277 if ( $new_widgets != $sidebars_widgets )
1278 update_option( 'sidebars_widgets', $new_widgets );
1279 }
1280
1281 return $newoptions;
1282}
1283
1284/**
1285 * Display recent entries widget.
1286 *
1287 * @since 2.2.0
1288 *
1289 * @param array $args Widget arguments.
1290 * @return int Displayed cache.
1291 */
1292function wp_widget_recent_entries($args) {
1293 if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
1294 if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
1295 return print($output);
1296 ob_start();
1297 }
1298
1299 extract($args);
1300 $options = get_option('widget_recent_entries');
1301 $title = empty($options['title']) ? __('Recent Posts') : apply_filters('widget_title', $options['title']);
1302 if ( !$number = (int) $options['number'] )
1303 $number = 10;
1304 else if ( $number < 1 )
1305 $number = 1;
1306 else if ( $number > 15 )
1307 $number = 15;
1308
1309 $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
1310 if ($r->have_posts()) :
1311?>
1312 <?php echo $before_widget; ?>
1313 <?php echo $before_title . $title . $after_title; ?>
1314 <ul>
1315 <?php while ($r->have_posts()) : $r->the_post(); ?>
1316 <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
1317 <?php endwhile; ?>
1318 </ul>
1319 <?php echo $after_widget; ?>
1320<?php
1321 wp_reset_query(); // Restore global post data stomped by the_post().
1322 endif;
1323
1324 if ( '%BEG_OF_TITLE%' != $args['before_title'] )
1325 wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
1326}
1327
1328/**
1329 * Remove recent entries widget items cache.
1330 *
1331 * @since 2.2.0
1332 */
1333function wp_flush_widget_recent_entries() {
1334 wp_cache_delete('widget_recent_entries', 'widget');
1335}
1336
1337add_action('save_post', 'wp_flush_widget_recent_entries');
1338add_action('deleted_post', 'wp_flush_widget_recent_entries');
1339add_action('switch_theme', 'wp_flush_widget_recent_entries');
1340
1341/**
1342 * Display and process recent entries widget options form.
1343 *
1344 * @since 2.2.0
1345 */
1346function wp_widget_recent_entries_control() {
1347 $options = $newoptions = get_option('widget_recent_entries');
1348 if ( isset($_POST["recent-entries-submit"]) ) {
1349 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
1350 $newoptions['number'] = (int) $_POST["recent-entries-number"];
1351 }
1352 if ( $options != $newoptions ) {
1353 $options = $newoptions;
1354 update_option('widget_recent_entries', $options);
1355 wp_flush_widget_recent_entries();
1356 }
1357 $title = attribute_escape($options['title']);
1358 if ( !$number = (int) $options['number'] )
1359 $number = 5;
1360?>
1361
1362 <p><label for="recent-entries-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p>
1363 <p>
1364 <label for="recent-entries-number"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-number" name="recent-entries-number" type="text" value="<?php echo $number; ?>" /></label>
1365 <br />
1366 <small><?php _e('(at most 15)'); ?></small>
1367 </p>
1368 <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
1369<?php
1370}
1371
1372/**
1373 * Display recent comments widget.
1374 *
1375 * @since 2.2.0
1376 *
1377 * @param array $args Widget arguments.
1378 */
1379function wp_widget_recent_comments($args) {
1380 global $wpdb, $comments, $comment;
1381 extract($args, EXTR_SKIP);
1382 $options = get_option('widget_recent_comments');
1383 $title = empty($options['title']) ? __('Recent Comments') : apply_filters('widget_title', $options['title']);
1384 if ( !$number = (int) $options['number'] )
1385 $number = 5;
1386 else if ( $number < 1 )
1387 $number = 1;
1388 else if ( $number > 15 )
1389 $number = 15;
1390
1391 if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
1392 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
1393 wp_cache_add( 'recent_comments', $comments, 'widget' );
1394 }
1395?>
1396
1397 <?php echo $before_widget; ?>
1398 <?php echo $before_title . $title . $after_title; ?>
1399 <ul id="recentcomments"><?php
1400 if ( $comments ) : foreach ( (array) $comments as $comment) :
1401 echo '<li class="recentcomments">' . sprintf(__('%1$s on %2$s'), get_comment_author_link(), '<a href="'. get_comment_link($comment->comment_ID) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
1402 endforeach; endif;?></ul>
1403 <?php echo $after_widget; ?>
1404<?php
1405}
1406
1407/**
1408 * Remove the cache for recent comments widget.
1409 *
1410 * @since 2.2.0
1411 */
1412function wp_delete_recent_comments_cache() {
1413 wp_cache_delete( 'recent_comments', 'widget' );
1414}
1415add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
1416add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
1417
1418/**
1419 * Display and process recent comments widget options form.
1420 *
1421 * @since 2.2.0
1422 */
1423function wp_widget_recent_comments_control() {
1424 $options = $newoptions = get_option('widget_recent_comments');
1425 if ( isset($_POST["recent-comments-submit"]) ) {
1426 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
1427 $newoptions['number'] = (int) $_POST["recent-comments-number"];
1428 }
1429 if ( $options != $newoptions ) {
1430 $options = $newoptions;
1431 update_option('widget_recent_comments', $options);
1432 wp_delete_recent_comments_cache();
1433 }
1434 $title = attribute_escape($options['title']);
1435 if ( !$number = (int) $options['number'] )
1436 $number = 5;
1437?>
1438 <p><label for="recent-comments-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
1439 <p>
1440 <label for="recent-comments-number"><?php _e('Number of comments to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label>
1441 <br />
1442 <small><?php _e('(at most 15)'); ?></small>
1443 </p>
1444 <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
1445<?php
1446}
1447
1448/**
1449 * Display the style for recent comments widget.
1450 *
1451 * @since 2.2.0
1452 */
1453function wp_widget_recent_comments_style() {
1454?>
1455<style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
1456<?php
1457}
1458
1459/**
1460 * Register recent comments with control and hook for 'wp_head' action.
1461 *
1462 * @since 2.2.0
1463 */
1464function wp_widget_recent_comments_register() {
1465 $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
1466 wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops);
1467 wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control');
1468
1469 if ( is_active_widget('wp_widget_recent_comments') )
1470 add_action('wp_head', 'wp_widget_recent_comments_style');
1471}
1472
1473/**
1474 * Display RSS widget.
1475 *
1476 * Allows for multiple widgets to be displayed.
1477 *
1478 * @since 2.2.0
1479 *
1480 * @param array $args Widget arguments.
1481 * @param int $number Widget number.
1482 */
1483function wp_widget_rss($args, $widget_args = 1) {
1484 extract($args, EXTR_SKIP);
1485 if ( is_numeric($widget_args) )
1486 $widget_args = array( 'number' => $widget_args );
1487 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1488 extract($widget_args, EXTR_SKIP);
1489
1490 $options = get_option('widget_rss');
1491
1492 if ( !isset($options[$number]) )
1493 return;
1494
1495 if ( isset($options[$number]['error']) && $options[$number]['error'] )
1496 return;
1497
1498 $url = $options[$number]['url'];
1499 while ( strstr($url, 'http') != $url )
1500 $url = substr($url, 1);
1501 if ( empty($url) )
1502 return;
1503
1504 require_once(ABSPATH . WPINC . '/rss.php');
1505
1506 $rss = fetch_rss($url);
1507 $link = clean_url(strip_tags($rss->channel['link']));
1508 while ( strstr($link, 'http') != $link )
1509 $link = substr($link, 1);
1510 $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
1511 $title = $options[$number]['title'];
1512 if ( empty($title) )
1513 $title = htmlentities(strip_tags($rss->channel['title']));
1514 if ( empty($title) )
1515 $title = $desc;
1516 if ( empty($title) )
1517 $title = __('Unknown Feed');
1518 $title = apply_filters('widget_title', $title );
1519 $url = clean_url(strip_tags($url));
1520 if ( file_exists(dirname(__FILE__) . '/rss.png') )
1521 $icon = str_replace(ABSPATH, site_url() . '/', dirname(__FILE__)) . '/rss.png';
1522 else
1523 $icon = includes_url('images/rss.png');
1524 $title = "<a class='rsswidget' href='$url' title='" . attribute_escape(__('Syndicate this content')) ."'><img style='background:orange;color:white;border:none;' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
1525
1526 echo $before_widget;
1527 echo $before_title . $title . $after_title;
1528
1529 wp_widget_rss_output( $rss, $options[$number] );
1530
1531 echo $after_widget;
1532}
1533
1534/**
1535 * Display the RSS entries in a list.
1536 *
1537 * @since 2.5.0
1538 *
1539 * @param string|array|object $rss RSS url.
1540 * @param array $args Widget arguments.
1541 */
1542function wp_widget_rss_output( $rss, $args = array() ) {
1543 if ( is_string( $rss ) ) {
1544 require_once(ABSPATH . WPINC . '/rss.php');
1545 if ( !$rss = fetch_rss($rss) )
1546 return;
1547 } elseif ( is_array($rss) && isset($rss['url']) ) {
1548 require_once(ABSPATH . WPINC . '/rss.php');
1549 $args = $rss;
1550 if ( !$rss = fetch_rss($rss['url']) )
1551 return;
1552 } elseif ( !is_object($rss) ) {
1553 return;
1554 }
1555
1556 $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
1557 $args = wp_parse_args( $args, $default_args );
1558 extract( $args, EXTR_SKIP );
1559
1560 $items = (int) $items;
1561 if ( $items < 1 || 20 < $items )
1562 $items = 10;
1563 $show_summary = (int) $show_summary;
1564 $show_author = (int) $show_author;
1565 $show_date = (int) $show_date;
1566
1567 if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
1568 $rss->items = array_slice($rss->items, 0, $items);
1569 echo '<ul>';
1570 foreach ( (array) $rss->items as $item ) {
1571 while ( strstr($item['link'], 'http') != $item['link'] )
1572 $item['link'] = substr($item['link'], 1);
1573 $link = clean_url(strip_tags($item['link']));
1574 $title = attribute_escape(strip_tags($item['title']));
1575 if ( empty($title) )
1576 $title = __('Untitled');
1577 $desc = '';
1578 if ( isset( $item['description'] ) && is_string( $item['description'] ) )
1579 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
1580 elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
1581 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
1582 if ( 360 < strlen( $desc ) )
1583 $desc = wp_html_excerpt( $desc, 360 ) . ' [&hellip;]';
1584 $summary = $desc;
1585
1586 if ( $show_summary ) {
1587 $desc = '';
1588 $summary = wp_specialchars( $summary );
1589 $summary = "<div class='rssSummary'>$summary</div>";
1590 } else {
1591 $summary = '';
1592 }
1593
1594 $date = '';
1595 if ( $show_date ) {
1596 if ( isset($item['pubdate']) )
1597 $date = $item['pubdate'];
1598 elseif ( isset($item['published']) )
1599 $date = $item['published'];
1600
1601 if ( $date ) {
1602 if ( $date_stamp = strtotime( $date ) )
1603 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
1604 else
1605 $date = '';
1606 }
1607 }
1608
1609 $author = '';
1610 if ( $show_author ) {
1611 if ( isset($item['dc']['creator']) )
1612 $author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
1613 elseif ( isset($item['author_name']) )
1614 $author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
1615 }
1616
1617 if ( $link == '' ) {
1618 echo "<li>$title{$date}{$summary}{$author}</li>";
1619 } else {
1620 echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
1621 }
1622}
1623 echo '</ul>';
1624 } else {
1625 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
1626 }
1627}
1628
1629/**
1630 * Display and process RSS widget control form.
1631 *
1632 * @since 2.2.0
1633 *
1634 * @param int $widget_args Widget number.
1635 */
1636function wp_widget_rss_control($widget_args) {
1637 global $wp_registered_widgets;
1638 static $updated = false;
1639
1640 if ( is_numeric($widget_args) )
1641 $widget_args = array( 'number' => $widget_args );
1642 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1643 extract($widget_args, EXTR_SKIP);
1644
1645 $options = get_option('widget_rss');
1646 if ( !is_array($options) )
1647 $options = array();
1648
1649 $urls = array();
1650 foreach ( (array) $options as $option )
1651 if ( isset($option['url']) )
1652 $urls[$option['url']] = true;
1653
1654 if ( !$updated && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['sidebar']) ) {
1655 $sidebar = (string) $_POST['sidebar'];
1656
1657 $sidebars_widgets = wp_get_sidebars_widgets();
1658 if ( isset($sidebars_widgets[$sidebar]) )
1659 $this_sidebar =& $sidebars_widgets[$sidebar];
1660 else
1661 $this_sidebar = array();
1662
1663 foreach ( (array) $this_sidebar as $_widget_id ) {
1664 if ( 'wp_widget_rss' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
1665 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
1666 if ( !in_array( "rss-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
1667 unset($options[$widget_number]);
1668 }
1669 }
1670
1671 foreach( (array) $_POST['widget-rss'] as $widget_number => $widget_rss ) {
1672 if ( !isset($widget_rss['url']) && isset($options[$widget_number]) ) // user clicked cancel
1673 continue;
1674 $widget_rss = stripslashes_deep( $widget_rss );
1675 $url = sanitize_url(strip_tags($widget_rss['url']));
1676 $options[$widget_number] = wp_widget_rss_process( $widget_rss, !isset($urls[$url]) );
1677 }
1678
1679 update_option('widget_rss', $options);
1680 $updated = true;
1681 }
1682
1683 if ( -1 == $number ) {
1684 $title = '';
1685 $url = '';
1686 $items = 10;
1687 $error = false;
1688 $number = '%i%';
1689 $show_summary = 0;
1690 $show_author = 0;
1691 $show_date = 0;
1692 } else {
1693 extract( (array) $options[$number] );
1694 }
1695
1696 wp_widget_rss_form( compact( 'number', 'title', 'url', 'items', 'error', 'show_summary', 'show_author', 'show_date' ) );
1697}
1698
1699/**
1700 * Display RSS widget options form.
1701 *
1702 * The options for what fields are displayed for the RSS form are all booleans
1703 * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
1704 * 'show_date'.
1705 *
1706 * @since 2.5.0
1707 *
1708 * @param array|string $args Values for input fields.
1709 * @param array $inputs Override default display options.
1710 */
1711function wp_widget_rss_form( $args, $inputs = null ) {
1712
1713 $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
1714 $inputs = wp_parse_args( $inputs, $default_inputs );
1715 extract( $args );
1716 extract( $inputs, EXTR_SKIP);
1717
1718 $number = attribute_escape( $number );
1719 $title = attribute_escape( $title );
1720 $url = attribute_escape( $url );
1721 $items = (int) $items;
1722 if ( $items < 1 || 20 < $items )
1723 $items = 10;
1724 $show_summary = (int) $show_summary;
1725 $show_author = (int) $show_author;
1726 $show_date = (int) $show_date;
1727
1728 if ( $inputs['url'] ) :
1729?>
1730 <p>
1731 <label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?>
1732 <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" />
1733 </label>
1734 </p>
1735<?php endif; if ( $inputs['title'] ) : ?>
1736 <p>
1737 <label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?>
1738 <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
1739 </label>
1740 </p>
1741<?php endif; if ( $inputs['items'] ) : ?>
1742 <p>
1743 <label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?>
1744 <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
1745 <?php
1746 for ( $i = 1; $i <= 20; ++$i )
1747 echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
1748 ?>
1749 </select>
1750 </label>
1751 </p>
1752<?php endif; if ( $inputs['show_summary'] ) : ?>
1753 <p>
1754 <label for="rss-show-summary-<?php echo $number; ?>">
1755 <input id="rss-show-summary-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_summary]" type="checkbox" value="1" <?php if ( $show_summary ) echo 'checked="checked"'; ?>/>
1756 <?php _e('Display item content?'); ?>
1757 </label>
1758 </p>
1759<?php endif; if ( $inputs['show_author'] ) : ?>
1760 <p>
1761 <label for="rss-show-author-<?php echo $number; ?>">
1762 <input id="rss-show-author-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_author]" type="checkbox" value="1" <?php if ( $show_author ) echo 'checked="checked"'; ?>/>
1763 <?php _e('Display item author if available?'); ?>
1764 </label>
1765 </p>
1766<?php endif; if ( $inputs['show_date'] ) : ?>
1767 <p>
1768 <label for="rss-show-date-<?php echo $number; ?>">
1769 <input id="rss-show-date-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_date]" type="checkbox" value="1" <?php if ( $show_date ) echo 'checked="checked"'; ?>/>
1770 <?php _e('Display item date?'); ?>
1771 </label>
1772 </p>
1773 <input type="hidden" name="widget-rss[<?php echo $number; ?>][submit]" value="1" />
1774<?php
1775 endif;
1776 foreach ( array_keys($default_inputs) as $input ) :
1777 if ( 'hidden' === $inputs[$input] ) :
1778 $id = str_replace( '_', '-', $input );
1779?>
1780 <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
1781<?php
1782 endif;
1783 endforeach;
1784}
1785
1786/**
1787 * Process RSS feed widget data and optionally retrieve feed items.
1788 *
1789 * The feed widget can not have more than 20 items or it will reset back to the
1790 * default, which is 10.
1791 *
1792 * The resulting array has the feed title, feed url, feed link (from channel),
1793 * feed items, error (if any), and whether to show summary, author, and date.
1794 * All respectively in the order of the array elements.
1795 *
1796 * @since 2.5.0
1797 *
1798 * @param array $widget_rss RSS widget feed data. Expects unescaped data.
1799 * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
1800 * @return array
1801 */
1802function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
1803 $items = (int) $widget_rss['items'];
1804 if ( $items < 1 || 20 < $items )
1805 $items = 10;
1806 $url = sanitize_url(strip_tags( $widget_rss['url'] ));
1807 $title = trim(strip_tags( $widget_rss['title'] ));
1808 $show_summary = (int) $widget_rss['show_summary'];
1809 $show_author = (int) $widget_rss['show_author'];
1810 $show_date = (int) $widget_rss['show_date'];
1811
1812 if ( $check_feed ) {
1813 require_once(ABSPATH . WPINC . '/rss.php');
1814 $rss = fetch_rss($url);
1815 $error = false;
1816 $link = '';
1817 if ( !is_object($rss) ) {
1818 $url = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
1819 $error = sprintf(__('Error in RSS %1$d'), $widget_number );
1820 } else {
1821 $link = clean_url(strip_tags($rss->channel['link']));
1822 while ( strstr($link, 'http') != $link )
1823 $link = substr($link, 1);
1824 }
1825 }
1826
1827 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
1828}
1829
1830/**
1831 * Register RSS widget to allow multiple RSS widgets on startup.
1832 *
1833 * @since 2.2.0
1834 */
1835function wp_widget_rss_register() {
1836 if ( !$options = get_option('widget_rss') )
1837 $options = array();
1838 $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
1839 $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
1840 $name = __('RSS');
1841
1842 $id = false;
1843 foreach ( (array) array_keys($options) as $o ) {
1844 // Old widgets can have null values for some reason
1845 if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
1846 continue;
1847 $id = "rss-$o"; // Never never never translate an id
1848 wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
1849 wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
1850 }
1851
1852 // If there are none, we register the widget's existance with a generic template
1853 if ( !$id ) {
1854 wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
1855 wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
1856 }
1857}
1858
1859/**
1860 * Display tag cloud widget.
1861 *
1862 * @since 2.3.0
1863 *
1864 * @param array $args Widget arguments.
1865 */
1866function wp_widget_tag_cloud($args) {
1867 extract($args);
1868 $options = get_option('widget_tag_cloud');
1869 $title = empty($options['title']) ? __('Tags') : apply_filters('widget_title', $options['title']);
1870
1871 echo $before_widget;
1872 echo $before_title . $title . $after_title;
1873 wp_tag_cloud();
1874 echo $after_widget;
1875}
1876
1877/**
1878 * Manage WordPress Tag Cloud widget options.
1879 *
1880 * Displays management form for changing the tag cloud widget title.
1881 *
1882 * @since 2.3.0
1883 */
1884function wp_widget_tag_cloud_control() {
1885 $options = $newoptions = get_option('widget_tag_cloud');
1886
1887 if ( isset($_POST['tag-cloud-submit']) ) {
1888 $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
1889 }
1890
1891 if ( $options != $newoptions ) {
1892 $options = $newoptions;
1893 update_option('widget_tag_cloud', $options);
1894 }
1895
1896 $title = attribute_escape( $options['title'] );
1897?>
1898 <p><label for="tag-cloud-title">
1899 <?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
1900 </p>
1901 <input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
1902<?php
1903}
1904
1905/**
1906 * Register all of the default WordPress widgets on startup.
1907 *
1908 * Calls 'widgets_init' action after all of the WordPress widgets have been
1909 * registered.
1910 *
1911 * @since 2.2.0
1912 */
1913function wp_widgets_init() {
1914 if ( !is_blog_installed() )
1915 return;
1916
1917 $widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
1918 wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
1919 wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
1920
1921 $widget_ops = array('classname' => 'widget_calendar', 'description' => __( "A calendar of your blog's posts") );
1922 wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
1923 wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control' );
1924
1925 $widget_ops = array('classname' => 'widget_archive', 'description' => __( "A monthly archive of your blog's posts") );
1926 wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $widget_ops);
1927 wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
1928
1929 $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
1930 wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
1931
1932 $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
1933 wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $widget_ops);
1934 wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control' );
1935
1936 $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your blog") );
1937 wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $widget_ops);
1938
1939 $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
1940 wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
1941 wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
1942
1943 $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
1944 wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);
1945 wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
1946
1947 wp_widget_categories_register();
1948 wp_widget_text_register();
1949 wp_widget_rss_register();
1950 wp_widget_recent_comments_register();
1951
1952 do_action('widgets_init');
1953}
1954
1955add_action('init', 'wp_widgets_init', 1);
1956
1957/*
1958 * Pattern for multi-widget (allows multiple instances such as the text widget).
1959 *
1960 * Make sure to close the comments after copying.
1961
1962/**
1963 * Displays widget.
1964 *
1965 * Supports multiple widgets.
1966 *
1967 * @param array $args Widget arguments.
1968 * @param array|int $widget_args Widget number. Which of the several widgets of this type do we mean.
1969 * /
1970function widget_many( $args, $widget_args = 1 ) {
1971 extract( $args, EXTR_SKIP );
1972 if ( is_numeric($widget_args) )
1973 $widget_args = array( 'number' => $widget_args );
1974 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
1975 extract( $widget_args, EXTR_SKIP );
1976
1977 // Data should be stored as array: array( number => data for that instance of the widget, ... )
1978 $options = get_option('widget_many');
1979 if ( !isset($options[$number]) )
1980 return;
1981
1982 echo $before_widget;
1983
1984 // Do stuff for this widget, drawing data from $options[$number]
1985
1986 echo $after_widget;
1987}
1988
1989/**
1990 * Displays form for a particular instance of the widget.
1991 *
1992 * Also updates the data after a POST submit.
1993 *
1994 * @param array|int $widget_args Widget number. Which of the several widgets of this type do we mean.
1995 * /
1996function widget_many_control( $widget_args = 1 ) {
1997 global $wp_registered_widgets;
1998 static $updated = false; // Whether or not we have already updated the data after a POST submit
1999
2000 if ( is_numeric($widget_args) )
2001 $widget_args = array( 'number' => $widget_args );
2002 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
2003 extract( $widget_args, EXTR_SKIP );
2004
2005 // Data should be stored as array: array( number => data for that instance of the widget, ... )
2006 $options = get_option('widget_many');
2007 if ( !is_array($options) )
2008 $options = array();
2009
2010 // We need to update the data
2011 if ( !$updated && !empty($_POST['sidebar']) ) {
2012 // Tells us what sidebar to put the data in
2013 $sidebar = (string) $_POST['sidebar'];
2014
2015 $sidebars_widgets = wp_get_sidebars_widgets();
2016 if ( isset($sidebars_widgets[$sidebar]) )
2017 $this_sidebar =& $sidebars_widgets[$sidebar];
2018 else
2019 $this_sidebar = array();
2020
2021 foreach ( $this_sidebar as $_widget_id ) {
2022 // Remove all widgets of this type from the sidebar. We'll add the new data in a second. This makes sure we don't get any duplicate data
2023 // since widget ids aren't necessarily persistent across multiple updates
2024 if ( 'widget_many' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
2025 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
2026 if ( !in_array( "many-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
2027 unset($options[$widget_number]);
2028 }
2029 }
2030
2031 foreach ( (array) $_POST['widget-many'] as $widget_number => $widget_many_instance ) {
2032 // compile data from $widget_many_instance
2033 if ( !isset($widget_many_instance['something']) && isset($options[$widget_number]) ) // user clicked cancel
2034 continue;
2035 $something = wp_specialchars( $widget_many_instance['something'] );
2036 $options[$widget_number] = array( 'something' => $something ); // Even simple widgets should store stuff in array, rather than in scalar
2037 }
2038
2039 update_option('widget_many', $options);
2040
2041 $updated = true; // So that we don't go through this more than once
2042 }
2043
2044
2045 // Here we echo out the form
2046 if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
2047 $something = '';
2048 $number = '%i%';
2049 } else {
2050 $something = attribute_escape($options[$number]['something']);
2051 }
2052
2053 // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
2054 // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
2055?>
2056 <p>
2057 <input class="widefat" id="widget-many-something-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][something]" type="text" value="<?php echo $data; ?>" />
2058 <input type="hidden" id="widget-many-submit-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][submit]" value="1" />
2059 </p>
2060<?php
2061}
2062
2063/**
2064 * Registers each instance of our widget on startup.
2065 * /
2066function widget_many_register() {
2067 if ( !$options = get_option('widget_many') )
2068 $options = array();
2069
2070 $widget_ops = array('classname' => 'widget_many', 'description' => __('Widget which allows multiple instances'));
2071 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'many');
2072 $name = __('Many');
2073
2074 $registered = false;
2075 foreach ( array_keys($options) as $o ) {
2076 // Old widgets can have null values for some reason
2077 if ( !isset($options[$o]['something']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
2078 continue;
2079
2080 // $id should look like {$id_base}-{$o}
2081 $id = "many-$o"; // Never never never translate an id
2082 $registered = true;
2083 wp_register_sidebar_widget( $id, $name, 'widget_many', $widget_ops, array( 'number' => $o ) );
2084 wp_register_widget_control( $id, $name, 'widget_many_control', $control_ops, array( 'number' => $o ) );
2085 }
2086
2087 // If there are none, we register the widget's existance with a generic template
2088 if ( !$registered ) {
2089 wp_register_sidebar_widget( 'many-1', $name, 'widget_many', $widget_ops, array( 'number' => -1 ) );
2090 wp_register_widget_control( 'many-1', $name, 'widget_many_control', $control_ops, array( 'number' => -1 ) );
2091 }
2092}
2093
2094// This is important
2095add_action( 'widgets_init', 'widget_many_register' )
2096
2097*/
2098
2099?>