From b6e4a62d889d9a9f1615d0529ed3d8ad99902180 Mon Sep 17 00:00:00 2001 From: ernilambar Date: Wed, 29 Jun 2016 13:40:13 +0545 Subject: [PATCH 1/6] Add reset subcommand for widget --- php/commands/widget.php | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/php/commands/widget.php b/php/commands/widget.php index 2ff89be750..0a412e18ec 100644 --- a/php/commands/widget.php +++ b/php/commands/widget.php @@ -304,6 +304,75 @@ public function delete( $args, $assoc_args ) { WP_CLI::success( "Widget(s) removed from sidebar." ); } + /** + * Reset sidebar. + * + * Removes all widgets from the sidebar and place those to Inactive Widgets. + * + * [...] + * : One or more sidebars to reset. + * + * [--all] + * : If set, all sidebars will be reset. + * + * ## EXAMPLES + * + * # Reset a sidebar + * $ wp widget reset sidebar-1 + * Success: Sidebar 'sidebar-1' reset. + * + * # Reset multiple sidebars + * $ wp widget reset sidebar-1 sidebar-2 + * Success: Sidebar 'sidebar-1' reset. + * Success: Sidebar 'sidebar-2' reset. + */ + public function reset( $args, $assoc_args ) { + + global $wp_registered_sidebars; + + $all = \WP_CLI\Utils\get_flag_value( $assoc_args, 'all', false ); + + // Bail if no arguments and no all flag. + if ( ! $all && empty( $args ) ) { + WP_CLI::error( 'Please specify one or more sidebars, or use --all.' ); + } + + // Fetch all sidebars if all flag is set. + if ( $all ) { + $args = array_keys( $wp_registered_sidebars ); + } + + // Sidebar ID wp_inactive_widgets is reserved by WP core for inactive widgets. + if ( isset( $args['wp_inactive_widgets'] ) ) { + unset( $args['wp_inactive_widgets'] ); + } + + // Check if no registered sidebar. + if ( empty( $args ) ) { + WP_CLI::error( 'No sidebar registered.' ); + } + + foreach ( $args as $sidebar_id ) { + if ( ! array_key_exists( $sidebar_id, $wp_registered_sidebars ) ) { + WP_CLI::warning( sprintf( 'Invalid sidebar: %s', $sidebar_id ) ); + continue; + } + + $widgets = $this->get_sidebar_widgets( $sidebar_id ); + if ( empty( $widgets ) ) { + WP_CLI::warning( sprintf( "'%s' is already empty.", $sidebar_id ) ); + } + else { + foreach ( $widgets as $widget ) { + $widget_id = $widget->id; + list( $name, $option_index, $new_sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id ); + $this->move_sidebar_widget( $widget_id, $new_sidebar_id, 'wp_inactive_widgets', $sidebar_index, 0 ); + } + WP_CLI::success( sprintf( "Sidebar '%s' reset.", $sidebar_id ) ); + } + } + } + /** * Check whether a sidebar is a valid sidebar * From c7567036d779b36df5111feb5491ed99a25035e6 Mon Sep 17 00:00:00 2001 From: ernilambar Date: Wed, 29 Jun 2016 14:05:17 +0545 Subject: [PATCH 2/6] Add test for widget reset command --- features/widget-reset.feature | 96 +++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 features/widget-reset.feature diff --git a/features/widget-reset.feature b/features/widget-reset.feature new file mode 100644 index 0000000000..52a042961a --- /dev/null +++ b/features/widget-reset.feature @@ -0,0 +1,96 @@ +Feature: Reset widgets in WordPress sidebar + + Scenario: Reset sidebar + Given a WP install + + When I run `wp theme install twentysixteen --activate` + Then STDOUT should not be empty + + When I run `wp widget list sidebar-1 --format=count` + Then STDOUT should be: + """ + 6 + """ + + When I run `wp widget reset sidebar-1` + And I run `wp widget list sidebar-1 --format=count` + Then STDOUT should be: + """ + 0 + """ + + When I try `wp widget reset` + Then STDERR should be: + """ + Error: Please specify one or more sidebars, or use --all. + """ + + When I try `wp widget reset sidebar-1` + Then STDERR should be: + """ + Warning: 'sidebar-1' is already empty. + """ + + When I try `wp widget reset non-existing-sidebar-id` + Then STDERR should be: + """ + Warning: Invalid sidebar: non-existing-sidebar-id + """ + + When I run `wp widget add calendar sidebar-1 --title="Calendar"` + Then STDOUT should not be empty + And I run `wp widget list sidebar-1 --format=count` + Then STDOUT should be: + """ + 1 + """ + + When I run `wp widget add search sidebar-2 --title="Quick Search"` + Then STDOUT should not be empty + And I run `wp widget list sidebar-2 --format=count` + Then STDOUT should be: + """ + 1 + """ + + When I run `wp widget reset sidebar-1 sidebar-2` + And I run `wp widget list sidebar-1 --format=count` + Then STDOUT should be: + """ + 0 + """ + And I run `wp widget list sidebar-2 --format=count` + Then STDOUT should be: + """ + 0 + """ + + Scenario: Reset all sidebars + Given a WP install + + When I run `wp theme install twentysixteen --activate` + Then STDOUT should not be empty + + When I run `wp widget add calendar sidebar-1 --title="Calendar"` + Then STDOUT should not be empty + When I run `wp widget add search sidebar-2 --title="Quick Search"` + Then STDOUT should not be empty + When I run `wp widget add text sidebar-3 --title="Text"` + Then STDOUT should not be empty + + When I run `wp widget reset --all` + And I run `wp widget list sidebar-1 --format=count` + Then STDOUT should be: + """ + 0 + """ + And I run `wp widget list sidebar-2 --format=count` + Then STDOUT should be: + """ + 0 + """ + And I run `wp widget list sidebar-3 --format=count` + Then STDOUT should be: + """ + 0 + """ From 6e886da6eaa44d24a7ae08d26cec5c48afe9e50b Mon Sep 17 00:00:00 2001 From: ernilambar Date: Wed, 29 Jun 2016 14:08:32 +0545 Subject: [PATCH 3/6] Add more examples for the command --- features/widget-reset.feature | 2 +- php/commands/widget.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/features/widget-reset.feature b/features/widget-reset.feature index 52a042961a..2a98611757 100644 --- a/features/widget-reset.feature +++ b/features/widget-reset.feature @@ -1,4 +1,4 @@ -Feature: Reset widgets in WordPress sidebar +Feature: Reset WordPress sidebars Scenario: Reset sidebar Given a WP install diff --git a/php/commands/widget.php b/php/commands/widget.php index 0a412e18ec..f401eaea00 100644 --- a/php/commands/widget.php +++ b/php/commands/widget.php @@ -325,6 +325,12 @@ public function delete( $args, $assoc_args ) { * $ wp widget reset sidebar-1 sidebar-2 * Success: Sidebar 'sidebar-1' reset. * Success: Sidebar 'sidebar-2' reset. + * + * # Reset all sidebars + * $ wp widget reset --all + * Success: Sidebar 'sidebar-1' reset. + * Success: Sidebar 'sidebar-2' reset. + * Success: Sidebar 'sidebar-3' reset. */ public function reset( $args, $assoc_args ) { From 7cfa5f056d6dc0e0e4ffa365798f8dd721134523 Mon Sep 17 00:00:00 2001 From: ernilambar Date: Wed, 29 Jun 2016 14:36:33 +0545 Subject: [PATCH 4/6] Improve test for widget reset --- features/widget-reset.feature | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/features/widget-reset.feature b/features/widget-reset.feature index 2a98611757..83ddf39d55 100644 --- a/features/widget-reset.feature +++ b/features/widget-reset.feature @@ -94,3 +94,31 @@ Feature: Reset WordPress sidebars """ 0 """ + + Scenario: Testing movement of widgets while reset + Given a WP install + + When I run `wp theme install twentysixteen --activate` + Then STDOUT should not be empty + + When I run `wp widget add calendar sidebar-2 --title="Calendar"` + Then STDOUT should not be empty + And I run `wp widget add search sidebar-2 --title="Quick Search"` + Then STDOUT should not be empty + + When I run `wp widget list sidebar-2 --format=ids` + Then STDOUT should be: + """ + search-3 calendar-1 + """ + When I run `wp widget list wp_inactive_widgets --format=ids` + Then STDOUT should be empty + + When I run `wp widget reset sidebar-2` + And I run `wp widget list sidebar-2 --format=ids` + Then STDOUT should be empty + And I run `wp widget list wp_inactive_widgets --format=ids` + Then STDOUT should be: + """ + calendar-1 search-3 + """ From e93078e4bfa47b091315635148dd0f76d441cf93 Mon Sep 17 00:00:00 2001 From: ernilambar Date: Wed, 29 Jun 2016 16:38:34 +0545 Subject: [PATCH 5/6] Resolving issues from the PR review in widget reset --- features/widget-reset.feature | 7 ++++++- php/commands/widget.php | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/features/widget-reset.feature b/features/widget-reset.feature index 83ddf39d55..482bafd5a4 100644 --- a/features/widget-reset.feature +++ b/features/widget-reset.feature @@ -28,7 +28,7 @@ Feature: Reset WordPress sidebars When I try `wp widget reset sidebar-1` Then STDERR should be: """ - Warning: 'sidebar-1' is already empty. + Warning: Sidebar 'sidebar-1' is already empty. """ When I try `wp widget reset non-existing-sidebar-id` @@ -94,6 +94,11 @@ Feature: Reset WordPress sidebars """ 0 """ + When I run `wp widget list wp_inactive_widgets --format=ids` + Then STDOUT should be: + """ + text-1 search-3 meta-2 categories-2 archives-2 recent-comments-2 recent-posts-2 search-2 calendar-1 + """ Scenario: Testing movement of widgets while reset Given a WP install diff --git a/php/commands/widget.php b/php/commands/widget.php index f401eaea00..1def3cb798 100644 --- a/php/commands/widget.php +++ b/php/commands/widget.php @@ -307,7 +307,7 @@ public function delete( $args, $assoc_args ) { /** * Reset sidebar. * - * Removes all widgets from the sidebar and place those to Inactive Widgets. + * Removes all widgets from the sidebar and places them in Inactive Widgets. * * [...] * : One or more sidebars to reset. @@ -366,7 +366,7 @@ public function reset( $args, $assoc_args ) { $widgets = $this->get_sidebar_widgets( $sidebar_id ); if ( empty( $widgets ) ) { - WP_CLI::warning( sprintf( "'%s' is already empty.", $sidebar_id ) ); + WP_CLI::warning( sprintf( "Sidebar '%s' is already empty.", $sidebar_id ) ); } else { foreach ( $widgets as $widget ) { From ff354272a5a938e58ad90334abd9792da2084c7d Mon Sep 17 00:00:00 2001 From: ernilambar Date: Wed, 29 Jun 2016 17:28:36 +0545 Subject: [PATCH 6/6] Use Twentytwelve theme in test of widget reset because 2016 wont run in WP 3.7 --- features/widget-reset.feature | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/features/widget-reset.feature b/features/widget-reset.feature index 482bafd5a4..4c2d84bdcc 100644 --- a/features/widget-reset.feature +++ b/features/widget-reset.feature @@ -3,7 +3,7 @@ Feature: Reset WordPress sidebars Scenario: Reset sidebar Given a WP install - When I run `wp theme install twentysixteen --activate` + When I run `wp theme install twentytwelve --activate` Then STDOUT should not be empty When I run `wp widget list sidebar-1 --format=count` @@ -68,7 +68,7 @@ Feature: Reset WordPress sidebars Scenario: Reset all sidebars Given a WP install - When I run `wp theme install twentysixteen --activate` + When I run `wp theme install twentytwelve --activate` Then STDOUT should not be empty When I run `wp widget add calendar sidebar-1 --title="Calendar"` @@ -103,7 +103,7 @@ Feature: Reset WordPress sidebars Scenario: Testing movement of widgets while reset Given a WP install - When I run `wp theme install twentysixteen --activate` + When I run `wp theme install twentytwelve --activate` Then STDOUT should not be empty When I run `wp widget add calendar sidebar-2 --title="Calendar"`