Skip to content
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

wp pll widget list subcommand #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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 );

}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions src/Commands/Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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 );
}

}

}