-
+ F7EC8D95E978C7A07F1954C334189C68E142C63F797FA07A7493ECB2FF19C97052AC990CF39532A3D2E0CE7548E789D0C44D78AE44F1A31438F945B465D814A8
mp-wp/wp-includes/widgets.php
(0 . 0)(1 . 2099)
156032 <?php
156033 /**
156034 * API for creating dynamic sidebar without hardcoding functionality into
156035 * themes. Includes both internal WordPress routines and theme use routines.
156036 *
156037 * This functionality was found in a plugin before WordPress 2.2 release which
156038 * included it in the core from that point on.
156039 *
156040 * @link http://codex.wordpress.org/Plugins/WordPress_Widgets WordPress Widgets
156041 * @link http://codex.wordpress.org/Plugins/WordPress_Widgets_Api Widgets API
156042 *
156043 * @package WordPress
156044 * @subpackage Widgets
156045 */
156046
156047 /* Global Variables */
156048
156049 /** @ignore */
156050 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
156051
156052 /**
156053 * Stores the sidebars, since many themes can have more than one.
156054 *
156055 * @global array $wp_registered_sidebars
156056 * @since 2.2.0
156057 */
156058 $wp_registered_sidebars = array();
156059
156060 /**
156061 * Stores the registered widgets.
156062 *
156063 * @global array $wp_registered_widgets
156064 * @since 2.2.0
156065 */
156066 $wp_registered_widgets = array();
156067
156068 /**
156069 * Stores the registered widget control (options).
156070 *
156071 * @global array $wp_registered_widget_controls
156072 * @since 2.2.0
156073 */
156074 $wp_registered_widget_controls = array();
156075
156076 /* Template tags & API functions */
156077
156078 /**
156079 * Creates multiple sidebars.
156080 *
156081 * If you wanted to quickly create multiple sidebars for a theme or internally.
156082 * This function will allow you to do so. If you don't pass the 'name' and/or
156083 * 'id' in $args, then they will be built for you.
156084 *
156085 * The default for the name is "Sidebar #", with '#' being replaced with the
156086 * number the sidebar is currently when greater than one. If first sidebar, the
156087 * name will be just "Sidebar". The default for id is "sidebar-" followed by the
156088 * number the sidebar creation is currently at.
156089 *
156090 * @since 2.2.0
156091 *
156092 * @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
156093 * @uses parse_str() Converts a string to an array to be used in the rest of the function.
156094 * @uses register_sidebar() Sends single sidebar information [name, id] to this
156095 * function to handle building the sidebar.
156096 *
156097 * @param int $number Number of sidebars to create.
156098 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values.
156099 */
156100 function register_sidebars($number = 1, $args = array()) {
156101 global $wp_registered_sidebars;
156102 $number = (int) $number;
156103
156104 if ( is_string($args) )
156105 parse_str($args, $args);
156106
156107 for ( $i=1; $i <= $number; $i++ ) {
156108 $_args = $args;
156109
156110 if ( $number > 1 ) {
156111 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i);
156112 } else {
156113 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar');
156114 }
156115
156116 if (isset($args['id'])) {
156117 $_args['id'] = $args['id'];
156118 } else {
156119 $n = count($wp_registered_sidebars);
156120 do {
156121 $n++;
156122 $_args['id'] = "sidebar-$n";
156123 } while (isset($wp_registered_sidebars[$_args['id']]));
156124 }
156125
156126 register_sidebar($_args);
156127 }
156128 }
156129
156130 /**
156131 * Builds the definition for a single sidebar and returns the ID.
156132 *
156133 * The $args parameter takes either a string or an array with 'name' and 'id'
156134 * contained in either usage. It will be noted that the values will be applied
156135 * to all sidebars, so if creating more than one, it will be advised to allow
156136 * for WordPress to create the defaults for you.
156137 *
156138 * Example for string would be <code>'name=whatever;id=whatever1'</code> and for
156139 * the array it would be <code>array(
156140 * 'name' => 'whatever',
156141 * 'id' => 'whatever1')</code>.
156142 *
156143 * name - The name of the sidebar, which presumably the title which will be
156144 * displayed.
156145 * id - The unique identifier by which the sidebar will be called by.
156146 * before_widget - The content that will prepended to the widgets when they are
156147 * displayed.
156148 * after_widget - The content that will be appended to the widgets when they are
156149 * displayed.
156150 * before_title - The content that will be prepended to the title when displayed.
156151 * after_title - the content that will be appended to the title when displayed.
156152 *
156153 * <em>Content</em> is assumed to be HTML and should be formatted as such, but
156154 * doesn't have to be.
156155 *
156156 * @since 2.2.0
156157 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
156158 * @uses parse_str() Converts a string to an array to be used in the rest of the function.
156159 * @usedby register_sidebars()
156160 *
156161 * @param string|array $args Builds Sidebar based off of 'name' and 'id' values
156162 * @return string The sidebar id that was added.
156163 */
156164 function register_sidebar($args = array()) {
156165 global $wp_registered_sidebars;
156166
156167 if ( is_string($args) )
156168 parse_str($args, $args);
156169
156170 $i = count($wp_registered_sidebars) + 1;
156171
156172 $defaults = array(
156173 'name' => sprintf(__('Sidebar %d'), $i ),
156174 'id' => "sidebar-$i",
156175 'before_widget' => '<li id="%1$s" class="widget %2$s">',
156176 'after_widget' => "</li>\n",
156177 'before_title' => '<h2 class="widgettitle">',
156178 'after_title' => "</h2>\n",
156179 );
156180
156181 $sidebar = array_merge($defaults, (array) $args);
156182
156183 $wp_registered_sidebars[$sidebar['id']] = $sidebar;
156184
156185 return $sidebar['id'];
156186 }
156187
156188 /**
156189 * Removes a sidebar from the list.
156190 *
156191 * @since 2.2.0
156192 *
156193 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
156194 *
156195 * @param string $name The ID of the sidebar when it was added.
156196 */
156197 function unregister_sidebar( $name ) {
156198 global $wp_registered_sidebars;
156199
156200 if ( isset( $wp_registered_sidebars[$name] ) )
156201 unset( $wp_registered_sidebars[$name] );
156202 }
156203
156204 /**
156205 * Register widget for sidebar with backwards compatibility.
156206 *
156207 * Allows $name to be an array that accepts either three elements to grab the
156208 * first element and the third for the name or just uses the first element of
156209 * the array for the name.
156210 *
156211 * Passes to {@link wp_register_sidebar_widget()} after argument list and
156212 * backwards compatibility is complete.
156213 *
156214 * @since 2.2.0
156215 * @uses wp_register_sidebar_widget() Passes the compiled arguments.
156216 *
156217 * @param string|int $name Widget ID.
156218 * @param callback $output_callback Run when widget is called.
156219 * @param string $classname Classname widget option.
156220 * @param mixed $params,... Widget parameters.
156221 */
156222 function register_sidebar_widget($name, $output_callback, $classname = '') {
156223 // Compat
156224 if ( is_array($name) ) {
156225 if ( count($name) == 3 )
156226 $name = sprintf($name[0], $name[2]);
156227 else
156228 $name = $name[0];
156229 }
156230
156231 $id = sanitize_title($name);
156232 $options = array();
156233 if ( !empty($classname) && is_string($classname) )
156234 $options['classname'] = $classname;
156235 $params = array_slice(func_get_args(), 2);
156236 $args = array($id, $name, $output_callback, $options);
156237 if ( !empty($params) )
156238 $args = array_merge($args, $params);
156239
156240 call_user_func_array('wp_register_sidebar_widget', $args);
156241 }
156242
156243 /**
156244 * Register widget for use in sidebars.
156245 *
156246 * The default widget option is 'classname' that can be override.
156247 *
156248 * The function can also be used to unregister widgets when $output_callback
156249 * parameter is an empty string.
156250 *
156251 * @since 2.2.0
156252 *
156253 * @uses $wp_registered_widgets Uses stored registered widgets.
156254 * @uses $wp_register_widget_defaults Retrieves widget defaults.
156255 *
156256 * @param int|string $id Widget ID.
156257 * @param string $name Widget display title.
156258 * @param callback $output_callback Run when widget is called.
156259 * @param array|string Optional. $options Widget Options.
156260 * @param mixed $params,... Widget parameters to add to widget.
156261 * @return null Will return if $output_callback is empty after removing widget.
156262 */
156263 function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) {
156264 global $wp_registered_widgets;
156265
156266 $id = strtolower($id);
156267
156268 if ( empty($output_callback) ) {
156269 unset($wp_registered_widgets[$id]);
156270 return;
156271 }
156272
156273 $defaults = array('classname' => $output_callback);
156274 $options = wp_parse_args($options, $defaults);
156275 $widget = array(
156276 'name' => $name,
156277 'id' => $id,
156278 'callback' => $output_callback,
156279 'params' => array_slice(func_get_args(), 4)
156280 );
156281 $widget = array_merge($widget, $options);
156282
156283 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) )
156284 $wp_registered_widgets[$id] = $widget;
156285 }
156286
156287 /**
156288 * Retrieve description for widget.
156289 *
156290 * When registering widgets, the options can also include 'description' that
156291 * describes the widget for display on the widget administration panel or
156292 * in the theme.
156293 *
156294 * @since 2.5.0
156295 *
156296 * @param int|string $id Widget ID.
156297 * @return string Widget description, if available. Null on failure to retrieve description.
156298 */
156299 function wp_widget_description( $id ) {
156300 if ( !is_scalar($id) )
156301 return;
156302
156303 global $wp_registered_widgets;
156304
156305 if ( isset($wp_registered_widgets[$id]['description']) )
156306 return wp_specialchars( $wp_registered_widgets[$id]['description'] );
156307 }
156308
156309 /**
156310 * Alias of {@link wp_unregister_sidebar_widget()}.
156311 *
156312 * @see wp_unregister_sidebar_widget()
156313 *
156314 * @since 2.2.0
156315 *
156316 * @param int|string $id Widget ID.
156317 */
156318 function unregister_sidebar_widget($id) {
156319 return wp_unregister_sidebar_widget($id);
156320 }
156321
156322 /**
156323 * Remove widget from sidebar.
156324 *
156325 * @since 2.2.0
156326 *
156327 * @param int|string $id Widget ID.
156328 */
156329 function wp_unregister_sidebar_widget($id) {
156330 wp_register_sidebar_widget($id, '', '');
156331 wp_unregister_widget_control($id);
156332 }
156333
156334 /**
156335 * Registers widget control callback for customizing options.
156336 *
156337 * Allows $name to be an array that accepts either three elements to grab the
156338 * first element and the third for the name or just uses the first element of
156339 * the array for the name.
156340 *
156341 * Passes to {@link wp_register_widget_control()} after the argument list has
156342 * been compiled.
156343 *
156344 * @since 2.2.0
156345 *
156346 * @param int|string $name Sidebar ID.
156347 * @param callback $control_callback Widget control callback to display and process form.
156348 * @param int $width Widget width.
156349 * @param int $height Widget height.
156350 */
156351 function register_widget_control($name, $control_callback, $width = '', $height = '') {
156352 // Compat
156353 if ( is_array($name) ) {
156354 if ( count($name) == 3 )
156355 $name = sprintf($name[0], $name[2]);
156356 else
156357 $name = $name[0];
156358 }
156359
156360 $id = sanitize_title($name);
156361 $options = array();
156362 if ( !empty($width) )
156363 $options['width'] = $width;
156364 if ( !empty($height) )
156365 $options['height'] = $height;
156366 $params = array_slice(func_get_args(), 4);
156367 $args = array($id, $name, $control_callback, $options);
156368 if ( !empty($params) )
156369 $args = array_merge($args, $params);
156370
156371 call_user_func_array('wp_register_widget_control', $args);
156372 }
156373
156374 /**
156375 * Registers widget control callback for customizing options.
156376 *
156377 * The options contains the 'height', 'width', and 'id_base' keys. The 'height'
156378 * option is never used. The 'width' option is the width of the fully expanded
156379 * control form, but try hard to use the default width. The 'id_base' is for
156380 * multi-widgets (widgets which allow multiple instances such as the text
156381 * widget), an id_base must be provided. The widget id will end up looking like
156382 * {$id_base}-{$unique_number}.
156383 *
156384 * @since 2.2.0
156385 *
156386 * @param int|string $id Sidebar ID.
156387 * @param string $name Sidebar display name.
156388 * @param callback $control_callback Run when sidebar is displayed.
156389 * @param array|string $options Optional. Widget options. See above long description.
156390 * @param mixed $params,... Optional. Additional parameters to add to widget.
156391 */
156392 function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
156393 global $wp_registered_widget_controls;
156394
156395 $id = strtolower($id);
156396
156397 if ( empty($control_callback) ) {
156398 unset($wp_registered_widget_controls[$id]);
156399 return;
156400 }
156401
156402 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
156403 return;
156404
156405 $defaults = array('width' => 250, 'height' => 200 ); // height is never used
156406 $options = wp_parse_args($options, $defaults);
156407 $options['width'] = (int) $options['width'];
156408 $options['height'] = (int) $options['height'];
156409
156410 $widget = array(
156411 'name' => $name,
156412 'id' => $id,
156413 'callback' => $control_callback,
156414 'params' => array_slice(func_get_args(), 4)
156415 );
156416 $widget = array_merge($widget, $options);
156417
156418 $wp_registered_widget_controls[$id] = $widget;
156419 }
156420
156421 /**
156422 * Alias of {@link wp_unregister_widget_control()}.
156423 *
156424 * @since 2.2.0
156425 * @see wp_unregister_widget_control()
156426 *
156427 * @param int|string $id Widget ID.
156428 */
156429 function unregister_widget_control($id) {
156430 return wp_unregister_widget_control($id);
156431 }
156432
156433 /**
156434 * Remove control callback for widget.
156435 *
156436 * @since 2.2.0
156437 * @uses wp_register_widget_control() Unregisters by using empty callback.
156438 *
156439 * @param int|string $id Widget ID.
156440 */
156441 function wp_unregister_widget_control($id) {
156442 return wp_register_widget_control($id, '', '');
156443 }
156444
156445 /**
156446 * Display dynamic sidebar.
156447 *
156448 * By default it displays the default sidebar or 'sidebar-1'. The 'sidebar-1' is
156449 * not named by the theme, the actual name is '1', but 'sidebar-' is added to
156450 * the registered sidebars for the name. If you named your sidebar 'after-post',
156451 * then the parameter $index will still be 'after-post', but the lookup will be
156452 * for 'sidebar-after-post'.
156453 *
156454 * It is confusing for the $index parameter, but just know that it should just
156455 * work. When you register the sidebar in the theme, you will use the same name
156456 * for this function or "Pay no heed to the man behind the curtain." Just accept
156457 * it as an oddity of WordPress sidebar register and display.
156458 *
156459 * @since 2.2.0
156460 *
156461 * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
156462 * @return bool True, if widget sidebar was found and called. False if not found or not called.
156463 */
156464 function dynamic_sidebar($index = 1) {
156465 global $wp_registered_sidebars, $wp_registered_widgets;
156466
156467 if ( is_int($index) ) {
156468 $index = "sidebar-$index";
156469 } else {
156470 $index = sanitize_title($index);
156471 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
156472 if ( sanitize_title($value['name']) == $index ) {
156473 $index = $key;
156474 break;
156475 }
156476 }
156477 }
156478
156479 $sidebars_widgets = wp_get_sidebars_widgets();
156480
156481 if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
156482 return false;
156483
156484 $sidebar = $wp_registered_sidebars[$index];
156485
156486 $did_one = false;
156487 foreach ( (array) $sidebars_widgets[$index] as $id ) {
156488 $params = array_merge(
156489 array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
156490 (array) $wp_registered_widgets[$id]['params']
156491 );
156492
156493 // Substitute HTML id and class attributes into before_widget
156494 $classname_ = '';
156495 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
156496 if ( is_string($cn) )
156497 $classname_ .= '_' . $cn;
156498 elseif ( is_object($cn) )
156499 $classname_ .= '_' . get_class($cn);
156500 }
156501 $classname_ = ltrim($classname_, '_');
156502 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
156503
156504 $params = apply_filters( 'dynamic_sidebar_params', $params );
156505
156506 $callback = $wp_registered_widgets[$id]['callback'];
156507
156508 if ( is_callable($callback) ) {
156509 call_user_func_array($callback, $params);
156510 $did_one = true;
156511 }
156512 }
156513
156514 return $did_one;
156515 }
156516
156517 /**
156518 * Whether widget is registered using callback with widget ID.
156519 *
156520 * Will only check if both parameters are used. Used to find which sidebar the
156521 * widget is located in, but requires that both the callback and the widget ID
156522 * be known.
156523 *
156524 * @since 2.2.0
156525 *
156526 * @param callback $callback Widget callback to check.
156527 * @param int $widget_id Optional, but needed for checking. Widget ID.
156528 /* @return mixed false if widget is not active or id of sidebar in which the widget is active.
156529 */
156530 function is_active_widget($callback, $widget_id = false) {
156531 global $wp_registered_widgets;
156532
156533 $sidebars_widgets = wp_get_sidebars_widgets(false);
156534
156535 if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
156536 if ( is_array($widgets) ) foreach ( $widgets as $widget )
156537 if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
156538 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
156539 return $sidebar;
156540
156541
156542 return false;
156543 }
156544
156545 /**
156546 * Whether the dynamic sidebar is enabled and used by theme.
156547 *
156548 * @since 2.2.0
156549 *
156550 * @return bool True, if using widgets. False, if not using widgets.
156551 */
156552 function is_dynamic_sidebar() {
156553 global $wp_registered_widgets, $wp_registered_sidebars;
156554 $sidebars_widgets = get_option('sidebars_widgets');
156555 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) {
156556 if ( count($sidebars_widgets[$index]) ) {
156557 foreach ( (array) $sidebars_widgets[$index] as $widget )
156558 if ( array_key_exists($widget, $wp_registered_widgets) )
156559 return true;
156560 }
156561 }
156562 return false;
156563 }
156564
156565 /* Internal Functions */
156566
156567 /**
156568 * Retrieve full list of sidebars and their widgets.
156569 *
156570 * Will upgrade sidebar widget list, if needed. Will also save updated list, if
156571 * needed.
156572 *
156573 * @since 2.2.0
156574 * @access private
156575 *
156576 * @param bool $update Optional, default is true. Whether to save upgrade of widget array list.
156577 * @return array Upgraded list of widgets to version 2 array format.
156578 */
156579 function wp_get_sidebars_widgets($update = true) {
156580 global $wp_registered_widgets, $wp_registered_sidebars;
156581
156582 $sidebars_widgets = get_option('sidebars_widgets');
156583 $_sidebars_widgets = array();
156584
156585 if ( !isset($sidebars_widgets['array_version']) )
156586 $sidebars_widgets['array_version'] = 1;
156587
156588 switch ( $sidebars_widgets['array_version'] ) {
156589 case 1 :
156590 foreach ( (array) $sidebars_widgets as $index => $sidebar )
156591 if ( is_array($sidebar) )
156592 foreach ( (array) $sidebar as $i => $name ) {
156593 $id = strtolower($name);
156594 if ( isset($wp_registered_widgets[$id]) ) {
156595 $_sidebars_widgets[$index][$i] = $id;
156596 continue;
156597 }
156598 $id = sanitize_title($name);
156599 if ( isset($wp_registered_widgets[$id]) ) {
156600 $_sidebars_widgets[$index][$i] = $id;
156601 continue;
156602 }
156603
156604 $found = false;
156605
156606 foreach ( $wp_registered_widgets as $widget_id => $widget ) {
156607 if ( strtolower($widget['name']) == strtolower($name) ) {
156608 $_sidebars_widgets[$index][$i] = $widget['id'];
156609 $found = true;
156610 break;
156611 } elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
156612 $_sidebars_widgets[$index][$i] = $widget['id'];
156613 $found = true;
156614 break;
156615 }
156616 }
156617
156618 if ( $found )
156619 continue;
156620
156621 unset($_sidebars_widgets[$index][$i]);
156622 }
156623 $_sidebars_widgets['array_version'] = 2;
156624 $sidebars_widgets = $_sidebars_widgets;
156625 unset($_sidebars_widgets);
156626
156627 case 2 :
156628 $sidebars = array_keys( $wp_registered_sidebars );
156629 if ( !empty( $sidebars ) ) {
156630 // Move the known-good ones first
156631 foreach ( (array) $sidebars as $id ) {
156632 if ( array_key_exists( $id, $sidebars_widgets ) ) {
156633 $_sidebars_widgets[$id] = $sidebars_widgets[$id];
156634 unset($sidebars_widgets[$id], $sidebars[$id]);
156635 }
156636 }
156637
156638 // Assign to each unmatched registered sidebar the first available orphan
156639 unset( $sidebars_widgets[ 'array_version' ] );
156640 while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
156641 $_sidebars_widgets[ $sidebar ] = $widgets;
156642
156643 $_sidebars_widgets['array_version'] = 3;
156644 $sidebars_widgets = $_sidebars_widgets;
156645 unset($_sidebars_widgets);
156646 }
156647
156648 if ( $update )
156649 update_option('sidebars_widgets', $sidebars_widgets);
156650 }
156651
156652 unset($sidebars_widgets['array_version']);
156653
156654 $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets);
156655 return $sidebars_widgets;
156656 }
156657
156658 /**
156659 * Set the sidebar widget option to update sidebars.
156660 *
156661 * @since 2.2.0
156662 * @access private
156663 *
156664 * @param array $sidebars_widgets Sidebar widgets and their settings.
156665 */
156666 function wp_set_sidebars_widgets( $sidebars_widgets ) {
156667 if ( !isset( $sidebars_widgets['array_version'] ) )
156668 $sidebars_widgets['array_version'] = 3;
156669 update_option( 'sidebars_widgets', $sidebars_widgets );
156670 }
156671
156672 /**
156673 * Retrieve default registered sidebars list.
156674 *
156675 * @since 2.2.0
156676 * @access private
156677 *
156678 * @return array
156679 */
156680 function wp_get_widget_defaults() {
156681 global $wp_registered_sidebars;
156682
156683 $defaults = array();
156684
156685 foreach ( (array) $wp_registered_sidebars as $index => $sidebar )
156686 $defaults[$index] = array();
156687
156688 return $defaults;
156689 }
156690
156691 /* Default Widgets */
156692
156693 /**
156694 * Display pages widget.
156695 *
156696 * @since 2.2.0
156697 *
156698 * @param array $args Widget arguments.
156699 */
156700 function wp_widget_pages( $args ) {
156701 extract( $args );
156702 $options = get_option( 'widget_pages' );
156703
156704 $title = empty( $options['title'] ) ? __( 'Pages' ) : apply_filters('widget_title', $options['title']);
156705 $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby'];
156706 $exclude = empty( $options['exclude'] ) ? '' : $options['exclude'];
156707
156708 if ( $sortby == 'menu_order' ) {
156709 $sortby = 'menu_order, post_title';
156710 }
156711
156712 $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) );
156713
156714 if ( !empty( $out ) ) {
156715 ?>
156716 <?php echo $before_widget; ?>
156717 <?php echo $before_title . $title . $after_title; ?>
156718 <ul>
156719 <?php echo $out; ?>
156720 </ul>
156721 <?php echo $after_widget; ?>
156722 <?php
156723 }
156724 }
156725
156726 /**
156727 * Display and process pages widget options form.
156728 *
156729 * @since 2.2.0
156730 */
156731 function wp_widget_pages_control() {
156732 $options = $newoptions = get_option('widget_pages');
156733 if ( isset($_POST['pages-submit']) ) {
156734 $newoptions['title'] = strip_tags(stripslashes($_POST['pages-title']));
156735
156736 $sortby = stripslashes( $_POST['pages-sortby'] );
156737
156738 if ( in_array( $sortby, array( 'post_title', 'menu_order', 'ID' ) ) ) {
156739 $newoptions['sortby'] = $sortby;
156740 } else {
156741 $newoptions['sortby'] = 'menu_order';
156742 }
156743
156744 $newoptions['exclude'] = strip_tags( stripslashes( $_POST['pages-exclude'] ) );
156745 }
156746 if ( $options != $newoptions ) {
156747 $options = $newoptions;
156748 update_option('widget_pages', $options);
156749 }
156750 $title = attribute_escape($options['title']);
156751 $exclude = attribute_escape( $options['exclude'] );
156752 ?>
156753 <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>
156754 <p>
156755 <label for="pages-sortby"><?php _e( 'Sort by:' ); ?>
156756 <select name="pages-sortby" id="pages-sortby" class="widefat">
156757 <option value="post_title"<?php selected( $options['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
156758 <option value="menu_order"<?php selected( $options['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
156759 <option value="ID"<?php selected( $options['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
156760 </select>
156761 </label>
156762 </p>
156763 <p>
156764 <label for="pages-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="pages-exclude" id="pages-exclude" class="widefat" /></label>
156765 <br />
156766 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
156767 </p>
156768 <input type="hidden" id="pages-submit" name="pages-submit" value="1" />
156769 <?php
156770 }
156771
156772 /**
156773 * Display links widget.
156774 *
156775 * @since 2.2.0
156776 *
156777 * @param array $args Widget arguments.
156778 */
156779 function wp_widget_links($args) {
156780 extract($args, EXTR_SKIP);
156781
156782 $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
156783 wp_list_bookmarks(apply_filters('widget_links_args', array(
156784 'title_before' => $before_title, 'title_after' => $after_title,
156785 'category_before' => $before_widget, 'category_after' => $after_widget,
156786 'show_images' => true, 'class' => 'linkcat widget'
156787 )));
156788 }
156789
156790 /**
156791 * Display search widget.
156792 *
156793 * @since 2.2.0
156794 *
156795 * @param array $args Widget arguments.
156796 */
156797 function wp_widget_search($args) {
156798 extract($args);
156799 echo $before_widget;
156800
156801 // Use current theme search form if it exists
156802 get_search_form();
156803
156804 echo $after_widget;
156805 }
156806
156807 /**
156808 * Display archives widget.
156809 *
156810 * @since 2.2.0
156811 *
156812 * @param array $args Widget arguments.
156813 */
156814 function wp_widget_archives($args) {
156815 extract($args);
156816 $options = get_option('widget_archives');
156817 $c = $options['count'] ? '1' : '0';
156818 $d = $options['dropdown'] ? '1' : '0';
156819 $title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);
156820
156821 echo $before_widget;
156822 echo $before_title . $title . $after_title;
156823
156824 if($d) {
156825 ?>
156826 <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>
156827 <?php
156828 } else {
156829 ?>
156830 <ul>
156831 <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
156832 </ul>
156833 <?php
156834 }
156835
156836 echo $after_widget;
156837 }
156838
156839 /**
156840 * Display and process archives widget options form.
156841 *
156842 * @since 2.2.0
156843 */
156844 function wp_widget_archives_control() {
156845 $options = $newoptions = get_option('widget_archives');
156846 if ( isset($_POST["archives-submit"]) ) {
156847 $newoptions['count'] = isset($_POST['archives-count']);
156848 $newoptions['dropdown'] = isset($_POST['archives-dropdown']);
156849 $newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
156850 }
156851 if ( $options != $newoptions ) {
156852 $options = $newoptions;
156853 update_option('widget_archives', $options);
156854 }
156855 $count = $options['count'] ? 'checked="checked"' : '';
156856 $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
156857 $title = attribute_escape($options['title']);
156858 ?>
156859 <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>
156860 <p>
156861 <label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
156862 <br />
156863 <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>
156864 </p>
156865 <input type="hidden" id="archives-submit" name="archives-submit" value="1" />
156866 <?php
156867 }
156868
156869 /**
156870 * Display meta widget.
156871 *
156872 * Displays log in/out, RSS feed links, etc.
156873 *
156874 * @since 2.2.0
156875 *
156876 * @param array $args Widget arguments.
156877 */
156878 function wp_widget_meta($args) {
156879 extract($args);
156880 $options = get_option('widget_meta');
156881 $title = empty($options['title']) ? __('Meta') : apply_filters('widget_title', $options['title']);
156882 ?>
156883 <?php echo $before_widget; ?>
156884 <?php echo $before_title . $title . $after_title; ?>
156885 <ul>
156886 <?php wp_register(); ?>
156887 <li><?php wp_loginout(); ?></li>
156888 <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>
156889 <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>
156890 <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>
156891 <?php wp_meta(); ?>
156892 </ul>
156893 <?php echo $after_widget; ?>
156894 <?php
156895 }
156896
156897 /**
156898 * Display and process meta widget options form.
156899 *
156900 * @since 2.2.0
156901 */
156902 function wp_widget_meta_control() {
156903 $options = $newoptions = get_option('widget_meta');
156904 if ( isset($_POST["meta-submit"]) ) {
156905 $newoptions['title'] = strip_tags(stripslashes($_POST["meta-title"]));
156906 }
156907 if ( $options != $newoptions ) {
156908 $options = $newoptions;
156909 update_option('widget_meta', $options);
156910 }
156911 $title = attribute_escape($options['title']);
156912 ?>
156913 <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>
156914 <input type="hidden" id="meta-submit" name="meta-submit" value="1" />
156915 <?php
156916 }
156917
156918 /**
156919 * Display calendar widget.
156920 *
156921 * @since 2.2.0
156922 *
156923 * @param array $args Widget arguments.
156924 */
156925 function wp_widget_calendar($args) {
156926 extract($args);
156927 $options = get_option('widget_calendar');
156928 $title = apply_filters('widget_title', $options['title']);
156929 if ( empty($title) )
156930 $title = ' ';
156931 echo $before_widget . $before_title . $title . $after_title;
156932 echo '<div id="calendar_wrap">';
156933 get_calendar();
156934 echo '</div>';
156935 echo $after_widget;
156936 }
156937
156938 /**
156939 * Display and process calendar widget options form.
156940 *
156941 * @since 2.2.0
156942 */
156943 function wp_widget_calendar_control() {
156944 $options = $newoptions = get_option('widget_calendar');
156945 if ( isset($_POST["calendar-submit"]) ) {
156946 $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"]));
156947 }
156948 if ( $options != $newoptions ) {
156949 $options = $newoptions;
156950 update_option('widget_calendar', $options);
156951 }
156952 $title = attribute_escape($options['title']);
156953 ?>
156954 <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>
156955 <input type="hidden" id="calendar-submit" name="calendar-submit" value="1" />
156956 <?php
156957 }
156958
156959 /**
156960 * Display the Text widget, depending on the widget number.
156961 *
156962 * Supports multiple text widgets and keeps track of the widget number by using
156963 * the $widget_args parameter. The option 'widget_text' is used to store the
156964 * content for the widgets. The content and title are passed through the
156965 * 'widget_text' and 'widget_title' filters respectively.
156966 *
156967 * @since 2.2.0
156968 *
156969 * @param array $args Widget arguments.
156970 * @param int $number Widget number.
156971 */
156972 function wp_widget_text($args, $widget_args = 1) {
156973 extract( $args, EXTR_SKIP );
156974 if ( is_numeric($widget_args) )
156975 $widget_args = array( 'number' => $widget_args );
156976 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
156977 extract( $widget_args, EXTR_SKIP );
156978
156979 $options = get_option('widget_text');
156980 if ( !isset($options[$number]) )
156981 return;
156982
156983 $title = apply_filters('widget_title', $options[$number]['title']);
156984 $text = apply_filters( 'widget_text', $options[$number]['text'] );
156985 ?>
156986 <?php echo $before_widget; ?>
156987 <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
156988 <div class="textwidget"><?php echo $text; ?></div>
156989 <?php echo $after_widget; ?>
156990 <?php
156991 }
156992
156993 /**
156994 * Display and process text widget options form.
156995 *
156996 * @since 2.2.0
156997 *
156998 * @param int $widget_args Widget number.
156999 */
157000 function wp_widget_text_control($widget_args) {
157001 global $wp_registered_widgets;
157002 static $updated = false;
157003
157004 if ( is_numeric($widget_args) )
157005 $widget_args = array( 'number' => $widget_args );
157006 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
157007 extract( $widget_args, EXTR_SKIP );
157008
157009 $options = get_option('widget_text');
157010 if ( !is_array($options) )
157011 $options = array();
157012
157013 if ( !$updated && !empty($_POST['sidebar']) ) {
157014 $sidebar = (string) $_POST['sidebar'];
157015
157016 $sidebars_widgets = wp_get_sidebars_widgets();
157017 if ( isset($sidebars_widgets[$sidebar]) )
157018 $this_sidebar =& $sidebars_widgets[$sidebar];
157019 else
157020 $this_sidebar = array();
157021
157022 foreach ( (array) $this_sidebar as $_widget_id ) {
157023 if ( 'wp_widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
157024 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
157025 if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
157026 unset($options[$widget_number]);
157027 }
157028 }
157029
157030 foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
157031 if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
157032 continue;
157033 $title = strip_tags(stripslashes($widget_text['title']));
157034 if ( current_user_can('unfiltered_html') )
157035 $text = stripslashes( $widget_text['text'] );
157036 else
157037 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
157038 $options[$widget_number] = compact( 'title', 'text' );
157039 }
157040
157041 update_option('widget_text', $options);
157042 $updated = true;
157043 }
157044
157045 if ( -1 == $number ) {
157046 $title = '';
157047 $text = '';
157048 $number = '%i%';
157049 } else {
157050 $title = attribute_escape($options[$number]['title']);
157051 $text = format_to_edit($options[$number]['text']);
157052 }
157053 ?>
157054 <p>
157055 <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
157056 <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
157057 <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
157058 </p>
157059 <?php
157060 }
157061
157062 /**
157063 * Register text widget on startup.
157064 *
157065 * @since 2.2.0
157066 */
157067 function wp_widget_text_register() {
157068 if ( !$options = get_option('widget_text') )
157069 $options = array();
157070 $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
157071 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
157072 $name = __('Text');
157073
157074 $id = false;
157075 foreach ( (array) array_keys($options) as $o ) {
157076 // Old widgets can have null values for some reason
157077 if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
157078 continue;
157079 $id = "text-$o"; // Never never never translate an id
157080 wp_register_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
157081 wp_register_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
157082 }
157083
157084 // If there are none, we register the widget's existance with a generic template
157085 if ( !$id ) {
157086 wp_register_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
157087 wp_register_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
157088 }
157089 }
157090
157091 /**
157092 * Display categories widget.
157093 *
157094 * Allows multiple category widgets.
157095 *
157096 * @since 2.2.0
157097 *
157098 * @param array $args Widget arguments.
157099 * @param int $number Widget number.
157100 */
157101 function wp_widget_categories($args, $widget_args = 1) {
157102 extract($args, EXTR_SKIP);
157103 if ( is_numeric($widget_args) )
157104 $widget_args = array( 'number' => $widget_args );
157105 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
157106 extract($widget_args, EXTR_SKIP);
157107
157108 $options = get_option('widget_categories');
157109 if ( !isset($options[$number]) )
157110 return;
157111
157112 $c = $options[$number]['count'] ? '1' : '0';
157113 $h = $options[$number]['hierarchical'] ? '1' : '0';
157114 $d = $options[$number]['dropdown'] ? '1' : '0';
157115
157116 $title = empty($options[$number]['title']) ? __('Categories') : apply_filters('widget_title', $options[$number]['title']);
157117
157118 echo $before_widget;
157119 echo $before_title . $title . $after_title;
157120
157121 $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
157122
157123 if ( $d ) {
157124 $cat_args['show_option_none'] = __('Select Category');
157125 wp_dropdown_categories($cat_args);
157126 ?>
157127
157128 <script type='text/javascript'>
157129 /* <![CDATA[ */
157130 var dropdown = document.getElementById("cat");
157131 function onCatChange() {
157132 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
157133 location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
157134 }
157135 }
157136 dropdown.onchange = onCatChange;
157137 /* ]]> */
157138 </script>
157139
157140 <?php
157141 } else {
157142 ?>
157143 <ul>
157144 <?php
157145 $cat_args['title_li'] = '';
157146 wp_list_categories($cat_args);
157147 ?>
157148 </ul>
157149 <?php
157150 }
157151
157152 echo $after_widget;
157153 }
157154
157155 /**
157156 * Display and process categories widget options form.
157157 *
157158 * @since 2.2.0
157159 *
157160 * @param int $widget_args Widget number.
157161 */
157162 function wp_widget_categories_control( $widget_args ) {
157163 global $wp_registered_widgets;
157164 static $updated = false;
157165
157166 if ( is_numeric($widget_args) )
157167 $widget_args = array( 'number' => $widget_args );
157168 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
157169 extract($widget_args, EXTR_SKIP);
157170
157171 $options = get_option('widget_categories');
157172
157173 if ( !is_array( $options ) )
157174 $options = array();
157175
157176 if ( !$updated && !empty($_POST['sidebar']) ) {
157177 $sidebar = (string) $_POST['sidebar'];
157178
157179 $sidebars_widgets = wp_get_sidebars_widgets();
157180 if ( isset($sidebars_widgets[$sidebar]) )
157181 $this_sidebar =& $sidebars_widgets[$sidebar];
157182 else
157183 $this_sidebar = array();
157184
157185 foreach ( (array) $this_sidebar as $_widget_id ) {
157186 if ( 'wp_widget_categories' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
157187 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
157188 if ( !in_array( "categories-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
157189 unset($options[$widget_number]);
157190 }
157191 }
157192
157193 foreach ( (array) $_POST['widget-categories'] as $widget_number => $widget_cat ) {
157194 if ( !isset($widget_cat['title']) && isset($options[$widget_number]) ) // user clicked cancel
157195 continue;
157196 $title = trim(strip_tags(stripslashes($widget_cat['title'])));
157197 $count = isset($widget_cat['count']);
157198 $hierarchical = isset($widget_cat['hierarchical']);
157199 $dropdown = isset($widget_cat['dropdown']);
157200 $options[$widget_number] = compact( 'title', 'count', 'hierarchical', 'dropdown' );
157201 }
157202
157203 update_option('widget_categories', $options);
157204 $updated = true;
157205 }
157206
157207 if ( -1 == $number ) {
157208 $title = '';
157209 $count = false;
157210 $hierarchical = false;
157211 $dropdown = false;
157212 $number = '%i%';
157213 } else {
157214 $title = attribute_escape( $options[$number]['title'] );
157215 $count = (bool) $options[$number]['count'];
157216 $hierarchical = (bool) $options[$number]['hierarchical'];
157217 $dropdown = (bool) $options[$number]['dropdown'];
157218 }
157219 ?>
157220 <p>
157221 <label for="categories-title-<?php echo $number; ?>">
157222 <?php _e( 'Title:' ); ?>
157223 <input class="widefat" id="categories-title-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
157224 </label>
157225 </p>
157226
157227 <p>
157228 <label for="categories-dropdown-<?php echo $number; ?>">
157229 <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][dropdown]"<?php checked( $dropdown, true ); ?> />
157230 <?php _e( 'Show as dropdown' ); ?>
157231 </label>
157232 <br />
157233 <label for="categories-count-<?php echo $number; ?>">
157234 <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][count]"<?php checked( $count, true ); ?> />
157235 <?php _e( 'Show post counts' ); ?>
157236 </label>
157237 <br />
157238 <label for="categories-hierarchical-<?php echo $number; ?>">
157239 <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][hierarchical]"<?php checked( $hierarchical, true ); ?> />
157240 <?php _e( 'Show hierarchy' ); ?>
157241 </label>
157242 </p>
157243
157244 <input type="hidden" name="widget-categories[<?php echo $number; ?>][submit]" value="1" />
157245 <?php
157246 }
157247
157248 /**
157249 * Register categories widget on startup.
157250 *
157251 * @since 2.3.0
157252 */
157253 function wp_widget_categories_register() {
157254 if ( !$options = get_option( 'widget_categories' ) )
157255 $options = array();
157256
157257 if ( isset($options['title']) )
157258 $options = wp_widget_categories_upgrade();
157259
157260 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
157261
157262 $name = __( 'Categories' );
157263
157264 $id = false;
157265 foreach ( (array) array_keys($options) as $o ) {
157266 // Old widgets can have null values for some reason
157267 if ( !isset($options[$o]['title']) )
157268 continue;
157269 $id = "categories-$o";
157270 wp_register_sidebar_widget( $id, $name, 'wp_widget_categories', $widget_ops, array( 'number' => $o ) );
157271 wp_register_widget_control( $id, $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => $o ) );
157272 }
157273
157274 // If there are none, we register the widget's existance with a generic template
157275 if ( !$id ) {
157276 wp_register_sidebar_widget( 'categories-1', $name, 'wp_widget_categories', $widget_ops, array( 'number' => -1 ) );
157277 wp_register_widget_control( 'categories-1', $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => -1 ) );
157278 }
157279 }
157280
157281 /**
157282 * Upgrade previous category widget to current version.
157283 *
157284 * @since 2.3.0
157285 *
157286 * @return array
157287 */
157288 function wp_widget_categories_upgrade() {
157289 $options = get_option( 'widget_categories' );
157290
157291 if ( !isset( $options['title'] ) )
157292 return $options;
157293
157294 $newoptions = array( 1 => $options );
157295
157296 update_option( 'widget_categories', $newoptions );
157297
157298 $sidebars_widgets = get_option( 'sidebars_widgets' );
157299 if ( is_array( $sidebars_widgets ) ) {
157300 foreach ( $sidebars_widgets as $sidebar => $widgets ) {
157301 if ( is_array( $widgets ) ) {
157302 foreach ( $widgets as $widget )
157303 $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
157304 } else {
157305 $new_widgets[$sidebar] = $widgets;
157306 }
157307 }
157308 if ( $new_widgets != $sidebars_widgets )
157309 update_option( 'sidebars_widgets', $new_widgets );
157310 }
157311
157312 return $newoptions;
157313 }
157314
157315 /**
157316 * Display recent entries widget.
157317 *
157318 * @since 2.2.0
157319 *
157320 * @param array $args Widget arguments.
157321 * @return int Displayed cache.
157322 */
157323 function wp_widget_recent_entries($args) {
157324 if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
157325 if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
157326 return print($output);
157327 ob_start();
157328 }
157329
157330 extract($args);
157331 $options = get_option('widget_recent_entries');
157332 $title = empty($options['title']) ? __('Recent Posts') : apply_filters('widget_title', $options['title']);
157333 if ( !$number = (int) $options['number'] )
157334 $number = 10;
157335 else if ( $number < 1 )
157336 $number = 1;
157337 else if ( $number > 15 )
157338 $number = 15;
157339
157340 $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
157341 if ($r->have_posts()) :
157342 ?>
157343 <?php echo $before_widget; ?>
157344 <?php echo $before_title . $title . $after_title; ?>
157345 <ul>
157346 <?php while ($r->have_posts()) : $r->the_post(); ?>
157347 <li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
157348 <?php endwhile; ?>
157349 </ul>
157350 <?php echo $after_widget; ?>
157351 <?php
157352 wp_reset_query(); // Restore global post data stomped by the_post().
157353 endif;
157354
157355 if ( '%BEG_OF_TITLE%' != $args['before_title'] )
157356 wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
157357 }
157358
157359 /**
157360 * Remove recent entries widget items cache.
157361 *
157362 * @since 2.2.0
157363 */
157364 function wp_flush_widget_recent_entries() {
157365 wp_cache_delete('widget_recent_entries', 'widget');
157366 }
157367
157368 add_action('save_post', 'wp_flush_widget_recent_entries');
157369 add_action('deleted_post', 'wp_flush_widget_recent_entries');
157370 add_action('switch_theme', 'wp_flush_widget_recent_entries');
157371
157372 /**
157373 * Display and process recent entries widget options form.
157374 *
157375 * @since 2.2.0
157376 */
157377 function wp_widget_recent_entries_control() {
157378 $options = $newoptions = get_option('widget_recent_entries');
157379 if ( isset($_POST["recent-entries-submit"]) ) {
157380 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
157381 $newoptions['number'] = (int) $_POST["recent-entries-number"];
157382 }
157383 if ( $options != $newoptions ) {
157384 $options = $newoptions;
157385 update_option('widget_recent_entries', $options);
157386 wp_flush_widget_recent_entries();
157387 }
157388 $title = attribute_escape($options['title']);
157389 if ( !$number = (int) $options['number'] )
157390 $number = 5;
157391 ?>
157392
157393 <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>
157394 <p>
157395 <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>
157396 <br />
157397 <small><?php _e('(at most 15)'); ?></small>
157398 </p>
157399 <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
157400 <?php
157401 }
157402
157403 /**
157404 * Display recent comments widget.
157405 *
157406 * @since 2.2.0
157407 *
157408 * @param array $args Widget arguments.
157409 */
157410 function wp_widget_recent_comments($args) {
157411 global $wpdb, $comments, $comment;
157412 extract($args, EXTR_SKIP);
157413 $options = get_option('widget_recent_comments');
157414 $title = empty($options['title']) ? __('Recent Comments') : apply_filters('widget_title', $options['title']);
157415 if ( !$number = (int) $options['number'] )
157416 $number = 5;
157417 else if ( $number < 1 )
157418 $number = 1;
157419 else if ( $number > 15 )
157420 $number = 15;
157421
157422 if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
157423 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
157424 wp_cache_add( 'recent_comments', $comments, 'widget' );
157425 }
157426 ?>
157427
157428 <?php echo $before_widget; ?>
157429 <?php echo $before_title . $title . $after_title; ?>
157430 <ul id="recentcomments"><?php
157431 if ( $comments ) : foreach ( (array) $comments as $comment) :
157432 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>';
157433 endforeach; endif;?></ul>
157434 <?php echo $after_widget; ?>
157435 <?php
157436 }
157437
157438 /**
157439 * Remove the cache for recent comments widget.
157440 *
157441 * @since 2.2.0
157442 */
157443 function wp_delete_recent_comments_cache() {
157444 wp_cache_delete( 'recent_comments', 'widget' );
157445 }
157446 add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
157447 add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
157448
157449 /**
157450 * Display and process recent comments widget options form.
157451 *
157452 * @since 2.2.0
157453 */
157454 function wp_widget_recent_comments_control() {
157455 $options = $newoptions = get_option('widget_recent_comments');
157456 if ( isset($_POST["recent-comments-submit"]) ) {
157457 $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
157458 $newoptions['number'] = (int) $_POST["recent-comments-number"];
157459 }
157460 if ( $options != $newoptions ) {
157461 $options = $newoptions;
157462 update_option('widget_recent_comments', $options);
157463 wp_delete_recent_comments_cache();
157464 }
157465 $title = attribute_escape($options['title']);
157466 if ( !$number = (int) $options['number'] )
157467 $number = 5;
157468 ?>
157469 <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>
157470 <p>
157471 <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>
157472 <br />
157473 <small><?php _e('(at most 15)'); ?></small>
157474 </p>
157475 <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
157476 <?php
157477 }
157478
157479 /**
157480 * Display the style for recent comments widget.
157481 *
157482 * @since 2.2.0
157483 */
157484 function wp_widget_recent_comments_style() {
157485 ?>
157486 <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
157487 <?php
157488 }
157489
157490 /**
157491 * Register recent comments with control and hook for 'wp_head' action.
157492 *
157493 * @since 2.2.0
157494 */
157495 function wp_widget_recent_comments_register() {
157496 $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
157497 wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops);
157498 wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control');
157499
157500 if ( is_active_widget('wp_widget_recent_comments') )
157501 add_action('wp_head', 'wp_widget_recent_comments_style');
157502 }
157503
157504 /**
157505 * Display RSS widget.
157506 *
157507 * Allows for multiple widgets to be displayed.
157508 *
157509 * @since 2.2.0
157510 *
157511 * @param array $args Widget arguments.
157512 * @param int $number Widget number.
157513 */
157514 function wp_widget_rss($args, $widget_args = 1) {
157515 extract($args, EXTR_SKIP);
157516 if ( is_numeric($widget_args) )
157517 $widget_args = array( 'number' => $widget_args );
157518 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
157519 extract($widget_args, EXTR_SKIP);
157520
157521 $options = get_option('widget_rss');
157522
157523 if ( !isset($options[$number]) )
157524 return;
157525
157526 if ( isset($options[$number]['error']) && $options[$number]['error'] )
157527 return;
157528
157529 $url = $options[$number]['url'];
157530 while ( strstr($url, 'http') != $url )
157531 $url = substr($url, 1);
157532 if ( empty($url) )
157533 return;
157534
157535 require_once(ABSPATH . WPINC . '/rss.php');
157536
157537 $rss = fetch_rss($url);
157538 $link = clean_url(strip_tags($rss->channel['link']));
157539 while ( strstr($link, 'http') != $link )
157540 $link = substr($link, 1);
157541 $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES)));
157542 $title = $options[$number]['title'];
157543 if ( empty($title) )
157544 $title = htmlentities(strip_tags($rss->channel['title']));
157545 if ( empty($title) )
157546 $title = $desc;
157547 if ( empty($title) )
157548 $title = __('Unknown Feed');
157549 $title = apply_filters('widget_title', $title );
157550 $url = clean_url(strip_tags($url));
157551 if ( file_exists(dirname(__FILE__) . '/rss.png') )
157552 $icon = str_replace(ABSPATH, site_url() . '/', dirname(__FILE__)) . '/rss.png';
157553 else
157554 $icon = includes_url('images/rss.png');
157555 $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>";
157556
157557 echo $before_widget;
157558 echo $before_title . $title . $after_title;
157559
157560 wp_widget_rss_output( $rss, $options[$number] );
157561
157562 echo $after_widget;
157563 }
157564
157565 /**
157566 * Display the RSS entries in a list.
157567 *
157568 * @since 2.5.0
157569 *
157570 * @param string|array|object $rss RSS url.
157571 * @param array $args Widget arguments.
157572 */
157573 function wp_widget_rss_output( $rss, $args = array() ) {
157574 if ( is_string( $rss ) ) {
157575 require_once(ABSPATH . WPINC . '/rss.php');
157576 if ( !$rss = fetch_rss($rss) )
157577 return;
157578 } elseif ( is_array($rss) && isset($rss['url']) ) {
157579 require_once(ABSPATH . WPINC . '/rss.php');
157580 $args = $rss;
157581 if ( !$rss = fetch_rss($rss['url']) )
157582 return;
157583 } elseif ( !is_object($rss) ) {
157584 return;
157585 }
157586
157587 $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
157588 $args = wp_parse_args( $args, $default_args );
157589 extract( $args, EXTR_SKIP );
157590
157591 $items = (int) $items;
157592 if ( $items < 1 || 20 < $items )
157593 $items = 10;
157594 $show_summary = (int) $show_summary;
157595 $show_author = (int) $show_author;
157596 $show_date = (int) $show_date;
157597
157598 if ( is_array( $rss->items ) && !empty( $rss->items ) ) {
157599 $rss->items = array_slice($rss->items, 0, $items);
157600 echo '<ul>';
157601 foreach ( (array) $rss->items as $item ) {
157602 while ( strstr($item['link'], 'http') != $item['link'] )
157603 $item['link'] = substr($item['link'], 1);
157604 $link = clean_url(strip_tags($item['link']));
157605 $title = attribute_escape(strip_tags($item['title']));
157606 if ( empty($title) )
157607 $title = __('Untitled');
157608 $desc = '';
157609 if ( isset( $item['description'] ) && is_string( $item['description'] ) )
157610 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['description'], ENT_QUOTES))));
157611 elseif ( isset( $item['summary'] ) && is_string( $item['summary'] ) )
157612 $desc = str_replace(array("\n", "\r"), ' ', attribute_escape(strip_tags(html_entity_decode($item['summary'], ENT_QUOTES))));
157613 if ( 360 < strlen( $desc ) )
157614 $desc = wp_html_excerpt( $desc, 360 ) . ' […]';
157615 $summary = $desc;
157616
157617 if ( $show_summary ) {
157618 $desc = '';
157619 $summary = wp_specialchars( $summary );
157620 $summary = "<div class='rssSummary'>$summary</div>";
157621 } else {
157622 $summary = '';
157623 }
157624
157625 $date = '';
157626 if ( $show_date ) {
157627 if ( isset($item['pubdate']) )
157628 $date = $item['pubdate'];
157629 elseif ( isset($item['published']) )
157630 $date = $item['published'];
157631
157632 if ( $date ) {
157633 if ( $date_stamp = strtotime( $date ) )
157634 $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date_stamp ) . '</span>';
157635 else
157636 $date = '';
157637 }
157638 }
157639
157640 $author = '';
157641 if ( $show_author ) {
157642 if ( isset($item['dc']['creator']) )
157643 $author = ' <cite>' . wp_specialchars( strip_tags( $item['dc']['creator'] ) ) . '</cite>';
157644 elseif ( isset($item['author_name']) )
157645 $author = ' <cite>' . wp_specialchars( strip_tags( $item['author_name'] ) ) . '</cite>';
157646 }
157647
157648 if ( $link == '' ) {
157649 echo "<li>$title{$date}{$summary}{$author}</li>";
157650 } else {
157651 echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
157652 }
157653 }
157654 echo '</ul>';
157655 } else {
157656 echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
157657 }
157658 }
157659
157660 /**
157661 * Display and process RSS widget control form.
157662 *
157663 * @since 2.2.0
157664 *
157665 * @param int $widget_args Widget number.
157666 */
157667 function wp_widget_rss_control($widget_args) {
157668 global $wp_registered_widgets;
157669 static $updated = false;
157670
157671 if ( is_numeric($widget_args) )
157672 $widget_args = array( 'number' => $widget_args );
157673 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
157674 extract($widget_args, EXTR_SKIP);
157675
157676 $options = get_option('widget_rss');
157677 if ( !is_array($options) )
157678 $options = array();
157679
157680 $urls = array();
157681 foreach ( (array) $options as $option )
157682 if ( isset($option['url']) )
157683 $urls[$option['url']] = true;
157684
157685 if ( !$updated && 'POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['sidebar']) ) {
157686 $sidebar = (string) $_POST['sidebar'];
157687
157688 $sidebars_widgets = wp_get_sidebars_widgets();
157689 if ( isset($sidebars_widgets[$sidebar]) )
157690 $this_sidebar =& $sidebars_widgets[$sidebar];
157691 else
157692 $this_sidebar = array();
157693
157694 foreach ( (array) $this_sidebar as $_widget_id ) {
157695 if ( 'wp_widget_rss' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
157696 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
157697 if ( !in_array( "rss-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
157698 unset($options[$widget_number]);
157699 }
157700 }
157701
157702 foreach( (array) $_POST['widget-rss'] as $widget_number => $widget_rss ) {
157703 if ( !isset($widget_rss['url']) && isset($options[$widget_number]) ) // user clicked cancel
157704 continue;
157705 $widget_rss = stripslashes_deep( $widget_rss );
157706 $url = sanitize_url(strip_tags($widget_rss['url']));
157707 $options[$widget_number] = wp_widget_rss_process( $widget_rss, !isset($urls[$url]) );
157708 }
157709
157710 update_option('widget_rss', $options);
157711 $updated = true;
157712 }
157713
157714 if ( -1 == $number ) {
157715 $title = '';
157716 $url = '';
157717 $items = 10;
157718 $error = false;
157719 $number = '%i%';
157720 $show_summary = 0;
157721 $show_author = 0;
157722 $show_date = 0;
157723 } else {
157724 extract( (array) $options[$number] );
157725 }
157726
157727 wp_widget_rss_form( compact( 'number', 'title', 'url', 'items', 'error', 'show_summary', 'show_author', 'show_date' ) );
157728 }
157729
157730 /**
157731 * Display RSS widget options form.
157732 *
157733 * The options for what fields are displayed for the RSS form are all booleans
157734 * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
157735 * 'show_date'.
157736 *
157737 * @since 2.5.0
157738 *
157739 * @param array|string $args Values for input fields.
157740 * @param array $inputs Override default display options.
157741 */
157742 function wp_widget_rss_form( $args, $inputs = null ) {
157743
157744 $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
157745 $inputs = wp_parse_args( $inputs, $default_inputs );
157746 extract( $args );
157747 extract( $inputs, EXTR_SKIP);
157748
157749 $number = attribute_escape( $number );
157750 $title = attribute_escape( $title );
157751 $url = attribute_escape( $url );
157752 $items = (int) $items;
157753 if ( $items < 1 || 20 < $items )
157754 $items = 10;
157755 $show_summary = (int) $show_summary;
157756 $show_author = (int) $show_author;
157757 $show_date = (int) $show_date;
157758
157759 if ( $inputs['url'] ) :
157760 ?>
157761 <p>
157762 <label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?>
157763 <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" />
157764 </label>
157765 </p>
157766 <?php endif; if ( $inputs['title'] ) : ?>
157767 <p>
157768 <label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?>
157769 <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
157770 </label>
157771 </p>
157772 <?php endif; if ( $inputs['items'] ) : ?>
157773 <p>
157774 <label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?>
157775 <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
157776 <?php
157777 for ( $i = 1; $i <= 20; ++$i )
157778 echo "<option value='$i' " . ( $items == $i ? "selected='selected'" : '' ) . ">$i</option>";
157779 ?>
157780 </select>
157781 </label>
157782 </p>
157783 <?php endif; if ( $inputs['show_summary'] ) : ?>
157784 <p>
157785 <label for="rss-show-summary-<?php echo $number; ?>">
157786 <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"'; ?>/>
157787 <?php _e('Display item content?'); ?>
157788 </label>
157789 </p>
157790 <?php endif; if ( $inputs['show_author'] ) : ?>
157791 <p>
157792 <label for="rss-show-author-<?php echo $number; ?>">
157793 <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"'; ?>/>
157794 <?php _e('Display item author if available?'); ?>
157795 </label>
157796 </p>
157797 <?php endif; if ( $inputs['show_date'] ) : ?>
157798 <p>
157799 <label for="rss-show-date-<?php echo $number; ?>">
157800 <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"'; ?>/>
157801 <?php _e('Display item date?'); ?>
157802 </label>
157803 </p>
157804 <input type="hidden" name="widget-rss[<?php echo $number; ?>][submit]" value="1" />
157805 <?php
157806 endif;
157807 foreach ( array_keys($default_inputs) as $input ) :
157808 if ( 'hidden' === $inputs[$input] ) :
157809 $id = str_replace( '_', '-', $input );
157810 ?>
157811 <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
157812 <?php
157813 endif;
157814 endforeach;
157815 }
157816
157817 /**
157818 * Process RSS feed widget data and optionally retrieve feed items.
157819 *
157820 * The feed widget can not have more than 20 items or it will reset back to the
157821 * default, which is 10.
157822 *
157823 * The resulting array has the feed title, feed url, feed link (from channel),
157824 * feed items, error (if any), and whether to show summary, author, and date.
157825 * All respectively in the order of the array elements.
157826 *
157827 * @since 2.5.0
157828 *
157829 * @param array $widget_rss RSS widget feed data. Expects unescaped data.
157830 * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
157831 * @return array
157832 */
157833 function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
157834 $items = (int) $widget_rss['items'];
157835 if ( $items < 1 || 20 < $items )
157836 $items = 10;
157837 $url = sanitize_url(strip_tags( $widget_rss['url'] ));
157838 $title = trim(strip_tags( $widget_rss['title'] ));
157839 $show_summary = (int) $widget_rss['show_summary'];
157840 $show_author = (int) $widget_rss['show_author'];
157841 $show_date = (int) $widget_rss['show_date'];
157842
157843 if ( $check_feed ) {
157844 require_once(ABSPATH . WPINC . '/rss.php');
157845 $rss = fetch_rss($url);
157846 $error = false;
157847 $link = '';
157848 if ( !is_object($rss) ) {
157849 $url = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1);
157850 $error = sprintf(__('Error in RSS %1$d'), $widget_number );
157851 } else {
157852 $link = clean_url(strip_tags($rss->channel['link']));
157853 while ( strstr($link, 'http') != $link )
157854 $link = substr($link, 1);
157855 }
157856 }
157857
157858 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
157859 }
157860
157861 /**
157862 * Register RSS widget to allow multiple RSS widgets on startup.
157863 *
157864 * @since 2.2.0
157865 */
157866 function wp_widget_rss_register() {
157867 if ( !$options = get_option('widget_rss') )
157868 $options = array();
157869 $widget_ops = array('classname' => 'widget_rss', 'description' => __( 'Entries from any RSS or Atom feed' ));
157870 $control_ops = array('width' => 400, 'height' => 200, 'id_base' => 'rss');
157871 $name = __('RSS');
157872
157873 $id = false;
157874 foreach ( (array) array_keys($options) as $o ) {
157875 // Old widgets can have null values for some reason
157876 if ( !isset($options[$o]['url']) || !isset($options[$o]['title']) || !isset($options[$o]['items']) )
157877 continue;
157878 $id = "rss-$o"; // Never never never translate an id
157879 wp_register_sidebar_widget($id, $name, 'wp_widget_rss', $widget_ops, array( 'number' => $o ));
157880 wp_register_widget_control($id, $name, 'wp_widget_rss_control', $control_ops, array( 'number' => $o ));
157881 }
157882
157883 // If there are none, we register the widget's existance with a generic template
157884 if ( !$id ) {
157885 wp_register_sidebar_widget( 'rss-1', $name, 'wp_widget_rss', $widget_ops, array( 'number' => -1 ) );
157886 wp_register_widget_control( 'rss-1', $name, 'wp_widget_rss_control', $control_ops, array( 'number' => -1 ) );
157887 }
157888 }
157889
157890 /**
157891 * Display tag cloud widget.
157892 *
157893 * @since 2.3.0
157894 *
157895 * @param array $args Widget arguments.
157896 */
157897 function wp_widget_tag_cloud($args) {
157898 extract($args);
157899 $options = get_option('widget_tag_cloud');
157900 $title = empty($options['title']) ? __('Tags') : apply_filters('widget_title', $options['title']);
157901
157902 echo $before_widget;
157903 echo $before_title . $title . $after_title;
157904 wp_tag_cloud();
157905 echo $after_widget;
157906 }
157907
157908 /**
157909 * Manage WordPress Tag Cloud widget options.
157910 *
157911 * Displays management form for changing the tag cloud widget title.
157912 *
157913 * @since 2.3.0
157914 */
157915 function wp_widget_tag_cloud_control() {
157916 $options = $newoptions = get_option('widget_tag_cloud');
157917
157918 if ( isset($_POST['tag-cloud-submit']) ) {
157919 $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
157920 }
157921
157922 if ( $options != $newoptions ) {
157923 $options = $newoptions;
157924 update_option('widget_tag_cloud', $options);
157925 }
157926
157927 $title = attribute_escape( $options['title'] );
157928 ?>
157929 <p><label for="tag-cloud-title">
157930 <?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
157931 </p>
157932 <input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
157933 <?php
157934 }
157935
157936 /**
157937 * Register all of the default WordPress widgets on startup.
157938 *
157939 * Calls 'widgets_init' action after all of the WordPress widgets have been
157940 * registered.
157941 *
157942 * @since 2.2.0
157943 */
157944 function wp_widgets_init() {
157945 if ( !is_blog_installed() )
157946 return;
157947
157948 $widget_ops = array('classname' => 'widget_pages', 'description' => __( "Your blog's WordPress Pages") );
157949 wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $widget_ops);
157950 wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control' );
157951
157952 $widget_ops = array('classname' => 'widget_calendar', 'description' => __( "A calendar of your blog's posts") );
157953 wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $widget_ops);
157954 wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control' );
157955
157956 $widget_ops = array('classname' => 'widget_archive', 'description' => __( "A monthly archive of your blog's posts") );
157957 wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $widget_ops);
157958 wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
157959
157960 $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
157961 wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
157962
157963 $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
157964 wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $widget_ops);
157965 wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control' );
157966
157967 $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your blog") );
157968 wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $widget_ops);
157969
157970 $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
157971 wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
157972 wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
157973
157974 $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
157975 wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops);
157976 wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
157977
157978 wp_widget_categories_register();
157979 wp_widget_text_register();
157980 wp_widget_rss_register();
157981 wp_widget_recent_comments_register();
157982
157983 do_action('widgets_init');
157984 }
157985
157986 add_action('init', 'wp_widgets_init', 1);
157987
157988 /*
157989 * Pattern for multi-widget (allows multiple instances such as the text widget).
157990 *
157991 * Make sure to close the comments after copying.
157992
157993 /**
157994 * Displays widget.
157995 *
157996 * Supports multiple widgets.
157997 *
157998 * @param array $args Widget arguments.
157999 * @param array|int $widget_args Widget number. Which of the several widgets of this type do we mean.
158000 * /
158001 function widget_many( $args, $widget_args = 1 ) {
158002 extract( $args, EXTR_SKIP );
158003 if ( is_numeric($widget_args) )
158004 $widget_args = array( 'number' => $widget_args );
158005 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
158006 extract( $widget_args, EXTR_SKIP );
158007
158008 // Data should be stored as array: array( number => data for that instance of the widget, ... )
158009 $options = get_option('widget_many');
158010 if ( !isset($options[$number]) )
158011 return;
158012
158013 echo $before_widget;
158014
158015 // Do stuff for this widget, drawing data from $options[$number]
158016
158017 echo $after_widget;
158018 }
158019
158020 /**
158021 * Displays form for a particular instance of the widget.
158022 *
158023 * Also updates the data after a POST submit.
158024 *
158025 * @param array|int $widget_args Widget number. Which of the several widgets of this type do we mean.
158026 * /
158027 function widget_many_control( $widget_args = 1 ) {
158028 global $wp_registered_widgets;
158029 static $updated = false; // Whether or not we have already updated the data after a POST submit
158030
158031 if ( is_numeric($widget_args) )
158032 $widget_args = array( 'number' => $widget_args );
158033 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
158034 extract( $widget_args, EXTR_SKIP );
158035
158036 // Data should be stored as array: array( number => data for that instance of the widget, ... )
158037 $options = get_option('widget_many');
158038 if ( !is_array($options) )
158039 $options = array();
158040
158041 // We need to update the data
158042 if ( !$updated && !empty($_POST['sidebar']) ) {
158043 // Tells us what sidebar to put the data in
158044 $sidebar = (string) $_POST['sidebar'];
158045
158046 $sidebars_widgets = wp_get_sidebars_widgets();
158047 if ( isset($sidebars_widgets[$sidebar]) )
158048 $this_sidebar =& $sidebars_widgets[$sidebar];
158049 else
158050 $this_sidebar = array();
158051
158052 foreach ( $this_sidebar as $_widget_id ) {
158053 // 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
158054 // since widget ids aren't necessarily persistent across multiple updates
158055 if ( 'widget_many' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
158056 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
158057 if ( !in_array( "many-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed. "many-$widget_number" is "{id_base}-{widget_number}
158058 unset($options[$widget_number]);
158059 }
158060 }
158061
158062 foreach ( (array) $_POST['widget-many'] as $widget_number => $widget_many_instance ) {
158063 // compile data from $widget_many_instance
158064 if ( !isset($widget_many_instance['something']) && isset($options[$widget_number]) ) // user clicked cancel
158065 continue;
158066 $something = wp_specialchars( $widget_many_instance['something'] );
158067 $options[$widget_number] = array( 'something' => $something ); // Even simple widgets should store stuff in array, rather than in scalar
158068 }
158069
158070 update_option('widget_many', $options);
158071
158072 $updated = true; // So that we don't go through this more than once
158073 }
158074
158075
158076 // Here we echo out the form
158077 if ( -1 == $number ) { // We echo out a template for a form which can be converted to a specific form later via JS
158078 $something = '';
158079 $number = '%i%';
158080 } else {
158081 $something = attribute_escape($options[$number]['something']);
158082 }
158083
158084 // The form has inputs with names like widget-many[$number][something] so that all data for that instance of
158085 // the widget are stored in one $_POST variable: $_POST['widget-many'][$number]
158086 ?>
158087 <p>
158088 <input class="widefat" id="widget-many-something-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][something]" type="text" value="<?php echo $data; ?>" />
158089 <input type="hidden" id="widget-many-submit-<?php echo $number; ?>" name="widget-many[<?php echo $number; ?>][submit]" value="1" />
158090 </p>
158091 <?php
158092 }
158093
158094 /**
158095 * Registers each instance of our widget on startup.
158096 * /
158097 function widget_many_register() {
158098 if ( !$options = get_option('widget_many') )
158099 $options = array();
158100
158101 $widget_ops = array('classname' => 'widget_many', 'description' => __('Widget which allows multiple instances'));
158102 $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'many');
158103 $name = __('Many');
158104
158105 $registered = false;
158106 foreach ( array_keys($options) as $o ) {
158107 // Old widgets can have null values for some reason
158108 if ( !isset($options[$o]['something']) ) // we used 'something' above in our exampple. Replace with with whatever your real data are.
158109 continue;
158110
158111 // $id should look like {$id_base}-{$o}
158112 $id = "many-$o"; // Never never never translate an id
158113 $registered = true;
158114 wp_register_sidebar_widget( $id, $name, 'widget_many', $widget_ops, array( 'number' => $o ) );
158115 wp_register_widget_control( $id, $name, 'widget_many_control', $control_ops, array( 'number' => $o ) );
158116 }
158117
158118 // If there are none, we register the widget's existance with a generic template
158119 if ( !$registered ) {
158120 wp_register_sidebar_widget( 'many-1', $name, 'widget_many', $widget_ops, array( 'number' => -1 ) );
158121 wp_register_widget_control( 'many-1', $name, 'widget_many_control', $control_ops, array( 'number' => -1 ) );
158122 }
158123 }
158124
158125 // This is important
158126 add_action( 'widgets_init', 'widget_many_register' )
158127
158128 */
158129
158130 ?>