Skip to content
Closed
Changes from 1 commit
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
187 changes: 152 additions & 35 deletions src/Sidebar_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
use WP_CLI\Formatter;

/**
* Lists registered sidebars.
* Manage registered sidebars.
*
* A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/) is any widgetized area of your theme.
*
* ## EXAMPLES
*
* # List sidebars
* $ wp sidebar list --fields=name,id --format=csv
* name,id
* "Widget Area",sidebar-1
* "Inactive Widgets",wp_inactive_widgets
* A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/)
* is any widgetized area of your theme.
Comment thread
swissspidy marked this conversation as resolved.
Outdated
*/
class Sidebar_Command extends WP_CLI_Command {

private $fields = [
/**
* Default fields for sidebar output.
*
* @var array
*/
private $default_fields = [
'name',
'id',
'description',
Expand All @@ -40,48 +38,167 @@ class Sidebar_Command extends WP_CLI_Command {
* - table
* - csv
* - json
* - yaml
* - ids
* - count
* ---
*
Comment thread
iamsohilvahora marked this conversation as resolved.
* ## EXAMPLES
*
* $ wp sidebar list
* $ wp sidebar list --fields=name,id
* $ wp sidebar list --format=ids
* $ wp sidebar list --format=count
*
* @subcommand list
* @when after_wp_load
*/
public function list_( $args, $assoc_args ) {
Comment thread
swissspidy marked this conversation as resolved.
global $wp_registered_sidebars;

if ( function_exists( 'wp_register_unused_sidebar' ) ) {
Utils\wp_register_unused_sidebar();
}

$sidebars = $wp_registered_sidebars;

if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
WP_CLI::line( implode( ' ', wp_list_pluck( $sidebars, 'id' ) ) );
return;
}

if ( isset( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
WP_CLI::line( count( $sidebars ) );
return;
}

$formatter = new Formatter( $assoc_args, $this->default_fields );
$formatter->display_items( $sidebars );
}

/**
* Get details about a specific sidebar.
*
* ## OPTIONS
*
* <id>
* : The sidebar ID.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - json
* - yaml
* ---
*
* ## AVAILABLE FIELDS
* ## EXAMPLES
*
* $ wp sidebar get sidebar-1
* $ wp sidebar get wp_inactive_widgets --format=json
*
* @when after_wp_load
*/
public function get( $args, $assoc_args ) {
Comment thread
swissspidy marked this conversation as resolved.
global $wp_registered_sidebars;

Comment thread
iamsohilvahora marked this conversation as resolved.
$id = $args[0];

if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
WP_CLI::error( "Sidebar '{$id}' does not exist." );
}

$formatter = new Formatter( $assoc_args, $this->default_fields );
$formatter->display_item( $wp_registered_sidebars[ $id ] );
}

/**
* Check if a sidebar exists.
*
* ## OPTIONS
*
* These fields will be displayed by default for each sidebar:
* <id>
* : The sidebar ID.
*
* ## EXAMPLES
*
* $ wp sidebar exists sidebar-1
* $ wp sidebar exists foo && echo "exists"
*
* @when after_wp_load
*/
public function exists( $args ) {
Comment thread
swissspidy marked this conversation as resolved.
global $wp_registered_sidebars;

Comment thread
iamsohilvahora marked this conversation as resolved.
if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) {
WP_CLI::halt( 0 );
}

WP_CLI::halt( 1 );
}

/**
* List widgets assigned to a sidebar.
*
* * name
* * id
* * description
* ## OPTIONS
*
* These fields are optionally available:
* <id>
* : The sidebar ID.
*
* * class
* * before_widget
* * after_widget
* * before_title
* * after_title
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - ids
* ---
*
* ## EXAMPLES
*
* $ wp sidebar list --fields=name,id --format=csv
* name,id
* "Widget Area",sidebar-1
* "Inactive Widgets",wp_inactive_widgets
* $ wp sidebar widgets sidebar-1
* $ wp sidebar widgets wp_inactive_widgets --format=ids
*
* @subcommand list
* @when after_wp_load
*/
public function list_( $args, $assoc_args ) {
public function widgets( $args, $assoc_args ) {
global $wp_registered_sidebars;

Comment thread
iamsohilvahora marked this conversation as resolved.
Outdated
Utils\wp_register_unused_sidebar();
$id = $args[0];

if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
$sidebars = wp_list_pluck( $wp_registered_sidebars, 'id' );
} else {
$sidebars = $wp_registered_sidebars;
if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
WP_CLI::error( "Sidebar '{$id}' does not exist." );
}

$formatter = new Formatter( $assoc_args, $this->fields );
$formatter->display_items( $sidebars );
$sidebars_widgets = wp_get_sidebars_widgets();
$widgets = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : [];

if ( empty( $widgets ) ) {
WP_CLI::warning( "No widgets found in sidebar '{$id}'." );
return;
}

if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
WP_CLI::line( implode( ' ', $widgets ) );
return;
}

$items = array_map(
function ( $widget_id ) {
return [ 'id' => $widget_id ];
},
$widgets
);
Comment thread
iamsohilvahora marked this conversation as resolved.
Outdated

$formatter = new Formatter( $assoc_args, [ 'id' ] );
$formatter->display_items( $items );
}
}
Loading