@@ -10,6 +10,7 @@ class Search_Replace_Command extends WP_CLI_Command {
1010 private $ regex_flags ;
1111 private $ skip_columns ;
1212 private $ include_columns ;
13+ private $ format ;
1314
1415 /**
1516 * Search/replace strings in the database.
@@ -88,6 +89,15 @@ class Search_Replace_Command extends WP_CLI_Command {
8889 * [--regex-flags=<regex-flags>]
8990 * : Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity).
9091 *
92+ * [--format=<format>]
93+ * : Render output in a particular format.
94+ * ---
95+ * default: table
96+ * options:
97+ * - table
98+ * - count
99+ * ---
100+ *
91101 * ## EXAMPLES
92102 *
93103 * # Search and replace but skip one column
@@ -125,6 +135,7 @@ public function __invoke( $args, $assoc_args ) {
125135 $ this ->verbose = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'verbose ' );
126136 $ this ->regex = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'regex ' );
127137 $ this ->regex_flags = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'regex-flags ' );
138+ $ this ->format = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'format ' );
128139
129140 $ this ->skip_columns = explode ( ', ' , \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'skip-columns ' ) );
130141 $ this ->include_columns = array_filter ( explode ( ', ' , \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'include-columns ' ) ) );
@@ -194,7 +205,7 @@ public function __invoke( $args, $assoc_args ) {
194205 continue ;
195206 }
196207
197- if ( $ this ->verbose ) {
208+ if ( $ this ->verbose && ' count ' !== $ this -> format ) {
198209 $ this ->start_time = microtime ( true );
199210 WP_CLI ::log ( sprintf ( 'Checking: %s.%s ' , $ table , $ col ) );
200211 }
@@ -231,6 +242,11 @@ public function __invoke( $args, $assoc_args ) {
231242 return ;
232243 }
233244
245+ if ( 'count ' === $ this ->format ) {
246+ WP_CLI ::line ( $ total );
247+ return ;
248+ }
249+
234250 $ table = new \cli \Table ();
235251 $ table ->setHeaders ( array ( 'Table ' , 'Column ' , 'Replacements ' , 'Type ' ) );
236252 $ table ->setRows ( $ report );
@@ -264,7 +280,7 @@ private function php_export_table( $table, $old, $new ) {
264280
265281 $ replacer = new \WP_CLI \SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags );
266282 $ col_counts = array_fill_keys ( $ all_columns , 0 );
267- if ( $ this ->verbose ) {
283+ if ( $ this ->verbose && ' table ' === $ this -> format ) {
268284 $ this ->start_time = microtime ( true );
269285 WP_CLI ::log ( sprintf ( 'Checking: %s ' , $ table ) );
270286 }
@@ -297,7 +313,7 @@ private function php_export_table( $table, $old, $new ) {
297313 }
298314 }
299315
300- if ( $ this ->verbose ) {
316+ if ( $ this ->verbose && ' table ' === $ this -> format ) {
301317 $ time = round ( microtime ( true ) - $ this ->start_time , 3 );
302318 WP_CLI ::log ( sprintf ( '%d columns and %d total rows affected using PHP (in %ss). ' , $ total_cols , $ total_rows , $ time ) );
303319 }
@@ -314,7 +330,7 @@ private function sql_handle_col( $col, $table, $old, $new ) {
314330 $ count = $ wpdb ->query ( $ wpdb ->prepare ( "UPDATE ` $ table` SET ` $ col` = REPLACE(` $ col`, %s, %s); " , $ old , $ new ) );
315331 }
316332
317- if ( $ this ->verbose ) {
333+ if ( $ this ->verbose && ' table ' === $ this -> format ) {
318334 $ time = round ( microtime ( true ) - $ this ->start_time , 3 );
319335 WP_CLI ::log ( sprintf ( '%d rows affected using SQL (in %ss). ' , $ count , $ time ) );
320336 }
@@ -362,7 +378,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
362378 }
363379 }
364380
365- if ( $ this ->verbose ) {
381+ if ( $ this ->verbose && ' table ' === $ this -> format ) {
366382 $ time = round ( microtime ( true ) - $ this ->start_time , 3 );
367383 WP_CLI ::log ( sprintf ( '%d rows affected using PHP (in %ss). ' , $ count , $ time ) );
368384 }
0 commit comments