Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions features/widget-reset.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,53 @@ Feature: Reset WordPress sidebars
"""
Warning: Sidebar 'sidebar-1' is already empty.
"""
And STDOUT should be:
"""
Success: Sidebar already reset.
"""
And the return code should be 0

When I try `wp widget reset non-existing-sidebar-id`
Then STDERR should be:
"""
Warning: Invalid sidebar: non-existing-sidebar-id
Error: No sidebars reset.
"""
And the return code should be 1

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`

When 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`

When 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`
When I try `wp widget reset sidebar-1 sidebar-2 non-existing-sidebar-id`
Then STDERR should be:
"""
Warning: Invalid sidebar: non-existing-sidebar-id
Error: Only reset 2 of 3 sidebars.
"""
And the return code should be 1

When I run `wp widget list sidebar-1 --format=count`
Then STDOUT should be:
"""
0
"""
And I run `wp widget list sidebar-2 --format=count`

When I run `wp widget list sidebar-2 --format=count`
Then STDOUT should be:
"""
0
Expand All @@ -79,7 +96,16 @@ Feature: Reset WordPress sidebars
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:
"""
Sidebar 'sidebar-1' reset.
Sidebar 'sidebar-2' reset.
Sidebar 'sidebar-3' reset.
Success: Reset 3 of 3 sidebars.
"""
And the return code should be 0

When I run `wp widget list sidebar-1 --format=count`
Then STDOUT should be:
"""
0
Expand Down
97 changes: 92 additions & 5 deletions features/widget.feature
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Feature: Manage widgets in WordPress sidebar

Scenario: Widget CRUD
Background:
Given a WP install

When I run `wp theme install p2 --activate`
Then STDOUT should not be empty

Scenario: Widget CRUD
When I run `wp widget list sidebar-1 --fields=name,id,position`
Then STDOUT should be a table containing rows:
| name | id | position |
Expand Down Expand Up @@ -34,9 +34,11 @@ Feature: Manage widgets in WordPress sidebar

When I run `wp widget deactivate meta-2`
Then STDOUT should be:
"""
Success: 1 widget deactivated.
"""
"""
Success: 1 widget deactivated.
"""
And STDERR should be empty
And the return code should be 0

When I run `wp widget list sidebar-1 --fields=name,id,position`
Then STDOUT should be a table containing rows:
Expand All @@ -57,6 +59,8 @@ Feature: Manage widgets in WordPress sidebar
"""
Success: 2 widgets removed from sidebar.
"""
And STDERR should be empty
And the return code should be 0

When I run `wp widget list sidebar-1 --fields=name,id,position`
Then STDOUT should be a table containing rows:
Expand Down Expand Up @@ -87,3 +91,86 @@ Feature: Manage widgets in WordPress sidebar
Then STDOUT should be a table containing rows:
| name | position | options |
| calendar | 2 | {"title":"Calendar"} |

Scenario: Validate sidebar widgets
When I try `wp widget update calendar-999`
Then STDERR should be:
"""
Error: Widget doesn't exist.
"""
And the return code should be 1

When I try `wp widget move calendar-999`
Then STDERR should be:
"""
Error: Widget doesn't exist.
"""
And the return code should be 1

Scenario: Return code is 0 when all widgets exist, deactivation
When I run `wp widget deactivate recent-posts-2`
Then STDOUT should be:
"""
Success: 1 widget deactivated.
"""
And STDERR should be empty
And the return code should be 0

When I run `wp widget deactivate search-2 archives-2`
Then STDOUT should be:
"""
Success: 2 widgets deactivated.
"""
And STDERR should be empty
And the return code should be 0

Scenario: Return code is 0 when all widgets exist, deletion
When I run `wp widget delete recent-posts-2`
Then STDOUT should be:
"""
Success: 1 widget removed from sidebar.
"""
And STDERR should be empty
And the return code should be 0

When I run `wp widget delete search-2 archives-2`
Then STDOUT should be:
"""
Success: 2 widgets removed from sidebar.
"""
And STDERR should be empty
And the return code should be 0

Scenario: Return code is 1 when 1 or more widgets doesn't exist, deactivation
When I try `wp widget deactivate calendar-999`
Then STDERR should be:
"""
Warning: Widget 'calendar-999' doesn't exist.
Error: No widgets deactivated.
"""
And the return code should be 1

When I try `wp widget deactivate recent-posts-2 calendar-999`
Then STDERR should be:
"""
Warning: Widget 'calendar-999' doesn't exist.
Error: Only deactivated 1 of 2 widgets.
"""
And the return code should be 1

Scenario: Return code is 1 when 1 or more widgets doesn't exist, deletion
When I try `wp widget delete calendar-999`
Then STDERR should be:
"""
Warning: Widget 'calendar-999' doesn't exist.
Error: No widgets removed from sidebar.
"""
And the return code should be 1

When I try `wp widget delete recent-posts-2 calendar-999`
Then STDERR should be:
"""
Warning: Widget 'calendar-999' doesn't exist.
Error: Only removed 1 of 2 widgets from sidebar.
"""
And the return code should be 1
78 changes: 62 additions & 16 deletions php/commands/widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ public function add( $args, $assoc_args ) {
public function update( $args, $assoc_args ) {

list( $widget_id ) = $args;
$this->validate_sidebar_widget( $widget_id );
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::error( "Widget doesn't exist." );
}

if ( empty( $assoc_args ) ) {
WP_CLI::error( "No options specified to update." );
Expand Down Expand Up @@ -231,7 +233,9 @@ public function update( $args, $assoc_args ) {
public function move( $args, $assoc_args ) {

list( $widget_id ) = $args;
$this->validate_sidebar_widget( $widget_id );
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::error( "Widget doesn't exist." );
}

if ( empty( $assoc_args['position'] ) && empty( $assoc_args['sidebar-id'] ) ) {
WP_CLI::error( "A new position or new sidebar must be specified." );
Expand Down Expand Up @@ -275,10 +279,14 @@ public function move( $args, $assoc_args ) {
*/
public function deactivate( $args, $assoc_args ) {

$count = 0;
$count = $errors = 0;

foreach( $args as $widget_id ) {
$this->validate_sidebar_widget( $widget_id );
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::warning( "Widget '{$widget_id}' doesn't exist." );
$errors++;
continue;
}

list( $name, $option_index, $sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id );
if ( 'wp_inactive_widgets' == $sidebar_id ) {
Expand All @@ -292,8 +300,17 @@ public function deactivate( $args, $assoc_args ) {

}

$success_message = ( 1 === $count ) ? '%d widget deactivated.' : '%d widgets deactivated.';
WP_CLI::success( sprintf( $success_message, $count ) );
if ( $errors ) {
if ( $count ) {
$widget_count = count( $args );
WP_CLI::error( "Only deactivated {$count} of {$widget_count} widgets." );
} else {
WP_CLI::error( "No widgets deactivated." );
}
} else {
$success_message = ( 1 === $count ) ? '%d widget deactivated.' : '%d widgets deactivated.';
WP_CLI::success( sprintf( $success_message, $count ) );
}
}

/**
Expand All @@ -314,10 +331,14 @@ public function deactivate( $args, $assoc_args ) {
*/
public function delete( $args, $assoc_args ) {

$count = 0;
$count = $errors = 0;

foreach( $args as $widget_id ) {
$this->validate_sidebar_widget( $widget_id );
if ( ! $this->validate_sidebar_widget( $widget_id ) ) {
WP_CLI::warning( "Widget '{$widget_id}' doesn't exist." );
$errors++;
continue;
}

// Remove the widget's settings.
list( $name, $option_index, $sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id );
Expand All @@ -334,8 +355,17 @@ public function delete( $args, $assoc_args ) {
$count++;
}

$success_message = ( 1 === $count ) ? '%d widget removed from sidebar.' : '%d widgets removed from sidebar.';
WP_CLI::success( sprintf( $success_message, $count ) );
if ( $errors ) {
if ( $count ) {
$widget_count = count( $args );
WP_CLI::error( "Only removed {$count} of {$widget_count} widgets from sidebar." );
} else {
WP_CLI::error( "No widgets removed from sidebar." );
}
} else {
$success_message = ( 1 === $count ) ? '%d widget removed from sidebar.' : '%d widgets removed from sidebar.';
WP_CLI::success( sprintf( $success_message, $count ) );
}
}

/**
Expand Down Expand Up @@ -394,9 +424,11 @@ public function reset( $args, $assoc_args ) {
WP_CLI::error( 'No sidebar registered.' );
}

$count = $errors = 0;
foreach ( $args as $sidebar_id ) {
if ( ! array_key_exists( $sidebar_id, $wp_registered_sidebars ) ) {
WP_CLI::warning( sprintf( 'Invalid sidebar: %s', $sidebar_id ) );
$errors++;
continue;
}

Expand All @@ -410,9 +442,27 @@ public function reset( $args, $assoc_args ) {
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 ) );
WP_CLI::log( sprintf( "Sidebar '%s' reset.", $sidebar_id ) );
$count++;
}
}

$sidebar_count = count( $args );
if ( $errors ) {
if ( $count ) {
WP_CLI::error( "Only reset {$count} of {$sidebar_count} sidebars." );
} else {
WP_CLI::error( 'No sidebars reset.' );
}
} else {
if ( $count ) {
WP_CLI::success( "Reset {$count} of {$sidebar_count} sidebars." );
} else {
$message = $sidebar_count > 1 ? 'Sidebars' : 'Sidebar';
WP_CLI::success( "{$message} already reset." );
}
}

}

/**
Expand Down Expand Up @@ -448,11 +498,7 @@ private function validate_sidebar_widget( $widget_id ) {
}

}

if ( false === $widget_exists ) {
WP_CLI::error( "Specified widget isn't present on sidebar." );
}

return $widget_exists;
}

/**
Expand Down