PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.2
Jetpack – WP Security, Backup, Speed, & Growth v9.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 +183 -295 13.6.29.2 View file →
@@ -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.
964 + // Kick off a full sync
1039 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' )
@@ -1262,17 +1181,13 @@
1262 1181 * $ wp jetpack partner_provision '{ some: "json" }' premium 1
1263 1182 * { success: true }
1264 1183 *
1265 1184 * @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 1185 */
1270 1186 public function partner_provision( $args, $named_args ) {
1271 1187 list( $token_json ) = $args;
1272 1188
1273 - $token = $token_json ? json_decode( $token_json ) : null;
1274 - if ( ! $token ) {
1189 + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
1275 1190 /* translators: %s is the invalid JSON string */
1276 1191 $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1277 1192 }
1278 1193
@@ -1291,10 +1206,10 @@
1291 1206
1292 1207 $body_json = Jetpack_Provision::partner_provision( $token->access_token, $named_args );
1293 1208
1294 1209 if ( is_wp_error( $body_json ) ) {
1295 - WP_CLI::error(
1296 - wp_json_encode(
1210 + error_log(
1211 + json_encode(
1297 1212 array(
1298 1213 'success' => false,
1299 1214 'error_code' => $body_json->get_error_code(),
1300 1215 'error_message' => $body_json->get_error_message(),
@@ -1303,9 +1218,9 @@
1303 1218 );
1304 1219 exit( 1 );
1305 1220 }
1306 1221
1307 - WP_CLI::log( wp_json_encode( $body_json ) );
1222 + WP_CLI::log( json_encode( $body_json ) );
1308 1223 }
1309 1224
1310 1225 /**
1311 1226 * Manages your Jetpack sitemap
@@ -1320,14 +1235,11 @@
1320 1235 * wp jetpack sitemap rebuild
1321 1236 *
1322 1237 * @subcommand sitemap
1323 1238 * @synopsis <rebuild> [--purge]
1324 - *
1325 - * @param array $args Positional args.
1326 - * @param array $assoc_args Named args.
1327 1239 */
1328 1240 public function sitemap( $args, $assoc_args ) {
1329 - if ( ! Jetpack::is_connection_ready() ) {
1241 + if ( ! Jetpack::is_active() ) {
1330 1242 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1331 1243 }
1332 1244 if ( ! Jetpack::is_module_active( 'sitemaps' ) ) {
1333 1245 WP_CLI::error( __( 'Jetpack Sitemaps module is not currently active. Activate it first if you want to work with sitemaps.', 'jetpack' ) );
@@ -1352,11 +1264,8 @@
1352 1264 *
1353 1265 * wp jetpack authorize_user --token=123456789abcdef
1354 1266 *
1355 1267 * @synopsis --token=<value>
1356 - *
1357 - * @param array $args Positional args.
1358 - * @param array $named_args Named args.
1359 1268 */
1360 1269 public function authorize_user( $args, $named_args ) {
1361 1270 if ( ! is_user_logged_in() ) {
1362 1271 WP_CLI::error( __( 'Please select a user to authorize via the --user global argument.', 'jetpack' ) );
@@ -1365,16 +1274,16 @@
1365 1274 if ( empty( $named_args['token'] ) ) {
1366 1275 WP_CLI::error( __( 'A non-empty token argument must be passed.', 'jetpack' ) );
1367 1276 }
1368 1277
1369 - $is_connection_owner = ! Jetpack::connection()->has_connected_owner();
1370 - $current_user_id = get_current_user_id();
1278 + $is_master_user = ! Jetpack::is_active();
1279 + $current_user_id = get_current_user_id();
1371 1280
1372 - ( new Tokens() )->update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_connection_owner );
1281 + Connection_Utils::update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_master_user );
1373 1282
1374 1283 WP_CLI::log( wp_json_encode( $named_args ) );
1375 1284
1376 - if ( $is_connection_owner ) {
1285 + if ( $is_master_user ) {
1377 1286 /**
1378 1287 * Auto-enable SSO module for new Jetpack Start connections
1379 1288 *
1380 1289 * @since 5.0.0
@@ -1422,14 +1331,11 @@
1422 1331 *
1423 1332 * ## EXAMPLES
1424 1333 *
1425 1334 * wp jetpack call_api --resource='/sites/%d'
1426 - *
1427 - * @param array $args Positional args.
1428 - * @param array $named_args Named args.
1429 1335 */
1430 1336 public function call_api( $args, $named_args ) {
1431 - if ( ! Jetpack::is_connection_ready() ) {
1337 + if ( ! Jetpack::is_active() ) {
1432 1338 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1433 1339 }
1434 1340
1435 1341 $consumed_args = array(
@@ -1446,9 +1352,9 @@
1446 1352 $decoded_body = ! empty( $named_args['body'] )
1447 1353 ? json_decode( $named_args['body'], true )
1448 1354 : false;
1449 1355
1450 - $resource_url = ( ! str_contains( $named_args['resource'], '%d' ) )
1356 + $resource_url = ( false === strpos( $named_args['resource'], '%d' ) )
1451 1357 ? $named_args['resource']
1452 1358 : sprintf( $named_args['resource'], Jetpack_Options::get_option( 'id' ) );
1453 1359
1454 1360 $response = Client::wpcom_json_api_request_as_blog(
@@ -1524,14 +1430,11 @@
1524 1430 * ## EXAMPLES
1525 1431 *
1526 1432 * wp jetpack upload_ssh_creds --host=example.com --ssh-user=example --pass=password
1527 1433 * 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 1434 */
1532 1435 public function upload_ssh_creds( $args, $named_args ) {
1533 - if ( ! Jetpack::is_connection_ready() ) {
1436 + if ( ! Jetpack::is_active() ) {
1534 1437 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1535 1438 }
1536 1439
1537 1440 $required_args = array(
@@ -1616,11 +1519,8 @@
1616 1519 *
1617 1520 * ## EXAMPLES
1618 1521 *
1619 1522 * wp jetpack get_stats
1620 - *
1621 - * @param array $args Positional args.
1622 - * @param array $named_args Named args.
1623 1523 */
1624 1524 public function get_stats( $args, $named_args ) {
1625 1525 $selected_args = array_intersect_key(
1626 1526 $named_args,
@@ -1691,15 +1591,15 @@
1691 1591 * # List all publicize connections.
1692 1592 * $ wp jetpack publicize list
1693 1593 *
1694 1594 * # List publicize connections for a given service.
1695 - * $ wp jetpack publicize list linkedin
1595 + * $ wp jetpack publicize list twitter
1696 1596 *
1697 1597 * # List all publicize connections for a given user.
1698 1598 * $ wp --user=1 jetpack publicize list
1699 1599 *
1700 1600 * # List all publicize connections for a given user and service.
1701 - * $ wp --user=1 jetpack publicize list linkedin
1601 + * $ wp --user=1 jetpack publicize list twitter
1702 1602 *
1703 1603 * # Display details for a given connection.
1704 1604 * $ wp jetpack publicize list 123456
1705 1605 *
@@ -1709,20 +1609,17 @@
1709 1609 * # Disconnect all connections.
1710 1610 * $ wp jetpack publicize disconnect all
1711 1611 *
1712 1612 * # 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.
1613 + * $ wp jetpack publicize disconnect twitter
1717 1614 */
1718 1615 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' ) );
1616 + if ( ! Jetpack::is_active() ) {
1617 + WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1721 1618 }
1722 1619
1723 1620 if ( ! Jetpack::is_module_active( 'publicize' ) ) {
1724 - WP_CLI::error( __( 'The Jetpack Social module is not active.', 'jetpack' ) );
1621 + WP_CLI::error( __( 'The publicize module is not active.', 'jetpack' ) );
1725 1622 }
1726 1623
1727 1624 if ( ( new Status() )->is_offline_mode() ) {
1728 1625 if (
@@ -1728,18 +1625,18 @@
1728 1625 if (
1729 1626 ! defined( 'JETPACK_DEV_DEBUG' ) &&
1730 1627 ! has_filter( 'jetpack_development_mode' ) &&
1731 1628 ! has_filter( 'jetpack_offline_mode' ) &&
1732 - ! str_contains( site_url(), '.' )
1629 + false === strpos( site_url(), '.' )
1733 1630 ) {
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' ) );
1631 + 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' ) );
1735 1632 }
1736 1633
1737 - WP_CLI::error( __( 'Jetpack is currently in offline mode, so the Jetpack Social module will not load.', 'jetpack' ) );
1634 + WP_CLI::error( __( 'Jetpack is currently in offline mode, so the publicize module will not load.', 'jetpack' ) );
1738 1635 }
1739 1636
1740 - if ( ! class_exists( Publicize::class ) ) {
1741 - WP_CLI::error( __( 'The Jetpack Social module is not loaded.', 'jetpack' ) );
1637 + if ( ! class_exists( 'Publicize' ) ) {
1638 + WP_CLI::error( __( 'The publicize module is not loaded.', 'jetpack' ) );
1742 1639 }
1743 1640
1744 1641 $action = $args[0];
1745 1642 $publicize = new Publicize();
@@ -1822,12 +1719,12 @@
1822 1719 // If the connection ID is 'all' then delete all connections. If the connection ID
1823 1720 // matches a service, delete all connections for that service.
1824 1721 if ( 'all' === $identifier || $id_is_service ) {
1825 1722 if ( 'all' === $identifier ) {
1826 - WP_CLI::log( __( "You're about to delete all Jetpack Social connections.", 'jetpack' ) );
1723 + WP_CLI::log( __( "You're about to delete all publicize connections.", 'jetpack' ) );
1827 1724 } else {
1828 1725 /* 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 ) );
1726 + WP_CLI::log( sprintf( __( "You're about to delete all publicize connections to %s.", 'jetpack' ), $identifier ) );
1830 1727 }
1831 1728
1832 1729 jetpack_cli_are_you_sure();
1833 1730
@@ -1848,9 +1745,9 @@
1848 1745 $connections = $option_connections[ $service ];
1849 1746 }
1850 1747
1851 1748 if ( ! empty( $connections ) ) {
1852 - $count = is_countable( $connections ) ? count( $connections ) : 0;
1749 + $count = count( $connections );
1853 1750 $progress = \WP_CLI\Utils\make_progress_bar(
1854 1751 /* translators: %s is a lowercase string for a social network. */
1855 1752 sprintf( __( 'Disconnecting all connections to %s.', 'jetpack' ), $service ),
1856 1753 $count
@@ -1860,58 +1757,47 @@
1860 1757 if ( false === $publicize->disconnect( false, $id ) ) {
1861 1758 WP_CLI::error(
1862 1759 sprintf(
1863 1760 /* 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' ),
1761 + __( 'Publicize connection %d could not be disconnected', 'jetpack' ),
1865 1762 $id
1866 1763 )
1867 1764 );
1868 1765 }
1869 1766
1870 - // @phan-suppress-next-line PhanUndeclaredClassMethod - Class is missing from php-stubs/wp-cli-stubs 🤷
1871 1767 $progress->tick();
1872 1768 }
1873 1769
1874 - // @phan-suppress-next-line PhanUndeclaredClassMethod - Class is missing from php-stubs/wp-cli-stubs 🤷
1875 1770 $progress->finish();
1876 1771
1877 1772 if ( 'all' === $service ) {
1878 - WP_CLI::success( __( 'All Jetpack Social connections were successfully disconnected.', 'jetpack' ) );
1773 + WP_CLI::success( __( 'All publicize connections were successfully disconnected.', 'jetpack' ) );
1879 1774 } else {
1880 1775 /* 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 );
1776 + WP_CLI::success( __( 'All publicize connections to %s were successfully disconnected.', 'jetpack' ), $service );
1882 1777 }
1883 1778 }
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 1779 } 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 ) );
1780 + if ( false !== $publicize->disconnect( false, $identifier ) ) {
1781 + /* translators: %d is a numeric ID. Example: 1234. */
1782 + WP_CLI::success( sprintf( __( 'Publicize connection %d has been disconnected.', 'jetpack' ), $identifier ) );
1783 + } else {
1784 + /* translators: %d is a numeric ID. Example: 1234. */
1785 + WP_CLI::error( sprintf( __( 'Publicize connection %d could not be disconnected.', 'jetpack' ), $identifier ) );
1786 + }
1890 1787 }
1891 1788 break; // disconnect.
1892 1789 }
1893 1790 }
1894 1791
1895 - /**
1896 - * Get the API host.
1897 - *
1898 - * @return string URL.
1899 - */
1900 1792 private function get_api_host() {
1901 1793 $env_api_host = getenv( 'JETPACK_START_API_HOST', true );
1902 1794 return $env_api_host ? 'https://' . $env_api_host : JETPACK__WPCOM_JSON_API_BASE;
1903 1795 }
1904 1796
1905 - /**
1906 - * Log and exit on a partner provision error.
1907 - *
1908 - * @param WP_Error $error Error.
1909 - * @return never
1910 - */
1911 1797 private function partner_provision_error( $error ) {
1912 1798 WP_CLI::log(
1913 - wp_json_encode(
1799 + json_encode(
1914 1800 array(
1915 1801 'success' => false,
1916 1802 'error_code' => $error->get_error_code(),
1917 1803 'error_message' => $error->get_error_message(),
@@ -1980,10 +1866,8 @@
1980 1866 $slug = isset( $assoc_args['slug'] )
1981 1867 ? $assoc_args['slug']
1982 1868 : sanitize_title( $title );
1983 1869
1984 - $next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh
1985 -
1986 1870 $variation_options = array( 'production', 'experimental', 'beta' );
1987 1871 $variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) )
1988 1872 ? $assoc_args['variation']
1989 1873 : 'beta';
@@ -2010,40 +1894,43 @@
2010 1894 }
2011 1895
2012 1896 $wp_filesystem->mkdir( $path );
2013 1897
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();
1898 + $hasKeywords = isset( $assoc_args['keywords'] );
2022 1899
2023 1900 $files = array(
2024 - "$path/block.json" => self::render_block_file(
2025 - 'block-block-json',
1901 + "$path/$slug.php" => $this->render_block_file(
1902 + 'block-register-php',
2026 1903 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 ),
1904 + 'slug' => $slug,
1905 + 'title' => $title,
1906 + 'underscoredSlug' => str_replace( '-', '_', $slug ),
1907 + 'underscoredTitle' => str_replace( ' ', '_', $title ),
1908 + 'jetpackVersion' => substr( JETPACK__VERSION, 0, strpos( JETPACK__VERSION, '.' ) ) . '.x',
2034 1909 )
2035 1910 ),
2036 - "$path/$slug.php" => self::render_block_file(
2037 - 'block-register-php',
1911 + "$path/index.js" => $this->render_block_file(
1912 + 'block-index-js',
2038 1913 array(
2039 - 'nextVersion' => $next_version,
2040 - 'title' => $title,
2041 - 'underscoredTitle' => str_replace( ' ', '_', $title ),
1914 + 'slug' => $slug,
1915 + 'title' => $title,
1916 + 'description' => isset( $assoc_args['description'] )
1917 + ? $assoc_args['description']
1918 + : $title,
1919 + 'keywords' => $hasKeywords
1920 + ? array_map(
1921 + function( $keyword ) {
1922 + // Construction necessary for Mustache lists
1923 + return array( 'keyword' => trim( $keyword ) );
1924 + },
1925 + explode( ',', $assoc_args['keywords'], 3 )
1926 + )
1927 + : '',
1928 + 'hasKeywords' => $hasKeywords,
2042 1929 )
2043 1930 ),
2044 - "$path/editor.js" => self::render_block_file( 'block-editor-js' ),
2045 - "$path/editor.scss" => self::render_block_file(
1931 + "$path/editor.js" => $this->render_block_file( 'block-editor-js' ),
1932 + "$path/editor.scss" => $this->render_block_file(
2046 1933 'block-editor-scss',
2047 1934 array(
2048 1935 'slug' => $slug,
2049 1936 'title' => $title,
@@ -2048,9 +1935,9 @@
2048 1935 'slug' => $slug,
2049 1936 'title' => $title,
2050 1937 )
2051 1938 ),
2052 - "$path/edit.js" => self::render_block_file(
1939 + "$path/edit.js" => $this->render_block_file(
2053 1940 'block-edit-js',
2054 1941 array(
2055 1942 'title' => $title,
2056 1943 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
@@ -2055,8 +1942,10 @@
2055 1942 'title' => $title,
2056 1943 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
2057 1944 )
2058 1945 ),
1946 + "$path/icon.js" => $this->render_block_file( 'block-icon-js' ),
1947 + "$path/attributes.js" => $this->render_block_file( 'block-attributes-js' ),
2059 1948 );
2060 1949
2061 1950 $files_written = array();
2062 1951
@@ -2102,10 +1991,10 @@
2102 1991
2103 1992 if ( 'beta' === $variation || 'experimental' === $variation ) {
2104 1993 $block_constant = sprintf(
2105 1994 /* 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
1995 + esc_html__( 'To load the block, add the constant %1$s as true to your wp-config.php file', 'jetpack' ),
1996 + ( 'beta' === $variation ? 'JETPACK_BETA_BLOCKS' : 'JETPACK_EXPERIMENTAL_BLOCKS' )
2108 1997 );
2109 1998 } else {
2110 1999 $block_constant = '';
2111 2000 }
@@ -2116,9 +2005,9 @@
2116 2005 esc_html__( 'Successfully created block %1$s with slug %2$s', 'jetpack' ) . ' 🎉' . "\n" .
2117 2006 "--------------------------------------------------------------------------------------------------------------------\n" .
2118 2007 /* translators: the placeholder is a directory path */
2119 2008 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" .
2009 + esc_html__( 'To start using the block, build the blocks with yarn run build-extensions', 'jetpack' ) . "\n" .
2121 2010 /* translators: the placeholder is a file path */
2122 2011 esc_html__( 'The block slug has been added to the %4$s list at %5$s', 'jetpack' ) . "\n" .
2123 2012 '%6$s' . "\n" .
2124 2013 /* translators: the placeholder is a URL */
@@ -2128,9 +2017,9 @@
2128 2017 $path,
2129 2018 $variation,
2130 2019 $block_list_path,
2131 2020 $block_constant,
2132 - 'https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#developing-block-editor-extensions-in-jetpack'
2021 + 'https://github.com/Automattic/jetpack/blob/master/extensions/README.md#develop-new-blocks'
2133 2022 ) . '--------------------------------------------------------------------------------------------------------------------'
2134 2023 );
2135 2024 }
2136 2025 }
@@ -2137,10 +2026,11 @@
2137 2026
2138 2027 /**
2139 2028 * Built the file replacing the placeholders in the template with the data supplied.
2140 2029 *
2141 - * @param string $template Template.
2142 - * @param array $data Data.
2030 + * @param string $template
2031 + * @param array $data
2032 + *
2143 2033 * @return string mixed
2144 2034 */
2145 2035 private static function render_block_file( $template, $data = array() ) {
2146 2036 return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data );
@@ -2146,23 +2036,21 @@
2146 2036 return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data );
2147 2037 }
2148 2038 }
2149 2039
2150 -// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.
2151 -
2152 -/**
2040 +/*
2153 2041 * Standard "ask for permission to continue" function.
2154 2042 * If action cancelled, ask if they need help.
2155 2043 *
2156 2044 * Written outside of the class so it's not listed as an executable command w/ 'wp jetpack'
2157 2045 *
2158 - * @param bool $flagged false = normal option | true = flagged by get_jetpack_options_for_reset().
2159 - * @param string $error_msg Error message.
2046 + * @param $flagged bool false = normal option | true = flagged by get_jetpack_options_for_reset()
2047 + * @param $error_msg string (optional)
2160 2048 */
2161 2049 function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) {
2162 2050 $cli = new Jetpack_CLI();
2163 2051
2164 - // Default cancellation message.
2052 + // Default cancellation message
2165 2053 if ( ! $error_msg ) {
2166 2054 $error_msg =
2167 2055 __( 'Action cancelled. Have a question?', 'jetpack' )
2168 2056 . ' '
@@ -2173,14 +2061,14 @@
2173 2061
2174 2062 if ( ! $flagged ) {
2175 2063 $prompt_message = _x( 'Are you sure? This cannot be undone. Type "yes" to continue:', '"yes" is a command - do not translate.', 'jetpack' );
2176 2064 } 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' );
2065 + $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 2066 }
2179 2067
2180 2068 WP_CLI::line( $prompt_message );
2181 2069 $handle = fopen( 'php://stdin', 'r' );
2182 2070 $line = fgets( $handle );
2183 - if ( 'yes' !== trim( $line ) ) {
2071 + if ( 'yes' != trim( $line ) ) {
2184 2072 WP_CLI::error( $error_msg );
2185 2073 }
2186 2074 }