-
Notifications
You must be signed in to change notification settings - Fork 10
Adds new get and exists subcommands to the sidebar command
#70
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
base: main
Are you sure you want to change the base?
Adds new get and exists subcommands to the sidebar command
#70
Conversation
|
Hello! 👋 Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project. Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation. Here are some useful Composer commands to get you started:
To run a single Behat test, you can use the following command: # Run all tests in a single file
composer behat features/some-feature.feature
# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123You can find a list of all available Behat steps in our handbook. |
Summary of ChangesHello @iamsohilvahora, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request expands the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces get and exists subcommands for the sidebar command, along with corresponding Behat tests. The implementation is straightforward and follows existing patterns. I've identified a couple of opportunities for improvement: one to enhance the robustness of the new tests, and another to reduce code duplication by introducing a helper method. These changes will improve the maintainability and quality of the code.
features/sidebar.feature
Outdated
| And I run `wp sidebar get sidebar-1` | ||
| Then STDOUT should contain: | ||
| """ | ||
| sidebar-1 | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current test for the get subcommand only verifies that the output contains the sidebar ID. This is a weak assertion. To make the test more robust and less prone to breaking from unrelated output changes, I suggest using the --format=json option and asserting the structure and content of the JSON output. This ensures the command returns the expected data fields and values correctly.
And I run `wp sidebar get sidebar-1 --format=json`
Then STDOUT should be JSON containing:
"""
{"name":"Main Sidebar","id":"sidebar-1"}
"""
|
|
||
| /** | ||
| * 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 ) { | ||
| global $wp_registered_sidebars; | ||
|
|
||
| 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->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 ) { | ||
| global $wp_registered_sidebars; | ||
|
|
||
| Utils\wp_register_unused_sidebar(); | ||
|
|
||
| if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) { | ||
| WP_CLI::halt( 0 ); | ||
| } | ||
|
|
||
| WP_CLI::halt( 1 ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for loading registered sidebars (global $wp_registered_sidebars; and Utils\wp_register_unused_sidebar();) is duplicated across the list_, get, and exists methods. To improve maintainability and adhere to the DRY (Don't Repeat Yourself) principle, I recommend extracting this logic into a private helper method. This way, if the sidebar loading mechanism changes in the future, you'll only need to update it in one place.
private function get_registered_sidebars() {
global $wp_registered_sidebars;
Utils\wp_register_unused_sidebar();
return $wp_registered_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 ) {
$wp_registered_sidebars = $this->get_registered_sidebars();
$id = $args[0];
if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
WP_CLI::error( "Sidebar '{$id}' does not exist." );
}
$formatter = new Formatter( $assoc_args, $this->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 ) {
$wp_registered_sidebars = $this->get_registered_sidebars();
if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) {
WP_CLI::halt( 0 );
}
WP_CLI::halt( 1 );
}
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Why the new PR? Does this replace #69? |
|
Yes @swissspidy and actually, I got error where I confuse how to edit and push this file?
|
|
The Behat steps you used don't exist. You can find a list of all available Behat steps in our handbook. I recommend checking out the repository locslly to make edits & pushing changes. |
features/sidebar.feature
Outdated
| Then STDOUT should not be empty | ||
|
|
||
| When I run `wp theme install twentytwelve` | ||
| And I run `wp theme activate twentytwelve` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| And I run `wp theme activate twentytwelve` | |
| And I run `wp theme activate twentytwelve --force` |
There would be warning if theme already exists. Try "--force" argument.
|
Please check @ernilambar and there is also file called - src/Sidebar_Command.php |

This pull request adds new
getandexistssubcommands to the sidebar command.It includes Behat tests to ensure proper coverage and correct exit codes, and aligns behavior with existing wp-cli command patterns.