Make WordPress Core

Ticket #11160: 11160.diff

File 11160.diff, 4.7 KB (added by CharlesClarkson, 17 years ago)
  • wp-includes/widgets.php

     
    448448}
    449449
    450450/**
     451 * Return the ID of a sidebar given it's name or 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 = get_sidebar_id(1);
     457 *     $id = get_sidebar_id('Sidebar Top');
     458 *
     459 * get_sidebar_id() will also take an array of arguments. The array can contain a
     460 * sidebar ID, a sidebar name or both. key/values pairs are ignored.
     461 * $id = get_sidebar_id( array('id' => 'sidebar-1') );
     462 *
     463 * @since 2.9.0
     464 *
     465 * @param int|string|array $args The name or id of the wigdet specified by
     466 * register_sidebar(s) functions. As an array use the register_sidebar() syntax
     467 * identifying sidebar by name or id or both.
     468 * @return mixed returns false if id not found or returns sidebar id.
     469 */
     470function get_sidebar_id($args) {
     471    global $wp_registered_sidebars;
     472
     473    if ( is_int($args) || is_string($args) ) {
     474
     475        // $args is a name or an id
     476        $name_or_id = $args;
     477
     478        // for backward compatibility
     479        if ( is_int($name_or_id) ) {
     480            return "sidebar-{$name_or_id}";
     481
     482        } else {
     483            $name_or_id = sanitize_title($name_or_id);
     484            foreach ( (array) $wp_registered_sidebars as $id => $value ) {
     485                if ( sanitize_title($value['name']) == $name_or_id ) {
     486                    return $id;
     487                }
     488            }
     489        }
     490
     491        return $name_or_id;
     492
     493    } elseif ( is_array($args) ) {
     494
     495        if ( !array_key_exists('id', $args) and !array_key_exists('name', $args) )
     496            return false;
     497
     498        // $args is an array holding a name, an id or both.
     499        if ( array_key_exists('id', $args) && array_key_exists($args['id'], $wp_registered_sidebars) )
     500            return $args['id'];
     501
     502        if ( array_key_exists('name', $args) ) {
     503            $name = sanitize_title($args['name']);
     504            foreach ( (array) $wp_registered_sidebars as $id => $value ) {
     505                if ( sanitize_title($value['name']) == $name ) {
     506                    return $id;
     507                }
     508            }
     509        }
     510    }
     511
     512    return false;
     513}
     514
     515
     516/**
    451517 * Creates multiple sidebars.
    452518 *
    453519 * If you wanted to quickly create multiple sidebars for a theme or internally.
     
    564630 *
    565631 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
    566632 *
    567  * @param string $name The ID of the sidebar when it was added.
     633 * @param int|string|array $args The name or id of the wigdet specified by
     634 * register_sidebar(s) functions. As an array use the register_sidebar() syntax
     635 * identifying sidebar by name or id or both.
    568636 */
    569 function unregister_sidebar( $name ) {
     637function unregister_sidebar($name) {
    570638        global $wp_registered_sidebars;
    571639
     640        $name = get_sidebar_id($name);
     641
    572642        if ( isset( $wp_registered_sidebars[$name] ) )
    573643                unset( $wp_registered_sidebars[$name] );
    574644}
     
    802872 *
    803873 * @since 2.2.0
    804874 *
    805  * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
     875 * @param int|string|array $args Optional, default is 1. The name or id of the wigdet
     876 * specified by register_sidebar(s) functions. As an array use the register_sidebar()
     877 * syntax identifying sidebar by name or id or both.
    806878 * @return bool True, if widget sidebar was found and called. False if not found or not called.
    807879 */
    808880function dynamic_sidebar($index = 1) {
    809881        global $wp_registered_sidebars, $wp_registered_widgets;
    810882
    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         }
     883        $index = get_sidebar_id($index);
    822884
    823885        $sidebars_widgets = wp_get_sidebars_widgets();
    824886
     
    930992 *
    931993 * @since 2.8
    932994 *
    933  * @param mixed $index, sidebar name, id or number to check.
     995 * @param int|string|array $args The name or id of the wigdet specified by
     996 * register_sidebar(s) functions. As an array use the register_sidebar() syntax
     997 * identifying sidebar by name or id or both.
    934998 * @return bool true if the sidebar is in use, false otherwise.
    935999 */
    9361000function is_active_sidebar( $index ) {
    937         $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
     1001
     1002        $index = get_sidebar_id($index);
     1003
    9381004        $sidebars_widgets = wp_get_sidebars_widgets();
    9391005        if ( isset($sidebars_widgets[$index]) && !empty($sidebars_widgets[$index]) )
    9401006                return true;