From 7b439c95a717f3bf8b9196f323f10b790e076a65 Mon Sep 17 00:00:00 2001 From: Nathan Rice Date: Wed, 8 Mar 2017 22:42:02 -0500 Subject: [PATCH] Use more reliable method for switching sidebars. should eliminate issues moving sidebars in #22. --- .../class-genesis-simple-sidebars-core.php | 110 +++++++----------- 1 file changed, 44 insertions(+), 66 deletions(-) diff --git a/includes/class-genesis-simple-sidebars-core.php b/includes/class-genesis-simple-sidebars-core.php index 1fb525c..5fda384 100644 --- a/includes/class-genesis-simple-sidebars-core.php +++ b/includes/class-genesis-simple-sidebars-core.php @@ -23,7 +23,7 @@ class Genesis_Simple_Sidebars_Core { public function init() { add_action( 'widgets_init', array( $this, 'register_sidebars' ) ); - add_action( 'get_header', array( $this, 'swap_sidebars' ) ); + add_filter( 'sidebars_widgets', array( $this, 'sidebars_widgets_filter' ) ); } @@ -34,7 +34,7 @@ class Genesis_Simple_Sidebars_Core { */ public function register_sidebars() { - $sidebars = Genesis_Simple_Sidebars()->core->get_sidebars(); + $sidebars = $this->get_sidebars(); if ( ! $sidebars ) { return; @@ -59,98 +59,76 @@ class Genesis_Simple_Sidebars_Core { } /** - * Remove default sidebars and inject custom sidebars. + * Filter the widgets in each widget area. * * @since 2.1.0 */ - public function swap_sidebars() { + public function sidebars_widgets_filter( $widgets ) { - // Header - if ( is_registered_sidebar( 'header-right' ) ) { - global $wp_registered_sidebars; - $wp_registered_sidebars['ss-header-right-temp'] = $wp_registered_sidebars['header-right']; - unset( $wp_registered_sidebars['header-right'] ); - add_action( 'genesis_header_right', array( $this, 'do_header_right' ) ); - } + $sidebars = array( + 'sidebar' => '_ss_sidebar', + 'sidebar-alt' => '_ss_sidebar_alt', + 'header-right' => '_ss_header', + ); - // Sidebars - remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); - remove_action( 'genesis_sidebar_alt', 'genesis_do_sidebar_alt' ); - add_action( 'genesis_sidebar', array( $this, 'do_primary_sidebar' ) ); - add_action( 'genesis_sidebar_alt', array( $this, 'do_secondary_sidebar' ) ); + /** + * Swappable widget areas. + * + * An array of original widget area => GSS key for new sidebar. Can be used to add or remove widget areas from being + * swappable in the Genesis Simple Sidebars admin. + * + * @since 2.1.0 + * + * @param array $sidebars Array of widget areas that can be swapped, with the keys used to find the ID of the new widget area to swap in. + */ + $sidebars = apply_filters( 'genesis_simple_sidebars_widget_areas', $sidebars ); + + $widgets = $this->swap_widgets( $widgets, $sidebars ); + + return $widgets; } /** - * Output custom header widget area, if one is set. Otherwise output default. + * Take the $widgets array and swap the contents of each widget area with a custom widget area, if specified. * * @since 2.1.0 */ - public function do_header_right() { + public function swap_widgets( $widgets, $sidebars ) { - global $wp_registered_sidebars; - - if ( ! $this->do_sidebar( '_ss_header' ) ) { - $wp_registered_sidebars['header-right'] = $wp_registered_sidebars['ss-header-right-temp']; + if ( is_admin() ) { + return $widgets; } - unset( $wp_registered_sidebars['ss-header-right-temp'] ); + foreach ( (array) $sidebars as $old_sidebar => $new_sidebar_key ) { - } + if ( ! is_registered_sidebar( $old_sidebar ) ) { + continue; + } - /** - * Output custom primary sidebar, if one is set. Otherwise output default. - * - * @since 2.1.0 - */ - public function do_primary_sidebar() { + if ( is_singular() ) { - if ( ! $this->do_sidebar( '_ss_sidebar' ) ) { - genesis_do_sidebar(); - } + $new_sidebar = genesis_get_custom_field( $new_sidebar_key ); - } + if ( $new_sidebar && ! empty( $widgets[ $new_sidebar ] ) ) { + $widgets[ $old_sidebar ] = $widgets[ $new_sidebar ]; + } - /** - * Output custom secondary sidebar, if one is set. Otherwise output default. - * - * @since 2.1.0 - */ - public function do_secondary_sidebar() { + } - if ( ! $this->do_sidebar( '_ss_sidebar_alt' ) ) { - genesis_do_sidebar_alt(); - } + if ( is_tax() || is_category() || is_tag() ) { - } + $new_sidebar = get_term_meta( get_queried_object()->term_id, $new_sidebar_key, true ); - /** - * Show widgets in a particular sidebar. - * - * @param string $key sidebar id you wish to output. - * - * @since 2.1.0 - */ - public function do_sidebar( $key ) { + if ( $new_sidebar && ! empty( $widgets[ $new_sidebar ] ) ) { + $widgets[ $old_sidebar ] = $widgets[ $new_sidebar ]; + } - if ( is_singular() && $key = genesis_get_custom_field( $key ) ) { - - if ( dynamic_sidebar( $key ) ) { - return true; } } - if ( is_tax() || is_category() || is_tag() ) { - - if ( $key = get_term_meta( get_queried_object()->term_id, $key, true ) ) { - dynamic_sidebar( $key ); - return true; - } - - } - - return false; + return $widgets; }