Skip to content

Improve command suggestions for taxonomies and post types #6059

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

Merged
merged 5 commits into from
Mar 6, 2025

Conversation

karthick-murugan
Copy link
Contributor

@karthick-murugan karthick-murugan commented Mar 4, 2025

Fixes #5624

Summary

This PR improves user experience by suggesting appropriate WP-CLI commands when a user attempts to run wp or wp <post_type>, which do not exist. Instead of failing with an error, WP-CLI will now provide a helpful "Did you mean" message.

Changes

  • Added logic to detect when a user enters wp or wp <post_type> .
  • Implemented a suggestion system that recommends:
    wp term <command> <taxonomy> for taxonomies.
    wp post <command> --post_type=<post_type> for post types.
  • If the exact command format cannot be inferred, a general suggestion for wp term or wp post is provided.

Testing Instructions

  1. Run wp category list (or any taxonomy name instead of category).
  • Expected output: Error: Did you mean 'wp term ' ?
  1. Run wp product create (where product is a post type).
  • Expected output: Error: Did you mean 'wp post ' ?
  1. Run a valid wp term or wp post command to ensure existing functionality remains unaffected.

This PR enhances usability by helping users discover the correct WP-CLI commands efficiently.

Video

REC-20250304201920.mp4

@karthick-murugan karthick-murugan requested a review from a team as a code owner March 4, 2025 14:51
@codecov-commenter
Copy link

codecov-commenter commented Mar 4, 2025

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
php/WP_CLI/Runner.php 0.00% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy
Copy link
Member

Thanks for your PR! I left some early feedback that should help enhance this further. As you can see, some tests are currently failing otherwise.

Speaking of, this would definitely benefit from some tests soo. Would you be up for adding some? Perhaps in runner.feature


// Suggest appropriate commands for taxonomy or post type.
if ( $this->is_taxonomy( $cmd_path[0] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the extra methods, we can just use taxonomy_exists() and post_type_exists() provided by WordPress.

Copy link
Contributor Author

@karthick-murugan karthick-murugan Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @swissspidy for the feedback. Im on it and when updated the method as you have mentioned I am always getting a fatal error of undefined function.

if ( taxonomy_exists( $cmd_path[0] ) ) {
      return sprintf( "Did you mean 'wp term %s %s'?", $cmd_path[0], isset( $sub_path ) ? $sub_path : 'list' );
} elseif ( post_type_exists( $cmd_path[0] ) ) {
      return sprintf( "Did you mean 'wp post <command> --post_type=%s'?", $cmd_path[0] );
}

Please advice. Here is the video

REC-20250305190645.mp4

Also please provide some simple steps to fix the Test Errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using \taxonomy_exists() could solve it, may be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@swissspidy - Any ideas for the above mentioned error in the video when using taxonomy_exists ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a thorough look at the PR shortly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So turns out load_wordpress() ends up calling find_command_to_run() again, hence the function not existing.

But because WP-CLI already tries to load WordPress anyway if it's detected, I realized the extra call isn't really needed.

So I now simplified the code a little bit and it should be good to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @swissspidy you are really helpful to learn the contribution easily. Love it.

@karthick-murugan
Copy link
Contributor Author

@swissspidy - Thanks for the updated changes. I understand it now.

@swissspidy swissspidy changed the title Enhance Command Suggestions for Non-Existent Taxonomy and Post Type Commands Improve command suggestions for taxonomies and post types Mar 6, 2025
@swissspidy swissspidy added this to the 2.12.0 milestone Mar 6, 2025
@swissspidy swissspidy merged commit 3385cc2 into wp-cli:main Mar 6, 2025
48 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest an appropriate command when wp <taxonomy> or wp <post_type> is called
4 participants