From 3616fe59399bf91b1e3267125e83a1d18a5145d3 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 13 Oct 2015 01:32:27 +0000 Subject: [PATCH] Widgets: add a function, `is_registered_sidebar()` - helps us avoid touching the `$wp_registered_sidebars` global. Props GaryJ, wonderboymusic. Fixes #24878. git-svn-id: https://develop.svn.wordpress.org/trunk@35102 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/widgets.php | 4 ++-- .../class-wp-customize-widgets.php | 10 +++----- src/wp-includes/widget-functions.php | 23 ++++++++++++++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/wp-admin/widgets.php b/src/wp-admin/widgets.php index 165f190f1f..6dc662afc0 100644 --- a/src/wp-admin/widgets.php +++ b/src/wp-admin/widgets.php @@ -89,7 +89,7 @@ foreach ( $sidebars_widgets as $sidebar_id => $widgets ) { if ( 'wp_inactive_widgets' == $sidebar_id ) continue; - if ( !isset( $wp_registered_sidebars[ $sidebar_id ] ) ) { + if ( ! is_registered_sidebar( $sidebar_id ) ) { if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar register_sidebar(array( 'name' => __( 'Inactive Sidebar (not used)' ), @@ -155,7 +155,7 @@ if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) { /** * Fires immediately after a widget has been marked for deletion. - * + * * @since 4.4.0 * * @param string $widget_id ID of the widget marked for deletion. diff --git a/src/wp-includes/class-wp-customize-widgets.php b/src/wp-includes/class-wp-customize-widgets.php index a003393298..7b73dc2f15 100644 --- a/src/wp-includes/class-wp-customize-widgets.php +++ b/src/wp-includes/class-wp-customize-widgets.php @@ -365,7 +365,7 @@ final class WP_Customize_Widgets { $sidebar_widget_ids = array(); } - $is_registered_sidebar = isset( $wp_registered_sidebars[ $sidebar_id ] ); + $is_registered_sidebar = is_registered_sidebar( $sidebar_id ); $is_inactive_widgets = ( 'wp_inactive_widgets' === $sidebar_id ); $is_active_sidebar = ( $is_registered_sidebar && ! $is_inactive_widgets ); @@ -1102,14 +1102,12 @@ final class WP_Customize_Widgets { * @since 3.9.0 * @access public * - * @global array $wp_registered_sidebars - * * @param bool $is_active Whether the sidebar is active. * @param string $sidebar_id Sidebar ID. * @return bool */ public function tally_sidebars_via_is_active_sidebar_calls( $is_active, $sidebar_id ) { - if ( isset( $GLOBALS['wp_registered_sidebars'][$sidebar_id] ) ) { + if ( is_registered_sidebar( $sidebar_id ) ) { $this->rendered_sidebars[] = $sidebar_id; } /* @@ -1130,14 +1128,12 @@ final class WP_Customize_Widgets { * @since 3.9.0 * @access public * - * @global array $wp_registered_sidebars - * * @param bool $has_widgets Whether the current sidebar has widgets. * @param string $sidebar_id Sidebar ID. * @return bool */ public function tally_sidebars_via_dynamic_sidebar_calls( $has_widgets, $sidebar_id ) { - if ( isset( $GLOBALS['wp_registered_sidebars'][$sidebar_id] ) ) { + if ( is_registered_sidebar( $sidebar_id ) ) { $this->rendered_sidebars[] = $sidebar_id; } diff --git a/src/wp-includes/widget-functions.php b/src/wp-includes/widget-functions.php index 2d5982777f..79c26c242f 100644 --- a/src/wp-includes/widget-functions.php +++ b/src/wp-includes/widget-functions.php @@ -96,13 +96,14 @@ function register_sidebars( $number = 1, $args = array() ) { if ( isset($args['id']) ) { $_args['id'] = $args['id']; $n = 2; // Start at -2 for conflicting custom ID's - while ( isset($wp_registered_sidebars[$_args['id']]) ) + while ( is_registered_sidebar( $_args['id'] ) ) { $_args['id'] = $args['id'] . '-' . $n++; + } } else { - $n = count($wp_registered_sidebars); + $n = count( $wp_registered_sidebars ); do { $_args['id'] = 'sidebar-' . ++$n; - } while ( isset($wp_registered_sidebars[$_args['id']]) ); + } while ( is_registered_sidebar( $_args['id'] ) ); } register_sidebar($_args); } @@ -205,6 +206,22 @@ function unregister_sidebar( $name ) { unset( $wp_registered_sidebars[ $name ] ); } +/** + * Checks if a sidebar is registered. + * + * @since 4.4.0 + * + * @global array $wp_registered_sidebars Registered sidebars. + * + * @param string $name The ID of the sidebar when it was added. + * + * @return bool True if the sidebar is registered, false otherwise. + */ +function is_registered_sidebar( $name ) { + global $wp_registered_sidebars; + return isset( $wp_registered_sidebars[ $name ] ); +} + /** * Register an instance of a widget. *