Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_CLI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_CLI, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Jetpack_CLI extends WP_CLI_Command { |
||
9 | |||
10 | // Aesthetics |
||
11 | public $green_open = "\033[32m"; |
||
12 | public $red_open = "\033[31m"; |
||
13 | public $yellow_open = "\033[33m"; |
||
14 | public $color_close = "\033[0m"; |
||
15 | |||
16 | /** |
||
17 | * Get Jetpack Details |
||
18 | * |
||
19 | * ## OPTIONS |
||
20 | * |
||
21 | * empty: Leave it empty for basic stats |
||
22 | * |
||
23 | * full: View full stats. It's the data from the heartbeat |
||
24 | * |
||
25 | * ## EXAMPLES |
||
26 | * |
||
27 | * wp jetpack status |
||
28 | * wp jetpack status full |
||
29 | * |
||
30 | */ |
||
31 | public function status( $args, $assoc_args ) { |
||
32 | if ( ! Jetpack::is_active() ) { |
||
33 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
||
34 | } |
||
35 | |||
36 | View Code Duplication | if ( isset( $args[0] ) && 'full' !== $args[0] ) { |
|
37 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) ); |
||
38 | } |
||
39 | |||
40 | /* |
||
41 | * Are they asking for all data? |
||
42 | * |
||
43 | * Loop through heartbeat data and organize by priority. |
||
44 | */ |
||
45 | $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false; |
||
46 | if ( $all_data ) { |
||
|
|||
47 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
48 | WP_CLI::line( sprintf( __( "The Jetpack Version is %s", 'jetpack' ), JETPACK__VERSION ) ); |
||
49 | WP_CLI::line( sprintf( __( "The WordPress.com blog_id is %d", 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
50 | |||
51 | // Heartbeat data |
||
52 | WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) ); |
||
53 | |||
54 | // Get the filtered heartbeat data. |
||
55 | // Filtered so we can color/list by severity |
||
56 | $stats = Jetpack::jetpack_check_heartbeat_data(); |
||
57 | |||
58 | // Display red flags first |
||
59 | foreach ( $stats['bad'] as $stat => $value ) { |
||
60 | printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
61 | } |
||
62 | |||
63 | // Display caution warnings next |
||
64 | foreach ( $stats['caution'] as $stat => $value ) { |
||
65 | printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
66 | } |
||
67 | |||
68 | // The rest of the results are good! |
||
69 | foreach ( $stats['good'] as $stat => $value ) { |
||
70 | |||
71 | // Modules should get special spacing for aestetics |
||
72 | if ( strpos( $stat, 'odule-' ) ) { |
||
73 | printf( "%-'.30s %s\n", $stat, $value ); |
||
74 | usleep( 4000 ); // For dramatic effect lolz |
||
75 | continue; |
||
76 | } |
||
77 | printf( "%-'.16s %s\n", $stat, $value ); |
||
78 | usleep( 4000 ); // For dramatic effect lolz |
||
79 | } |
||
80 | } else { |
||
81 | // Just the basics |
||
82 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
83 | WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) ); |
||
84 | WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
85 | WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Disconnect Jetpack Blogs or Users |
||
91 | * |
||
92 | * ## OPTIONS |
||
93 | * |
||
94 | * blog: Disconnect the entire blog. |
||
95 | * |
||
96 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
97 | * |
||
98 | * Please note, the primary account that the blog is connected |
||
99 | * to WordPress.com with cannot be disconnected without |
||
100 | * disconnecting the entire blog. |
||
101 | * |
||
102 | * ## EXAMPLES |
||
103 | * |
||
104 | * wp jetpack disconnect blog |
||
105 | * wp jetpack disconnect user 13 |
||
106 | * wp jetpack disconnect user username |
||
107 | * wp jetpack disconnect user [email protected] |
||
108 | * |
||
109 | * @synopsis <blog|user> [<user_identifier>] |
||
110 | */ |
||
111 | public function disconnect( $args, $assoc_args ) { |
||
161 | |||
162 | /** |
||
163 | * Reset Jetpack options and settings to default |
||
164 | * |
||
165 | * ## OPTIONS |
||
166 | * |
||
167 | * modules: Resets modules to default state ( get_default_modules() ) |
||
168 | * |
||
169 | * options: Resets all Jetpack options except: |
||
170 | * - All private options (Blog token, user token, etc...) |
||
171 | * - id (The Client ID/WP.com Blog ID of this site) |
||
172 | * - master_user |
||
173 | * - version |
||
174 | * - activated |
||
175 | * |
||
176 | * ## EXAMPLES |
||
177 | * |
||
178 | * wp jetpack reset options |
||
179 | * wp jetpack reset modules |
||
180 | * |
||
181 | * @synopsis <modules|options> |
||
182 | */ |
||
183 | public function reset( $args, $assoc_args ) { |
||
184 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
185 | View Code Duplication | if ( ! in_array( $action, array( 'options', 'modules' ) ) ) { |
|
186 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
187 | } |
||
188 | |||
189 | // Are you sure? |
||
190 | jetpack_cli_are_you_sure(); |
||
191 | |||
192 | switch ( $action ) { |
||
193 | case 'options': |
||
194 | $options_to_reset = Jetpack::get_jetpack_options_for_reset(); |
||
195 | |||
196 | // Reset the Jetpack options |
||
197 | _e( "Resetting Jetpack Options...\n", "jetpack" ); |
||
198 | sleep(1); // Take a breath |
||
199 | foreach ( $options_to_reset['jp_options'] as $option_to_reset ) { |
||
200 | Jetpack_Options::delete_option( $option_to_reset ); |
||
201 | usleep( 100000 ); |
||
202 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
||
203 | } |
||
204 | |||
205 | // Reset the WP options |
||
206 | _e( "Resetting the jetpack options stored in wp_options...\n", "jetpack" ); |
||
207 | usleep( 500000 ); // Take a breath |
||
208 | foreach ( $options_to_reset['wp_options'] as $option_to_reset ) { |
||
209 | delete_option( $option_to_reset ); |
||
210 | usleep( 100000 ); |
||
211 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
||
212 | } |
||
213 | |||
214 | // Reset to default modules |
||
215 | _e( "Resetting default modules...\n", "jetpack" ); |
||
216 | usleep( 500000 ); // Take a breath |
||
217 | $default_modules = Jetpack::get_default_modules(); |
||
218 | Jetpack_Options::update_option( 'active_modules', $default_modules ); |
||
219 | WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); |
||
220 | |||
221 | // Jumpstart option is special |
||
222 | Jetpack_Options::update_option( 'jumpstart', 'new_connection' ); |
||
223 | WP_CLI::success( __( 'jumpstart option reset', 'jetpack' ) ); |
||
224 | break; |
||
225 | View Code Duplication | case 'modules': |
|
226 | $default_modules = Jetpack::get_default_modules(); |
||
227 | Jetpack_Options::update_option( 'active_modules', $default_modules ); |
||
228 | WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) ); |
||
229 | break; |
||
230 | case 'prompt': |
||
231 | WP_CLI::error( __( 'Please specify if you would like to reset your options, or modules', 'jetpack' ) ); |
||
232 | break; |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Manage Jetpack Modules |
||
238 | * |
||
239 | * ## OPTIONS |
||
240 | * |
||
241 | * list : View all available modules, and their status. |
||
242 | * activate all : Activate all modules |
||
243 | * deactivate all: Deactivate all modules |
||
244 | * |
||
245 | * activate <module_slug> : Activate a module. |
||
246 | * deactivate <module_slug> : Deactivate a module. |
||
247 | * toggle <module_slug> : Toggle a module on or off. |
||
248 | * |
||
249 | * ## EXAMPLES |
||
250 | * |
||
251 | * wp jetpack module list |
||
252 | * wp jetpack module activate stats |
||
253 | * wp jetpack module deactivate stats |
||
254 | * wp jetpack module toggle stats |
||
255 | * |
||
256 | * wp jetpack module activate all |
||
257 | * wp jetpack module deactivate all |
||
258 | * |
||
259 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
260 | */ |
||
261 | public function module( $args, $assoc_args ) { |
||
262 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
263 | if ( ! in_array( $action, array( 'list', 'activate', 'deactivate', 'toggle' ) ) ) { |
||
264 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
265 | } |
||
266 | if ( in_array( $action, array( 'activate', 'deactivate', 'toggle' ) ) ) { |
||
267 | if ( isset( $args[1] ) ) { |
||
268 | $module_slug = $args[1]; |
||
269 | if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) { |
||
270 | WP_CLI::error( sprintf( __( '%s is not a valid module.', 'jetpack' ), $module_slug ) ); |
||
271 | } |
||
272 | if ( 'toggle' == $action ) { |
||
273 | $action = Jetpack::is_module_active( $module_slug ) ? 'deactivate' : 'activate'; |
||
274 | } |
||
275 | // Bulk actions |
||
276 | if ( 'all' == $args[1] ) { |
||
277 | $action = ( 'deactivate' == $action ) ? 'deactivate_all' : 'activate_all'; |
||
278 | } |
||
279 | // VaultPress needs to be handled elsewhere. |
||
280 | if ( in_array( $action, array( 'activate', 'deactivate', 'toggle' ) ) && 'vaultpress' == $args[1] ) { |
||
281 | WP_CLI::error( sprintf( _x( 'Please visit %s to configure your VaultPress subscription.', '%s is a website', 'jetpack' ), esc_url( 'https://vaultpress.com/jetpack/' ) ) ); |
||
282 | } |
||
283 | } else { |
||
284 | WP_CLI::line( __( 'Please specify a valid module.', 'jetpack' ) ); |
||
285 | $action = 'list'; |
||
286 | } |
||
287 | } |
||
288 | switch ( $action ) { |
||
289 | case 'list': |
||
290 | WP_CLI::line( __( 'Available Modules:', 'jetpack' ) ); |
||
291 | $modules = Jetpack::get_available_modules(); |
||
292 | sort( $modules ); |
||
293 | foreach( $modules as $module_slug ) { |
||
294 | if ( 'vaultpress' == $module_slug ) { |
||
295 | continue; |
||
296 | } |
||
297 | $active = Jetpack::is_module_active( $module_slug ) ? __( 'Active', 'jetpack' ) : __( 'Inactive', 'jetpack' ); |
||
298 | WP_CLI::line( "\t" . str_pad( $module_slug, 24 ) . $active ); |
||
299 | } |
||
300 | break; |
||
301 | View Code Duplication | case 'activate': |
|
302 | $module = Jetpack::get_module( $module_slug ); |
||
303 | Jetpack::log( 'activate', $module_slug ); |
||
304 | Jetpack::activate_module( $module_slug, false, false ); |
||
305 | WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) ); |
||
306 | break; |
||
307 | View Code Duplication | case 'activate_all': |
|
308 | $modules = Jetpack::get_available_modules(); |
||
309 | Jetpack_Options::update_option( 'active_modules', $modules ); |
||
310 | WP_CLI::success( __( 'All modules activated!', 'jetpack' ) ); |
||
311 | break; |
||
312 | View Code Duplication | case 'deactivate': |
|
313 | $module = Jetpack::get_module( $module_slug ); |
||
314 | Jetpack::log( 'deactivate', $module_slug ); |
||
315 | Jetpack::deactivate_module( $module_slug ); |
||
316 | WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) ); |
||
317 | break; |
||
318 | case 'deactivate_all': |
||
319 | Jetpack_Options::update_option( 'active_modules', '' ); |
||
320 | WP_CLI::success( __( 'All modules deactivated!', 'jetpack' ) ); |
||
321 | break; |
||
322 | case 'toggle': |
||
323 | // Will never happen, should have been handled above and changed to activate or deactivate. |
||
324 | break; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Manage Protect Settings |
||
330 | * |
||
331 | * ## OPTIONS |
||
332 | * |
||
333 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
334 | * |
||
335 | * |
||
336 | * ## EXAMPLES |
||
337 | * |
||
338 | * wp jetpack protect whitelist <ip address> |
||
339 | * wp jetpack protect whitelist list |
||
340 | * wp jetpack protect whitelist clear |
||
341 | * |
||
342 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
343 | */ |
||
344 | public function protect( $args, $assoc_args ) { |
||
345 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
346 | if ( ! in_array( $action, array( 'whitelist' ) ) ) { |
||
347 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
348 | } |
||
349 | // Check if module is active |
||
350 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
||
351 | WP_CLI::error( sprintf( _x( '%s is not active. You can activate it with "wp jetpack module activate %s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) ); |
||
352 | } |
||
353 | if ( in_array( $action, array( 'whitelist' ) ) ) { |
||
354 | if ( isset( $args[1] ) ) { |
||
355 | $action = 'whitelist'; |
||
356 | } else { |
||
357 | $action = 'prompt'; |
||
358 | } |
||
359 | } |
||
360 | switch ( $action ) { |
||
361 | case 'whitelist': |
||
362 | $whitelist = array(); |
||
363 | $new_ip = $args[1]; |
||
364 | $current_whitelist = get_site_option( 'jetpack_protect_whitelist' ); |
||
365 | |||
366 | // Build array of IPs that are already whitelisted. |
||
367 | // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get |
||
368 | // low & high range params for jetpack_protect_ip_address_is_in_range(); |
||
369 | foreach( $current_whitelist as $whitelisted ) { |
||
370 | |||
371 | // IP ranges |
||
372 | if ( $whitelisted->range ) { |
||
373 | |||
374 | // Is it already whitelisted? |
||
375 | if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { |
||
376 | WP_CLI::error( sprintf( __( "%s has already been whitelisted", 'jetpack' ), $new_ip ) ); |
||
377 | break; |
||
378 | } |
||
379 | $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; |
||
380 | |||
381 | } else { // Individual IPs |
||
382 | |||
383 | // Check if the IP is already whitelisted (single IP only) |
||
384 | if ( $new_ip == $whitelisted->ip_address ) { |
||
385 | WP_CLI::error( sprintf( __( "%s has already been whitelisted", 'jetpack' ), $new_ip ) ); |
||
386 | break; |
||
387 | } |
||
388 | $whitelist[] = $whitelisted->ip_address; |
||
389 | |||
390 | } |
||
391 | } |
||
392 | |||
393 | /* |
||
394 | * List the whitelist |
||
395 | * Done here because it's easier to read the $whitelist array after it's been rebuilt |
||
396 | */ |
||
397 | if ( isset( $args[1] ) && 'list' == $args[1] ) { |
||
398 | if ( ! empty( $whitelist ) ) { |
||
399 | WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); |
||
400 | foreach ( $whitelist as $ip ) { |
||
401 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; |
||
402 | } |
||
403 | } else { |
||
404 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
405 | } |
||
406 | break; |
||
407 | } |
||
408 | |||
409 | /* |
||
410 | * Clear the whitelist |
||
411 | */ |
||
412 | if ( isset( $args[1] ) && 'clear' == $args[1] ) { |
||
413 | if ( ! empty( $whitelist ) ) { |
||
414 | $whitelist = array(); |
||
415 | jetpack_protect_save_whitelist( $whitelist ); |
||
416 | WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); |
||
417 | } else { |
||
418 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
419 | } |
||
420 | break; |
||
421 | } |
||
422 | |||
423 | // Append new IP to whitelist array |
||
424 | array_push( $whitelist, $new_ip ); |
||
425 | |||
426 | // Save whitelist if there are no errors |
||
427 | $result = jetpack_protect_save_whitelist( $whitelist ); |
||
428 | if ( is_wp_error( $result ) ) { |
||
429 | WP_CLI::error( __( $result, 'jetpack' ) ); |
||
430 | } |
||
431 | |||
432 | WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); |
||
433 | break; |
||
434 | case 'prompt': |
||
435 | WP_CLI::error( |
||
436 | __( 'No command found.', 'jetpack' ) . "\n" . |
||
437 | __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . |
||
438 | _x( 'You can save a range of IPs {low_range}-{high_range}. No spaces allowed. (example: 1.1.1.1-2.2.2.2)', 'Instructions on how to whitelist IP ranges - low_range/high_range should be translated.', 'jetpack' ) . "\n" . |
||
439 | _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
||
440 | ); |
||
441 | break; |
||
442 | } |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Manage Jetpack Options |
||
447 | * |
||
448 | * ## OPTIONS |
||
449 | * |
||
450 | * list : List all jetpack options and their values |
||
451 | * delete : Delete an option |
||
452 | * - can only delete options that are white listed. |
||
453 | * update : update an option |
||
454 | * - can only update option strings |
||
455 | * get : get the value of an option |
||
456 | * |
||
457 | * ## EXAMPLES |
||
458 | * |
||
459 | * wp jetpack options list |
||
460 | * wp jetpack options get <option_name> |
||
461 | * wp jetpack options delete <option_name> |
||
462 | * wp jetpack options update <option_name> [<option_value>] |
||
463 | * |
||
464 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
465 | */ |
||
466 | public function options( $args, $assoc_args ) { |
||
562 | } |
||
563 | |||
564 | /* |
||
565 | * Standard "ask for permission to continue" function. |
||
566 | * If action cancelled, ask if they need help. |
||
599 | } |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: