From 4aa93bf805496ce05e045c46a2786778ed4f381f Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 6 Sep 2024 11:32:34 -0300 Subject: [PATCH 01/22] add wp-cli integration --- theme-check.php | 9 +- wp-cli/class-theme-check-cli.php | 176 +++++++++++++++++++++++++++++++ 2 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 wp-cli/class-theme-check-cli.php diff --git a/theme-check.php b/theme-check.php index a8e45451..d2b2fc86 100644 --- a/theme-check.php +++ b/theme-check.php @@ -14,8 +14,12 @@ class ThemeCheckMain { function __construct() { add_action( 'admin_init', array( $this, 'tc_i18n' ) ); add_action( 'admin_menu', array( $this, 'themecheck_add_page' ) ); - } + if ( defined( 'WP_CLI' ) && WP_CLI ) { + include 'wp-cli/class-theme-check-cli.php'; + } + } + function tc_i18n() { load_plugin_textdomain( 'theme-check', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); } @@ -43,6 +47,7 @@ function themecheck_do_page() { include 'checkbase.php'; include 'main.php'; + ?>
@@ -71,4 +76,4 @@ function themecheck_do_page() { ] + * : The slug of the theme to check. If not provided, checks the current theme. + * + * [--format=] + * : Output format. Accepts 'cli' or 'json'. Default: 'cli'. + * + * ## EXAMPLES + * + * wp theme-check run + * wp theme-check run twentytwentyfour + * wp theme-check run --format=json + * wp theme-check run twentytwentyfour --format=json + * + */ + public function run( $args, $assoc_args ) { + // Get the output format + $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' ); + if ( !in_array( $format, ['cli', 'json'] ) ) { + WP_CLI::error( "Invalid format. Accepts 'cli' or 'json'." ); + return; + } + + // Get the current theme + $current_theme = wp_get_theme(); + $current_theme_slug = $current_theme->get_stylesheet(); + + // Use the provided theme slug if available, otherwise use the current theme + $check_theme_slug = $args[0] ?? $current_theme_slug; + + // Get the theme + $theme = wp_get_theme( $check_theme_slug ); + + if ( ! $theme->exists() ) { + if ( $format === 'json' ) { + $json_output = array( + 'check-completed' => false, + 'result' => "Error: Theme '{$check_theme_slug}' not found.", + 'messages' => [] + ); + WP_CLI::log (wp_json_encode($json_output, JSON_PRETTY_PRINT) ); + return; + } + WP_CLI::error( "Theme '{$check_theme_slug}' not found." ); + return; + } + + // Run the checks. + $success = run_themechecks_against_theme( $theme, $check_theme_slug ); + + if ( $format === 'json' ) { + if ( ! $success ) { + $json_output = array( + 'check-completed' => false, + 'result' => "Error: Theme check failed for {$slug}.", + 'messages' => [] + ); + WP_CLI::log (wp_json_encode($json_output, JSON_PRETTY_PRINT) ); + return; + } + $this->display_themechecks_as_json( $check_theme_slug ); + } else { + if ( ! $success ) { + WP_CLI::error( "Theme check failed for {$slug}." ); + return; + } + $this->display_themechecks_in_cli( $check_theme_slug ); + } + } + + /** + * Process theme check messages. + * + * @return array Processed messages categorized by type. + */ + private function process_themecheck_messages() { + global $themechecks; + $messages = array(); + $processed_messages = array( + 'errors' => array(), + 'warnings' => array(), + 'infos' => array(), + 'others' => array() + ); + + foreach ($themechecks as $check) { + if ($check instanceof themecheck) { + $error = $check->getError(); + $error = (array) $error; + if (!empty($error)) { + $messages = array_merge($messages, $error); + } + } + } + + foreach ($messages as $message) { + $clean_message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'); + + if (strpos($clean_message, 'ERROR:') === 0) { + $processed_messages['errors'][] = $clean_message; + } elseif (strpos($clean_message, 'WARNING:') === 0) { + $processed_messages['warnings'][] = $clean_message; + } elseif (strpos($clean_message, 'INFO') === 0) { + $processed_messages['infos'][] = $clean_message; + } else { + $processed_messages['others'][] = $clean_message; + } + } + + return $processed_messages; + } + + /** + * Display the theme checks in the CLI. + * + * @return void + */ + private function display_themechecks_in_cli( $slug ) { + $processed_messages = $this->process_themecheck_messages(); + + WP_CLI::success("Theme check completed for {$slug}."); + + if ( + !empty($processed_messages['errors']) || + !empty($processed_messages['warnings']) || + !empty($processed_messages['infos']) || + !empty($processed_messages['others']) + ) { + foreach ($processed_messages['errors'] as $error) { + WP_CLI::error(ltrim($error, 'ERROR: '), false); + } + foreach ($processed_messages['warnings'] as $warning) { + WP_CLI::warning(ltrim($warning, 'WARNING: ')); + } + foreach ($processed_messages['infos'] as $info) { + WP_CLI::log( $info ); + } + foreach ($processed_messages['others'] as $other) { + WP_CLI::log("LOG: " . $other); + } + } else { + WP_CLI::success("No issues found."); + } + } + + /** + * Display the theme checks in JSON format. + * + * @return void + */ + private function display_themechecks_as_json( $slug ) { + $processed_messages = $this->process_themecheck_messages(); + + $json_output = array( + 'check-completed' => true, + 'result' => "Theme check completed for {$slug}.", + 'messages' => $processed_messages + ); + + WP_CLI::log(wp_json_encode($json_output, JSON_PRETTY_PRINT)); + } +} \ No newline at end of file From 4b4b75c2027571d839579d06a51d8754137cb609 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 6 Sep 2024 11:48:52 -0300 Subject: [PATCH 02/22] php lint fix --- theme-check.php | 5 +- wp-cli/class-theme-check-cli.php | 339 +++++++++++++++---------------- 2 files changed, 171 insertions(+), 173 deletions(-) diff --git a/theme-check.php b/theme-check.php index d2b2fc86..aaa818e7 100644 --- a/theme-check.php +++ b/theme-check.php @@ -19,7 +19,7 @@ function __construct() { include 'wp-cli/class-theme-check-cli.php'; } } - + function tc_i18n() { load_plugin_textdomain( 'theme-check', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); } @@ -47,7 +47,6 @@ function themecheck_do_page() { include 'checkbase.php'; include 'main.php'; - ?>
@@ -76,4 +75,4 @@ function themecheck_do_page() { ] - * : The slug of the theme to check. If not provided, checks the current theme. - * - * [--format=] - * : Output format. Accepts 'cli' or 'json'. Default: 'cli'. - * - * ## EXAMPLES - * - * wp theme-check run - * wp theme-check run twentytwentyfour - * wp theme-check run --format=json - * wp theme-check run twentytwentyfour --format=json - * - */ - public function run( $args, $assoc_args ) { - // Get the output format - $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' ); - if ( !in_array( $format, ['cli', 'json'] ) ) { - WP_CLI::error( "Invalid format. Accepts 'cli' or 'json'." ); - return; - } - - // Get the current theme - $current_theme = wp_get_theme(); - $current_theme_slug = $current_theme->get_stylesheet(); - - // Use the provided theme slug if available, otherwise use the current theme - $check_theme_slug = $args[0] ?? $current_theme_slug; - - // Get the theme - $theme = wp_get_theme( $check_theme_slug ); - - if ( ! $theme->exists() ) { - if ( $format === 'json' ) { - $json_output = array( - 'check-completed' => false, - 'result' => "Error: Theme '{$check_theme_slug}' not found.", - 'messages' => [] - ); - WP_CLI::log (wp_json_encode($json_output, JSON_PRETTY_PRINT) ); - return; - } - WP_CLI::error( "Theme '{$check_theme_slug}' not found." ); - return; - } - - // Run the checks. - $success = run_themechecks_against_theme( $theme, $check_theme_slug ); - - if ( $format === 'json' ) { - if ( ! $success ) { - $json_output = array( - 'check-completed' => false, - 'result' => "Error: Theme check failed for {$slug}.", - 'messages' => [] - ); - WP_CLI::log (wp_json_encode($json_output, JSON_PRETTY_PRINT) ); - return; - } - $this->display_themechecks_as_json( $check_theme_slug ); - } else { - if ( ! $success ) { - WP_CLI::error( "Theme check failed for {$slug}." ); - return; - } - $this->display_themechecks_in_cli( $check_theme_slug ); - } - } - - /** - * Process theme check messages. - * - * @return array Processed messages categorized by type. - */ - private function process_themecheck_messages() { - global $themechecks; - $messages = array(); - $processed_messages = array( - 'errors' => array(), - 'warnings' => array(), - 'infos' => array(), - 'others' => array() - ); - - foreach ($themechecks as $check) { - if ($check instanceof themecheck) { - $error = $check->getError(); - $error = (array) $error; - if (!empty($error)) { - $messages = array_merge($messages, $error); - } - } - } - - foreach ($messages as $message) { - $clean_message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'); - - if (strpos($clean_message, 'ERROR:') === 0) { - $processed_messages['errors'][] = $clean_message; - } elseif (strpos($clean_message, 'WARNING:') === 0) { - $processed_messages['warnings'][] = $clean_message; - } elseif (strpos($clean_message, 'INFO') === 0) { - $processed_messages['infos'][] = $clean_message; - } else { - $processed_messages['others'][] = $clean_message; - } - } - - return $processed_messages; - } - - /** - * Display the theme checks in the CLI. - * - * @return void - */ - private function display_themechecks_in_cli( $slug ) { - $processed_messages = $this->process_themecheck_messages(); - - WP_CLI::success("Theme check completed for {$slug}."); - - if ( - !empty($processed_messages['errors']) || - !empty($processed_messages['warnings']) || - !empty($processed_messages['infos']) || - !empty($processed_messages['others']) - ) { - foreach ($processed_messages['errors'] as $error) { - WP_CLI::error(ltrim($error, 'ERROR: '), false); - } - foreach ($processed_messages['warnings'] as $warning) { - WP_CLI::warning(ltrim($warning, 'WARNING: ')); - } - foreach ($processed_messages['infos'] as $info) { - WP_CLI::log( $info ); - } - foreach ($processed_messages['others'] as $other) { - WP_CLI::log("LOG: " . $other); - } - } else { - WP_CLI::success("No issues found."); - } - } - - /** - * Display the theme checks in JSON format. - * - * @return void - */ - private function display_themechecks_as_json( $slug ) { - $processed_messages = $this->process_themecheck_messages(); - - $json_output = array( - 'check-completed' => true, - 'result' => "Theme check completed for {$slug}.", - 'messages' => $processed_messages - ); - - WP_CLI::log(wp_json_encode($json_output, JSON_PRETTY_PRINT)); - } -} \ No newline at end of file + /** + * Run a theme check on the specified theme or the current theme. + * + * ## OPTIONS + * + * [] + * : The slug of the theme to check. If not provided, checks the current theme. + * + * [--format=] + * : Output format. Accepts 'cli' or 'json'. Default: 'cli'. + * + * ## EXAMPLES + * + * wp theme-check run + * wp theme-check run twentytwentyfour + * wp theme-check run --format=json + * wp theme-check run twentytwentyfour --format=json + */ + public function run( $args, $assoc_args ) { + // Get the output format + $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' ); + if ( ! in_array( $format, array( 'cli', 'json' ) ) ) { + WP_CLI::error( "Invalid format. Accepts 'cli' or 'json'." ); + return; + } + + // Get the current theme + $current_theme = wp_get_theme(); + $current_theme_slug = $current_theme->get_stylesheet(); + + // Use the provided theme slug if available, otherwise use the current theme + $check_theme_slug = $args[0] ?? $current_theme_slug; + + // Get the theme + $theme = wp_get_theme( $check_theme_slug ); + + if ( ! $theme->exists() ) { + if ( $format === 'json' ) { + $json_output = array( + 'check-completed' => false, + 'result' => "Error: Theme '{$check_theme_slug}' not found.", + 'messages' => array(), + ); + WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); + return; + } + WP_CLI::error( "Theme '{$check_theme_slug}' not found." ); + return; + } + + // Run the checks. + $success = run_themechecks_against_theme( $theme, $check_theme_slug ); + + if ( $format === 'json' ) { + if ( ! $success ) { + $json_output = array( + 'check-completed' => false, + 'result' => "Error: Theme check failed for {$slug}.", + 'messages' => array(), + ); + WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); + return; + } + $this->display_themechecks_as_json( $check_theme_slug ); + } else { + if ( ! $success ) { + WP_CLI::error( "Theme check failed for {$slug}." ); + return; + } + $this->display_themechecks_in_cli( $check_theme_slug ); + } + } + + /** + * Process theme check messages. + * + * @return array Processed messages categorized by type. + */ + private function process_themecheck_messages() { + global $themechecks; + $messages = array(); + $processed_messages = array( + 'errors' => array(), + 'warnings' => array(), + 'infos' => array(), + 'others' => array(), + ); + + foreach ( $themechecks as $check ) { + if ( $check instanceof themecheck ) { + $error = $check->getError(); + $error = (array) $error; + if ( ! empty( $error ) ) { + $messages = array_merge( $messages, $error ); + } + } + } + + foreach ( $messages as $message ) { + $clean_message = html_entity_decode( strip_tags( $message ), ENT_QUOTES, 'UTF-8' ); + + if ( strpos( $clean_message, 'ERROR:' ) === 0 ) { + $processed_messages['errors'][] = $clean_message; + } elseif ( strpos( $clean_message, 'WARNING:' ) === 0 ) { + $processed_messages['warnings'][] = $clean_message; + } elseif ( strpos( $clean_message, 'INFO' ) === 0 ) { + $processed_messages['infos'][] = $clean_message; + } else { + $processed_messages['others'][] = $clean_message; + } + } + + return $processed_messages; + } + + /** + * Display the theme checks in the CLI. + * + * @return void + */ + private function display_themechecks_in_cli( $slug ) { + $processed_messages = $this->process_themecheck_messages(); + + WP_CLI::success( "Theme check completed for {$slug}." ); + + if ( + ! empty( $processed_messages['errors'] ) || + ! empty( $processed_messages['warnings'] ) || + ! empty( $processed_messages['infos'] ) || + ! empty( $processed_messages['others'] ) + ) { + foreach ( $processed_messages['errors'] as $error ) { + WP_CLI::error( ltrim( $error, 'ERROR: ' ), false ); + } + foreach ( $processed_messages['warnings'] as $warning ) { + WP_CLI::warning( ltrim( $warning, 'WARNING: ' ) ); + } + foreach ( $processed_messages['infos'] as $info ) { + WP_CLI::log( $info ); + } + foreach ( $processed_messages['others'] as $other ) { + WP_CLI::log( 'LOG: ' . $other ); + } + } else { + WP_CLI::success( 'No issues found.' ); + } + } + + /** + * Display the theme checks in JSON format. + * + * @return void + */ + private function display_themechecks_as_json( $slug ) { + $processed_messages = $this->process_themecheck_messages(); + + $json_output = array( + 'check-completed' => true, + 'result' => "Theme check completed for {$slug}.", + 'messages' => $processed_messages, + ); + + WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); + } +} From 9508699aa0d0a6ab9ed531990a9b498e17aa39f2 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 6 Sep 2024 11:52:25 -0300 Subject: [PATCH 03/22] lint fix --- wp-cli/class-theme-check-cli.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index b7078dc8..63f07b60 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -11,6 +11,9 @@ class Theme_Check_CLI { /** * Run a theme check on the specified theme or the current theme. * + * @param array $args Indexed array of positional arguments. + * @param array $assoc_args Associative array of options. + * * ## OPTIONS * * [] @@ -126,6 +129,7 @@ private function process_themecheck_messages() { /** * Display the theme checks in the CLI. * + * @param string $slug The slug of the theme to display the checks for. * @return void */ private function display_themechecks_in_cli( $slug ) { @@ -159,6 +163,7 @@ private function display_themechecks_in_cli( $slug ) { /** * Display the theme checks in JSON format. * + * @param string $slug The slug of the theme to display the checks for. * @return void */ private function display_themechecks_as_json( $slug ) { From a9d4944ab69ca9d1019df3e01407abab3cc8f8b3 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 6 Sep 2024 13:41:02 -0300 Subject: [PATCH 04/22] use wp_strip_all_tags --- wp-cli/class-theme-check-cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 63f07b60..0a07f737 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -110,7 +110,7 @@ private function process_themecheck_messages() { } foreach ( $messages as $message ) { - $clean_message = html_entity_decode( strip_tags( $message ), ENT_QUOTES, 'UTF-8' ); + $clean_message = html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' ); if ( strpos( $clean_message, 'ERROR:' ) === 0 ) { $processed_messages['errors'][] = $clean_message; From b28cd1b77ffc5960b3614da7e3da1f19ea62a98d Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 6 Sep 2024 13:42:28 -0300 Subject: [PATCH 05/22] avoid coalescing operator (??) --- wp-cli/class-theme-check-cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 0a07f737..b778b43b 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -42,7 +42,7 @@ public function run( $args, $assoc_args ) { $current_theme_slug = $current_theme->get_stylesheet(); // Use the provided theme slug if available, otherwise use the current theme - $check_theme_slug = $args[0] ?? $current_theme_slug; + $check_theme_slug = !empty( $args[0] ) ? $args[0] : $current_theme_slug; // Get the theme $theme = wp_get_theme( $check_theme_slug ); From b39c7bc0c865b0681644e74561f7a2bdfe71f32c Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 6 Sep 2024 13:47:34 -0300 Subject: [PATCH 06/22] lint --- wp-cli/class-theme-check-cli.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index b778b43b..041b49e3 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -13,9 +13,9 @@ class Theme_Check_CLI { * * @param array $args Indexed array of positional arguments. * @param array $assoc_args Associative array of options. + * @return void * * ## OPTIONS - * * [] * : The slug of the theme to check. If not provided, checks the current theme. * @@ -23,7 +23,6 @@ class Theme_Check_CLI { * : Output format. Accepts 'cli' or 'json'. Default: 'cli'. * * ## EXAMPLES - * * wp theme-check run * wp theme-check run twentytwentyfour * wp theme-check run --format=json @@ -42,7 +41,7 @@ public function run( $args, $assoc_args ) { $current_theme_slug = $current_theme->get_stylesheet(); // Use the provided theme slug if available, otherwise use the current theme - $check_theme_slug = !empty( $args[0] ) ? $args[0] : $current_theme_slug; + $check_theme_slug = ! empty( $args[0] ) ? $args[0] : $current_theme_slug; // Get the theme $theme = wp_get_theme( $check_theme_slug ); From 4bdcdd95915e34eee7e1479462240ccf9073fd30 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 6 Sep 2024 14:31:10 -0300 Subject: [PATCH 07/22] use the right slug variable Co-authored-by: Jeff Ong --- wp-cli/class-theme-check-cli.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 041b49e3..99cfda5c 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -67,7 +67,7 @@ public function run( $args, $assoc_args ) { if ( ! $success ) { $json_output = array( 'check-completed' => false, - 'result' => "Error: Theme check failed for {$slug}.", + 'result' => "Error: Theme check failed for {$check_theme_slug}.", 'messages' => array(), ); WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); @@ -76,7 +76,7 @@ public function run( $args, $assoc_args ) { $this->display_themechecks_as_json( $check_theme_slug ); } else { if ( ! $success ) { - WP_CLI::error( "Theme check failed for {$slug}." ); + WP_CLI::error( "Theme check failed for {$check_theme_slug}." ); return; } $this->display_themechecks_in_cli( $check_theme_slug ); From 745e077a9b80a778f9b65f461090595c06af9e97 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Fri, 6 Sep 2024 13:27:11 -0400 Subject: [PATCH 08/22] Add documentation about wp-cli usage. --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 250a1139..9d3b8f05 100644 --- a/README.md +++ b/README.md @@ -50,5 +50,23 @@ add_filter( 'tc_skip_development_directories', '__return_true' ); To add more directories to the paths where other files are excluded then add them to the array through the `tc_common_dev_directories` filter. +### Usage with wp-cli + +To use with [wp-cli](https://wp-cli.org/), ensure the theme check plugin is active and `wp-cli` is installed. The `theme-check` subcommand is added to `wp-cli` and can be used as follows: + +`wp theme-check run [] [--format=]` + +#### Options +| Option | Accepts | Required | Default +| -- | -- | -- | -- | +| `theme` | The slug of the theme to check | No | Current theme slug +| `format` | `cli` or `json` | No | `cli` + +#### Examples +`wp theme-check run` +`wp theme-check run twentytwentyfour` +`wp theme-check run --format=json` +`wp theme-check run twentytwentyfour --format=json` + ## Contributors Otto42, pross, The theme review team From 2218fdc0af482f3b982ae652b0ba4c7f01dc3553 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 9 Sep 2024 11:41:32 -0300 Subject: [PATCH 09/22] rename class and extend WP_CLI_Command class --- wp-cli/class-theme-check-cli.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 99cfda5c..5ccea8ba 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -4,10 +4,10 @@ $parent_dir = dirname( __DIR__ ); require_once $parent_dir . '/checkbase.php'; require_once $parent_dir . '/main.php'; - WP_CLI::add_command( 'theme-check', 'Theme_Check_CLI' ); + WP_CLI::add_command( 'theme-check', 'Theme_Check_Command' ); } -class Theme_Check_CLI { +class Theme_Check_Command extends WP_CLI_Command { /** * Run a theme check on the specified theme or the current theme. * @@ -29,13 +29,11 @@ class Theme_Check_CLI { * wp theme-check run twentytwentyfour --format=json */ public function run( $args, $assoc_args ) { - // Get the output format $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' ); if ( ! in_array( $format, array( 'cli', 'json' ) ) ) { WP_CLI::error( "Invalid format. Accepts 'cli' or 'json'." ); return; } - // Get the current theme $current_theme = wp_get_theme(); $current_theme_slug = $current_theme->get_stylesheet(); From 75c2f9cf123ebb6ec2272436cf421429dd6ad7d8 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 9 Sep 2024 11:44:13 -0300 Subject: [PATCH 10/22] format comments --- wp-cli/class-theme-check-cli.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 5ccea8ba..c02ed9dd 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -20,12 +20,25 @@ class Theme_Check_Command extends WP_CLI_Command { * : The slug of the theme to check. If not provided, checks the current theme. * * [--format=] - * : Output format. Accepts 'cli' or 'json'. Default: 'cli'. + * : Render output in a particular format. + * --- + * default: cli + * options: + * - cli + * - json + * --- * * ## EXAMPLES + * # Check the current active theme * wp theme-check run + * + * # Check a specific theme * wp theme-check run twentytwentyfour + * + * # Check the current active theme and output results as JSON * wp theme-check run --format=json + * + * # Check a specific theme and output results as JSON * wp theme-check run twentytwentyfour --format=json */ public function run( $args, $assoc_args ) { From 37542b38dd2ac7b8d2125f67dcc4cfc59dfa4541 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 9 Sep 2024 13:25:22 -0300 Subject: [PATCH 11/22] simplify logging --- .vscode/launch.json | 48 +++++++++++++++++++++ wp-cli/class-theme-check-cli.php | 73 +++++++++----------------------- 2 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..9dc6b4df --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,48 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Listen for Xdebug", + "type": "php", + "request": "launch", + "port": 9003 + }, + { + "name": "Launch currently open script", + "type": "php", + "request": "launch", + "program": "${file}", + "cwd": "${fileDirname}", + "port": 0, + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + }, + { + "name": "Launch Built-in web server", + "type": "php", + "request": "launch", + "runtimeArgs": [ + "-dxdebug.mode=debug", + "-dxdebug.start_with_request=yes", + "-S", + "localhost:0" + ], + "program": "", + "cwd": "${workspaceRoot}", + "port": 9003, + "serverReadyAction": { + "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", + "uriFormat": "http://localhost:%s", + "action": "openExternally" + } + } + ] +} \ No newline at end of file diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index c02ed9dd..38038523 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -43,10 +43,7 @@ class Theme_Check_Command extends WP_CLI_Command { */ public function run( $args, $assoc_args ) { $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' ); - if ( ! in_array( $format, array( 'cli', 'json' ) ) ) { - WP_CLI::error( "Invalid format. Accepts 'cli' or 'json'." ); - return; - } + // Get the current theme $current_theme = wp_get_theme(); $current_theme_slug = $current_theme->get_stylesheet(); @@ -60,15 +57,14 @@ public function run( $args, $assoc_args ) { if ( ! $theme->exists() ) { if ( $format === 'json' ) { $json_output = array( - 'check-completed' => false, + 'completed' => false, 'result' => "Error: Theme '{$check_theme_slug}' not found.", 'messages' => array(), ); - WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); + WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); return; } WP_CLI::error( "Theme '{$check_theme_slug}' not found." ); - return; } // Run the checks. @@ -77,18 +73,17 @@ public function run( $args, $assoc_args ) { if ( $format === 'json' ) { if ( ! $success ) { $json_output = array( - 'check-completed' => false, + 'completed' => false, 'result' => "Error: Theme check failed for {$check_theme_slug}.", 'messages' => array(), ); - WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); + WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); return; } $this->display_themechecks_as_json( $check_theme_slug ); } else { if ( ! $success ) { WP_CLI::error( "Theme check failed for {$check_theme_slug}." ); - return; } $this->display_themechecks_in_cli( $check_theme_slug ); } @@ -97,17 +92,11 @@ public function run( $args, $assoc_args ) { /** * Process theme check messages. * - * @return array Processed messages categorized by type. + * @return array Processed messages. */ private function process_themecheck_messages() { global $themechecks; - $messages = array(); - $processed_messages = array( - 'errors' => array(), - 'warnings' => array(), - 'infos' => array(), - 'others' => array(), - ); + $messages = array(); foreach ( $themechecks as $check ) { if ( $check instanceof themecheck ) { @@ -119,19 +108,9 @@ private function process_themecheck_messages() { } } - foreach ( $messages as $message ) { - $clean_message = html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' ); - - if ( strpos( $clean_message, 'ERROR:' ) === 0 ) { - $processed_messages['errors'][] = $clean_message; - } elseif ( strpos( $clean_message, 'WARNING:' ) === 0 ) { - $processed_messages['warnings'][] = $clean_message; - } elseif ( strpos( $clean_message, 'INFO' ) === 0 ) { - $processed_messages['infos'][] = $clean_message; - } else { - $processed_messages['others'][] = $clean_message; - } - } + $processed_messages = array_map(function($message) { + return html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' ); + }, $messages); return $processed_messages; } @@ -147,26 +126,14 @@ private function display_themechecks_in_cli( $slug ) { WP_CLI::success( "Theme check completed for {$slug}." ); - if ( - ! empty( $processed_messages['errors'] ) || - ! empty( $processed_messages['warnings'] ) || - ! empty( $processed_messages['infos'] ) || - ! empty( $processed_messages['others'] ) - ) { - foreach ( $processed_messages['errors'] as $error ) { - WP_CLI::error( ltrim( $error, 'ERROR: ' ), false ); - } - foreach ( $processed_messages['warnings'] as $warning ) { - WP_CLI::warning( ltrim( $warning, 'WARNING: ' ) ); - } - foreach ( $processed_messages['infos'] as $info ) { - WP_CLI::log( $info ); - } - foreach ( $processed_messages['others'] as $other ) { - WP_CLI::log( 'LOG: ' . $other ); - } - } else { - WP_CLI::success( 'No issues found.' ); + if ( empty( $processed_messages ) ) { + WP_CLI::line( 'No issues found.' ); + return; + } + + foreach ( $processed_messages as $message ) { + WP_CLI::line( '' ); + WP_CLI::line( $message ); } } @@ -180,11 +147,11 @@ private function display_themechecks_as_json( $slug ) { $processed_messages = $this->process_themecheck_messages(); $json_output = array( - 'check-completed' => true, + 'completed' => true, 'result' => "Theme check completed for {$slug}.", 'messages' => $processed_messages, ); - WP_CLI::log( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); + WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); } } From e998dc02035639c6f0bb3fa297842a09391abd95 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 9 Sep 2024 16:41:39 -0300 Subject: [PATCH 12/22] format function comment to make wp help command work as expected --- wp-cli/class-theme-check-cli.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 38038523..4174f1e0 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -11,10 +11,6 @@ class Theme_Check_Command extends WP_CLI_Command { /** * Run a theme check on the specified theme or the current theme. * - * @param array $args Indexed array of positional arguments. - * @param array $assoc_args Associative array of options. - * @return void - * * ## OPTIONS * [] * : The slug of the theme to check. If not provided, checks the current theme. @@ -40,6 +36,10 @@ class Theme_Check_Command extends WP_CLI_Command { * * # Check a specific theme and output results as JSON * wp theme-check run twentytwentyfour --format=json + * + * @param array $args Indexed array of positional arguments. + * @param array $assoc_args Associative array of options. + * @return void */ public function run( $args, $assoc_args ) { $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' ); From 097bfc8c8bb7e0cb676e68cc63e537048b803dae Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 9 Sep 2024 16:43:34 -0300 Subject: [PATCH 13/22] lint fix --- wp-cli/class-theme-check-cli.php | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 4174f1e0..31b5bd87 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -25,18 +25,18 @@ class Theme_Check_Command extends WP_CLI_Command { * --- * * ## EXAMPLES - * # Check the current active theme + * # Check the current active theme * wp theme-check run - * + * * # Check a specific theme * wp theme-check run twentytwentyfour - * + * * # Check the current active theme and output results as JSON * wp theme-check run --format=json - * + * * # Check a specific theme and output results as JSON * wp theme-check run twentytwentyfour --format=json - * + * * @param array $args Indexed array of positional arguments. * @param array $assoc_args Associative array of options. * @return void @@ -58,8 +58,8 @@ public function run( $args, $assoc_args ) { if ( $format === 'json' ) { $json_output = array( 'completed' => false, - 'result' => "Error: Theme '{$check_theme_slug}' not found.", - 'messages' => array(), + 'result' => "Error: Theme '{$check_theme_slug}' not found.", + 'messages' => array(), ); WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); return; @@ -74,8 +74,8 @@ public function run( $args, $assoc_args ) { if ( ! $success ) { $json_output = array( 'completed' => false, - 'result' => "Error: Theme check failed for {$check_theme_slug}.", - 'messages' => array(), + 'result' => "Error: Theme check failed for {$check_theme_slug}.", + 'messages' => array(), ); WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); return; @@ -108,9 +108,12 @@ private function process_themecheck_messages() { } } - $processed_messages = array_map(function($message) { - return html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' ); - }, $messages); + $processed_messages = array_map( + function( $message ) { + return html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' ); + }, + $messages + ); return $processed_messages; } @@ -148,8 +151,8 @@ private function display_themechecks_as_json( $slug ) { $json_output = array( 'completed' => true, - 'result' => "Theme check completed for {$slug}.", - 'messages' => $processed_messages, + 'result' => "Theme check completed for {$slug}.", + 'messages' => $processed_messages, ); WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); From 8d61a5995bed77cdcf42dad6259e7549bb0f61f4 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Tue, 10 Sep 2024 15:07:54 -0300 Subject: [PATCH 14/22] remove ide file commited accidentally --- .vscode/launch.json | 48 --------------------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 9dc6b4df..00000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Listen for Xdebug", - "type": "php", - "request": "launch", - "port": 9003 - }, - { - "name": "Launch currently open script", - "type": "php", - "request": "launch", - "program": "${file}", - "cwd": "${fileDirname}", - "port": 0, - "runtimeArgs": [ - "-dxdebug.start_with_request=yes" - ], - "env": { - "XDEBUG_MODE": "debug,develop", - "XDEBUG_CONFIG": "client_port=${port}" - } - }, - { - "name": "Launch Built-in web server", - "type": "php", - "request": "launch", - "runtimeArgs": [ - "-dxdebug.mode=debug", - "-dxdebug.start_with_request=yes", - "-S", - "localhost:0" - ], - "program": "", - "cwd": "${workspaceRoot}", - "port": 9003, - "serverReadyAction": { - "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", - "uriFormat": "http://localhost:%s", - "action": "openExternally" - } - } - ] -} \ No newline at end of file From 92a3b768d6cebdccc511e4d826ea393dea0b8e1e Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 13 Sep 2024 10:40:25 -0300 Subject: [PATCH 15/22] add table format, simplify code and right exit code --- .vscode/launch.json | 48 +++++++++++++ wp-cli/class-theme-check-cli.php | 116 ++++++++++++------------------- 2 files changed, 94 insertions(+), 70 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..9dc6b4df --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,48 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Listen for Xdebug", + "type": "php", + "request": "launch", + "port": 9003 + }, + { + "name": "Launch currently open script", + "type": "php", + "request": "launch", + "program": "${file}", + "cwd": "${fileDirname}", + "port": 0, + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + }, + { + "name": "Launch Built-in web server", + "type": "php", + "request": "launch", + "runtimeArgs": [ + "-dxdebug.mode=debug", + "-dxdebug.start_with_request=yes", + "-S", + "localhost:0" + ], + "program": "", + "cwd": "${workspaceRoot}", + "port": 9003, + "serverReadyAction": { + "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", + "uriFormat": "http://localhost:%s", + "action": "openExternally" + } + } + ] +} \ No newline at end of file diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 31b5bd87..f7f82f2e 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -18,9 +18,9 @@ class Theme_Check_Command extends WP_CLI_Command { * [--format=] * : Render output in a particular format. * --- - * default: cli + * default: table * options: - * - cli + * - table * - json * --- * @@ -42,7 +42,7 @@ class Theme_Check_Command extends WP_CLI_Command { * @return void */ public function run( $args, $assoc_args ) { - $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'cli' ); + $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' ); // Get the current theme $current_theme = wp_get_theme(); @@ -55,38 +55,35 @@ public function run( $args, $assoc_args ) { $theme = wp_get_theme( $check_theme_slug ); if ( ! $theme->exists() ) { - if ( $format === 'json' ) { - $json_output = array( - 'completed' => false, - 'result' => "Error: Theme '{$check_theme_slug}' not found.", - 'messages' => array(), - ); - WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); - return; - } WP_CLI::error( "Theme '{$check_theme_slug}' not found." ); } - // Run the checks. + // Run the checks $success = run_themechecks_against_theme( $theme, $check_theme_slug ); + $processed_messages = $this->process_themecheck_messages(); - if ( $format === 'json' ) { - if ( ! $success ) { - $json_output = array( - 'completed' => false, - 'result' => "Error: Theme check failed for {$check_theme_slug}.", - 'messages' => array(), - ); - WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); - return; - } - $this->display_themechecks_as_json( $check_theme_slug ); - } else { - if ( ! $success ) { - WP_CLI::error( "Theme check failed for {$check_theme_slug}." ); - } - $this->display_themechecks_in_cli( $check_theme_slug ); + // The validation value is a boolean, but if the format is not JSON, we want to return a string. + $validation_value = $format === 'json' + ? false + : "There are no required changes in the theme {$check_theme_slug}."; + + if ( ! $success ) { + $validation_value = + // If the format is JSON, return false, otherwise return a message + $format === 'json' + ? true + : "There are required changes in the theme {$check_theme_slug}."; } + + $processed_messages[] = array( + 'type' => 'VALIDATION', + 'value' => $validation_value, + ); + + WP_CLI\Utils\format_items( $format, $processed_messages, array( 'type', 'value' ) ); + + // Set the exit code based on $success + WP_CLI::halt( $success ? 0 : 1 ); } /** @@ -110,51 +107,30 @@ private function process_themecheck_messages() { $processed_messages = array_map( function( $message ) { - return html_entity_decode( wp_strip_all_tags( $message ), ENT_QUOTES, 'UTF-8' ); - }, - $messages - ); - - return $processed_messages; - } - - /** - * Display the theme checks in the CLI. - * - * @param string $slug The slug of the theme to display the checks for. - * @return void - */ - private function display_themechecks_in_cli( $slug ) { - $processed_messages = $this->process_themecheck_messages(); - - WP_CLI::success( "Theme check completed for {$slug}." ); - - if ( empty( $processed_messages ) ) { - WP_CLI::line( 'No issues found.' ); - return; - } + if ( preg_match( '/]*>(.*?)<\/span>(.*)/', $message, $matches ) ) { + $key = $matches[1]; + $value = $matches[2]; + } else { + $key = ''; + $value = $message; + } - foreach ( $processed_messages as $message ) { - WP_CLI::line( '' ); - WP_CLI::line( $message ); - } - } + $key = wp_strip_all_tags( $key ); + $key = html_entity_decode( $key, ENT_QUOTES, 'UTF-8' ); + $key = rtrim( $key, ':' ); - /** - * Display the theme checks in JSON format. - * - * @param string $slug The slug of the theme to display the checks for. - * @return void - */ - private function display_themechecks_as_json( $slug ) { - $processed_messages = $this->process_themecheck_messages(); + $value = wp_strip_all_tags( $value ); + $value = html_entity_decode( $value, ENT_QUOTES, 'UTF-8' ); + $value = ltrim( $value, ': ' ); - $json_output = array( - 'completed' => true, - 'result' => "Theme check completed for {$slug}.", - 'messages' => $processed_messages, + return array( + 'type' => trim( $key ), + 'value' => trim( $value ), + ); + }, + $messages ); - WP_CLI::line( wp_json_encode( $json_output, JSON_PRETTY_PRINT ) ); + return $processed_messages; } } From 98b127b989d682291bbbbe54977e4108b138bf6c Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 13 Sep 2024 10:48:55 -0300 Subject: [PATCH 16/22] correct validation boolean --- wp-cli/class-theme-check-cli.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index f7f82f2e..92f0486b 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -64,14 +64,14 @@ public function run( $args, $assoc_args ) { // The validation value is a boolean, but if the format is not JSON, we want to return a string. $validation_value = $format === 'json' - ? false + ? $success : "There are no required changes in the theme {$check_theme_slug}."; if ( ! $success ) { $validation_value = // If the format is JSON, return false, otherwise return a message $format === 'json' - ? true + ? $success : "There are required changes in the theme {$check_theme_slug}."; } @@ -81,7 +81,7 @@ public function run( $args, $assoc_args ) { ); WP_CLI\Utils\format_items( $format, $processed_messages, array( 'type', 'value' ) ); - + // Set the exit code based on $success WP_CLI::halt( $success ? 0 : 1 ); } From d82740ea74277dd3e776019594ab10923cfbdd60 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 13 Sep 2024 10:49:57 -0300 Subject: [PATCH 17/22] validation value --- wp-cli/class-theme-check-cli.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 92f0486b..09e215cf 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -64,14 +64,14 @@ public function run( $args, $assoc_args ) { // The validation value is a boolean, but if the format is not JSON, we want to return a string. $validation_value = $format === 'json' - ? $success + ? true : "There are no required changes in the theme {$check_theme_slug}."; if ( ! $success ) { $validation_value = // If the format is JSON, return false, otherwise return a message $format === 'json' - ? $success + ? false : "There are required changes in the theme {$check_theme_slug}."; } From 6879e1f935b793d67cea015bc15dbd02fc10ed88 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Fri, 13 Sep 2024 12:38:48 -0300 Subject: [PATCH 18/22] lint php fix --- wp-cli/class-theme-check-cli.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 09e215cf..7f86cc5a 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -59,7 +59,7 @@ public function run( $args, $assoc_args ) { } // Run the checks - $success = run_themechecks_against_theme( $theme, $check_theme_slug ); + $success = run_themechecks_against_theme( $theme, $check_theme_slug ); $processed_messages = $this->process_themecheck_messages(); // The validation value is a boolean, but if the format is not JSON, we want to return a string. @@ -68,7 +68,7 @@ public function run( $args, $assoc_args ) { : "There are no required changes in the theme {$check_theme_slug}."; if ( ! $success ) { - $validation_value = + $validation_value = // If the format is JSON, return false, otherwise return a message $format === 'json' ? false @@ -76,7 +76,7 @@ public function run( $args, $assoc_args ) { } $processed_messages[] = array( - 'type' => 'VALIDATION', + 'type' => 'VALIDATION', 'value' => $validation_value, ); @@ -108,23 +108,23 @@ private function process_themecheck_messages() { $processed_messages = array_map( function( $message ) { if ( preg_match( '/]*>(.*?)<\/span>(.*)/', $message, $matches ) ) { - $key = $matches[1]; + $key = $matches[1]; $value = $matches[2]; } else { - $key = ''; + $key = ''; $value = $message; } - $key = wp_strip_all_tags( $key ); - $key = html_entity_decode( $key, ENT_QUOTES, 'UTF-8' ); - $key = rtrim( $key, ':' ); + $key = wp_strip_all_tags( $key ); + $key = html_entity_decode( $key, ENT_QUOTES, 'UTF-8' ); + $key = rtrim( $key, ':' ); $value = wp_strip_all_tags( $value ); $value = html_entity_decode( $value, ENT_QUOTES, 'UTF-8' ); $value = ltrim( $value, ': ' ); return array( - 'type' => trim( $key ), + 'type' => trim( $key ), 'value' => trim( $value ), ); }, From 8660260d69ac964d5916b0d25cd9f513718f9fe5 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 16 Sep 2024 16:35:49 -0300 Subject: [PATCH 19/22] remove custom VALIDATION message --- wp-cli/class-theme-check-cli.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index 7f86cc5a..ca0b7e01 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -62,24 +62,6 @@ public function run( $args, $assoc_args ) { $success = run_themechecks_against_theme( $theme, $check_theme_slug ); $processed_messages = $this->process_themecheck_messages(); - // The validation value is a boolean, but if the format is not JSON, we want to return a string. - $validation_value = $format === 'json' - ? true - : "There are no required changes in the theme {$check_theme_slug}."; - - if ( ! $success ) { - $validation_value = - // If the format is JSON, return false, otherwise return a message - $format === 'json' - ? false - : "There are required changes in the theme {$check_theme_slug}."; - } - - $processed_messages[] = array( - 'type' => 'VALIDATION', - 'value' => $validation_value, - ); - WP_CLI\Utils\format_items( $format, $processed_messages, array( 'type', 'value' ) ); // Set the exit code based on $success From d60d1e3c4d563d06d3d5a4fe721d05afd23ac6ed Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 16 Sep 2024 17:56:45 -0300 Subject: [PATCH 20/22] add a help comment --- wp-cli/class-theme-check-cli.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index ca0b7e01..f2fe457e 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -6,11 +6,13 @@ require_once $parent_dir . '/main.php'; WP_CLI::add_command( 'theme-check', 'Theme_Check_Command' ); } - +/** + * Run a theme check on the specified theme or the current theme. + */ class Theme_Check_Command extends WP_CLI_Command { /** * Run a theme check on the specified theme or the current theme. - * + * * ## OPTIONS * [] * : The slug of the theme to check. If not provided, checks the current theme. @@ -40,6 +42,7 @@ class Theme_Check_Command extends WP_CLI_Command { * @param array $args Indexed array of positional arguments. * @param array $assoc_args Associative array of options. * @return void + * */ public function run( $args, $assoc_args ) { $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' ); From 4131d0163b0c780b11a0d6829627eb4eb8fd9506 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 16 Sep 2024 17:57:25 -0300 Subject: [PATCH 21/22] Update README.md Co-authored-by: Jeff Ong --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9d3b8f05..f52fe077 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,8 @@ To use with [wp-cli](https://wp-cli.org/), ensure the theme check plugin is acti `wp theme-check run [] [--format=]` +On success, the command returns a formatted table of results from the theme check plugin. + #### Options | Option | Accepts | Required | Default | -- | -- | -- | -- | From 5b520e17b7beea434ffd14146c60ee848b4e81cb Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Mon, 16 Sep 2024 18:00:51 -0300 Subject: [PATCH 22/22] fix lint --- wp-cli/class-theme-check-cli.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wp-cli/class-theme-check-cli.php b/wp-cli/class-theme-check-cli.php index f2fe457e..603f8262 100644 --- a/wp-cli/class-theme-check-cli.php +++ b/wp-cli/class-theme-check-cli.php @@ -12,7 +12,7 @@ class Theme_Check_Command extends WP_CLI_Command { /** * Run a theme check on the specified theme or the current theme. - * + * * ## OPTIONS * [] * : The slug of the theme to check. If not provided, checks the current theme. @@ -42,7 +42,6 @@ class Theme_Check_Command extends WP_CLI_Command { * @param array $args Indexed array of positional arguments. * @param array $assoc_args Associative array of options. * @return void - * */ public function run( $args, $assoc_args ) { $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' );