11<?php
22
3+ use WP_CLI \Formatter ;
34use WP_CLI \Utils ;
45use WP_CLI \WpOrgApi ;
56
@@ -24,6 +25,13 @@ class Checksum_Core_Command extends Checksum_Base_Command {
2425 */
2526 private $ exclude_files = [];
2627
28+ /**
29+ * Array of detected errors.
30+ *
31+ * @var array
32+ */
33+ private $ errors = [];
34+
2735 /**
2836 * Verifies WordPress files against WordPress.org's checksums.
2937 *
@@ -54,6 +62,18 @@ class Checksum_Core_Command extends Checksum_Base_Command {
5462 * [--exclude=<files>]
5563 * : Exclude specific files from the checksum verification. Provide a comma-separated list of file paths.
5664 *
65+ * [--format=<format>]
66+ * : Render output in a specific format.
67+ * ---
68+ * default: table
69+ * options:
70+ * - table
71+ * - json
72+ * - csv
73+ * - yaml
74+ * - count
75+ * ---
76+ *
5777 * ## EXAMPLES
5878 *
5979 * # Verify checksums
@@ -79,6 +99,10 @@ class Checksum_Core_Command extends Checksum_Base_Command {
7999 * $ wp core verify-checksums --exclude="readme.html"
80100 * Success: WordPress installation verifies against checksums.
81101 *
102+ * # Verify checksums with formatted output
103+ * $ wp core verify-checksums --format=json
104+ * Success: WordPress installation verifies against checksums.
105+ *
82106 * @when before_wp_load
83107 */
84108 public function __invoke ( $ args , $ assoc_args ) {
@@ -137,14 +161,14 @@ public function __invoke( $args, $assoc_args ) {
137161 }
138162
139163 if ( ! file_exists ( ABSPATH . $ file ) ) {
140- WP_CLI :: warning ( "File doesn't exist: { $ file } " );
164+ $ this -> add_error ( $ file , "File doesn't exist " );
141165 $ has_errors = true ;
142166 continue ;
143167 }
144168
145169 $ md5_file = md5_file ( ABSPATH . $ file );
146170 if ( $ md5_file !== $ checksum ) {
147- WP_CLI :: warning ( "File doesn't verify against checksum: { $ file } " );
171+ $ this -> add_error ( $ file , "File doesn't verify against checksum " );
148172 $ has_errors = true ;
149173 }
150174 }
@@ -158,10 +182,18 @@ public function __invoke( $args, $assoc_args ) {
158182 if ( in_array ( $ additional_file , $ this ->exclude_files , true ) ) {
159183 continue ;
160184 }
161- WP_CLI :: warning ( " File should not exist: { $ additional_file }" );
185+ $ this -> add_error ( $ additional_file , ' File should not exist ' );
162186 }
163187 }
164188
189+ if ( ! empty ( $ this ->errors ) ) {
190+ $ formatter = new Formatter (
191+ $ assoc_args ,
192+ array ( 'file ' , 'message ' )
193+ );
194+ $ formatter ->display_items ( $ this ->errors );
195+ }
196+
165197 if ( ! $ has_errors ) {
166198 WP_CLI ::success ( 'WordPress installation verifies against checksums. ' );
167199 } else {
@@ -203,7 +235,7 @@ private static function get_wp_details() {
203235 if ( ! is_readable ( $ versions_path ) ) {
204236 WP_CLI ::error (
205237 "This does not seem to be a WordPress install. \n" .
206- 'Pass --path=`path/to/wordpress` or run `wp core download`. '
238+ 'Pass --path=`path/to/wordpress` or run `wp core download`. '
207239 );
208240 }
209241
@@ -245,4 +277,16 @@ private static function find_var( $var_name, $code ) {
245277
246278 return trim ( $ value , " ' " );
247279 }
280+
281+ /**
282+ * Adds a new error to the array of detected errors.
283+ *
284+ * @param string $file Relative path to the file that had the error.
285+ * @param string $message Message explaining the error.
286+ */
287+ private function add_error ( $ file , $ message ) {
288+ $ error ['file ' ] = $ file ;
289+ $ error ['message ' ] = $ message ;
290+ $ this ->errors [] = $ error ;
291+ }
248292}
0 commit comments