Skip to content

Commit 5ffe7bc

Browse files
authored
Merge pull request #52 from wp-cli/fix/43-add-widgets-to-end-of-sidebar
2 parents 52b3829 + 344e29c commit 5ffe7bc

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
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: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ Feature: Manage widgets in WordPress sidebar
1919
When I run `wp widget list sidebar-1 --fields=name,id,position`
2020
Then STDOUT should be a table containing rows:
2121
| name | id | position |
22-
| text | text-2 | 1 |
23-
| search | search-1 | 2 |
22+
| text | text-1 | 1 |
23+
| archives | archives-1 | 2 |
2424
| calendar | calendar-1 | 3 |
25-
| archives | archives-1 | 4 |
26-
| text | text-1 | 5 |
25+
| search | search-1 | 4 |
26+
| text | text-2 | 5 |
2727

28-
When I run `wp widget move archives-1 --position=2`
28+
When I run `wp widget move search-1 --position=2`
2929
Then STDOUT should not be empty
3030

3131
When I run `wp widget list sidebar-1 --fields=name,id,position`
3232
Then STDOUT should be a table containing rows:
3333
| name | id | position |
34-
| text | text-2 | 1 |
35-
| archives | archives-1 | 2 |
36-
| search | search-1 | 3 |
34+
| text | text-1 | 1 |
35+
| search | search-1 | 2 |
36+
| archives | archives-1 | 3 |
3737
| calendar | calendar-1 | 4 |
38-
| text | text-1 | 5 |
38+
| text | text-2 | 5 |
3939

40-
When I run `wp widget move text-2 --sidebar-id=wp_inactive_widgets`
40+
When I run `wp widget move text-1 --sidebar-id=wp_inactive_widgets`
4141
Then STDOUT should not be empty
4242

4343
When I run `wp widget deactivate calendar-1`
@@ -51,17 +51,17 @@ Feature: Manage widgets in WordPress sidebar
5151
When I run `wp widget list sidebar-1 --fields=name,id,position`
5252
Then STDOUT should be a table containing rows:
5353
| name | id | position |
54-
| archives | archives-1 | 1 |
55-
| search | search-1 | 2 |
56-
| text | text-1 | 3 |
54+
| search | search-1 | 1 |
55+
| archives | archives-1 | 2 |
56+
| text | text-2 | 3 |
5757

5858
When I run `wp widget list wp_inactive_widgets --fields=name,id,position`
5959
Then STDOUT should be a table containing rows:
6060
| name | id | position |
61-
| calendar | calendar-1 | 1 |
62-
| text | text-2 | 2 |
61+
| text | text-1 | 1 |
62+
| 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.
@@ -73,7 +73,7 @@ Feature: Manage widgets in WordPress sidebar
7373
Then STDOUT should be a table containing rows:
7474
| name | id | position |
7575
| search | search-1 | 1 |
76-
| text | text-1 | 2 |
76+
| text | text-2 | 2 |
7777

7878
When I run `wp widget add archives sidebar-1 2 --title="Archives"`
7979
Then STDOUT should not be empty
@@ -83,12 +83,12 @@ Feature: Manage widgets in WordPress sidebar
8383
| name | id | position |
8484
| search | search-1 | 1 |
8585
| archives | archives-1 | 2 |
86-
| text | text-1 | 3 |
86+
| text | text-2 | 3 |
8787

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)