Skip to content

Append new widgets to the bottom of a sidebar #52

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

Merged
merged 2 commits into from
Jul 26, 2021
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
2 changes: 1 addition & 1 deletion features/widget-reset.feature
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Feature: Reset WordPress sidebars
When I run `wp widget list sidebar-2 --format=ids`
Then STDOUT should contain:
"""
search-1 calendar-1
calendar-1 search-1
"""
When I run `wp widget list wp_inactive_widgets --format=ids`
Then STDOUT should be empty
Expand Down
38 changes: 19 additions & 19 deletions features/widget.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ Feature: Manage widgets in WordPress sidebar
When I run `wp widget list sidebar-1 --fields=name,id,position`
Then STDOUT should be a table containing rows:
| name | id | position |
| text | text-2 | 1 |
| search | search-1 | 2 |
| text | text-1 | 1 |
| archives | archives-1 | 2 |
| calendar | calendar-1 | 3 |
| archives | archives-1 | 4 |
| text | text-1 | 5 |
| search | search-1 | 4 |
| text | text-2 | 5 |

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

When I run `wp widget list sidebar-1 --fields=name,id,position`
Then STDOUT should be a table containing rows:
| name | id | position |
| text | text-2 | 1 |
| archives | archives-1 | 2 |
| search | search-1 | 3 |
| text | text-1 | 1 |
| search | search-1 | 2 |
| archives | archives-1 | 3 |
| calendar | calendar-1 | 4 |
| text | text-1 | 5 |
| text | text-2 | 5 |

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

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

When I run `wp widget list wp_inactive_widgets --fields=name,id,position`
Then STDOUT should be a table containing rows:
| name | id | position |
| calendar | calendar-1 | 1 |
| text | text-2 | 2 |
| text | text-1 | 1 |
| calendar | calendar-1 | 2 |

When I run `wp widget delete archives-1 text-2`
When I run `wp widget delete archives-1 text-1`
Then STDOUT should be:
"""
Success: Deleted 2 of 2 widgets.
Expand All @@ -73,7 +73,7 @@ Feature: Manage widgets in WordPress sidebar
Then STDOUT should be a table containing rows:
| name | id | position |
| search | search-1 | 1 |
| text | text-1 | 2 |
| text | text-2 | 2 |

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

When I run `wp widget list sidebar-1 --format=ids`
Then STDOUT should be:
"""
search-1 archives-1 text-1
search-1 archives-1 text-2
"""

When I run `wp widget list sidebar-1 --fields=name,position,options`
Expand Down
23 changes: 19 additions & 4 deletions src/Widget_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ public function list_( $args, $assoc_args ) {
* @subcommand add
*/
public function add( $args, $assoc_args ) {

list( $name, $sidebar_id ) = $args;
$position = Utils\get_flag_value( $args, 2, 1 ) - 1;

$this->validate_sidebar( $sidebar_id );

$position = count( $args ) > 2
? $args[2] - 1
: count( $this->get_sidebar_widgets( $sidebar_id ) );

$widget = $this->get_widget_obj( $name );
if ( false === $widget ) {
WP_CLI::error( 'Invalid widget type.' );
Expand Down Expand Up @@ -300,7 +303,13 @@ public function deactivate( $args, $assoc_args ) {
continue;
}

$this->move_sidebar_widget( $widget_id, $sidebar_id, 'wp_inactive_widgets', $sidebar_index, 0 );
$this->move_sidebar_widget(
$widget_id,
$sidebar_id,
'wp_inactive_widgets',
$sidebar_index,
count( $this->get_sidebar_widgets( 'wp_inactive_widgets' ) )
);

$count++;

Expand Down Expand Up @@ -427,7 +436,13 @@ public function reset( $args, $assoc_args ) {
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 );
$this->move_sidebar_widget(
$widget_id,
$new_sidebar_id,
'wp_inactive_widgets',
$sidebar_index,
count( $this->get_sidebar_widgets( 'wp_inactive_widgets' ) )
);
}
WP_CLI::log( sprintf( "Sidebar '%s' reset.", $sidebar_id ) );
$count++;
Expand Down