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