diff --git a/includes/CLI/Plugin_Check_Command.php b/includes/CLI/Plugin_Check_Command.php index 4e1e28d32..ee0ff7fe4 100644 --- a/includes/CLI/Plugin_Check_Command.php +++ b/includes/CLI/Plugin_Check_Command.php @@ -40,6 +40,20 @@ final class Plugin_Check_Command { 'json', ); + /** + * Field names. + * + * @since n.e.x.t + * @var string[] + */ + protected $allowed_fields = array( + 'line', + 'column', + 'type', + 'code', + 'message', + ); + /** * Constructor. * @@ -114,21 +128,12 @@ public function __construct( Plugin_Context $plugin_context ) { * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function check( $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 ); + // Validate field names. + $this->validate_fields( wp_parse_list( $options['fields'] ) ); + // Create the plugin and checks array from CLI arguments. $plugin = isset( $args[0] ) ? $args[0] : ''; $checks = wp_parse_list( $options['checks'] ); @@ -411,4 +416,21 @@ private function has_runtime_check( array $checks ) { return false; } + + /** + * Validate field names. + * + * @since n.e.x.t + * + * @param array $fields An array of field names. + */ + private function validate_fields( array $fields ) { + $invalid_fields = array_diff( $fields, $this->allowed_fields ); + + if ( count( $invalid_fields ) > 0 ) { + // translators: %s: Field names. + $message = ( 1 === count( $invalid_fields ) ) ? __( 'Invalid field: %s', 'plugin-check' ) : __( 'Invalid fields: %s', 'plugin-check' ); + WP_CLI::error( sprintf( $message, join( ', ', $invalid_fields ) ) ); + } + } }