Skip to content

Commit

Permalink
When disabling the search module, move any active search widgets to t…
Browse files Browse the repository at this point in the history
…he inactive list. (#8931)
  • Loading branch information
Viper007Bond authored Feb 26, 2018
1 parent ae4be92 commit 1ab21c7
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions modules/search/class.jetpack-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public function init_hooks() {
} else {
add_action( 'update_option', array( $this, 'track_widget_updates' ), 10, 3 );
}

add_action( 'jetpack_deactivate_module_search', array( $this, 'move_search_widgets_to_inactive' ) );
}

/**
Expand Down Expand Up @@ -1748,4 +1750,46 @@ public function track_widget_updates( $option, $old_value, $new_value ) {
$event['widget']
);
}

/**
* Moves any active search widgets to the inactive category.
*
* @since 5.9.0
*
* @param string $module Unused. The Jetpack module being disabled.
*/
public function move_search_widgets_to_inactive( $module ) {
if ( ! is_active_widget( false, false, Jetpack_Search_Helpers::FILTER_WIDGET_BASE, true ) ) {
return;
}

$sidebars_widgets = wp_get_sidebars_widgets();

if ( ! is_array( $sidebars_widgets ) ) {
return;
}

$changed = false;

foreach ( $sidebars_widgets as $sidebar => $widgets ) {
if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) {
continue;
}

if ( is_array( $widgets ) ) {
foreach ( $widgets as $key => $widget ) {
if ( _get_widget_id_base( $widget ) == Jetpack_Search_Helpers::FILTER_WIDGET_BASE ) {
$changed = true;

array_unshift( $sidebars_widgets['wp_inactive_widgets'], $widget );
unset( $sidebars_widgets[ $sidebar ][ $key ] );
}
}
}
}

if ( $changed ) {
wp_set_sidebars_widgets( $sidebars_widgets );
}
}
}

0 comments on commit 1ab21c7

Please sign in to comment.