-
Notifications
You must be signed in to change notification settings - Fork 794
/
class.core-rest-api-endpoints.php
2280 lines (2037 loc) · 77.2 KB
/
class.core-rest-api-endpoints.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
/**
* Register WP REST API endpoints for Jetpack.
*
* @author Automattic
*/
/**
* Disable direct access.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Load WP_Error for error messages.
require_once ABSPATH . '/wp-includes/class-wp-error.php';
// Register endpoints when WP REST API is initialized.
add_action( 'rest_api_init', array( 'Jetpack_Core_Json_Api_Endpoints', 'register_endpoints' ) );
/**
* Class Jetpack_Core_Json_Api_Endpoints
*
* @since 4.3.0
*/
class Jetpack_Core_Json_Api_Endpoints {
/**
* @var string Generic error message when user is not allowed to perform an action.
*/
public static $user_permissions_error_msg;
/**
* @var array Roles that can access Stats once they're granted access.
*/
public static $stats_roles;
/**
* Declare the Jetpack REST API endpoints.
*
* @since 4.3.0
*/
public static function register_endpoints() {
// Load API endpoint base classes
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php';
// Load API endpoints
require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
self::$user_permissions_error_msg = esc_html__(
'You do not have the correct user permissions to perform this action.
Please contact your site admin if you think this is a mistake.',
'jetpack'
);
self::$stats_roles = array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' );
// Get current connection status of Jetpack
register_rest_route( 'jetpack/v4', '/connection', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::jetpack_connection_status',
) );
// Fetches a fresh connect URL
register_rest_route( 'jetpack/v4', '/connection/url', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::build_connect_url',
'permission_callback' => __CLASS__ . '::connect_url_permission_callback',
) );
// Get current user connection data
register_rest_route( 'jetpack/v4', '/connection/data', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_user_connection_data',
'permission_callback' => __CLASS__ . '::get_user_connection_data_permission_callback',
) );
// Disconnect site from WordPress.com servers
register_rest_route( 'jetpack/v4', '/connection', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => __CLASS__ . '::disconnect_site',
'permission_callback' => __CLASS__ . '::disconnect_site_permission_callback',
) );
// Disconnect/unlink user from WordPress.com servers
register_rest_route( 'jetpack/v4', '/connection/user', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => __CLASS__ . '::unlink_user',
'permission_callback' => __CLASS__ . '::unlink_user_permission_callback',
) );
// Get current site data
register_rest_route( 'jetpack/v4', '/site', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_site_data',
'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
) );
// Confirm that a site in identity crisis should be in staging mode
register_rest_route( 'jetpack/v4', '/identity-crisis/confirm-safe-mode', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => __CLASS__ . '::confirm_safe_mode',
'permission_callback' => __CLASS__ . '::identity_crisis_mitigation_permission_check',
) );
// Return all modules
self::route(
'module/all',
'Jetpack_Core_API_Module_List_Endpoint',
WP_REST_Server::READABLE
);
// Activate many modules
self::route(
'/module/all/active',
'Jetpack_Core_API_Module_List_Endpoint',
WP_REST_Server::EDITABLE,
NULL,
array(
'modules' => array(
'default' => '',
'type' => 'array',
'required' => true,
'validate_callback' => __CLASS__ . '::validate_module_list',
),
'active' => array(
'default' => true,
'type' => 'boolean',
'required' => false,
'validate_callback' => __CLASS__ . '::validate_boolean',
),
)
);
Jetpack::load_xml_rpc_client();
// Return a single module and update it when needed
self::route(
'/module/(?P<slug>[a-z\-]+)',
'Jetpack_Core_API_Data',
WP_REST_Server::READABLE,
new Jetpack_IXR_Client( array( 'user_id' => get_current_user_id() ) )
);
// Activate and deactivate a module
self::route(
'/module/(?P<slug>[a-z\-]+)/active',
'Jetpack_Core_API_Module_Toggle_Endpoint',
WP_REST_Server::EDITABLE,
new Jetpack_IXR_Client(),
array(
'active' => array(
'default' => true,
'type' => 'boolean',
'required' => true,
'validate_callback' => __CLASS__ . '::validate_boolean',
),
)
);
// Update a module
self::route(
'/module/(?P<slug>[a-z\-]+)',
'Jetpack_Core_API_Data',
WP_REST_Server::EDITABLE,
new Jetpack_IXR_Client( array( 'user_id' => get_current_user_id() ) ),
self::get_updateable_parameters()
);
// Get data for a specific module, i.e. Protect block count, WPCOM stats,
// Akismet spam count, etc.
self::route(
'/module/(?P<slug>[a-z\-]+)/data',
'Jetpack_Core_API_Module_Data_Endpoint',
WP_REST_Server::READABLE,
NULL,
array(
'range' => array(
'default' => 'day',
'type' => 'string',
'required' => false,
'validate_callback' => __CLASS__ . '::validate_string',
),
)
);
// Update any Jetpack module option or setting
self::route(
'/settings',
'Jetpack_Core_API_Data',
WP_REST_Server::EDITABLE,
new Jetpack_IXR_Client( array( 'user_id' => get_current_user_id() ) ),
self::get_updateable_parameters( 'any' )
);
// Update a module
self::route(
'/settings/(?P<slug>[a-z\-]+)',
'Jetpack_Core_API_Data',
WP_REST_Server::EDITABLE,
new Jetpack_IXR_Client( array( 'user_id' => get_current_user_id() ) ),
self::get_updateable_parameters()
);
// Return miscellaneous settings
register_rest_route( 'jetpack/v4', '/settings', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_settings',
'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
) );
// Reset all Jetpack options
register_rest_route( 'jetpack/v4', '/options/(?P<options>[a-z\-]+)', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => __CLASS__ . '::reset_jetpack_options',
'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
) );
// Jumpstart
register_rest_route( 'jetpack/v4', '/jumpstart', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => __CLASS__ . '::jumpstart_toggle',
'permission_callback' => __CLASS__ . '::manage_modules_permission_check',
'args' => array(
'active' => array(
'required' => true,
'validate_callback' => __CLASS__ . '::validate_boolean',
),
),
) );
// Updates: get number of plugin updates available
register_rest_route( 'jetpack/v4', '/updates/plugins', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_plugin_update_count',
'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
) );
// Dismiss Jetpack Notices
register_rest_route( 'jetpack/v4', '/notice/(?P<notice>[a-z\-_]+)', array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => __CLASS__ . '::dismiss_notice',
'permission_callback' => __CLASS__ . '::view_admin_page_permission_check',
) );
// Plugins: get list of all plugins.
register_rest_route( 'jetpack/v4', '/plugins', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_plugins',
'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
) );
// Plugins: check if the plugin is active.
register_rest_route( 'jetpack/v4', '/plugin/(?P<plugin>[a-z\/\.\-_]+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::get_plugin',
'permission_callback' => __CLASS__ . '::activate_plugins_permission_check',
) );
}
public static function route( $path, $classname, $method,
$constructor_arguments = NULL,
$endpoint_arguments = NULL
) {
if ( ! empty( $constructor_arguments ) ) {
$endpoint = new $classname( $constructor_arguments );
} else {
$endpoint = new $classname();
}
$parameters = array(
'methods' => $method,
'callback' => array( $endpoint, 'process' ),
'permission_callback' => array( $endpoint, 'can_request' )
);
if ( ! empty( $endpoint_arguments ) ) {
$parameters['args'] = $endpoint_arguments;
}
register_rest_route( 'jetpack/v4', $path, $parameters );
}
/**
* Handles dismissing of Jetpack Notices
*
* @since 4.3.0
*
* @return array|wp-error
*/
public static function dismiss_notice( $data ) {
$notice = $data['notice'];
$param = $data->get_json_params();
if ( ! isset( $param['dismissed'] ) || $param['dismissed'] !== true ) {
return new WP_Error( 'invalid_param', esc_html__( 'Invalid parameter "dismissed".', 'jetpack' ), array( 'status' => 404 ) );
}
if ( isset( $notice ) && ! empty( $notice ) ) {
switch( $notice ) {
case 'feedback_dash_request':
case 'welcome':
$notices = get_option( 'jetpack_dismissed_notices', array() );
$notices[ $notice ] = true;
update_option( 'jetpack_dismissed_notices', $notices );
return rest_ensure_response( get_option( 'jetpack_dismissed_notices', array() ) );
default:
return new WP_Error( 'invalid_param', esc_html__( 'Invalid parameter "notice".', 'jetpack' ), array( 'status' => 404 ) );
}
}
return new WP_Error( 'required_param', esc_html__( 'Missing parameter "notice".', 'jetpack' ), array( 'status' => 404 ) );
}
/**
* Verify that the user can disconnect the site.
*
* @since 4.3.0
*
* @return bool|WP_Error True if user is able to disconnect the site.
*/
public static function disconnect_site_permission_callback() {
if ( current_user_can( 'jetpack_disconnect' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_jetpack_disconnect', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that the user can get a connect/link URL
*
* @since 4.3.0
*
* @return bool|WP_Error True if user is able to disconnect the site.
*/
public static function connect_url_permission_callback() {
if ( current_user_can( 'jetpack_connect_user' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_jetpack_disconnect', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that a user can get the data about the current user.
* Only those who can connect.
*
* @since 4.3.0
*
* @uses Jetpack::is_user_connected();
*
* @return bool|WP_Error True if user is able to unlink.
*/
public static function get_user_connection_data_permission_callback() {
if ( current_user_can( 'jetpack_connect_user' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_user_connection_data', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that a user can use the /connection/user endpoint. Has to be a registered user and be currently linked.
*
* @since 4.3.0
*
* @uses Jetpack::is_user_connected();
*
* @return bool|WP_Error True if user is able to unlink.
*/
public static function unlink_user_permission_callback() {
if ( current_user_can( 'jetpack_connect_user' ) && Jetpack::is_user_connected( get_current_user_id() ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_unlink_user', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that user can manage Jetpack modules.
*
* @since 4.3.0
*
* @return bool Whether user has the capability 'jetpack_manage_modules'.
*/
public static function manage_modules_permission_check() {
if ( current_user_can( 'jetpack_manage_modules' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_manage_modules', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that user can update Jetpack modules.
*
* @since 4.3.0
*
* @return bool Whether user has the capability 'jetpack_configure_modules'.
*/
public static function configure_modules_permission_check() {
if ( current_user_can( 'jetpack_configure_modules' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_configure_modules', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that user can view Jetpack admin page.
*
* @since 4.3.0
*
* @return bool Whether user has the capability 'jetpack_admin_page'.
*/
public static function view_admin_page_permission_check() {
if ( current_user_can( 'jetpack_admin_page' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_view_admin', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that user can mitigate an identity crisis.
*
* @since 4.4.0
*
* @return bool Whether user has capability 'jetpack_disconnect'.
*/
public static function identity_crisis_mitigation_permission_check() {
if ( current_user_can( 'jetpack_disconnect' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_identity_crisis', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that user can update Jetpack general settings.
*
* @since 4.3.0
*
* @return bool Whether user has the capability 'update_settings_permission_check'.
*/
public static function update_settings_permission_check() {
if ( current_user_can( 'jetpack_configure_modules' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_manage_settings', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Verify that user can view Jetpack admin page and can activate plugins.
*
* @since 4.3.0
*
* @return bool Whether user has the capability 'jetpack_admin_page' and 'activate_plugins'.
*/
public static function activate_plugins_permission_check() {
if ( current_user_can( 'jetpack_admin_page' ) && current_user_can( 'activate_plugins' ) ) {
return true;
}
return new WP_Error( 'invalid_user_permission_activate_plugins', self::$user_permissions_error_msg, array( 'status' => self::rest_authorization_required_code() ) );
}
/**
* Contextual HTTP error code for authorization failure.
*
* Taken from rest_authorization_required_code() in WP-API plugin until is added to core.
* @see https://github.com/WP-API/WP-API/commit/7ba0ae6fe4f605d5ffe4ee85b1cd5f9fb46900a6
*
* @since 4.3.0
*
* @return int
*/
public static function rest_authorization_required_code() {
return is_user_logged_in() ? 403 : 401;
}
/**
* Get connection status for this Jetpack site.
*
* @since 4.3.0
*
* @return bool True if site is connected
*/
public static function jetpack_connection_status() {
return rest_ensure_response( array(
'isActive' => Jetpack::is_active(),
'isStaging' => Jetpack::is_staging_site(),
'devMode' => array(
'isActive' => Jetpack::is_development_mode(),
'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
'url' => site_url() && false === strpos( site_url(), '.' ),
'filter' => apply_filters( 'jetpack_development_mode', false ),
),
)
);
}
/**
* Disconnects Jetpack from the WordPress.com Servers
*
* @uses Jetpack::disconnect();
* @since 4.3.0
* @return bool|WP_Error True if Jetpack successfully disconnected.
*/
public static function disconnect_site( $data ) {
$param = $data->get_json_params();
if ( ! isset( $param['isActive'] ) || $param['isActive'] !== false ) {
return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
}
if ( Jetpack::is_active() ) {
Jetpack::disconnect();
return rest_ensure_response( array( 'code' => 'success' ) );
}
return new WP_Error( 'disconnect_failed', esc_html__( 'Was not able to disconnect the site. Please try again.', 'jetpack' ), array( 'status' => 400 ) );
}
/**
* Gets a new connect raw URL with fresh nonce.
*
* @uses Jetpack::disconnect();
* @since 4.3.0
* @return string|WP_Error A raw URL if the connection URL could be built; error message otherwise.
*/
public static function build_connect_url() {
$url = Jetpack::init()->build_connect_url( true, false, false );
if ( $url ) {
return rest_ensure_response( $url );
}
return new WP_Error( 'build_connect_url_failed', esc_html__( 'Unable to build the connect URL. Please reload the page and try again.', 'jetpack' ), array( 'status' => 400 ) );
}
/**
* Get miscellaneous settings for this Jetpack installation, like Holiday Snow.
*
* @since 4.3.0
*
* @return object $response {
* Array of miscellaneous settings.
*
* @type bool $holiday-snow Did Jack steal Christmas?
* }
*/
public static function get_settings() {
$response = array(
self::holiday_snow_option_name() => get_option( self::holiday_snow_option_name() ) == 'letitsnow',
);
return rest_ensure_response( $response );
}
/**
* Get miscellaneous user data related to the connection. Similar data available in old "My Jetpack".
* Information about the master/primary user.
* Information about the current user.
*
* @since 4.3.0
*
* @return object
*/
public static function get_user_connection_data() {
require_once( JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-react-page.php' );
$response = array(
// 'othersLinked' => Jetpack::get_other_linked_admins(),
'currentUser' => jetpack_current_user_data(),
);
return rest_ensure_response( $response );
}
/**
* Returns the proper name for Jetpack Holiday Snow setting.
* When the REST route starts, the holiday-snow.php file where jetpack_holiday_snow_option_name() function is defined is not loaded,
* so where using this to replicate it and have the same functionality.
*
* @since 4.4.0
*
* @return string
*/
public static function holiday_snow_option_name() {
/** This filter is documented in modules/holiday-snow.php */
return apply_filters( 'jetpack_holiday_snow_option_name', 'jetpack_holiday_snow_enabled' );
}
/**
* Update a single miscellaneous setting for this Jetpack installation, like Holiday Snow.
*
* @since 4.3.0
*
* @param WP_REST_Request $data
*
* @return object Jetpack miscellaneous settings.
*/
public static function update_setting( $data ) {
// Get parameters to update the module.
$param = $data->get_json_params();
// Exit if no parameters were passed.
if ( ! is_array( $param ) ) {
return new WP_Error( 'missing_setting', esc_html__( 'Missing setting.', 'jetpack' ), array( 'status' => 404 ) );
}
// Get option name and value.
$option = key( $param );
$value = current( $param );
// Log success or not
$updated = false;
switch ( $option ) {
case self::holiday_snow_option_name():
$updated = update_option( $option, ( true == (bool) $value ) ? 'letitsnow' : '' );
break;
}
if ( $updated ) {
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Setting updated.', 'jetpack' ),
'value' => $value,
) );
}
return new WP_Error( 'setting_not_updated', esc_html__( 'The setting was not updated.', 'jetpack' ), array( 'status' => 400 ) );
}
/**
* Unlinks current user from the WordPress.com Servers.
*
* @since 4.3.0
* @uses Jetpack::unlink_user
*
* @return bool|WP_Error True if user successfully unlinked.
*/
public static function unlink_user( $data ) {
$param = $data->get_json_params();
if ( ! isset( $param['linked'] ) || $param['linked'] !== false ) {
return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
}
if ( Jetpack::unlink_user() ) {
return rest_ensure_response(
array(
'code' => 'success'
)
);
}
return new WP_Error( 'unlink_user_failed', esc_html__( 'Was not able to unlink the user. Please try again.', 'jetpack' ), array( 'status' => 400 ) );
}
/**
* Get site data, including for example, the site's current plan.
*
* @since 4.3.0
*
* @return array Array of Jetpack modules.
*/
public static function get_site_data() {
if ( $site_id = Jetpack_Options::get_option( 'id' ) ) {
$response = Jetpack_Client::wpcom_json_api_request_as_blog( sprintf( '/sites/%d', $site_id ), '1.1' );
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error( 'site_data_fetch_failed', esc_html__( 'Failed fetching site data. Try again later.', 'jetpack' ), array( 'status' => 400 ) );
}
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Site data correctly received.', 'jetpack' ),
'data' => wp_remote_retrieve_body( $response ),
)
);
}
return new WP_Error( 'site_id_missing', esc_html__( 'The ID of this site does not exist.', 'jetpack' ), array( 'status' => 404 ) );
}
/**
* Sets a flag to confirm safe mode.
*
* @since 4.4.0
*
* @return bool True
*/
public static function confirm_safe_mode() {
$updated = Jetpack_Options::update_option( 'safe_mode_confirmed', true );
if ( $updated ) {
return rest_ensure_response(
array(
'code' => 'success'
)
);
}
return new WP_Error( 'error_confirming_safe_mode', esc_html__( 'Jetpack could not confirm safe mode.', 'jetpack' ), array( 'status' => 500 ) );
}
/**
* Reset Jetpack options
*
* @since 4.3.0
*
* @param WP_REST_Request $data {
* Array of parameters received by request.
*
* @type string $options Available options to reset are options|modules
* }
*
* @return bool|WP_Error True if options were reset. Otherwise, a WP_Error instance with the corresponding error.
*/
public static function reset_jetpack_options( $data ) {
$param = $data->get_json_params();
if ( ! isset( $param['reset'] ) || $param['reset'] !== true ) {
return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
}
if ( isset( $data['options'] ) ) {
$data = $data['options'];
switch( $data ) {
case ( 'options' ) :
$options_to_reset = Jetpack::get_jetpack_options_for_reset();
// Reset the Jetpack options
foreach ( $options_to_reset['jp_options'] as $option_to_reset ) {
Jetpack_Options::delete_option( $option_to_reset );
}
foreach ( $options_to_reset['wp_options'] as $option_to_reset ) {
delete_option( $option_to_reset );
}
// Reset to default modules
$default_modules = Jetpack::get_default_modules();
Jetpack::update_active_modules( $default_modules );
// Jumpstart option is special
Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Jetpack options reset.', 'jetpack' ),
) );
break;
case 'modules':
$default_modules = Jetpack::get_default_modules();
Jetpack::update_active_modules( $default_modules );
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Modules reset to default.', 'jetpack' ),
) );
break;
default:
return new WP_Error( 'invalid_param', esc_html__( 'Invalid Parameter', 'jetpack' ), array( 'status' => 404 ) );
}
}
return new WP_Error( 'required_param', esc_html__( 'Missing parameter "type".', 'jetpack' ), array( 'status' => 404 ) );
}
/**
* Toggles activation or deactivation of the JumpStart
*
* @since 4.3.0
*
* @param WP_REST_Request $data {
* Array of parameters received by request.
* }
*
* @return bool|WP_Error True if toggling Jumpstart succeeded. Otherwise, a WP_Error instance with the corresponding error.
*/
public static function jumpstart_toggle( $data ) {
$param = $data->get_json_params();
if ( $param[ 'active' ] ) {
return self::jumpstart_activate( $data );
} else {
return self::jumpstart_deactivate( $data );
}
}
/**
* Activates a series of valid Jetpack modules and initializes some options.
*
* @since 4.3.0
*
* @param WP_REST_Request $data {
* Array of parameters received by request.
* }
*
* @return bool|WP_Error True if Jumpstart succeeded. Otherwise, a WP_Error instance with the corresponding error.
*/
public static function jumpstart_activate( $data ) {
$modules = Jetpack::get_available_modules();
$activate_modules = array();
foreach ( $modules as $module ) {
$module_info = Jetpack::get_module( $module );
if ( isset( $module_info['feature'] ) && is_array( $module_info['feature'] ) && in_array( 'Jumpstart', $module_info['feature'] ) ) {
$activate_modules[] = $module;
}
}
// Collect success/error messages like modules that are properly activated.
$result = array(
'activated_modules' => array(),
'failed_modules' => array(),
);
// Update the jumpstart option
if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
$result['jumpstart_activated'] = Jetpack_Options::update_option( 'jumpstart', 'jumpstart_activated' );
}
// Check for possible conflicting plugins
$module_slugs_filtered = Jetpack::init()->filter_default_modules( $activate_modules );
foreach ( $module_slugs_filtered as $module_slug ) {
Jetpack::log( 'activate', $module_slug );
if ( Jetpack::activate_module( $module_slug, false, false ) ) {
$result['activated_modules'][] = $module_slug;
} else {
$result['failed_modules'][] = $module_slug;
}
}
// Set the default sharing buttons and set to display on posts if none have been set.
$sharing_services = get_option( 'sharing-services' );
$sharing_options = get_option( 'sharing-options' );
if ( empty( $sharing_services['visible'] ) ) {
// Default buttons to set
$visible = array(
'twitter',
'facebook',
'google-plus-1',
);
$hidden = array();
// Set some sharing settings
if ( class_exists( 'Sharing_Service' ) ) {
$sharing = new Sharing_Service();
$sharing_options['global'] = array(
'button_style' => 'icon',
'sharing_label' => $sharing->default_sharing_label,
'open_links' => 'same',
'show' => array( 'post' ),
'custom' => isset( $sharing_options['global']['custom'] ) ? $sharing_options['global']['custom'] : array()
);
$result['sharing_options'] = update_option( 'sharing-options', $sharing_options );
$result['sharing_services'] = update_option( 'sharing-services', array( 'visible' => $visible, 'hidden' => $hidden ) );
}
}
// If all Jumpstart modules were activated
if ( empty( $result['failed_modules'] ) ) {
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Jumpstart done.', 'jetpack' ),
'data' => $result,
) );
}
return new WP_Error( 'jumpstart_failed', esc_html( sprintf( _n( 'Jumpstart failed activating this module: %s.', 'Jumpstart failed activating these modules: %s.', count( $result['failed_modules'] ), 'jetpack' ), join( ', ', $result['failed_modules'] ) ) ), array( 'status' => 400 ) );
}
/**
* Dismisses Jumpstart so user is not prompted to go through it again.
*
* @since 4.3.0
*
* @param WP_REST_Request $data {
* Array of parameters received by request.
* }
*
* @return bool|WP_Error True if Jumpstart was disabled or was nothing to dismiss. Otherwise, a WP_Error instance with a message.
*/
public static function jumpstart_deactivate( $data ) {
// If dismissed, flag the jumpstart option as such.
if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
if ( Jetpack_Options::update_option( 'jumpstart', 'jumpstart_dismissed' ) ) {
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Jumpstart dismissed.', 'jetpack' ),
) );
} else {
return new WP_Error( 'jumpstart_failed_dismiss', esc_html__( 'Jumpstart could not be dismissed.', 'jetpack' ), array( 'status' => 400 ) );
}
}
// If this was not a new connection and there was nothing to dismiss, don't fail.
return rest_ensure_response( array(
'code' => 'success',
'message' => esc_html__( 'Nothing to dismiss. This was not a new connection.', 'jetpack' ),
) );
}
/**
* Get the query parameters to update module options or general settings.
*
* @since 4.3.0
* @since 4.4.0 Accepts a $selector parameter.
*
* @param string $selector Selects a set of options to update, Can be empty, a module slug or 'any'.
*
* @return array
*/
public static function get_updateable_parameters( $selector = '' ) {
$parameters = array(
'context' => array(
'default' => 'edit',
),
);
return array_merge( $parameters, self::get_updateable_data_list( $selector ) );
}
/**
* Returns a list of module options or general settings that can be updated.
*
* @since 4.3.0
* @since 4.4.0 Accepts 'any' as a parameter which will make it return the entire list.
*
* @param string|array $selector Module slug, 'any', or an array of parameters.
* If empty, it's assumed we're updating a module and we'll try to get its slug.
* If 'any' the full list is returned.
* If it's an array of parameters, includes the elements by matching keys.
*
* @return array
*/
public static function get_updateable_data_list( $selector = '' ) {
$options = array(
// Carousel
'carousel_background_color' => array(
'description' => esc_html__( 'Background color.', 'jetpack' ),
'type' => 'string',
'default' => 'black',
'enum' => array(
'black' => esc_html__( 'Black', 'jetpack' ),
'white' => esc_html__( 'White', 'jetpack' ),
),
'validate_callback' => __CLASS__ . '::validate_list_item',
'jp_group' => 'carousel',
),
'carousel_display_exif' => array(
'description' => wp_kses( sprintf( __( 'Show photo metadata (<a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format" target="_blank">Exif</a>) in carousel, when available.', 'jetpack' ) ), array( 'a' => array( 'href' => true, 'target' => true ) ) ),
'type' => 'boolean',
'default' => 0,
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'carousel',
),
// Comments
'highlander_comment_form_prompt' => array(
'description' => esc_html__( 'Greeting Text', 'jetpack' ),
'type' => 'string',
'default' => esc_html__( 'Leave a Reply', 'jetpack' ),
'sanitize_callback' => 'sanitize_text_field',
'jp_group' => 'comments',
),
'jetpack_comment_form_color_scheme' => array(
'description' => esc_html__( "Color Scheme", 'jetpack' ),
'type' => 'string',
'default' => 'light',
'enum' => array(
'light' => esc_html__( 'Light', 'jetpack' ),
'dark' => esc_html__( 'Dark', 'jetpack' ),
'transparent' => esc_html__( 'Transparent', 'jetpack' ),
),
'validate_callback' => __CLASS__ . '::validate_list_item',
'jp_group' => 'comments',
),
// Custom Content Types
'jetpack_portfolio' => array(
'description' => esc_html__( 'Enable or disable Jetpack portfolio post type.', 'jetpack' ),
'type' => 'boolean',
'default' => 0,
'validate_callback' => __CLASS__ . '::validate_boolean',
'jp_group' => 'custom-content-types',
),
'jetpack_portfolio_posts_per_page' => array(