From 1ab21c73ae202f68e6affe40f562bba16167f874 Mon Sep 17 00:00:00 2001 From: Alex Mills Date: Mon, 26 Feb 2018 15:54:13 -0800 Subject: [PATCH] When disabling the search module, move any active search widgets to the inactive list. (#8931) --- modules/search/class.jetpack-search.php | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/modules/search/class.jetpack-search.php b/modules/search/class.jetpack-search.php index a83cfe73bf28a..6d5ac10264876 100644 --- a/modules/search/class.jetpack-search.php +++ b/modules/search/class.jetpack-search.php @@ -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' ) ); } /** @@ -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 ); + } + } }