diff --git a/README.md b/README.md index c8b4cc0..8f3ece8 100644 --- a/README.md +++ b/README.md @@ -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 [--fields=] [--format=] +~~~ + +**OPTIONS** + + + The language code (slug) to get widgets for. Required. + + + ID for the corresponding sidebar. Required. + + [--fields=] + Limit the output to specific object fields. + + [--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. diff --git a/command.php b/command.php index ec1f3d1..fa6740c 100644 --- a/command.php +++ b/command.php @@ -44,6 +44,7 @@ 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 @@ 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 ); } diff --git a/composer.json b/composer.json index 28b5d5e..00f69db 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/composer.lock b/composer.lock index b9171d6..9044bac 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/src/Commands/Widget.php b/src/Commands/Widget.php new file mode 100644 index 0000000..832443f --- /dev/null +++ b/src/Commands/Widget.php @@ -0,0 +1,102 @@ + + * : The language code (slug) to get widgets for. Required. + * + * + * : ID for the corresponding sidebar. Required. + * + * [--fields=] + * : Limit the output to specific object fields. + * + * [--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 ); + } + +} + +}