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,19 @@ 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. When provided, messages are displayed in the chosen format.
67+ * ---
68+ * default: plain
69+ * options:
70+ * - plain
71+ * - table
72+ * - json
73+ * - csv
74+ * - yaml
75+ * - count
76+ * ---
77+ *
5778 * ## EXAMPLES
5879 *
5980 * # Verify checksums
@@ -79,6 +100,11 @@ class Checksum_Core_Command extends Checksum_Base_Command {
79100 * $ wp core verify-checksums --exclude="readme.html"
80101 * Success: WordPress installation verifies against checksums.
81102 *
103+ * # Verify checksums with formatted output
104+ * $ wp core verify-checksums --format=json
105+ * [{"file":"readme.html","message":"File doesn't verify against checksum"}]
106+ * Error: WordPress installation doesn't verify against checksums.
107+ *
82108 * @when before_wp_load
83109 */
84110 public function __invoke ( $ args , $ assoc_args ) {
@@ -137,14 +163,23 @@ public function __invoke( $args, $assoc_args ) {
137163 }
138164
139165 if ( ! file_exists ( ABSPATH . $ file ) ) {
140- WP_CLI ::warning ( "File doesn't exist: {$ file }" );
166+ $ this ->errors [] = [
167+ 'file ' => $ file ,
168+ 'message ' => "File doesn't exist " ,
169+ ];
170+
141171 $ has_errors = true ;
172+
142173 continue ;
143174 }
144175
145176 $ md5_file = md5_file ( ABSPATH . $ file );
146- if ( $ md5_file !== $ checksum ) {
147- WP_CLI ::warning ( "File doesn't verify against checksum: {$ file }" );
177+ if ( $ checksum !== $ md5_file ) {
178+ $ this ->errors [] = [
179+ 'file ' => $ file ,
180+ 'message ' => "File doesn't verify against checksum " ,
181+ ];
182+
148183 $ has_errors = true ;
149184 }
150185 }
@@ -158,7 +193,25 @@ public function __invoke( $args, $assoc_args ) {
158193 if ( in_array ( $ additional_file , $ this ->exclude_files , true ) ) {
159194 continue ;
160195 }
161- WP_CLI ::warning ( "File should not exist: {$ additional_file }" );
196+
197+ $ this ->errors [] = [
198+ 'file ' => $ additional_file ,
199+ 'message ' => 'File should not exist ' ,
200+ ];
201+ }
202+ }
203+
204+ if ( ! empty ( $ this ->errors ) ) {
205+ if ( ! isset ( $ assoc_args ['format ' ] ) || 'plain ' === $ assoc_args ['format ' ] ) {
206+ foreach ( $ this ->errors as $ error ) {
207+ WP_CLI ::warning ( sprintf ( '%s: %s ' , $ error ['message ' ], $ error ['file ' ] ) );
208+ }
209+ } else {
210+ $ formatter = new Formatter (
211+ $ assoc_args ,
212+ array ( 'file ' , 'message ' )
213+ );
214+ $ formatter ->display_items ( $ this ->errors );
162215 }
163216 }
164217
@@ -234,7 +287,7 @@ private static function get_wp_details() {
234287 private static function find_var ( $ var_name , $ code ) {
235288 $ start = strpos ( $ code , '$ ' . $ var_name . ' = ' );
236289
237- if ( ! $ start ) {
290+ if ( false === $ start ) {
238291 return null ;
239292 }
240293
0 commit comments