Make WordPress Core

Ticket #11160: 11160-3.patch

File 11160-3.patch, 5.6 KB (added by CharlesClarkson, 14 years ago)
  • 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    // This section is for backward compatibility
     473
     474    // $args is a name or an id
     475    if ( is_int($args) ) {
     476        return "sidebar-{$args}";
     477
     478    } elseif ( is_string($args) ) {
     479        $args = sanitize_title($args);
     480        foreach ( (array) $wp_registered_sidebars as $id => $value ) {
     481            if ( sanitize_title($value['name']) == $args ) {
     482                return $id;
     483            }
     484        }
     485
     486        return $args;
     487
     488        // args is an array with an exact id or an exact name or both.
     489    } elseif ( is_array($args) ) {
     490
     491        // id takes precedence over name.
     492        if ( array_key_exists('id', $args) && array_key_exists($args['id'], $wp_registered_sidebars) )
     493            return $args['id'];
     494
     495        if ( array_key_exists('name', $args) ) {
     496            foreach ( (array) $wp_registered_sidebars as $id => $value ) {
     497                if ( $value['name'] == $args['name'] ) {
     498                    return $id;
     499                }
     500            }
     501        }
     502    }
     503
     504    return false;
     505}
     506
     507
     508/**
    451509 * Creates multiple sidebars.
    452510 *
    453511 * If you wanted to quickly create multiple sidebars for a theme or internally.
     
    565623 *
    566624 * @uses $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID.
    567625 *
    568  * @param string $name The ID of the sidebar when it was added.
     626 * @param int|string|array $name The name or id of the sidebar as registered with
     627 * register_sidebar(s) functions. If an array, use the register_sidebar() syntax
     628 * including keys 'name', 'id' or both.
    569629 */
    570 function unregister_sidebar( $name ) {
     630function unregister_sidebar($name) {
    571631        global $wp_registered_sidebars;
     632        $name = wp_get_sidebar_id($name);
    572633
    573634        if ( isset( $wp_registered_sidebars[$name] ) )
    574635                unset( $wp_registered_sidebars[$name] );
     
    825886 *
    826887 * @since 2.2.0
    827888 *
    828  * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
     889 * @param int|string|array $args Optional, default is 1. The name or id of the wigdet
     890 * specified by register_sidebar(s) functions. As an array use the register_sidebar()
     891 * syntax identifying sidebar by name or id or both.
    829892 * @return bool True, if widget sidebar was found and called. False if not found or not called.
    830893 */
    831 function dynamic_sidebar($index = 1) {
     894function dynamic_sidebar($name = 1) {
    832895        global $wp_registered_sidebars, $wp_registered_widgets;
    833896
    834         if ( is_int($index) ) {
    835                 $index = "sidebar-$index";
    836         } else {
    837                 $index = sanitize_title($index);
    838                 foreach ( (array) $wp_registered_sidebars as $key => $value ) {
    839                         if ( sanitize_title($value['name']) == $index ) {
    840                                 $index = $key;
    841                                 break;
    842                         }
    843                 }
    844         }
     897        $name = wp_get_sidebar_id($name);
    845898
    846899        $sidebars_widgets = wp_get_sidebars_widgets();
    847900
    848         if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
     901        if ( empty($wp_registered_sidebars[$name]) || !array_key_exists($name, $sidebars_widgets) || !is_array($sidebars_widgets[$name]) || empty($sidebars_widgets[$name]) )
    849902                return false;
    850903
    851         $sidebar = $wp_registered_sidebars[$index];
     904        $sidebar = $wp_registered_sidebars[$name];
    852905
    853906        $did_one = false;
    854         foreach ( (array) $sidebars_widgets[$index] as $id ) {
     907        foreach ( (array) $sidebars_widgets[$name] as $id ) {
    855908
    856909                if ( !isset($wp_registered_widgets[$id]) ) continue;
    857910
     
    9531006 *
    9541007 * @since 2.8
    9551008 *
    956  * @param mixed $index, sidebar name, id or number to check.
     1009 * @param int|string|array $name The name or id of the sidebar as registered with
     1010 * register_sidebar(s) functions. If an array, use the register_sidebar() syntax
     1011 * including keys 'name' or 'id' or both.
     1012 *
    9571013 * @return bool true if the sidebar is in use, false otherwise.
    9581014 */
    959 function is_active_sidebar( $index ) {
    960         $index = ( is_int($index) ) ? "sidebar-$index" : sanitize_title($index);
     1015function is_active_sidebar($name) {
     1016        $name = wp_get_sidebar_id($name);
    9611017        $sidebars_widgets = wp_get_sidebars_widgets();
    962         if ( isset($sidebars_widgets[$index]) && !empty($sidebars_widgets[$index]) )
    963                 return true;
    9641018
    965         return false;
     1019        return !empty($sidebars_widgets[$name]);
    9661020}
    9671021
    9681022/* Internal Functions */