PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.7
Jetpack – WP Security, Backup, Speed, & Growth v7.7
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 +330 -527 14.2.27.7 View file →
@@ -1,62 +1,26 @@
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;
14 -use Automattic\Jetpack\Status;
15 7 use Automattic\Jetpack\Sync\Actions;
16 8 use Automattic\Jetpack\Sync\Listener;
17 -use Automattic\Jetpack\Sync\Modules;
18 9 use Automattic\Jetpack\Sync\Queue;
19 10 use Automattic\Jetpack\Sync\Settings;
20 -use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions;
21 11
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 12 /**
30 13 * Control your local Jetpack installation.
14 + *
15 + * Minimum PHP requirement for WP-CLI is PHP 5.3, so ignore PHP 5.2 compatibility issues.
16 + * @phpcs:disable PHPCompatibility.PHP.NewLanguageConstructs.t_ns_separatorFound
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
@@ -71,12 +35,11 @@
71 35 *
72 36 * wp jetpack status
73 37 * wp jetpack status full
74 38 *
75 - * @param array $args Positional args.
76 39 */
77 - public function status( $args ) {
78 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
40 + public function status( $args, $assoc_args ) {
41 + jetpack_require_lib( 'debugger' );
79 42
80 43 /* translators: %s is the site URL */
81 44 WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) );
82 45
@@ -95,15 +58,15 @@
95 58 WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) );
96 59 } else {
97 60 $error = array();
98 61 foreach ( $cxntests->list_fails() as $fail ) {
99 - $error[] = $fail['name'] . ( empty( $fail['message'] ) ? '' : ': ' . $fail['message'] );
62 + $error[] = $fail['name'] . ': ' . $fail['message'];
100 63 }
101 64 WP_CLI::error_multi_line( $error );
102 65
103 66 $cxntests->output_results_for_cli();
104 67
105 - WP_CLI::error( __( 'One or more tests did not pass. Please investigate!', 'jetpack' ) ); // Exit CLI.
68 + WP_CLI::error( __('Jetpack connection is broken.', 'jetpack' ) ); // Exit CLI.
106 69 }
107 70
108 71 /* translators: %s is current version of Jetpack, for example 7.3 */
109 72 WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) );
@@ -116,41 +79,41 @@
116 79 * Are they asking for all data?
117 80 *
118 81 * Loop through heartbeat data and organize by priority.
119 82 */
120 - $all_data = ( isset( $args[0] ) && 'full' === $args[0] ) ? 'full' : false;
83 + $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false;
121 84 if ( $all_data ) {
122 - // Heartbeat data.
85 + // Heartbeat data
123 86 WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) );
124 87
125 88 // Get the filtered heartbeat data.
126 - // Filtered so we can color/list by severity.
89 + // Filtered so we can color/list by severity
127 90 $stats = Jetpack::jetpack_check_heartbeat_data();
128 91
129 - // Display red flags first.
92 + // Display red flags first
130 93 foreach ( $stats['bad'] as $stat => $value ) {
131 - WP_CLI::line( sprintf( "$this->red_open%-'.16s %s $this->color_close", $stat, $value ) );
94 + printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value );
132 95 }
133 96
134 - // Display caution warnings next.
97 + // Display caution warnings next
135 98 foreach ( $stats['caution'] as $stat => $value ) {
136 - WP_CLI::line( sprintf( "$this->yellow_open%-'.16s %s $this->color_close", $stat, $value ) );
99 + printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value );
137 100 }
138 101
139 102 // The rest of the results are good!
140 103 foreach ( $stats['good'] as $stat => $value ) {
141 104
142 - // Modules should get special spacing for aestetics.
105 + // Modules should get special spacing for aestetics
143 106 if ( strpos( $stat, 'odule-' ) ) {
144 - WP_CLI::line( sprintf( "%-'.30s %s", $stat, $value ) );
145 - usleep( 4000 ); // For dramatic effect lolz.
107 + printf( "%-'.30s %s\n", $stat, $value );
108 + usleep( 4000 ); // For dramatic effect lolz
146 109 continue;
147 110 }
148 - WP_CLI::line( sprintf( "%-'.16s %s", $stat, $value ) );
149 - usleep( 4000 ); // For dramatic effect lolz.
111 + printf( "%-'.16s %s\n", $stat, $value );
112 + usleep( 4000 ); // For dramatic effect lolz
150 113 }
151 114 } else {
152 - // Just the basics.
115 + // Just the basics
153 116 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 117 }
155 118 }
156 119
@@ -164,14 +127,14 @@
164 127 * wp jetpack test-connection
165 128 *
166 129 * @subcommand test-connection
167 130 */
168 - public function test_connection() {
131 + public function test_connection( $args, $assoc_args ) {
169 132
170 133 /* translators: %s is the site URL */
171 134 WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) );
172 135
173 - if ( ! Jetpack::is_connection_ready() ) {
136 + if ( ! Jetpack::is_active() ) {
174 137 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
175 138 }
176 139
177 140 $response = Client::wpcom_json_api_request_as_blog(
@@ -188,11 +151,11 @@
188 151 if ( ! $body ) {
189 152 WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) );
190 153 }
191 154
192 - $result = json_decode( $body );
155 + $result = json_decode( $body );
193 156 $is_connected = (bool) $result->connected;
194 - $message = $result->message;
157 + $message = $result->message;
195 158
196 159 if ( $is_connected ) {
197 160 WP_CLI::success( $message );
198 161 } else {
@@ -208,52 +171,46 @@
208 171 * blog: Disconnect the entire blog.
209 172 *
210 173 * user <user_identifier>: Disconnect a specific user from WordPress.com.
211 174 *
212 - * [--force]
213 - * If the user ID provided is the connection owner, it will only be disconnected if --force is passed
175 + * Please note, the primary account that the blog is connected
176 + * to WordPress.com with cannot be disconnected without
177 + * disconnecting the entire blog.
214 178 *
215 179 * ## EXAMPLES
216 180 *
217 181 * wp jetpack disconnect blog
218 182 * wp jetpack disconnect user 13
219 - * wp jetpack disconnect user 1 --force
220 183 * wp jetpack disconnect user username
221 184 * wp jetpack disconnect user email@domain.com
222 185 *
223 - * @synopsis <blog|user> [<user_identifier>] [--force]
224 - *
225 - * @param array $args Positional args.
226 - * @param array $assoc_args Named args.
186 + * @synopsis <blog|user> [<user_identifier>]
227 187 */
228 188 public function disconnect( $args, $assoc_args ) {
229 - $user = null;
230 - if ( ! Jetpack::is_connection_ready() ) {
231 - WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) );
232 - return;
189 + if ( ! Jetpack::is_active() ) {
190 + WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) );
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 - $field = 'id';
203 + $field = 'id';
246 204 $user_id = (int) $user_id;
247 205 } elseif ( is_email( $user_id ) ) {
248 - $field = 'email';
206 + $field = 'email';
249 207 $user_id = sanitize_user( $user_id, true );
250 208 } else {
251 - $field = 'login';
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,40 +216,25 @@
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();
269 - WP_CLI::success(
270 - sprintf(
271 - /* translators: %s is the site URL */
272 - __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
273 - esc_url( get_site_url() )
274 - )
275 - );
223 + Jetpack::disconnect();
224 + WP_CLI::success( sprintf(
225 + /* translators: %s is the site URL */
226 + __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
227 + esc_url( get_site_url() )
228 + ) );
276 229 break;
277 230 case 'user':
278 - $connection_manager = new Connection_Manager( 'jetpack' );
279 - $disconnected = $connection_manager->disconnect_user( $user->ID, $force_user_disconnect );
280 - if ( $disconnected ) {
231 + if ( Connection_Manager::disconnect_user( $user->ID ) ) {
281 232 Jetpack::log( 'unlink', $user->ID );
282 233 WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) );
283 234 } 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 );
235 + /* translators: %s is a username */
236 + WP_CLI::error( sprintf( __( "User %s could not be disconnected. Are you sure they're connected currently?", 'jetpack' ), "{$user->login} <{$user->email}>" ) );
295 237 }
296 238 break;
297 239 case 'prompt':
298 240 WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) );
@@ -321,10 +263,8 @@
321 263 * wp jetpack reset sync-checksum --dry-run --offset=0
322 264 *
323 265 * @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>]
324 266 *
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,17 +287,15 @@
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.
352 - WP_CLI::line(
353 - sprintf(
354 - /* translators: %s is the site URL */
355 - __( "Resetting Jetpack Options for %s...\n", 'jetpack' ),
356 - esc_url( get_site_url() )
357 - )
358 - );
359 - sleep( 1 ); // Take a breath.
291 + // Reset the Jetpack options
292 + WP_CLI::line( sprintf(
293 + /* translators: %s is the site URL */
294 + __( "Resetting Jetpack Options for %s...\n", "jetpack" ),
295 + esc_url( get_site_url() )
296 + ) );
297 + sleep(1); // Take a breath
360 298 foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
361 299 if ( ! $is_dry_run ) {
362 300 Jetpack_Options::delete_option( $option_to_reset );
363 301 usleep( 100000 );
@@ -366,11 +304,11 @@
366 304 /* translators: This is the result of an action. The option named %s was reset */
367 305 WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
368 306 }
369 307
370 - // Reset the WP options.
371 - WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", 'jetpack' ) );
372 - usleep( 500000 ); // Take a breath.
308 + // Reset the WP options
309 + WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", "jetpack" ) );
310 + usleep( 500000 ); // Take a breath
373 311 foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
374 312 if ( ! $is_dry_run ) {
375 313 delete_option( $option_to_reset );
376 314 usleep( 100000 );
@@ -378,11 +316,11 @@
378 316 /* translators: This is the result of an action. The option named %s was reset */
379 317 WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
380 318 }
381 319
382 - // Reset to default modules.
383 - WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) );
384 - usleep( 500000 ); // Take a breath.
320 + // Reset to default modules
321 + WP_CLI::line( __( "Resetting default modules...\n", "jetpack" ) );
322 + usleep( 500000 ); // Take a breath
385 323 $default_modules = Jetpack::get_default_modules();
386 324 if ( ! $is_dry_run ) {
387 325 Jetpack::update_active_modules( $default_modules );
388 326 }
@@ -408,14 +346,9 @@
408 346 /*
409 347 * 1000 is a good limit since we don't expect the number of sites to be more than 1000
410 348 * Offset can be used to paginate and try to clean up more sites.
411 349 */
412 - $sites = get_sites(
413 - array(
414 - 'number' => 1000,
415 - 'offset' => $offset,
416 - )
417 - );
350 + $sites = get_sites( array( 'number' => 1000, 'offset' => $offset ) );
418 351 $count_fixes = 0;
419 352 foreach ( $sites as $site ) {
420 353 switch_to_blog( $site->blog_id );
421 354 $count = self::count_option( $option );
@@ -431,9 +364,9 @@
431 364 $option,
432 365 "{$site->domain}{$site->path}"
433 366 )
434 367 );
435 - ++$count_fixes;
368 + $count_fixes++;
436 369 if ( ! $is_dry_run ) {
437 370 /*
438 371 * We could be deleting a lot of options rows at the same time.
439 372 * Allow some time for replication to catch up.
@@ -497,8 +430,9 @@
497 430 "SELECT COUNT(*) FROM $wpdb->options WHERE option_name = %s",
498 431 $option
499 432 )
500 433 );
434 +
501 435 }
502 436
503 437 /**
504 438 * Manage Jetpack Modules
@@ -540,15 +474,11 @@
540 474 * wp jetpack module deactivate stats
541 475 * wp jetpack module toggle stats
542 476 * wp jetpack module activate all
543 477 * wp jetpack module deactivate all
544 - *
545 - * @param array $args Positional args.
546 - * @param array $assoc_args Named args.
547 478 */
548 479 public function module( $args, $assoc_args ) {
549 - $module_slug = null;
550 - $action = isset( $args[0] ) ? $args[0] : 'list';
480 + $action = isset( $args[0] ) ? $args[0] : 'list';
551 481
552 482 if ( isset( $args[1] ) ) {
553 483 $module_slug = $args[1];
554 484 if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) {
@@ -625,122 +555,120 @@
625 555 * Manage Protect Settings
626 556 *
627 557 * ## OPTIONS
628 558 *
629 - * allow: Add an IP address to an always allow list. You can also read or clear the allow list.
559 + * whitelist: Whitelist an IP address. You can also read or clear the whitelist.
630 560 *
631 561 *
632 562 * ## EXAMPLES
633 563 *
634 - * wp jetpack protect allow <ip address>
635 - * wp jetpack protect allow list
636 - * wp jetpack protect allow clear
564 + * wp jetpack protect whitelist <ip address>
565 + * wp jetpack protect whitelist list
566 + * wp jetpack protect whitelist clear
637 567 *
638 - * @synopsis <allow> [<ip|ip_low-ip_high|list|clear>]
639 - *
640 - * @param array $args Positional args.
568 + * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>]
641 569 */
642 - public function protect( $args ) {
570 + public function protect( $args, $assoc_args ) {
643 571 $action = isset( $args[0] ) ? $args[0] : 'prompt';
644 - if ( ! in_array( $action, array( 'whitelist', 'allow' ), true ) ) { // Still allow "whitelist" for legacy support.
572 + if ( ! in_array( $action, array( 'whitelist' ) ) ) {
645 573 /* translators: %s is a command like "prompt" */
646 574 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
647 575 }
648 - // Check if module is active.
576 + // Check if module is active
649 577 if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) {
650 578 /* translators: %s is a module name */
651 - 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__ ) );
579 + WP_CLI::error( sprintf( _x( '%s is not active. You can activate it with "wp jetpack module activate %s"', '"wp jetpack module activate" is a command - do not translate', 'jetpack' ), __FUNCTION__, __FUNCTION__ ) );
652 580 }
653 - if ( in_array( $action, array( 'allow', 'whitelist' ), true ) ) {
581 + if ( in_array( $action, array( 'whitelist' ) ) ) {
654 582 if ( isset( $args[1] ) ) {
655 - $action = 'allow';
583 + $action = 'whitelist';
656 584 } else {
657 585 $action = 'prompt';
658 586 }
659 587 }
660 588 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.
589 + case 'whitelist':
590 + $whitelist = array();
591 + $new_ip = $args[1];
592 + $current_whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
665 593
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 ) {
594 + // Build array of IPs that are already whitelisted.
595 + // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get
596 + // low & high range params for jetpack_protect_ip_address_is_in_range();
597 + foreach( $current_whitelist as $whitelisted ) {
670 598
671 - // IP ranges.
672 - if ( $allowed->range ) {
599 + // IP ranges
600 + if ( $whitelisted->range ) {
673 601
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 ) ) {
602 + // Is it already whitelisted?
603 + if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) {
676 604 /* translators: %s is an IP address */
677 - WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
605 + WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) );
678 606 break;
679 607 }
680 - $allow[] = $allowed->range_low . ' - ' . $allowed->range_high;
608 + $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high;
681 609
682 - } else { // Individual IPs.
610 + } else { // Individual IPs
683 611
684 - // Check if the IP is already on the allow list (single IP only).
685 - if ( $new_ip === $allowed->ip_address ) {
612 + // Check if the IP is already whitelisted (single IP only)
613 + if ( $new_ip == $whitelisted->ip_address ) {
686 614 /* translators: %s is an IP address */
687 - WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
615 + WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) );
688 616 break;
689 617 }
690 - $allow[] = $allowed->ip_address;
618 + $whitelist[] = $whitelisted->ip_address;
691 619
692 620 }
693 621 }
694 622
695 623 /*
696 - * List the allowed IPs.
697 - * Done here because it's easier to read the $allow array after it's been rebuilt.
624 + * List the whitelist
625 + * Done here because it's easier to read the $whitelist array after it's been rebuilt
698 626 */
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 ) {
703 - WP_CLI::line( "\t" . str_pad( $ip, 24 ) );
627 + if ( isset( $args[1] ) && 'list' == $args[1] ) {
628 + if ( ! empty( $whitelist ) ) {
629 + WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) );
630 + foreach ( $whitelist as $ip ) {
631 + WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ;
704 632 }
705 633 } else {
706 - WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
634 + WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
707 635 }
708 636 break;
709 637 }
710 638
711 639 /*
712 - * Clear the always allow list.
640 + * Clear the whitelist
713 641 */
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' ) );
642 + if ( isset( $args[1] ) && 'clear' == $args[1] ) {
643 + if ( ! empty( $whitelist ) ) {
644 + $whitelist = array();
645 + jetpack_protect_save_whitelist( $whitelist );
646 + WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) );
719 647 } else {
720 - WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
648 + WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
721 649 }
722 650 break;
723 651 }
724 652
725 - // Append new IP to allow array.
726 - array_push( $allow, $new_ip );
653 + // Append new IP to whitelist array
654 + array_push( $whitelist, $new_ip );
727 655
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.
656 + // Save whitelist if there are no errors
657 + $result = jetpack_protect_save_whitelist( $whitelist );
730 658 if ( is_wp_error( $result ) ) {
731 659 WP_CLI::error( $result );
732 660 }
733 661
734 662 /* translators: %s is an IP address */
735 - WP_CLI::success( sprintf( __( '%s has been added to the always allowed list.', 'jetpack' ), $new_ip ) );
663 + WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) );
736 664 break;
737 665 case 'prompt':
738 666 WP_CLI::error(
739 667 __( '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"
668 + __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" .
669 + _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" .
670 + _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n"
743 671 );
744 672 break;
745 673 }
746 674 }
@@ -764,29 +692,27 @@
764 692 * wp jetpack options delete <option_name>
765 693 * wp jetpack options update <option_name> [<option_value>]
766 694 *
767 695 * @synopsis <list|get|delete|update> [<option_name>] [<option_value>]
768 - *
769 - * @param array $args Positional args.
770 696 */
771 - public function options( $args ) {
772 - $action = isset( $args[0] ) ? $args[0] : 'list';
697 + public function options( $args, $assoc_args ) {
698 + $action = isset( $args[0] ) ? $args[0] : 'list';
773 699 $safe_to_modify = Jetpack_Options::get_options_for_reset();
774 700
775 701 // Is the option flagged as unsafe?
776 - $flagged = ! in_array( $args[1], $safe_to_modify, true );
702 + $flagged = ! in_array( $args[1], $safe_to_modify );
777 703
778 - if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ), true ) ) {
704 + if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) {
779 705 /* translators: %s is a command like "prompt" */
780 706 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
781 707 }
782 708
783 709 if ( isset( $args[0] ) ) {
784 - if ( 'get' === $args[0] && isset( $args[1] ) ) {
710 + if ( 'get' == $args[0] && isset( $args[1] ) ) {
785 711 $action = 'get';
786 - } elseif ( 'delete' === $args[0] && isset( $args[1] ) ) {
712 + } else if ( 'delete' == $args[0] && isset( $args[1] ) ) {
787 713 $action = 'delete';
788 - } elseif ( 'update' === $args[0] && isset( $args[1] ) ) {
714 + } else if ( 'update' == $args[0] && isset( $args[1] ) ) {
789 715 $action = 'update';
790 716 } else {
791 717 $action = 'list';
792 718 }
@@ -791,18 +717,17 @@
791 717 $action = 'list';
792 718 }
793 719 }
794 720
795 - // Bail if the option isn't found.
721 + // Bail if the option isn't found
796 722 $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false;
797 723 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' ) );
724 + WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) );
799 725 }
800 726
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;
727 + // Let's print_r the option if it's an array
728 + // Used in the 'get' and 'list' actions
729 + $option = is_array( $option ) ? print_r( $option ) : $option;
805 730
806 731 switch ( $action ) {
807 732 case 'get':
808 733 WP_CLI::success( "\t" . $option );
@@ -832,14 +757,14 @@
832 757 $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' );
833 758 $options_private = Jetpack_Options::get_option_names( 'private' );
834 759 $options = array_merge( $options_compact, $options_non_compact, $options_private );
835 760
836 - // Table headers.
761 + // Table headers
837 762 WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) );
838 763
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.
764 + // List out the options and their values
765 + // Tell them if the value is empty or not
766 + // Tell them if it's an array
842 767 foreach ( $options as $option ) {
843 768 $value = Jetpack_Options::get_option( $option );
844 769 if ( ! $value ) {
845 770 WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' );
@@ -847,9 +772,9 @@
847 772 }
848 773
849 774 if ( ! is_array( $value ) ) {
850 775 WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value );
851 - } elseif ( is_array( $value ) ) {
776 + } else if ( is_array( $value ) ) {
852 777 WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' );
853 778 }
854 779 }
855 780 $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}';
@@ -856,11 +781,11 @@
856 781 $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}';
857 782
858 783 WP_CLI::success(
859 784 _x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" .
860 - str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" .
785 + str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" .
861 786 str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" .
862 - str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text\n" .
787 + str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" .
863 788 _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n"
864 789 );
865 790 break;
866 791 }
@@ -888,11 +813,8 @@
888 813 * wp jetpack sync reset
889 814 * wp jetpack sync reset --queue=full or regular
890 815 *
891 816 * @synopsis <status|start> [--<field>=<value>]
892 - *
893 - * @param array $args Positional args.
894 - * @param array $assoc_args Named args.
895 817 */
896 818 public function sync( $args, $assoc_args ) {
897 819
898 820 $action = isset( $args[0] ) ? $args[0] : 'status';
@@ -898,14 +820,14 @@
898 820 $action = isset( $args[0] ) ? $args[0] : 'status';
899 821
900 822 switch ( $action ) {
901 823 case 'status':
902 - $status = Actions::get_sync_status();
824 + $status = Actions::get_sync_status();
903 825 $collection = array();
904 826 foreach ( $status as $key => $item ) {
905 - $collection[] = array(
827 + $collection[] = array(
906 828 'option' => $key,
907 - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
829 + 'value' => is_scalar( $item ) ? $item : json_encode( $item )
908 830 );
909 831 }
910 832 WP_CLI::log( __( 'Sync Status:', 'jetpack' ) );
911 833 WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) );
@@ -911,17 +833,16 @@
911 833 WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) );
912 834 break;
913 835 case 'settings':
914 836 WP_CLI::log( __( 'Sync Settings:', 'jetpack' ) );
915 - $settings = array();
916 - foreach ( Settings::get_settings() as $setting => $item ) {
917 - $settings[] = array(
837 + foreach( Settings::get_settings() as $setting => $item ) {
838 + $settings[] = array(
918 839 'setting' => $setting,
919 - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
840 + 'value' => is_scalar( $item ) ? $item : json_encode( $item )
920 841 );
921 842 }
922 843 WP_CLI\Utils\format_items( 'table', $settings, array( 'setting', 'value' ) );
923 - break;
844 +
924 845 case 'disable':
925 846 // Don't set it via the Settings since that also resets the queues.
926 847 update_option( 'jetpack_sync_settings_disable', 1 );
927 848 /* translators: %s is the site URL */
@@ -967,53 +888,47 @@
967 888
968 889 break;
969 890 case 'start':
970 891 if ( ! Actions::sync_allowed() ) {
971 - if ( Settings::get_setting( 'disable' ) ) {
892 + if( ! Settings::get_setting( 'disable' ) ) {
972 893 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 894 return;
974 895 }
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 - }
896 + if ( doing_action( 'jetpack_user_authorized' ) || Jetpack::is_active() ) {
897 + WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. Jetpack is not connected.', 'jetpack' ) );
898 + return;
981 899 }
982 -
983 - $status = new Status();
984 -
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' ) );
900 + if ( Jetpack::is_development_mode() ) {
901 + WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in development mode.', 'jetpack' ) );
987 902 return;
988 903 }
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' ) );
904 + if ( Jetpack::is_staging_site() ) {
905 + WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in staging mode.', 'jetpack' ) );
991 906 return;
992 907 }
908 +
993 909 }
994 - // Get the original settings so that we can restore them later.
910 + // Get the original settings so that we can restore them later
995 911 $original_settings = Settings::get_settings();
996 912
997 - // Initialize sync settigns so we can sync as quickly as possible.
913 + // Initialize sync settigns so we can sync as quickly as possible
998 914 $sync_settings = wp_parse_args(
999 915 array_intersect_key( $assoc_args, Settings::$valid_settings ),
1000 916 array(
1001 - 'sync_wait_time' => 0,
1002 - 'enqueue_wait_time' => 0,
1003 - 'queue_max_writes_sec' => 10000,
1004 - 'max_queue_size_full_sync' => 100000,
1005 - 'full_sync_send_duration' => HOUR_IN_SECONDS,
917 + 'sync_wait_time' => 0,
918 + 'enqueue_wait_time' => 0,
919 + 'queue_max_writes_sec' => 10000,
920 + 'max_queue_size_full_sync' => 100000
1006 921 )
1007 922 );
1008 923 Settings::update_settings( $sync_settings );
1009 924
1010 - // Convert comma-delimited string of modules to an array.
925 + // Convert comma-delimited string of modules to an array
1011 926 if ( ! empty( $assoc_args['modules'] ) ) {
1012 927 $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) );
1013 928
1014 929 // 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.
930 + // that we want to sync the module
1016 931 $modules = array_map( '__return_true', array_flip( $modules ) );
1017 932 }
1018 933
1019 934 foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
@@ -1021,12 +936,12 @@
1021 936 'users' === $module_name &&
1022 937 isset( $assoc_args[ $module_name ] ) &&
1023 938 'initial' === $assoc_args[ $module_name ]
1024 939 ) {
1025 - $modules['users'] = 'initial';
940 + $modules[ 'users' ] = 'initial';
1026 941 } elseif ( isset( $assoc_args[ $module_name ] ) ) {
1027 942 $ids = explode( ',', $assoc_args[ $module_name ] );
1028 - if ( $ids !== array() ) {
943 + if ( count( $ids ) > 0 ) {
1029 944 $modules[ $module_name ] = $ids;
1030 945 }
1031 946 }
1032 947 }
@@ -1034,13 +949,13 @@
1034 949 if ( empty( $modules ) ) {
1035 950 $modules = null;
1036 951 }
1037 952
1038 - // Kick off a full sync.
953 + // Kick off a full sync
1039 954 if ( Actions::do_full_sync( $modules ) ) {
1040 955 if ( $modules ) {
1041 956 /* 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 ) ) ) );
957 + WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) );
1043 958 } else {
1044 959 WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) );
1045 960 }
1046 961 } else {
@@ -1049,37 +964,32 @@
1049 964 Settings::update_settings( $original_settings );
1050 965
1051 966 if ( $modules ) {
1052 967 /* 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 ) ) );
968 + WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) );
1054 969 } else {
1055 970 WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) );
1056 971 }
1057 972 }
1058 973
1059 - // Keep sending to WPCOM until there's nothing to send.
974 + // Keep sending to WPCOM until there's nothing to send
1060 975 $i = 1;
1061 976 do {
1062 977 $result = Actions::$sender->do_full_sync();
1063 978 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 ) ) ) {
979 + $queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() );
980 + if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) {
1066 981 /* translators: %s is an error code */
1067 982 WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) );
1068 983 }
1069 984 } else {
1070 - if ( 1 === $i ) {
985 + if ( 1 == $i ) {
1071 986 WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) );
1072 987 } else {
1073 988 WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) );
1074 989 }
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 990 }
1081 - ++$i;
991 + $i++;
1082 992 } while ( $result && ! is_wp_error( $result ) );
1083 993
1084 994 // Reset sync settings to original.
1085 995 Settings::update_settings( $original_settings );
@@ -1100,29 +1010,26 @@
1100 1010 *
1101 1011 * wp jetpack sync_queue full_sync peek
1102 1012 *
1103 1013 * @synopsis <incremental|full_sync> <peek>
1104 - *
1105 - * @param array $args Positional args.
1106 1014 */
1107 - public function sync_queue( $args ) {
1015 + public function sync_queue( $args, $assoc_args ) {
1108 1016 if ( ! Actions::sync_allowed() ) {
1109 1017 WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) );
1110 1018 }
1111 1019
1112 1020 $queue_name = isset( $args[0] ) ? $args[0] : 'sync';
1113 - $action = isset( $args[1] ) ? $args[1] : 'peek';
1021 + $action = isset( $args[1] ) ? $args[1] : 'peek';
1114 1022
1115 1023 // We map the queue name that way we can support more friendly queue names in the commands, but still use
1116 1024 // the queue name that the code expects.
1117 - $allowed_queues = array(
1025 + $queue_name_map = $allowed_queues = array(
1118 1026 'incremental' => 'sync',
1119 1027 'full' => 'full_sync',
1120 1028 );
1121 - $queue_name_map = $allowed_queues;
1122 1029 $mapped_queue_name = isset( $queue_name_map[ $queue_name ] ) ? $queue_name_map[ $queue_name ] : $queue_name;
1123 1030
1124 - switch ( $action ) {
1031 + switch( $action ) {
1125 1032 case 'peek':
1126 1033 $queue = new Queue( $mapped_queue_name );
1127 1034 $items = $queue->peek( 100 );
1128 1035
@@ -1127,15 +1034,15 @@
1127 1034 $items = $queue->peek( 100 );
1128 1035
1129 1036 if ( empty( $items ) ) {
1130 1037 /* translators: %s is the name of the queue, either 'incremental' or 'full' */
1131 - WP_CLI::log( sprintf( __( 'Nothing is in the queue: %s', 'jetpack' ), $queue_name ) );
1038 + WP_CLI::log( sprintf( __( 'Nothing is in the queue: %s', 'jetpack' ), $queue_name ) );
1132 1039 } else {
1133 1040 $collection = array();
1134 1041 foreach ( $items as $item ) {
1135 1042 $collection[] = array(
1136 1043 'action' => $item[0],
1137 - 'args' => wp_json_encode( $item[1] ),
1044 + 'args' => json_encode( $item[1] ),
1138 1045 'current_user_id' => $item[2],
1139 1046 'microtime' => $item[3],
1140 1047 'importing' => (string) $item[4],
1141 1048 );
@@ -1165,20 +1072,16 @@
1165 1072 * : JSON blob of WPCOM API token
1166 1073 * [--partner_tracking_id=<partner_tracking_id>]
1167 1074 * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com
1168 1075 *
1169 - * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>]
1170 - *
1171 - * @param array $args Positional args.
1172 - * @param array $named_args Named args.
1076 + * * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>]
1173 1077 */
1174 1078 public function partner_cancel( $args, $named_args ) {
1175 1079 list( $token_json ) = $args;
1176 1080
1177 - $token = $token_json ? json_decode( $token_json ) : null;
1178 - if ( ! $token ) {
1081 + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
1179 1082 /* translators: %s is the invalid JSON string */
1180 - $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1083 + $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1181 1084 }
1182 1085
1183 1086 if ( isset( $token->error ) ) {
1184 1087 $this->partner_provision_error( new WP_Error( $token->error, $token->message ) );
@@ -1187,27 +1090,24 @@
1187 1090 if ( ! isset( $token->access_token ) ) {
1188 1091 $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) );
1189 1092 }
1190 1093
1191 - if ( Identity_Crisis::validate_sync_error_idc_option() ) {
1192 - $this->partner_provision_error(
1193 - new WP_Error(
1194 - 'site_in_safe_mode',
1195 - esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
1196 - )
1197 - );
1094 + if ( Jetpack::validate_sync_error_idc_option() ) {
1095 + $this->partner_provision_error( new WP_Error(
1096 + 'site_in_safe_mode',
1097 + esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
1098 + ) );
1198 1099 }
1199 1100
1200 1101 $site_identifier = Jetpack_Options::get_option( 'id' );
1201 1102
1202 1103 if ( ! $site_identifier ) {
1203 - $status = new Status();
1204 - $site_identifier = $status->get_site_suffix();
1104 + $site_identifier = Jetpack::build_raw_urls( get_home_url() );
1205 1105 }
1206 1106
1207 1107 $request = array(
1208 1108 'headers' => array(
1209 - 'Authorization' => 'Bearer ' . $token->access_token,
1109 + 'Authorization' => "Bearer " . $token->access_token,
1210 1110 'Host' => 'public-api.wordpress.com',
1211 1111 ),
1212 1112 'timeout' => 60,
1213 1113 'method' => 'POST',
@@ -1212,9 +1112,9 @@
1212 1112 'timeout' => 60,
1213 1113 'method' => 'POST',
1214 1114 );
1215 1115
1216 - $url = sprintf( '%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier );
1116 + $url = sprintf( 'https://%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier );
1217 1117 if ( ! empty( $named_args ) && ! empty( $named_args['partner_tracking_id'] ) ) {
1218 1118 $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
1219 1119 }
1220 1120
@@ -1219,8 +1119,10 @@
1219 1119 }
1220 1120
1221 1121 $result = Client::_wp_remote_request( $url, $request );
1222 1122
1123 + Jetpack_Options::delete_option( 'onboarding' );
1124 +
1223 1125 if ( is_wp_error( $result ) ) {
1224 1126 $this->partner_provision_error( $result );
1225 1127 }
1226 1128
@@ -1241,8 +1143,10 @@
1241 1143 * [--wpcom_user_id=<user_id>]
1242 1144 * : WordPress.com ID of user to connect as (must be whitelisted against partner key)
1243 1145 * [--wpcom_user_email=<wpcom_user_email>]
1244 1146 * : Override the email we send to WordPress.com for registration
1147 + * [--onboarding=<onboarding>]
1148 + * : Guide the user through an onboarding wizard
1245 1149 * [--force_register=<register>]
1246 1150 * : Whether to force a site to register
1247 1151 * [--force_connect=<force_connect>]
1248 1152 * : Force JPS to not reuse existing credentials
@@ -1257,20 +1161,16 @@
1257 1161 *
1258 1162 * $ wp jetpack partner_provision '{ some: "json" }' premium 1
1259 1163 * { success: true }
1260 1164 *
1261 - * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--force_register=<register>] [--force_connect=<force_connect>] [--home_url=<home_url>] [--site_url=<site_url>] [--wpcom_user_email=<wpcom_user_email>] [--partner_tracking_id=<partner_tracking_id>]
1262 - *
1263 - * @param array $args Positional args.
1264 - * @param array $named_args Named args.
1165 + * @synopsis <token_json> [--wpcom_user_id=<user_id>] [--plan=<plan_name>] [--onboarding=<onboarding>] [--force_register=<register>] [--force_connect=<force_connect>] [--home_url=<home_url>] [--site_url=<site_url>] [--wpcom_user_email=<wpcom_user_email>] [--partner_tracking_id=<partner_tracking_id>]
1265 1166 */
1266 1167 public function partner_provision( $args, $named_args ) {
1267 1168 list( $token_json ) = $args;
1268 1169
1269 - $token = $token_json ? json_decode( $token_json ) : null;
1270 - if ( ! $token ) {
1170 + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
1271 1171 /* translators: %s is the invalid JSON string */
1272 - $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1172 + $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1273 1173 }
1274 1174
1275 1175 if ( isset( $token->error ) ) {
1276 1176 $message = isset( $token->message )
@@ -1287,21 +1187,17 @@
1287 1187
1288 1188 $body_json = Jetpack_Provision::partner_provision( $token->access_token, $named_args );
1289 1189
1290 1190 if ( is_wp_error( $body_json ) ) {
1291 - WP_CLI::error(
1292 - wp_json_encode(
1293 - array(
1294 - 'success' => false,
1295 - 'error_code' => $body_json->get_error_code(),
1296 - 'error_message' => $body_json->get_error_message(),
1297 - )
1298 - )
1299 - );
1191 + error_log( json_encode( array(
1192 + 'success' => false,
1193 + 'error_code' => $body_json->get_error_code(),
1194 + 'error_message' => $body_json->get_error_message()
1195 + ) ) );
1300 1196 exit( 1 );
1301 1197 }
1302 1198
1303 - WP_CLI::log( wp_json_encode( $body_json ) );
1199 + WP_CLI::log( json_encode( $body_json ) );
1304 1200 }
1305 1201
1306 1202 /**
1307 1203 * Manages your Jetpack sitemap
@@ -1316,14 +1212,11 @@
1316 1212 * wp jetpack sitemap rebuild
1317 1213 *
1318 1214 * @subcommand sitemap
1319 1215 * @synopsis <rebuild> [--purge]
1320 - *
1321 - * @param array $args Positional args.
1322 - * @param array $assoc_args Named args.
1323 1216 */
1324 1217 public function sitemap( $args, $assoc_args ) {
1325 - if ( ! Jetpack::is_connection_ready() ) {
1218 + if ( ! Jetpack::is_active() ) {
1326 1219 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1327 1220 }
1328 1221 if ( ! Jetpack::is_module_active( 'sitemaps' ) ) {
1329 1222 WP_CLI::error( __( 'Jetpack Sitemaps module is not currently active. Activate it first if you want to work with sitemaps.', 'jetpack' ) );
@@ -1348,11 +1241,8 @@
1348 1241 *
1349 1242 * wp jetpack authorize_user --token=123456789abcdef
1350 1243 *
1351 1244 * @synopsis --token=<value>
1352 - *
1353 - * @param array $args Positional args.
1354 - * @param array $named_args Named args.
1355 1245 */
1356 1246 public function authorize_user( $args, $named_args ) {
1357 1247 if ( ! is_user_logged_in() ) {
1358 1248 WP_CLI::error( __( 'Please select a user to authorize via the --user global argument.', 'jetpack' ) );
@@ -1361,16 +1251,16 @@
1361 1251 if ( empty( $named_args['token'] ) ) {
1362 1252 WP_CLI::error( __( 'A non-empty token argument must be passed.', 'jetpack' ) );
1363 1253 }
1364 1254
1365 - $is_connection_owner = ! Jetpack::connection()->has_connected_owner();
1366 - $current_user_id = get_current_user_id();
1255 + $is_master_user = ! Jetpack::is_active();
1256 + $current_user_id = get_current_user_id();
1367 1257
1368 - ( new Tokens() )->update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_connection_owner );
1258 + Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_master_user );
1369 1259
1370 1260 WP_CLI::log( wp_json_encode( $named_args ) );
1371 1261
1372 - if ( $is_connection_owner ) {
1262 + if ( $is_master_user ) {
1373 1263 /**
1374 1264 * Auto-enable SSO module for new Jetpack Start connections
1375 1265 *
1376 1266 * @since 5.0.0
@@ -1418,14 +1308,11 @@
1418 1308 *
1419 1309 * ## EXAMPLES
1420 1310 *
1421 1311 * wp jetpack call_api --resource='/sites/%d'
1422 - *
1423 - * @param array $args Positional args.
1424 - * @param array $named_args Named args.
1425 1312 */
1426 1313 public function call_api( $args, $named_args ) {
1427 - if ( ! Jetpack::is_connection_ready() ) {
1314 + if ( ! Jetpack::is_active() ) {
1428 1315 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1429 1316 }
1430 1317
1431 1318 $consumed_args = array(
@@ -1442,9 +1329,9 @@
1442 1329 $decoded_body = ! empty( $named_args['body'] )
1443 1330 ? json_decode( $named_args['body'], true )
1444 1331 : false;
1445 1332
1446 - $resource_url = ( ! str_contains( $named_args['resource'], '%d' ) )
1333 + $resource_url = ( false === strpos( $named_args['resource'], '%d' ) )
1447 1334 ? $named_args['resource']
1448 1335 : sprintf( $named_args['resource'], Jetpack_Options::get_option( 'id' ) );
1449 1336
1450 1337 $response = Client::wpcom_json_api_request_as_blog(
@@ -1455,28 +1342,24 @@
1455 1342 empty( $named_args['base_api_path'] ) ? 'rest' : $named_args['base_api_path']
1456 1343 );
1457 1344
1458 1345 if ( is_wp_error( $response ) ) {
1459 - WP_CLI::error(
1460 - sprintf(
1461 - /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an error code, %3$s is an error message. */
1462 - __( 'Request to %1$s returned an error: (%2$d) %3$s.', 'jetpack' ),
1463 - $resource_url,
1464 - $response->get_error_code(),
1465 - $response->get_error_message()
1466 - )
1467 - );
1346 + WP_CLI::error( sprintf(
1347 + /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an error code, %3$s is an error message. */
1348 + __( 'Request to %1$s returned an error: (%2$d) %3$s.', 'jetpack' ),
1349 + $resource_url,
1350 + $response->get_error_code(),
1351 + $response->get_error_message()
1352 + ) );
1468 1353 }
1469 1354
1470 1355 if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
1471 - WP_CLI::error(
1472 - sprintf(
1473 - /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an HTTP status code. */
1474 - __( 'Request to %1$s returned a non-200 response code: %2$d.', 'jetpack' ),
1475 - $resource_url,
1476 - wp_remote_retrieve_response_code( $response )
1477 - )
1478 - );
1356 + WP_CLI::error( sprintf(
1357 + /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an HTTP status code. */
1358 + __( 'Request to %1$s returned a non-200 response code: %2$d.', 'jetpack' ),
1359 + $resource_url,
1360 + wp_remote_retrieve_response_code( $response )
1361 + ) );
1479 1362 }
1480 1363
1481 1364 $output = wp_remote_retrieve_body( $response );
1482 1365 if ( isset( $named_args['pretty'] ) ) {
@@ -1520,14 +1403,11 @@
1520 1403 * ## EXAMPLES
1521 1404 *
1522 1405 * wp jetpack upload_ssh_creds --host=example.com --ssh-user=example --pass=password
1523 1406 * wp jetpack updload_ssh_creds --host=example.com --ssh-user=example --kpri=key
1524 - *
1525 - * @param array $args Positional args.
1526 - * @param array $named_args Named args.
1527 1407 */
1528 1408 public function upload_ssh_creds( $args, $named_args ) {
1529 - if ( ! Jetpack::is_connection_ready() ) {
1409 + if ( ! Jetpack::is_active() ) {
1530 1410 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1531 1411 }
1532 1412
1533 1413 $required_args = array(
@@ -1612,21 +1492,16 @@
1612 1492 *
1613 1493 * ## EXAMPLES
1614 1494 *
1615 1495 * wp jetpack get_stats
1616 - *
1617 - * @param array $args Positional args.
1618 - * @param array $named_args Named args.
1619 1496 */
1620 1497 public function get_stats( $args, $named_args ) {
1621 1498 $selected_args = array_intersect_key(
1622 1499 $named_args,
1623 - array_flip(
1624 - array(
1625 - 'quantity',
1626 - 'date',
1627 - )
1628 - )
1500 + array_flip( array(
1501 + 'quantity',
1502 + 'date',
1503 + ) )
1629 1504 );
1630 1505
1631 1506 // The API expects unit, but period seems to be more correct.
1632 1507 $selected_args['unit'] = $named_args['period'];
@@ -1687,15 +1562,15 @@
1687 1562 * # List all publicize connections.
1688 1563 * $ wp jetpack publicize list
1689 1564 *
1690 1565 * # List publicize connections for a given service.
1691 - * $ wp jetpack publicize list linkedin
1566 + * $ wp jetpack publicize list twitter
1692 1567 *
1693 1568 * # List all publicize connections for a given user.
1694 1569 * $ wp --user=1 jetpack publicize list
1695 1570 *
1696 1571 * # List all publicize connections for a given user and service.
1697 - * $ wp --user=1 jetpack publicize list linkedin
1572 + * $ wp --user=1 jetpack publicize list twitter
1698 1573 *
1699 1574 * # Display details for a given connection.
1700 1575 * $ wp jetpack publicize list 123456
1701 1576 *
@@ -1705,37 +1580,33 @@
1705 1580 * # Disconnect all connections.
1706 1581 * $ wp jetpack publicize disconnect all
1707 1582 *
1708 1583 * # Disconnect all connections for a given service.
1709 - * $ wp jetpack publicize disconnect linkedin
1710 - *
1711 - * @param array $args Positional args.
1712 - * @param array $named_args Named args.
1584 + * $ wp jetpack publicize disconnect twitter
1713 1585 */
1714 1586 public function publicize( $args, $named_args ) {
1715 - if ( ! Jetpack::connection()->has_connected_owner() ) {
1716 - WP_CLI::error( __( 'Jetpack Social requires a user-level connection to WordPress.com', 'jetpack' ) );
1587 + if ( ! Jetpack::is_active() ) {
1588 + WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1717 1589 }
1718 1590
1719 1591 if ( ! Jetpack::is_module_active( 'publicize' ) ) {
1720 - WP_CLI::error( __( 'The Jetpack Social module is not active.', 'jetpack' ) );
1592 + WP_CLI::error( __( 'The publicize module is not active.', 'jetpack' ) );
1721 1593 }
1722 1594
1723 - if ( ( new Status() )->is_offline_mode() ) {
1595 + if ( Jetpack::is_development_mode() ) {
1724 1596 if (
1725 1597 ! defined( 'JETPACK_DEV_DEBUG' ) &&
1726 1598 ! has_filter( 'jetpack_development_mode' ) &&
1727 - ! has_filter( 'jetpack_offline_mode' ) &&
1728 - ! str_contains( site_url(), '.' )
1599 + false === strpos( site_url(), '.' )
1729 1600 ) {
1730 - WP_CLI::error( __( "Jetpack is current in offline mode because the site url does not contain a '.', which often occurs when dynamically setting the WP_SITEURL constant. While in offline mode, the Jetpack Social module will not load.", 'jetpack' ) );
1601 + 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' ) );
1731 1602 }
1732 1603
1733 - WP_CLI::error( __( 'Jetpack is currently in offline mode, so the Jetpack Social module will not load.', 'jetpack' ) );
1604 + WP_CLI::error( __( 'Jetpack is currently in development mode, so the publicize module will not load.', 'jetpack' ) );
1734 1605 }
1735 1606
1736 - if ( ! class_exists( Publicize::class ) ) {
1737 - WP_CLI::error( __( 'The Jetpack Social module is not loaded.', 'jetpack' ) );
1607 + if ( ! class_exists( 'Publicize' ) ) {
1608 + WP_CLI::error( __( 'The publicize module is not loaded.', 'jetpack' ) );
1738 1609 }
1739 1610
1740 1611 $action = $args[0];
1741 1612 $publicize = new Publicize();
@@ -1818,12 +1689,12 @@
1818 1689 // If the connection ID is 'all' then delete all connections. If the connection ID
1819 1690 // matches a service, delete all connections for that service.
1820 1691 if ( 'all' === $identifier || $id_is_service ) {
1821 1692 if ( 'all' === $identifier ) {
1822 - WP_CLI::log( __( "You're about to delete all Jetpack Social connections.", 'jetpack' ) );
1693 + WP_CLI::log( __( "You're about to delete all publicize connections.", 'jetpack' ) );
1823 1694 } else {
1824 1695 /* translators: %s is a lowercase string for a social network. */
1825 - WP_CLI::log( sprintf( __( "You're about to delete all Jetpack Social connections to %s.", 'jetpack' ), $identifier ) );
1696 + WP_CLI::log( sprintf( __( "You're about to delete all publicize connections to %s.", 'jetpack' ), $identifier ) );
1826 1697 }
1827 1698
1828 1699 jetpack_cli_are_you_sure();
1829 1700
@@ -1844,9 +1715,9 @@
1844 1715 $connections = $option_connections[ $service ];
1845 1716 }
1846 1717
1847 1718 if ( ! empty( $connections ) ) {
1848 - $count = is_countable( $connections ) ? count( $connections ) : 0;
1719 + $count = count( $connections );
1849 1720 $progress = \WP_CLI\Utils\make_progress_bar(
1850 1721 /* translators: %s is a lowercase string for a social network. */
1851 1722 sprintf( __( 'Disconnecting all connections to %s.', 'jetpack' ), $service ),
1852 1723 $count
@@ -1853,68 +1724,51 @@
1853 1724 );
1854 1725
1855 1726 foreach ( $connections as $id => $connection ) {
1856 1727 if ( false === $publicize->disconnect( false, $id ) ) {
1857 - WP_CLI::error(
1858 - sprintf(
1859 - /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */
1860 - __( 'Jetpack Social connection %d could not be disconnected', 'jetpack' ),
1861 - $id
1862 - )
1863 - );
1728 + WP_CLI::error( sprintf(
1729 + /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */
1730 + __( 'Publicize connection %d could not be disconnected', 'jetpack' ),
1731 + $id
1732 + ) );
1864 1733 }
1865 1734
1866 - // @phan-suppress-next-line PhanUndeclaredClassMethod - Class is missing from php-stubs/wp-cli-stubs 🤷
1867 1735 $progress->tick();
1868 1736 }
1869 1737
1870 - // @phan-suppress-next-line PhanUndeclaredClassMethod - Class is missing from php-stubs/wp-cli-stubs 🤷
1871 1738 $progress->finish();
1872 1739
1873 1740 if ( 'all' === $service ) {
1874 - WP_CLI::success( __( 'All Jetpack Social connections were successfully disconnected.', 'jetpack' ) );
1741 + WP_CLI::success( __( 'All publicize connections were successfully disconnected.', 'jetpack' ) );
1875 1742 } else {
1876 1743 /* translators: %s is a lowercase string for a social network. */
1877 - WP_CLI::success( __( 'All Jetpack Social connections to %s were successfully disconnected.', 'jetpack' ), $service );
1744 + WP_CLI::success( __( 'All publicize connections to %s were successfully disconnected.', 'jetpack' ), $service );
1878 1745 }
1879 1746 }
1880 - } elseif ( false !== $publicize->disconnect( false, $identifier ) ) {
1881 - /* translators: %d is a numeric ID. Example: 1234. */
1882 - WP_CLI::success( sprintf( __( 'Jetpack Social connection %d has been disconnected.', 'jetpack' ), $identifier ) );
1883 1747 } else {
1884 - /* translators: %d is a numeric ID. Example: 1234. */
1885 - WP_CLI::error( sprintf( __( 'Jetpack Social connection %d could not be disconnected.', 'jetpack' ), $identifier ) );
1748 + if ( false !== $publicize->disconnect( false, $identifier ) ) {
1749 + /* translators: %d is a numeric ID. Example: 1234. */
1750 + WP_CLI::success( sprintf( __( 'Publicize connection %d has been disconnected.', 'jetpack' ), $identifier ) );
1751 + } else {
1752 + /* translators: %d is a numeric ID. Example: 1234. */
1753 + WP_CLI::error( sprintf( __( 'Publicize connection %d could not be disconnected.', 'jetpack' ), $identifier ) );
1754 + }
1886 1755 }
1887 1756 break; // disconnect.
1888 1757 }
1889 1758 }
1890 1759
1891 - /**
1892 - * Get the API host.
1893 - *
1894 - * @return string URL.
1895 - */
1896 1760 private function get_api_host() {
1897 1761 $env_api_host = getenv( 'JETPACK_START_API_HOST', true );
1898 - return $env_api_host ? 'https://' . $env_api_host : JETPACK__WPCOM_JSON_API_BASE;
1762 + return $env_api_host ? $env_api_host : JETPACK__WPCOM_JSON_API_HOST;
1899 1763 }
1900 1764
1901 - /**
1902 - * Log and exit on a partner provision error.
1903 - *
1904 - * @param WP_Error $error Error.
1905 - * @return never
1906 - */
1907 1765 private function partner_provision_error( $error ) {
1908 - WP_CLI::log(
1909 - wp_json_encode(
1910 - array(
1911 - 'success' => false,
1912 - 'error_code' => $error->get_error_code(),
1913 - 'error_message' => $error->get_error_message(),
1914 - )
1915 - )
1916 - );
1766 + WP_CLI::log( json_encode( array(
1767 + 'success' => false,
1768 + 'error_code' => $error->get_error_code(),
1769 + 'error_message' => $error->get_error_message()
1770 + ) ) );
1917 1771 exit( 1 );
1918 1772 }
1919 1773
1920 1774 /**
@@ -1930,9 +1784,8 @@
1930 1784 * The title is also used to create the slug and the edit PHP class name. If it's something like "Logo gallery", the slug will be 'logo-gallery' and the class name will be LogoGalleryEdit.
1931 1785 * --slug: Specific slug to identify the block that overrides the one generated based on the title.
1932 1786 * --description: Allows to provide a text description of the block.
1933 1787 * --keywords: Provide up to three keywords separated by comma so users can find this block when they search in Gutenberg's inserter.
1934 - * --variation: Allows to decide whether the block should be a production block, experimental, or beta. Defaults to Beta when arg not provided.
1935 1788 *
1936 1789 * ## BLOCK TYPE EXAMPLES
1937 1790 *
1938 1791 * wp jetpack scaffold block "Cool Block"
@@ -1937,12 +1790,11 @@
1937 1790 *
1938 1791 * wp jetpack scaffold block "Cool Block"
1939 1792 * wp jetpack scaffold block "Amazing Rock" --slug="good-music" --description="Rock the best music on your site"
1940 1793 * wp jetpack scaffold block "Jukebox" --keywords="music, audio, media"
1941 - * wp jetpack scaffold block "Jukebox" --variation="experimental"
1942 1794 *
1943 1795 * @subcommand scaffold block
1944 - * @synopsis <type> <title> [--slug] [--description] [--keywords] [--variation]
1796 + * @synopsis <type> <title> [--slug] [--description] [--keywords]
1945 1797 *
1946 1798 * @param array $args Positional parameters, when strings are passed, wrap them in quotes.
1947 1799 * @param array $assoc_args Associative parameters like --slug="nice-block".
1948 1800 */
@@ -1976,15 +1828,8 @@
1976 1828 $slug = isset( $assoc_args['slug'] )
1977 1829 ? $assoc_args['slug']
1978 1830 : sanitize_title( $title );
1979 1831
1980 - $next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh
1981 -
1982 - $variation_options = array( 'production', 'experimental', 'beta' );
1983 - $variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) )
1984 - ? $assoc_args['variation']
1985 - : 'beta';
1986 -
1987 1832 if ( preg_match( '#^jetpack/#', $slug ) ) {
1988 1833 $slug = preg_replace( '#^jetpack/#', '', $slug );
1989 1834 }
1990 1835
@@ -2006,53 +1851,39 @@
2006 1851 }
2007 1852
2008 1853 $wp_filesystem->mkdir( $path );
2009 1854
2010 - $keywords = isset( $assoc_args['keywords'] )
2011 - ? array_map(
2012 - function ( $keyword ) {
2013 - return trim( $keyword );
2014 - },
2015 - array_slice( explode( ',', $assoc_args['keywords'] ), 0, 3 )
2016 - )
2017 - : array();
1855 + $hasKeywords = isset( $assoc_args['keywords'] );
2018 1856
2019 1857 $files = array(
2020 - "$path/block.json" => self::render_block_file(
2021 - 'block-block-json',
2022 - array(
2023 - 'slug' => $slug,
2024 - 'title' => wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2025 - 'description' => isset( $assoc_args['description'] )
2026 - ? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_UNICODE )
2027 - : wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2028 - 'nextVersion' => $next_version,
2029 - 'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_UNICODE ),
2030 - )
2031 - ),
2032 - "$path/$slug.php" => self::render_block_file(
2033 - 'block-register-php',
2034 - array(
2035 - 'nextVersion' => $next_version,
2036 - 'title' => $title,
2037 - 'underscoredTitle' => str_replace( ' ', '_', $title ),
2038 - )
2039 - ),
2040 - "$path/editor.js" => self::render_block_file( 'block-editor-js' ),
2041 - "$path/editor.scss" => self::render_block_file(
2042 - 'block-editor-scss',
2043 - array(
2044 - 'slug' => $slug,
2045 - 'title' => $title,
2046 - )
2047 - ),
2048 - "$path/edit.js" => self::render_block_file(
2049 - 'block-edit-js',
2050 - array(
2051 - 'title' => $title,
2052 - 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
2053 - )
2054 - ),
1858 + "$path/$slug.php" => $this->render_block_file( 'block-register-php', array(
1859 + 'slug' => $slug,
1860 + 'title' => $title,
1861 + 'underscoredSlug' => str_replace( '-', '_', $slug ),
1862 + ) ),
1863 + "$path/index.js" => $this->render_block_file( 'block-index-js', array(
1864 + 'slug' => $slug,
1865 + 'title' => $title,
1866 + 'description' => isset( $assoc_args['description'] )
1867 + ? $assoc_args['description']
1868 + : $title,
1869 + 'keywords' => $hasKeywords
1870 + ? array_map( function( $keyword ) {
1871 + // Construction necessary for Mustache lists
1872 + return array( 'keyword' => trim( $keyword ) );
1873 + }, explode( ',', $assoc_args['keywords'], 3 ) )
1874 + : '',
1875 + 'hasKeywords' => $hasKeywords
1876 + ) ),
1877 + "$path/editor.js" => $this->render_block_file( 'block-editor-js' ),
1878 + "$path/editor.scss" => $this->render_block_file( 'block-editor-scss', array(
1879 + 'slug' => $slug,
1880 + 'title' => $title,
1881 + ) ),
1882 + "$path/edit.js" => $this->render_block_file( 'block-edit-js', array(
1883 + 'title' => $title,
1884 + 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
1885 + ) )
2055 1886 );
2056 1887
2057 1888 $files_written = array();
2058 1889
@@ -2067,68 +1898,41 @@
2067 1898
2068 1899 if ( empty( $files_written ) ) {
2069 1900 WP_CLI::log( esc_html__( 'No files were created', 'jetpack' ) );
2070 1901 } else {
2071 - // Load index.json and insert the slug of the new block in its block variation array.
1902 + // Load index.json and insert the slug of the new block in the production array
2072 1903 $block_list_path = JETPACK__PLUGIN_DIR . 'extensions/index.json';
2073 - $block_list = $wp_filesystem->get_contents( $block_list_path );
1904 + $block_list = $wp_filesystem->get_contents( $block_list_path );
2074 1905 if ( empty( $block_list ) ) {
2075 1906 /* translators: %s is the path to the file with the block list */
2076 1907 WP_CLI::error( sprintf( esc_html__( 'Error fetching contents of %s', 'jetpack' ), $block_list_path ) );
2077 - } elseif ( false === stripos( $block_list, $slug ) ) {
2078 - $new_block_list = json_decode( $block_list );
2079 - $new_block_list->{ $variation }[] = $slug;
2080 -
2081 - // Format the JSON to match our coding standards.
2082 - $new_block_list_formatted = wp_json_encode( $new_block_list, JSON_PRETTY_PRINT ) . "\n";
2083 - $new_block_list_formatted = preg_replace_callback(
2084 - // Find all occurrences of multiples of 4 spaces a the start of the line.
2085 - '/^((?: )+)/m',
2086 - function ( $matches ) {
2087 - // Replace each occurrence of 4 spaces with a tab character.
2088 - return str_repeat( "\t", substr_count( $matches[0], ' ' ) );
2089 - },
2090 - $new_block_list_formatted
2091 - );
2092 -
2093 - if ( ! $wp_filesystem->put_contents( $block_list_path, $new_block_list_formatted ) ) {
1908 + } else if ( false === stripos( $block_list, $slug ) ) {
1909 + $new_block_list = json_decode( $block_list );
1910 + $new_block_list->beta[] = $slug;
1911 + if ( ! $wp_filesystem->put_contents( $block_list_path, wp_json_encode( $new_block_list ) ) ) {
2094 1912 /* translators: %s is the path to the file with the block list */
2095 1913 WP_CLI::error( sprintf( esc_html__( 'Error writing new %s', 'jetpack' ), $block_list_path ) );
2096 1914 }
2097 1915 }
2098 1916
2099 - if ( 'beta' === $variation || 'experimental' === $variation ) {
2100 - $block_constant = sprintf(
2101 - /* translators: the placeholder is a constant name */
2102 - esc_html__( 'To load the block, add the constant JETPACK_BLOCKS_VARIATION set to %1$s to your wp-config.php file', 'jetpack' ),
2103 - $variation
2104 - );
2105 - } else {
2106 - $block_constant = '';
2107 - }
2108 -
2109 - WP_CLI::success(
2110 - sprintf(
2111 - /* translators: the placeholders are a human readable title, and a series of words separated by dashes */
2112 - esc_html__( 'Successfully created block %1$s with slug %2$s', 'jetpack' ) . ' 🎉' . "\n" .
2113 - "--------------------------------------------------------------------------------------------------------------------\n" .
2114 - /* translators: the placeholder is a directory path */
2115 - esc_html__( 'The files were created at %3$s', 'jetpack' ) . "\n" .
2116 - esc_html__( 'To start using the block, build the blocks with pnpm run build-extensions', 'jetpack' ) . "\n" .
2117 - /* translators: the placeholder is a file path */
2118 - esc_html__( 'The block slug has been added to the %4$s list at %5$s', 'jetpack' ) . "\n" .
2119 - '%6$s' . "\n" .
2120 - /* translators: the placeholder is a URL */
2121 - "\n" . esc_html__( 'Read more at %7$s', 'jetpack' ) . "\n",
2122 - $title,
2123 - $slug,
2124 - $path,
2125 - $variation,
2126 - $block_list_path,
2127 - $block_constant,
2128 - 'https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#developing-block-editor-extensions-in-jetpack'
2129 - ) . '--------------------------------------------------------------------------------------------------------------------'
2130 - );
1917 + WP_CLI::success( sprintf(
1918 + /* translators: the placeholders are a human readable title, and a series of words separated by dashes */
1919 + esc_html__( 'Successfully created block %s with slug %s', 'jetpack' ) . ' 🎉' . "\n" .
1920 + "--------------------------------------------------------------------------------------------------------------------\n" .
1921 + /* translators: the placeholder is a directory path */
1922 + esc_html__( 'The files were created at %s', 'jetpack' ) . "\n" .
1923 + esc_html__( 'To start using the block, build the blocks with yarn run build-extensions', 'jetpack' ) . "\n" .
1924 + /* translators: the placeholder is a file path */
1925 + esc_html__( 'The block slug has been added to the beta list at %s', 'jetpack' ) . "\n" .
1926 + esc_html__( 'To load the block, add the constant JETPACK_BETA_BLOCKS as true to your wp-config.php file', 'jetpack' ) . "\n" .
1927 + /* translators: the placeholder is a URL */
1928 + "\n" . esc_html__( 'Read more at %s', 'jetpack' ) . "\n",
1929 + $title,
1930 + $slug,
1931 + $path,
1932 + $block_list_path,
1933 + 'https://github.com/Automattic/jetpack/blob/master/extensions/README.md#develop-new-blocks'
1934 + ) . '--------------------------------------------------------------------------------------------------------------------' );
2131 1935 }
2132 1936 }
2133 1937
2134 1938 /**
@@ -2133,10 +1937,11 @@
2133 1937
2134 1938 /**
2135 1939 * Built the file replacing the placeholders in the template with the data supplied.
2136 1940 *
2137 - * @param string $template Template.
2138 - * @param array $data Data.
1941 + * @param string $template
1942 + * @param array $data
1943 + *
2139 1944 * @return string mixed
2140 1945 */
2141 1946 private static function render_block_file( $template, $data = array() ) {
2142 1947 return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data );
@@ -2142,23 +1947,21 @@
2142 1947 return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data );
2143 1948 }
2144 1949 }
2145 1950
2146 -// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.
2147 -
2148 -/**
1951 +/*
2149 1952 * Standard "ask for permission to continue" function.
2150 1953 * If action cancelled, ask if they need help.
2151 1954 *
2152 1955 * Written outside of the class so it's not listed as an executable command w/ 'wp jetpack'
2153 1956 *
2154 - * @param bool $flagged false = normal option | true = flagged by get_jetpack_options_for_reset().
2155 - * @param string $error_msg Error message.
1957 + * @param $flagged bool false = normal option | true = flagged by get_jetpack_options_for_reset()
1958 + * @param $error_msg string (optional)
2156 1959 */
2157 1960 function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) {
2158 1961 $cli = new Jetpack_CLI();
2159 1962
2160 - // Default cancellation message.
1963 + // Default cancellation message
2161 1964 if ( ! $error_msg ) {
2162 1965 $error_msg =
2163 1966 __( 'Action cancelled. Have a question?', 'jetpack' )
2164 1967 . ' '
@@ -2163,20 +1966,20 @@
2163 1966 __( 'Action cancelled. Have a question?', 'jetpack' )
2164 1967 . ' '
2165 1968 . $cli->green_open
2166 1969 . 'jetpack.com/support'
2167 - . $cli->color_close;
1970 + . $cli->color_close;
2168 1971 }
2169 1972
2170 1973 if ( ! $flagged ) {
2171 1974 $prompt_message = _x( 'Are you sure? This cannot be undone. Type "yes" to continue:', '"yes" is a command - do not translate.', 'jetpack' );
2172 1975 } else {
2173 - $prompt_message = _x( 'Are you sure? Modifying this option may disrupt your Jetpack connection. Type "yes" to continue.', '"yes" is a command - do not translate.', 'jetpack' );
1976 + $prompt_message = _x( 'Are you sure? Modifying this option may disrupt your Jetpack connection. Type "yes" to continue.', '"yes" is a command - do not translate.', 'jetpack' );
2174 1977 }
2175 1978
2176 1979 WP_CLI::line( $prompt_message );
2177 - $handle = fopen( 'php://stdin', 'r' );
2178 - $line = fgets( $handle );
2179 - if ( 'yes' !== trim( $line ) ) {
1980 + $handle = fopen( "php://stdin", "r" );
1981 + $line = fgets( $handle );
1982 + if ( 'yes' != trim( $line ) ){
2180 1983 WP_CLI::error( $error_msg );
2181 1984 }
2182 1985 }