Skip to content

Commit

Permalink
Merge pull request #3077 from ernilambar/introduce-reset-for-widget
Browse files Browse the repository at this point in the history
Introduce reset subcommand for widget
  • Loading branch information
danielbachhuber committed Jun 30, 2016
2 parents 982fd54 + d024247 commit 2241b8a
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
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
"""
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 ) ) {
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

0 comments on commit 2241b8a

Please sign in to comment.