-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Categories and Checks CLI commands
- Loading branch information
1 parent
0101c91
commit 7fbb19b
Showing
5 changed files
with
482 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
<?php | ||
/** | ||
* Class WordPress\Plugin_Check\CLI\Plugin_Check_Categories_Command | ||
* | ||
* @package plugin-check | ||
*/ | ||
|
||
namespace WordPress\Plugin_Check\CLI; | ||
|
||
use WordPress\Plugin_Check\Checker\Check_Categories; | ||
use WordPress\Plugin_Check\Plugin_Context; | ||
use WP_CLI; | ||
|
||
/** | ||
* Plugin check categories command. | ||
*/ | ||
final class Plugin_Check_Categories_Command { | ||
|
||
/** | ||
* Plugin context. | ||
* | ||
* @since n.e.x.t | ||
* @var Plugin_Context | ||
*/ | ||
protected $plugin_context; | ||
|
||
/** | ||
* Output format type. | ||
* | ||
* @since n.e.x.t | ||
* @var string[] | ||
*/ | ||
protected $output_formats = array( | ||
'table', | ||
'csv', | ||
'json', | ||
); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param Plugin_Context $plugin_context Plugin context. | ||
*/ | ||
public function __construct( Plugin_Context $plugin_context ) { | ||
$this->plugin_context = $plugin_context; | ||
} | ||
|
||
/** | ||
* Runs plugin check-categories. | ||
* | ||
* ## OPTIONS | ||
* | ||
* [--fields=<fields>] | ||
* : Limit displayed results to a subset of fields provided. | ||
* | ||
* [--format=<format>] | ||
* : Format to display the results. Options are table, csv, and json. The default will be a table. | ||
* --- | ||
* default: table | ||
* options: | ||
* - table | ||
* - csv | ||
* - json | ||
* --- | ||
* | ||
* ## EXAMPLES | ||
* | ||
* wp plugin check-categories | ||
* wp plugin check-categories --format=json | ||
* | ||
* @subcommand check-categories | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param array $args List of the positional arguments. | ||
* @param array $assoc_args List of the associative arguments. | ||
* | ||
* @SuppressWarnings(PHPMD.NPathComplexity) | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function check_categories( $args, $assoc_args ) { | ||
/* | ||
* Bail early if the Plugin Checker is not activated. | ||
* | ||
* If the Plugin Checker is not activated, it will throw an error and | ||
* CLI commands won't be usable. | ||
*/ | ||
if ( is_plugin_inactive( $this->plugin_context->basename() ) ) { | ||
WP_CLI::error( | ||
__( 'Plugin Checker is not active.', 'plugin-check' ) | ||
); | ||
} | ||
|
||
// Get options based on the CLI arguments. | ||
$options = $this->get_options( $assoc_args ); | ||
|
||
// Get categories details. | ||
$categories = $this->get_categories(); | ||
|
||
// Get formatter. | ||
$formatter = $this->get_formatter( $options ); | ||
|
||
// Display results. | ||
$formatter->display_items( $categories ); | ||
} | ||
|
||
/** | ||
* Returns check categories details. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return array List of the check categories. | ||
*/ | ||
private function get_categories() { | ||
$categories = array(); | ||
|
||
$check_categories = new Check_Categories(); | ||
$categories_slugs = $check_categories->get_categories(); | ||
|
||
if ( is_array( $categories_slugs ) && count( $categories_slugs ) > 0 ) { | ||
$categories = array_map( | ||
function ( $slug ) { | ||
return array( | ||
'slug' => $slug, | ||
'name' => ucfirst( str_replace( '_', ' ', $slug ) ), | ||
); | ||
}, | ||
$categories_slugs | ||
); | ||
|
||
} | ||
|
||
return $categories; | ||
} | ||
|
||
/** | ||
* Validates the associative arguments. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param array $assoc_args List of the associative arguments. | ||
* @return array List of the associative arguments. | ||
* | ||
* @throws WP_CLI\ExitException Show error if plugin not found. | ||
*/ | ||
private function get_options( $assoc_args ) { | ||
$defaults = array( | ||
'format' => 'table', | ||
); | ||
|
||
$options = wp_parse_args( $assoc_args, $defaults ); | ||
|
||
if ( ! in_array( $options['format'], $this->output_formats, true ) ) { | ||
WP_CLI::error( | ||
sprintf( | ||
// translators: 1. Output formats. | ||
__( 'Invalid format argument, valid value will be one of [%1$s]', 'plugin-check' ), | ||
implode( ', ', $this->output_formats ) | ||
) | ||
); | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* Gets the formatter instance to format check results. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param array $assoc_args Associative arguments. | ||
* @return WP_CLI\Formatter The formatter instance. | ||
*/ | ||
private function get_formatter( $assoc_args ) { | ||
$default_fields = array( | ||
'name', | ||
'slug', | ||
); | ||
|
||
if ( isset( $assoc_args['fields'] ) ) { | ||
$default_fields = wp_parse_args( $assoc_args['fields'], $default_fields ); | ||
} | ||
|
||
return new WP_CLI\Formatter( | ||
$assoc_args, | ||
$default_fields | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?php | ||
/** | ||
* Class WordPress\Plugin_Check\CLI\Plugin_Check_Checks_Command | ||
* | ||
* @package plugin-check | ||
*/ | ||
|
||
namespace WordPress\Plugin_Check\CLI; | ||
|
||
use WordPress\Plugin_Check\Checker\CLI_Checks_Runner; | ||
use WordPress\Plugin_Check\Plugin_Context; | ||
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; | ||
use WP_CLI; | ||
|
||
/** | ||
* Plugin check checks command. | ||
*/ | ||
final class Plugin_Check_Checks_Command { | ||
|
||
/** | ||
* Plugin context. | ||
* | ||
* @since n.e.x.t | ||
* @var Plugin_Context | ||
*/ | ||
protected $plugin_context; | ||
|
||
/** | ||
* Output format type. | ||
* | ||
* @since n.e.x.t | ||
* @var string[] | ||
*/ | ||
protected $output_formats = array( | ||
'table', | ||
'csv', | ||
'json', | ||
); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param Plugin_Context $plugin_context Plugin context. | ||
*/ | ||
public function __construct( Plugin_Context $plugin_context ) { | ||
$this->plugin_context = $plugin_context; | ||
} | ||
|
||
/** | ||
* Runs plugin check-checks. | ||
* | ||
* ## OPTIONS | ||
* | ||
* [--fields=<fields>] | ||
* : Limit displayed results to a subset of fields provided. | ||
* | ||
* [--format=<format>] | ||
* : Format to display the results. Options are table, csv, and json. The default will be a table. | ||
* --- | ||
* default: table | ||
* options: | ||
* - table | ||
* - csv | ||
* - json | ||
* --- | ||
* | ||
* @subcommand check-checks | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param array $args List of the positional arguments. | ||
* @param array $assoc_args List of the associative arguments. | ||
* | ||
* @throws Exception Throws exception. | ||
* | ||
* @SuppressWarnings(PHPMD.NPathComplexity) | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function check_checks( $args, $assoc_args ) { | ||
Check failure on line 81 in includes/CLI/Plugin_Check_Checks_Command.php GitHub Actions / PHP
|
||
// Get the Runner. | ||
$runner = Plugin_Request_Utility::get_runner(); | ||
|
||
// Create the runner if not already initialized. | ||
if ( is_null( $runner ) ) { | ||
$runner = new CLI_Checks_Runner(); | ||
} | ||
|
||
// Make sure we are using the correct runner instance. | ||
if ( ! ( $runner instanceof CLI_Checks_Runner ) ) { | ||
WP_CLI::error( | ||
__( 'CLI Checks Runner was not initialized correctly.', 'plugin-check' ) | ||
); | ||
} | ||
|
||
$all_checks = array(); | ||
|
||
$checks_to_run = $runner->get_checks_to_run(); | ||
|
||
if ( is_array( $checks_to_run ) && count( $checks_to_run ) > 0 ) { | ||
foreach ( $checks_to_run as $key => $check ) { | ||
$item = array(); | ||
|
||
$item['slug'] = $key; | ||
$item['stability'] = strtolower( $check->get_stability() ); | ||
$item['category'] = join( ', ', $check->get_categories() ); | ||
|
||
$all_checks[] = $item; | ||
} | ||
} | ||
|
||
// Get options based on the CLI arguments. | ||
$options = $this->get_options( $assoc_args ); | ||
|
||
// Get formatter. | ||
$formatter = $this->get_formatter( $options ); | ||
|
||
// Display results. | ||
$formatter->display_items( $all_checks ); | ||
} | ||
|
||
/** | ||
* Validates the associative arguments. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param array $assoc_args List of the associative arguments. | ||
* @return array List of the associative arguments. | ||
* | ||
* @throws WP_CLI\ExitException Show error if plugin not found. | ||
*/ | ||
private function get_options( $assoc_args ) { | ||
$defaults = array( | ||
'format' => 'table', | ||
); | ||
|
||
$options = wp_parse_args( $assoc_args, $defaults ); | ||
|
||
if ( ! in_array( $options['format'], $this->output_formats, true ) ) { | ||
WP_CLI::error( | ||
sprintf( | ||
// translators: 1. Output formats. | ||
__( 'Invalid format argument, valid value will be one of [%1$s]', 'plugin-check' ), | ||
implode( ', ', $this->output_formats ) | ||
) | ||
); | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* Gets the formatter instance to format results. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param array $assoc_args Associative arguments. | ||
* @return WP_CLI\Formatter The formatter instance. | ||
*/ | ||
private function get_formatter( $assoc_args ) { | ||
$default_fields = array( | ||
'slug', | ||
'category', | ||
'stability', | ||
); | ||
|
||
if ( isset( $assoc_args['fields'] ) ) { | ||
$default_fields = wp_parse_args( $assoc_args['fields'], $default_fields ); | ||
} | ||
|
||
return new WP_CLI\Formatter( | ||
$assoc_args, | ||
$default_fields | ||
); | ||
} | ||
} |
Oops, something went wrong.