| @@ -1,17 +1,11 @@ | ||
| 1 | -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | -/** | |
| 3 | - * WP-CLI command class. | |
| 4 | - * | |
| 5 | - * @package automattic/jetpack | |
| 6 | - */ | |
| 1 | +<?php | |
| 7 | 2 | |
| 3 | +WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' ); | |
| 4 | + | |
| 8 | 5 | use Automattic\Jetpack\Connection\Client; |
| 9 | 6 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 10 | -use Automattic\Jetpack\Connection\Tokens; | |
| 11 | -use Automattic\Jetpack\Identity_Crisis; | |
| 12 | -use Automattic\Jetpack\IP\Utils as IP_Utils; | |
| 13 | -use Automattic\Jetpack\Publicize\Publicize; | |
| 7 | +use Automattic\Jetpack\Connection\Utils as Connection_Utils; | |
| 14 | 8 | use Automattic\Jetpack\Status; |
| 15 | 9 | use Automattic\Jetpack\Sync\Actions; |
| 16 | 10 | use Automattic\Jetpack\Sync\Listener; |
| 17 | 11 | use Automattic\Jetpack\Sync\Modules; |
| @@ -16,47 +10,17 @@ | ||
| 16 | 10 | use Automattic\Jetpack\Sync\Listener; |
| 17 | 11 | use Automattic\Jetpack\Sync\Modules; |
| 18 | 12 | use Automattic\Jetpack\Sync\Queue; |
| 19 | 13 | use Automattic\Jetpack\Sync\Settings; |
| 20 | -use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions; | |
| 21 | 14 | |
| 22 | -if ( ! class_exists( 'WP_CLI_Command' ) ) { | |
| 23 | - return; | |
| 24 | -} | |
| 25 | - | |
| 26 | -// @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- https://github.com/phan/phan/issues/4763 | |
| 27 | -WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' ); | |
| 28 | - | |
| 29 | 15 | /** |
| 30 | 16 | * Control your local Jetpack installation. |
| 31 | 17 | */ |
| 32 | 18 | class Jetpack_CLI extends WP_CLI_Command { |
| 33 | - /** | |
| 34 | - * Console escape code for green. | |
| 35 | - * | |
| 36 | - * @var string | |
| 37 | - */ | |
| 38 | - public $green_open = "\033[32m"; | |
| 39 | - | |
| 40 | - /** | |
| 41 | - * Console escape code for red. | |
| 42 | - * | |
| 43 | - * @var string | |
| 44 | - */ | |
| 45 | - public $red_open = "\033[31m"; | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Console escape code for yellow. | |
| 49 | - * | |
| 50 | - * @var string | |
| 51 | - */ | |
| 19 | + // Aesthetics. | |
| 20 | + public $green_open = "\033[32m"; | |
| 21 | + public $red_open = "\033[31m"; | |
| 52 | 22 | public $yellow_open = "\033[33m"; |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Console escape code to reset coloring. | |
| 56 | - * | |
| 57 | - * @var string | |
| 58 | - */ | |
| 59 | 23 | public $color_close = "\033[0m"; |
| 60 | 24 | |
| 61 | 25 | /** |
| 62 | 26 | * Get Jetpack Details |
| @@ -70,13 +34,11 @@ | ||
| 70 | 34 | * ## EXAMPLES |
| 71 | 35 | * |
| 72 | 36 | * wp jetpack status |
| 73 | 37 | * wp jetpack status full |
| 74 | - * | |
| 75 | - * @param array $args Positional args. | |
| 76 | 38 | */ |
| 77 | - public function status( $args ) { | |
| 78 | - require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php'; | |
| 39 | + public function status( $args, $assoc_args ) { | |
| 40 | + jetpack_require_lib( 'debugger' ); | |
| 79 | 41 | |
| 80 | 42 | /* translators: %s is the site URL */ |
| 81 | 43 | WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) ); |
| 82 | 44 | |
| @@ -95,9 +57,9 @@ | ||
| 95 | 57 | WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) ); |
| 96 | 58 | } else { |
| 97 | 59 | $error = array(); |
| 98 | 60 | foreach ( $cxntests->list_fails() as $fail ) { |
| 99 | - $error[] = $fail['name'] . ( empty( $fail['message'] ) ? '' : ': ' . $fail['message'] ); | |
| 61 | + $error[] = $fail['name'] . ': ' . $fail['message']; | |
| 100 | 62 | } |
| 101 | 63 | WP_CLI::error_multi_line( $error ); |
| 102 | 64 | |
| 103 | 65 | $cxntests->output_results_for_cli(); |
| @@ -116,41 +78,41 @@ | ||
| 116 | 78 | * Are they asking for all data? |
| 117 | 79 | * |
| 118 | 80 | * Loop through heartbeat data and organize by priority. |
| 119 | 81 | */ |
| 120 | - $all_data = ( isset( $args[0] ) && 'full' === $args[0] ) ? 'full' : false; | |
| 82 | + $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false; | |
| 121 | 83 | if ( $all_data ) { |
| 122 | - // Heartbeat data. | |
| 84 | + // Heartbeat data | |
| 123 | 85 | WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) ); |
| 124 | 86 | |
| 125 | 87 | // Get the filtered heartbeat data. |
| 126 | - // Filtered so we can color/list by severity. | |
| 88 | + // Filtered so we can color/list by severity | |
| 127 | 89 | $stats = Jetpack::jetpack_check_heartbeat_data(); |
| 128 | 90 | |
| 129 | - // Display red flags first. | |
| 91 | + // Display red flags first | |
| 130 | 92 | foreach ( $stats['bad'] as $stat => $value ) { |
| 131 | - WP_CLI::line( sprintf( "$this->red_open%-'.16s %s $this->color_close", $stat, $value ) ); | |
| 93 | + printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value ); | |
| 132 | 94 | } |
| 133 | 95 | |
| 134 | - // Display caution warnings next. | |
| 96 | + // Display caution warnings next | |
| 135 | 97 | foreach ( $stats['caution'] as $stat => $value ) { |
| 136 | - WP_CLI::line( sprintf( "$this->yellow_open%-'.16s %s $this->color_close", $stat, $value ) ); | |
| 98 | + printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value ); | |
| 137 | 99 | } |
| 138 | 100 | |
| 139 | 101 | // The rest of the results are good! |
| 140 | 102 | foreach ( $stats['good'] as $stat => $value ) { |
| 141 | 103 | |
| 142 | - // Modules should get special spacing for aestetics. | |
| 104 | + // Modules should get special spacing for aestetics | |
| 143 | 105 | if ( strpos( $stat, 'odule-' ) ) { |
| 144 | - WP_CLI::line( sprintf( "%-'.30s %s", $stat, $value ) ); | |
| 145 | - usleep( 4000 ); // For dramatic effect lolz. | |
| 106 | + printf( "%-'.30s %s\n", $stat, $value ); | |
| 107 | + usleep( 4000 ); // For dramatic effect lolz | |
| 146 | 108 | continue; |
| 147 | 109 | } |
| 148 | - WP_CLI::line( sprintf( "%-'.16s %s", $stat, $value ) ); | |
| 149 | - usleep( 4000 ); // For dramatic effect lolz. | |
| 110 | + printf( "%-'.16s %s\n", $stat, $value ); | |
| 111 | + usleep( 4000 ); // For dramatic effect lolz | |
| 150 | 112 | } |
| 151 | 113 | } else { |
| 152 | - // Just the basics. | |
| 114 | + // Just the basics | |
| 153 | 115 | WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) ); |
| 154 | 116 | } |
| 155 | 117 | } |
| 156 | 118 | |
| @@ -164,14 +126,14 @@ | ||
| 164 | 126 | * wp jetpack test-connection |
| 165 | 127 | * |
| 166 | 128 | * @subcommand test-connection |
| 167 | 129 | */ |
| 168 | - public function test_connection() { | |
| 130 | + public function test_connection( $args, $assoc_args ) { | |
| 169 | 131 | |
| 170 | 132 | /* translators: %s is the site URL */ |
| 171 | 133 | WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) ); |
| 172 | 134 | |
| 173 | - if ( ! Jetpack::is_connection_ready() ) { | |
| 135 | + if ( ! Jetpack::is_active() ) { | |
| 174 | 136 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
| 175 | 137 | } |
| 176 | 138 | |
| 177 | 139 | $response = Client::wpcom_json_api_request_as_blog( |
| @@ -208,38 +170,34 @@ | ||
| 208 | 170 | * blog: Disconnect the entire blog. |
| 209 | 171 | * |
| 210 | 172 | * user <user_identifier>: Disconnect a specific user from WordPress.com. |
| 211 | 173 | * |
| 212 | - * [--force] | |
| 213 | - * If the user ID provided is the connection owner, it will only be disconnected if --force is passed | |
| 174 | + * Please note, the primary account that the blog is connected | |
| 175 | + * to WordPress.com with cannot be disconnected without | |
| 176 | + * disconnecting the entire blog. | |
| 214 | 177 | * |
| 215 | 178 | * ## EXAMPLES |
| 216 | 179 | * |
| 217 | 180 | * wp jetpack disconnect blog |
| 218 | 181 | * wp jetpack disconnect user 13 |
| 219 | - * wp jetpack disconnect user 1 --force | |
| 220 | 182 | * wp jetpack disconnect user username |
| 221 | 183 | * wp jetpack disconnect user email@domain.com |
| 222 | 184 | * |
| 223 | - * @synopsis <blog|user> [<user_identifier>] [--force] | |
| 224 | - * | |
| 225 | - * @param array $args Positional args. | |
| 226 | - * @param array $assoc_args Named args. | |
| 185 | + * @synopsis <blog|user> [<user_identifier>] | |
| 227 | 186 | */ |
| 228 | 187 | public function disconnect( $args, $assoc_args ) { |
| 229 | - $user = null; | |
| 230 | - if ( ! Jetpack::is_connection_ready() ) { | |
| 188 | + if ( ! Jetpack::is_active() ) { | |
| 231 | 189 | WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) ); |
| 232 | 190 | return; |
| 233 | 191 | } |
| 234 | 192 | |
| 235 | 193 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
| 236 | - if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ), true ) ) { | |
| 194 | + if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) { | |
| 237 | 195 | /* translators: %s is a command like "prompt" */ |
| 238 | 196 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
| 239 | 197 | } |
| 240 | 198 | |
| 241 | - if ( in_array( $action, array( 'user' ), true ) ) { | |
| 199 | + if ( in_array( $action, array( 'user' ) ) ) { | |
| 242 | 200 | if ( isset( $args[1] ) ) { |
| 243 | 201 | $user_id = $args[1]; |
| 244 | 202 | if ( ctype_digit( $user_id ) ) { |
| 245 | 203 | $field = 'id'; |
| @@ -250,10 +208,9 @@ | ||
| 250 | 208 | } else { |
| 251 | 209 | $field = 'login'; |
| 252 | 210 | $user_id = sanitize_user( $user_id, true ); |
| 253 | 211 | } |
| 254 | - $user = get_user_by( $field, $user_id ); | |
| 255 | - if ( ! $user ) { | |
| 212 | + if ( ! $user = get_user_by( $field, $user_id ) ) { | |
| 256 | 213 | WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) ); |
| 257 | 214 | } |
| 258 | 215 | } else { |
| 259 | 216 | WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) ); |
| @@ -259,14 +216,12 @@ | ||
| 259 | 216 | WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) ); |
| 260 | 217 | } |
| 261 | 218 | } |
| 262 | 219 | |
| 263 | - $force_user_disconnect = ! empty( $assoc_args['force'] ); | |
| 264 | - | |
| 265 | 220 | switch ( $action ) { |
| 266 | 221 | case 'blog': |
| 267 | 222 | Jetpack::log( 'disconnect' ); |
| 268 | - ( new Connection_Manager( 'jetpack' ) )->disconnect_site(); | |
| 223 | + Jetpack::disconnect(); | |
| 269 | 224 | WP_CLI::success( |
| 270 | 225 | sprintf( |
| 271 | 226 | /* translators: %s is the site URL */ |
| 272 | 227 | __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ), |
| @@ -274,25 +229,14 @@ | ||
| 274 | 229 | ) |
| 275 | 230 | ); |
| 276 | 231 | break; |
| 277 | 232 | case 'user': |
| 278 | - $connection_manager = new Connection_Manager( 'jetpack' ); | |
| 279 | - $disconnected = $connection_manager->disconnect_user( $user->ID, $force_user_disconnect ); | |
| 280 | - if ( $disconnected ) { | |
| 233 | + if ( Connection_Manager::disconnect_user( $user->ID ) ) { | |
| 281 | 234 | Jetpack::log( 'unlink', $user->ID ); |
| 282 | 235 | WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) ); |
| 283 | 236 | } else { |
| 284 | - if ( ! $connection_manager->is_user_connected( $user->ID ) ) { | |
| 285 | - /* translators: %s is a username */ | |
| 286 | - $error_message = sprintf( __( 'User %s could not be disconnected because it is not connected!', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" ); | |
| 287 | - } elseif ( ! $force_user_disconnect && $connection_manager->is_connection_owner( $user->ID ) ) { | |
| 288 | - /* translators: %s is a username */ | |
| 289 | - $error_message = sprintf( __( 'User %s could not be disconnected because it is the connection owner! If you want to disconnect in anyway, use the --force parameter.', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" ); | |
| 290 | - } else { | |
| 291 | - /* translators: %s is a username */ | |
| 292 | - $error_message = sprintf( __( 'User %s could not be disconnected.', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" ); | |
| 293 | - } | |
| 294 | - WP_CLI::error( $error_message ); | |
| 237 | + /* translators: %s is a username */ | |
| 238 | + WP_CLI::error( sprintf( __( "User %s could not be disconnected. Are you sure they're connected currently?", 'jetpack' ), "{$user->login} <{$user->email}>" ) ); | |
| 295 | 239 | } |
| 296 | 240 | break; |
| 297 | 241 | case 'prompt': |
| 298 | 242 | WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) ); |
| @@ -320,11 +264,8 @@ | ||
| 320 | 264 | * wp jetpack reset modules |
| 321 | 265 | * wp jetpack reset sync-checksum --dry-run --offset=0 |
| 322 | 266 | * |
| 323 | 267 | * @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>] |
| 324 | - * | |
| 325 | - * @param array $args Positional args. | |
| 326 | - * @param array $assoc_args Named args. | |
| 327 | 268 | */ |
| 328 | 269 | public function reset( $args, $assoc_args ) { |
| 329 | 270 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
| 330 | 271 | if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) { |
| @@ -347,9 +288,9 @@ | ||
| 347 | 288 | |
| 348 | 289 | switch ( $action ) { |
| 349 | 290 | case 'options': |
| 350 | 291 | $options_to_reset = Jetpack_Options::get_options_for_reset(); |
| 351 | - // Reset the Jetpack options. | |
| 292 | + // Reset the Jetpack options | |
| 352 | 293 | WP_CLI::line( |
| 353 | 294 | sprintf( |
| 354 | 295 | /* translators: %s is the site URL */ |
| 355 | 296 | __( "Resetting Jetpack Options for %s...\n", 'jetpack' ), |
| @@ -355,9 +296,9 @@ | ||
| 355 | 296 | __( "Resetting Jetpack Options for %s...\n", 'jetpack' ), |
| 356 | 297 | esc_url( get_site_url() ) |
| 357 | 298 | ) |
| 358 | 299 | ); |
| 359 | - sleep( 1 ); // Take a breath. | |
| 300 | + sleep( 1 ); // Take a breath | |
| 360 | 301 | foreach ( $options_to_reset['jp_options'] as $option_to_reset ) { |
| 361 | 302 | if ( ! $is_dry_run ) { |
| 362 | 303 | Jetpack_Options::delete_option( $option_to_reset ); |
| 363 | 304 | usleep( 100000 ); |
| @@ -366,11 +307,11 @@ | ||
| 366 | 307 | /* translators: This is the result of an action. The option named %s was reset */ |
| 367 | 308 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
| 368 | 309 | } |
| 369 | 310 | |
| 370 | - // Reset the WP options. | |
| 311 | + // Reset the WP options | |
| 371 | 312 | WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", 'jetpack' ) ); |
| 372 | - usleep( 500000 ); // Take a breath. | |
| 313 | + usleep( 500000 ); // Take a breath | |
| 373 | 314 | foreach ( $options_to_reset['wp_options'] as $option_to_reset ) { |
| 374 | 315 | if ( ! $is_dry_run ) { |
| 375 | 316 | delete_option( $option_to_reset ); |
| 376 | 317 | usleep( 100000 ); |
| @@ -378,11 +319,11 @@ | ||
| 378 | 319 | /* translators: This is the result of an action. The option named %s was reset */ |
| 379 | 320 | WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) ); |
| 380 | 321 | } |
| 381 | 322 | |
| 382 | - // Reset to default modules. | |
| 323 | + // Reset to default modules | |
| 383 | 324 | WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) ); |
| 384 | - usleep( 500000 ); // Take a breath. | |
| 325 | + usleep( 500000 ); // Take a breath | |
| 385 | 326 | $default_modules = Jetpack::get_default_modules(); |
| 386 | 327 | if ( ! $is_dry_run ) { |
| 387 | 328 | Jetpack::update_active_modules( $default_modules ); |
| 388 | 329 | } |
| @@ -431,9 +372,9 @@ | ||
| 431 | 372 | $option, |
| 432 | 373 | "{$site->domain}{$site->path}" |
| 433 | 374 | ) |
| 434 | 375 | ); |
| 435 | - ++$count_fixes; | |
| 376 | + $count_fixes++; | |
| 436 | 377 | if ( ! $is_dry_run ) { |
| 437 | 378 | /* |
| 438 | 379 | * We could be deleting a lot of options rows at the same time. |
| 439 | 380 | * Allow some time for replication to catch up. |
| @@ -497,8 +438,9 @@ | ||
| 497 | 438 | "SELECT COUNT(*) FROM $wpdb->options WHERE option_name = %s", |
| 498 | 439 | $option |
| 499 | 440 | ) |
| 500 | 441 | ); |
| 442 | + | |
| 501 | 443 | } |
| 502 | 444 | |
| 503 | 445 | /** |
| 504 | 446 | * Manage Jetpack Modules |
| @@ -540,15 +482,11 @@ | ||
| 540 | 482 | * wp jetpack module deactivate stats |
| 541 | 483 | * wp jetpack module toggle stats |
| 542 | 484 | * wp jetpack module activate all |
| 543 | 485 | * wp jetpack module deactivate all |
| 544 | - * | |
| 545 | - * @param array $args Positional args. | |
| 546 | - * @param array $assoc_args Named args. | |
| 547 | 486 | */ |
| 548 | 487 | public function module( $args, $assoc_args ) { |
| 549 | - $module_slug = null; | |
| 550 | - $action = isset( $args[0] ) ? $args[0] : 'list'; | |
| 488 | + $action = isset( $args[0] ) ? $args[0] : 'list'; | |
| 551 | 489 | |
| 552 | 490 | if ( isset( $args[1] ) ) { |
| 553 | 491 | $module_slug = $args[1]; |
| 554 | 492 | if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) { |
| @@ -635,18 +573,16 @@ | ||
| 635 | 573 | * wp jetpack protect allow list |
| 636 | 574 | * wp jetpack protect allow clear |
| 637 | 575 | * |
| 638 | 576 | * @synopsis <allow> [<ip|ip_low-ip_high|list|clear>] |
| 639 | - * | |
| 640 | - * @param array $args Positional args. | |
| 641 | 577 | */ |
| 642 | - public function protect( $args ) { | |
| 578 | + public function protect( $args, $assoc_args ) { | |
| 643 | 579 | $action = isset( $args[0] ) ? $args[0] : 'prompt'; |
| 644 | 580 | if ( ! in_array( $action, array( 'whitelist', 'allow' ), true ) ) { // Still allow "whitelist" for legacy support. |
| 645 | 581 | /* translators: %s is a command like "prompt" */ |
| 646 | 582 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
| 647 | 583 | } |
| 648 | - // Check if module is active. | |
| 584 | + // Check if module is active | |
| 649 | 585 | if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) { |
| 650 | 586 | /* translators: %s is a module name */ |
| 651 | 587 | WP_CLI::error( sprintf( _x( '%1$s is not active. You can activate it with "wp jetpack module activate %2$s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) ); |
| 652 | 588 | } |
| @@ -663,17 +599,17 @@ | ||
| 663 | 599 | $new_ip = $args[1]; |
| 664 | 600 | $current_allow = get_site_option( 'jetpack_protect_whitelist', array() ); // @todo Update the option name. |
| 665 | 601 | |
| 666 | 602 | // Build array of IPs that are already on the allowed list. |
| 667 | - // Re-build manually instead of using jetpack_protect_format_allow_list() so we can easily get | |
| 668 | - // low & high range params for IP_Utils::ip_address_is_in_range(). | |
| 603 | + // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get | |
| 604 | + // low & high range params for jetpack_protect_ip_address_is_in_range(); | |
| 669 | 605 | foreach ( $current_allow as $allowed ) { |
| 670 | 606 | |
| 671 | - // IP ranges. | |
| 607 | + // IP ranges | |
| 672 | 608 | if ( $allowed->range ) { |
| 673 | 609 | |
| 674 | 610 | // Is it already on the allowed list? |
| 675 | - if ( IP_Utils::ip_address_is_in_range( $new_ip, $allowed->range_low, $allowed->range_high ) ) { | |
| 611 | + if ( jetpack_protect_ip_address_is_in_range( $new_ip, $allowed->range_low, $allowed->range_high ) ) { | |
| 676 | 612 | /* translators: %s is an IP address */ |
| 677 | 613 | WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) ); |
| 678 | 614 | break; |
| 679 | 615 | } |
| @@ -678,9 +614,9 @@ | ||
| 678 | 614 | break; |
| 679 | 615 | } |
| 680 | 616 | $allow[] = $allowed->range_low . ' - ' . $allowed->range_high; |
| 681 | 617 | |
| 682 | - } else { // Individual IPs. | |
| 618 | + } else { // Individual IPs | |
| 683 | 619 | |
| 684 | 620 | // Check if the IP is already on the allow list (single IP only). |
| 685 | 621 | if ( $new_ip === $allowed->ip_address ) { |
| 686 | 622 | /* translators: %s is an IP address */ |
| @@ -695,9 +631,9 @@ | ||
| 695 | 631 | /* |
| 696 | 632 | * List the allowed IPs. |
| 697 | 633 | * Done here because it's easier to read the $allow array after it's been rebuilt. |
| 698 | 634 | */ |
| 699 | - if ( isset( $args[1] ) && 'list' === $args[1] ) { | |
| 635 | + if ( isset( $args[1] ) && 'list' == $args[1] ) { | |
| 700 | 636 | if ( ! empty( $allow ) ) { |
| 701 | 637 | WP_CLI::success( __( 'Here are your always allowed IPs:', 'jetpack' ) ); |
| 702 | 638 | foreach ( $allow as $ip ) { |
| 703 | 639 | WP_CLI::line( "\t" . str_pad( $ip, 24 ) ); |
| @@ -710,12 +646,12 @@ | ||
| 710 | 646 | |
| 711 | 647 | /* |
| 712 | 648 | * Clear the always allow list. |
| 713 | 649 | */ |
| 714 | - if ( isset( $args[1] ) && 'clear' === $args[1] ) { | |
| 650 | + if ( isset( $args[1] ) && 'clear' == $args[1] ) { | |
| 715 | 651 | if ( ! empty( $allow ) ) { |
| 716 | 652 | $allow = array(); |
| 717 | - Brute_Force_Protection_Shared_Functions::save_allow_list( $allow ); // @todo Need to update function name in the Protect module. | |
| 653 | + jetpack_protect_save_whitelist( $allow ); // @todo Need to update function name in the Protect module. | |
| 718 | 654 | WP_CLI::success( __( 'Cleared all IPs from the always allow list.', 'jetpack' ) ); |
| 719 | 655 | } else { |
| 720 | 656 | WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) ); |
| 721 | 657 | } |
| @@ -725,9 +661,9 @@ | ||
| 725 | 661 | // Append new IP to allow array. |
| 726 | 662 | array_push( $allow, $new_ip ); |
| 727 | 663 | |
| 728 | 664 | // Save allow list if there are no errors. |
| 729 | - $result = Brute_Force_Protection_Shared_Functions::save_allow_list( $allow ); // @todo Need to update function name in the Protect module. | |
| 665 | + $result = jetpack_protect_save_whitelist( $allow ); // @todo Need to update function name in the Protect module. | |
| 730 | 666 | if ( is_wp_error( $result ) ) { |
| 731 | 667 | WP_CLI::error( $result ); |
| 732 | 668 | } |
| 733 | 669 | |
| @@ -737,9 +673,9 @@ | ||
| 737 | 673 | case 'prompt': |
| 738 | 674 | WP_CLI::error( |
| 739 | 675 | __( 'No command found.', 'jetpack' ) . "\n" . |
| 740 | 676 | __( 'Please enter the IP address you want to always allow.', 'jetpack' ) . "\n" . |
| 741 | - _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 add IP ranges - low_range/high_range should be translated.', 'jetpack' ) . "\n" . | |
| 677 | + _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 add IP ranges - low_range/high_range should be translated.', 'jetpack' ) . "\n" . | |
| 742 | 678 | _x( "You can also 'list' or 'clear' the always allowed list.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n" |
| 743 | 679 | ); |
| 744 | 680 | break; |
| 745 | 681 | } |
| @@ -764,29 +700,27 @@ | ||
| 764 | 700 | * wp jetpack options delete <option_name> |
| 765 | 701 | * wp jetpack options update <option_name> [<option_value>] |
| 766 | 702 | * |
| 767 | 703 | * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] |
| 768 | - * | |
| 769 | - * @param array $args Positional args. | |
| 770 | 704 | */ |
| 771 | - public function options( $args ) { | |
| 705 | + public function options( $args, $assoc_args ) { | |
| 772 | 706 | $action = isset( $args[0] ) ? $args[0] : 'list'; |
| 773 | 707 | $safe_to_modify = Jetpack_Options::get_options_for_reset(); |
| 774 | 708 | |
| 775 | 709 | // Is the option flagged as unsafe? |
| 776 | - $flagged = ! in_array( $args[1], $safe_to_modify, true ); | |
| 710 | + $flagged = ! in_array( $args[1], $safe_to_modify ); | |
| 777 | 711 | |
| 778 | - if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ), true ) ) { | |
| 712 | + if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) { | |
| 779 | 713 | /* translators: %s is a command like "prompt" */ |
| 780 | 714 | WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) ); |
| 781 | 715 | } |
| 782 | 716 | |
| 783 | 717 | if ( isset( $args[0] ) ) { |
| 784 | - if ( 'get' === $args[0] && isset( $args[1] ) ) { | |
| 718 | + if ( 'get' == $args[0] && isset( $args[1] ) ) { | |
| 785 | 719 | $action = 'get'; |
| 786 | - } elseif ( 'delete' === $args[0] && isset( $args[1] ) ) { | |
| 720 | + } elseif ( 'delete' == $args[0] && isset( $args[1] ) ) { | |
| 787 | 721 | $action = 'delete'; |
| 788 | - } elseif ( 'update' === $args[0] && isset( $args[1] ) ) { | |
| 722 | + } elseif ( 'update' == $args[0] && isset( $args[1] ) ) { | |
| 789 | 723 | $action = 'update'; |
| 790 | 724 | } else { |
| 791 | 725 | $action = 'list'; |
| 792 | 726 | } |
| @@ -791,18 +725,17 @@ | ||
| 791 | 725 | $action = 'list'; |
| 792 | 726 | } |
| 793 | 727 | } |
| 794 | 728 | |
| 795 | - // Bail if the option isn't found. | |
| 729 | + // Bail if the option isn't found | |
| 796 | 730 | $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false; |
| 797 | 731 | if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) { |
| 798 | - WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); | |
| 732 | + WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) ); | |
| 799 | 733 | } |
| 800 | 734 | |
| 801 | - // Let's print_r the option if it's an array. | |
| 802 | - // Used in the 'get' and 'list' actions. | |
| 803 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r | |
| 804 | - $option = is_array( $option ) ? print_r( $option, true ) : $option; | |
| 735 | + // Let's print_r the option if it's an array | |
| 736 | + // Used in the 'get' and 'list' actions | |
| 737 | + $option = is_array( $option ) ? print_r( $option ) : $option; | |
| 805 | 738 | |
| 806 | 739 | switch ( $action ) { |
| 807 | 740 | case 'get': |
| 808 | 741 | WP_CLI::success( "\t" . $option ); |
| @@ -832,14 +765,14 @@ | ||
| 832 | 765 | $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' ); |
| 833 | 766 | $options_private = Jetpack_Options::get_option_names( 'private' ); |
| 834 | 767 | $options = array_merge( $options_compact, $options_non_compact, $options_private ); |
| 835 | 768 | |
| 836 | - // Table headers. | |
| 769 | + // Table headers | |
| 837 | 770 | WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) ); |
| 838 | 771 | |
| 839 | - // List out the options and their values. | |
| 840 | - // Tell them if the value is empty or not. | |
| 841 | - // Tell them if it's an array. | |
| 772 | + // List out the options and their values | |
| 773 | + // Tell them if the value is empty or not | |
| 774 | + // Tell them if it's an array | |
| 842 | 775 | foreach ( $options as $option ) { |
| 843 | 776 | $value = Jetpack_Options::get_option( $option ); |
| 844 | 777 | if ( ! $value ) { |
| 845 | 778 | WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' ); |
| @@ -858,9 +791,9 @@ | ||
| 858 | 791 | WP_CLI::success( |
| 859 | 792 | _x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" . |
| 860 | 793 | str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" . |
| 861 | 794 | str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" . |
| 862 | - str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text\n" . | |
| 795 | + str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" . | |
| 863 | 796 | _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n" |
| 864 | 797 | ); |
| 865 | 798 | break; |
| 866 | 799 | } |
| @@ -888,11 +821,8 @@ | ||
| 888 | 821 | * wp jetpack sync reset |
| 889 | 822 | * wp jetpack sync reset --queue=full or regular |
| 890 | 823 | * |
| 891 | 824 | * @synopsis <status|start> [--<field>=<value>] |
| 892 | - * | |
| 893 | - * @param array $args Positional args. | |
| 894 | - * @param array $assoc_args Named args. | |
| 895 | 825 | */ |
| 896 | 826 | public function sync( $args, $assoc_args ) { |
| 897 | 827 | |
| 898 | 828 | $action = isset( $args[0] ) ? $args[0] : 'status'; |
| @@ -903,9 +833,9 @@ | ||
| 903 | 833 | $collection = array(); |
| 904 | 834 | foreach ( $status as $key => $item ) { |
| 905 | 835 | $collection[] = array( |
| 906 | 836 | 'option' => $key, |
| 907 | - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ), | |
| 837 | + 'value' => is_scalar( $item ) ? $item : json_encode( $item ), | |
| 908 | 838 | ); |
| 909 | 839 | } |
| 910 | 840 | WP_CLI::log( __( 'Sync Status:', 'jetpack' ) ); |
| 911 | 841 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
| @@ -911,17 +841,16 @@ | ||
| 911 | 841 | WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); |
| 912 | 842 | break; |
| 913 | 843 | case 'settings': |
| 914 | 844 | WP_CLI::log( __( 'Sync Settings:', 'jetpack' ) ); |
| 915 | - $settings = array(); | |
| 916 | 845 | foreach ( Settings::get_settings() as $setting => $item ) { |
| 917 | 846 | $settings[] = array( |
| 918 | 847 | 'setting' => $setting, |
| 919 | - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ), | |
| 848 | + 'value' => is_scalar( $item ) ? $item : json_encode( $item ), | |
| 920 | 849 | ); |
| 921 | 850 | } |
| 922 | 851 | WP_CLI\Utils\format_items( 'table', $settings, array( 'setting', 'value' ) ); |
| 923 | - break; | |
| 852 | + | |
| 924 | 853 | case 'disable': |
| 925 | 854 | // Don't set it via the Settings since that also resets the queues. |
| 926 | 855 | update_option( 'jetpack_sync_settings_disable', 1 ); |
| 927 | 856 | /* translators: %s is the site URL */ |
| @@ -967,18 +896,15 @@ | ||
| 967 | 896 | |
| 968 | 897 | break; |
| 969 | 898 | case 'start': |
| 970 | 899 | if ( ! Actions::sync_allowed() ) { |
| 971 | - if ( Settings::get_setting( 'disable' ) ) { | |
| 900 | + if ( ! Settings::get_setting( 'disable' ) ) { | |
| 972 | 901 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. It is currently disabled. Run `wp jetpack sync enable` to enable it.', 'jetpack' ) ); |
| 973 | 902 | return; |
| 974 | 903 | } |
| 975 | - $connection = new Connection_Manager(); | |
| 976 | - if ( ! $connection->is_connected() ) { | |
| 977 | - if ( ! doing_action( 'jetpack_site_registered' ) ) { | |
| 978 | - WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. Jetpack is not connected.', 'jetpack' ) ); | |
| 979 | - return; | |
| 980 | - } | |
| 904 | + if ( doing_action( 'jetpack_user_authorized' ) || Jetpack::is_active() ) { | |
| 905 | + WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. Jetpack is not connected.', 'jetpack' ) ); | |
| 906 | + return; | |
| 981 | 907 | } |
| 982 | 908 | |
| 983 | 909 | $status = new Status(); |
| 984 | 910 | |
| @@ -985,17 +911,17 @@ | ||
| 985 | 911 | if ( $status->is_offline_mode() ) { |
| 986 | 912 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in offline mode.', 'jetpack' ) ); |
| 987 | 913 | return; |
| 988 | 914 | } |
| 989 | - if ( $status->in_safe_mode() ) { | |
| 990 | - WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in safe mode.', 'jetpack' ) ); | |
| 915 | + if ( $status->is_staging_site() ) { | |
| 916 | + WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in staging mode.', 'jetpack' ) ); | |
| 991 | 917 | return; |
| 992 | 918 | } |
| 993 | 919 | } |
| 994 | - // Get the original settings so that we can restore them later. | |
| 920 | + // Get the original settings so that we can restore them later | |
| 995 | 921 | $original_settings = Settings::get_settings(); |
| 996 | 922 | |
| 997 | - // Initialize sync settigns so we can sync as quickly as possible. | |
| 923 | + // Initialize sync settigns so we can sync as quickly as possible | |
| 998 | 924 | $sync_settings = wp_parse_args( |
| 999 | 925 | array_intersect_key( $assoc_args, Settings::$valid_settings ), |
| 1000 | 926 | array( |
| 1001 | 927 | 'sync_wait_time' => 0, |
| @@ -1006,14 +932,14 @@ | ||
| 1006 | 932 | ) |
| 1007 | 933 | ); |
| 1008 | 934 | Settings::update_settings( $sync_settings ); |
| 1009 | 935 | |
| 1010 | - // Convert comma-delimited string of modules to an array. | |
| 936 | + // Convert comma-delimited string of modules to an array | |
| 1011 | 937 | if ( ! empty( $assoc_args['modules'] ) ) { |
| 1012 | 938 | $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) ); |
| 1013 | 939 | |
| 1014 | 940 | // Convert the array so that the keys are the module name and the value is true to indicate |
| 1015 | - // that we want to sync the module. | |
| 941 | + // that we want to sync the module | |
| 1016 | 942 | $modules = array_map( '__return_true', array_flip( $modules ) ); |
| 1017 | 943 | } |
| 1018 | 944 | |
| 1019 | 945 | foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { |
| @@ -1024,9 +950,9 @@ | ||
| 1024 | 950 | ) { |
| 1025 | 951 | $modules['users'] = 'initial'; |
| 1026 | 952 | } elseif ( isset( $assoc_args[ $module_name ] ) ) { |
| 1027 | 953 | $ids = explode( ',', $assoc_args[ $module_name ] ); |
| 1028 | - if ( $ids !== array() ) { | |
| 954 | + if ( count( $ids ) > 0 ) { | |
| 1029 | 955 | $modules[ $module_name ] = $ids; |
| 1030 | 956 | } |
| 1031 | 957 | } |
| 1032 | 958 | } |
| @@ -1034,13 +960,13 @@ | ||
| 1034 | 960 | if ( empty( $modules ) ) { |
| 1035 | 961 | $modules = null; |
| 1036 | 962 | } |
| 1037 | 963 | |
| 1038 | - // Kick off a full sync. | |
| 1039 | - if ( Actions::do_full_sync( $modules, 'jetpack_cli' ) ) { | |
| 964 | + // Kick off a full sync | |
| 965 | + if ( Actions::do_full_sync( $modules ) ) { | |
| 1040 | 966 | if ( $modules ) { |
| 1041 | 967 | /* translators: %s is a comma separated list of Jetpack modules */ |
| 1042 | - WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), implode( ', ', array_keys( $modules ) ) ) ); | |
| 968 | + WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) ); | |
| 1043 | 969 | } else { |
| 1044 | 970 | WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) ); |
| 1045 | 971 | } |
| 1046 | 972 | } else { |
| @@ -1049,26 +975,26 @@ | ||
| 1049 | 975 | Settings::update_settings( $original_settings ); |
| 1050 | 976 | |
| 1051 | 977 | if ( $modules ) { |
| 1052 | 978 | /* translators: %s is a comma separated list of Jetpack modules */ |
| 1053 | - WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), implode( ', ', $modules ) ) ); | |
| 979 | + WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) ); | |
| 1054 | 980 | } else { |
| 1055 | 981 | WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) ); |
| 1056 | 982 | } |
| 1057 | 983 | } |
| 1058 | 984 | |
| 1059 | - // Keep sending to WPCOM until there's nothing to send. | |
| 985 | + // Keep sending to WPCOM until there's nothing to send | |
| 1060 | 986 | $i = 1; |
| 1061 | 987 | do { |
| 1062 | 988 | $result = Actions::$sender->do_full_sync(); |
| 1063 | 989 | if ( is_wp_error( $result ) ) { |
| 1064 | - $queue_empty_error = ( 'empty_queue_full_sync' === $result->get_error_code() ); | |
| 1065 | - if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 === $i ) ) ) { | |
| 990 | + $queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() ); | |
| 991 | + if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) { | |
| 1066 | 992 | /* translators: %s is an error code */ |
| 1067 | 993 | WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) ); |
| 1068 | 994 | } |
| 1069 | 995 | } else { |
| 1070 | - if ( 1 === $i ) { | |
| 996 | + if ( 1 == $i ) { | |
| 1071 | 997 | WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) ); |
| 1072 | 998 | } else { |
| 1073 | 999 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
| 1074 | 1000 | } |
| @@ -1073,13 +999,13 @@ | ||
| 1073 | 999 | WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) ); |
| 1074 | 1000 | } |
| 1075 | 1001 | |
| 1076 | 1002 | // Immediate Full Sync does not wait for WP.com to process data so we need to enforce a wait. |
| 1077 | - if ( Modules::get_module( 'full-sync' ) instanceof \Automattic\Jetpack\Sync\Modules\Full_Sync_Immediately ) { | |
| 1003 | + if ( false !== strpos( get_class( Modules::get_module( 'full-sync' ) ), 'Full_Sync_Immediately' ) ) { | |
| 1078 | 1004 | sleep( 15 ); |
| 1079 | 1005 | } |
| 1080 | 1006 | } |
| 1081 | - ++$i; | |
| 1007 | + $i++; | |
| 1082 | 1008 | } while ( $result && ! is_wp_error( $result ) ); |
| 1083 | 1009 | |
| 1084 | 1010 | // Reset sync settings to original. |
| 1085 | 1011 | Settings::update_settings( $original_settings ); |
| @@ -1100,12 +1026,10 @@ | ||
| 1100 | 1026 | * |
| 1101 | 1027 | * wp jetpack sync_queue full_sync peek |
| 1102 | 1028 | * |
| 1103 | 1029 | * @synopsis <incremental|full_sync> <peek> |
| 1104 | - * | |
| 1105 | - * @param array $args Positional args. | |
| 1106 | 1030 | */ |
| 1107 | - public function sync_queue( $args ) { | |
| 1031 | + public function sync_queue( $args, $assoc_args ) { | |
| 1108 | 1032 | if ( ! Actions::sync_allowed() ) { |
| 1109 | 1033 | WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) ); |
| 1110 | 1034 | } |
| 1111 | 1035 | |
| @@ -1113,13 +1037,12 @@ | ||
| 1113 | 1037 | $action = isset( $args[1] ) ? $args[1] : 'peek'; |
| 1114 | 1038 | |
| 1115 | 1039 | // We map the queue name that way we can support more friendly queue names in the commands, but still use |
| 1116 | 1040 | // the queue name that the code expects. |
| 1117 | - $allowed_queues = array( | |
| 1041 | + $queue_name_map = $allowed_queues = array( | |
| 1118 | 1042 | 'incremental' => 'sync', |
| 1119 | 1043 | 'full' => 'full_sync', |
| 1120 | 1044 | ); |
| 1121 | - $queue_name_map = $allowed_queues; | |
| 1122 | 1045 | $mapped_queue_name = isset( $queue_name_map[ $queue_name ] ) ? $queue_name_map[ $queue_name ] : $queue_name; |
| 1123 | 1046 | |
| 1124 | 1047 | switch ( $action ) { |
| 1125 | 1048 | case 'peek': |
| @@ -1133,9 +1056,9 @@ | ||
| 1133 | 1056 | $collection = array(); |
| 1134 | 1057 | foreach ( $items as $item ) { |
| 1135 | 1058 | $collection[] = array( |
| 1136 | 1059 | 'action' => $item[0], |
| 1137 | - 'args' => wp_json_encode( $item[1] ), | |
| 1060 | + 'args' => json_encode( $item[1] ), | |
| 1138 | 1061 | 'current_user_id' => $item[2], |
| 1139 | 1062 | 'microtime' => $item[3], |
| 1140 | 1063 | 'importing' => (string) $item[4], |
| 1141 | 1064 | ); |
| @@ -1165,18 +1088,14 @@ | ||
| 1165 | 1088 | * : JSON blob of WPCOM API token |
| 1166 | 1089 | * [--partner_tracking_id=<partner_tracking_id>] |
| 1167 | 1090 | * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com |
| 1168 | 1091 | * |
| 1169 | - * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>] | |
| 1170 | - * | |
| 1171 | - * @param array $args Positional args. | |
| 1172 | - * @param array $named_args Named args. | |
| 1092 | + * * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>] | |
| 1173 | 1093 | */ |
| 1174 | 1094 | public function partner_cancel( $args, $named_args ) { |
| 1175 | 1095 | list( $token_json ) = $args; |
| 1176 | 1096 | |
| 1177 | - $token = $token_json ? json_decode( $token_json ) : null; | |
| 1178 | - if ( ! $token ) { | |
| 1097 | + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { | |
| 1179 | 1098 | /* translators: %s is the invalid JSON string */ |
| 1180 | 1099 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
| 1181 | 1100 | } |
| 1182 | 1101 | |
| @@ -1187,9 +1106,9 @@ | ||
| 1187 | 1106 | if ( ! isset( $token->access_token ) ) { |
| 1188 | 1107 | $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) ); |
| 1189 | 1108 | } |
| 1190 | 1109 | |
| 1191 | - if ( Identity_Crisis::validate_sync_error_idc_option() ) { | |
| 1110 | + if ( Jetpack::validate_sync_error_idc_option() ) { | |
| 1192 | 1111 | $this->partner_provision_error( |
| 1193 | 1112 | new WP_Error( |
| 1194 | 1113 | 'site_in_safe_mode', |
| 1195 | 1114 | esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' ) |
| @@ -1199,10 +1118,9 @@ | ||
| 1199 | 1118 | |
| 1200 | 1119 | $site_identifier = Jetpack_Options::get_option( 'id' ); |
| 1201 | 1120 | |
| 1202 | 1121 | if ( ! $site_identifier ) { |
| 1203 | - $status = new Status(); | |
| 1204 | - $site_identifier = $status->get_site_suffix(); | |
| 1122 | + $site_identifier = Jetpack::build_raw_urls( get_home_url() ); | |
| 1205 | 1123 | } |
| 1206 | 1124 | |
| 1207 | 1125 | $request = array( |
| 1208 | 1126 | 'headers' => array( |
| @@ -1212,9 +1130,9 @@ | ||
| 1212 | 1130 | 'timeout' => 60, |
| 1213 | 1131 | 'method' => 'POST', |
| 1214 | 1132 | ); |
| 1215 | 1133 | |
| 1216 | - $url = sprintf( '%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier ); | |
| 1134 | + $url = sprintf( 'https://%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier ); | |
| 1217 | 1135 | if ( ! empty( $named_args ) && ! empty( $named_args['partner_tracking_id'] ) ) { |
| 1218 | 1136 | $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) ); |
| 1219 | 1137 | } |
| 1220 | 1138 | |
| @@ -1219,8 +1137,10 @@ | ||
| 1219 | 1137 | } |
| 1220 | 1138 | |
| 1221 | 1139 | $result = Client::_wp_remote_request( $url, $request ); |
| 1222 | 1140 | |
| 1141 | + Jetpack_Options::delete_option( 'onboarding' ); | |
| 1142 | + | |
| 1223 | 1143 | if ( is_wp_error( $result ) ) { |
| 1224 | 1144 | $this->partner_provision_error( $result ); |
| 1225 | 1145 | } |
| 1226 | 1146 | |
| @@ -1241,8 +1161,10 @@ | ||
| 1241 | 1161 | * [--wpcom_user_id=<user_id>] |
| 1242 | 1162 | * : WordPress.com ID of user to connect as (must be whitelisted against partner key) |
| 1243 | 1163 | * [--wpcom_user_email=<wpcom_user_email>] |
| 1244 | 1164 | * : Override the email we send to WordPress.com for registration |
| 1165 | + * [--onboarding=<onboarding>] | |
| 1166 | + * : Guide the user through an onboarding wizard | |
| 1245 | 1167 | * [--force_register=<register>] |
| 1246 | 1168 | * : Whether to force a site to register |
| 1247 | 1169 | * [--force_connect=<force_connect>] |
| 1248 | 1170 | * : Force JPS to not reuse existing credentials |
| @@ -1257,18 +1179,14 @@ | ||
| 1257 | 1179 | * |
| 1258 | 1180 | * $ wp jetpack partner_provision '{ some: "json" }' premium 1 |
| 1259 | 1181 | * { success: true } |
| 1260 | 1182 | * |
| 1261 | - * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--force_register=<register>] [--force_connect=<force_connect>] [--home_url=<home_url>] [--site_url=<site_url>] [--wpcom_user_email=<wpcom_user_email>] [--partner_tracking_id=<partner_tracking_id>] | |
| 1262 | - * | |
| 1263 | - * @param array $args Positional args. | |
| 1264 | - * @param array $named_args Named args. | |
| 1183 | + * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--onboarding=<onboarding>] [--force_register=<register>] [--force_connect=<force_connect>] [--home_url=<home_url>] [--site_url=<site_url>] [--wpcom_user_email=<wpcom_user_email>] [--partner_tracking_id=<partner_tracking_id>] | |
| 1265 | 1184 | */ |
| 1266 | 1185 | public function partner_provision( $args, $named_args ) { |
| 1267 | 1186 | list( $token_json ) = $args; |
| 1268 | 1187 | |
| 1269 | - $token = $token_json ? json_decode( $token_json ) : null; | |
| 1270 | - if ( ! $token ) { | |
| 1188 | + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) { | |
| 1271 | 1189 | /* translators: %s is the invalid JSON string */ |
| 1272 | 1190 | $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) ); |
| 1273 | 1191 | } |
| 1274 | 1192 | |
| @@ -1287,10 +1205,10 @@ | ||
| 1287 | 1205 | |
| 1288 | 1206 | $body_json = Jetpack_Provision::partner_provision( $token->access_token, $named_args ); |
| 1289 | 1207 | |
| 1290 | 1208 | if ( is_wp_error( $body_json ) ) { |
| 1291 | - WP_CLI::error( | |
| 1292 | - wp_json_encode( | |
| 1209 | + error_log( | |
| 1210 | + json_encode( | |
| 1293 | 1211 | array( |
| 1294 | 1212 | 'success' => false, |
| 1295 | 1213 | 'error_code' => $body_json->get_error_code(), |
| 1296 | 1214 | 'error_message' => $body_json->get_error_message(), |
| @@ -1299,9 +1217,9 @@ | ||
| 1299 | 1217 | ); |
| 1300 | 1218 | exit( 1 ); |
| 1301 | 1219 | } |
| 1302 | 1220 | |
| 1303 | - WP_CLI::log( wp_json_encode( $body_json ) ); | |
| 1221 | + WP_CLI::log( json_encode( $body_json ) ); | |
| 1304 | 1222 | } |
| 1305 | 1223 | |
| 1306 | 1224 | /** |
| 1307 | 1225 | * Manages your Jetpack sitemap |
| @@ -1316,14 +1234,11 @@ | ||
| 1316 | 1234 | * wp jetpack sitemap rebuild |
| 1317 | 1235 | * |
| 1318 | 1236 | * @subcommand sitemap |
| 1319 | 1237 | * @synopsis <rebuild> [--purge] |
| 1320 | - * | |
| 1321 | - * @param array $args Positional args. | |
| 1322 | - * @param array $assoc_args Named args. | |
| 1323 | 1238 | */ |
| 1324 | 1239 | public function sitemap( $args, $assoc_args ) { |
| 1325 | - if ( ! Jetpack::is_connection_ready() ) { | |
| 1240 | + if ( ! Jetpack::is_active() ) { | |
| 1326 | 1241 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
| 1327 | 1242 | } |
| 1328 | 1243 | if ( ! Jetpack::is_module_active( 'sitemaps' ) ) { |
| 1329 | 1244 | WP_CLI::error( __( 'Jetpack Sitemaps module is not currently active. Activate it first if you want to work with sitemaps.', 'jetpack' ) ); |
| @@ -1348,11 +1263,8 @@ | ||
| 1348 | 1263 | * |
| 1349 | 1264 | * wp jetpack authorize_user --token=123456789abcdef |
| 1350 | 1265 | * |
| 1351 | 1266 | * @synopsis --token=<value> |
| 1352 | - * | |
| 1353 | - * @param array $args Positional args. | |
| 1354 | - * @param array $named_args Named args. | |
| 1355 | 1267 | */ |
| 1356 | 1268 | public function authorize_user( $args, $named_args ) { |
| 1357 | 1269 | if ( ! is_user_logged_in() ) { |
| 1358 | 1270 | WP_CLI::error( __( 'Please select a user to authorize via the --user global argument.', 'jetpack' ) ); |
| @@ -1361,16 +1273,16 @@ | ||
| 1361 | 1273 | if ( empty( $named_args['token'] ) ) { |
| 1362 | 1274 | WP_CLI::error( __( 'A non-empty token argument must be passed.', 'jetpack' ) ); |
| 1363 | 1275 | } |
| 1364 | 1276 | |
| 1365 | - $is_connection_owner = ! Jetpack::connection()->has_connected_owner(); | |
| 1366 | - $current_user_id = get_current_user_id(); | |
| 1277 | + $is_master_user = ! Jetpack::is_active(); | |
| 1278 | + $current_user_id = get_current_user_id(); | |
| 1367 | 1279 | |
| 1368 | - ( new Tokens() )->update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_connection_owner ); | |
| 1280 | + Connection_Utils::update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_master_user ); | |
| 1369 | 1281 | |
| 1370 | 1282 | WP_CLI::log( wp_json_encode( $named_args ) ); |
| 1371 | 1283 | |
| 1372 | - if ( $is_connection_owner ) { | |
| 1284 | + if ( $is_master_user ) { | |
| 1373 | 1285 | /** |
| 1374 | 1286 | * Auto-enable SSO module for new Jetpack Start connections |
| 1375 | 1287 | * |
| 1376 | 1288 | * @since 5.0.0 |
| @@ -1418,14 +1330,11 @@ | ||
| 1418 | 1330 | * |
| 1419 | 1331 | * ## EXAMPLES |
| 1420 | 1332 | * |
| 1421 | 1333 | * wp jetpack call_api --resource='/sites/%d' |
| 1422 | - * | |
| 1423 | - * @param array $args Positional args. | |
| 1424 | - * @param array $named_args Named args. | |
| 1425 | 1334 | */ |
| 1426 | 1335 | public function call_api( $args, $named_args ) { |
| 1427 | - if ( ! Jetpack::is_connection_ready() ) { | |
| 1336 | + if ( ! Jetpack::is_active() ) { | |
| 1428 | 1337 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
| 1429 | 1338 | } |
| 1430 | 1339 | |
| 1431 | 1340 | $consumed_args = array( |
| @@ -1442,9 +1351,9 @@ | ||
| 1442 | 1351 | $decoded_body = ! empty( $named_args['body'] ) |
| 1443 | 1352 | ? json_decode( $named_args['body'], true ) |
| 1444 | 1353 | : false; |
| 1445 | 1354 | |
| 1446 | - $resource_url = ( ! str_contains( $named_args['resource'], '%d' ) ) | |
| 1355 | + $resource_url = ( false === strpos( $named_args['resource'], '%d' ) ) | |
| 1447 | 1356 | ? $named_args['resource'] |
| 1448 | 1357 | : sprintf( $named_args['resource'], Jetpack_Options::get_option( 'id' ) ); |
| 1449 | 1358 | |
| 1450 | 1359 | $response = Client::wpcom_json_api_request_as_blog( |
| @@ -1520,14 +1429,11 @@ | ||
| 1520 | 1429 | * ## EXAMPLES |
| 1521 | 1430 | * |
| 1522 | 1431 | * wp jetpack upload_ssh_creds --host=example.com --ssh-user=example --pass=password |
| 1523 | 1432 | * wp jetpack updload_ssh_creds --host=example.com --ssh-user=example --kpri=key |
| 1524 | - * | |
| 1525 | - * @param array $args Positional args. | |
| 1526 | - * @param array $named_args Named args. | |
| 1527 | 1433 | */ |
| 1528 | 1434 | public function upload_ssh_creds( $args, $named_args ) { |
| 1529 | - if ( ! Jetpack::is_connection_ready() ) { | |
| 1435 | + if ( ! Jetpack::is_active() ) { | |
| 1530 | 1436 | WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); |
| 1531 | 1437 | } |
| 1532 | 1438 | |
| 1533 | 1439 | $required_args = array( |
| @@ -1612,11 +1518,8 @@ | ||
| 1612 | 1518 | * |
| 1613 | 1519 | * ## EXAMPLES |
| 1614 | 1520 | * |
| 1615 | 1521 | * wp jetpack get_stats |
| 1616 | - * | |
| 1617 | - * @param array $args Positional args. | |
| 1618 | - * @param array $named_args Named args. | |
| 1619 | 1522 | */ |
| 1620 | 1523 | public function get_stats( $args, $named_args ) { |
| 1621 | 1524 | $selected_args = array_intersect_key( |
| 1622 | 1525 | $named_args, |
| @@ -1687,15 +1590,15 @@ | ||
| 1687 | 1590 | * # List all publicize connections. |
| 1688 | 1591 | * $ wp jetpack publicize list |
| 1689 | 1592 | * |
| 1690 | 1593 | * # List publicize connections for a given service. |
| 1691 | - * $ wp jetpack publicize list linkedin | |
| 1594 | + * $ wp jetpack publicize list twitter | |
| 1692 | 1595 | * |
| 1693 | 1596 | * # List all publicize connections for a given user. |
| 1694 | 1597 | * $ wp --user=1 jetpack publicize list |
| 1695 | 1598 | * |
| 1696 | 1599 | * # List all publicize connections for a given user and service. |
| 1697 | - * $ wp --user=1 jetpack publicize list linkedin | |
| 1600 | + * $ wp --user=1 jetpack publicize list twitter | |
| 1698 | 1601 | * |
| 1699 | 1602 | * # Display details for a given connection. |
| 1700 | 1603 | * $ wp jetpack publicize list 123456 |
| 1701 | 1604 | * |
| @@ -1705,20 +1608,17 @@ | ||
| 1705 | 1608 | * # Disconnect all connections. |
| 1706 | 1609 | * $ wp jetpack publicize disconnect all |
| 1707 | 1610 | * |
| 1708 | 1611 | * # Disconnect all connections for a given service. |
| 1709 | - * $ wp jetpack publicize disconnect linkedin | |
| 1710 | - * | |
| 1711 | - * @param array $args Positional args. | |
| 1712 | - * @param array $named_args Named args. | |
| 1612 | + * $ wp jetpack publicize disconnect twitter | |
| 1713 | 1613 | */ |
| 1714 | 1614 | public function publicize( $args, $named_args ) { |
| 1715 | - if ( ! Jetpack::connection()->has_connected_owner() ) { | |
| 1716 | - WP_CLI::error( __( 'Jetpack Social requires a user-level connection to WordPress.com', 'jetpack' ) ); | |
| 1615 | + if ( ! Jetpack::is_active() ) { | |
| 1616 | + WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) ); | |
| 1717 | 1617 | } |
| 1718 | 1618 | |
| 1719 | 1619 | if ( ! Jetpack::is_module_active( 'publicize' ) ) { |
| 1720 | - WP_CLI::error( __( 'The Jetpack Social module is not active.', 'jetpack' ) ); | |
| 1620 | + WP_CLI::error( __( 'The publicize module is not active.', 'jetpack' ) ); | |
| 1721 | 1621 | } |
| 1722 | 1622 | |
| 1723 | 1623 | if ( ( new Status() )->is_offline_mode() ) { |
| 1724 | 1624 | if ( |
| @@ -1724,18 +1624,18 @@ | ||
| 1724 | 1624 | if ( |
| 1725 | 1625 | ! defined( 'JETPACK_DEV_DEBUG' ) && |
| 1726 | 1626 | ! has_filter( 'jetpack_development_mode' ) && |
| 1727 | 1627 | ! has_filter( 'jetpack_offline_mode' ) && |
| 1728 | - ! str_contains( site_url(), '.' ) | |
| 1628 | + false === strpos( site_url(), '.' ) | |
| 1729 | 1629 | ) { |
| 1730 | - WP_CLI::error( __( "Jetpack is current in offline mode because the site url does not contain a '.', which often occurs when dynamically setting the WP_SITEURL constant. While in offline mode, the Jetpack Social module will not load.", 'jetpack' ) ); | |
| 1630 | + WP_CLI::error( __( "Jetpack is current in offline mode because the site url does not contain a '.', which often occurs when dynamically setting the WP_SITEURL constant. While in offline mode, the publicize module will not load.", 'jetpack' ) ); | |
| 1731 | 1631 | } |
| 1732 | 1632 | |
| 1733 | - WP_CLI::error( __( 'Jetpack is currently in offline mode, so the Jetpack Social module will not load.', 'jetpack' ) ); | |
| 1633 | + WP_CLI::error( __( 'Jetpack is currently in offline mode, so the publicize module will not load.', 'jetpack' ) ); | |
| 1734 | 1634 | } |
| 1735 | 1635 | |
| 1736 | - if ( ! class_exists( Publicize::class ) ) { | |
| 1737 | - WP_CLI::error( __( 'The Jetpack Social module is not loaded.', 'jetpack' ) ); | |
| 1636 | + if ( ! class_exists( 'Publicize' ) ) { | |
| 1637 | + WP_CLI::error( __( 'The publicize module is not loaded.', 'jetpack' ) ); | |
| 1738 | 1638 | } |
| 1739 | 1639 | |
| 1740 | 1640 | $action = $args[0]; |
| 1741 | 1641 | $publicize = new Publicize(); |
| @@ -1818,12 +1718,12 @@ | ||
| 1818 | 1718 | // If the connection ID is 'all' then delete all connections. If the connection ID |
| 1819 | 1719 | // matches a service, delete all connections for that service. |
| 1820 | 1720 | if ( 'all' === $identifier || $id_is_service ) { |
| 1821 | 1721 | if ( 'all' === $identifier ) { |
| 1822 | - WP_CLI::log( __( "You're about to delete all Jetpack Social connections.", 'jetpack' ) ); | |
| 1722 | + WP_CLI::log( __( "You're about to delete all publicize connections.", 'jetpack' ) ); | |
| 1823 | 1723 | } else { |
| 1824 | 1724 | /* translators: %s is a lowercase string for a social network. */ |
| 1825 | - WP_CLI::log( sprintf( __( "You're about to delete all Jetpack Social connections to %s.", 'jetpack' ), $identifier ) ); | |
| 1725 | + WP_CLI::log( sprintf( __( "You're about to delete all publicize connections to %s.", 'jetpack' ), $identifier ) ); | |
| 1826 | 1726 | } |
| 1827 | 1727 | |
| 1828 | 1728 | jetpack_cli_are_you_sure(); |
| 1829 | 1729 | |
| @@ -1844,9 +1744,9 @@ | ||
| 1844 | 1744 | $connections = $option_connections[ $service ]; |
| 1845 | 1745 | } |
| 1846 | 1746 | |
| 1847 | 1747 | if ( ! empty( $connections ) ) { |
| 1848 | - $count = is_countable( $connections ) ? count( $connections ) : 0; | |
| 1748 | + $count = count( $connections ); | |
| 1849 | 1749 | $progress = \WP_CLI\Utils\make_progress_bar( |
| 1850 | 1750 | /* translators: %s is a lowercase string for a social network. */ |
| 1851 | 1751 | sprintf( __( 'Disconnecting all connections to %s.', 'jetpack' ), $service ), |
| 1852 | 1752 | $count |
| @@ -1856,58 +1756,47 @@ | ||
| 1856 | 1756 | if ( false === $publicize->disconnect( false, $id ) ) { |
| 1857 | 1757 | WP_CLI::error( |
| 1858 | 1758 | sprintf( |
| 1859 | 1759 | /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */ |
| 1860 | - __( 'Jetpack Social connection %d could not be disconnected', 'jetpack' ), | |
| 1760 | + __( 'Publicize connection %d could not be disconnected', 'jetpack' ), | |
| 1861 | 1761 | $id |
| 1862 | 1762 | ) |
| 1863 | 1763 | ); |
| 1864 | 1764 | } |
| 1865 | 1765 | |
| 1866 | - // @phan-suppress-next-line PhanUndeclaredClassMethod - Class is missing from php-stubs/wp-cli-stubs 🤷 | |
| 1867 | 1766 | $progress->tick(); |
| 1868 | 1767 | } |
| 1869 | 1768 | |
| 1870 | - // @phan-suppress-next-line PhanUndeclaredClassMethod - Class is missing from php-stubs/wp-cli-stubs 🤷 | |
| 1871 | 1769 | $progress->finish(); |
| 1872 | 1770 | |
| 1873 | 1771 | if ( 'all' === $service ) { |
| 1874 | - WP_CLI::success( __( 'All Jetpack Social connections were successfully disconnected.', 'jetpack' ) ); | |
| 1772 | + WP_CLI::success( __( 'All publicize connections were successfully disconnected.', 'jetpack' ) ); | |
| 1875 | 1773 | } else { |
| 1876 | 1774 | /* translators: %s is a lowercase string for a social network. */ |
| 1877 | - WP_CLI::success( sprintf( __( 'All Jetpack Social connections to %s were successfully disconnected.', 'jetpack' ), $service ) ); | |
| 1775 | + WP_CLI::success( __( 'All publicize connections to %s were successfully disconnected.', 'jetpack' ), $service ); | |
| 1878 | 1776 | } |
| 1879 | 1777 | } |
| 1880 | - } elseif ( false !== $publicize->disconnect( false, $identifier ) ) { | |
| 1881 | - /* translators: %d is a numeric ID. Example: 1234. */ | |
| 1882 | - WP_CLI::success( sprintf( __( 'Jetpack Social connection %d has been disconnected.', 'jetpack' ), $identifier ) ); | |
| 1883 | 1778 | } else { |
| 1884 | - /* translators: %d is a numeric ID. Example: 1234. */ | |
| 1885 | - WP_CLI::error( sprintf( __( 'Jetpack Social connection %d could not be disconnected.', 'jetpack' ), $identifier ) ); | |
| 1779 | + if ( false !== $publicize->disconnect( false, $identifier ) ) { | |
| 1780 | + /* translators: %d is a numeric ID. Example: 1234. */ | |
| 1781 | + WP_CLI::success( sprintf( __( 'Publicize connection %d has been disconnected.', 'jetpack' ), $identifier ) ); | |
| 1782 | + } else { | |
| 1783 | + /* translators: %d is a numeric ID. Example: 1234. */ | |
| 1784 | + WP_CLI::error( sprintf( __( 'Publicize connection %d could not be disconnected.', 'jetpack' ), $identifier ) ); | |
| 1785 | + } | |
| 1886 | 1786 | } |
| 1887 | 1787 | break; // disconnect. |
| 1888 | 1788 | } |
| 1889 | 1789 | } |
| 1890 | 1790 | |
| 1891 | - /** | |
| 1892 | - * Get the API host. | |
| 1893 | - * | |
| 1894 | - * @return string URL. | |
| 1895 | - */ | |
| 1896 | 1791 | private function get_api_host() { |
| 1897 | 1792 | $env_api_host = getenv( 'JETPACK_START_API_HOST', true ); |
| 1898 | - return $env_api_host ? 'https://' . $env_api_host : JETPACK__WPCOM_JSON_API_BASE; | |
| 1793 | + return $env_api_host ? $env_api_host : JETPACK__WPCOM_JSON_API_HOST; | |
| 1899 | 1794 | } |
| 1900 | 1795 | |
| 1901 | - /** | |
| 1902 | - * Log and exit on a partner provision error. | |
| 1903 | - * | |
| 1904 | - * @param WP_Error $error Error. | |
| 1905 | - * @return never | |
| 1906 | - */ | |
| 1907 | 1796 | private function partner_provision_error( $error ) { |
| 1908 | 1797 | WP_CLI::log( |
| 1909 | - wp_json_encode( | |
| 1798 | + json_encode( | |
| 1910 | 1799 | array( |
| 1911 | 1800 | 'success' => false, |
| 1912 | 1801 | 'error_code' => $error->get_error_code(), |
| 1913 | 1802 | 'error_message' => $error->get_error_message(), |
| @@ -1965,21 +1854,19 @@ | ||
| 1965 | 1854 | * @param array $args Positional parameters. Only one is used, that corresponds to the block title. |
| 1966 | 1855 | * @param array $assoc_args Associative parameters defined in the scaffold() method. |
| 1967 | 1856 | */ |
| 1968 | 1857 | public function block( $args, $assoc_args ) { |
| 1969 | - if ( ! isset( $args[1] ) ) { | |
| 1858 | + if ( isset( $args[1] ) ) { | |
| 1859 | + $title = ucwords( $args[1] ); | |
| 1860 | + } else { | |
| 1970 | 1861 | WP_CLI::error( esc_html__( 'The title parameter is required.', 'jetpack' ) . ' 👻' ); |
| 1971 | 1862 | exit( 1 ); |
| 1972 | 1863 | } |
| 1973 | 1864 | |
| 1974 | - $title = ucwords( $args[1] ); | |
| 1975 | - | |
| 1976 | 1865 | $slug = isset( $assoc_args['slug'] ) |
| 1977 | 1866 | ? $assoc_args['slug'] |
| 1978 | 1867 | : sanitize_title( $title ); |
| 1979 | 1868 | |
| 1980 | - $next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh | |
| 1981 | - | |
| 1982 | 1869 | $variation_options = array( 'production', 'experimental', 'beta' ); |
| 1983 | 1870 | $variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) ) |
| 1984 | 1871 | ? $assoc_args['variation'] |
| 1985 | 1872 | : 'beta'; |
| @@ -2006,40 +1893,43 @@ | ||
| 2006 | 1893 | } |
| 2007 | 1894 | |
| 2008 | 1895 | $wp_filesystem->mkdir( $path ); |
| 2009 | 1896 | |
| 2010 | - $keywords = isset( $assoc_args['keywords'] ) | |
| 2011 | - ? array_map( | |
| 2012 | - function ( $keyword ) { | |
| 2013 | - return trim( $keyword ); | |
| 2014 | - }, | |
| 2015 | - array_slice( explode( ',', $assoc_args['keywords'] ), 0, 3 ) | |
| 2016 | - ) | |
| 2017 | - : array(); | |
| 1897 | + $hasKeywords = isset( $assoc_args['keywords'] ); | |
| 2018 | 1898 | |
| 2019 | 1899 | $files = array( |
| 2020 | - "$path/block.json" => self::render_block_file( | |
| 2021 | - 'block-block-json', | |
| 1900 | + "$path/$slug.php" => $this->render_block_file( | |
| 1901 | + 'block-register-php', | |
| 2022 | 1902 | array( |
| 2023 | - 'slug' => $slug, | |
| 2024 | - 'title' => wp_json_encode( $title, JSON_UNESCAPED_UNICODE ), | |
| 2025 | - 'description' => isset( $assoc_args['description'] ) | |
| 2026 | - ? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_UNICODE ) | |
| 2027 | - : wp_json_encode( $title, JSON_UNESCAPED_UNICODE ), | |
| 2028 | - 'nextVersion' => $next_version, | |
| 2029 | - 'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_UNICODE ), | |
| 1903 | + 'slug' => $slug, | |
| 1904 | + 'title' => $title, | |
| 1905 | + 'underscoredSlug' => str_replace( '-', '_', $slug ), | |
| 1906 | + 'underscoredTitle' => str_replace( ' ', '_', $title ), | |
| 1907 | + 'jetpackVersion' => substr( JETPACK__VERSION, 0, strpos( JETPACK__VERSION, '.' ) ) . '.x', | |
| 2030 | 1908 | ) |
| 2031 | 1909 | ), |
| 2032 | - "$path/$slug.php" => self::render_block_file( | |
| 2033 | - 'block-register-php', | |
| 1910 | + "$path/index.js" => $this->render_block_file( | |
| 1911 | + 'block-index-js', | |
| 2034 | 1912 | array( |
| 2035 | - 'nextVersion' => $next_version, | |
| 2036 | - 'title' => $title, | |
| 2037 | - 'underscoredTitle' => str_replace( ' ', '_', $title ), | |
| 1913 | + 'slug' => $slug, | |
| 1914 | + 'title' => $title, | |
| 1915 | + 'description' => isset( $assoc_args['description'] ) | |
| 1916 | + ? $assoc_args['description'] | |
| 1917 | + : $title, | |
| 1918 | + 'keywords' => $hasKeywords | |
| 1919 | + ? array_map( | |
| 1920 | + function( $keyword ) { | |
| 1921 | + // Construction necessary for Mustache lists | |
| 1922 | + return array( 'keyword' => trim( $keyword ) ); | |
| 1923 | + }, | |
| 1924 | + explode( ',', $assoc_args['keywords'], 3 ) | |
| 1925 | + ) | |
| 1926 | + : '', | |
| 1927 | + 'hasKeywords' => $hasKeywords, | |
| 2038 | 1928 | ) |
| 2039 | 1929 | ), |
| 2040 | - "$path/editor.js" => self::render_block_file( 'block-editor-js' ), | |
| 2041 | - "$path/editor.scss" => self::render_block_file( | |
| 1930 | + "$path/editor.js" => $this->render_block_file( 'block-editor-js' ), | |
| 1931 | + "$path/editor.scss" => $this->render_block_file( | |
| 2042 | 1932 | 'block-editor-scss', |
| 2043 | 1933 | array( |
| 2044 | 1934 | 'slug' => $slug, |
| 2045 | 1935 | 'title' => $title, |
| @@ -2044,9 +1934,9 @@ | ||
| 2044 | 1934 | 'slug' => $slug, |
| 2045 | 1935 | 'title' => $title, |
| 2046 | 1936 | ) |
| 2047 | 1937 | ), |
| 2048 | - "$path/edit.js" => self::render_block_file( | |
| 1938 | + "$path/edit.js" => $this->render_block_file( | |
| 2049 | 1939 | 'block-edit-js', |
| 2050 | 1940 | array( |
| 2051 | 1941 | 'title' => $title, |
| 2052 | 1942 | 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ), |
| @@ -2051,8 +1941,10 @@ | ||
| 2051 | 1941 | 'title' => $title, |
| 2052 | 1942 | 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ), |
| 2053 | 1943 | ) |
| 2054 | 1944 | ), |
| 1945 | + "$path/icon.js" => $this->render_block_file( 'block-icon-js' ), | |
| 1946 | + "$path/attributes.js" => $this->render_block_file( 'block-attributes-js' ), | |
| 2055 | 1947 | ); |
| 2056 | 1948 | |
| 2057 | 1949 | $files_written = array(); |
| 2058 | 1950 | |
| @@ -2098,10 +1990,10 @@ | ||
| 2098 | 1990 | |
| 2099 | 1991 | if ( 'beta' === $variation || 'experimental' === $variation ) { |
| 2100 | 1992 | $block_constant = sprintf( |
| 2101 | 1993 | /* translators: the placeholder is a constant name */ |
| 2102 | - esc_html__( 'To load the block, add the constant JETPACK_BLOCKS_VARIATION set to %1$s to your wp-config.php file', 'jetpack' ), | |
| 2103 | - $variation | |
| 1994 | + esc_html__( 'To load the block, add the constant %1$s as true to your wp-config.php file', 'jetpack' ), | |
| 1995 | + ( 'beta' === $variation ? 'JETPACK_BETA_BLOCKS' : 'JETPACK_EXPERIMENTAL_BLOCKS' ) | |
| 2104 | 1996 | ); |
| 2105 | 1997 | } else { |
| 2106 | 1998 | $block_constant = ''; |
| 2107 | 1999 | } |
| @@ -2112,9 +2004,9 @@ | ||
| 2112 | 2004 | esc_html__( 'Successfully created block %1$s with slug %2$s', 'jetpack' ) . ' 🎉' . "\n" . |
| 2113 | 2005 | "--------------------------------------------------------------------------------------------------------------------\n" . |
| 2114 | 2006 | /* translators: the placeholder is a directory path */ |
| 2115 | 2007 | esc_html__( 'The files were created at %3$s', 'jetpack' ) . "\n" . |
| 2116 | - esc_html__( 'To start using the block, build the blocks with pnpm run build-extensions', 'jetpack' ) . "\n" . | |
| 2008 | + esc_html__( 'To start using the block, build the blocks with yarn run build-extensions', 'jetpack' ) . "\n" . | |
| 2117 | 2009 | /* translators: the placeholder is a file path */ |
| 2118 | 2010 | esc_html__( 'The block slug has been added to the %4$s list at %5$s', 'jetpack' ) . "\n" . |
| 2119 | 2011 | '%6$s' . "\n" . |
| 2120 | 2012 | /* translators: the placeholder is a URL */ |
| @@ -2124,9 +2016,9 @@ | ||
| 2124 | 2016 | $path, |
| 2125 | 2017 | $variation, |
| 2126 | 2018 | $block_list_path, |
| 2127 | 2019 | $block_constant, |
| 2128 | - 'https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#developing-block-editor-extensions-in-jetpack' | |
| 2020 | + 'https://github.com/Automattic/jetpack/blob/master/extensions/README.md#develop-new-blocks' | |
| 2129 | 2021 | ) . '--------------------------------------------------------------------------------------------------------------------' |
| 2130 | 2022 | ); |
| 2131 | 2023 | } |
| 2132 | 2024 | } |
| @@ -2133,10 +2025,11 @@ | ||
| 2133 | 2025 | |
| 2134 | 2026 | /** |
| 2135 | 2027 | * Built the file replacing the placeholders in the template with the data supplied. |
| 2136 | 2028 | * |
| 2137 | - * @param string $template Template. | |
| 2138 | - * @param array $data Data. | |
| 2029 | + * @param string $template | |
| 2030 | + * @param array $data | |
| 2031 | + * | |
| 2139 | 2032 | * @return string mixed |
| 2140 | 2033 | */ |
| 2141 | 2034 | private static function render_block_file( $template, $data = array() ) { |
| 2142 | 2035 | return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data ); |
| @@ -2142,23 +2035,21 @@ | ||
| 2142 | 2035 | return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data ); |
| 2143 | 2036 | } |
| 2144 | 2037 | } |
| 2145 | 2038 | |
| 2146 | -// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file. | |
| 2147 | - | |
| 2148 | -/** | |
| 2039 | +/* | |
| 2149 | 2040 | * Standard "ask for permission to continue" function. |
| 2150 | 2041 | * If action cancelled, ask if they need help. |
| 2151 | 2042 | * |
| 2152 | 2043 | * Written outside of the class so it's not listed as an executable command w/ 'wp jetpack' |
| 2153 | 2044 | * |
| 2154 | - * @param bool $flagged false = normal option | true = flagged by get_jetpack_options_for_reset(). | |
| 2155 | - * @param string $error_msg Error message. | |
| 2045 | + * @param $flagged bool false = normal option | true = flagged by get_jetpack_options_for_reset() | |
| 2046 | + * @param $error_msg string (optional) | |
| 2156 | 2047 | */ |
| 2157 | 2048 | function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) { |
| 2158 | 2049 | $cli = new Jetpack_CLI(); |
| 2159 | 2050 | |
| 2160 | - // Default cancellation message. | |
| 2051 | + // Default cancellation message | |
| 2161 | 2052 | if ( ! $error_msg ) { |
| 2162 | 2053 | $error_msg = |
| 2163 | 2054 | __( 'Action cancelled. Have a question?', 'jetpack' ) |
| 2164 | 2055 | . ' ' |
| @@ -2169,14 +2060,14 @@ | ||
| 2169 | 2060 | |
| 2170 | 2061 | if ( ! $flagged ) { |
| 2171 | 2062 | $prompt_message = _x( 'Are you sure? This cannot be undone. Type "yes" to continue:', '"yes" is a command - do not translate.', 'jetpack' ); |
| 2172 | 2063 | } else { |
| 2173 | - $prompt_message = _x( 'Are you sure? Modifying this option may disrupt your Jetpack connection. Type "yes" to continue.', '"yes" is a command - do not translate.', 'jetpack' ); | |
| 2064 | + $prompt_message = _x( 'Are you sure? Modifying this option may disrupt your Jetpack connection. Type "yes" to continue.', '"yes" is a command - do not translate.', 'jetpack' ); | |
| 2174 | 2065 | } |
| 2175 | 2066 | |
| 2176 | 2067 | WP_CLI::line( $prompt_message ); |
| 2177 | 2068 | $handle = fopen( 'php://stdin', 'r' ); |
| 2178 | 2069 | $line = fgets( $handle ); |
| 2179 | - if ( 'yes' !== trim( $line ) ) { | |
| 2070 | + if ( 'yes' != trim( $line ) ) { | |
| 2180 | 2071 | WP_CLI::error( $error_msg ); |
| 2181 | 2072 | } |
| 2182 | 2073 | } |