Skip to content
Closed
Changes from 2 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
189 changes: 167 additions & 22 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,9 +38,9 @@ class Sidebar_Command extends WP_CLI_Command {
* - table
* - csv
* - json
* - yaml
* - ids
* - count
* - yaml
* ---
*
Comment thread
iamsohilvahora marked this conversation as resolved.
* ## AVAILABLE FIELDS
Expand All @@ -63,25 +61,172 @@ class Sidebar_Command extends WP_CLI_Command {
*
* ## EXAMPLES
*
* $ wp sidebar list --fields=name,id --format=csv
* name,id
* "Widget Area",sidebar-1
* "Inactive Widgets",wp_inactive_widgets
* $ 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;

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

$sidebars = $wp_registered_sidebars;

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

$formatter = new Formatter( $assoc_args, $this->fields );
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
* ---
*
* ## 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.
if ( function_exists( 'wp_register_unused_sidebar' ) ) {
Utils\wp_register_unused_sidebar();
}

$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
*
* <id>
* : The sidebar ID.
*
* ## EXAMPLES
*
* $ wp sidebar exists sidebar-1
* $ wp sidebar exists wp_inactive_widgets && 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 ( function_exists( 'wp_register_unused_sidebar' ) ) {
Utils\wp_register_unused_sidebar();
}

if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) {
WP_CLI::halt( 0 );
}

WP_CLI::halt( 1 );
}

/**
* List widgets assigned to a sidebar.
*
* ## OPTIONS
*
* <id>
* : The sidebar ID.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - ids
* ---
*
* ## EXAMPLES
*
* $ wp sidebar widgets sidebar-1
* $ wp sidebar widgets wp_inactive_widgets --format=ids
*
* @when after_wp_load
*/
public function widgets( $args, $assoc_args ) {
global $wp_registered_sidebars;

Comment thread
iamsohilvahora marked this conversation as resolved.
Outdated
if ( function_exists( 'wp_register_unused_sidebar' ) ) {
Utils\wp_register_unused_sidebar();
}

$id = $args[0];

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

$sidebars_widgets = wp_get_sidebars_widgets();
$widget_ids = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : [];

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

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

$items = array_map(
function ( $widget_id ) {
return [ 'id' => $widget_id ];
},
$widget_ids
);

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