Use more reliable method for switching sidebars. should eliminate issues moving sidebars in #22.

This commit is contained in:
Nathan Rice
2017-03-08 22:42:02 -05:00
parent 775ece1a8b
commit 7b439c95a7
+44 -66
View File
@@ -23,7 +23,7 @@ class Genesis_Simple_Sidebars_Core {
public function init() { public function init() {
add_action( 'widgets_init', array( $this, 'register_sidebars' ) ); 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() { public function register_sidebars() {
$sidebars = Genesis_Simple_Sidebars()->core->get_sidebars(); $sidebars = $this->get_sidebars();
if ( ! $sidebars ) { if ( ! $sidebars ) {
return; 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 * @since 2.1.0
*/ */
public function swap_sidebars() { public function sidebars_widgets_filter( $widgets ) {
// Header $sidebars = array(
if ( is_registered_sidebar( 'header-right' ) ) { 'sidebar' => '_ss_sidebar',
global $wp_registered_sidebars; 'sidebar-alt' => '_ss_sidebar_alt',
$wp_registered_sidebars['ss-header-right-temp'] = $wp_registered_sidebars['header-right']; 'header-right' => '_ss_header',
unset( $wp_registered_sidebars['header-right'] ); );
add_action( 'genesis_header_right', array( $this, 'do_header_right' ) );
}
// Sidebars /**
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); * Swappable widget areas.
remove_action( 'genesis_sidebar_alt', 'genesis_do_sidebar_alt' ); *
add_action( 'genesis_sidebar', array( $this, 'do_primary_sidebar' ) ); * An array of original widget area => GSS key for new sidebar. Can be used to add or remove widget areas from being
add_action( 'genesis_sidebar_alt', array( $this, 'do_secondary_sidebar' ) ); * 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 * @since 2.1.0
*/ */
public function do_header_right() { public function swap_widgets( $widgets, $sidebars ) {
global $wp_registered_sidebars; if ( is_admin() ) {
return $widgets;
if ( ! $this->do_sidebar( '_ss_header' ) ) {
$wp_registered_sidebars['header-right'] = $wp_registered_sidebars['ss-header-right-temp'];
} }
unset( $wp_registered_sidebars['ss-header-right-temp'] ); foreach ( (array) $sidebars as $old_sidebar => $new_sidebar_key ) {
} if ( ! is_registered_sidebar( $old_sidebar ) ) {
continue;
}
/** if ( is_singular() ) {
* Output custom primary sidebar, if one is set. Otherwise output default.
*
* @since 2.1.0
*/
public function do_primary_sidebar() {
if ( ! $this->do_sidebar( '_ss_sidebar' ) ) { $new_sidebar = genesis_get_custom_field( $new_sidebar_key );
genesis_do_sidebar();
}
} 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' ) ) { if ( is_tax() || is_category() || is_tag() ) {
genesis_do_sidebar_alt();
}
} $new_sidebar = get_term_meta( get_queried_object()->term_id, $new_sidebar_key, true );
/** if ( $new_sidebar && ! empty( $widgets[ $new_sidebar ] ) ) {
* Show widgets in a particular sidebar. $widgets[ $old_sidebar ] = $widgets[ $new_sidebar ];
* }
* @param string $key sidebar id you wish to output.
*
* @since 2.1.0
*/
public function do_sidebar( $key ) {
if ( is_singular() && $key = genesis_get_custom_field( $key ) ) {
if ( dynamic_sidebar( $key ) ) {
return true;
} }
} }
if ( is_tax() || is_category() || is_tag() ) { return $widgets;
if ( $key = get_term_meta( get_queried_object()->term_id, $key, true ) ) {
dynamic_sidebar( $key );
return true;
}
}
return false;
} }