Skip to content

Commit 344e29c

Browse files
committed
Adapt ordering of widget movement/addition
1 parent dd8c286 commit 344e29c

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

features/widget-reset.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Feature: Reset WordPress sidebars
138138
When I run `wp widget list sidebar-2 --format=ids`
139139
Then STDOUT should contain:
140140
"""
141-
search-1 calendar-1
141+
calendar-1 search-1
142142
"""
143143
When I run `wp widget list wp_inactive_widgets --format=ids`
144144
Then STDOUT should be empty

features/widget.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Feature: Manage widgets in WordPress sidebar
6161
| text | text-1 | 1 |
6262
| calendar | calendar-1 | 2 |
6363

64-
When I run `wp widget delete archives-1 text-2`
64+
When I run `wp widget delete archives-1 text-1`
6565
Then STDOUT should be:
6666
"""
6767
Success: Deleted 2 of 2 widgets.
@@ -88,7 +88,7 @@ Feature: Manage widgets in WordPress sidebar
8888
When I run `wp widget list sidebar-1 --format=ids`
8989
Then STDOUT should be:
9090
"""
91-
search-1 archives-1 text-1
91+
search-1 archives-1 text-2
9292
"""
9393

9494
When I run `wp widget list sidebar-1 --fields=name,position,options`

src/Widget_Command.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,14 @@ public function list_( $args, $assoc_args ) {
130130
* @subcommand add
131131
*/
132132
public function add( $args, $assoc_args ) {
133-
134133
list( $name, $sidebar_id ) = $args;
135-
$position = Utils\get_flag_value( $args, 2, 1 ) - 1;
134+
136135
$this->validate_sidebar( $sidebar_id );
137136

137+
$position = count( $args ) > 2
138+
? $args[2] - 1
139+
: count( $this->get_sidebar_widgets( $sidebar_id ) );
140+
138141
$widget = $this->get_widget_obj( $name );
139142
if ( false === $widget ) {
140143
WP_CLI::error( 'Invalid widget type.' );
@@ -300,7 +303,13 @@ public function deactivate( $args, $assoc_args ) {
300303
continue;
301304
}
302305

303-
$this->move_sidebar_widget( $widget_id, $sidebar_id, 'wp_inactive_widgets', $sidebar_index, 0 );
306+
$this->move_sidebar_widget(
307+
$widget_id,
308+
$sidebar_id,
309+
'wp_inactive_widgets',
310+
$sidebar_index,
311+
count( $this->get_sidebar_widgets( 'wp_inactive_widgets' ) )
312+
);
304313

305314
$count++;
306315

@@ -427,7 +436,13 @@ public function reset( $args, $assoc_args ) {
427436
foreach ( $widgets as $widget ) {
428437
$widget_id = $widget->id;
429438
list( $name, $option_index, $new_sidebar_id, $sidebar_index ) = $this->get_widget_data( $widget_id );
430-
$this->move_sidebar_widget( $widget_id, $new_sidebar_id, 'wp_inactive_widgets', $sidebar_index, 0 );
439+
$this->move_sidebar_widget(
440+
$widget_id,
441+
$new_sidebar_id,
442+
'wp_inactive_widgets',
443+
$sidebar_index,
444+
count( $this->get_sidebar_widgets( 'wp_inactive_widgets' ) )
445+
);
431446
}
432447
WP_CLI::log( sprintf( "Sidebar '%s' reset.", $sidebar_id ) );
433448
$count++;

0 commit comments

Comments
 (0)