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