Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search: When disabling the search module, move any active search widgets to the inactive list. #8931

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
}
}
}