22
33namespace WP_CLI ;
44
5+ use WP_CLI ;
6+ use WP_CLI_Command ;
7+ use WP_CLI \Formatter ;
8+ use WP_CLI \Utils ;
9+
510/**
611 * Base class for WP-CLI commands that deal with database objects.
712 *
813 * @package wp-cli
914 */
10- abstract class CommandWithDBObject extends \ WP_CLI_Command {
15+ abstract class CommandWithDBObject extends WP_CLI_Command {
1116
1217 /**
1318 * @var string $object_type WordPress' expected name for the object.
@@ -38,13 +43,14 @@ protected function _create( $args, $assoc_args, $callback ) {
3843 $ obj_id = $ callback ( $ assoc_args );
3944
4045 if ( is_wp_error ( $ obj_id ) ) {
41- \ WP_CLI ::error ( $ obj_id );
46+ WP_CLI ::error ( $ obj_id );
4247 }
4348
44- if ( \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'porcelain ' ) )
45- \WP_CLI ::line ( $ obj_id );
46- else
47- \WP_CLI ::success ( "Created $ this ->obj_type $ obj_id. " );
49+ if ( Utils \get_flag_value ( $ assoc_args , 'porcelain ' ) ) {
50+ WP_CLI ::line ( $ obj_id );
51+ } else {
52+ WP_CLI ::success ( "Created $ this ->obj_type $ obj_id. " );
53+ }
4854 }
4955
5056 /**
@@ -59,23 +65,25 @@ protected function _update( $args, $assoc_args, $callback ) {
5965 $ status = 0 ;
6066
6167 if ( empty ( $ assoc_args ) ) {
62- \ WP_CLI ::error ( " Need some fields to update. " );
68+ WP_CLI ::error ( ' Need some fields to update. ' );
6369 }
6470
65- if ( \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
71+ if ( Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
6672 wp_defer_term_counting ( true );
6773 }
6874
6975 foreach ( $ args as $ obj_id ) {
70- $ params = array_merge ( $ assoc_args , array ( $ this ->obj_id_key => $ obj_id ) );
71-
72- $ status = $ this ->success_or_failure ( $ this ->wp_error_to_resp (
73- $ callback ( $ params ),
74- "Updated $ this ->obj_type $ obj_id. "
75- ) );
76+ $ params = array_merge ( $ assoc_args , [ $ this ->obj_id_key => $ obj_id ] );
77+
78+ $ status = $ this ->success_or_failure (
79+ $ this ->wp_error_to_resp (
80+ $ callback ( $ params ),
81+ "Updated $ this ->obj_type $ obj_id. "
82+ )
83+ );
7684 }
7785
78- if ( \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
86+ if ( Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
7987 wp_defer_term_counting ( false );
8088 }
8189
@@ -89,7 +97,7 @@ protected function _update( $args, $assoc_args, $callback ) {
8997 * @return array
9098 */
9199 protected static function process_csv_arguments_to_arrays ( $ assoc_args ) {
92- foreach ( $ assoc_args as $ k => $ v ) {
100+ foreach ( $ assoc_args as $ k => $ v ) {
93101 if ( false !== strpos ( $ k , '__ ' ) ) {
94102 $ assoc_args [ $ k ] = explode ( ', ' , $ v );
95103 }
@@ -108,16 +116,16 @@ protected static function process_csv_arguments_to_arrays( $assoc_args ) {
108116 protected function _delete ( $ args , $ assoc_args , $ callback ) {
109117 $ status = 0 ;
110118
111- if ( \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
119+ if ( Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
112120 wp_defer_term_counting ( true );
113121 }
114122
115123 foreach ( $ args as $ obj_id ) {
116- $ r = $ callback ( $ obj_id , $ assoc_args );
124+ $ r = $ callback ( $ obj_id , $ assoc_args );
117125 $ status = $ this ->success_or_failure ( $ r );
118126 }
119127
120- if ( \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
128+ if ( Utils \get_flag_value ( $ assoc_args , 'defer-term-counting ' ) ) {
121129 wp_defer_term_counting ( false );
122130 }
123131
@@ -132,10 +140,11 @@ protected function _delete( $args, $assoc_args, $callback ) {
132140 * @return array
133141 */
134142 protected function wp_error_to_resp ( $ r , $ success_msg ) {
135- if ( is_wp_error ( $ r ) )
136- return array ( 'error ' , $ r ->get_error_message () );
137- else
138- return array ( 'success ' , $ success_msg );
143+ if ( is_wp_error ( $ r ) ) {
144+ return [ 'error ' , $ r ->get_error_message () ];
145+ } else {
146+ return [ 'success ' , $ success_msg ];
147+ }
139148 }
140149
141150 /**
@@ -147,11 +156,11 @@ protected function wp_error_to_resp( $r, $success_msg ) {
147156 protected function success_or_failure ( $ r ) {
148157 list ( $ type , $ msg ) = $ r ;
149158
150- if ( 'success ' == $ type ) {
151- \ WP_CLI ::success ( $ msg );
159+ if ( 'success ' === $ type ) {
160+ WP_CLI ::success ( $ msg );
152161 $ status = 0 ;
153162 } else {
154- \ WP_CLI ::warning ( $ msg );
163+ WP_CLI ::warning ( $ msg );
155164 $ status = 1 ;
156165 }
157166
@@ -175,7 +184,7 @@ protected function get_formatter( &$assoc_args ) {
175184 } else {
176185 $ fields = $ this ->obj_fields ;
177186 }
178- return new \ WP_CLI \ Formatter ( $ assoc_args , $ fields , $ this ->obj_type );
187+ return new Formatter ( $ assoc_args , $ fields , $ this ->obj_type );
179188 }
180189
181190 /**
@@ -187,7 +196,7 @@ protected function get_formatter( &$assoc_args ) {
187196 protected function _url ( $ args , $ callback ) {
188197 foreach ( $ args as $ obj_id ) {
189198 $ object = $ this ->fetcher ->get_check ( $ obj_id );
190- \ WP_CLI ::line ( $ callback ( $ object ->{$ this ->obj_id_key } ) );
199+ WP_CLI ::line ( $ callback ( $ object ->{$ this ->obj_id_key } ) );
191200 }
192201 }
193202}
0 commit comments