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

Introduce reset subcommand for widget #3077

Merged
merged 7 commits into from Jun 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
129 changes: 129 additions & 0 deletions features/widget-reset.feature
@@ -0,0 +1,129 @@
Feature: Reset WordPress sidebars

Scenario: Reset sidebar
Given a WP install

When I run `wp theme install twentytwelve --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 '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 twentytwelve --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
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we verify the widgets were added to the inactive sidebar?

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

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"`
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
"""
75 changes: 75 additions & 0 deletions php/commands/widget.php
Expand Up @@ -304,6 +304,81 @@ public function delete( $args, $assoc_args ) {
WP_CLI::success( "Widget(s) removed from sidebar." );
}

/**
* Reset sidebar.
*
* Removes all widgets from the sidebar and places them in Inactive Widgets.
*
* [<sidebar-id>...]
* : 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.
*
* # 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 ) {

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 ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you get this error, if we're already checking for empty( $args ) above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When wp widget reset --all and there is no any registered sidebar in the theme.

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( "Sidebar '%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
*
Expand Down