PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2
Jetpack – WP Security, Backup, Speed, & Growth v7.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
← All changes | class.jetpack-cli.php +291 -957 13.2.47.2 View file →
@@ -1,60 +1,19 @@
1 -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 -/**
3 - * WP-CLI command class.
4 - *
5 - * @package automattic/jetpack
6 - */
1 +<?php
7 2
8 -use Automattic\Jetpack\Connection\Client;
9 -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 -use Automattic\Jetpack\Sync\Actions;
15 -use Automattic\Jetpack\Sync\Listener;
16 -use Automattic\Jetpack\Sync\Modules;
17 -use Automattic\Jetpack\Sync\Queue;
18 -use Automattic\Jetpack\Sync\Settings;
19 -use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions;
20 -
21 -if ( ! class_exists( 'WP_CLI_Command' ) ) {
22 - return;
23 -}
24 -
25 3 WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' );
26 4
27 5 /**
28 6 * Control your local Jetpack installation.
7 + *
8 + * Minimum PHP requirement for WP-CLI is PHP 5.3, so ignore PHP 5.2 compatibility issues.
9 + * @phpcs:disable PHPCompatibility.PHP.NewLanguageConstructs.t_ns_separatorFound
29 10 */
30 11 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 - */
12 + // Aesthetics
13 + public $green_open = "\033[32m";
14 + public $red_open = "\033[31m";
50 15 public $yellow_open = "\033[33m";
51 -
52 - /**
53 - * Console escape code to reset coloring.
54 - *
55 - * @var string
56 - */
57 16 public $color_close = "\033[0m";
58 17
59 18 /**
60 19 * Get Jetpack Details
@@ -69,14 +28,12 @@
69 28 *
70 29 * wp jetpack status
71 30 * wp jetpack status full
72 31 *
73 - * @param array $args Positional args.
74 32 */
75 - public function status( $args ) {
76 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
33 + public function status( $args, $assoc_args ) {
34 + jetpack_require_lib( 'debugger' );
77 35
78 - /* translators: %s is the site URL */
79 36 WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) );
80 37
81 38 if ( isset( $args[0] ) && 'full' !== $args[0] ) {
82 39 /* translators: %s is a command like "prompt" */
@@ -93,22 +50,19 @@
93 50 WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) );
94 51 } else {
95 52 $error = array();
96 53 foreach ( $cxntests->list_fails() as $fail ) {
97 - $error[] = $fail['name'] . ( empty( $fail['message'] ) ? '' : ': ' . $fail['message'] );
54 + $error[] = $fail['name'] . ': ' . $fail['message'];
98 55 }
99 56 WP_CLI::error_multi_line( $error );
100 57
101 58 $cxntests->output_results_for_cli();
102 59
103 - WP_CLI::error( __( 'One or more tests did not pass. Please investigate!', 'jetpack' ) ); // Exit CLI.
60 + WP_CLI::error( __('Jetpack connection is broken.', 'jetpack' ) ); // Exit CLI.
104 61 }
105 62
106 - /* translators: %s is current version of Jetpack, for example 7.3 */
107 63 WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) );
108 - /* translators: %d is WP.com ID of this blog */
109 64 WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) );
110 - /* translators: %s is the email address of the connection owner */
111 65 WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) );
112 66
113 67 /*
114 68 * Are they asking for all data?
@@ -114,41 +68,41 @@
114 68 * Are they asking for all data?
115 69 *
116 70 * Loop through heartbeat data and organize by priority.
117 71 */
118 - $all_data = ( isset( $args[0] ) && 'full' === $args[0] ) ? 'full' : false;
72 + $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false;
119 73 if ( $all_data ) {
120 - // Heartbeat data.
74 + // Heartbeat data
121 75 WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) );
122 76
123 77 // Get the filtered heartbeat data.
124 - // Filtered so we can color/list by severity.
78 + // Filtered so we can color/list by severity
125 79 $stats = Jetpack::jetpack_check_heartbeat_data();
126 80
127 - // Display red flags first.
81 + // Display red flags first
128 82 foreach ( $stats['bad'] as $stat => $value ) {
129 - WP_CLI::line( sprintf( "$this->red_open%-'.16s %s $this->color_close", $stat, $value ) );
83 + printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value );
130 84 }
131 85
132 - // Display caution warnings next.
86 + // Display caution warnings next
133 87 foreach ( $stats['caution'] as $stat => $value ) {
134 - WP_CLI::line( sprintf( "$this->yellow_open%-'.16s %s $this->color_close", $stat, $value ) );
88 + printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value );
135 89 }
136 90
137 91 // The rest of the results are good!
138 92 foreach ( $stats['good'] as $stat => $value ) {
139 93
140 - // Modules should get special spacing for aestetics.
94 + // Modules should get special spacing for aestetics
141 95 if ( strpos( $stat, 'odule-' ) ) {
142 - WP_CLI::line( sprintf( "%-'.30s %s", $stat, $value ) );
143 - usleep( 4000 ); // For dramatic effect lolz.
96 + printf( "%-'.30s %s\n", $stat, $value );
97 + usleep( 4000 ); // For dramatic effect lolz
144 98 continue;
145 99 }
146 - WP_CLI::line( sprintf( "%-'.16s %s", $stat, $value ) );
147 - usleep( 4000 ); // For dramatic effect lolz.
100 + printf( "%-'.16s %s\n", $stat, $value );
101 + usleep( 4000 ); // For dramatic effect lolz
148 102 }
149 103 } else {
150 - // Just the basics.
104 + // Just the basics
151 105 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 106 }
153 107 }
154 108
@@ -162,20 +116,19 @@
162 116 * wp jetpack test-connection
163 117 *
164 118 * @subcommand test-connection
165 119 */
166 - public function test_connection() {
120 + public function test_connection( $args, $assoc_args ) {
167 121
168 - /* translators: %s is the site URL */
169 122 WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) );
170 123
171 - if ( ! Jetpack::is_connection_ready() ) {
124 + if ( ! Jetpack::is_active() ) {
172 125 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
173 126 }
174 127
175 - $response = Client::wpcom_json_api_request_as_blog(
128 + $response = Jetpack_Client::wpcom_json_api_request_as_blog(
176 129 sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
177 - Client::WPCOM_JSON_API_VERSION
130 + Jetpack_Client::WPCOM_JSON_API_VERSION
178 131 );
179 132
180 133 if ( is_wp_error( $response ) ) {
181 134 /* translators: %1$s is the error code, %2$s is the error message */
@@ -186,11 +139,11 @@
186 139 if ( ! $body ) {
187 140 WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) );
188 141 }
189 142
190 - $result = json_decode( $body );
143 + $result = json_decode( $body );
191 144 $is_connected = (bool) $result->connected;
192 - $message = $result->message;
145 + $message = $result->message;
193 146
194 147 if ( $is_connected ) {
195 148 WP_CLI::success( $message );
196 149 } else {
@@ -206,52 +159,46 @@
206 159 * blog: Disconnect the entire blog.
207 160 *
208 161 * user <user_identifier>: Disconnect a specific user from WordPress.com.
209 162 *
210 - * [--force]
211 - * If the user ID provided is the connection owner, it will only be disconnected if --force is passed
163 + * Please note, the primary account that the blog is connected
164 + * to WordPress.com with cannot be disconnected without
165 + * disconnecting the entire blog.
212 166 *
213 167 * ## EXAMPLES
214 168 *
215 169 * wp jetpack disconnect blog
216 170 * wp jetpack disconnect user 13
217 - * wp jetpack disconnect user 1 --force
218 171 * wp jetpack disconnect user username
219 172 * wp jetpack disconnect user email@domain.com
220 173 *
221 - * @synopsis <blog|user> [<user_identifier>] [--force]
222 - *
223 - * @param array $args Positional args.
224 - * @param array $assoc_args Named args.
174 + * @synopsis <blog|user> [<user_identifier>]
225 175 */
226 176 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;
177 + if ( ! Jetpack::is_active() ) {
178 + WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) );
231 179 }
232 180
233 181 $action = isset( $args[0] ) ? $args[0] : 'prompt';
234 - if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ), true ) ) {
182 + if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) {
235 183 /* translators: %s is a command like "prompt" */
236 184 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
237 185 }
238 186
239 - if ( in_array( $action, array( 'user' ), true ) ) {
187 + if ( in_array( $action, array( 'user' ) ) ) {
240 188 if ( isset( $args[1] ) ) {
241 189 $user_id = $args[1];
242 190 if ( ctype_digit( $user_id ) ) {
243 - $field = 'id';
191 + $field = 'id';
244 192 $user_id = (int) $user_id;
245 193 } elseif ( is_email( $user_id ) ) {
246 - $field = 'email';
194 + $field = 'email';
247 195 $user_id = sanitize_user( $user_id, true );
248 196 } else {
249 - $field = 'login';
197 + $field = 'login';
250 198 $user_id = sanitize_user( $user_id, true );
251 199 }
252 - $user = get_user_by( $field, $user_id );
253 - if ( ! $user ) {
200 + if ( ! $user = get_user_by( $field, $user_id ) ) {
254 201 WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) );
255 202 }
256 203 } else {
257 204 WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) );
@@ -257,40 +204,24 @@
257 204 WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) );
258 205 }
259 206 }
260 207
261 - $force_user_disconnect = ! empty( $assoc_args['force'] );
262 -
263 208 switch ( $action ) {
264 209 case 'blog':
265 210 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 - );
211 + Jetpack::disconnect();
212 + WP_CLI::success( sprintf(
213 + __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
214 + esc_url( get_site_url() )
215 + ) );
274 216 break;
275 217 case 'user':
276 - $connection_manager = new Connection_Manager( 'jetpack' );
277 - $disconnected = $connection_manager->disconnect_user( $user->ID, $force_user_disconnect );
278 - if ( $disconnected ) {
218 + if ( Jetpack::unlink_user( $user->ID ) ) {
279 219 Jetpack::log( 'unlink', $user->ID );
280 220 WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) );
281 221 } 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 );
222 + /* translators: %s is a username */
223 + WP_CLI::error( sprintf( __( "User %s could not be disconnected. Are you sure they're connected currently?", 'jetpack' ), "{$user->login} <{$user->email}>" ) );
293 224 }
294 225 break;
295 226 case 'prompt':
296 227 WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) );
@@ -315,191 +246,71 @@
315 246 * ## EXAMPLES
316 247 *
317 248 * wp jetpack reset options
318 249 * wp jetpack reset modules
319 - * wp jetpack reset sync-checksum --dry-run --offset=0
320 250 *
321 - * @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>]
322 - *
323 - * @param array $args Positional args.
324 - * @param array $assoc_args Named args.
251 + * @synopsis <modules|options>
325 252 */
326 253 public function reset( $args, $assoc_args ) {
327 254 $action = isset( $args[0] ) ? $args[0] : 'prompt';
328 - if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) {
255 + if ( ! in_array( $action, array( 'options', 'modules' ) ) ) {
329 256 /* translators: %s is a command like "prompt" */
330 257 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
331 258 }
332 259
333 - $is_dry_run = ! empty( $assoc_args['dry-run'] );
260 + // Are you sure?
261 + jetpack_cli_are_you_sure();
334 262
335 - if ( $is_dry_run ) {
336 - WP_CLI::warning(
337 - __( "\nThis is a dry run.\n", 'jetpack' ) .
338 - __( "No actions will be taken.\n", 'jetpack' ) .
339 - __( "The following messages will give you preview of what will happen when you run this command.\n\n", 'jetpack' )
340 - );
341 - } else {
342 - // We only need to confirm "Are you sure?" when we are not doing a dry run.
343 - jetpack_cli_are_you_sure();
344 - }
345 -
346 263 switch ( $action ) {
347 264 case 'options':
348 265 $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.
266 +
267 + // Reset the Jetpack options
268 + WP_CLI::line( sprintf(
269 + __( "Resetting Jetpack Options for %s...\n", "jetpack" ),
270 + esc_url( get_site_url() )
271 + ) );
272 + sleep(1); // Take a breath
358 273 foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
359 - if ( ! $is_dry_run ) {
360 - Jetpack_Options::delete_option( $option_to_reset );
361 - usleep( 100000 );
362 - }
363 -
274 + Jetpack_Options::delete_option( $option_to_reset );
275 + usleep( 100000 );
364 276 /* translators: This is the result of an action. The option named %s was reset */
365 277 WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
366 278 }
367 279
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.
280 + // Reset the WP options
281 + WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", "jetpack" ) );
282 + usleep( 500000 ); // Take a breath
371 283 foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
372 - if ( ! $is_dry_run ) {
373 - delete_option( $option_to_reset );
374 - usleep( 100000 );
375 - }
284 + delete_option( $option_to_reset );
285 + usleep( 100000 );
376 286 /* translators: This is the result of an action. The option named %s was reset */
377 287 WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
378 288 }
379 289
380 - // Reset to default modules.
381 - WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) );
382 - usleep( 500000 ); // Take a breath.
290 + // Reset to default modules
291 + WP_CLI::line( __( "Resetting default modules...\n", "jetpack" ) );
292 + usleep( 500000 ); // Take a breath
383 293 $default_modules = Jetpack::get_default_modules();
384 - if ( ! $is_dry_run ) {
385 - Jetpack::update_active_modules( $default_modules );
386 - }
294 + Jetpack::update_active_modules( $default_modules );
387 295 WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
296 +
297 + // Jumpstart option is special
298 + Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
299 + WP_CLI::success( __( 'jumpstart option reset', 'jetpack' ) );
388 300 break;
389 301 case 'modules':
390 - if ( ! $is_dry_run ) {
391 - $default_modules = Jetpack::get_default_modules();
392 - Jetpack::update_active_modules( $default_modules );
393 - }
394 -
302 + $default_modules = Jetpack::get_default_modules();
303 + Jetpack::update_active_modules( $default_modules );
395 304 WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
396 305 break;
397 306 case 'prompt':
398 - WP_CLI::error( __( 'Please specify if you would like to reset your options, modules or sync-checksum', 'jetpack' ) );
307 + WP_CLI::error( __( 'Please specify if you would like to reset your options, or modules', 'jetpack' ) );
399 308 break;
400 - case 'sync-checksum':
401 - $option = 'jetpack_callables_sync_checksum';
402 -
403 - if ( is_multisite() ) {
404 - $offset = isset( $assoc_args['offset'] ) ? (int) $assoc_args['offset'] : 0;
405 -
406 - /*
407 - * 1000 is a good limit since we don't expect the number of sites to be more than 1000
408 - * Offset can be used to paginate and try to clean up more sites.
409 - */
410 - $sites = get_sites(
411 - array(
412 - 'number' => 1000,
413 - 'offset' => $offset,
414 - )
415 - );
416 - $count_fixes = 0;
417 - foreach ( $sites as $site ) {
418 - switch_to_blog( $site->blog_id );
419 - $count = self::count_option( $option );
420 - if ( $count > 1 ) {
421 - if ( ! $is_dry_run ) {
422 - delete_option( $option );
423 - }
424 - WP_CLI::line(
425 - sprintf(
426 - /* translators: %1$d is a number, %2$s is the name of an option, %2$s is the site URL. */
427 - __( 'Deleted %1$d %2$s options from %3$s', 'jetpack' ),
428 - $count,
429 - $option,
430 - "{$site->domain}{$site->path}"
431 - )
432 - );
433 - ++$count_fixes;
434 - if ( ! $is_dry_run ) {
435 - /*
436 - * We could be deleting a lot of options rows at the same time.
437 - * Allow some time for replication to catch up.
438 - */
439 - sleep( 3 );
440 - }
441 - }
442 -
443 - restore_current_blog();
444 - }
445 - if ( $count_fixes ) {
446 - WP_CLI::success(
447 - sprintf(
448 - /* translators: %1$s is the name of an option, %2$d is a number of sites. */
449 - __( 'Successfully reset %1$s on %2$d sites.', 'jetpack' ),
450 - $option,
451 - $count_fixes
452 - )
453 - );
454 - } else {
455 - WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
456 - }
457 - return;
458 - }
459 -
460 - $count = self::count_option( $option );
461 - if ( $count > 1 ) {
462 - if ( ! $is_dry_run ) {
463 - delete_option( $option );
464 - }
465 - WP_CLI::success(
466 - sprintf(
467 - /* translators: %1$d is a number, %2$s is the name of an option. */
468 - __( 'Deleted %1$d %2$s options', 'jetpack' ),
469 - $count,
470 - $option
471 - )
472 - );
473 - return;
474 - }
475 -
476 - WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
477 - break;
478 -
479 309 }
480 310 }
481 311
482 312 /**
483 - * Return the number of times an option appears
484 - * Normally an option would only appear 1 since the option key is supposed to be unique
485 - * but if a site hasn't updated the DB schema then that would not be the case.
486 - *
487 - * @param string $option Option name.
488 - *
489 - * @return int
490 - */
491 - private static function count_option( $option ) {
492 - global $wpdb;
493 - return (int) $wpdb->get_var(
494 - $wpdb->prepare(
495 - "SELECT COUNT(*) FROM $wpdb->options WHERE option_name = %s",
496 - $option
497 - )
498 - );
499 - }
500 -
501 - /**
502 313 * Manage Jetpack Modules
503 314 *
504 315 * ## OPTIONS
505 316 *
@@ -538,15 +349,11 @@
538 349 * wp jetpack module deactivate stats
539 350 * wp jetpack module toggle stats
540 351 * wp jetpack module activate all
541 352 * wp jetpack module deactivate all
542 - *
543 - * @param array $args Positional args.
544 - * @param array $assoc_args Named args.
545 353 */
546 354 public function module( $args, $assoc_args ) {
547 - $module_slug = null;
548 - $action = isset( $args[0] ) ? $args[0] : 'list';
355 + $action = isset( $args[0] ) ? $args[0] : 'list';
549 356
550 357 if ( isset( $args[1] ) ) {
551 358 $module_slug = $args[1];
552 359 if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) {
@@ -589,12 +396,10 @@
589 396 case 'activate':
590 397 $module = Jetpack::get_module( $module_slug );
591 398 Jetpack::log( 'activate', $module_slug );
592 399 if ( Jetpack::activate_module( $module_slug, false, false ) ) {
593 - /* translators: %s is the name of a Jetpack module */
594 400 WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) );
595 401 } else {
596 - /* translators: %s is the name of a Jetpack module */
597 402 WP_CLI::error( sprintf( __( '%s could not be activated.', 'jetpack' ), $module['name'] ) );
598 403 }
599 404 break;
600 405 case 'activate_all':
@@ -605,9 +410,8 @@
605 410 case 'deactivate':
606 411 $module = Jetpack::get_module( $module_slug );
607 412 Jetpack::log( 'deactivate', $module_slug );
608 413 Jetpack::deactivate_module( $module_slug );
609 - /* translators: %s is the name of a Jetpack module */
610 414 WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) );
611 415 break;
612 416 case 'deactivate_all':
613 417 Jetpack::delete_active_modules();
@@ -623,122 +427,119 @@
623 427 * Manage Protect Settings
624 428 *
625 429 * ## OPTIONS
626 430 *
627 - * allow: Add an IP address to an always allow list. You can also read or clear the allow list.
431 + * whitelist: Whitelist an IP address. You can also read or clear the whitelist.
628 432 *
629 433 *
630 434 * ## EXAMPLES
631 435 *
632 - * wp jetpack protect allow <ip address>
633 - * wp jetpack protect allow list
634 - * wp jetpack protect allow clear
436 + * wp jetpack protect whitelist <ip address>
437 + * wp jetpack protect whitelist list
438 + * wp jetpack protect whitelist clear
635 439 *
636 - * @synopsis <allow> [<ip|ip_low-ip_high|list|clear>]
637 - *
638 - * @param array $args Positional args.
440 + * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>]
639 441 */
640 - public function protect( $args ) {
442 + public function protect( $args, $assoc_args ) {
641 443 $action = isset( $args[0] ) ? $args[0] : 'prompt';
642 - if ( ! in_array( $action, array( 'whitelist', 'allow' ), true ) ) { // Still allow "whitelist" for legacy support.
444 + if ( ! in_array( $action, array( 'whitelist' ) ) ) {
643 445 /* translators: %s is a command like "prompt" */
644 446 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
645 447 }
646 - // Check if module is active.
448 + // Check if module is active
647 449 if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) {
648 - /* 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__ ) );
450 + 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 451 }
651 - if ( in_array( $action, array( 'allow', 'whitelist' ), true ) ) {
452 + if ( in_array( $action, array( 'whitelist' ) ) ) {
652 453 if ( isset( $args[1] ) ) {
653 - $action = 'allow';
454 + $action = 'whitelist';
654 455 } else {
655 456 $action = 'prompt';
656 457 }
657 458 }
658 459 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.
460 + case 'whitelist':
461 + $whitelist = array();
462 + $new_ip = $args[1];
463 + $current_whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
663 464
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 ) {
465 + // Build array of IPs that are already whitelisted.
466 + // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get
467 + // low & high range params for jetpack_protect_ip_address_is_in_range();
468 + foreach( $current_whitelist as $whitelisted ) {
668 469
669 - // IP ranges.
670 - if ( $allowed->range ) {
470 + // IP ranges
471 + if ( $whitelisted->range ) {
671 472
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 ) ) {
473 + // Is it already whitelisted?
474 + if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) {
674 475 /* translators: %s is an IP address */
675 - WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
476 + WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) );
676 477 break;
677 478 }
678 - $allow[] = $allowed->range_low . ' - ' . $allowed->range_high;
479 + $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high;
679 480
680 - } else { // Individual IPs.
481 + } else { // Individual IPs
681 482
682 - // Check if the IP is already on the allow list (single IP only).
683 - if ( $new_ip === $allowed->ip_address ) {
483 + // Check if the IP is already whitelisted (single IP only)
484 + if ( $new_ip == $whitelisted->ip_address ) {
684 485 /* translators: %s is an IP address */
685 - WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
486 + WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) );
686 487 break;
687 488 }
688 - $allow[] = $allowed->ip_address;
489 + $whitelist[] = $whitelisted->ip_address;
689 490
690 491 }
691 492 }
692 493
693 494 /*
694 - * List the allowed IPs.
695 - * Done here because it's easier to read the $allow array after it's been rebuilt.
495 + * List the whitelist
496 + * Done here because it's easier to read the $whitelist array after it's been rebuilt
696 497 */
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 ) );
498 + if ( isset( $args[1] ) && 'list' == $args[1] ) {
499 + if ( ! empty( $whitelist ) ) {
500 + WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) );
501 + foreach ( $whitelist as $ip ) {
502 + WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ;
702 503 }
703 504 } else {
704 - WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
505 + WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
705 506 }
706 507 break;
707 508 }
708 509
709 510 /*
710 - * Clear the always allow list.
511 + * Clear the whitelist
711 512 */
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' ) );
513 + if ( isset( $args[1] ) && 'clear' == $args[1] ) {
514 + if ( ! empty( $whitelist ) ) {
515 + $whitelist = array();
516 + jetpack_protect_save_whitelist( $whitelist );
517 + WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) );
717 518 } else {
718 - WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
519 + WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
719 520 }
720 521 break;
721 522 }
722 523
723 - // Append new IP to allow array.
724 - array_push( $allow, $new_ip );
524 + // Append new IP to whitelist array
525 + array_push( $whitelist, $new_ip );
725 526
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.
527 + // Save whitelist if there are no errors
528 + $result = jetpack_protect_save_whitelist( $whitelist );
728 529 if ( is_wp_error( $result ) ) {
729 - WP_CLI::error( $result );
530 + WP_CLI::error( __( $result, 'jetpack' ) );
730 531 }
731 532
732 533 /* translators: %s is an IP address */
733 - WP_CLI::success( sprintf( __( '%s has been added to the always allowed list.', 'jetpack' ), $new_ip ) );
534 + WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) );
734 535 break;
735 536 case 'prompt':
736 537 WP_CLI::error(
737 538 __( '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"
539 + __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" .
540 + _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" .
541 + _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n"
741 542 );
742 543 break;
743 544 }
744 545 }
@@ -762,29 +563,30 @@
762 563 * wp jetpack options delete <option_name>
763 564 * wp jetpack options update <option_name> [<option_value>]
764 565 *
765 566 * @synopsis <list|get|delete|update> [<option_name>] [<option_value>]
766 - *
767 - * @param array $args Positional args.
768 567 */
769 - public function options( $args ) {
770 - $action = isset( $args[0] ) ? $args[0] : 'list';
568 + public function options( $args, $assoc_args ) {
569 + $action = isset( $args[0] ) ? $args[0] : 'list';
771 570 $safe_to_modify = Jetpack_Options::get_options_for_reset();
772 571
572 + // Jumpstart is special
573 + array_push( $safe_to_modify, 'jumpstart' );
574 +
773 575 // Is the option flagged as unsafe?
774 - $flagged = ! in_array( $args[1], $safe_to_modify, true );
576 + $flagged = ! in_array( $args[1], $safe_to_modify );
775 577
776 - if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ), true ) ) {
578 + if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) {
777 579 /* translators: %s is a command like "prompt" */
778 580 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
779 581 }
780 582
781 583 if ( isset( $args[0] ) ) {
782 - if ( 'get' === $args[0] && isset( $args[1] ) ) {
584 + if ( 'get' == $args[0] && isset( $args[1] ) ) {
783 585 $action = 'get';
784 - } elseif ( 'delete' === $args[0] && isset( $args[1] ) ) {
586 + } else if ( 'delete' == $args[0] && isset( $args[1] ) ) {
785 587 $action = 'delete';
786 - } elseif ( 'update' === $args[0] && isset( $args[1] ) ) {
588 + } else if ( 'update' == $args[0] && isset( $args[1] ) ) {
787 589 $action = 'update';
788 590 } else {
789 591 $action = 'list';
790 592 }
@@ -789,18 +591,17 @@
789 591 $action = 'list';
790 592 }
791 593 }
792 594
793 - // Bail if the option isn't found.
595 + // Bail if the option isn't found
794 596 $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false;
795 597 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' ) );
598 + WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) );
797 599 }
798 600
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;
601 + // Let's print_r the option if it's an array
602 + // Used in the 'get' and 'list' actions
603 + $option = is_array( $option ) ? print_r( $option ) : $option;
803 604
804 605 switch ( $action ) {
805 606 case 'get':
806 607 WP_CLI::success( "\t" . $option );
@@ -808,9 +609,8 @@
808 609 case 'delete':
809 610 jetpack_cli_are_you_sure( $flagged );
810 611
811 612 Jetpack_Options::delete_option( $args[1] );
812 - /* translators: %s is the option name */
813 613 WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) );
814 614 break;
815 615 case 'update':
816 616 jetpack_cli_are_you_sure( $flagged );
@@ -821,10 +621,9 @@
821 621 WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) );
822 622 }
823 623
824 624 Jetpack_Options::update_option( $args[1], $args[2] );
825 - /* translators: %1$s is the previous value, %2$s is the new value */
826 - WP_CLI::success( sprintf( _x( 'Updated option: %1$s to "%2$s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) );
625 + WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) );
827 626 break;
828 627 case 'list':
829 628 $options_compact = Jetpack_Options::get_option_names();
830 629 $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' );
@@ -830,14 +629,14 @@
830 629 $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' );
831 630 $options_private = Jetpack_Options::get_option_names( 'private' );
832 631 $options = array_merge( $options_compact, $options_non_compact, $options_private );
833 632
834 - // Table headers.
633 + // Table headers
835 634 WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) );
836 635
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.
636 + // List out the options and their values
637 + // Tell them if the value is empty or not
638 + // Tell them if it's an array
840 639 foreach ( $options as $option ) {
841 640 $value = Jetpack_Options::get_option( $option );
842 641 if ( ! $value ) {
843 642 WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' );
@@ -845,9 +644,9 @@
845 644 }
846 645
847 646 if ( ! is_array( $value ) ) {
848 647 WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value );
849 - } elseif ( is_array( $value ) ) {
648 + } else if ( is_array( $value ) ) {
850 649 WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' );
851 650 }
852 651 }
853 652 $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}';
@@ -854,11 +653,11 @@
854 653 $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}';
855 654
856 655 WP_CLI::success(
857 656 _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" .
657 + str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" .
859 658 str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" .
860 - str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text\n" .
659 + str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" .
861 660 _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n"
862 661 );
863 662 break;
864 663 }
@@ -868,150 +667,60 @@
868 667 * Get the status of or start a new Jetpack sync.
869 668 *
870 669 * ## OPTIONS
871 670 *
872 - * status : Print the current sync status
873 - * settings : Prints the current sync settings
874 - * start : Start a full sync from this site to WordPress.com
875 - * enable : Enables sync on the site
876 - * disable : Disable sync on a site
877 - * reset : Disables sync and Resets the sync queues on a site
671 + * status : Print the current sync status
672 + * start : Start a full sync from this site to WordPress.com
878 673 *
879 674 * ## EXAMPLES
880 675 *
881 676 * wp jetpack sync status
882 - * wp jetpack sync settings
883 677 * wp jetpack sync start --modules=functions --sync_wait_time=5
884 - * wp jetpack sync enable
885 - * wp jetpack sync disable
886 - * wp jetpack sync reset
887 - * wp jetpack sync reset --queue=full or regular
888 678 *
889 679 * @synopsis <status|start> [--<field>=<value>]
890 - *
891 - * @param array $args Positional args.
892 - * @param array $assoc_args Named args.
893 680 */
894 681 public function sync( $args, $assoc_args ) {
682 + if ( ! Jetpack_Sync_Actions::sync_allowed() ) {
683 + WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) );
684 + }
895 685
896 686 $action = isset( $args[0] ) ? $args[0] : 'status';
897 687
898 688 switch ( $action ) {
899 689 case 'status':
900 - $status = Actions::get_sync_status();
690 + $status = Jetpack_Sync_Actions::get_sync_status();
901 691 $collection = array();
902 692 foreach ( $status as $key => $item ) {
903 - $collection[] = array(
693 + $collection[] = array(
904 694 'option' => $key,
905 - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
695 + 'value' => is_scalar( $item ) ? $item : json_encode( $item )
906 696 );
907 697 }
908 - WP_CLI::log( __( 'Sync Status:', 'jetpack' ) );
698 +
909 699 WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) );
910 700 break;
911 - case 'settings':
912 - WP_CLI::log( __( 'Sync Settings:', 'jetpack' ) );
913 - $settings = array();
914 - foreach ( Settings::get_settings() as $setting => $item ) {
915 - $settings[] = array(
916 - 'setting' => $setting,
917 - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
918 - );
919 - }
920 - WP_CLI\Utils\format_items( 'table', $settings, array( 'setting', 'value' ) );
921 - break;
922 - case 'disable':
923 - // Don't set it via the Settings since that also resets the queues.
924 - update_option( 'jetpack_sync_settings_disable', 1 );
925 - /* translators: %s is the site URL */
926 - WP_CLI::log( sprintf( __( 'Sync Disabled on %s', 'jetpack' ), get_site_url() ) );
927 - break;
928 - case 'enable':
929 - Settings::update_settings( array( 'disable' => 0 ) );
930 - /* translators: %s is the site URL */
931 - WP_CLI::log( sprintf( __( 'Sync Enabled on %s', 'jetpack' ), get_site_url() ) );
932 - break;
933 - case 'reset':
934 - // Don't set it via the Settings since that also resets the queues.
935 - update_option( 'jetpack_sync_settings_disable', 1 );
936 -
937 - /* translators: %s is the site URL */
938 - WP_CLI::log( sprintf( __( 'Sync Disabled on %s. Use `wp jetpack sync enable` to enable syncing again.', 'jetpack' ), get_site_url() ) );
939 - $listener = Listener::get_instance();
940 - if ( empty( $assoc_args['queue'] ) ) {
941 - $listener->get_sync_queue()->reset();
942 - $listener->get_full_sync_queue()->reset();
943 - /* translators: %s is the site URL */
944 - WP_CLI::log( sprintf( __( 'Reset Full Sync and Regular Queues Queue on %s', 'jetpack' ), get_site_url() ) );
945 - break;
946 - }
947 -
948 - if ( ! empty( $assoc_args['queue'] ) ) {
949 - switch ( $assoc_args['queue'] ) {
950 - case 'regular':
951 - $listener->get_sync_queue()->reset();
952 - /* translators: %s is the site URL */
953 - WP_CLI::log( sprintf( __( 'Reset Regular Sync Queue on %s', 'jetpack' ), get_site_url() ) );
954 - break;
955 - case 'full':
956 - $listener->get_full_sync_queue()->reset();
957 - /* translators: %s is the site URL */
958 - WP_CLI::log( sprintf( __( 'Reset Full Sync Queue on %s', 'jetpack' ), get_site_url() ) );
959 - break;
960 - default:
961 - WP_CLI::error( __( 'Please specify what type of queue do you want to reset: `full` or `regular`.', 'jetpack' ) );
962 - break;
963 - }
964 - }
965 -
966 - break;
967 701 case 'start':
968 - if ( ! Actions::sync_allowed() ) {
969 - if ( Settings::get_setting( 'disable' ) ) {
970 - 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 - return;
972 - }
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 - }
979 - }
702 + // Get the original settings so that we can restore them later
703 + $original_settings = Jetpack_Sync_Settings::get_settings();
980 704
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' ) );
985 - return;
986 - }
987 - if ( $status->is_staging_site() ) {
988 - WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in staging mode.', 'jetpack' ) );
989 - return;
990 - }
991 - }
992 - // Get the original settings so that we can restore them later.
993 - $original_settings = Settings::get_settings();
994 -
995 - // Initialize sync settigns so we can sync as quickly as possible.
705 + // Initialize sync settigns so we can sync as quickly as possible
996 706 $sync_settings = wp_parse_args(
997 - array_intersect_key( $assoc_args, Settings::$valid_settings ),
707 + array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ),
998 708 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,
709 + 'sync_wait_time' => 0,
710 + 'enqueue_wait_time' => 0,
711 + 'queue_max_writes_sec' => 10000,
712 + 'max_queue_size_full_sync' => 100000
1004 713 )
1005 714 );
1006 - Settings::update_settings( $sync_settings );
715 + Jetpack_Sync_Settings::update_settings( $sync_settings );
1007 716
1008 - // Convert comma-delimited string of modules to an array.
717 + // Convert comma-delimited string of modules to an array
1009 718 if ( ! empty( $assoc_args['modules'] ) ) {
1010 719 $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) );
1011 720
1012 721 // 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.
722 + // that we want to sync the module
1014 723 $modules = array_map( '__return_true', array_flip( $modules ) );
1015 724 }
1016 725
1017 726 foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
@@ -1019,12 +728,12 @@
1019 728 'users' === $module_name &&
1020 729 isset( $assoc_args[ $module_name ] ) &&
1021 730 'initial' === $assoc_args[ $module_name ]
1022 731 ) {
1023 - $modules['users'] = 'initial';
732 + $modules[ 'users' ] = 'initial';
1024 733 } elseif ( isset( $assoc_args[ $module_name ] ) ) {
1025 734 $ids = explode( ',', $assoc_args[ $module_name ] );
1026 - if ( $ids !== array() ) {
735 + if ( count( $ids ) > 0 ) {
1027 736 $modules[ $module_name ] = $ids;
1028 737 }
1029 738 }
1030 739 }
@@ -1032,13 +741,12 @@
1032 741 if ( empty( $modules ) ) {
1033 742 $modules = null;
1034 743 }
1035 744
1036 - // Kick off a full sync.
1037 - if ( Actions::do_full_sync( $modules ) ) {
745 + // Kick off a full sync
746 + if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) {
1038 747 if ( $modules ) {
1039 - /* 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 ) ) ) );
748 + WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) );
1041 749 } else {
1042 750 WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) );
1043 751 }
1044 752 } else {
@@ -1043,45 +751,38 @@
1043 751 }
1044 752 } else {
1045 753
1046 754 // Reset sync settings to original.
1047 - Settings::update_settings( $original_settings );
755 + Jetpack_Sync_Settings::update_settings( $original_settings );
1048 756
1049 757 if ( $modules ) {
1050 - /* 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 ) ) );
758 + WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) );
1052 759 } else {
1053 760 WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) );
1054 761 }
1055 762 }
1056 763
1057 - // Keep sending to WPCOM until there's nothing to send.
764 + // Keep sending to WPCOM until there's nothing to send
1058 765 $i = 1;
1059 766 do {
1060 - $result = Actions::$sender->do_full_sync();
767 + $result = Jetpack_Sync_Actions::$sender->do_full_sync();
1061 768 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 ) ) ) {
1064 - /* translators: %s is an error code */
769 + $queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() );
770 + if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) {
1065 771 WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) );
1066 772 }
1067 773 } else {
1068 - if ( 1 === $i ) {
774 + if ( 1 == $i ) {
1069 775 WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) );
1070 776 } else {
1071 777 WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) );
1072 778 }
1073 -
1074 - // Immediate Full Sync does not wait for WP.com to process data so we need to enforce a wait.
1075 - if ( str_contains( get_class( Modules::get_module( 'full-sync' ) ), 'Full_Sync_Immediately' ) ) {
1076 - sleep( 15 );
1077 - }
1078 779 }
1079 - ++$i;
780 + $i++;
1080 781 } while ( $result && ! is_wp_error( $result ) );
1081 782
1082 783 // Reset sync settings to original.
1083 - Settings::update_settings( $original_settings );
784 + Jetpack_Sync_Settings::update_settings( $original_settings );
1084 785
1085 786 WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) );
1086 787 break;
1087 788 }
@@ -1098,42 +799,40 @@
1098 799 *
1099 800 * wp jetpack sync_queue full_sync peek
1100 801 *
1101 802 * @synopsis <incremental|full_sync> <peek>
1102 - *
1103 - * @param array $args Positional args.
1104 803 */
1105 - public function sync_queue( $args ) {
1106 - if ( ! Actions::sync_allowed() ) {
804 + public function sync_queue( $args, $assoc_args ) {
805 + if ( ! Jetpack_Sync_Actions::sync_allowed() ) {
1107 806 WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) );
1108 807 }
1109 808
1110 809 $queue_name = isset( $args[0] ) ? $args[0] : 'sync';
1111 - $action = isset( $args[1] ) ? $args[1] : 'peek';
810 + $action = isset( $args[1] ) ? $args[1] : 'peek';
1112 811
1113 812 // We map the queue name that way we can support more friendly queue names in the commands, but still use
1114 813 // the queue name that the code expects.
1115 - $allowed_queues = array(
814 + $queue_name_map = $allowed_queues = array(
1116 815 'incremental' => 'sync',
1117 816 'full' => 'full_sync',
1118 817 );
1119 - $queue_name_map = $allowed_queues;
1120 818 $mapped_queue_name = isset( $queue_name_map[ $queue_name ] ) ? $queue_name_map[ $queue_name ] : $queue_name;
1121 819
1122 - switch ( $action ) {
820 + switch( $action ) {
1123 821 case 'peek':
1124 - $queue = new Queue( $mapped_queue_name );
822 + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
823 + $queue = new Jetpack_Sync_Queue( $mapped_queue_name );
1125 824 $items = $queue->peek( 100 );
1126 825
1127 826 if ( empty( $items ) ) {
1128 827 /* 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 ) );
828 + WP_CLI::log( sprintf( __( 'Nothing is in the queue: %s', 'jetpack' ), $queue_name ) );
1130 829 } else {
1131 830 $collection = array();
1132 831 foreach ( $items as $item ) {
1133 832 $collection[] = array(
1134 833 'action' => $item[0],
1135 - 'args' => wp_json_encode( $item[1] ),
834 + 'args' => json_encode( $item[1] ),
1136 835 'current_user_id' => $item[2],
1137 836 'microtime' => $item[3],
1138 837 'importing' => (string) $item[4],
1139 838 );
@@ -1163,20 +862,15 @@
1163 862 * : JSON blob of WPCOM API token
1164 863 * [--partner_tracking_id=<partner_tracking_id>]
1165 864 * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com
1166 865 *
1167 - * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>]
1168 - *
1169 - * @param array $args Positional args.
1170 - * @param array $named_args Named args.
866 + * * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>]
1171 867 */
1172 868 public function partner_cancel( $args, $named_args ) {
1173 869 list( $token_json ) = $args;
1174 870
1175 - $token = $token_json ? json_decode( $token_json ) : null;
1176 - if ( ! $token ) {
1177 - /* 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 ) ) );
871 + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
872 + $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1179 873 }
1180 874
1181 875 if ( isset( $token->error ) ) {
1182 876 $this->partner_provision_error( new WP_Error( $token->error, $token->message ) );
@@ -1185,27 +879,24 @@
1185 879 if ( ! isset( $token->access_token ) ) {
1186 880 $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) );
1187 881 }
1188 882
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 - );
883 + if ( Jetpack::validate_sync_error_idc_option() ) {
884 + $this->partner_provision_error( new WP_Error(
885 + 'site_in_safe_mode',
886 + esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
887 + ) );
1196 888 }
1197 889
1198 890 $site_identifier = Jetpack_Options::get_option( 'id' );
1199 891
1200 892 if ( ! $site_identifier ) {
1201 - $status = new Status();
1202 - $site_identifier = $status->get_site_suffix();
893 + $site_identifier = Jetpack::build_raw_urls( get_home_url() );
1203 894 }
1204 895
1205 896 $request = array(
1206 897 'headers' => array(
1207 - 'Authorization' => 'Bearer ' . $token->access_token,
898 + 'Authorization' => "Bearer " . $token->access_token,
1208 899 'Host' => 'public-api.wordpress.com',
1209 900 ),
1210 901 'timeout' => 60,
1211 902 'method' => 'POST',
@@ -1210,14 +901,14 @@
1210 901 'timeout' => 60,
1211 902 'method' => 'POST',
1212 903 );
1213 904
1214 - $url = sprintf( '%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier );
905 + $url = sprintf( 'https://%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier );
1215 906 if ( ! empty( $named_args ) && ! empty( $named_args['partner_tracking_id'] ) ) {
1216 907 $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
1217 908 }
1218 909
1219 - $result = Client::_wp_remote_request( $url, $request );
910 + $result = Jetpack_Client::_wp_remote_request( $url, $request );
1220 911
1221 912 Jetpack_Options::delete_option( 'onboarding' );
1222 913
1223 914 if ( is_wp_error( $result ) ) {
@@ -1260,19 +951,14 @@
1260 951 * $ wp jetpack partner_provision '{ some: "json" }' premium 1
1261 952 * { success: true }
1262 953 *
1263 954 * @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 955 */
1268 956 public function partner_provision( $args, $named_args ) {
1269 957 list( $token_json ) = $args;
1270 958
1271 - $token = $token_json ? json_decode( $token_json ) : null;
1272 - if ( ! $token ) {
1273 - /* 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 ) ) );
959 + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
960 + $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1275 961 }
1276 962
1277 963 if ( isset( $token->error ) ) {
1278 964 $message = isset( $token->message )
@@ -1289,21 +975,17 @@
1289 975
1290 976 $body_json = Jetpack_Provision::partner_provision( $token->access_token, $named_args );
1291 977
1292 978 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 - );
979 + error_log( json_encode( array(
980 + 'success' => false,
981 + 'error_code' => $body_json->get_error_code(),
982 + 'error_message' => $body_json->get_error_message()
983 + ) ) );
1302 984 exit( 1 );
1303 985 }
1304 986
1305 - WP_CLI::log( wp_json_encode( $body_json ) );
987 + WP_CLI::log( json_encode( $body_json ) );
1306 988 }
1307 989
1308 990 /**
1309 991 * Manages your Jetpack sitemap
@@ -1318,14 +1000,11 @@
1318 1000 * wp jetpack sitemap rebuild
1319 1001 *
1320 1002 * @subcommand sitemap
1321 1003 * @synopsis <rebuild> [--purge]
1322 - *
1323 - * @param array $args Positional args.
1324 - * @param array $assoc_args Named args.
1325 1004 */
1326 1005 public function sitemap( $args, $assoc_args ) {
1327 - if ( ! Jetpack::is_connection_ready() ) {
1006 + if ( ! Jetpack::is_active() ) {
1328 1007 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1329 1008 }
1330 1009 if ( ! Jetpack::is_module_active( 'sitemaps' ) ) {
1331 1010 WP_CLI::error( __( 'Jetpack Sitemaps module is not currently active. Activate it first if you want to work with sitemaps.', 'jetpack' ) );
@@ -1350,11 +1029,8 @@
1350 1029 *
1351 1030 * wp jetpack authorize_user --token=123456789abcdef
1352 1031 *
1353 1032 * @synopsis --token=<value>
1354 - *
1355 - * @param array $args Positional args.
1356 - * @param array $named_args Named args.
1357 1033 */
1358 1034 public function authorize_user( $args, $named_args ) {
1359 1035 if ( ! is_user_logged_in() ) {
1360 1036 WP_CLI::error( __( 'Please select a user to authorize via the --user global argument.', 'jetpack' ) );
@@ -1363,16 +1039,16 @@
1363 1039 if ( empty( $named_args['token'] ) ) {
1364 1040 WP_CLI::error( __( 'A non-empty token argument must be passed.', 'jetpack' ) );
1365 1041 }
1366 1042
1367 - $is_connection_owner = ! Jetpack::connection()->has_connected_owner();
1368 - $current_user_id = get_current_user_id();
1043 + $is_master_user = ! Jetpack::is_active();
1044 + $current_user_id = get_current_user_id();
1369 1045
1370 - ( new Tokens() )->update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_connection_owner );
1046 + Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_master_user );
1371 1047
1372 1048 WP_CLI::log( wp_json_encode( $named_args ) );
1373 1049
1374 - if ( $is_connection_owner ) {
1050 + if ( $is_master_user ) {
1375 1051 /**
1376 1052 * Auto-enable SSO module for new Jetpack Start connections
1377 1053 *
1378 1054 * @since 5.0.0
@@ -1420,14 +1096,11 @@
1420 1096 *
1421 1097 * ## EXAMPLES
1422 1098 *
1423 1099 * wp jetpack call_api --resource='/sites/%d'
1424 - *
1425 - * @param array $args Positional args.
1426 - * @param array $named_args Named args.
1427 1100 */
1428 1101 public function call_api( $args, $named_args ) {
1429 - if ( ! Jetpack::is_connection_ready() ) {
1102 + if ( ! Jetpack::is_active() ) {
1430 1103 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1431 1104 }
1432 1105
1433 1106 $consumed_args = array(
@@ -1441,44 +1114,40 @@
1441 1114 // Get args that should be passed to resource.
1442 1115 $other_args = array_diff_key( $named_args, array_flip( $consumed_args ) );
1443 1116
1444 1117 $decoded_body = ! empty( $named_args['body'] )
1445 - ? json_decode( $named_args['body'], true )
1118 + ? json_decode( $named_args['body'] )
1446 1119 : false;
1447 1120
1448 - $resource_url = ( ! str_contains( $named_args['resource'], '%d' ) )
1121 + $resource_url = ( false === strpos( $named_args['resource'], '%d' ) )
1449 1122 ? $named_args['resource']
1450 1123 : sprintf( $named_args['resource'], Jetpack_Options::get_option( 'id' ) );
1451 1124
1452 - $response = Client::wpcom_json_api_request_as_blog(
1125 + $response = Jetpack_Client::wpcom_json_api_request_as_blog(
1453 1126 $resource_url,
1454 - empty( $named_args['api_version'] ) ? Client::WPCOM_JSON_API_VERSION : $named_args['api_version'],
1127 + empty( $named_args['api_version'] ) ? Jetpack_Client::WPCOM_JSON_API_VERSION : $named_args['api_version'],
1455 1128 $other_args,
1456 1129 empty( $decoded_body ) ? null : $decoded_body,
1457 - empty( $named_args['base_api_path'] ) ? 'rest' : $named_args['base_api_path']
1130 + $named_args['base_api_path']
1458 1131 );
1459 1132
1460 1133 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 - );
1134 + WP_CLI::error( sprintf(
1135 + /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an error code, %3$s is an error message. */
1136 + __( 'Request to %1$s returned an error: (%2$d) %3$s.', 'jetpack' ),
1137 + $resource_url,
1138 + $response->get_error_code(),
1139 + $response->get_error_message()
1140 + ) );
1470 1141 }
1471 1142
1472 1143 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 - );
1144 + WP_CLI::error( sprintf(
1145 + /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an HTTP status code. */
1146 + __( 'Request to %1$s returned a non-200 response code: %2$d.', 'jetpack' ),
1147 + $resource_url,
1148 + wp_remote_retrieve_response_code( $response )
1149 + ) );
1481 1150 }
1482 1151
1483 1152 $output = wp_remote_retrieve_body( $response );
1484 1153 if ( isset( $named_args['pretty'] ) ) {
@@ -1496,93 +1165,8 @@
1496 1165 WP_CLI::success( $output );
1497 1166 }
1498 1167
1499 1168 /**
1500 - * Allows uploading SSH Credentials to the current site for backups, restores, and security scanning.
1501 - *
1502 - * ## OPTIONS
1503 - *
1504 - * [--host=<host>]
1505 - * : The SSH server's address.
1506 - *
1507 - * [--ssh-user=<user>]
1508 - * : The username to use to log in to the SSH server.
1509 - *
1510 - * [--pass=<pass>]
1511 - * : The password used to log in, if using a password. (optional)
1512 - *
1513 - * [--kpri=<kpri>]
1514 - * : The private key used to log in, if using a private key. (optional)
1515 - *
1516 - * [--pretty]
1517 - * : Will pretty print the results of a successful API call. (optional)
1518 - *
1519 - * [--strip-success]
1520 - * : Will remove the green success label from successful API calls. (optional)
1521 - *
1522 - * ## EXAMPLES
1523 - *
1524 - * wp jetpack upload_ssh_creds --host=example.com --ssh-user=example --pass=password
1525 - * 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 - */
1530 - public function upload_ssh_creds( $args, $named_args ) {
1531 - if ( ! Jetpack::is_connection_ready() ) {
1532 - WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1533 - }
1534 -
1535 - $required_args = array(
1536 - 'host',
1537 - 'ssh-user',
1538 - );
1539 -
1540 - foreach ( $required_args as $arg ) {
1541 - if ( empty( $named_args[ $arg ] ) ) {
1542 - WP_CLI::error(
1543 - sprintf(
1544 - /* translators: %s is a slug, such as 'host'. */
1545 - __( '`%s` cannot be empty.', 'jetpack' ),
1546 - $arg
1547 - )
1548 - );
1549 - }
1550 - }
1551 -
1552 - if ( empty( $named_args['pass'] ) && empty( $named_args['kpri'] ) ) {
1553 - WP_CLI::error( __( 'Both `pass` and `kpri` fields cannot be blank.', 'jetpack' ) );
1554 - }
1555 -
1556 - $values = array(
1557 - 'credentials' => array(
1558 - 'site_url' => get_site_url(),
1559 - 'abspath' => ABSPATH,
1560 - 'protocol' => 'ssh',
1561 - 'port' => 22,
1562 - 'role' => 'main',
1563 - 'host' => $named_args['host'],
1564 - 'user' => $named_args['ssh-user'],
1565 - 'pass' => empty( $named_args['pass'] ) ? '' : $named_args['pass'],
1566 - 'kpri' => empty( $named_args['kpri'] ) ? '' : $named_args['kpri'],
1567 - ),
1568 - );
1569 -
1570 - $named_args = wp_parse_args(
1571 - array(
1572 - 'resource' => '/activity-log/%d/update-credentials',
1573 - 'method' => 'POST',
1574 - 'api_version' => '1.1',
1575 - 'body' => wp_json_encode( $values ),
1576 - 'timeout' => 30,
1577 - ),
1578 - $named_args
1579 - );
1580 -
1581 - self::call_api( $args, $named_args );
1582 - }
1583 -
1584 - /**
1585 1169 * API wrapper for getting stats from the WordPress.com API for the current site.
1586 1170 *
1587 1171 * ## OPTIONS
1588 1172 *
@@ -1614,21 +1198,16 @@
1614 1198 *
1615 1199 * ## EXAMPLES
1616 1200 *
1617 1201 * wp jetpack get_stats
1618 - *
1619 - * @param array $args Positional args.
1620 - * @param array $named_args Named args.
1621 1202 */
1622 1203 public function get_stats( $args, $named_args ) {
1623 1204 $selected_args = array_intersect_key(
1624 1205 $named_args,
1625 - array_flip(
1626 - array(
1627 - 'quantity',
1628 - 'date',
1629 - )
1630 - )
1206 + array_flip( array(
1207 + 'quantity',
1208 + 'date',
1209 + ) )
1631 1210 );
1632 1211
1633 1212 // The API expects unit, but period seems to be more correct.
1634 1213 $selected_args['unit'] = $named_args['period'];
@@ -1689,15 +1268,15 @@
1689 1268 * # List all publicize connections.
1690 1269 * $ wp jetpack publicize list
1691 1270 *
1692 1271 * # List publicize connections for a given service.
1693 - * $ wp jetpack publicize list linkedin
1272 + * $ wp jetpack publicize list twitter
1694 1273 *
1695 1274 * # List all publicize connections for a given user.
1696 1275 * $ wp --user=1 jetpack publicize list
1697 1276 *
1698 1277 * # List all publicize connections for a given user and service.
1699 - * $ wp --user=1 jetpack publicize list linkedin
1278 + * $ wp --user=1 jetpack publicize list twitter
1700 1279 *
1701 1280 * # Display details for a given connection.
1702 1281 * $ wp jetpack publicize list 123456
1703 1282 *
@@ -1707,37 +1286,33 @@
1707 1286 * # Disconnect all connections.
1708 1287 * $ wp jetpack publicize disconnect all
1709 1288 *
1710 1289 * # 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.
1290 + * $ wp jetpack publicize disconnect twitter
1715 1291 */
1716 1292 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' ) );
1293 + if ( ! Jetpack::is_active() ) {
1294 + WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1719 1295 }
1720 1296
1721 1297 if ( ! Jetpack::is_module_active( 'publicize' ) ) {
1722 - WP_CLI::error( __( 'The Jetpack Social module is not active.', 'jetpack' ) );
1298 + WP_CLI::error( __( 'The publicize module is not active.', 'jetpack' ) );
1723 1299 }
1724 1300
1725 - if ( ( new Status() )->is_offline_mode() ) {
1301 + if ( Jetpack::is_development_mode() ) {
1726 1302 if (
1727 1303 ! defined( 'JETPACK_DEV_DEBUG' ) &&
1728 1304 ! has_filter( 'jetpack_development_mode' ) &&
1729 - ! has_filter( 'jetpack_offline_mode' ) &&
1730 - ! str_contains( site_url(), '.' )
1305 + false === strpos( site_url(), '.' )
1731 1306 ) {
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' ) );
1307 + 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 1308 }
1734 1309
1735 - WP_CLI::error( __( 'Jetpack is currently in offline mode, so the Jetpack Social module will not load.', 'jetpack' ) );
1310 + WP_CLI::error( __( 'Jetpack is currently in development mode, so the publicize module will not load.', 'jetpack' ) );
1736 1311 }
1737 1312
1738 1313 if ( ! class_exists( 'Publicize' ) ) {
1739 - WP_CLI::error( __( 'The Jetpack Social module is not loaded.', 'jetpack' ) );
1314 + WP_CLI::error( __( 'The publicize module is not loaded.', 'jetpack' ) );
1740 1315 }
1741 1316
1742 1317 $action = $args[0];
1743 1318 $publicize = new Publicize();
@@ -1820,12 +1395,12 @@
1820 1395 // If the connection ID is 'all' then delete all connections. If the connection ID
1821 1396 // matches a service, delete all connections for that service.
1822 1397 if ( 'all' === $identifier || $id_is_service ) {
1823 1398 if ( 'all' === $identifier ) {
1824 - WP_CLI::log( __( "You're about to delete all Jetpack Social connections.", 'jetpack' ) );
1399 + WP_CLI::log( __( "You're about to delete all publicize connections.", 'jetpack' ) );
1825 1400 } else {
1826 1401 /* 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 ) );
1402 + WP_CLI::log( sprintf( __( "You're about to delete all publicize connections to %s.", 'jetpack' ), $identifier ) );
1828 1403 }
1829 1404
1830 1405 jetpack_cli_are_you_sure();
1831 1406
@@ -1846,9 +1421,9 @@
1846 1421 $connections = $option_connections[ $service ];
1847 1422 }
1848 1423
1849 1424 if ( ! empty( $connections ) ) {
1850 - $count = is_countable( $connections ) ? count( $connections ) : 0;
1425 + $count = count( $connections );
1851 1426 $progress = \WP_CLI\Utils\make_progress_bar(
1852 1427 /* translators: %s is a lowercase string for a social network. */
1853 1428 sprintf( __( 'Disconnecting all connections to %s.', 'jetpack' ), $service ),
1854 1429 $count
@@ -1855,15 +1430,13 @@
1855 1430 );
1856 1431
1857 1432 foreach ( $connections as $id => $connection ) {
1858 1433 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 - );
1434 + WP_CLI::error( sprintf(
1435 + /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */
1436 + __( 'Publicize connection %d could not be disconnected', 'jetpack' ),
1437 + $id
1438 + ) );
1866 1439 }
1867 1440
1868 1441 $progress->tick();
1869 1442 }
@@ -1870,294 +1443,55 @@
1870 1443
1871 1444 $progress->finish();
1872 1445
1873 1446 if ( 'all' === $service ) {
1874 - WP_CLI::success( __( 'All Jetpack Social connections were successfully disconnected.', 'jetpack' ) );
1447 + WP_CLI::success( __( 'All publicize connections were successfully disconnected.', 'jetpack' ) );
1875 1448 } else {
1876 1449 /* 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 );
1450 + WP_CLI::success( __( 'All publicize connections to %s were successfully disconnected.', 'jetpack' ), $service );
1878 1451 }
1879 1452 }
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 1453 } 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 ) );
1454 + if ( false !== $publicize->disconnect( false, $identifier ) ) {
1455 + /* translators: %d is a numeric ID. Example: 1234. */
1456 + WP_CLI::success( sprintf( __( 'Publicize connection %d has been disconnected.', 'jetpack' ), $identifier ) );
1457 + } else {
1458 + /* translators: %d is a numeric ID. Example: 1234. */
1459 + WP_CLI::error( sprintf( __( 'Publicize connection %d could not be disconnected.', 'jetpack' ), $identifier ) );
1460 + }
1886 1461 }
1887 1462 break; // disconnect.
1888 1463 }
1889 1464 }
1890 1465
1891 - /**
1892 - * Get the API host.
1893 - *
1894 - * @return string URL.
1895 - */
1896 1466 private function get_api_host() {
1897 1467 $env_api_host = getenv( 'JETPACK_START_API_HOST', true );
1898 - return $env_api_host ? 'https://' . $env_api_host : JETPACK__WPCOM_JSON_API_BASE;
1468 + return $env_api_host ? $env_api_host : JETPACK__WPCOM_JSON_API_HOST;
1899 1469 }
1900 1470
1901 - /**
1902 - * Log and exit on a partner provision error.
1903 - *
1904 - * @param WP_Error $error Error.
1905 - */
1906 1471 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 - );
1472 + WP_CLI::log( json_encode( array(
1473 + 'success' => false,
1474 + 'error_code' => $error->get_error_code(),
1475 + 'error_message' => $error->get_error_message()
1476 + ) ) );
1916 1477 exit( 1 );
1917 1478 }
1918 -
1919 - /**
1920 - * Creates the essential files in Jetpack to start building a Gutenberg block or plugin.
1921 - *
1922 - * ## TYPES
1923 - *
1924 - * block: it creates a Jetpack block. All files will be created in a directory under extensions/blocks named based on the block title or a specific given slug.
1925 - *
1926 - * ## BLOCK TYPE OPTIONS
1927 - *
1928 - * The first parameter is the block title and it's not associative. Add it wrapped in quotes.
1929 - * 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 - * --slug: Specific slug to identify the block that overrides the one generated based on the title.
1931 - * --description: Allows to provide a text description of the block.
1932 - * --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 - *
1935 - * ## BLOCK TYPE EXAMPLES
1936 - *
1937 - * wp jetpack scaffold block "Cool Block"
1938 - * wp jetpack scaffold block "Amazing Rock" --slug="good-music" --description="Rock the best music on your site"
1939 - * wp jetpack scaffold block "Jukebox" --keywords="music, audio, media"
1940 - * wp jetpack scaffold block "Jukebox" --variation="experimental"
1941 - *
1942 - * @subcommand scaffold block
1943 - * @synopsis <type> <title> [--slug] [--description] [--keywords] [--variation]
1944 - *
1945 - * @param array $args Positional parameters, when strings are passed, wrap them in quotes.
1946 - * @param array $assoc_args Associative parameters like --slug="nice-block".
1947 - */
1948 - public function scaffold( $args, $assoc_args ) {
1949 - // It's ok not to check if it's set, because otherwise WPCLI exits earlier.
1950 - switch ( $args[0] ) {
1951 - case 'block':
1952 - $this->block( $args, $assoc_args );
1953 - break;
1954 - default:
1955 - /* translators: %s is the subcommand */
1956 - WP_CLI::error( sprintf( esc_html__( 'Invalid subcommand %s.', 'jetpack' ), $args[0] ) . ' 👻' );
1957 - exit( 1 );
1958 - }
1959 - }
1960 -
1961 - /**
1962 - * Creates the essential files in Jetpack to build a Gutenberg block.
1963 - *
1964 - * @param array $args Positional parameters. Only one is used, that corresponds to the block title.
1965 - * @param array $assoc_args Associative parameters defined in the scaffold() method.
1966 - */
1967 - public function block( $args, $assoc_args ) {
1968 - if ( isset( $args[1] ) ) {
1969 - $title = ucwords( $args[1] );
1970 - } else {
1971 - WP_CLI::error( esc_html__( 'The title parameter is required.', 'jetpack' ) . ' 👻' );
1972 - exit( 1 );
1973 - }
1974 -
1975 - $slug = isset( $assoc_args['slug'] )
1976 - ? $assoc_args['slug']
1977 - : sanitize_title( $title );
1978 -
1979 - $next_version = "\x24\x24next-version$$"; // Escapes to hide the string from tools/replace-next-version-tag.sh
1980 -
1981 - $variation_options = array( 'production', 'experimental', 'beta' );
1982 - $variation = ( isset( $assoc_args['variation'] ) && in_array( $assoc_args['variation'], $variation_options, true ) )
1983 - ? $assoc_args['variation']
1984 - : 'beta';
1985 -
1986 - if ( preg_match( '#^jetpack/#', $slug ) ) {
1987 - $slug = preg_replace( '#^jetpack/#', '', $slug );
1988 - }
1989 -
1990 - if ( ! preg_match( '/^[a-z][a-z0-9\-]*$/', $slug ) ) {
1991 - WP_CLI::error( esc_html__( 'Invalid block slug. They can contain only lowercase alphanumeric characters or dashes, and start with a letter', 'jetpack' ) . ' 👻' );
1992 - }
1993 -
1994 - global $wp_filesystem;
1995 - if ( ! WP_Filesystem() ) {
1996 - WP_CLI::error( esc_html__( "Can't write files", 'jetpack' ) . ' 😱' );
1997 - }
1998 -
1999 - $path = JETPACK__PLUGIN_DIR . "extensions/blocks/$slug";
2000 -
2001 - if ( $wp_filesystem->exists( $path ) && $wp_filesystem->is_dir( $path ) ) {
2002 - /* translators: %s is path to the conflicting block */
2003 - WP_CLI::error( sprintf( esc_html__( 'Name conflicts with the existing block %s', 'jetpack' ), $path ) . ' ⛔️' );
2004 - exit( 1 );
2005 - }
2006 -
2007 - $wp_filesystem->mkdir( $path );
2008 -
2009 - $keywords = isset( $assoc_args['keywords'] )
2010 - ? array_map(
2011 - function ( $keyword ) {
2012 - return trim( $keyword );
2013 - },
2014 - array_slice( explode( ',', $assoc_args['keywords'] ), 0, 3 )
2015 - )
2016 - : array();
2017 -
2018 - $files = array(
2019 - "$path/block.json" => self::render_block_file(
2020 - 'block-block-json',
2021 - array(
2022 - 'slug' => $slug,
2023 - 'title' => wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2024 - 'description' => isset( $assoc_args['description'] )
2025 - ? wp_json_encode( $assoc_args['description'], JSON_UNESCAPED_UNICODE )
2026 - : wp_json_encode( $title, JSON_UNESCAPED_UNICODE ),
2027 - 'nextVersion' => $next_version,
2028 - 'keywords' => wp_json_encode( $keywords, JSON_UNESCAPED_UNICODE ),
2029 - )
2030 - ),
2031 - "$path/$slug.php" => self::render_block_file(
2032 - 'block-register-php',
2033 - array(
2034 - 'nextVersion' => $next_version,
2035 - 'title' => $title,
2036 - 'underscoredTitle' => str_replace( ' ', '_', $title ),
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 - );
2055 -
2056 - $files_written = array();
2057 -
2058 - foreach ( $files as $filename => $contents ) {
2059 - if ( $wp_filesystem->put_contents( $filename, $contents ) ) {
2060 - $files_written[] = $filename;
2061 - } else {
2062 - /* translators: %s is a file name */
2063 - WP_CLI::error( sprintf( esc_html__( 'Error creating %s', 'jetpack' ), $filename ) );
2064 - }
2065 - }
2066 -
2067 - if ( empty( $files_written ) ) {
2068 - WP_CLI::log( esc_html__( 'No files were created', 'jetpack' ) );
2069 - } else {
2070 - // Load index.json and insert the slug of the new block in its block variation array.
2071 - $block_list_path = JETPACK__PLUGIN_DIR . 'extensions/index.json';
2072 - $block_list = $wp_filesystem->get_contents( $block_list_path );
2073 - if ( empty( $block_list ) ) {
2074 - /* translators: %s is the path to the file with the block list */
2075 - WP_CLI::error( sprintf( esc_html__( 'Error fetching contents of %s', 'jetpack' ), $block_list_path ) );
2076 - } elseif ( false === stripos( $block_list, $slug ) ) {
2077 - $new_block_list = json_decode( $block_list );
2078 - $new_block_list->{ $variation }[] = $slug;
2079 -
2080 - // Format the JSON to match our coding standards.
2081 - $new_block_list_formatted = wp_json_encode( $new_block_list, JSON_PRETTY_PRINT ) . "\n";
2082 - $new_block_list_formatted = preg_replace_callback(
2083 - // Find all occurrences of multiples of 4 spaces a the start of the line.
2084 - '/^((?: )+)/m',
2085 - function ( $matches ) {
2086 - // Replace each occurrence of 4 spaces with a tab character.
2087 - return str_repeat( "\t", substr_count( $matches[0], ' ' ) );
2088 - },
2089 - $new_block_list_formatted
2090 - );
2091 -
2092 - if ( ! $wp_filesystem->put_contents( $block_list_path, $new_block_list_formatted ) ) {
2093 - /* translators: %s is the path to the file with the block list */
2094 - WP_CLI::error( sprintf( esc_html__( 'Error writing new %s', 'jetpack' ), $block_list_path ) );
2095 - }
2096 - }
2097 -
2098 - if ( 'beta' === $variation || 'experimental' === $variation ) {
2099 - $block_constant = sprintf(
2100 - /* translators: the placeholder is a constant name */
2101 - esc_html__( 'To load the block, add the constant JETPACK_BLOCKS_VARIATION set to %1$s to your wp-config.php file', 'jetpack' ),
2102 - $variation
2103 - );
2104 - } else {
2105 - $block_constant = '';
2106 - }
2107 -
2108 - WP_CLI::success(
2109 - sprintf(
2110 - /* translators: the placeholders are a human readable title, and a series of words separated by dashes */
2111 - esc_html__( 'Successfully created block %1$s with slug %2$s', 'jetpack' ) . ' 🎉' . "\n" .
2112 - "--------------------------------------------------------------------------------------------------------------------\n" .
2113 - /* translators: the placeholder is a directory path */
2114 - esc_html__( 'The files were created at %3$s', 'jetpack' ) . "\n" .
2115 - esc_html__( 'To start using the block, build the blocks with pnpm run build-extensions', 'jetpack' ) . "\n" .
2116 - /* translators: the placeholder is a file path */
2117 - esc_html__( 'The block slug has been added to the %4$s list at %5$s', 'jetpack' ) . "\n" .
2118 - '%6$s' . "\n" .
2119 - /* translators: the placeholder is a URL */
2120 - "\n" . esc_html__( 'Read more at %7$s', 'jetpack' ) . "\n",
2121 - $title,
2122 - $slug,
2123 - $path,
2124 - $variation,
2125 - $block_list_path,
2126 - $block_constant,
2127 - 'https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#developing-block-editor-extensions-in-jetpack'
2128 - ) . '--------------------------------------------------------------------------------------------------------------------'
2129 - );
2130 - }
2131 - }
2132 -
2133 - /**
2134 - * Built the file replacing the placeholders in the template with the data supplied.
2135 - *
2136 - * @param string $template Template.
2137 - * @param array $data Data.
2138 - * @return string mixed
2139 - */
2140 - private static function render_block_file( $template, $data = array() ) {
2141 - return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data );
2142 - }
2143 1479 }
2144 1480
2145 -// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.
2146 -
2147 -/**
1481 +/*
2148 1482 * Standard "ask for permission to continue" function.
2149 1483 * If action cancelled, ask if they need help.
2150 1484 *
2151 1485 * Written outside of the class so it's not listed as an executable command w/ 'wp jetpack'
2152 1486 *
2153 - * @param bool $flagged false = normal option | true = flagged by get_jetpack_options_for_reset().
2154 - * @param string $error_msg Error message.
1487 + * @param $flagged bool false = normal option | true = flagged by get_jetpack_options_for_reset()
1488 + * @param $error_msg string (optional)
2155 1489 */
2156 1490 function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) {
2157 1491 $cli = new Jetpack_CLI();
2158 1492
2159 - // Default cancellation message.
1493 + // Default cancellation message
2160 1494 if ( ! $error_msg ) {
2161 1495 $error_msg =
2162 1496 __( 'Action cancelled. Have a question?', 'jetpack' )
2163 1497 . ' '
@@ -2162,20 +1496,20 @@
2162 1496 __( 'Action cancelled. Have a question?', 'jetpack' )
2163 1497 . ' '
2164 1498 . $cli->green_open
2165 1499 . 'jetpack.com/support'
2166 - . $cli->color_close;
1500 + . $cli->color_close;
2167 1501 }
2168 1502
2169 1503 if ( ! $flagged ) {
2170 1504 $prompt_message = _x( 'Are you sure? This cannot be undone. Type "yes" to continue:', '"yes" is a command - do not translate.', 'jetpack' );
2171 1505 } else {
2172 - $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' );
1506 + $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' );
2173 1507 }
2174 1508
2175 1509 WP_CLI::line( $prompt_message );
2176 - $handle = fopen( 'php://stdin', 'r' );
2177 - $line = fgets( $handle );
2178 - if ( 'yes' !== trim( $line ) ) {
1510 + $handle = fopen( "php://stdin", "r" );
1511 + $line = fgets( $handle );
1512 + if ( 'yes' != trim( $line ) ){
2179 1513 WP_CLI::error( $error_msg );
2180 1514 }
2181 1515 }