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 | /* translators: %s is a command like "prompt" */ |
||
| 38 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) ); |
||
| 39 | } |
||
| 40 | |||
| 41 | /* |
||
| 42 | * Are they asking for all data? |
||
| 43 | * |
||
| 44 | * Loop through heartbeat data and organize by priority. |
||
| 45 | */ |
||
| 46 | $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false; |
||
| 47 | if ( $all_data ) { |
||
|
|
|||
| 48 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
| 49 | WP_CLI::line( sprintf( __( "The Jetpack Version is %s", 'jetpack' ), JETPACK__VERSION ) ); |
||
| 50 | WP_CLI::line( sprintf( __( "The WordPress.com blog_id is %d", 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
| 51 | |||
| 52 | // Heartbeat data |
||
| 53 | WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) ); |
||
| 54 | |||
| 55 | // Get the filtered heartbeat data. |
||
| 56 | // Filtered so we can color/list by severity |
||
| 57 | $stats = Jetpack::jetpack_check_heartbeat_data(); |
||
| 58 | |||
| 59 | // Display red flags first |
||
| 60 | foreach ( $stats['bad'] as $stat => $value ) { |
||
| 61 | printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Display caution warnings next |
||
| 65 | foreach ( $stats['caution'] as $stat => $value ) { |
||
| 66 | printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); |
||
| 67 | } |
||
| 68 | |||
| 69 | // The rest of the results are good! |
||
| 70 | foreach ( $stats['good'] as $stat => $value ) { |
||
| 71 | |||
| 72 | // Modules should get special spacing for aestetics |
||
| 73 | if ( strpos( $stat, 'odule-' ) ) { |
||
| 74 | printf( "%-'.30s %s\n", $stat, $value ); |
||
| 75 | usleep( 4000 ); // For dramatic effect lolz |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | printf( "%-'.16s %s\n", $stat, $value ); |
||
| 79 | usleep( 4000 ); // For dramatic effect lolz |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | // Just the basics |
||
| 83 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
||
| 84 | WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) ); |
||
| 85 | WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) ); |
||
| 86 | WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Disconnect Jetpack Blogs or Users |
||
| 92 | * |
||
| 93 | * ## OPTIONS |
||
| 94 | * |
||
| 95 | * blog: Disconnect the entire blog. |
||
| 96 | * |
||
| 97 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
||
| 98 | * |
||
| 99 | * Please note, the primary account that the blog is connected |
||
| 100 | * to WordPress.com with cannot be disconnected without |
||
| 101 | * disconnecting the entire blog. |
||
| 102 | * |
||
| 103 | * ## EXAMPLES |
||
| 104 | * |
||
| 105 | * wp jetpack disconnect blog |
||
| 106 | * wp jetpack disconnect user 13 |
||
| 107 | * wp jetpack disconnect user username |
||
| 108 | * wp jetpack disconnect user [email protected] |
||
| 109 | * |
||
| 110 | * @synopsis <blog|user> [<user_identifier>] |
||
| 111 | */ |
||
| 112 | public function disconnect( $args, $assoc_args ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Reset Jetpack options and settings to default |
||
| 167 | * |
||
| 168 | * ## OPTIONS |
||
| 169 | * |
||
| 170 | * modules: Resets modules to default state ( get_default_modules() ) |
||
| 171 | * |
||
| 172 | * options: Resets all Jetpack options except: |
||
| 173 | * - All private options (Blog token, user token, etc...) |
||
| 174 | * - id (The Client ID/WP.com Blog ID of this site) |
||
| 175 | * - master_user |
||
| 176 | * - version |
||
| 177 | * - activated |
||
| 178 | * |
||
| 179 | * ## EXAMPLES |
||
| 180 | * |
||
| 181 | * wp jetpack reset options |
||
| 182 | * wp jetpack reset modules |
||
| 183 | * |
||
| 184 | * @synopsis <modules|options> |
||
| 185 | */ |
||
| 186 | public function reset( $args, $assoc_args ) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Manage Jetpack Modules |
||
| 244 | * |
||
| 245 | * ## OPTIONS |
||
| 246 | * |
||
| 247 | * list : View all available modules, and their status. |
||
| 248 | * activate all : Activate all modules |
||
| 249 | * deactivate all: Deactivate all modules |
||
| 250 | * |
||
| 251 | * activate <module_slug> : Activate a module. |
||
| 252 | * deactivate <module_slug> : Deactivate a module. |
||
| 253 | * toggle <module_slug> : Toggle a module on or off. |
||
| 254 | * |
||
| 255 | * ## EXAMPLES |
||
| 256 | * |
||
| 257 | * wp jetpack module list |
||
| 258 | * wp jetpack module activate stats |
||
| 259 | * wp jetpack module deactivate stats |
||
| 260 | * wp jetpack module toggle stats |
||
| 261 | * |
||
| 262 | * wp jetpack module activate all |
||
| 263 | * wp jetpack module deactivate all |
||
| 264 | * |
||
| 265 | * @synopsis <list|activate|deactivate|toggle> [<module_name>] |
||
| 266 | */ |
||
| 267 | public function module( $args, $assoc_args ) { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Manage Protect Settings |
||
| 337 | * |
||
| 338 | * ## OPTIONS |
||
| 339 | * |
||
| 340 | * whitelist: Whitelist an IP address. You can also read or clear the whitelist. |
||
| 341 | * |
||
| 342 | * |
||
| 343 | * ## EXAMPLES |
||
| 344 | * |
||
| 345 | * wp jetpack protect whitelist <ip address> |
||
| 346 | * wp jetpack protect whitelist list |
||
| 347 | * wp jetpack protect whitelist clear |
||
| 348 | * |
||
| 349 | * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>] |
||
| 350 | */ |
||
| 351 | public function protect( $args, $assoc_args ) { |
||
| 352 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
||
| 353 | if ( ! in_array( $action, array( 'whitelist' ) ) ) { |
||
| 354 | /* translators: %s is a command like "prompt" */ |
||
| 355 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 356 | } |
||
| 357 | // Check if module is active |
||
| 358 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
||
| 359 | 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__ ) ); |
||
| 360 | } |
||
| 361 | if ( in_array( $action, array( 'whitelist' ) ) ) { |
||
| 362 | if ( isset( $args[1] ) ) { |
||
| 363 | $action = 'whitelist'; |
||
| 364 | } else { |
||
| 365 | $action = 'prompt'; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | switch ( $action ) { |
||
| 369 | case 'whitelist': |
||
| 370 | $whitelist = array(); |
||
| 371 | $new_ip = $args[1]; |
||
| 372 | $current_whitelist = get_site_option( 'jetpack_protect_whitelist' ); |
||
| 373 | |||
| 374 | // Build array of IPs that are already whitelisted. |
||
| 375 | // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get |
||
| 376 | // low & high range params for jetpack_protect_ip_address_is_in_range(); |
||
| 377 | foreach( $current_whitelist as $whitelisted ) { |
||
| 378 | |||
| 379 | // IP ranges |
||
| 380 | if ( $whitelisted->range ) { |
||
| 381 | |||
| 382 | // Is it already whitelisted? |
||
| 383 | if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) { |
||
| 384 | /* translators: %s is an IP address */ |
||
| 385 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 386 | break; |
||
| 387 | } |
||
| 388 | $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high; |
||
| 389 | |||
| 390 | } else { // Individual IPs |
||
| 391 | |||
| 392 | // Check if the IP is already whitelisted (single IP only) |
||
| 393 | if ( $new_ip == $whitelisted->ip_address ) { |
||
| 394 | /* translators: %s is an IP address */ |
||
| 395 | WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) ); |
||
| 396 | break; |
||
| 397 | } |
||
| 398 | $whitelist[] = $whitelisted->ip_address; |
||
| 399 | |||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | /* |
||
| 404 | * List the whitelist |
||
| 405 | * Done here because it's easier to read the $whitelist array after it's been rebuilt |
||
| 406 | */ |
||
| 407 | if ( isset( $args[1] ) && 'list' == $args[1] ) { |
||
| 408 | if ( ! empty( $whitelist ) ) { |
||
| 409 | WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) ); |
||
| 410 | foreach ( $whitelist as $ip ) { |
||
| 411 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ; |
||
| 412 | } |
||
| 413 | } else { |
||
| 414 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 415 | } |
||
| 416 | break; |
||
| 417 | } |
||
| 418 | |||
| 419 | /* |
||
| 420 | * Clear the whitelist |
||
| 421 | */ |
||
| 422 | if ( isset( $args[1] ) && 'clear' == $args[1] ) { |
||
| 423 | if ( ! empty( $whitelist ) ) { |
||
| 424 | $whitelist = array(); |
||
| 425 | jetpack_protect_save_whitelist( $whitelist ); |
||
| 426 | WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) ); |
||
| 427 | } else { |
||
| 428 | WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ; |
||
| 429 | } |
||
| 430 | break; |
||
| 431 | } |
||
| 432 | |||
| 433 | // Append new IP to whitelist array |
||
| 434 | array_push( $whitelist, $new_ip ); |
||
| 435 | |||
| 436 | // Save whitelist if there are no errors |
||
| 437 | $result = jetpack_protect_save_whitelist( $whitelist ); |
||
| 438 | if ( is_wp_error( $result ) ) { |
||
| 439 | WP_CLI::error( __( $result, 'jetpack' ) ); |
||
| 440 | } |
||
| 441 | |||
| 442 | /* translators: %s is an IP address */ |
||
| 443 | WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) ); |
||
| 444 | break; |
||
| 445 | case 'prompt': |
||
| 446 | WP_CLI::error( |
||
| 447 | __( 'No command found.', 'jetpack' ) . "\n" . |
||
| 448 | __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" . |
||
| 449 | _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" . |
||
| 450 | _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
||
| 451 | ); |
||
| 452 | break; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Manage Jetpack Options |
||
| 458 | * |
||
| 459 | * ## OPTIONS |
||
| 460 | * |
||
| 461 | * list : List all jetpack options and their values |
||
| 462 | * delete : Delete an option |
||
| 463 | * - can only delete options that are white listed. |
||
| 464 | * update : update an option |
||
| 465 | * - can only update option strings |
||
| 466 | * get : get the value of an option |
||
| 467 | * |
||
| 468 | * ## EXAMPLES |
||
| 469 | * |
||
| 470 | * wp jetpack options list |
||
| 471 | * wp jetpack options get <option_name> |
||
| 472 | * wp jetpack options delete <option_name> |
||
| 473 | * wp jetpack options update <option_name> [<option_value>] |
||
| 474 | * |
||
| 475 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
||
| 476 | */ |
||
| 477 | public function options( $args, $assoc_args ) { |
||
| 478 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
||
| 479 | $safe_to_modify = Jetpack::get_jetpack_options_for_reset(); |
||
| 480 | |||
| 481 | // Jumpstart is special |
||
| 482 | array_push( $safe_to_modify, 'jumpstart' ); |
||
| 483 | |||
| 484 | // Is the option flagged as unsafe? |
||
| 485 | $flagged = ! in_array( $args[1], $safe_to_modify ); |
||
| 486 | |||
| 487 | View Code Duplication | if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) { |
|
| 488 | /* translators: %s is a command like "prompt" */ |
||
| 489 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
||
| 490 | } |
||
| 491 | |||
| 492 | if ( isset( $args[0] ) ) { |
||
| 493 | if ( 'get' == $args[0] && isset( $args[1] ) ) { |
||
| 494 | $action = 'get'; |
||
| 495 | } else if ( 'delete' == $args[0] && isset( $args[1] ) ) { |
||
| 496 | $action = 'delete'; |
||
| 497 | } else if ( 'update' == $args[0] && isset( $args[1] ) ) { |
||
| 498 | $action = 'update'; |
||
| 499 | } else { |
||
| 500 | $action = 'list'; |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | // Bail if the option isn't found |
||
| 505 | $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false; |
||
| 506 | View Code Duplication | if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) { |
|
| 507 | WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); |
||
| 508 | } |
||
| 509 | |||
| 510 | // Let's print_r the option if it's an array |
||
| 511 | // Used in the 'get' and 'list' actions |
||
| 512 | $option = is_array( $option ) ? print_r( $option ) : $option; |
||
| 513 | |||
| 514 | switch ( $action ) { |
||
| 515 | case 'get': |
||
| 516 | WP_CLI::success( "\t" . $option ); |
||
| 517 | break; |
||
| 518 | case 'delete': |
||
| 519 | jetpack_cli_are_you_sure( $flagged ); |
||
| 520 | |||
| 521 | Jetpack_Options::delete_option( $args[1] ); |
||
| 522 | WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) ); |
||
| 523 | break; |
||
| 524 | case 'update': |
||
| 525 | jetpack_cli_are_you_sure( $flagged ); |
||
| 526 | |||
| 527 | // Updating arrays would get pretty tricky... |
||
| 528 | $value = Jetpack_Options::get_option( $args[1] ); |
||
| 529 | if ( $value && is_array( $value ) ) { |
||
| 530 | WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) ); |
||
| 531 | } |
||
| 532 | |||
| 533 | Jetpack_Options::update_option( $args[1], $args[2] ); |
||
| 534 | WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) ); |
||
| 535 | break; |
||
| 536 | case 'list': |
||
| 537 | $options_compact = Jetpack_Options::get_option_names(); |
||
| 538 | $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' ); |
||
| 539 | $options_private = Jetpack_Options::get_option_names( 'private' ); |
||
| 540 | $options = array_merge( $options_compact, $options_non_compact, $options_private ); |
||
| 541 | |||
| 542 | // Table headers |
||
| 543 | WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) ); |
||
| 544 | |||
| 545 | // List out the options and their values |
||
| 546 | // Tell them if the value is empty or not |
||
| 547 | // Tell them if it's an array |
||
| 548 | foreach ( $options as $option ) { |
||
| 549 | $value = Jetpack_Options::get_option( $option ); |
||
| 550 | if ( ! $value ) { |
||
| 551 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' ); |
||
| 552 | continue; |
||
| 553 | } |
||
| 554 | |||
| 555 | if ( ! is_array( $value ) ) { |
||
| 556 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value ); |
||
| 557 | } else if ( is_array( $value ) ) { |
||
| 558 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' ); |
||
| 559 | } |
||
| 560 | } |
||
| 561 | $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}'; |
||
| 562 | $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}'; |
||
| 563 | |||
| 564 | WP_CLI::success( |
||
| 565 | _x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" . |
||
| 566 | str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" . |
||
| 567 | str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" . |
||
| 568 | str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" . |
||
| 569 | _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n" |
||
| 570 | ); |
||
| 571 | break; |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Get the status of or start a new Jetpack sync. |
||
| 577 | * |
||
| 578 | * ## OPTIONS |
||
| 579 | * |
||
| 580 | * status : Print the current sync status |
||
| 581 | * start : Start a full sync from this site to WordPress.com |
||
| 582 | * |
||
| 583 | * ## EXAMPLES |
||
| 584 | * |
||
| 585 | * wp jetpack sync status |
||
| 586 | * wp jetpack sync start --modules=functions --sync_wait_time=5 |
||
| 587 | * |
||
| 588 | * @synopsis <status|start> [--<field>=<value>] |
||
| 589 | */ |
||
| 590 | public function sync( $args, $assoc_args ) { |
||
| 591 | if ( ! Jetpack_Sync_Actions::sync_allowed() ) { |
||
| 592 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) ); |
||
| 593 | } |
||
| 594 | |||
| 595 | $action = isset( $args[0] ) ? $args[0] : 'status'; |
||
| 596 | |||
| 597 | switch ( $action ) { |
||
| 598 | case 'status': |
||
| 599 | $status = Jetpack_Sync_Actions::get_sync_status(); |
||
| 600 | $collection = array(); |
||
| 601 | foreach ( $status as $key => $item ) { |
||
| 602 | $collection[] = array( |
||
| 603 | 'option' => $key, |
||
| 604 | 'value' => is_scalar( $item ) ? $item : json_encode( $item ) |
||
| 605 | ); |
||
| 606 | } |
||
| 607 | |||
| 608 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
||
| 609 | break; |
||
| 610 | case 'start': |
||
| 611 | // Get the original settings so that we can restore them later |
||
| 612 | $original_settings = Jetpack_Sync_Settings::get_settings(); |
||
| 613 | |||
| 614 | // Initialize sync settigns so we can sync as quickly as possible |
||
| 615 | $sync_settings = wp_parse_args( |
||
| 616 | array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ), |
||
| 617 | array( |
||
| 618 | 'sync_wait_time' => 0, |
||
| 619 | 'enqueue_wait_time' => 0, |
||
| 620 | 'queue_max_writes_sec' => 10000, |
||
| 621 | 'max_queue_size_full_sync' => 100000 |
||
| 622 | ) |
||
| 623 | ); |
||
| 624 | Jetpack_Sync_Settings::update_settings( $sync_settings ); |
||
| 625 | |||
| 626 | // Convert comma-delimited string of modules to an array |
||
| 627 | if ( ! empty( $assoc_args['modules'] ) ) { |
||
| 628 | $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) ); |
||
| 629 | |||
| 630 | // Convert the array so that the keys are the module name and the value is true to indicate |
||
| 631 | // that we want to sync the module |
||
| 632 | $modules = array_map( '__return_true', array_flip( $modules ) ); |
||
| 633 | } |
||
| 634 | |||
| 635 | View Code Duplication | foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
|
| 636 | if ( |
||
| 637 | 'users' === $module_name && |
||
| 638 | isset( $assoc_args[ $module_name ] ) && |
||
| 639 | 'initial' === $assoc_args[ $module_name ] |
||
| 640 | ) { |
||
| 641 | $modules[ 'users' ] = 'initial'; |
||
| 642 | } elseif ( isset( $assoc_args[ $module_name ] ) ) { |
||
| 643 | $ids = explode( ',', $assoc_args[ $module_name ] ); |
||
| 644 | if ( count( $ids ) > 0 ) { |
||
| 645 | $modules[ $module_name ] = $ids; |
||
| 646 | } |
||
| 647 | } |
||
| 648 | } |
||
| 649 | |||
| 650 | if ( empty( $modules ) ) { |
||
| 651 | $modules = null; |
||
| 652 | } |
||
| 653 | |||
| 654 | // Kick off a full sync |
||
| 655 | if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) { |
||
| 656 | View Code Duplication | if ( $modules ) { |
|
| 657 | WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) ); |
||
| 658 | } else { |
||
| 659 | WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) ); |
||
| 660 | } |
||
| 661 | View Code Duplication | } else { |
|
| 662 | |||
| 663 | // Reset sync settings to original. |
||
| 664 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 665 | |||
| 666 | if ( $modules ) { |
||
| 667 | WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) ); |
||
| 668 | } else { |
||
| 669 | WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) ); |
||
| 670 | } |
||
| 671 | } |
||
| 672 | |||
| 673 | // Keep sending to WPCOM until there's nothing to send |
||
| 674 | $i = 1; |
||
| 675 | do { |
||
| 676 | $result = Jetpack_Sync_Actions::$sender->do_full_sync(); |
||
| 677 | if ( $result ) { |
||
| 678 | if ( 1 == $i++ ) { |
||
| 679 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
||
| 680 | } else { |
||
| 681 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
||
| 682 | } |
||
| 683 | } |
||
| 684 | } while ( $result ); |
||
| 685 | |||
| 686 | // Reset sync settings to original. |
||
| 687 | Jetpack_Sync_Settings::update_settings( $original_settings ); |
||
| 688 | |||
| 689 | WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) ); |
||
| 690 | break; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * List the contents of a specific Jetpack sync queue. |
||
| 696 | * |
||
| 697 | * ## OPTIONS |
||
| 698 | * |
||
| 699 | * peek : List the 100 front-most items on the queue. |
||
| 700 | * |
||
| 701 | * ## EXAMPLES |
||
| 702 | * |
||
| 703 | * wp jetpack sync_queue full_sync peek |
||
| 704 | * |
||
| 705 | * @synopsis <incremental|full_sync> <peek> |
||
| 706 | */ |
||
| 707 | public function sync_queue( $args, $assoc_args ) { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Provision a site using a Jetpack Partner license |
||
| 761 | * |
||
| 762 | * Returns JSON blob |
||
| 763 | * |
||
| 764 | * ## OPTIONS |
||
| 765 | * |
||
| 766 | * <token_json> |
||
| 767 | * : JSON blob of WPCOM API token |
||
| 768 | * --plan=<plan_name> |
||
| 769 | * : Slug of the requested plan, e.g. premium |
||
| 770 | * --user_id=<user_id> |
||
| 771 | * : Local ID of user to connect as (if omitted, user will be required to redirect via wp-admin) |
||
| 772 | * |
||
| 773 | * ## EXAMPLES |
||
| 774 | * |
||
| 775 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
||
| 776 | * { success: true } |
||
| 777 | * |
||
| 778 | * @synopsis <token_json> --plan=<plan_name> --user_id=<user_id> |
||
| 779 | */ |
||
| 780 | public function partner_provision( $args, $named_args ) { |
||
| 911 | |||
| 912 | private function partner_provision_error( $error ) { |
||
| 920 | } |
||
| 921 | |||
| 958 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: