-
Notifications
You must be signed in to change notification settings - Fork 5
/
class.jetpack-cli.php
2182 lines (1969 loc) · 71.6 KB
/
class.jetpack-cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* WP-CLI command class.
*
* @package automattic/jetpack
*/
use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Connection\Tokens;
use Automattic\Jetpack\Identity_Crisis;
use Automattic\Jetpack\IP\Utils as IP_Utils;
use Automattic\Jetpack\Publicize\Publicize;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Sync\Actions;
use Automattic\Jetpack\Sync\Listener;
use Automattic\Jetpack\Sync\Modules;
use Automattic\Jetpack\Sync\Queue;
use Automattic\Jetpack\Sync\Settings;
use Automattic\Jetpack\Waf\Brute_Force_Protection\Brute_Force_Protection_Shared_Functions;
if ( ! class_exists( 'WP_CLI_Command' ) ) {
return;
}
// @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- https://github.com/phan/phan/issues/4763
WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' );
/**
* Control your local Jetpack installation.
*/
class Jetpack_CLI extends WP_CLI_Command {
/**
* Console escape code for green.
*
* @var string
*/
public $green_open = "\033[32m";
/**
* Console escape code for red.
*
* @var string
*/
public $red_open = "\033[31m";
/**
* Console escape code for yellow.
*
* @var string
*/
public $yellow_open = "\033[33m";
/**
* Console escape code to reset coloring.
*
* @var string
*/
public $color_close = "\033[0m";
/**
* Get Jetpack Details
*
* ## OPTIONS
*
* empty: Leave it empty for basic stats
*
* full: View full stats. It's the data from the heartbeat
*
* ## EXAMPLES
*
* wp jetpack status
* wp jetpack status full
*
* @param array $args Positional args.
*/
public function status( $args ) {
require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger.php';
/* translators: %s is the site URL */
WP_CLI::line( sprintf( __( 'Checking status for %s', 'jetpack' ), esc_url( get_home_url() ) ) );
if ( isset( $args[0] ) && 'full' !== $args[0] ) {
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $args[0] ) );
}
$master_user_email = Jetpack::get_master_user_email();
$cxntests = new Jetpack_Cxn_Tests();
if ( $cxntests->pass() ) {
$cxntests->output_results_for_cli();
WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) );
} else {
$error = array();
foreach ( $cxntests->list_fails() as $fail ) {
$error[] = $fail['name'] . ( empty( $fail['message'] ) ? '' : ': ' . $fail['message'] );
}
WP_CLI::error_multi_line( $error );
$cxntests->output_results_for_cli();
WP_CLI::error( __( 'One or more tests did not pass. Please investigate!', 'jetpack' ) ); // Exit CLI.
}
/* translators: %s is current version of Jetpack, for example 7.3 */
WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) );
/* translators: %d is WP.com ID of this blog */
WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) );
/* translators: %s is the email address of the connection owner */
WP_CLI::line( sprintf( __( 'The WordPress.com account for the primary connection is %s', 'jetpack' ), $master_user_email ) );
/*
* Are they asking for all data?
*
* Loop through heartbeat data and organize by priority.
*/
$all_data = ( isset( $args[0] ) && 'full' === $args[0] ) ? 'full' : false;
if ( $all_data ) {
// Heartbeat data.
WP_CLI::line( "\n" . __( 'Additional data: ', 'jetpack' ) );
// Get the filtered heartbeat data.
// Filtered so we can color/list by severity.
$stats = Jetpack::jetpack_check_heartbeat_data();
// Display red flags first.
foreach ( $stats['bad'] as $stat => $value ) {
WP_CLI::line( sprintf( "$this->red_open%-'.16s %s $this->color_close", $stat, $value ) );
}
// Display caution warnings next.
foreach ( $stats['caution'] as $stat => $value ) {
WP_CLI::line( sprintf( "$this->yellow_open%-'.16s %s $this->color_close", $stat, $value ) );
}
// The rest of the results are good!
foreach ( $stats['good'] as $stat => $value ) {
// Modules should get special spacing for aestetics.
if ( strpos( $stat, 'odule-' ) ) {
WP_CLI::line( sprintf( "%-'.30s %s", $stat, $value ) );
usleep( 4000 ); // For dramatic effect lolz.
continue;
}
WP_CLI::line( sprintf( "%-'.16s %s", $stat, $value ) );
usleep( 4000 ); // For dramatic effect lolz.
}
} else {
// Just the basics.
WP_CLI::line( "\n" . _x( "View full status with 'wp jetpack status full'", '"wp jetpack status full" is a command - do not translate', 'jetpack' ) );
}
}
/**
* Tests the active connection
*
* Does a two-way test to verify that the local site can communicate with remote Jetpack/WP.com servers and that Jetpack/WP.com servers can talk to the local site.
*
* ## EXAMPLES
*
* wp jetpack test-connection
*
* @subcommand test-connection
*/
public function test_connection() {
/* translators: %s is the site URL */
WP_CLI::line( sprintf( __( 'Testing connection for %s', 'jetpack' ), esc_url( get_site_url() ) ) );
if ( ! Jetpack::is_connection_ready() ) {
WP_CLI::error( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
}
$response = Client::wpcom_json_api_request_as_blog(
sprintf( '/jetpack-blogs/%d/test-connection', Jetpack_Options::get_option( 'id' ) ),
Client::WPCOM_JSON_API_VERSION
);
if ( is_wp_error( $response ) ) {
/* translators: %1$s is the error code, %2$s is the error message */
WP_CLI::error( sprintf( __( 'Failed to test connection (#%1$s: %2$s)', 'jetpack' ), $response->get_error_code(), $response->get_error_message() ) );
}
$body = wp_remote_retrieve_body( $response );
if ( ! $body ) {
WP_CLI::error( __( 'Failed to test connection (empty response body)', 'jetpack' ) );
}
$result = json_decode( $body );
$is_connected = (bool) $result->connected;
$message = $result->message;
if ( $is_connected ) {
WP_CLI::success( $message );
} else {
WP_CLI::error( $message );
}
}
/**
* Disconnect Jetpack Blogs or Users
*
* ## OPTIONS
*
* blog: Disconnect the entire blog.
*
* user <user_identifier>: Disconnect a specific user from WordPress.com.
*
* [--force]
* If the user ID provided is the connection owner, it will only be disconnected if --force is passed
*
* ## EXAMPLES
*
* wp jetpack disconnect blog
* wp jetpack disconnect user 13
* wp jetpack disconnect user 1 --force
* wp jetpack disconnect user username
* wp jetpack disconnect user [email protected]
*
* @synopsis <blog|user> [<user_identifier>] [--force]
*
* @param array $args Positional args.
* @param array $assoc_args Named args.
*/
public function disconnect( $args, $assoc_args ) {
$user = null;
if ( ! Jetpack::is_connection_ready() ) {
WP_CLI::success( __( 'The site is not currently connected, so nothing to do!', 'jetpack' ) );
return;
}
$action = isset( $args[0] ) ? $args[0] : 'prompt';
if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ), true ) ) {
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
}
if ( in_array( $action, array( 'user' ), true ) ) {
if ( isset( $args[1] ) ) {
$user_id = $args[1];
if ( ctype_digit( $user_id ) ) {
$field = 'id';
$user_id = (int) $user_id;
} elseif ( is_email( $user_id ) ) {
$field = 'email';
$user_id = sanitize_user( $user_id, true );
} else {
$field = 'login';
$user_id = sanitize_user( $user_id, true );
}
$user = get_user_by( $field, $user_id );
if ( ! $user ) {
WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) );
}
} else {
WP_CLI::error( __( 'Please specify a user by either ID, username, or email.', 'jetpack' ) );
}
}
$force_user_disconnect = ! empty( $assoc_args['force'] );
switch ( $action ) {
case 'blog':
Jetpack::log( 'disconnect' );
( new Connection_Manager( 'jetpack' ) )->disconnect_site();
WP_CLI::success(
sprintf(
/* translators: %s is the site URL */
__( 'Jetpack has been successfully disconnected for %s.', 'jetpack' ),
esc_url( get_site_url() )
)
);
break;
case 'user':
$connection_manager = new Connection_Manager( 'jetpack' );
$disconnected = $connection_manager->disconnect_user( $user->ID, $force_user_disconnect );
if ( $disconnected ) {
Jetpack::log( 'unlink', $user->ID );
WP_CLI::success( __( 'User has been successfully disconnected.', 'jetpack' ) );
} else {
if ( ! $connection_manager->is_user_connected( $user->ID ) ) {
/* translators: %s is a username */
$error_message = sprintf( __( 'User %s could not be disconnected because it is not connected!', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" );
} elseif ( ! $force_user_disconnect && $connection_manager->is_connection_owner( $user->ID ) ) {
/* translators: %s is a username */
$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}>" );
} else {
/* translators: %s is a username */
$error_message = sprintf( __( 'User %s could not be disconnected.', 'jetpack' ), "{$user->data->user_login} <{$user->data->user_email}>" );
}
WP_CLI::error( $error_message );
}
break;
case 'prompt':
WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) );
break;
}
}
/**
* Reset Jetpack options and settings to default
*
* ## OPTIONS
*
* modules: Resets modules to default state ( get_default_modules() )
*
* options: Resets all Jetpack options except:
* - All private options (Blog token, user token, etc...)
* - id (The Client ID/WP.com Blog ID of this site)
* - master_user
* - version
* - activated
*
* ## EXAMPLES
*
* wp jetpack reset options
* wp jetpack reset modules
* wp jetpack reset sync-checksum --dry-run --offset=0
*
* @synopsis <modules|options|sync-checksum> [--dry-run] [--offset=<offset>]
*
* @param array $args Positional args.
* @param array $assoc_args Named args.
*/
public function reset( $args, $assoc_args ) {
$action = isset( $args[0] ) ? $args[0] : 'prompt';
if ( ! in_array( $action, array( 'options', 'modules', 'sync-checksum' ), true ) ) {
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
}
$is_dry_run = ! empty( $assoc_args['dry-run'] );
if ( $is_dry_run ) {
WP_CLI::warning(
__( "\nThis is a dry run.\n", 'jetpack' ) .
__( "No actions will be taken.\n", 'jetpack' ) .
__( "The following messages will give you preview of what will happen when you run this command.\n\n", 'jetpack' )
);
} else {
// We only need to confirm "Are you sure?" when we are not doing a dry run.
jetpack_cli_are_you_sure();
}
switch ( $action ) {
case 'options':
$options_to_reset = Jetpack_Options::get_options_for_reset();
// Reset the Jetpack options.
WP_CLI::line(
sprintf(
/* translators: %s is the site URL */
__( "Resetting Jetpack Options for %s...\n", 'jetpack' ),
esc_url( get_site_url() )
)
);
sleep( 1 ); // Take a breath.
foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
if ( ! $is_dry_run ) {
Jetpack_Options::delete_option( $option_to_reset );
usleep( 100000 );
}
/* translators: This is the result of an action. The option named %s was reset */
WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
}
// Reset the WP options.
WP_CLI::line( __( "Resetting the jetpack options stored in wp_options...\n", 'jetpack' ) );
usleep( 500000 ); // Take a breath.
foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
if ( ! $is_dry_run ) {
delete_option( $option_to_reset );
usleep( 100000 );
}
/* translators: This is the result of an action. The option named %s was reset */
WP_CLI::success( sprintf( __( '%s option reset', 'jetpack' ), $option_to_reset ) );
}
// Reset to default modules.
WP_CLI::line( __( "Resetting default modules...\n", 'jetpack' ) );
usleep( 500000 ); // Take a breath.
$default_modules = Jetpack::get_default_modules();
if ( ! $is_dry_run ) {
Jetpack::update_active_modules( $default_modules );
}
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
break;
case 'modules':
if ( ! $is_dry_run ) {
$default_modules = Jetpack::get_default_modules();
Jetpack::update_active_modules( $default_modules );
}
WP_CLI::success( __( 'Modules reset to default.', 'jetpack' ) );
break;
case 'prompt':
WP_CLI::error( __( 'Please specify if you would like to reset your options, modules or sync-checksum', 'jetpack' ) );
break;
case 'sync-checksum':
$option = 'jetpack_callables_sync_checksum';
if ( is_multisite() ) {
$offset = isset( $assoc_args['offset'] ) ? (int) $assoc_args['offset'] : 0;
/*
* 1000 is a good limit since we don't expect the number of sites to be more than 1000
* Offset can be used to paginate and try to clean up more sites.
*/
$sites = get_sites(
array(
'number' => 1000,
'offset' => $offset,
)
);
$count_fixes = 0;
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
$count = self::count_option( $option );
if ( $count > 1 ) {
if ( ! $is_dry_run ) {
delete_option( $option );
}
WP_CLI::line(
sprintf(
/* translators: %1$d is a number, %2$s is the name of an option, %2$s is the site URL. */
__( 'Deleted %1$d %2$s options from %3$s', 'jetpack' ),
$count,
$option,
"{$site->domain}{$site->path}"
)
);
++$count_fixes;
if ( ! $is_dry_run ) {
/*
* We could be deleting a lot of options rows at the same time.
* Allow some time for replication to catch up.
*/
sleep( 3 );
}
}
restore_current_blog();
}
if ( $count_fixes ) {
WP_CLI::success(
sprintf(
/* translators: %1$s is the name of an option, %2$d is a number of sites. */
__( 'Successfully reset %1$s on %2$d sites.', 'jetpack' ),
$option,
$count_fixes
)
);
} else {
WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
}
return;
}
$count = self::count_option( $option );
if ( $count > 1 ) {
if ( ! $is_dry_run ) {
delete_option( $option );
}
WP_CLI::success(
sprintf(
/* translators: %1$d is a number, %2$s is the name of an option. */
__( 'Deleted %1$d %2$s options', 'jetpack' ),
$count,
$option
)
);
return;
}
WP_CLI::success( __( 'No options were deleted.', 'jetpack' ) );
break;
}
}
/**
* Return the number of times an option appears
* Normally an option would only appear 1 since the option key is supposed to be unique
* but if a site hasn't updated the DB schema then that would not be the case.
*
* @param string $option Option name.
*
* @return int
*/
private static function count_option( $option ) {
global $wpdb;
return (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->options WHERE option_name = %s",
$option
)
);
}
/**
* Manage Jetpack Modules
*
* ## OPTIONS
*
* <list|activate|deactivate|toggle>
* : The action to take.
* ---
* default: list
* options:
* - list
* - activate
* - deactivate
* - toggle
* ---
*
* [<module_slug>]
* : The slug of the module to perform an action on.
*
* [--format=<format>]
* : Allows overriding the output of the command when listing modules.
* ---
* default: table
* options:
* - table
* - json
* - csv
* - yaml
* - ids
* - count
* ---
*
* ## EXAMPLES
*
* wp jetpack module list
* wp jetpack module list --format=json
* wp jetpack module activate stats
* wp jetpack module deactivate stats
* wp jetpack module toggle stats
* wp jetpack module activate all
* wp jetpack module deactivate all
*
* @param array $args Positional args.
* @param array $assoc_args Named args.
*/
public function module( $args, $assoc_args ) {
$module_slug = null;
$action = isset( $args[0] ) ? $args[0] : 'list';
if ( isset( $args[1] ) ) {
$module_slug = $args[1];
if ( 'all' !== $module_slug && ! Jetpack::is_module( $module_slug ) ) {
/* translators: %s is a module slug like "stats" */
WP_CLI::error( sprintf( __( '%s is not a valid module.', 'jetpack' ), $module_slug ) );
}
if ( 'toggle' === $action ) {
$action = Jetpack::is_module_active( $module_slug )
? 'deactivate'
: 'activate';
}
if ( 'all' === $args[1] ) {
$action = ( 'deactivate' === $action )
? 'deactivate_all'
: 'activate_all';
}
} elseif ( 'list' !== $action ) {
WP_CLI::line( __( 'Please specify a valid module.', 'jetpack' ) );
$action = 'list';
}
switch ( $action ) {
case 'list':
$modules_list = array();
$modules = Jetpack::get_available_modules();
sort( $modules );
foreach ( (array) $modules as $module_slug ) {
if ( 'vaultpress' === $module_slug ) {
continue;
}
$modules_list[] = array(
'slug' => $module_slug,
'status' => Jetpack::is_module_active( $module_slug )
? __( 'Active', 'jetpack' )
: __( 'Inactive', 'jetpack' ),
);
}
WP_CLI\Utils\format_items( $assoc_args['format'], $modules_list, array( 'slug', 'status' ) );
break;
case 'activate':
$module = Jetpack::get_module( $module_slug );
Jetpack::log( 'activate', $module_slug );
if ( Jetpack::activate_module( $module_slug, false, false ) ) {
/* translators: %s is the name of a Jetpack module */
WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) );
} else {
/* translators: %s is the name of a Jetpack module */
WP_CLI::error( sprintf( __( '%s could not be activated.', 'jetpack' ), $module['name'] ) );
}
break;
case 'activate_all':
$modules = Jetpack::get_available_modules();
Jetpack::update_active_modules( $modules );
WP_CLI::success( __( 'All modules activated!', 'jetpack' ) );
break;
case 'deactivate':
$module = Jetpack::get_module( $module_slug );
Jetpack::log( 'deactivate', $module_slug );
Jetpack::deactivate_module( $module_slug );
/* translators: %s is the name of a Jetpack module */
WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) );
break;
case 'deactivate_all':
Jetpack::delete_active_modules();
WP_CLI::success( __( 'All modules deactivated!', 'jetpack' ) );
break;
case 'toggle':
// Will never happen, should have been handled above and changed to activate or deactivate.
break;
}
}
/**
* Manage Protect Settings
*
* ## OPTIONS
*
* allow: Add an IP address to an always allow list. You can also read or clear the allow list.
*
*
* ## EXAMPLES
*
* wp jetpack protect allow <ip address>
* wp jetpack protect allow list
* wp jetpack protect allow clear
*
* @synopsis <allow> [<ip|ip_low-ip_high|list|clear>]
*
* @param array $args Positional args.
*/
public function protect( $args ) {
$action = isset( $args[0] ) ? $args[0] : 'prompt';
if ( ! in_array( $action, array( 'whitelist', 'allow' ), true ) ) { // Still allow "whitelist" for legacy support.
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
}
// Check if module is active.
if ( ! Jetpack::is_module_active( __FUNCTION__ ) ) {
/* translators: %s is a module name */
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__ ) );
}
if ( in_array( $action, array( 'allow', 'whitelist' ), true ) ) {
if ( isset( $args[1] ) ) {
$action = 'allow';
} else {
$action = 'prompt';
}
}
switch ( $action ) {
case 'allow':
$allow = array();
$new_ip = $args[1];
$current_allow = get_site_option( 'jetpack_protect_whitelist', array() ); // @todo Update the option name.
// Build array of IPs that are already on the allowed list.
// Re-build manually instead of using jetpack_protect_format_allow_list() so we can easily get
// low & high range params for IP_Utils::ip_address_is_in_range().
foreach ( $current_allow as $allowed ) {
// IP ranges.
if ( $allowed->range ) {
// Is it already on the allowed list?
if ( IP_Utils::ip_address_is_in_range( $new_ip, $allowed->range_low, $allowed->range_high ) ) {
/* translators: %s is an IP address */
WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
break;
}
$allow[] = $allowed->range_low . ' - ' . $allowed->range_high;
} else { // Individual IPs.
// Check if the IP is already on the allow list (single IP only).
if ( $new_ip === $allowed->ip_address ) {
/* translators: %s is an IP address */
WP_CLI::error( sprintf( __( '%s is already on the always allow list.', 'jetpack' ), $new_ip ) );
break;
}
$allow[] = $allowed->ip_address;
}
}
/*
* List the allowed IPs.
* Done here because it's easier to read the $allow array after it's been rebuilt.
*/
if ( isset( $args[1] ) && 'list' === $args[1] ) {
if ( ! empty( $allow ) ) {
WP_CLI::success( __( 'Here are your always allowed IPs:', 'jetpack' ) );
foreach ( $allow as $ip ) {
WP_CLI::line( "\t" . str_pad( $ip, 24 ) );
}
} else {
WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
}
break;
}
/*
* Clear the always allow list.
*/
if ( isset( $args[1] ) && 'clear' === $args[1] ) {
if ( ! empty( $allow ) ) {
$allow = array();
Brute_Force_Protection_Shared_Functions::save_allow_list( $allow ); // @todo Need to update function name in the Protect module.
WP_CLI::success( __( 'Cleared all IPs from the always allow list.', 'jetpack' ) );
} else {
WP_CLI::line( __( 'Always allow list is empty.', 'jetpack' ) );
}
break;
}
// Append new IP to allow array.
array_push( $allow, $new_ip );
// Save allow list if there are no errors.
$result = Brute_Force_Protection_Shared_Functions::save_allow_list( $allow ); // @todo Need to update function name in the Protect module.
if ( is_wp_error( $result ) ) {
WP_CLI::error( $result );
}
/* translators: %s is an IP address */
WP_CLI::success( sprintf( __( '%s has been added to the always allowed list.', 'jetpack' ), $new_ip ) );
break;
case 'prompt':
WP_CLI::error(
__( 'No command found.', 'jetpack' ) . "\n" .
__( 'Please enter the IP address you want to always allow.', 'jetpack' ) . "\n" .
_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" .
_x( "You can also 'list' or 'clear' the always allowed list.", "'list' and 'clear' are commands and should not be translated", 'jetpack' ) . "\n"
);
break;
}
}
/**
* Manage Jetpack Options
*
* ## OPTIONS
*
* list : List all jetpack options and their values
* delete : Delete an option
* - can only delete options that are white listed.
* update : update an option
* - can only update option strings
* get : get the value of an option
*
* ## EXAMPLES
*
* wp jetpack options list
* wp jetpack options get <option_name>
* wp jetpack options delete <option_name>
* wp jetpack options update <option_name> [<option_value>]
*
* @synopsis <list|get|delete|update> [<option_name>] [<option_value>]
*
* @param array $args Positional args.
*/
public function options( $args ) {
$action = isset( $args[0] ) ? $args[0] : 'list';
$safe_to_modify = Jetpack_Options::get_options_for_reset();
// Is the option flagged as unsafe?
$flagged = ! in_array( $args[1], $safe_to_modify, true );
if ( ! in_array( $action, array( 'list', 'get', 'delete', 'update' ), true ) ) {
/* translators: %s is a command like "prompt" */
WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
}
if ( isset( $args[0] ) ) {
if ( 'get' === $args[0] && isset( $args[1] ) ) {
$action = 'get';
} elseif ( 'delete' === $args[0] && isset( $args[1] ) ) {
$action = 'delete';
} elseif ( 'update' === $args[0] && isset( $args[1] ) ) {
$action = 'update';
} else {
$action = 'list';
}
}
// Bail if the option isn't found.
$option = isset( $args[1] ) ? Jetpack_Options::get_option( $args[1] ) : false;
if ( isset( $args[1] ) && ! $option && 'update' !== $args[0] ) {
WP_CLI::error( __( 'Option not found or is empty. Use "list" to list option names', 'jetpack' ) );
}
// Let's print_r the option if it's an array.
// Used in the 'get' and 'list' actions.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
$option = is_array( $option ) ? print_r( $option, true ) : $option;
switch ( $action ) {
case 'get':
WP_CLI::success( "\t" . $option );
break;
case 'delete':
jetpack_cli_are_you_sure( $flagged );
Jetpack_Options::delete_option( $args[1] );
/* translators: %s is the option name */
WP_CLI::success( sprintf( __( 'Deleted option: %s', 'jetpack' ), $args[1] ) );
break;
case 'update':
jetpack_cli_are_you_sure( $flagged );
// Updating arrays would get pretty tricky...
$value = Jetpack_Options::get_option( $args[1] );
if ( $value && is_array( $value ) ) {
WP_CLI::error( __( 'Sorry, no updating arrays at this time', 'jetpack' ) );
}
Jetpack_Options::update_option( $args[1], $args[2] );
/* translators: %1$s is the previous value, %2$s is the new value */
WP_CLI::success( sprintf( _x( 'Updated option: %1$s to "%2$s"', 'Updating an option from "this" to "that".', 'jetpack' ), $args[1], $args[2] ) );
break;
case 'list':
$options_compact = Jetpack_Options::get_option_names();
$options_non_compact = Jetpack_Options::get_option_names( 'non_compact' );
$options_private = Jetpack_Options::get_option_names( 'private' );
$options = array_merge( $options_compact, $options_non_compact, $options_private );
// Table headers.
WP_CLI::line( "\t" . str_pad( __( 'Option', 'jetpack' ), 30 ) . __( 'Value', 'jetpack' ) );
// List out the options and their values.
// Tell them if the value is empty or not.
// Tell them if it's an array.
foreach ( $options as $option ) {
$value = Jetpack_Options::get_option( $option );
if ( ! $value ) {
WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Empty' );
continue;
}
if ( ! is_array( $value ) ) {
WP_CLI::line( "\t" . str_pad( $option, 30 ) . $value );
} elseif ( is_array( $value ) ) {
WP_CLI::line( "\t" . str_pad( $option, 30 ) . 'Array - Use "get <option>" to read option array.' );
}
}
$option_text = '{' . _x( 'option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack' ) . '}';
$value_text = '{' . _x( 'value', 'the value that they want to update the option to', 'jetpack' ) . '}';
WP_CLI::success(
_x( "Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack' ) . "\n" .
str_pad( 'wp jetpack options get', 26 ) . $option_text . "\n" .
str_pad( 'wp jetpack options delete', 26 ) . $option_text . "\n" .
str_pad( 'wp jetpack options update', 26 ) . "$option_text $value_text\n" .
_x( "Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack' ) . "\n"
);
break;
}
}
/**
* Get the status of or start a new Jetpack sync.
*
* ## OPTIONS
*
* status : Print the current sync status
* settings : Prints the current sync settings
* start : Start a full sync from this site to WordPress.com
* enable : Enables sync on the site
* disable : Disable sync on a site
* reset : Disables sync and Resets the sync queues on a site
*
* ## EXAMPLES
*
* wp jetpack sync status
* wp jetpack sync settings
* wp jetpack sync start --modules=functions --sync_wait_time=5
* wp jetpack sync enable
* wp jetpack sync disable
* wp jetpack sync reset
* wp jetpack sync reset --queue=full or regular
*
* @synopsis <status|start> [--<field>=<value>]
*
* @param array $args Positional args.
* @param array $assoc_args Named args.
*/
public function sync( $args, $assoc_args ) {
$action = isset( $args[0] ) ? $args[0] : 'status';
switch ( $action ) {
case 'status':
$status = Actions::get_sync_status();
$collection = array();
foreach ( $status as $key => $item ) {
$collection[] = array(
'option' => $key,
'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
);
}
WP_CLI::log( __( 'Sync Status:', 'jetpack' ) );
WP_CLI\Utils\format_items( 'table', $collection, array( 'option', 'value' ) );
break;
case 'settings':
WP_CLI::log( __( 'Sync Settings:', 'jetpack' ) );
$settings = array();
foreach ( Settings::get_settings() as $setting => $item ) {
$settings[] = array(
'setting' => $setting,
'value' => is_scalar( $item ) ? $item : wp_json_encode( $item ),
);
}
WP_CLI\Utils\format_items( 'table', $settings, array( 'setting', 'value' ) );
break;
case 'disable':
// Don't set it via the Settings since that also resets the queues.
update_option( 'jetpack_sync_settings_disable', 1 );
/* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Sync Disabled on %s', 'jetpack' ), get_site_url() ) );
break;
case 'enable':
Settings::update_settings( array( 'disable' => 0 ) );
/* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Sync Enabled on %s', 'jetpack' ), get_site_url() ) );
break;
case 'reset':
// Don't set it via the Settings since that also resets the queues.
update_option( 'jetpack_sync_settings_disable', 1 );
/* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Sync Disabled on %s. Use `wp jetpack sync enable` to enable syncing again.', 'jetpack' ), get_site_url() ) );
$listener = Listener::get_instance();
if ( empty( $assoc_args['queue'] ) ) {
$listener->get_sync_queue()->reset();
$listener->get_full_sync_queue()->reset();
/* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Reset Full Sync and Regular Queues Queue on %s', 'jetpack' ), get_site_url() ) );
break;
}
if ( ! empty( $assoc_args['queue'] ) ) {
switch ( $assoc_args['queue'] ) {
case 'regular':
$listener->get_sync_queue()->reset();
/* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Reset Regular Sync Queue on %s', 'jetpack' ), get_site_url() ) );
break;
case 'full':
$listener->get_full_sync_queue()->reset();
/* translators: %s is the site URL */
WP_CLI::log( sprintf( __( 'Reset Full Sync Queue on %s', 'jetpack' ), get_site_url() ) );
break;
default:
WP_CLI::error( __( 'Please specify what type of queue do you want to reset: `full` or `regular`.', 'jetpack' ) );
break;
}
}
break;
case 'start':
if ( ! Actions::sync_allowed() ) {
if ( Settings::get_setting( 'disable' ) ) {
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' ) );
return;
}
$connection = new Connection_Manager();
if ( ! $connection->is_connected() ) {
if ( ! doing_action( 'jetpack_site_registered' ) ) {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. Jetpack is not connected.', 'jetpack' ) );
return;
}
}
$status = new Status();
if ( $status->is_offline_mode() ) {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in offline mode.', 'jetpack' ) );
return;
}
if ( $status->in_safe_mode() ) {
WP_CLI::error( __( 'Jetpack sync is not currently allowed for this site. The site is in safe mode.', 'jetpack' ) );
return;
}
}
// Get the original settings so that we can restore them later.
$original_settings = Settings::get_settings();
// Initialize sync settigns so we can sync as quickly as possible.
$sync_settings = wp_parse_args(
array_intersect_key( $assoc_args, Settings::$valid_settings ),
array(