PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.4
Jetpack – WP Security, Backup, Speed, & Growth v6.4
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 +314 -1037 12.5.26.4 View file →
@@ -1,28 +1,6 @@
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.
@@ -27,34 +5,12 @@
27 5 /**
28 6 * Control your local Jetpack installation.
29 7 */
30 8 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 - */
9 + // Aesthetics
10 + public $green_open = "\033[32m";
11 + public $red_open = "\033[31m";
50 12 public $yellow_open = "\033[33m";
51 -
52 - /**
53 - * Console escape code to reset coloring.
54 - *
55 - * @var string
56 - */
57 13 public $color_close = "\033[0m";
58 14
59 15 /**
60 16 * Get Jetpack Details
@@ -69,16 +25,18 @@
69 25 *
70 26 * wp jetpack status
71 27 * wp jetpack status full
72 28 *
73 - * @param array $args Positional args.
74 29 */
75 - public function status( $args ) {
76 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
30 + public function status( $args, $assoc_args ) {
31 + require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-debugger.php' );
77 32
78 - /* translators: %s is the site URL */
79 33 WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) );
80 34
35 + if ( ! Jetpack::is_active() ) {
36 + WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
37 + }
38 +
81 39 if ( isset( $args[0] ) && 'full' !== $args[0] ) {
82 40 /* translators: %s is a command like "prompt" */
83 41 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) );
84 42 }
@@ -84,31 +42,20 @@
84 42 }
85 43
86 44 $master_user_email = Jetpack::get_master_user_email();
87 45
88 - $cxntests = new Jetpack_Cxn_Tests();
46 + $jetpack_self_test = Jetpack_Debugger::run_self_test(); // Performs the same tests as jetpack.com/support/debug/
89 47
90 - if ( $cxntests->pass() ) {
91 - $cxntests->output_results_for_cli();
92 -
48 + if ( ! $jetpack_self_test || ! wp_remote_retrieve_response_code( $jetpack_self_test ) ) {
49 + WP_CLI::error( __( 'Jetpack connection status unknown.', 'jetpack' ) );
50 + } else if ( 200 == wp_remote_retrieve_response_code( $jetpack_self_test ) ) {
93 51 WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) );
94 52 } else {
95 - $error = array();
96 - foreach ( $cxntests->list_fails() as $fail ) {
97 - $error[] = $fail['name'] . ( empty( $fail['message'] ) ? '' : ': ' . $fail['message'] );
98 - }
99 - WP_CLI::error_multi_line( $error );
100 -
101 - $cxntests->output_results_for_cli();
102 -
103 - WP_CLI::error( __( 'One or more tests did not pass. Please investigate!', 'jetpack' ) ); // Exit CLI.
53 + WP_CLI::error( __( 'Jetpack connection is broken.', 'jetpack' ) );
104 54 }
105 55
106 - /* translators: %s is current version of Jetpack, for example 7.3 */
107 56 WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) );
108 - /* translators: %d is WP.com ID of this blog */
109 57 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 58 WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) );
112 59
113 60 /*
114 61 * Are they asking for all data?
@@ -114,41 +61,41 @@
114 61 * Are they asking for all data?
115 62 *
116 63 * Loop through heartbeat data and organize by priority.
117 64 */
118 - $all_data = ( isset( $args[0] ) && 'full' === $args[0] ) ? 'full' : false;
65 + $all_data = ( isset( $args[0] ) && 'full' == $args[0] ) ? 'full' : false;
119 66 if ( $all_data ) {
120 - // Heartbeat data.
67 + // Heartbeat data
121 68 WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) );
122 69
123 70 // Get the filtered heartbeat data.
124 - // Filtered so we can color/list by severity.
71 + // Filtered so we can color/list by severity
125 72 $stats = Jetpack::jetpack_check_heartbeat_data();
126 73
127 - // Display red flags first.
74 + // Display red flags first
128 75 foreach ( $stats['bad'] as $stat => $value ) {
129 - WP_CLI::line( sprintf( "$this->red_open%-'.16s %s $this->color_close", $stat, $value ) );
76 + printf( "$this->red_open%-'.16s %s $this->color_close\n", $stat, $value );
130 77 }
131 78
132 - // Display caution warnings next.
79 + // Display caution warnings next
133 80 foreach ( $stats['caution'] as $stat => $value ) {
134 - WP_CLI::line( sprintf( "$this->yellow_open%-'.16s %s $this->color_close", $stat, $value ) );
81 + printf( "$this->yellow_open%-'.16s %s $this->color_close\n", $stat, $value );
135 82 }
136 83
137 84 // The rest of the results are good!
138 85 foreach ( $stats['good'] as $stat => $value ) {
139 86
140 - // Modules should get special spacing for aestetics.
87 + // Modules should get special spacing for aestetics
141 88 if ( strpos( $stat, 'odule-' ) ) {
142 - WP_CLI::line( sprintf( "%-'.30s %s", $stat, $value ) );
143 - usleep( 4000 ); // For dramatic effect lolz.
89 + printf( "%-'.30s %s\n", $stat, $value );
90 + usleep( 4000 ); // For dramatic effect lolz
144 91 continue;
145 92 }
146 - WP_CLI::line( sprintf( "%-'.16s %s", $stat, $value ) );
147 - usleep( 4000 ); // For dramatic effect lolz.
93 + printf( "%-'.16s %s\n", $stat, $value );
94 + usleep( 4000 ); // For dramatic effect lolz
148 95 }
149 96 } else {
150 - // Just the basics.
97 + // Just the basics
151 98 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 99 }
153 100 }
154 101
@@ -162,20 +109,19 @@
162 109 * wp jetpack test-connection
163 110 *
164 111 * @subcommand test-connection
165 112 */
166 - public function test_connection() {
113 + public function test_connection( $args, $assoc_args ) {
167 114
168 - /* translators: %s is the site URL */
169 115 WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) );
170 116
171 - if ( ! Jetpack::is_connection_ready() ) {
117 + if ( ! Jetpack::is_active() ) {
172 118 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
173 119 }
174 120
175 - $response = Client::wpcom_json_api_request_as_blog(
121 + $response = Jetpack_Client::wpcom_json_api_request_as_blog(
176 122 sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
177 - Client::WPCOM_JSON_API_VERSION
123 + Jetpack_Client::WPCOM_JSON_API_VERSION
178 124 );
179 125
180 126 if ( is_wp_error( $response ) ) {
181 127 /* translators: %1$s is the error code, %2$s is the error message */
@@ -186,11 +132,11 @@
186 132 if ( ! $body ) {
187 133 WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) );
188 134 }
189 135
190 - $result = json_decode( $body );
136 + $result = json_decode( $body );
191 137 $is_connected = (bool) $result->connected;
192 - $message = $result->message;
138 + $message = $result->message;
193 139
194 140 if ( $is_connected ) {
195 141 WP_CLI::success( $message );
196 142 } else {
@@ -206,52 +152,46 @@
206 152 * blog: Disconnect the entire blog.
207 153 *
208 154 * user <user_identifier>: Disconnect a specific user from WordPress.com.
209 155 *
210 - * [--force]
211 - * If the user ID provided is the connection owner, it will only be disconnected if --force is passed
156 + * Please note, the primary account that the blog is connected
157 + * to WordPress.com with cannot be disconnected without
158 + * disconnecting the entire blog.
212 159 *
213 160 * ## EXAMPLES
214 161 *
215 162 * wp jetpack disconnect blog
216 163 * wp jetpack disconnect user 13
217 - * wp jetpack disconnect user 1 --force
218 164 * wp jetpack disconnect user username
219 165 * wp jetpack disconnect user email@domain.com
220 166 *
221 - * @synopsis <blog|user> [<user_identifier>] [--force]
222 - *
223 - * @param array $args Positional args.
224 - * @param array $assoc_args Named args.
167 + * @synopsis <blog|user> [<user_identifier>]
225 168 */
226 169 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;
170 + if ( ! Jetpack::is_active() ) {
171 + WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) );
231 172 }
232 173
233 174 $action = isset( $args[0] ) ? $args[0] : 'prompt';
234 - if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ), true ) ) {
175 + if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) {
235 176 /* translators: %s is a command like "prompt" */
236 177 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
237 178 }
238 179
239 - if ( in_array( $action, array( 'user' ), true ) ) {
180 + if ( in_array( $action, array( 'user' ) ) ) {
240 181 if ( isset( $args[1] ) ) {
241 182 $user_id = $args[1];
242 183 if ( ctype_digit( $user_id ) ) {
243 - $field = 'id';
184 + $field = 'id';
244 185 $user_id = (int) $user_id;
245 186 } elseif ( is_email( $user_id ) ) {
246 - $field = 'email';
187 + $field = 'email';
247 188 $user_id = sanitize_user( $user_id, true );
248 189 } else {
249 - $field = 'login';
190 + $field = 'login';
250 191 $user_id = sanitize_user( $user_id, true );
251 192 }
252 - $user = get_user_by( $field, $user_id );
253 - if ( ! $user ) {
193 + if ( ! $user = get_user_by( $field, $user_id ) ) {
254 194 WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) );
255 195 }
256 196 } else {
257 197 WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) );
@@ -257,40 +197,24 @@
257 197 WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) );
258 198 }
259 199 }
260 200
261 - $force_user_disconnect = ! empty( $assoc_args['force'] );
262 -
263 201 switch ( $action ) {
264 202 case 'blog':
265 203 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 - );
204 + Jetpack::disconnect();
205 + WP_CLI::success( sprintf(
206 + __( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
207 + esc_url( get_site_url() )
208 + ) );
274 209 break;
275 210 case 'user':
276 - $connection_manager = new Connection_Manager( 'jetpack' );
277 - $disconnected = $connection_manager->disconnect_user( $user->ID, $force_user_disconnect );
278 - if ( $disconnected ) {
211 + if ( Jetpack::unlink_user( $user->ID ) ) {
279 212 Jetpack::log( 'unlink', $user->ID );
280 213 WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) );
281 214 } 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 );
215 + /* translators: %s is a username */
216 + WP_CLI::error( sprintf( __( "User %s could not be disconnected. Are you sure they're connected currently?", 'jetpack' ), "{$user->login} <{$user->email}>" ) );
293 217 }
294 218 break;
295 219 case 'prompt':
296 220 WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) );
@@ -315,191 +239,71 @@
315 239 * ## EXAMPLES
316 240 *
317 241 * wp jetpack reset options
318 242 * wp jetpack reset modules
319 - * wp jetpack reset sync-checksum --dry-run --offset=0
320 243 *
321 - * @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>]
322 - *
323 - * @param array $args Positional args.
324 - * @param array $assoc_args Named args.
244 + * @synopsis <modules|options>
325 245 */
326 246 public function reset( $args, $assoc_args ) {
327 247 $action = isset( $args[0] ) ? $args[0] : 'prompt';
328 - if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) {
248 + if ( ! in_array( $action, array( 'options', 'modules' ) ) ) {
329 249 /* translators: %s is a command like "prompt" */
330 250 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
331 251 }
332 252
333 - $is_dry_run = ! empty( $assoc_args['dry-run'] );
253 + // Are you sure?
254 + jetpack_cli_are_you_sure();
334 255
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 256 switch ( $action ) {
347 257 case 'options':
348 258 $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.
259 +
260 + // Reset the Jetpack options
261 + WP_CLI::line( sprintf(
262 + __( "Resetting Jetpack Options for %s...\n", "jetpack" ),
263 + esc_url( get_site_url() )
264 + ) );
265 + sleep(1); // Take a breath
358 266 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 -
267 + Jetpack_Options::delete_option( $option_to_reset );
268 + usleep( 100000 );
364 269 /* translators: This is the result of an action. The option named %s was reset */
365 270 WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
366 271 }
367 272
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.
273 + // Reset the WP options
274 + WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", "jetpack" ) );
275 + usleep( 500000 ); // Take a breath
371 276 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 - }
277 + delete_option( $option_to_reset );
278 + usleep( 100000 );
376 279 /* translators: This is the result of an action. The option named %s was reset */
377 280 WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
378 281 }
379 282
380 - // Reset to default modules.
381 - WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) );
382 - usleep( 500000 ); // Take a breath.
283 + // Reset to default modules
284 + WP_CLI::line( __( "Resetting default modules...\n", "jetpack" ) );
285 + usleep( 500000 ); // Take a breath
383 286 $default_modules = Jetpack::get_default_modules();
384 - if ( ! $is_dry_run ) {
385 - Jetpack::update_active_modules( $default_modules );
386 - }
287 + Jetpack::update_active_modules( $default_modules );
387 288 WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
289 +
290 + // Jumpstart option is special
291 + Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
292 + WP_CLI::success( __( 'jumpstart option reset', 'jetpack' ) );
388 293 break;
389 294 case 'modules':
390 - if ( ! $is_dry_run ) {
391 - $default_modules = Jetpack::get_default_modules();
392 - Jetpack::update_active_modules( $default_modules );
393 - }
394 -
295 + $default_modules = Jetpack::get_default_modules();
296 + Jetpack::update_active_modules( $default_modules );
395 297 WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
396 298 break;
397 299 case 'prompt':
398 - WP_CLI::error( __( 'Please specify if you would like to reset your options, modules or sync-checksum', 'jetpack' ) );
300 + WP_CLI::error( __( 'Please specify if you would like to reset your options, or modules', 'jetpack' ) );
399 301 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 302 }
480 303 }
481 304
482 305 /**
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 306 * Manage Jetpack Modules
503 307 *
504 308 * ## OPTIONS
505 309 *
@@ -538,15 +342,11 @@
538 342 * wp jetpack module deactivate stats
539 343 * wp jetpack module toggle stats
540 344 * wp jetpack module activate all
541 345 * wp jetpack module deactivate all
542 - *
543 - * @param array $args Positional args.
544 - * @param array $assoc_args Named args.
545 346 */
546 347 public function module( $args, $assoc_args ) {
547 - $module_slug = null;
548 - $action = isset( $args[0] ) ? $args[0] : 'list';
348 + $action = isset( $args[0] ) ? $args[0] : 'list';
549 349
550 350 if ( isset( $args[1] ) ) {
551 351 $module_slug = $args[1];
552 352 if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) {
@@ -583,18 +383,16 @@
583 383 ? __( 'Active', 'jetpack' )
584 384 : __( 'Inactive', 'jetpack' ),
585 385 );
586 386 }
587 - WP_CLI\Utils\format_items( $assoc_args['format'], $modules_list, array( 'slug', 'status' ) );
387 + WP_CLI\Utils\format_items( $assoc_args['format'], $modules_list, array( 'slug', 'status' ) ); // phpcs:ignore PHPCompatibility
588 388 break;
589 389 case 'activate':
590 390 $module = Jetpack::get_module( $module_slug );
591 391 Jetpack::log( 'activate', $module_slug );
592 392 if ( Jetpack::activate_module( $module_slug, false, false ) ) {
593 - /* translators: %s is the name of a Jetpack module */
594 393 WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) );
595 394 } else {
596 - /* translators: %s is the name of a Jetpack module */
597 395 WP_CLI::error( sprintf( __( '%s could not be activated.', 'jetpack' ), $module['name'] ) );
598 396 }
599 397 break;
600 398 case 'activate_all':
@@ -605,9 +403,8 @@
605 403 case 'deactivate':
606 404 $module = Jetpack::get_module( $module_slug );
607 405 Jetpack::log( 'deactivate', $module_slug );
608 406 Jetpack::deactivate_module( $module_slug );
609 - /* translators: %s is the name of a Jetpack module */
610 407 WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) );
611 408 break;
612 409 case 'deactivate_all':
613 410 Jetpack::delete_active_modules();
@@ -623,122 +420,119 @@
623 420 * Manage Protect Settings
624 421 *
625 422 * ## OPTIONS
626 423 *
627 - * allow: Add an IP address to an always allow list. You can also read or clear the allow list.
424 + * whitelist: Whitelist an IP address. You can also read or clear the whitelist.
628 425 *
629 426 *
630 427 * ## EXAMPLES
631 428 *
632 - * wp jetpack protect allow <ip address>
633 - * wp jetpack protect allow list
634 - * wp jetpack protect allow clear
429 + * wp jetpack protect whitelist <ip address>
430 + * wp jetpack protect whitelist list
431 + * wp jetpack protect whitelist clear
635 432 *
636 - * @synopsis <allow> [<ip|ip_low-ip_high|list|clear>]
637 - *
638 - * @param array $args Positional args.
433 + * @synopsis <whitelist> [<ip|ip_low-ip_high|list|clear>]
639 434 */
640 - public function protect( $args ) {
435 + public function protect( $args, $assoc_args ) {
641 436 $action = isset( $args[0] ) ? $args[0] : 'prompt';
642 - if ( ! in_array( $action, array( 'whitelist', 'allow' ), true ) ) { // Still allow "whitelist" for legacy support.
437 + if ( ! in_array( $action, array( 'whitelist' ) ) ) {
643 438 /* translators: %s is a command like "prompt" */
644 439 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
645 440 }
646 - // Check if module is active.
441 + // Check if module is active
647 442 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__ ) );
443 + 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 444 }
651 - if ( in_array( $action, array( 'allow', 'whitelist' ), true ) ) {
445 + if ( in_array( $action, array( 'whitelist' ) ) ) {
652 446 if ( isset( $args[1] ) ) {
653 - $action = 'allow';
447 + $action = 'whitelist';
654 448 } else {
655 449 $action = 'prompt';
656 450 }
657 451 }
658 452 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.
453 + case 'whitelist':
454 + $whitelist = array();
455 + $new_ip = $args[1];
456 + $current_whitelist = get_site_option( 'jetpack_protect_whitelist', array() );
663 457
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 ) {
458 + // Build array of IPs that are already whitelisted.
459 + // Re-build manually instead of using jetpack_protect_format_whitelist() so we can easily get
460 + // low & high range params for jetpack_protect_ip_address_is_in_range();
461 + foreach( $current_whitelist as $whitelisted ) {
668 462
669 - // IP ranges.
670 - if ( $allowed->range ) {
463 + // IP ranges
464 + if ( $whitelisted->range ) {
671 465
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 ) ) {
466 + // Is it already whitelisted?
467 + if ( jetpack_protect_ip_address_is_in_range( $new_ip, $whitelisted->range_low, $whitelisted->range_high ) ) {
674 468 /* translators: %s is an IP address */
675 - WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
469 + WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) );
676 470 break;
677 471 }
678 - $allow[] = $allowed->range_low . ' - ' . $allowed->range_high;
472 + $whitelist[] = $whitelisted->range_low . " - " . $whitelisted->range_high;
679 473
680 - } else { // Individual IPs.
474 + } else { // Individual IPs
681 475
682 - // Check if the IP is already on the allow list (single IP only).
683 - if ( $new_ip === $allowed->ip_address ) {
476 + // Check if the IP is already whitelisted (single IP only)
477 + if ( $new_ip == $whitelisted->ip_address ) {
684 478 /* translators: %s is an IP address */
685 - WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
479 + WP_CLI::error( sprintf( __( '%s has already been whitelisted', 'jetpack' ), $new_ip ) );
686 480 break;
687 481 }
688 - $allow[] = $allowed->ip_address;
482 + $whitelist[] = $whitelisted->ip_address;
689 483
690 484 }
691 485 }
692 486
693 487 /*
694 - * List the allowed IPs.
695 - * Done here because it's easier to read the $allow array after it's been rebuilt.
488 + * List the whitelist
489 + * Done here because it's easier to read the $whitelist array after it's been rebuilt
696 490 */
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 ) );
491 + if ( isset( $args[1] ) && 'list' == $args[1] ) {
492 + if ( ! empty( $whitelist ) ) {
493 + WP_CLI::success( __( 'Here are your whitelisted IPs:', 'jetpack' ) );
494 + foreach ( $whitelist as $ip ) {
495 + WP_CLI::line( "\t" . str_pad( $ip, 24 ) ) ;
702 496 }
703 497 } else {
704 - WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
498 + WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
705 499 }
706 500 break;
707 501 }
708 502
709 503 /*
710 - * Clear the always allow list.
504 + * Clear the whitelist
711 505 */
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' ) );
506 + if ( isset( $args[1] ) && 'clear' == $args[1] ) {
507 + if ( ! empty( $whitelist ) ) {
508 + $whitelist = array();
509 + jetpack_protect_save_whitelist( $whitelist );
510 + WP_CLI::success( __( 'Cleared all whitelisted IPs', 'jetpack' ) );
717 511 } else {
718 - WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
512 + WP_CLI::line( __( 'Whitelist is empty.', "jetpack" ) ) ;
719 513 }
720 514 break;
721 515 }
722 516
723 - // Append new IP to allow array.
724 - array_push( $allow, $new_ip );
517 + // Append new IP to whitelist array
518 + array_push( $whitelist, $new_ip );
725 519
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.
520 + // Save whitelist if there are no errors
521 + $result = jetpack_protect_save_whitelist( $whitelist );
728 522 if ( is_wp_error( $result ) ) {
729 - WP_CLI::error( $result );
523 + WP_CLI::error( __( $result, 'jetpack' ) );
730 524 }
731 525
732 526 /* translators: %s is an IP address */
733 - WP_CLI::success( sprintf( __( '%s has been added to the always allowed list.', 'jetpack' ), $new_ip ) );
527 + WP_CLI::success( sprintf( __( '%s has been whitelisted.', 'jetpack' ), $new_ip ) );
734 528 break;
735 529 case 'prompt':
736 530 WP_CLI::error(
737 531 __( '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"
532 + __( 'Please enter the IP address you want to whitelist.', 'jetpack' ) . "\n" .
533 + _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" .
534 + _x( "You can also 'list' or 'clear' the whitelist.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n"
741 535 );
742 536 break;
743 537 }
744 538 }
@@ -762,29 +556,30 @@
762 556 * wp jetpack options delete <option_name>
763 557 * wp jetpack options update <option_name> [<option_value>]
764 558 *
765 559 * @synopsis <list|get|delete|update> [<option_name>] [<option_value>]
766 - *
767 - * @param array $args Positional args.
768 560 */
769 - public function options( $args ) {
770 - $action = isset( $args[0] ) ? $args[0] : 'list';
561 + public function options( $args, $assoc_args ) {
562 + $action = isset( $args[0] ) ? $args[0] : 'list';
771 563 $safe_to_modify = Jetpack_Options::get_options_for_reset();
772 564
565 + // Jumpstart is special
566 + array_push( $safe_to_modify, 'jumpstart' );
567 +
773 568 // Is the option flagged as unsafe?
774 - $flagged = ! in_array( $args[1], $safe_to_modify, true );
569 + $flagged = ! in_array( $args[1], $safe_to_modify );
775 570
776 - if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ), true ) ) {
571 + if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ) ) ) {
777 572 /* translators: %s is a command like "prompt" */
778 573 WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
779 574 }
780 575
781 576 if ( isset( $args[0] ) ) {
782 - if ( 'get' === $args[0] && isset( $args[1] ) ) {
577 + if ( 'get' == $args[0] && isset( $args[1] ) ) {
783 578 $action = 'get';
784 - } elseif ( 'delete' === $args[0] && isset( $args[1] ) ) {
579 + } else if ( 'delete' == $args[0] && isset( $args[1] ) ) {
785 580 $action = 'delete';
786 - } elseif ( 'update' === $args[0] && isset( $args[1] ) ) {
581 + } else if ( 'update' == $args[0] && isset( $args[1] ) ) {
787 582 $action = 'update';
788 583 } else {
789 584 $action = 'list';
790 585 }
@@ -789,18 +584,17 @@
789 584 $action = 'list';
790 585 }
791 586 }
792 587
793 - // Bail if the option isn't found.
588 + // Bail if the option isn't found
794 589 $option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false;
795 590 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' ) );
591 + WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) );
797 592 }
798 593
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;
594 + // Let's print_r the option if it's an array
595 + // Used in the 'get' and 'list' actions
596 + $option = is_array( $option ) ? print_r( $option ) : $option;
803 597
804 598 switch ( $action ) {
805 599 case 'get':
806 600 WP_CLI::success( "\t" . $option );
@@ -808,9 +602,8 @@
808 602 case 'delete':
809 603 jetpack_cli_are_you_sure( $flagged );
810 604
811 605 Jetpack_Options::delete_option( $args[1] );
812 - /* translators: %s is the option name */
813 606 WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) );
814 607 break;
815 608 case 'update':
816 609 jetpack_cli_are_you_sure( $flagged );
@@ -821,10 +614,9 @@
821 614 WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) );
822 615 }
823 616
824 617 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] ) );
618 + WP_CLI::success( sprintf( _x( 'Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) );
827 619 break;
828 620 case 'list':
829 621 $options_compact = Jetpack_Options::get_option_names();
830 622 $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' );
@@ -830,14 +622,14 @@
830 622 $options_non_compact = Jetpack_Options::get_option_names( 'non_compact' );
831 623 $options_private = Jetpack_Options::get_option_names( 'private' );
832 624 $options = array_merge( $options_compact, $options_non_compact, $options_private );
833 625
834 - // Table headers.
626 + // Table headers
835 627 WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) );
836 628
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.
629 + // List out the options and their values
630 + // Tell them if the value is empty or not
631 + // Tell them if it's an array
840 632 foreach ( $options as $option ) {
841 633 $value = Jetpack_Options::get_option( $option );
842 634 if ( ! $value ) {
843 635 WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' );
@@ -845,9 +637,9 @@
845 637 }
846 638
847 639 if ( ! is_array( $value ) ) {
848 640 WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value );
849 - } elseif ( is_array( $value ) ) {
641 + } else if ( is_array( $value ) ) {
850 642 WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' );
851 643 }
852 644 }
853 645 $option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}';
@@ -854,11 +646,11 @@
854 646 $value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}';
855 647
856 648 WP_CLI::success(
857 649 _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" .
650 + str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" .
859 651 str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" .
860 - str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text\n" .
652 + str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text" . "\n" .
861 653 _x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n"
862 654 );
863 655 break;
864 656 }
@@ -868,150 +660,60 @@
868 660 * Get the status of or start a new Jetpack sync.
869 661 *
870 662 * ## OPTIONS
871 663 *
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
664 + * status : Print the current sync status
665 + * start : Start a full sync from this site to WordPress.com
878 666 *
879 667 * ## EXAMPLES
880 668 *
881 669 * wp jetpack sync status
882 - * wp jetpack sync settings
883 670 * 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 671 *
889 672 * @synopsis <status|start> [--<field>=<value>]
890 - *
891 - * @param array $args Positional args.
892 - * @param array $assoc_args Named args.
893 673 */
894 674 public function sync( $args, $assoc_args ) {
675 + if ( ! Jetpack_Sync_Actions::sync_allowed() ) {
676 + WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) );
677 + }
895 678
896 679 $action = isset( $args[0] ) ? $args[0] : 'status';
897 680
898 681 switch ( $action ) {
899 682 case 'status':
900 - $status = Actions::get_sync_status();
683 + $status = Jetpack_Sync_Actions::get_sync_status();
901 684 $collection = array();
902 685 foreach ( $status as $key => $item ) {
903 - $collection[] = array(
686 + $collection[] = array(
904 687 'option' => $key,
905 - 'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
688 + 'value' => is_scalar( $item ) ? $item : json_encode( $item )
906 689 );
907 690 }
908 - WP_CLI::log( __( 'Sync Status:', 'jetpack' ) );
909 - WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) );
910 - 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 691
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 -
692 + WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) ); // phpcs:ignore PHPCompatibility
966 693 break;
967 694 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 - }
695 + // Get the original settings so that we can restore them later
696 + $original_settings = Jetpack_Sync_Settings::get_settings();
980 697
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.
698 + // Initialize sync settigns so we can sync as quickly as possible
996 699 $sync_settings = wp_parse_args(
997 - array_intersect_key( $assoc_args, Settings::$valid_settings ),
700 + array_intersect_key( $assoc_args, Jetpack_Sync_Settings::$valid_settings ),
998 701 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,
702 + 'sync_wait_time' => 0,
703 + 'enqueue_wait_time' => 0,
704 + 'queue_max_writes_sec' => 10000,
705 + 'max_queue_size_full_sync' => 100000
1004 706 )
1005 707 );
1006 - Settings::update_settings( $sync_settings );
708 + Jetpack_Sync_Settings::update_settings( $sync_settings );
1007 709
1008 - // Convert comma-delimited string of modules to an array.
710 + // Convert comma-delimited string of modules to an array
1009 711 if ( ! empty( $assoc_args['modules'] ) ) {
1010 712 $modules = array_map( 'trim', explode( ',', $assoc_args['modules'] ) );
1011 713
1012 714 // 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.
715 + // that we want to sync the module
1014 716 $modules = array_map( '__return_true', array_flip( $modules ) );
1015 717 }
1016 718
1017 719 foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) {
@@ -1019,12 +721,12 @@
1019 721 'users' === $module_name &&
1020 722 isset( $assoc_args[ $module_name ] ) &&
1021 723 'initial' === $assoc_args[ $module_name ]
1022 724 ) {
1023 - $modules['users'] = 'initial';
725 + $modules[ 'users' ] = 'initial';
1024 726 } elseif ( isset( $assoc_args[ $module_name ] ) ) {
1025 727 $ids = explode( ',', $assoc_args[ $module_name ] );
1026 - if ( $ids !== array() ) {
728 + if ( count( $ids ) > 0 ) {
1027 729 $modules[ $module_name ] = $ids;
1028 730 }
1029 731 }
1030 732 }
@@ -1032,13 +734,12 @@
1032 734 if ( empty( $modules ) ) {
1033 735 $modules = null;
1034 736 }
1035 737
1036 - // Kick off a full sync.
1037 - if ( Actions::do_full_sync( $modules ) ) {
738 + // Kick off a full sync
739 + if ( Jetpack_Sync_Actions::do_full_sync( $modules ) ) {
1038 740 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 ) ) ) );
741 + WP_CLI::log( sprintf( __( 'Initialized a new full sync with modules: %s', 'jetpack' ), join( ', ', array_keys( $modules ) ) ) );
1041 742 } else {
1042 743 WP_CLI::log( __( 'Initialized a new full sync', 'jetpack' ) );
1043 744 }
1044 745 } else {
@@ -1043,45 +744,38 @@
1043 744 }
1044 745 } else {
1045 746
1046 747 // Reset sync settings to original.
1047 - Settings::update_settings( $original_settings );
748 + Jetpack_Sync_Settings::update_settings( $original_settings );
1048 749
1049 750 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 ) ) );
751 + WP_CLI::error( sprintf( __( 'Could not start a new full sync with modules: %s', 'jetpack' ), join( ', ', $modules ) ) );
1052 752 } else {
1053 753 WP_CLI::error( __( 'Could not start a new full sync', 'jetpack' ) );
1054 754 }
1055 755 }
1056 756
1057 - // Keep sending to WPCOM until there's nothing to send.
757 + // Keep sending to WPCOM until there's nothing to send
1058 758 $i = 1;
1059 759 do {
1060 - $result = Actions::$sender->do_full_sync();
760 + $result = Jetpack_Sync_Actions::$sender->do_full_sync();
1061 761 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 */
762 + $queue_empty_error = ( 'empty_queue_full_sync' == $result->get_error_code() );
763 + if ( ! $queue_empty_error || ( $queue_empty_error && ( 1 == $i ) ) ) {
1065 764 WP_CLI::error( sprintf( __( 'Sync errored with code: %s', 'jetpack' ), $result->get_error_code() ) );
1066 765 }
1067 766 } else {
1068 - if ( 1 === $i ) {
767 + if ( 1 == $i ) {
1069 768 WP_CLI::log( __( 'Sent data to WordPress.com', 'jetpack' ) );
1070 769 } else {
1071 770 WP_CLI::log( __( 'Sent more data to WordPress.com', 'jetpack' ) );
1072 771 }
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 772 }
1079 - ++$i;
773 + $i++;
1080 774 } while ( $result && ! is_wp_error( $result ) );
1081 775
1082 776 // Reset sync settings to original.
1083 - Settings::update_settings( $original_settings );
777 + Jetpack_Sync_Settings::update_settings( $original_settings );
1084 778
1085 779 WP_CLI::success( __( 'Finished syncing to WordPress.com', 'jetpack' ) );
1086 780 break;
1087 781 }
@@ -1098,48 +792,46 @@
1098 792 *
1099 793 * wp jetpack sync_queue full_sync peek
1100 794 *
1101 795 * @synopsis <incremental|full_sync> <peek>
1102 - *
1103 - * @param array $args Positional args.
1104 796 */
1105 - public function sync_queue( $args ) {
1106 - if ( ! Actions::sync_allowed() ) {
797 + public function sync_queue( $args, $assoc_args ) {
798 + if ( ! Jetpack_Sync_Actions::sync_allowed() ) {
1107 799 WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site.', 'jetpack' ) );
1108 800 }
1109 801
1110 802 $queue_name = isset( $args[0] ) ? $args[0] : 'sync';
1111 - $action = isset( $args[1] ) ? $args[1] : 'peek';
803 + $action = isset( $args[1] ) ? $args[1] : 'peek';
1112 804
1113 805 // We map the queue name that way we can support more friendly queue names in the commands, but still use
1114 806 // the queue name that the code expects.
1115 - $allowed_queues = array(
807 + $queue_name_map = $allowed_queues = array(
1116 808 'incremental' => 'sync',
1117 809 'full' => 'full_sync',
1118 810 );
1119 - $queue_name_map = $allowed_queues;
1120 811 $mapped_queue_name = isset( $queue_name_map[ $queue_name ] ) ? $queue_name_map[ $queue_name ] : $queue_name;
1121 812
1122 - switch ( $action ) {
813 + switch( $action ) {
1123 814 case 'peek':
1124 - $queue = new Queue( $mapped_queue_name );
815 + require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php';
816 + $queue = new Jetpack_Sync_Queue( $mapped_queue_name );
1125 817 $items = $queue->peek( 100 );
1126 818
1127 819 if ( empty( $items ) ) {
1128 820 /* 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 ) );
821 + WP_CLI::log( sprintf( __( 'Nothing is in the queue: %s', 'jetpack' ), $queue_name ) );
1130 822 } else {
1131 823 $collection = array();
1132 824 foreach ( $items as $item ) {
1133 825 $collection[] = array(
1134 826 'action' => $item[0],
1135 - 'args' => wp_json_encode( $item[1] ),
827 + 'args' => json_encode( $item[1] ),
1136 828 'current_user_id' => $item[2],
1137 829 'microtime' => $item[3],
1138 830 'importing' => (string) $item[4],
1139 831 );
1140 832 }
1141 - WP_CLI\Utils\format_items(
833 + WP_CLI\Utils\format_items( // phpcs:ignore PHPCompatibility
1142 834 'table',
1143 835 $collection,
1144 836 array(
1145 837 'action',
@@ -1163,20 +855,15 @@
1163 855 * : JSON blob of WPCOM API token
1164 856 * [--partner_tracking_id=<partner_tracking_id>]
1165 857 * : This is an optional ID that a host can pass to help identify a site in logs on WordPress.com
1166 858 *
1167 - * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>]
1168 - *
1169 - * @param array $args Positional args.
1170 - * @param array $named_args Named args.
859 + * * @synopsis <token_json> [--partner_tracking_id=<partner_tracking_id>]
1171 860 */
1172 861 public function partner_cancel( $args, $named_args ) {
1173 862 list( $token_json ) = $args;
1174 863
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 ) ) );
864 + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
865 + $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1179 866 }
1180 867
1181 868 if ( isset( $token->error ) ) {
1182 869 $this->partner_provision_error( new WP_Error( $token->error, $token->message ) );
@@ -1185,39 +872,36 @@
1185 872 if ( ! isset( $token->access_token ) ) {
1186 873 $this->partner_provision_error( new WP_Error( 'missing_access_token', __( 'Missing or invalid access token', 'jetpack' ) ) );
1187 874 }
1188 875
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 - );
876 + if ( Jetpack::validate_sync_error_idc_option() ) {
877 + $this->partner_provision_error( new WP_Error(
878 + 'site_in_safe_mode',
879 + esc_html__( 'Can not cancel a plan while in safe mode. See: https://jetpack.com/support/safe-mode/', 'jetpack' )
880 + ) );
1196 881 }
1197 882
1198 883 $site_identifier = Jetpack_Options::get_option( 'id' );
1199 884
1200 885 if ( ! $site_identifier ) {
1201 - $status = new Status();
1202 - $site_identifier = $status->get_site_suffix();
886 + $site_identifier = Jetpack::build_raw_urls( get_home_url() );
1203 887 }
1204 888
1205 889 $request = array(
1206 890 'headers' => array(
1207 - 'Authorization' => 'Bearer ' . $token->access_token,
1208 - 'Host' => 'public-api.wordpress.com',
891 + 'Authorization' => "Bearer " . $token->access_token,
892 + 'Host' => defined( 'JETPACK__WPCOM_JSON_API_HOST_HEADER' ) ? JETPACK__WPCOM_JSON_API_HOST_HEADER : 'public-api.wordpress.com',
1209 893 ),
1210 894 'timeout' => 60,
1211 895 'method' => 'POST',
1212 896 );
1213 897
1214 - $url = sprintf( '%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier );
898 + $url = sprintf( 'https://%s/rest/v1.3/jpphp/%s/partner-cancel', $this->get_api_host(), $site_identifier );
1215 899 if ( ! empty( $named_args ) && ! empty( $named_args['partner_tracking_id'] ) ) {
1216 900 $url = esc_url_raw( add_query_arg( 'partner_tracking_id', $named_args['partner_tracking_id'], $url ) );
1217 901 }
1218 902
1219 - $result = Client::_wp_remote_request( $url, $request );
903 + $result = Jetpack_Client::_wp_remote_request( $url, $request );
1220 904
1221 905 Jetpack_Options::delete_option( 'onboarding' );
1222 906
1223 907 if ( is_wp_error( $result ) ) {
@@ -1260,19 +944,14 @@
1260 944 * $ wp jetpack partner_provision '{ some: "json" }' premium 1
1261 945 * { success: true }
1262 946 *
1263 947 * @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 948 */
1268 949 public function partner_provision( $args, $named_args ) {
1269 950 list( $token_json ) = $args;
1270 951
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 ) ) );
952 + if ( ! $token_json || ! ( $token = json_decode( $token_json ) ) ) {
953 + $this->partner_provision_error( new WP_Error( 'missing_access_token', sprintf( __( 'Invalid token JSON: %s', 'jetpack' ), $token_json ) ) );
1275 954 }
1276 955
1277 956 if ( isset( $token->error ) ) {
1278 957 $message = isset( $token->message )
@@ -1289,21 +968,17 @@
1289 968
1290 969 $body_json = Jetpack_Provision::partner_provision( $token->access_token, $named_args );
1291 970
1292 971 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 - );
972 + error_log( json_encode( array(
973 + 'success' => false,
974 + 'error_code' => $body_json->get_error_code(),
975 + 'error_message' => $body_json->get_error_message()
976 + ) ) );
1302 977 exit( 1 );
1303 978 }
1304 979
1305 - WP_CLI::log( wp_json_encode( $body_json ) );
980 + WP_CLI::log( json_encode( $body_json ) );
1306 981 }
1307 982
1308 983 /**
1309 984 * Manages your Jetpack sitemap
@@ -1318,14 +993,11 @@
1318 993 * wp jetpack sitemap rebuild
1319 994 *
1320 995 * @subcommand sitemap
1321 996 * @synopsis <rebuild> [--purge]
1322 - *
1323 - * @param array $args Positional args.
1324 - * @param array $assoc_args Named args.
1325 997 */
1326 998 public function sitemap( $args, $assoc_args ) {
1327 - if ( ! Jetpack::is_connection_ready() ) {
999 + if ( ! Jetpack::is_active() ) {
1328 1000 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1329 1001 }
1330 1002 if ( ! Jetpack::is_module_active( 'sitemaps' ) ) {
1331 1003 WP_CLI::error( __( 'Jetpack Sitemaps module is not currently active. Activate it first if you want to work with sitemaps.', 'jetpack' ) );
@@ -1350,11 +1022,8 @@
1350 1022 *
1351 1023 * wp jetpack authorize_user --token=123456789abcdef
1352 1024 *
1353 1025 * @synopsis --token=<value>
1354 - *
1355 - * @param array $args Positional args.
1356 - * @param array $named_args Named args.
1357 1026 */
1358 1027 public function authorize_user( $args, $named_args ) {
1359 1028 if ( ! is_user_logged_in() ) {
1360 1029 WP_CLI::error( __( 'Please select a user to authorize via the --user global argument.', 'jetpack' ) );
@@ -1363,16 +1032,18 @@
1363 1032 if ( empty( $named_args['token'] ) ) {
1364 1033 WP_CLI::error( __( 'A non-empty token argument must be passed.', 'jetpack' ) );
1365 1034 }
1366 1035
1367 - $is_connection_owner = ! Jetpack::connection()->has_connected_owner();
1368 - $current_user_id = get_current_user_id();
1036 + $token = sanitize_text_field( $named_args['token'] );
1369 1037
1370 - ( new Tokens() )->update_user_token( $current_user_id, sprintf( '%s.%d', $named_args['token'], $current_user_id ), $is_connection_owner );
1038 + $is_master_user = ! Jetpack::is_active();
1039 + $current_user_id = get_current_user_id();
1371 1040
1041 + Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user );
1042 +
1372 1043 WP_CLI::log( wp_json_encode( $named_args ) );
1373 1044
1374 - if ( $is_connection_owner ) {
1045 + if ( $is_master_user ) {
1375 1046 /**
1376 1047 * Auto-enable SSO module for new Jetpack Start connections
1377 1048 *
1378 1049 * @since 5.0.0
@@ -1420,14 +1091,11 @@
1420 1091 *
1421 1092 * ## EXAMPLES
1422 1093 *
1423 1094 * wp jetpack call_api --resource='/sites/%d'
1424 - *
1425 - * @param array $args Positional args.
1426 - * @param array $named_args Named args.
1427 1095 */
1428 1096 public function call_api( $args, $named_args ) {
1429 - if ( ! Jetpack::is_connection_ready() ) {
1097 + if ( ! Jetpack::is_active() ) {
1430 1098 WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1431 1099 }
1432 1100
1433 1101 $consumed_args = array(
@@ -1441,9 +1109,9 @@
1441 1109 // Get args that should be passed to resource.
1442 1110 $other_args = array_diff_key( $named_args, array_flip( $consumed_args ) );
1443 1111
1444 1112 $decoded_body = ! empty( $named_args['body'] )
1445 - ? json_decode( $named_args['body'], true )
1113 + ? json_decode( $named_args['body'] )
1446 1114 : false;
1447 1115
1448 1116 $resource_url = ( false === strpos( $named_args['resource'], '%d' ) )
1449 1117 ? $named_args['resource']
@@ -1448,37 +1116,33 @@
1448 1116 $resource_url = ( false === strpos( $named_args['resource'], '%d' ) )
1449 1117 ? $named_args['resource']
1450 1118 : sprintf( $named_args['resource'], Jetpack_Options::get_option( 'id' ) );
1451 1119
1452 - $response = Client::wpcom_json_api_request_as_blog(
1120 + $response = Jetpack_Client::wpcom_json_api_request_as_blog(
1453 1121 $resource_url,
1454 - empty( $named_args['api_version'] ) ? Client::WPCOM_JSON_API_VERSION : $named_args['api_version'],
1122 + empty( $named_args['api_version'] ) ? Jetpack_Client::WPCOM_JSON_API_VERSION : $named_args['api_version'],
1455 1123 $other_args,
1456 1124 empty( $decoded_body ) ? null : $decoded_body,
1457 - empty( $named_args['base_api_path'] ) ? 'rest' : $named_args['base_api_path']
1125 + $named_args['base_api_path']
1458 1126 );
1459 1127
1460 1128 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 - );
1129 + WP_CLI::error( sprintf(
1130 + /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an error code, %3$s is an error message. */
1131 + __( 'Request to %1$s returned an error: (%2$d) %3$s.', 'jetpack' ),
1132 + $resource_url,
1133 + $response->get_error_code(),
1134 + $response->get_error_message()
1135 + ) );
1470 1136 }
1471 1137
1472 1138 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 - );
1139 + WP_CLI::error( sprintf(
1140 + /* translators: %1$s is an endpoint route (ex. /sites/123456), %2$d is an HTTP status code. */
1141 + __( 'Request to %1$s returned a non-200 response code: %2$d.', 'jetpack' ),
1142 + $resource_url,
1143 + wp_remote_retrieve_response_code( $response )
1144 + ) );
1481 1145 }
1482 1146
1483 1147 $output = wp_remote_retrieve_body( $response );
1484 1148 if ( isset( $named_args['pretty'] ) ) {
@@ -1483,9 +1147,9 @@
1483 1147 $output = wp_remote_retrieve_body( $response );
1484 1148 if ( isset( $named_args['pretty'] ) ) {
1485 1149 $decoded_output = json_decode( $output );
1486 1150 if ( $decoded_output ) {
1487 - $output = wp_json_encode( $decoded_output, JSON_PRETTY_PRINT );
1151 + $output = wp_json_encode( $decoded_output, JSON_PRETTY_PRINT ); // phpcs:ignore PHPCompatibility
1488 1152 }
1489 1153 }
1490 1154
1491 1155 if ( isset( $named_args['strip-success'] ) ) {
@@ -1496,93 +1160,8 @@
1496 1160 WP_CLI::success( $output );
1497 1161 }
1498 1162
1499 1163 /**
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 1164 * API wrapper for getting stats from the WordPress.com API for the current site.
1586 1165 *
1587 1166 * ## OPTIONS
1588 1167 *
@@ -1614,21 +1193,16 @@
1614 1193 *
1615 1194 * ## EXAMPLES
1616 1195 *
1617 1196 * wp jetpack get_stats
1618 - *
1619 - * @param array $args Positional args.
1620 - * @param array $named_args Named args.
1621 1197 */
1622 1198 public function get_stats( $args, $named_args ) {
1623 1199 $selected_args = array_intersect_key(
1624 1200 $named_args,
1625 - array_flip(
1626 - array(
1627 - 'quantity',
1628 - 'date',
1629 - )
1630 - )
1201 + array_flip( array(
1202 + 'quantity',
1203 + 'date',
1204 + ) )
1631 1205 );
1632 1206
1633 1207 // The API expects unit, but period seems to be more correct.
1634 1208 $selected_args['unit'] = $named_args['period'];
@@ -1654,9 +1228,9 @@
1654 1228 )
1655 1229 );
1656 1230 }
1657 1231
1658 - /**
1232 + /*
1659 1233 * Allows management of publicize connections.
1660 1234 *
1661 1235 * ## OPTIONS
1662 1236 *
@@ -1663,10 +1237,10 @@
1663 1237 * <list|disconnect>
1664 1238 * : The action to perform.
1665 1239 * ---
1666 1240 * options:
1667 - * - list
1668 - * - disconnect
1241 + * - list
1242 + * - disconnect
1669 1243 * ---
1670 1244 *
1671 1245 * [<identifier>]
1672 1246 * : The connection ID or service to perform an action on.
@@ -1675,71 +1249,32 @@
1675 1249 * : Allows overriding the output of the command when listing connections.
1676 1250 * ---
1677 1251 * default: table
1678 1252 * options:
1679 - * - table
1680 - * - json
1681 - * - csv
1682 - * - yaml
1683 - * - ids
1684 - * - count
1253 + * - table
1254 + * - json
1255 + * - csv
1256 + * - yaml
1257 + * - ids
1258 + * - count
1685 1259 * ---
1686 1260 *
1687 1261 * ## EXAMPLES
1688 1262 *
1689 - * # List all publicize connections.
1690 - * $ wp jetpack publicize list
1691 - *
1692 - * # List publicize connections for a given service.
1693 - * $ wp jetpack publicize list linkedin
1694 - *
1695 - * # List all publicize connections for a given user.
1696 - * $ wp --user=1 jetpack publicize list
1697 - *
1698 - * # List all publicize connections for a given user and service.
1699 - * $ wp --user=1 jetpack publicize list linkedin
1700 - *
1701 - * # Display details for a given connection.
1702 - * $ wp jetpack publicize list 123456
1703 - *
1704 - * # Diconnection a given connection.
1705 - * $ wp jetpack publicize disconnect 123456
1706 - *
1707 - * # Disconnect all connections.
1708 - * $ wp jetpack publicize disconnect all
1709 - *
1710 - * # 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.
1263 + * wp jetpack publicize list
1264 + * wp jetpack publicize list twitter
1265 + * wp --user=1 jetpack publicize list
1266 + * wp --user=1 jetpack publicize list twitter
1267 + * wp jetpack publicize list 123456
1268 + * wp jetpack publicize disconnect 123456
1269 + * wp jetpack publicize disconnect all
1270 + * wp jetpack publicize disconnect twitter
1715 1271 */
1716 1272 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' ) );
1273 + if ( ! Jetpack::is_active() ) {
1274 + WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
1719 1275 }
1720 1276
1721 - if ( ! Jetpack::is_module_active( 'publicize' ) ) {
1722 - WP_CLI::error( __( 'The Jetpack Social module is not active.', 'jetpack' ) );
1723 - }
1724 -
1725 - if ( ( new Status() )->is_offline_mode() ) {
1726 - if (
1727 - ! defined( 'JETPACK_DEV_DEBUG' ) &&
1728 - ! has_filter( 'jetpack_development_mode' ) &&
1729 - ! has_filter( 'jetpack_offline_mode' ) &&
1730 - false === strpos( site_url(), '.' )
1731 - ) {
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' ) );
1733 - }
1734 -
1735 - WP_CLI::error( __( 'Jetpack is currently in offline mode, so the Jetpack Social module will not load.', 'jetpack' ) );
1736 - }
1737 -
1738 - if ( ! class_exists( 'Publicize' ) ) {
1739 - WP_CLI::error( __( 'The Jetpack Social module is not loaded.', 'jetpack' ) );
1740 - }
1741 -
1742 1277 $action = $args[0];
1743 1278 $publicize = new Publicize();
1744 1279 $identifier = ! empty( $args[1] ) ? $args[1] : false;
1745 1280 $services = array_keys( $publicize->get_services() );
@@ -1777,8 +1312,12 @@
1777 1312 if ( $identifier && ! $id_is_service && ! empty( $connections_to_return ) ) {
1778 1313 $connections_to_return = wp_list_filter( $connections_to_return, array( 'id' => $identifier ) );
1779 1314 }
1780 1315
1316 + if ( empty( $connections_to_return ) ) {
1317 + return false;
1318 + }
1319 +
1781 1320 $expected_keys = array(
1782 1321 'id',
1783 1322 'service',
1784 1323 'user_id',
@@ -1791,27 +1330,9 @@
1791 1330 'type',
1792 1331 'connection_data',
1793 1332 );
1794 1333
1795 - // Somehow, a test site ended up in a state where $connections_to_return looked like:
1796 - // array( array( array( 'id' => 0, 'service' => 0 ) ) ) // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
1797 - // This caused the CLI command to error when running WP_CLI\Utils\format_items() below. So
1798 - // to minimize future issues, this nested loop will remove any connections that don't contain
1799 - // any keys that we expect.
1800 - foreach ( (array) $connections_to_return as $connection_key => $connection ) {
1801 - foreach ( $expected_keys as $expected_key ) {
1802 - if ( ! isset( $connection[ $expected_key ] ) ) {
1803 - unset( $connections_to_return[ $connection_key ] );
1804 - continue;
1805 - }
1806 - }
1807 - }
1808 -
1809 - if ( empty( $connections_to_return ) ) {
1810 - return false;
1811 - }
1812 -
1813 - WP_CLI\Utils\format_items( $named_args['format'], $connections_to_return, $expected_keys );
1334 + WP_CLI\Utils\format_items( $named_args['format'], $connections_to_return, $expected_keys ); // phpcs:ignore PHPCompatibility
1814 1335 break; // list.
1815 1336 case 'disconnect':
1816 1337 if ( ! $identifier ) {
1817 1338 WP_CLI::error( __( 'A connection ID must be passed in order to disconnect.', 'jetpack' ) );
@@ -1820,12 +1341,12 @@
1820 1341 // If the connection ID is 'all' then delete all connections. If the connection ID
1821 1342 // matches a service, delete all connections for that service.
1822 1343 if ( 'all' === $identifier || $id_is_service ) {
1823 1344 if ( 'all' === $identifier ) {
1824 - WP_CLI::log( __( "You're about to delete all Jetpack Social connections.", 'jetpack' ) );
1345 + WP_CLI::log( __( "You're about to delete all publicize connections.", 'jetpack' ) );
1825 1346 } else {
1826 1347 /* 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 ) );
1348 + WP_CLI::log( sprintf( __( "You're about to delete all publicize connections to %s.", 'jetpack' ), $identifier ) );
1828 1349 }
1829 1350
1830 1351 jetpack_cli_are_you_sure();
1831 1352
@@ -1846,10 +1367,10 @@
1846 1367 $connections = $option_connections[ $service ];
1847 1368 }
1848 1369
1849 1370 if ( ! empty( $connections ) ) {
1850 - $count = is_countable( $connections ) ? count( $connections ) : 0;
1851 - $progress = \WP_CLI\Utils\make_progress_bar(
1371 + $count = count( $connections );
1372 + $progress = \WP_CLI\Utils\make_progress_bar( // phpcs:ignore PHPCompatibility
1852 1373 /* translators: %s is a lowercase string for a social network. */
1853 1374 sprintf( __( 'Disconnecting all connections to %s.', 'jetpack' ), $service ),
1854 1375 $count
1855 1376 );
@@ -1855,15 +1376,13 @@
1855 1376 );
1856 1377
1857 1378 foreach ( $connections as $id => $connection ) {
1858 1379 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 - );
1380 + WP_CLI::error( sprintf(
1381 + /* translators: %1$d is a numeric ID and %2$s is a lowercase string for a social network. */
1382 + __( 'Publicize connection %d could not be disconnected', 'jetpack' ),
1383 + $id
1384 + ) );
1866 1385 }
1867 1386
1868 1387 $progress->tick();
1869 1388 }
@@ -1870,297 +1389,55 @@
1870 1389
1871 1390 $progress->finish();
1872 1391
1873 1392 if ( 'all' === $service ) {
1874 - WP_CLI::success( __( 'All Jetpack Social connections were successfully disconnected.', 'jetpack' ) );
1393 + WP_CLI::success( __( 'All publicize connections were successfully disconnected.', 'jetpack' ) );
1875 1394 } else {
1876 1395 /* 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 );
1396 + WP_CLI::success( __( 'All publicize connections to %s were successfully disconnected.', 'jetpack' ), $service );
1878 1397 }
1879 1398 }
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 1399 } 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 ) );
1400 + if ( false !== $publicize->disconnect( false, $identifier ) ) {
1401 + /* translators: %d is a numeric ID. Example: 1234. */
1402 + WP_CLI::success( sprintf( __( 'Publicize connection %d has been disconnected.', 'jetpack' ), $identifier ) );
1403 + } else {
1404 + /* translators: %d is a numeric ID. Example: 1234. */
1405 + WP_CLI::error( sprintf( __( 'Publicize connection %d could not be disconnected.', 'jetpack' ), $identifier ) );
1406 + }
1886 1407 }
1887 1408 break; // disconnect.
1888 1409 }
1889 1410 }
1890 1411
1891 - /**
1892 - * Get the API host.
1893 - *
1894 - * @return string URL.
1895 - */
1896 1412 private function get_api_host() {
1897 1413 $env_api_host = getenv( 'JETPACK_START_API_HOST', true );
1898 - return $env_api_host ? 'https://' . $env_api_host : JETPACK__WPCOM_JSON_API_BASE;
1414 + return $env_api_host ? $env_api_host : JETPACK__WPCOM_JSON_API_HOST;
1899 1415 }
1900 1416
1901 - /**
1902 - * Log and exit on a partner provision error.
1903 - *
1904 - * @param WP_Error $error Error.
1905 - */
1906 1417 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 - );
1418 + WP_CLI::log( json_encode( array(
1419 + 'success' => false,
1420 + 'error_code' => $error->get_error_code(),
1421 + 'error_message' => $error->get_error_message()
1422 + ) ) );
1916 1423 exit( 1 );
1917 1424 }
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 - $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 - if ( preg_match( '#^jetpack/#', $slug ) ) {
1985 - $slug = preg_replace( '#^jetpack/#', '', $slug );
1986 - }
1987 -
1988 - if ( ! preg_match( '/^[a-z][a-z0-9\-]*$/', $slug ) ) {
1989 - WP_CLI::error( esc_html__( 'Invalid block slug. They can contain only lowercase alphanumeric characters or dashes, and start with a letter', 'jetpack' ) . ' 👻' );
1990 - }
1991 -
1992 - global $wp_filesystem;
1993 - if ( ! WP_Filesystem() ) {
1994 - WP_CLI::error( esc_html__( "Can't write files", 'jetpack' ) . ' 😱' );
1995 - }
1996 -
1997 - $path = JETPACK__PLUGIN_DIR . "extensions/blocks/$slug";
1998 -
1999 - if ( $wp_filesystem->exists( $path ) && $wp_filesystem->is_dir( $path ) ) {
2000 - /* translators: %s is path to the conflicting block */
2001 - WP_CLI::error( sprintf( esc_html__( 'Name conflicts with the existing block %s', 'jetpack' ), $path ) . ' ⛔️' );
2002 - exit( 1 );
2003 - }
2004 -
2005 - $wp_filesystem->mkdir( $path );
2006 -
2007 - $has_keywords = isset( $assoc_args['keywords'] );
2008 -
2009 - $files = array(
2010 - "$path/$slug.php" => self::render_block_file(
2011 - 'block-register-php',
2012 - array(
2013 - 'nextVersion' => "\x24\x24next-version$$", // Escapes to hide the string from tools/replace-next-version-tag.sh
2014 - 'slug' => $slug,
2015 - 'title' => $title,
2016 - 'underscoredSlug' => str_replace( '-', '_', $slug ),
2017 - 'underscoredTitle' => str_replace( ' ', '_', $title ),
2018 - )
2019 - ),
2020 - "$path/index.js" => self::render_block_file(
2021 - 'block-index-js',
2022 - array(
2023 - 'slug' => $slug,
2024 - 'title' => $title,
2025 - 'description' => isset( $assoc_args['description'] )
2026 - ? $assoc_args['description']
2027 - : $title,
2028 - 'keywords' => $has_keywords
2029 - ? array_map(
2030 - function ( $keyword ) {
2031 - // Construction necessary for Mustache lists.
2032 - return array( 'keyword' => trim( $keyword ) );
2033 - },
2034 - explode( ',', $assoc_args['keywords'], 3 )
2035 - )
2036 - : '',
2037 - 'hasKeywords' => $has_keywords,
2038 - )
2039 - ),
2040 - "$path/editor.js" => self::render_block_file( 'block-editor-js' ),
2041 - "$path/editor.scss" => self::render_block_file(
2042 - 'block-editor-scss',
2043 - array(
2044 - 'slug' => $slug,
2045 - 'title' => $title,
2046 - )
2047 - ),
2048 - "$path/edit.js" => self::render_block_file(
2049 - 'block-edit-js',
2050 - array(
2051 - 'title' => $title,
2052 - 'className' => str_replace( ' ', '', ucwords( str_replace( '-', ' ', $slug ) ) ),
2053 - )
2054 - ),
2055 - "$path/icon.js" => self::render_block_file( 'block-icon-js' ),
2056 - "$path/attributes.js" => self::render_block_file( 'block-attributes-js' ),
2057 - );
2058 -
2059 - $files_written = array();
2060 -
2061 - foreach ( $files as $filename => $contents ) {
2062 - if ( $wp_filesystem->put_contents( $filename, $contents ) ) {
2063 - $files_written[] = $filename;
2064 - } else {
2065 - /* translators: %s is a file name */
2066 - WP_CLI::error( sprintf( esc_html__( 'Error creating %s', 'jetpack' ), $filename ) );
2067 - }
2068 - }
2069 -
2070 - if ( empty( $files_written ) ) {
2071 - WP_CLI::log( esc_html__( 'No files were created', 'jetpack' ) );
2072 - } else {
2073 - // Load index.json and insert the slug of the new block in its block variation array.
2074 - $block_list_path = JETPACK__PLUGIN_DIR . 'extensions/index.json';
2075 - $block_list = $wp_filesystem->get_contents( $block_list_path );
2076 - if ( empty( $block_list ) ) {
2077 - /* translators: %s is the path to the file with the block list */
2078 - WP_CLI::error( sprintf( esc_html__( 'Error fetching contents of %s', 'jetpack' ), $block_list_path ) );
2079 - } elseif ( false === stripos( $block_list, $slug ) ) {
2080 - $new_block_list = json_decode( $block_list );
2081 - $new_block_list->{ $variation }[] = $slug;
2082 -
2083 - // Format the JSON to match our coding standards.
2084 - $new_block_list_formatted = wp_json_encode( $new_block_list, JSON_PRETTY_PRINT ) . "\n";
2085 - $new_block_list_formatted = preg_replace_callback(
2086 - // Find all occurrences of multiples of 4 spaces a the start of the line.
2087 - '/^((?: )+)/m',
2088 - function ( $matches ) {
2089 - // Replace each occurrence of 4 spaces with a tab character.
2090 - return str_repeat( "\t", substr_count( $matches[0], ' ' ) );
2091 - },
2092 - $new_block_list_formatted
2093 - );
2094 -
2095 - if ( ! $wp_filesystem->put_contents( $block_list_path, $new_block_list_formatted ) ) {
2096 - /* translators: %s is the path to the file with the block list */
2097 - WP_CLI::error( sprintf( esc_html__( 'Error writing new %s', 'jetpack' ), $block_list_path ) );
2098 - }
2099 - }
2100 -
2101 - if ( 'beta' === $variation || 'experimental' === $variation ) {
2102 - $block_constant = sprintf(
2103 - /* translators: the placeholder is a constant name */
2104 - esc_html__( 'To load the block, add the constant JETPACK_BLOCKS_VARIATION set to %1$s to your wp-config.php file', 'jetpack' ),
2105 - $variation
2106 - );
2107 - } else {
2108 - $block_constant = '';
2109 - }
2110 -
2111 - WP_CLI::success(
2112 - sprintf(
2113 - /* translators: the placeholders are a human readable title, and a series of words separated by dashes */
2114 - esc_html__( 'Successfully created block %1$s with slug %2$s', 'jetpack' ) . ' 🎉' . "\n" .
2115 - "--------------------------------------------------------------------------------------------------------------------\n" .
2116 - /* translators: the placeholder is a directory path */
2117 - esc_html__( 'The files were created at %3$s', 'jetpack' ) . "\n" .
2118 - esc_html__( 'To start using the block, build the blocks with pnpm run build-extensions', 'jetpack' ) . "\n" .
2119 - /* translators: the placeholder is a file path */
2120 - esc_html__( 'The block slug has been added to the %4$s list at %5$s', 'jetpack' ) . "\n" .
2121 - '%6$s' . "\n" .
2122 - /* translators: the placeholder is a URL */
2123 - "\n" . esc_html__( 'Read more at %7$s', 'jetpack' ) . "\n",
2124 - $title,
2125 - $slug,
2126 - $path,
2127 - $variation,
2128 - $block_list_path,
2129 - $block_constant,
2130 - 'https://github.com/Automattic/jetpack/blob/trunk/projects/plugins/jetpack/extensions/README.md#developing-block-editor-extensions-in-jetpack'
2131 - ) . '--------------------------------------------------------------------------------------------------------------------'
2132 - );
2133 - }
2134 - }
2135 -
2136 - /**
2137 - * Built the file replacing the placeholders in the template with the data supplied.
2138 - *
2139 - * @param string $template Template.
2140 - * @param array $data Data.
2141 - * @return string mixed
2142 - */
2143 - private static function render_block_file( $template, $data = array() ) {
2144 - return \WP_CLI\Utils\mustache_render( JETPACK__PLUGIN_DIR . "wp-cli-templates/$template.mustache", $data );
2145 - }
2146 1425 }
2147 1426
2148 -// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move these functions to some other file.
2149 -
2150 -/**
1427 +/*
2151 1428 * Standard "ask for permission to continue" function.
2152 1429 * If action cancelled, ask if they need help.
2153 1430 *
2154 1431 * Written outside of the class so it's not listed as an executable command w/ 'wp jetpack'
2155 1432 *
2156 - * @param bool $flagged false = normal option | true = flagged by get_jetpack_options_for_reset().
2157 - * @param string $error_msg Error message.
1433 + * @param $flagged bool false = normal option | true = flagged by get_jetpack_options_for_reset()
1434 + * @param $error_msg string (optional)
2158 1435 */
2159 1436 function jetpack_cli_are_you_sure( $flagged = false, $error_msg = false ) {
2160 1437 $cli = new Jetpack_CLI();
2161 1438
2162 - // Default cancellation message.
1439 + // Default cancellation message
2163 1440 if ( ! $error_msg ) {
2164 1441 $error_msg =
2165 1442 __( 'Action cancelled. Have a question?', 'jetpack' )
2166 1443 . ' '
@@ -2165,20 +1442,20 @@
2165 1442 __( 'Action cancelled. Have a question?', 'jetpack' )
2166 1443 . ' '
2167 1444 . $cli->green_open
2168 1445 . 'jetpack.com/support'
2169 - . $cli->color_close;
1446 + . $cli->color_close;
2170 1447 }
2171 1448
2172 1449 if ( ! $flagged ) {
2173 1450 $prompt_message = _x( 'Are you sure? This cannot be undone. Type "yes" to continue:', '"yes" is a command - do not translate.', 'jetpack' );
2174 1451 } else {
2175 - $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' );
1452 + $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' );
2176 1453 }
2177 1454
2178 1455 WP_CLI::line( $prompt_message );
2179 - $handle = fopen( 'php://stdin', 'r' );
2180 - $line = fgets( $handle );
2181 - if ( 'yes' !== trim( $line ) ) {
1456 + $handle = fopen( "php://stdin", "r" );
1457 + $line = fgets( $handle );
1458 + if ( 'yes' != trim( $line ) ){
2182 1459 WP_CLI::error( $error_msg );
2183 1460 }
2184 1461 }