Commit 8bdd8c6c authored by Peter J. Herrel's avatar Peter J. Herrel
Browse files

`wp pll widget list` subcommand

cf. #82
parent 22cc7a56
......@@ -1431,6 +1431,74 @@ Errors if the term doesn't exist, or there was a problem in deleting it.
Deleted post_tag 58.
Success: Deleted 3 of 3 terms.
### wp pll widget
Manage localized sidebar widgets.
~~~
wp pll widget
~~~
### wp pll widget list
List localized widgets associated with a sidebar.
~~~
wp pll widget list <language-code> <sidebar-id> [--fields=<fields>] [--format=<format>]
~~~
**OPTIONS**
<language-code>
The language code (slug) to get widgets for. Required.
<sidebar-id>
ID for the corresponding sidebar. Required.
[--fields=<fields>]
Limit the output to specific object fields.
[--format=<format>]
Render output in a particular format.
---
default: table
options:
- table
- csv
- ids
- json
- count
- yaml
---
**AVAILABLE FIELDS**
These fields will be displayed by default for each widget:
* name
* id
* position
* options
There are no optionally available fields.
**EXAMPLES**
$ wp pll widget list nl sidebar-1
+------+--------+----------+-------------------------------------------------------------------+
| name | id | position | options |
+------+--------+----------+-------------------------------------------------------------------+
| text | text-2 | 2 | {"title":"test","text":"test","filter":"content","pll_lang":"nl"} |
+------+--------+----------+-------------------------------------------------------------------+
## Contributing
We appreciate you taking the initiative to contribute to this project.
......
......@@ -44,6 +44,7 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) {
require __DIR__ . '/src/Commands/Plugin.php';
require __DIR__ . '/src/Commands/Menu.php';
require __DIR__ . '/src/Commands/String.php';
require __DIR__ . '/src/Commands/Widget.php';
WP_CLI::add_hook( 'before_wp_load', function() {
......@@ -83,5 +84,6 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'pll string', Polylang_CLI\Commands\StringCommand::class );
WP_CLI::add_command( 'pll taxonomy', Polylang_CLI\Commands\TaxonomyCommand::class );
WP_CLI::add_command( 'pll term', Polylang_CLI\Commands\TermCommand::class );
WP_CLI::add_command( 'pll widget', Polylang_CLI\Commands\WidgetCommand::class );
}
......@@ -76,7 +76,9 @@
"pll term",
"pll term generate",
"pll term get",
"pll term delete"
"pll term delete",
"pll widget",
"pll widget list"
],
"readme": {
"sections": [
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "419eb8e23f530b373d6bd16623771727",
"content-hash": "127572b6a6bb976909195975e2c8de83",
"packages": [
{
"name": "composer/ca-bundle",
......
<?php
namespace Polylang_CLI\Commands;
if ( ! class_exists( 'Polylang_CLI\Commands\WidgetCommand' ) ) {
/**
* Manage localized sidebar widgets.
*
* @package Polylang_CLI
*/
class WidgetCommand extends BaseCommand {
/**
* List localized widgets associated with a sidebar.
*
* ## OPTIONS
*
* <language-code>
* : The language code (slug) to get widgets for. Required.
*
* <sidebar-id>
* : ID for the corresponding sidebar. Required.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - ids
* - json
* - count
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each widget:
*
* * name
* * id
* * position
* * options
*
* There are no optionally available fields.
*
* ## EXAMPLES
*
* $ wp pll widget list nl sidebar-1
* +------+--------+----------+-------------------------------------------------------------------+
* | name | id | position | options |
* +------+--------+----------+-------------------------------------------------------------------+
* | text | text-2 | 2 | {"title":"test","text":"test","filter":"content","pll_lang":"nl"} |
* +------+--------+----------+-------------------------------------------------------------------+
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
list( $language_slug, $sidebar_id ) = $args;
# get widget fields
$properties = new \ReflectionClass( new \Widget_Command() );
$fields = $properties->getDefaultProperties()['fields'];
# validate sidebar
$validate = new \ReflectionMethod( 'Widget_Command', 'validate_sidebar' );
$validate->setAccessible( true );
$validate->invokeArgs( new \Widget_Command(), array( $sidebar_id ) );
# get sidebar widgets
$validate = new \ReflectionMethod( 'Widget_Command', 'get_sidebar_widgets' );
$validate->setAccessible( true );
$output_widgets = $validate->invokeArgs( new \Widget_Command(), array( $sidebar_id ) );
# filter widgets by language
$output_widgets_filtered = array();
foreach ( $output_widgets as $obj ) {
if ( isset( $obj->options['pll_lang'] ) && $obj->options['pll_lang'] === $language_slug ) {
$output_widgets_filtered[] = $obj;
}
}
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
$output_widgets = wp_list_pluck( $output_widgets_filtered, 'id' );
}
$formatter = $this->cli->formatter( $assoc_args, $fields );
$formatter->display_items( $output_widgets_filtered );
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment