Make WordPress Core

Ticket #11160: 11160-1.patch

File 11160-1.patch, 5.3 KB (added by azaozz, 14 years ago)
  • wp-includes/widgets.php

     
    381381/**
    382382 * Private
    383383 */
    384  $_wp_deprecated_widgets_callbacks = array(
    385         'wp_widget_pages',
     384$_wp_deprecated_widgets_callbacks = array(
     385        'wp_widget_pages',
    386386        'wp_widget_pages_control',
    387387        'wp_widget_calendar',
    388388        'wp_widget_calendar_control',
     
    404404        'wp_widget_rss_control',
    405405        'wp_widget_recent_comments',
    406406        'wp_widget_recent_comments_control'
    407  );
     407);
    408408
    409409/* Template tags & API functions */
    410410
     
    448448}
    449449
    450450/**
     451 * Return the registered ID of a sidebar given it's name or registration ID.
     452 *
     453 * Since register_sidebar() supports it, this function will look for sidebars
     454 * named as integers or as a default id (like 2 or 'sidebar-13') first and check
     455 * those as IDs if the name fails (and those IDs exist).
     456 * ie: $id = wp_get_sidebar_id(1);
     457 *     $id = wp_get_sidebar_id('Sidebar Top');
     458 *
     459 * wp_get_sidebar_id() will also take an array of arguments. The array should have
     460 * keys 'name', 'id' or both: $id = wp_get_sidebar_id( array('id' => 'sidebar-1') );
     461 *
     462 * @since 2.9.0
     463 *
     464 * @param int|string|array $args The name or id of the sidebar as registered with
     465 * register_sidebar(s) functions. If an array, use the register_sidebar() syntax.
     466 *
     467 * @return mixed returns false if id not found or returns sidebar id.
     468 */
     469function wp_get_sidebar_id($args) {
     470        global $wp_registered_sidebars;
     471
     472         if ( is_array($args) ) {
     473                // $args is an array holding a name, an id or both.
     474                if ( !empty($args['id']) )
     475                        $args = $args['id'];
     476                elseif ( !empty($args['name']) )
     477                        $args = $args['name'];
     478                else
     479                        return false;
     480        }
     481
     482        if ( (int) $args ) { // $args could be int 1 or string '1'
     483                return "sidebar-$args";
     484        } elseif ( is_string($args) ) {
     485                $name = sanitize_title($args);
     486
     487                if ( array_key_exists($name, (array) $wp_registered_sidebars) )
     488                        return $name;
     489
     490                foreach ( (array) $wp_registered_sidebars as $id => $value ) {
     491                        if ( sanitize_title($value['name']) == $name )
     492                                return $id;
     493                }
     494        }
     495
     496        return false;
     497}
     498
     499
     500/**
    451501 * Creates multiple sidebars.
    452502 *
    453503 * If you wanted to quickly create multiple sidebars for a theme or internally.
     
    564614 *
    565615 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
    566616 *
    567  * @param string $name The ID of the sidebar when it was added.
     617 * @param int|string|array $name The name or id of the sidebar as registered with
     618 * register_sidebar(s) functions. If an array, use the register_sidebar() syntax
     619 * including keys 'name', 'id' or both.
    568620 */
    569 function unregister_sidebar( $name ) {
     621function unregister_sidebar($name) {
    570622        global $wp_registered_sidebars;
     623        $name = wp_get_sidebar_id($name);
    571624
    572625        if ( isset( $wp_registered_sidebars[$name] ) )
    573626                unset( $wp_registered_sidebars[$name] );
     
    802855 *
    803856 * @since 2.2.0
    804857 *
    805  * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
     858 * @param int|string|array $args Optional, default is 1. The name or id of the wigdet
     859 * specified by register_sidebar(s) functions. As an array use the register_sidebar()
     860 * syntax identifying sidebar by name or id or both.
    806861 * @return bool True, if widget sidebar was found and called. False if not found or not called.
    807862 */
    808 function dynamic_sidebar($index = 1) {
     863function dynamic_sidebar($name = 1) {
    809864        global $wp_registered_sidebars, $wp_registered_widgets;
    810865
    811         if ( is_int($index) ) {
    812                 $index = "sidebar-$index";
    813         } else {
    814                 $index = sanitize_title($index);
    815                 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
    816                         if ( sanitize_title($value['name']) == $index ) {
    817                                 $index = $key;
    818                                 break;
    819                         }
    820                 }
    821         }
     866        $name = wp_get_sidebar_id($name);
    822867
    823868        $sidebars_widgets = wp_get_sidebars_widgets();
    824869
    825         if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
     870        if ( empty($wp_registered_sidebars[$name]) || !array_key_exists($name, $sidebars_widgets) || !is_array($sidebars_widgets[$name]) || empty($sidebars_widgets[$name]) )
    826871                return false;
    827872
    828         $sidebar = $wp_registered_sidebars[$index];
     873        $sidebar = $wp_registered_sidebars[$name];
    829874
    830875        $did_one = false;
    831         foreach ( (array) $sidebars_widgets[$index] as $id ) {
     876        foreach ( (array) $sidebars_widgets[$name] as $id ) {
    832877
    833878                if ( !isset($wp_registered_widgets[$id]) ) continue;
    834879
     
    930975 *
    931976 * @since 2.8
    932977 *
    933  * @param mixed $index, sidebar name, id or number to check.
     978 * @param int|string|array $name The name or id of the sidebar as registered with
     979 * register_sidebar(s) functions. If an array, use the register_sidebar() syntax
     980 * including keys 'name' or 'id' or both.
     981 *
    934982 * @return bool true if the sidebar is in use, false otherwise.
    935983 */
    936 function is_active_sidebar( $index ) {
    937         $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
     984function is_active_sidebar($name) {
     985        $name = wp_get_sidebar_id($name);
    938986        $sidebars_widgets = wp_get_sidebars_widgets();
    939         if ( isset($sidebars_widgets[$index]) && !empty($sidebars_widgets[$index]) )
    940                 return true;
    941 
    942         return false;
     987       
     988        return !empty($sidebars_widgets[$name]);
    943989}
    944990
    945991/* Internal Functions */