165 */ |
165 */ |
166 function register_sidebars( $number = 1, $args = array() ) { |
166 function register_sidebars( $number = 1, $args = array() ) { |
167 global $wp_registered_sidebars; |
167 global $wp_registered_sidebars; |
168 $number = (int) $number; |
168 $number = (int) $number; |
169 |
169 |
170 if ( is_string($args) ) |
170 if ( is_string( $args ) ) { |
171 parse_str($args, $args); |
171 parse_str( $args, $args ); |
|
172 } |
172 |
173 |
173 for ( $i = 1; $i <= $number; $i++ ) { |
174 for ( $i = 1; $i <= $number; $i++ ) { |
174 $_args = $args; |
175 $_args = $args; |
175 |
176 |
176 if ( $number > 1 ) |
177 if ( $number > 1 ) { |
177 $_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : sprintf(__('Sidebar %d'), $i); |
178 $_args['name'] = isset( $args['name'] ) ? sprintf( $args['name'], $i ) : sprintf( __( 'Sidebar %d' ), $i ); |
178 else |
179 } else { |
179 $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar'); |
180 $_args['name'] = isset( $args['name'] ) ? $args['name'] : __( 'Sidebar' ); |
|
181 } |
180 |
182 |
181 // Custom specified ID's are suffixed if they exist already. |
183 // Custom specified ID's are suffixed if they exist already. |
182 // Automatically generated sidebar names need to be suffixed regardless starting at -0 |
184 // Automatically generated sidebar names need to be suffixed regardless starting at -0 |
183 if ( isset($args['id']) ) { |
185 if ( isset( $args['id'] ) ) { |
184 $_args['id'] = $args['id']; |
186 $_args['id'] = $args['id']; |
185 $n = 2; // Start at -2 for conflicting custom ID's |
187 $n = 2; // Start at -2 for conflicting custom ID's |
186 while ( is_registered_sidebar( $_args['id'] ) ) { |
188 while ( is_registered_sidebar( $_args['id'] ) ) { |
187 $_args['id'] = $args['id'] . '-' . $n++; |
189 $_args['id'] = $args['id'] . '-' . $n++; |
188 } |
190 } |
189 } else { |
191 } else { |
190 $n = count( $wp_registered_sidebars ); |
192 $n = count( $wp_registered_sidebars ); |
191 do { |
193 do { |
192 $_args['id'] = 'sidebar-' . ++$n; |
194 $_args['id'] = 'sidebar-' . ++$n; |
193 } while ( is_registered_sidebar( $_args['id'] ) ); |
195 } while ( is_registered_sidebar( $_args['id'] ) ); |
194 } |
196 } |
195 register_sidebar($_args); |
197 register_sidebar( $_args ); |
196 } |
198 } |
197 } |
199 } |
198 |
200 |
199 /** |
201 /** |
200 * Builds the definition for a single sidebar and returns the ID. |
202 * Builds the definition for a single sidebar and returns the ID. |
235 * @type string $after_title HTML content to append to the sidebar title when displayed. |
237 * @type string $after_title HTML content to append to the sidebar title when displayed. |
236 * Default is a closing h2 element. |
238 * Default is a closing h2 element. |
237 * } |
239 * } |
238 * @return string Sidebar ID added to $wp_registered_sidebars global. |
240 * @return string Sidebar ID added to $wp_registered_sidebars global. |
239 */ |
241 */ |
240 function register_sidebar($args = array()) { |
242 function register_sidebar( $args = array() ) { |
241 global $wp_registered_sidebars; |
243 global $wp_registered_sidebars; |
242 |
244 |
243 $i = count($wp_registered_sidebars) + 1; |
245 $i = count( $wp_registered_sidebars ) + 1; |
244 |
246 |
245 $id_is_empty = empty( $args['id'] ); |
247 $id_is_empty = empty( $args['id'] ); |
246 |
248 |
247 $defaults = array( |
249 $defaults = array( |
248 'name' => sprintf(__('Sidebar %d'), $i ), |
250 'name' => sprintf( __( 'Sidebar %d' ), $i ), |
249 'id' => "sidebar-$i", |
251 'id' => "sidebar-$i", |
250 'description' => '', |
252 'description' => '', |
251 'class' => '', |
253 'class' => '', |
252 'before_widget' => '<li id="%1$s" class="widget %2$s">', |
254 'before_widget' => '<li id="%1$s" class="widget %2$s">', |
253 'after_widget' => "</li>\n", |
255 'after_widget' => "</li>\n", |
254 'before_title' => '<h2 class="widgettitle">', |
256 'before_title' => '<h2 class="widgettitle">', |
255 'after_title' => "</h2>\n", |
257 'after_title' => "</h2>\n", |
256 ); |
258 ); |
257 |
259 |
258 $sidebar = wp_parse_args( $args, $defaults ); |
260 $sidebar = wp_parse_args( $args, $defaults ); |
259 |
261 |
260 if ( $id_is_empty ) { |
262 if ( $id_is_empty ) { |
261 /* translators: 1: the id argument, 2: sidebar name, 3: recommended id value */ |
263 /* translators: 1: the id argument, 2: sidebar name, 3: recommended id value */ |
262 _doing_it_wrong( __FUNCTION__, sprintf( __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), '<code>id</code>', $sidebar['name'], $sidebar['id'] ), '4.2.0' ); |
264 _doing_it_wrong( __FUNCTION__, sprintf( __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), '<code>id</code>', $sidebar['name'], $sidebar['id'] ), '4.2.0' ); |
263 } |
265 } |
264 |
266 |
265 $wp_registered_sidebars[$sidebar['id']] = $sidebar; |
267 $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar; |
266 |
268 |
267 add_theme_support('widgets'); |
269 add_theme_support( 'widgets' ); |
268 |
270 |
269 /** |
271 /** |
270 * Fires once a sidebar has been registered. |
272 * Fires once a sidebar has been registered. |
271 * |
273 * |
272 * @since 3.0.0 |
274 * @since 3.0.0 |
337 * } |
339 * } |
338 */ |
340 */ |
339 function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) { |
341 function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) { |
340 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; |
342 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; |
341 |
343 |
342 $id = strtolower($id); |
344 $id = strtolower( $id ); |
343 |
345 |
344 if ( empty($output_callback) ) { |
346 if ( empty( $output_callback ) ) { |
345 unset($wp_registered_widgets[$id]); |
347 unset( $wp_registered_widgets[ $id ] ); |
346 return; |
348 return; |
347 } |
349 } |
348 |
350 |
349 $id_base = _get_widget_id_base($id); |
351 $id_base = _get_widget_id_base( $id ); |
350 if ( in_array($output_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($output_callback) ) { |
352 if ( in_array( $output_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $output_callback ) ) { |
351 unset( $wp_registered_widget_controls[ $id ] ); |
353 unset( $wp_registered_widget_controls[ $id ] ); |
352 unset( $wp_registered_widget_updates[ $id_base ] ); |
354 unset( $wp_registered_widget_updates[ $id_base ] ); |
353 return; |
355 return; |
354 } |
356 } |
355 |
357 |
356 $defaults = array('classname' => $output_callback); |
358 $defaults = array( 'classname' => $output_callback ); |
357 $options = wp_parse_args($options, $defaults); |
359 $options = wp_parse_args( $options, $defaults ); |
358 $widget = array( |
360 $widget = array( |
359 'name' => $name, |
361 'name' => $name, |
360 'id' => $id, |
362 'id' => $id, |
361 'callback' => $output_callback, |
363 'callback' => $output_callback, |
362 'params' => array_slice(func_get_args(), 4) |
364 'params' => array_slice( func_get_args(), 4 ), |
363 ); |
365 ); |
364 $widget = array_merge($widget, $options); |
366 $widget = array_merge( $widget, $options ); |
365 |
367 |
366 if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || did_action( 'widgets_init' ) ) ) { |
368 if ( is_callable( $output_callback ) && ( ! isset( $wp_registered_widgets[ $id ] ) || did_action( 'widgets_init' ) ) ) { |
367 |
369 |
368 /** |
370 /** |
369 * Fires once for each registered widget. |
371 * Fires once for each registered widget. |
370 * |
372 * |
371 * @since 3.0.0 |
373 * @since 3.0.0 |
372 * |
374 * |
373 * @param array $widget An array of default widget arguments. |
375 * @param array $widget An array of default widget arguments. |
374 */ |
376 */ |
375 do_action( 'wp_register_sidebar_widget', $widget ); |
377 do_action( 'wp_register_sidebar_widget', $widget ); |
376 $wp_registered_widgets[$id] = $widget; |
378 $wp_registered_widgets[ $id ] = $widget; |
377 } |
379 } |
378 } |
380 } |
379 |
381 |
380 /** |
382 /** |
381 * Retrieve description for widget. |
383 * Retrieve description for widget. |
390 * |
392 * |
391 * @param int|string $id Widget ID. |
393 * @param int|string $id Widget ID. |
392 * @return string|void Widget description, if available. |
394 * @return string|void Widget description, if available. |
393 */ |
395 */ |
394 function wp_widget_description( $id ) { |
396 function wp_widget_description( $id ) { |
395 if ( !is_scalar($id) ) |
397 if ( ! is_scalar( $id ) ) { |
396 return; |
398 return; |
|
399 } |
397 |
400 |
398 global $wp_registered_widgets; |
401 global $wp_registered_widgets; |
399 |
402 |
400 if ( isset($wp_registered_widgets[$id]['description']) ) |
403 if ( isset( $wp_registered_widgets[ $id ]['description'] ) ) { |
401 return esc_html( $wp_registered_widgets[$id]['description'] ); |
404 return esc_html( $wp_registered_widgets[ $id ]['description'] ); |
|
405 } |
402 } |
406 } |
403 |
407 |
404 /** |
408 /** |
405 * Retrieve description for a sidebar. |
409 * Retrieve description for a sidebar. |
406 * |
410 * |
407 * When registering sidebars a 'description' parameter can be included that |
411 * When registering sidebars a 'description' parameter can be included that |
408 * describes the sidebar for display on the widget administration panel. |
412 * describes the sidebar for display on the widget administration panel. |
409 * |
413 * |
410 * @since 2.9.0 |
414 * @since 2.9.0 |
411 * |
415 * |
412 * @global array $wp_registered_sidebars |
416 * @global array $wp_registered_sidebars Registered sidebars. |
413 * |
417 * |
414 * @param string $id sidebar ID. |
418 * @param string $id sidebar ID. |
415 * @return string|void Sidebar description, if available. |
419 * @return string|void Sidebar description, if available. |
416 */ |
420 */ |
417 function wp_sidebar_description( $id ) { |
421 function wp_sidebar_description( $id ) { |
418 if ( !is_scalar($id) ) |
422 if ( ! is_scalar( $id ) ) { |
419 return; |
423 return; |
|
424 } |
420 |
425 |
421 global $wp_registered_sidebars; |
426 global $wp_registered_sidebars; |
422 |
427 |
423 if ( isset( $wp_registered_sidebars[ $id ]['description'] ) ) { |
428 if ( isset( $wp_registered_sidebars[ $id ]['description'] ) ) { |
424 return wp_kses( $wp_registered_sidebars[ $id ]['description'], 'sidebar_description' ); |
429 return wp_kses( $wp_registered_sidebars[ $id ]['description'], 'sidebar_description' ); |
430 * |
435 * |
431 * @since 2.2.0 |
436 * @since 2.2.0 |
432 * |
437 * |
433 * @param int|string $id Widget ID. |
438 * @param int|string $id Widget ID. |
434 */ |
439 */ |
435 function wp_unregister_sidebar_widget($id) { |
440 function wp_unregister_sidebar_widget( $id ) { |
436 |
441 |
437 /** |
442 /** |
438 * Fires just before a widget is removed from a sidebar. |
443 * Fires just before a widget is removed from a sidebar. |
439 * |
444 * |
440 * @since 3.0.0 |
445 * @since 3.0.0 |
441 * |
446 * |
442 * @param int $id The widget ID. |
447 * @param int $id The widget ID. |
443 */ |
448 */ |
444 do_action( 'wp_unregister_sidebar_widget', $id ); |
449 do_action( 'wp_unregister_sidebar_widget', $id ); |
445 |
450 |
446 wp_register_sidebar_widget($id, '', ''); |
451 wp_register_sidebar_widget( $id, '', '' ); |
447 wp_unregister_widget_control($id); |
452 wp_unregister_widget_control( $id ); |
448 } |
453 } |
449 |
454 |
450 /** |
455 /** |
451 * Registers widget control callback for customizing options. |
456 * Registers widget control callback for customizing options. |
452 * |
457 * |
473 * } |
478 * } |
474 */ |
479 */ |
475 function wp_register_widget_control( $id, $name, $control_callback, $options = array() ) { |
480 function wp_register_widget_control( $id, $name, $control_callback, $options = array() ) { |
476 global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks; |
481 global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks; |
477 |
482 |
478 $id = strtolower($id); |
483 $id = strtolower( $id ); |
479 $id_base = _get_widget_id_base($id); |
484 $id_base = _get_widget_id_base( $id ); |
480 |
485 |
481 if ( empty($control_callback) ) { |
486 if ( empty( $control_callback ) ) { |
482 unset($wp_registered_widget_controls[$id]); |
487 unset( $wp_registered_widget_controls[ $id ] ); |
483 unset($wp_registered_widget_updates[$id_base]); |
488 unset( $wp_registered_widget_updates[ $id_base ] ); |
484 return; |
489 return; |
485 } |
490 } |
486 |
491 |
487 if ( in_array($control_callback, $_wp_deprecated_widgets_callbacks, true) && !is_callable($control_callback) ) { |
492 if ( in_array( $control_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $control_callback ) ) { |
488 unset( $wp_registered_widgets[ $id ] ); |
493 unset( $wp_registered_widgets[ $id ] ); |
489 return; |
494 return; |
490 } |
495 } |
491 |
496 |
492 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) ) |
497 if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) { |
493 return; |
498 return; |
494 |
499 } |
495 $defaults = array('width' => 250, 'height' => 200 ); // height is never used |
500 |
496 $options = wp_parse_args($options, $defaults); |
501 $defaults = array( |
497 $options['width'] = (int) $options['width']; |
502 'width' => 250, |
|
503 'height' => 200, |
|
504 ); // height is never used |
|
505 $options = wp_parse_args( $options, $defaults ); |
|
506 $options['width'] = (int) $options['width']; |
498 $options['height'] = (int) $options['height']; |
507 $options['height'] = (int) $options['height']; |
499 |
508 |
500 $widget = array( |
509 $widget = array( |
501 'name' => $name, |
510 'name' => $name, |
502 'id' => $id, |
511 'id' => $id, |
503 'callback' => $control_callback, |
512 'callback' => $control_callback, |
504 'params' => array_slice(func_get_args(), 4) |
513 'params' => array_slice( func_get_args(), 4 ), |
505 ); |
514 ); |
506 $widget = array_merge($widget, $options); |
515 $widget = array_merge( $widget, $options ); |
507 |
516 |
508 $wp_registered_widget_controls[$id] = $widget; |
517 $wp_registered_widget_controls[ $id ] = $widget; |
509 |
518 |
510 if ( isset($wp_registered_widget_updates[$id_base]) ) |
519 if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) { |
511 return; |
520 return; |
512 |
521 } |
513 if ( isset($widget['params'][0]['number']) ) |
522 |
|
523 if ( isset( $widget['params'][0]['number'] ) ) { |
514 $widget['params'][0]['number'] = -1; |
524 $widget['params'][0]['number'] = -1; |
515 |
525 } |
516 unset($widget['width'], $widget['height'], $widget['name'], $widget['id']); |
526 |
517 $wp_registered_widget_updates[$id_base] = $widget; |
527 unset( $widget['width'], $widget['height'], $widget['name'], $widget['id'] ); |
|
528 $wp_registered_widget_updates[ $id_base ] = $widget; |
518 } |
529 } |
519 |
530 |
520 /** |
531 /** |
521 * Registers the update callback for a widget. |
532 * Registers the update callback for a widget. |
522 * |
533 * |
530 * Default empty array. |
541 * Default empty array. |
531 */ |
542 */ |
532 function _register_widget_update_callback( $id_base, $update_callback, $options = array() ) { |
543 function _register_widget_update_callback( $id_base, $update_callback, $options = array() ) { |
533 global $wp_registered_widget_updates; |
544 global $wp_registered_widget_updates; |
534 |
545 |
535 if ( isset($wp_registered_widget_updates[$id_base]) ) { |
546 if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) { |
536 if ( empty($update_callback) ) |
547 if ( empty( $update_callback ) ) { |
537 unset($wp_registered_widget_updates[$id_base]); |
548 unset( $wp_registered_widget_updates[ $id_base ] ); |
|
549 } |
538 return; |
550 return; |
539 } |
551 } |
540 |
552 |
541 $widget = array( |
553 $widget = array( |
542 'callback' => $update_callback, |
554 'callback' => $update_callback, |
543 'params' => array_slice(func_get_args(), 3) |
555 'params' => array_slice( func_get_args(), 3 ), |
544 ); |
556 ); |
545 |
557 |
546 $widget = array_merge($widget, $options); |
558 $widget = array_merge( $widget, $options ); |
547 $wp_registered_widget_updates[$id_base] = $widget; |
559 $wp_registered_widget_updates[ $id_base ] = $widget; |
548 } |
560 } |
549 |
561 |
550 /** |
562 /** |
551 * Registers the form callback for a widget. |
563 * Registers the form callback for a widget. |
552 * |
564 * |
558 * @param string $name Name attribute for the widget. |
570 * @param string $name Name attribute for the widget. |
559 * @param callable $form_callback Form callback. |
571 * @param callable $form_callback Form callback. |
560 * @param array $options Optional. Widget control options. See wp_register_widget_control(). |
572 * @param array $options Optional. Widget control options. See wp_register_widget_control(). |
561 * Default empty array. |
573 * Default empty array. |
562 */ |
574 */ |
563 function _register_widget_form_callback($id, $name, $form_callback, $options = array()) { |
575 function _register_widget_form_callback( $id, $name, $form_callback, $options = array() ) { |
564 global $wp_registered_widget_controls; |
576 global $wp_registered_widget_controls; |
565 |
577 |
566 $id = strtolower($id); |
578 $id = strtolower( $id ); |
567 |
579 |
568 if ( empty($form_callback) ) { |
580 if ( empty( $form_callback ) ) { |
569 unset($wp_registered_widget_controls[$id]); |
581 unset( $wp_registered_widget_controls[ $id ] ); |
570 return; |
582 return; |
571 } |
583 } |
572 |
584 |
573 if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) ) |
585 if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) { |
574 return; |
586 return; |
575 |
587 } |
576 $defaults = array('width' => 250, 'height' => 200 ); |
588 |
577 $options = wp_parse_args($options, $defaults); |
589 $defaults = array( |
578 $options['width'] = (int) $options['width']; |
590 'width' => 250, |
|
591 'height' => 200, |
|
592 ); |
|
593 $options = wp_parse_args( $options, $defaults ); |
|
594 $options['width'] = (int) $options['width']; |
579 $options['height'] = (int) $options['height']; |
595 $options['height'] = (int) $options['height']; |
580 |
596 |
581 $widget = array( |
597 $widget = array( |
582 'name' => $name, |
598 'name' => $name, |
583 'id' => $id, |
599 'id' => $id, |
584 'callback' => $form_callback, |
600 'callback' => $form_callback, |
585 'params' => array_slice(func_get_args(), 4) |
601 'params' => array_slice( func_get_args(), 4 ), |
586 ); |
602 ); |
587 $widget = array_merge($widget, $options); |
603 $widget = array_merge( $widget, $options ); |
588 |
604 |
589 $wp_registered_widget_controls[$id] = $widget; |
605 $wp_registered_widget_controls[ $id ] = $widget; |
590 } |
606 } |
591 |
607 |
592 /** |
608 /** |
593 * Remove control callback for widget. |
609 * Remove control callback for widget. |
594 * |
610 * |
595 * @since 2.2.0 |
611 * @since 2.2.0 |
596 * |
612 * |
597 * @param int|string $id Widget ID. |
613 * @param int|string $id Widget ID. |
598 */ |
614 */ |
599 function wp_unregister_widget_control($id) { |
615 function wp_unregister_widget_control( $id ) { |
600 wp_register_widget_control( $id, '', '' ); |
616 wp_register_widget_control( $id, '', '' ); |
601 } |
617 } |
602 |
618 |
603 /** |
619 /** |
604 * Display dynamic sidebar. |
620 * Display dynamic sidebar. |
607 * 'name' parameter for its registered sidebars you can pass an id or name as the $index parameter. |
623 * 'name' parameter for its registered sidebars you can pass an id or name as the $index parameter. |
608 * Otherwise, you can pass in a numerical index to display the sidebar at that index. |
624 * Otherwise, you can pass in a numerical index to display the sidebar at that index. |
609 * |
625 * |
610 * @since 2.2.0 |
626 * @since 2.2.0 |
611 * |
627 * |
612 * @global array $wp_registered_sidebars |
628 * @global array $wp_registered_sidebars Registered sidebars. |
613 * @global array $wp_registered_widgets |
629 * @global array $wp_registered_widgets |
614 * |
630 * |
615 * @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar. |
631 * @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar. |
616 * @return bool True, if widget sidebar was found and called. False if not found or not called. |
632 * @return bool True, if widget sidebar was found and called. False if not found or not called. |
617 */ |
633 */ |
633 $sidebars_widgets = wp_get_sidebars_widgets(); |
649 $sidebars_widgets = wp_get_sidebars_widgets(); |
634 if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) { |
650 if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) { |
635 /** This action is documented in wp-includes/widget.php */ |
651 /** This action is documented in wp-includes/widget.php */ |
636 do_action( 'dynamic_sidebar_before', $index, false ); |
652 do_action( 'dynamic_sidebar_before', $index, false ); |
637 /** This action is documented in wp-includes/widget.php */ |
653 /** This action is documented in wp-includes/widget.php */ |
638 do_action( 'dynamic_sidebar_after', $index, false ); |
654 do_action( 'dynamic_sidebar_after', $index, false ); |
639 /** This filter is documented in wp-includes/widget.php */ |
655 /** This filter is documented in wp-includes/widget.php */ |
640 return apply_filters( 'dynamic_sidebar_has_widgets', false, $index ); |
656 return apply_filters( 'dynamic_sidebar_has_widgets', false, $index ); |
641 } |
657 } |
642 |
658 |
643 /** |
659 /** |
651 * @param int|string $index Index, name, or ID of the dynamic sidebar. |
667 * @param int|string $index Index, name, or ID of the dynamic sidebar. |
652 * @param bool $has_widgets Whether the sidebar is populated with widgets. |
668 * @param bool $has_widgets Whether the sidebar is populated with widgets. |
653 * Default true. |
669 * Default true. |
654 */ |
670 */ |
655 do_action( 'dynamic_sidebar_before', $index, true ); |
671 do_action( 'dynamic_sidebar_before', $index, true ); |
656 $sidebar = $wp_registered_sidebars[$index]; |
672 $sidebar = $wp_registered_sidebars[ $index ]; |
657 |
673 |
658 $did_one = false; |
674 $did_one = false; |
659 foreach ( (array) $sidebars_widgets[$index] as $id ) { |
675 foreach ( (array) $sidebars_widgets[ $index ] as $id ) { |
660 |
676 |
661 if ( !isset($wp_registered_widgets[$id]) ) continue; |
677 if ( ! isset( $wp_registered_widgets[ $id ] ) ) { |
|
678 continue; |
|
679 } |
662 |
680 |
663 $params = array_merge( |
681 $params = array_merge( |
664 array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ), |
682 array( |
665 (array) $wp_registered_widgets[$id]['params'] |
683 array_merge( |
|
684 $sidebar, |
|
685 array( |
|
686 'widget_id' => $id, |
|
687 'widget_name' => $wp_registered_widgets[ $id ]['name'], |
|
688 ) |
|
689 ), |
|
690 ), |
|
691 (array) $wp_registered_widgets[ $id ]['params'] |
666 ); |
692 ); |
667 |
693 |
668 // Substitute HTML id and class attributes into before_widget |
694 // Substitute HTML id and class attributes into before_widget |
669 $classname_ = ''; |
695 $classname_ = ''; |
670 foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) { |
696 foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) { |
671 if ( is_string($cn) ) |
697 if ( is_string( $cn ) ) { |
672 $classname_ .= '_' . $cn; |
698 $classname_ .= '_' . $cn; |
673 elseif ( is_object($cn) ) |
699 } elseif ( is_object( $cn ) ) { |
674 $classname_ .= '_' . get_class($cn); |
700 $classname_ .= '_' . get_class( $cn ); |
675 } |
701 } |
676 $classname_ = ltrim($classname_, '_'); |
702 } |
677 $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_); |
703 $classname_ = ltrim( $classname_, '_' ); |
|
704 $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $id, $classname_ ); |
678 |
705 |
679 /** |
706 /** |
680 * Filters the parameters passed to a widget's display callback. |
707 * Filters the parameters passed to a widget's display callback. |
681 * |
708 * |
682 * Note: The filter is evaluated on both the front end and back end, |
709 * Note: The filter is evaluated on both the front end and back end, |
773 */ |
800 */ |
774 return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index ); |
801 return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index ); |
775 } |
802 } |
776 |
803 |
777 /** |
804 /** |
778 * Whether widget is displayed on the front end. |
805 * Determines whether a given widget is displayed on the front end. |
779 * |
806 * |
780 * Either $callback or $id_base can be used |
807 * Either $callback or $id_base can be used |
781 * $id_base is the first argument when extending WP_Widget class |
808 * $id_base is the first argument when extending WP_Widget class |
782 * Without the optional $widget_id parameter, returns the ID of the first sidebar |
809 * Without the optional $widget_id parameter, returns the ID of the first sidebar |
783 * in which the first instance of the widget with the given callback or $id_base is found. |
810 * in which the first instance of the widget with the given callback or $id_base is found. |
785 * the widget with that callback/$id_base AND that ID is found. |
812 * the widget with that callback/$id_base AND that ID is found. |
786 * |
813 * |
787 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective |
814 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective |
788 * this function has to run after widgets have initialized, at action {@see 'init'} or later. |
815 * this function has to run after widgets have initialized, at action {@see 'init'} or later. |
789 * |
816 * |
|
817 * For more information on this and similar theme functions, check out |
|
818 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
819 * Conditional Tags} article in the Theme Developer Handbook. |
|
820 * |
790 * @since 2.2.0 |
821 * @since 2.2.0 |
791 * |
822 * |
792 * @global array $wp_registered_widgets |
823 * @global array $wp_registered_widgets |
793 * |
824 * |
794 * @param string|false $callback Optional, Widget callback to check. Default false. |
825 * @param string|false $callback Optional, Widget callback to check. Default false. |
800 function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) { |
831 function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) { |
801 global $wp_registered_widgets; |
832 global $wp_registered_widgets; |
802 |
833 |
803 $sidebars_widgets = wp_get_sidebars_widgets(); |
834 $sidebars_widgets = wp_get_sidebars_widgets(); |
804 |
835 |
805 if ( is_array($sidebars_widgets) ) { |
836 if ( is_array( $sidebars_widgets ) ) { |
806 foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
837 foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
807 if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) { |
838 if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) { |
808 continue; |
839 continue; |
809 } |
840 } |
810 |
841 |
811 if ( is_array($widgets) ) { |
842 if ( is_array( $widgets ) ) { |
812 foreach ( $widgets as $widget ) { |
843 foreach ( $widgets as $widget ) { |
813 if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && _get_widget_id_base($widget) == $id_base ) ) { |
844 if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] == $callback ) || ( $id_base && _get_widget_id_base( $widget ) == $id_base ) ) { |
814 if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] ) |
845 if ( ! $widget_id || $widget_id == $wp_registered_widgets[ $widget ]['id'] ) { |
815 return $sidebar; |
846 return $sidebar; |
|
847 } |
816 } |
848 } |
817 } |
849 } |
818 } |
850 } |
819 } |
851 } |
820 } |
852 } |
821 return false; |
853 return false; |
822 } |
854 } |
823 |
855 |
824 /** |
856 /** |
825 * Whether the dynamic sidebar is enabled and used by theme. |
857 * Determines whether the dynamic sidebar is enabled and used by the theme. |
|
858 * |
|
859 * For more information on this and similar theme functions, check out |
|
860 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
861 * Conditional Tags} article in the Theme Developer Handbook. |
826 * |
862 * |
827 * @since 2.2.0 |
863 * @since 2.2.0 |
828 * |
864 * |
829 * @global array $wp_registered_widgets |
865 * @global array $wp_registered_widgets |
830 * @global array $wp_registered_sidebars |
866 * @global array $wp_registered_sidebars Registered sidebars. |
831 * |
867 * |
832 * @return bool True, if using widgets. False, if not using widgets. |
868 * @return bool True, if using widgets. False, if not using widgets. |
833 */ |
869 */ |
834 function is_dynamic_sidebar() { |
870 function is_dynamic_sidebar() { |
835 global $wp_registered_widgets, $wp_registered_sidebars; |
871 global $wp_registered_widgets, $wp_registered_sidebars; |
836 $sidebars_widgets = get_option('sidebars_widgets'); |
872 $sidebars_widgets = get_option( 'sidebars_widgets' ); |
837 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) { |
873 foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) { |
838 if ( ! empty( $sidebars_widgets[ $index ] ) ) { |
874 if ( ! empty( $sidebars_widgets[ $index ] ) ) { |
839 foreach ( (array) $sidebars_widgets[$index] as $widget ) |
875 foreach ( (array) $sidebars_widgets[ $index ] as $widget ) { |
840 if ( array_key_exists($widget, $wp_registered_widgets) ) |
876 if ( array_key_exists( $widget, $wp_registered_widgets ) ) { |
841 return true; |
877 return true; |
|
878 } |
|
879 } |
842 } |
880 } |
843 } |
881 } |
844 return false; |
882 return false; |
845 } |
883 } |
846 |
884 |
847 /** |
885 /** |
848 * Whether a sidebar is in use. |
886 * Determines whether a sidebar is in use. |
|
887 * |
|
888 * For more information on this and similar theme functions, check out |
|
889 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
890 * Conditional Tags} article in the Theme Developer Handbook. |
849 * |
891 * |
850 * @since 2.8.0 |
892 * @since 2.8.0 |
851 * |
893 * |
852 * @param string|int $index Sidebar name, id or number to check. |
894 * @param string|int $index Sidebar name, id or number to check. |
853 * @return bool true if the sidebar is in use, false otherwise. |
895 * @return bool true if the sidebar is in use, false otherwise. |
854 */ |
896 */ |
855 function is_active_sidebar( $index ) { |
897 function is_active_sidebar( $index ) { |
856 $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index); |
898 $index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index ); |
857 $sidebars_widgets = wp_get_sidebars_widgets(); |
899 $sidebars_widgets = wp_get_sidebars_widgets(); |
858 $is_active_sidebar = ! empty( $sidebars_widgets[$index] ); |
900 $is_active_sidebar = ! empty( $sidebars_widgets[ $index ] ); |
859 |
901 |
860 /** |
902 /** |
861 * Filters whether a dynamic sidebar is considered "active". |
903 * Filters whether a dynamic sidebar is considered "active". |
862 * |
904 * |
863 * @since 3.9.0 |
905 * @since 3.9.0 |
887 * |
929 * |
888 * @param bool $deprecated Not used (argument deprecated). |
930 * @param bool $deprecated Not used (argument deprecated). |
889 * @return array Upgraded list of widgets to version 3 array format when called from the admin. |
931 * @return array Upgraded list of widgets to version 3 array format when called from the admin. |
890 */ |
932 */ |
891 function wp_get_sidebars_widgets( $deprecated = true ) { |
933 function wp_get_sidebars_widgets( $deprecated = true ) { |
892 if ( $deprecated !== true ) |
934 if ( $deprecated !== true ) { |
893 _deprecated_argument( __FUNCTION__, '2.8.1' ); |
935 _deprecated_argument( __FUNCTION__, '2.8.1' ); |
|
936 } |
894 |
937 |
895 global $_wp_sidebars_widgets, $sidebars_widgets; |
938 global $_wp_sidebars_widgets, $sidebars_widgets; |
896 |
939 |
897 // If loading from front page, consult $_wp_sidebars_widgets rather than options |
940 // If loading from front page, consult $_wp_sidebars_widgets rather than options |
898 // to see if wp_convert_widget_settings() has made manipulations in memory. |
941 // to see if wp_convert_widget_settings() has made manipulations in memory. |
899 if ( !is_admin() ) { |
942 if ( ! is_admin() ) { |
900 if ( empty($_wp_sidebars_widgets) ) |
943 if ( empty( $_wp_sidebars_widgets ) ) { |
901 $_wp_sidebars_widgets = get_option('sidebars_widgets', array()); |
944 $_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
|
945 } |
902 |
946 |
903 $sidebars_widgets = $_wp_sidebars_widgets; |
947 $sidebars_widgets = $_wp_sidebars_widgets; |
904 } else { |
948 } else { |
905 $sidebars_widgets = get_option('sidebars_widgets', array()); |
949 $sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
906 } |
950 } |
907 |
951 |
908 if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) ) |
952 if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) { |
909 unset($sidebars_widgets['array_version']); |
953 unset( $sidebars_widgets['array_version'] ); |
|
954 } |
910 |
955 |
911 /** |
956 /** |
912 * Filters the list of sidebars and their widgets. |
957 * Filters the list of sidebars and their widgets. |
913 * |
958 * |
914 * @since 2.7.0 |
959 * @since 2.7.0 |
971 * @param string $base_name |
1017 * @param string $base_name |
972 * @param string $option_name |
1018 * @param string $option_name |
973 * @param array $settings |
1019 * @param array $settings |
974 * @return array |
1020 * @return array |
975 */ |
1021 */ |
976 function wp_convert_widget_settings($base_name, $option_name, $settings) { |
1022 function wp_convert_widget_settings( $base_name, $option_name, $settings ) { |
977 // This test may need expanding. |
1023 // This test may need expanding. |
978 $single = $changed = false; |
1024 $single = $changed = false; |
979 if ( empty($settings) ) { |
1025 if ( empty( $settings ) ) { |
980 $single = true; |
1026 $single = true; |
981 } else { |
1027 } else { |
982 foreach ( array_keys($settings) as $number ) { |
1028 foreach ( array_keys( $settings ) as $number ) { |
983 if ( 'number' == $number ) |
1029 if ( 'number' == $number ) { |
984 continue; |
1030 continue; |
985 if ( !is_numeric($number) ) { |
1031 } |
|
1032 if ( ! is_numeric( $number ) ) { |
986 $single = true; |
1033 $single = true; |
987 break; |
1034 break; |
988 } |
1035 } |
989 } |
1036 } |
990 } |
1037 } |
992 if ( $single ) { |
1039 if ( $single ) { |
993 $settings = array( 2 => $settings ); |
1040 $settings = array( 2 => $settings ); |
994 |
1041 |
995 // If loading from the front page, update sidebar in memory but don't save to options |
1042 // If loading from the front page, update sidebar in memory but don't save to options |
996 if ( is_admin() ) { |
1043 if ( is_admin() ) { |
997 $sidebars_widgets = get_option('sidebars_widgets'); |
1044 $sidebars_widgets = get_option( 'sidebars_widgets' ); |
998 } else { |
1045 } else { |
999 if ( empty($GLOBALS['_wp_sidebars_widgets']) ) |
1046 if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) { |
1000 $GLOBALS['_wp_sidebars_widgets'] = get_option('sidebars_widgets', array()); |
1047 $GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() ); |
|
1048 } |
1001 $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets']; |
1049 $sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets']; |
1002 } |
1050 } |
1003 |
1051 |
1004 foreach ( (array) $sidebars_widgets as $index => $sidebar ) { |
1052 foreach ( (array) $sidebars_widgets as $index => $sidebar ) { |
1005 if ( is_array($sidebar) ) { |
1053 if ( is_array( $sidebar ) ) { |
1006 foreach ( $sidebar as $i => $name ) { |
1054 foreach ( $sidebar as $i => $name ) { |
1007 if ( $base_name == $name ) { |
1055 if ( $base_name == $name ) { |
1008 $sidebars_widgets[$index][$i] = "$name-2"; |
1056 $sidebars_widgets[ $index ][ $i ] = "$name-2"; |
1009 $changed = true; |
1057 $changed = true; |
1010 break 2; |
1058 break 2; |
1011 } |
1059 } |
1012 } |
1060 } |
1013 } |
1061 } |
1014 } |
1062 } |
1015 |
1063 |
1016 if ( is_admin() && $changed ) |
1064 if ( is_admin() && $changed ) { |
1017 update_option('sidebars_widgets', $sidebars_widgets); |
1065 update_option( 'sidebars_widgets', $sidebars_widgets ); |
|
1066 } |
1018 } |
1067 } |
1019 |
1068 |
1020 $settings['_multiwidget'] = 1; |
1069 $settings['_multiwidget'] = 1; |
1021 if ( is_admin() ) |
1070 if ( is_admin() ) { |
1022 update_option( $option_name, $settings ); |
1071 update_option( $option_name, $settings ); |
|
1072 } |
1023 |
1073 |
1024 return $settings; |
1074 return $settings; |
1025 } |
1075 } |
1026 |
1076 |
1027 /** |
1077 /** |
1053 /* translators: %s: register_widget() */ |
1103 /* translators: %s: register_widget() */ |
1054 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Widgets need to be registered using %s, before they can be displayed.' ), '<code>register_widget()</code>' ), '4.9.0' ); |
1104 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Widgets need to be registered using %s, before they can be displayed.' ), '<code>register_widget()</code>' ), '4.9.0' ); |
1055 return; |
1105 return; |
1056 } |
1106 } |
1057 |
1107 |
1058 $widget_obj = $wp_widget_factory->widgets[$widget]; |
1108 $widget_obj = $wp_widget_factory->widgets[ $widget ]; |
1059 if ( ! ( $widget_obj instanceof WP_Widget ) ) { |
1109 if ( ! ( $widget_obj instanceof WP_Widget ) ) { |
1060 return; |
1110 return; |
1061 } |
1111 } |
1062 |
1112 |
1063 $default_args = array( |
1113 $default_args = array( |
1064 'before_widget' => '<div class="widget %s">', |
1114 'before_widget' => '<div class="widget %s">', |
1065 'after_widget' => "</div>", |
1115 'after_widget' => '</div>', |
1066 'before_title' => '<h2 class="widgettitle">', |
1116 'before_title' => '<h2 class="widgettitle">', |
1067 'after_title' => '</h2>', |
1117 'after_title' => '</h2>', |
1068 ); |
1118 ); |
1069 $args = wp_parse_args( $args, $default_args ); |
1119 $args = wp_parse_args( $args, $default_args ); |
1070 $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); |
1120 $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); |
1071 |
1121 |
1072 $instance = wp_parse_args($instance); |
1122 $instance = wp_parse_args( $instance ); |
1073 |
1123 |
1074 /** |
1124 /** |
1075 * Fires before rendering the requested widget. |
1125 * Fires before rendering the requested widget. |
1076 * |
1126 * |
1077 * @since 3.0.0 |
1127 * @since 3.0.0 |
1107 * @global array $sidebars_widgets |
1157 * @global array $sidebars_widgets |
1108 */ |
1158 */ |
1109 function _wp_sidebars_changed() { |
1159 function _wp_sidebars_changed() { |
1110 global $sidebars_widgets; |
1160 global $sidebars_widgets; |
1111 |
1161 |
1112 if ( ! is_array( $sidebars_widgets ) ) |
1162 if ( ! is_array( $sidebars_widgets ) ) { |
1113 $sidebars_widgets = wp_get_sidebars_widgets(); |
1163 $sidebars_widgets = wp_get_sidebars_widgets(); |
1114 |
1164 } |
1115 retrieve_widgets(true); |
1165 |
|
1166 retrieve_widgets( true ); |
1116 } |
1167 } |
1117 |
1168 |
1118 /** |
1169 /** |
1119 * Look for "lost" widgets, this has to run at least on each theme change. |
1170 * Look for "lost" widgets, this has to run at least on each theme change. |
1120 * |
1171 * |
1121 * @since 2.8.0 |
1172 * @since 2.8.0 |
1122 * |
1173 * |
1123 * @global array $wp_registered_sidebars |
1174 * @global array $wp_registered_sidebars Registered sidebars. |
1124 * @global array $sidebars_widgets |
1175 * @global array $sidebars_widgets |
1125 * @global array $wp_registered_widgets |
1176 * @global array $wp_registered_widgets |
1126 * |
1177 * |
1127 * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value |
1178 * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value |
1128 * of 'customize' defers updates for the Customizer. |
1179 * of 'customize' defers updates for the Customizer. |
1217 |
1268 |
1218 foreach ( $wp_registered_sidebars as $sidebar => $name ) { |
1269 foreach ( $wp_registered_sidebars as $sidebar => $name ) { |
1219 if ( in_array( $sidebar, $existing_sidebars, true ) ) { |
1270 if ( in_array( $sidebar, $existing_sidebars, true ) ) { |
1220 $new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ]; |
1271 $new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ]; |
1221 unset( $existing_sidebars_widgets[ $sidebar ] ); |
1272 unset( $existing_sidebars_widgets[ $sidebar ] ); |
1222 } else if ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) { |
1273 } elseif ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) { |
1223 $new_sidebars_widgets[ $sidebar ] = array(); |
1274 $new_sidebars_widgets[ $sidebar ] = array(); |
1224 } |
1275 } |
1225 } |
1276 } |
1226 |
1277 |
1227 // If there are more sidebars, try to map them. |
1278 // If there are more sidebars, try to map them. |
1343 } // endforeach ( $old_widgets as $key => $widget_id ) |
1394 } // endforeach ( $old_widgets as $key => $widget_id ) |
1344 } // endforeach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) |
1395 } // endforeach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) |
1345 } // endforeach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) |
1396 } // endforeach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) |
1346 } // endif ( ! empty( $old_sidebars_widgets ) ) |
1397 } // endif ( ! empty( $old_sidebars_widgets ) ) |
1347 |
1398 |
1348 |
|
1349 // Restore widget settings from when theme was previously active. |
1399 // Restore widget settings from when theme was previously active. |
1350 $new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets ); |
1400 $new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets ); |
1351 } |
1401 } |
1352 |
1402 |
1353 return $new_sidebars_widgets; |
1403 return $new_sidebars_widgets; |
1384 * @param string|array|object $rss RSS url. |
1434 * @param string|array|object $rss RSS url. |
1385 * @param array $args Widget arguments. |
1435 * @param array $args Widget arguments. |
1386 */ |
1436 */ |
1387 function wp_widget_rss_output( $rss, $args = array() ) { |
1437 function wp_widget_rss_output( $rss, $args = array() ) { |
1388 if ( is_string( $rss ) ) { |
1438 if ( is_string( $rss ) ) { |
1389 $rss = fetch_feed($rss); |
1439 $rss = fetch_feed( $rss ); |
1390 } elseif ( is_array($rss) && isset($rss['url']) ) { |
1440 } elseif ( is_array( $rss ) && isset( $rss['url'] ) ) { |
1391 $args = $rss; |
1441 $args = $rss; |
1392 $rss = fetch_feed($rss['url']); |
1442 $rss = fetch_feed( $rss['url'] ); |
1393 } elseif ( !is_object($rss) ) { |
1443 } elseif ( ! is_object( $rss ) ) { |
1394 return; |
1444 return; |
1395 } |
1445 } |
1396 |
1446 |
1397 if ( is_wp_error($rss) ) { |
1447 if ( is_wp_error( $rss ) ) { |
1398 if ( is_admin() || current_user_can('manage_options') ) |
1448 if ( is_admin() || current_user_can( 'manage_options' ) ) { |
1399 echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</p>'; |
1449 echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</p>'; |
|
1450 } |
1400 return; |
1451 return; |
1401 } |
1452 } |
1402 |
1453 |
1403 $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0, 'items' => 0 ); |
1454 $default_args = array( |
1404 $args = wp_parse_args( $args, $default_args ); |
1455 'show_author' => 0, |
|
1456 'show_date' => 0, |
|
1457 'show_summary' => 0, |
|
1458 'items' => 0, |
|
1459 ); |
|
1460 $args = wp_parse_args( $args, $default_args ); |
1405 |
1461 |
1406 $items = (int) $args['items']; |
1462 $items = (int) $args['items']; |
1407 if ( $items < 1 || 20 < $items ) |
1463 if ( $items < 1 || 20 < $items ) { |
1408 $items = 10; |
1464 $items = 10; |
1409 $show_summary = (int) $args['show_summary']; |
1465 } |
1410 $show_author = (int) $args['show_author']; |
1466 $show_summary = (int) $args['show_summary']; |
1411 $show_date = (int) $args['show_date']; |
1467 $show_author = (int) $args['show_author']; |
1412 |
1468 $show_date = (int) $args['show_date']; |
1413 if ( !$rss->get_item_quantity() ) { |
1469 |
|
1470 if ( ! $rss->get_item_quantity() ) { |
1414 echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; |
1471 echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; |
1415 $rss->__destruct(); |
1472 $rss->__destruct(); |
1416 unset($rss); |
1473 unset( $rss ); |
1417 return; |
1474 return; |
1418 } |
1475 } |
1419 |
1476 |
1420 echo '<ul>'; |
1477 echo '<ul>'; |
1421 foreach ( $rss->get_items( 0, $items ) as $item ) { |
1478 foreach ( $rss->get_items( 0, $items ) as $item ) { |
1487 * |
1544 * |
1488 * @param array|string $args Values for input fields. |
1545 * @param array|string $args Values for input fields. |
1489 * @param array $inputs Override default display options. |
1546 * @param array $inputs Override default display options. |
1490 */ |
1547 */ |
1491 function wp_widget_rss_form( $args, $inputs = null ) { |
1548 function wp_widget_rss_form( $args, $inputs = null ) { |
1492 $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true ); |
1549 $default_inputs = array( |
1493 $inputs = wp_parse_args( $inputs, $default_inputs ); |
1550 'url' => true, |
|
1551 'title' => true, |
|
1552 'items' => true, |
|
1553 'show_summary' => true, |
|
1554 'show_author' => true, |
|
1555 'show_date' => true, |
|
1556 ); |
|
1557 $inputs = wp_parse_args( $inputs, $default_inputs ); |
1494 |
1558 |
1495 $args['title'] = isset( $args['title'] ) ? $args['title'] : ''; |
1559 $args['title'] = isset( $args['title'] ) ? $args['title'] : ''; |
1496 $args['url'] = isset( $args['url'] ) ? $args['url'] : ''; |
1560 $args['url'] = isset( $args['url'] ) ? $args['url'] : ''; |
1497 $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0; |
1561 $args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0; |
1498 |
1562 |
1499 if ( $args['items'] < 1 || 20 < $args['items'] ) { |
1563 if ( $args['items'] < 1 || 20 < $args['items'] ) { |
1500 $args['items'] = 10; |
1564 $args['items'] = 10; |
1501 } |
1565 } |
1502 |
1566 |
1503 $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary']; |
1567 $args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary']; |
1504 $args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author']; |
1568 $args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author']; |
1505 $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date']; |
1569 $args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date']; |
1506 |
1570 |
1507 if ( ! empty( $args['error'] ) ) { |
1571 if ( ! empty( $args['error'] ) ) { |
1508 echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $args['error'] . '</p>'; |
1572 echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $args['error'] . '</p>'; |
1509 } |
1573 } |
1510 |
1574 |
1511 $esc_number = esc_attr( $args['number'] ); |
1575 $esc_number = esc_attr( $args['number'] ); |
1512 if ( $inputs['url'] ) : |
1576 if ( $inputs['url'] ) : |
1513 ?> |
1577 ?> |
1514 <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label> |
1578 <p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label> |
1515 <input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p> |
1579 <input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p> |
1516 <?php endif; if ( $inputs['title'] ) : ?> |
1580 <?php endif; if ( $inputs['title'] ) : ?> |
1517 <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label> |
1581 <p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label> |
1518 <input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p> |
1582 <input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p> |
1532 <p><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> /> |
1596 <p><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> /> |
1533 <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label></p> |
1597 <label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label></p> |
1534 <?php endif; if ( $inputs['show_date'] ) : ?> |
1598 <?php endif; if ( $inputs['show_date'] ) : ?> |
1535 <p><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/> |
1599 <p><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/> |
1536 <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label></p> |
1600 <label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label></p> |
1537 <?php |
1601 <?php |
1538 endif; |
1602 endif; |
1539 foreach ( array_keys($default_inputs) as $input ) : |
1603 foreach ( array_keys( $default_inputs ) as $input ) : |
1540 if ( 'hidden' === $inputs[$input] ) : |
1604 if ( 'hidden' === $inputs[ $input ] ) : |
1541 $id = str_replace( '_', '-', $input ); |
1605 $id = str_replace( '_', '-', $input ); |
1542 ?> |
1606 ?> |
1543 <input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" /> |
1607 <input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" /> |
1544 <?php |
1608 <?php |
1545 endif; |
1609 endif; |
1546 endforeach; |
1610 endforeach; |
1547 } |
1611 } |
1548 |
1612 |
1549 /** |
1613 /** |
1550 * Process RSS feed widget data and optionally retrieve feed items. |
1614 * Process RSS feed widget data and optionally retrieve feed items. |
1562 * @param bool $check_feed Optional, default is true. Whether to check feed for errors. |
1626 * @param bool $check_feed Optional, default is true. Whether to check feed for errors. |
1563 * @return array |
1627 * @return array |
1564 */ |
1628 */ |
1565 function wp_widget_rss_process( $widget_rss, $check_feed = true ) { |
1629 function wp_widget_rss_process( $widget_rss, $check_feed = true ) { |
1566 $items = (int) $widget_rss['items']; |
1630 $items = (int) $widget_rss['items']; |
1567 if ( $items < 1 || 20 < $items ) |
1631 if ( $items < 1 || 20 < $items ) { |
1568 $items = 10; |
1632 $items = 10; |
1569 $url = esc_url_raw( strip_tags( $widget_rss['url'] ) ); |
1633 } |
1570 $title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : ''; |
1634 $url = esc_url_raw( strip_tags( $widget_rss['url'] ) ); |
1571 $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0; |
1635 $title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : ''; |
1572 $show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] :0; |
1636 $show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0; |
1573 $show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0; |
1637 $show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0; |
|
1638 $show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0; |
1574 |
1639 |
1575 if ( $check_feed ) { |
1640 if ( $check_feed ) { |
1576 $rss = fetch_feed($url); |
1641 $rss = fetch_feed( $url ); |
1577 $error = false; |
1642 $error = false; |
1578 $link = ''; |
1643 $link = ''; |
1579 if ( is_wp_error($rss) ) { |
1644 if ( is_wp_error( $rss ) ) { |
1580 $error = $rss->get_error_message(); |
1645 $error = $rss->get_error_message(); |
1581 } else { |
1646 } else { |
1582 $link = esc_url(strip_tags($rss->get_permalink())); |
1647 $link = esc_url( strip_tags( $rss->get_permalink() ) ); |
1583 while ( stristr($link, 'http') != $link ) |
1648 while ( stristr( $link, 'http' ) != $link ) { |
1584 $link = substr($link, 1); |
1649 $link = substr( $link, 1 ); |
|
1650 } |
1585 |
1651 |
1586 $rss->__destruct(); |
1652 $rss->__destruct(); |
1587 unset($rss); |
1653 unset( $rss ); |
1588 } |
1654 } |
1589 } |
1655 } |
1590 |
1656 |
1591 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' ); |
1657 return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' ); |
1592 } |
1658 } |