-
Notifications
You must be signed in to change notification settings - Fork 274
/
Authentication.php
1441 lines (1275 loc) · 38.6 KB
/
Authentication.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
/**
* Class Google\Site_Kit\Core\Authentication\Authentication
*
* @package Google\Site_Kit
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\Site_Kit\Core\Authentication;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client;
use Google\Site_Kit\Core\Authentication\User_Input_State;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\REST_API\REST_Route;
use Google\Site_Kit\Core\REST_API\REST_Routes;
use Google\Site_Kit\Core\Storage\Encrypted_Options;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Storage\Transients;
use Google\Site_Kit\Core\Admin\Notice;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
use Google\Site_Kit\Core\Util\User_Input_Settings;
use Google\Site_Kit\Plugin;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use Exception;
use Google\Site_Kit\Core\Util\BC_Functions;
/**
* Authentication Class.
*
* @since 1.0.0
* @access private
* @ignore
*/
final class Authentication {
use Method_Proxy_Trait;
const ACTION_CONNECT = 'googlesitekit_connect';
const ACTION_DISCONNECT = 'googlesitekit_disconnect';
/**
* Plugin context.
*
* @since 1.0.0
* @var Context
*/
private $context;
/**
* Options object.
*
* @since 1.0.0
*
* @var Options
*/
private $options = null;
/**
* User_Options object.
*
* @since 1.0.0
*
* @var User_Options
*/
private $user_options = null;
/**
* User_Input_State object.
*
* @since 1.20.0
*
* @var User_Input_State
*/
private $user_input_state = null;
/**
* User_Input_Settings
*
* @since 1.20.0
*
* @var User_Input_Settings
*/
private $user_input_settings = null;
/**
* Transients object.
*
* @since 1.0.0
*
* @var Transients
*/
private $transients = null;
/**
* OAuth client object.
*
* @since 1.0.0
*
* @var Clients\OAuth_Client
*/
private $auth_client = null;
/**
* OAuth credentials instance.
*
* @since 1.0.0
* @var Credentials
*/
protected $credentials;
/**
* Verification instance.
*
* @since 1.0.0
* @var Verification
*/
protected $verification;
/**
* Verification meta instance.
*
* @since 1.1.0
* @var Verification_Meta
*/
protected $verification_meta;
/**
* Verification file instance.
*
* @since 1.1.0
* @var Verification_File
*/
protected $verification_file;
/**
* Profile instance.
*
* @since 1.0.0
* @var Profile
*/
protected $profile;
/**
* Token instance.
*
* @since 1.39.0
* @var Token
*/
protected $token;
/**
* Owner_ID instance.
*
* @since 1.16.0
* @var Owner_ID
*/
protected $owner_id;
/**
* Has_Connected_Admins instance.
*
* @since 1.14.0
* @var Has_Connected_Admins
*/
protected $has_connected_admins;
/**
* Has_Multiple_Admins instance.
*
* @since 1.29.0
* @var Has_Multiple_Admins
*/
protected $has_multiple_admins;
/**
* Connected_Proxy_URL instance.
*
* @since 1.17.0
* @var Connected_Proxy_URL
*/
protected $connected_proxy_url;
/**
* Disconnected_Reason instance.
*
* @since 1.17.0
* @var Disconnected_Reason
*/
protected $disconnected_reason;
/**
* Google_Proxy instance.
*
* @since 1.1.2
* @var Google_Proxy
*/
protected $google_proxy;
/**
* Initial_Version instance.
*
* @since 1.25.0
* @var Initial_Version
*/
protected $initial_version;
/**
* Flag set when site fields are synchronized during the current request.
*
* @var bool
*/
private $did_sync_fields;
/**
* Constructor.
*
* @since 1.0.0
*
* @param Context $context Plugin context.
* @param Options $options Optional. Option API instance. Default is a new instance.
* @param User_Options $user_options Optional. User Option API instance. Default is a new instance.
* @param Transients $transients Optional. Transient API instance. Default is a new instance.
*/
public function __construct(
Context $context,
Options $options = null,
User_Options $user_options = null,
Transients $transients = null
) {
$this->context = $context;
$this->options = $options ?: new Options( $this->context );
$this->user_options = $user_options ?: new User_Options( $this->context );
$this->transients = $transients ?: new Transients( $this->context );
$this->user_input_state = new User_Input_State( $this->user_options );
$this->user_input_settings = new User_Input_Settings( $context, $this, $transients );
$this->google_proxy = new Google_Proxy( $this->context );
$this->credentials = new Credentials( new Encrypted_Options( $this->options ) );
$this->verification = new Verification( $this->user_options );
$this->verification_meta = new Verification_Meta( $this->user_options );
$this->verification_file = new Verification_File( $this->user_options );
$this->profile = new Profile( $this->user_options );
$this->token = new Token( $this->user_options );
$this->owner_id = new Owner_ID( $this->options );
$this->has_connected_admins = new Has_Connected_Admins( $this->options, $this->user_options );
$this->has_multiple_admins = new Has_Multiple_Admins( $this->transients );
$this->connected_proxy_url = new Connected_Proxy_URL( $this->options );
$this->disconnected_reason = new Disconnected_Reason( $this->user_options );
$this->initial_version = new Initial_Version( $this->user_options );
}
/**
* Registers functionality through WordPress hooks.
*
* @since 1.0.0
*/
public function register() {
$this->credentials()->register();
$this->verification()->register();
$this->verification_file()->register();
$this->verification_meta()->register();
$this->has_connected_admins->register();
$this->owner_id->register();
$this->connected_proxy_url->register();
$this->disconnected_reason->register();
$this->user_input_state->register();
$this->initial_version->register();
add_filter( 'allowed_redirect_hosts', $this->get_method_proxy( 'allowed_redirect_hosts' ) );
add_filter( 'googlesitekit_admin_data', $this->get_method_proxy( 'inline_js_admin_data' ) );
add_filter( 'googlesitekit_admin_notices', $this->get_method_proxy( 'authentication_admin_notices' ) );
add_filter( 'googlesitekit_inline_base_data', $this->get_method_proxy( 'inline_js_base_data' ) );
add_filter( 'googlesitekit_setup_data', $this->get_method_proxy( 'inline_js_setup_data' ) );
add_filter( 'googlesitekit_is_feature_enabled', $this->get_method_proxy( 'filter_features_via_proxy' ), 10, 2 );
add_action( 'admin_init', $this->get_method_proxy( 'handle_oauth' ) );
add_action( 'admin_init', $this->get_method_proxy( 'check_connected_proxy_url' ) );
add_action( 'admin_init', $this->get_method_proxy( 'verify_user_input_settings' ) );
add_action(
'admin_init',
function() {
if (
'googlesitekit-dashboard' === $this->context->input()->filter( INPUT_GET, 'page', FILTER_SANITIZE_STRING )
&& User_Input_State::VALUE_REQUIRED === $this->user_input_state->get()
) {
wp_safe_redirect( $this->context->admin_url( 'user-input' ) );
exit;
}
}
);
add_action( 'admin_action_' . self::ACTION_CONNECT, $this->get_method_proxy( 'handle_connect' ) );
add_action( 'admin_action_' . self::ACTION_DISCONNECT, $this->get_method_proxy( 'handle_disconnect' ) );
// Google_Proxy::ACTION_SETUP is called from the proxy as an intermediate step.
add_action( 'admin_action_' . Google_Proxy::ACTION_SETUP, $this->get_method_proxy( 'verify_proxy_setup_nonce' ), -1 );
// Google_Proxy::ACTION_SETUP is called from Site Kit to redirect to the proxy initially.
add_action( 'admin_action_' . Google_Proxy::ACTION_SETUP, $this->get_method_proxy( 'handle_sync_site_fields' ), 5 );
add_action(
'admin_action_' . Google_Proxy::ACTION_SETUP,
function () {
$code = $this->context->input()->filter( INPUT_GET, 'googlesitekit_code', FILTER_SANITIZE_STRING );
$site_code = $this->context->input()->filter( INPUT_GET, 'googlesitekit_site_code', FILTER_SANITIZE_STRING );
$this->handle_site_code( $code, $site_code );
$this->redirect_to_proxy( $code );
}
);
add_action(
'admin_action_' . Google_Proxy::ACTION_PERMISSIONS,
function () {
$this->handle_proxy_permissions();
}
);
add_action(
'googlesitekit_authorize_user',
function ( $token_response, $scopes, $previous_scopes ) {
if ( ! $this->credentials->using_proxy() ) {
return;
}
$this->set_connected_proxy_url();
if ( empty( $previous_scopes ) ) {
$this->require_user_input();
}
},
10,
3
);
add_filter(
'googlesitekit_rest_routes',
function( $routes ) {
return array_merge( $routes, $this->get_rest_routes() );
}
);
add_filter(
'googlesitekit_apifetch_preload_paths',
function( $routes ) {
$authentication_routes = array(
'/' . REST_Routes::REST_ROOT . '/core/site/data/connection',
'/' . REST_Routes::REST_ROOT . '/core/user/data/authentication',
);
return array_merge( $routes, $authentication_routes );
}
);
add_filter(
'googlesitekit_user_data',
function( $user ) {
if ( $this->profile->has() ) {
$profile_data = $this->profile->get();
$user['user']['email'] = $profile_data['email'];
$user['user']['picture'] = $profile_data['photo'];
}
$user['connectURL'] = esc_url_raw( $this->get_connect_url() );
$user['initialVersion'] = $this->initial_version->get();
$user['userInputState'] = $this->user_input_state->get();
$user['verified'] = $this->verification->has();
return $user;
}
);
// Synchronize site fields on shutdown when select options change.
$option_updated = function () {
$sync_site_fields = function () {
if ( $this->did_sync_fields ) {
return;
}
// This method should run no more than once per request.
$this->did_sync_fields = true;
if ( $this->credentials->using_proxy() ) {
$this->google_proxy->sync_site_fields( $this->credentials() );
}
};
add_action( 'shutdown', $sync_site_fields );
};
add_action( 'update_option_blogname', $option_updated );
add_action( 'update_option_googlesitekit_db_version', $option_updated );
add_action(
OAuth_Client::CRON_REFRESH_PROFILE_DATA,
function ( $user_id ) {
$this->cron_refresh_profile_data( $user_id );
}
);
// If no initial version set for the current user, set it when getting a new access token.
if ( ! $this->initial_version->get() ) {
$set_initial_version = function() {
$this->initial_version->set( GOOGLESITEKIT_VERSION );
};
add_action( 'googlesitekit_authorize_user', $set_initial_version );
add_action( 'googlesitekit_reauthorize_user', $set_initial_version );
}
$maybe_refresh_token_for_screen = function( $screen_id ) {
if ( 'dashboard' !== $screen_id && 'toplevel_page_googlesitekit-dashboard' !== $screen_id ) {
return;
}
if ( ! current_user_can( Permissions::AUTHENTICATE ) || ! $this->credentials()->has() ) {
return;
}
$token = $this->token->get();
// Do nothing if the token is not set.
if ( empty( $token['created'] ) || empty( $token['expires_in'] ) ) {
return;
}
// Do nothing if the token expires in more than 5 minutes.
if ( $token['created'] + $token['expires_in'] > time() + 5 * MINUTE_IN_SECONDS ) {
return;
}
$this->get_oauth_client()->refresh_token();
};
add_action(
'current_screen',
function( $current_screen ) use ( $maybe_refresh_token_for_screen ) {
$maybe_refresh_token_for_screen( $current_screen->id );
}
);
add_action(
'heartbeat_tick',
function() use ( $maybe_refresh_token_for_screen ) {
$maybe_refresh_token_for_screen( $this->context->input()->filter( INPUT_POST, 'screen_id' ) );
}
);
}
/**
* Gets the OAuth credentials object.
*
* @since 1.0.0
*
* @return Credentials Credentials instance.
*/
public function credentials() {
return $this->credentials;
}
/**
* Gets the verification instance.
*
* @since 1.0.0
*
* @return Verification Verification instance.
*/
public function verification() {
return $this->verification;
}
/**
* Gets the verification tag instance.
*
* @since 1.0.0
* @deprecated 1.1.0
*
* @return Verification_Meta Verification tag instance.
*/
public function verification_tag() {
_deprecated_function( __METHOD__, '1.1.0', __CLASS__ . '::verification_meta()' );
return $this->verification_meta;
}
/**
* Gets the verification meta instance.
*
* @since 1.1.0
*
* @return Verification_Meta Verification tag instance.
*/
public function verification_meta() {
return $this->verification_meta;
}
/**
* Gets the verification file instance.
*
* @since 1.1.0
*
* @return Verification_File Verification file instance.
*/
public function verification_file() {
return $this->verification_file;
}
/**
* Gets the Profile instance.
*
* @since 1.0.0
*
* @return Profile Profile instance.
*/
public function profile() {
return $this->profile;
}
/**
* Gets the Token instance.
*
* @since 1.39.0
*
* @return Token Token instance.
*/
public function token() {
return $this->token;
}
/**
* Gets the OAuth client instance.
*
* @since 1.0.0
*
* @return Clients\OAuth_Client OAuth client instance.
*/
public function get_oauth_client() {
if ( ! $this->auth_client instanceof OAuth_Client ) {
$this->auth_client = new OAuth_Client(
$this->context,
$this->options,
$this->user_options,
$this->credentials,
$this->google_proxy,
$this->profile,
$this->token
);
}
return $this->auth_client;
}
/**
* Gets the Google Proxy instance.
*
* @since 1.19.0
*
* @return Google_Proxy An instance of Google Proxy.
*/
public function get_google_proxy() {
return $this->google_proxy;
}
/**
* Gets the User Input State instance.
*
* @since 1.21.0
*
* @return User_Input_State An instance of the User_Input_State class.
*/
public function get_user_input_state() {
return $this->user_input_state;
}
/**
* Revokes authentication along with user options settings.
*
* @since 1.0.0
*/
public function disconnect() {
global $wpdb;
// Revoke token via API call.
$this->get_oauth_client()->revoke_token();
// Delete all user data.
$user_id = $this->user_options->get_user_id();
$prefix = $this->user_options->get_meta_key( 'googlesitekit\_%' );
// Reset Has_Connected_Admins setting.
$this->has_connected_admins->delete();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->query(
$wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key LIKE %s", $user_id, $prefix )
);
wp_cache_delete( $user_id, 'user_meta' );
}
/**
* Gets the URL for connecting to Site Kit.
*
* @since 1.0.0
* @since 1.32.0 Updated to use dedicated action URL.
*
* @return string Connect URL.
*/
public function get_connect_url() {
return add_query_arg(
array(
'action' => self::ACTION_CONNECT,
'nonce' => wp_create_nonce( self::ACTION_CONNECT ),
),
admin_url( 'index.php' )
);
}
/**
* Gets the URL for disconnecting from Site Kit.
*
* @since 1.0.0
* @since 1.32.0 Updated to use dedicated action URL.
*
* @return string Disconnect URL.
*/
public function get_disconnect_url() {
return add_query_arg(
array(
'action' => self::ACTION_DISCONNECT,
'nonce' => wp_create_nonce( self::ACTION_DISCONNECT ),
),
admin_url( 'index.php' )
);
}
/**
* Check if the current user is authenticated.
*
* @since 1.0.0
*
* @return boolean True if the user is authenticated, false otherwise.
*/
public function is_authenticated() {
return $this->token->has();
}
/**
* Checks whether the Site Kit setup is considered complete.
*
* If this is not the case, most permissions will be force-prevented to ensure that only permissions required for
* initial setup are granted.
*
* @since 1.0.0
* @since 1.7.0 Moved from `Permissions` class.
*
* @return bool True if setup is completed, false otherwise.
*/
public function is_setup_completed() {
if ( ! $this->credentials->has() ) {
return false;
}
/**
* Filters whether the Site Kit plugin should consider its setup to be completed.
*
* This can be used by essential auto-activated modules to amend the result of this check.
*
* @since 1.0.0
*
* @param bool $complete Whether the setup is completed.
*/
return (bool) apply_filters( 'googlesitekit_setup_complete', true );
}
/**
* Refreshes user profile data in the background.
*
* @since 1.13.0
*
* @param int $user_id User ID to refresh profile data for.
*/
private function cron_refresh_profile_data( $user_id ) {
$original_user_id = $this->user_options->get_user_id();
$this->user_options->switch_user( $user_id );
if ( $this->is_authenticated() ) {
$this->get_oauth_client()->refresh_profile_data( 30 * MINUTE_IN_SECONDS );
}
$this->user_options->switch_user( $original_user_id );
}
/**
* Handles receiving a temporary OAuth code.
*
* @since 1.0.0
* @since 1.32.0 Moved connect and disconnect actions to dedicated handlers.
*/
private function handle_oauth() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
// Handles Direct OAuth client request.
if ( $this->context->input()->filter( INPUT_GET, 'oauth2callback' ) ) {
if ( ! current_user_can( Permissions::AUTHENTICATE ) ) {
wp_die( esc_html__( 'You don\'t have permissions to authenticate with Site Kit.', 'google-site-kit' ), 403 );
}
$this->get_oauth_client()->authorize_user();
}
}
/**
* Handles request to connect via oAuth.
*
* @since 1.32.0
*/
private function handle_connect() {
$input = $this->context->input();
$nonce = $input->filter( INPUT_GET, 'nonce' );
if ( ! wp_verify_nonce( $nonce, self::ACTION_CONNECT ) ) {
self::invalid_nonce_error( self::ACTION_CONNECT );
}
if ( ! current_user_can( Permissions::AUTHENTICATE ) ) {
wp_die( esc_html__( 'You don\'t have permissions to authenticate with Site Kit.', 'google-site-kit' ), 403 );
}
$redirect_url = $input->filter( INPUT_GET, 'redirect', FILTER_VALIDATE_URL );
if ( $redirect_url ) {
$redirect_url = esc_url_raw( wp_unslash( $redirect_url ) );
}
// User is trying to authenticate, but access token hasn't been set.
$additional_scopes = $input->filter( INPUT_GET, 'additional_scopes', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
wp_safe_redirect(
$this->get_oauth_client()->get_authentication_url( $redirect_url, $additional_scopes )
);
exit();
}
/**
* Handles request to disconnect via oAuth.
*
* @since 1.32.0
*/
private function handle_disconnect() {
$nonce = $this->context->input()->filter( INPUT_GET, 'nonce' );
if ( ! wp_verify_nonce( $nonce, self::ACTION_DISCONNECT ) ) {
self::invalid_nonce_error( self::ACTION_DISCONNECT );
}
if ( ! current_user_can( Permissions::AUTHENTICATE ) ) {
wp_die( esc_html__( 'You don\'t have permissions to authenticate with Site Kit.', 'google-site-kit' ), 403 );
}
$this->disconnect();
$redirect_url = $this->context->admin_url(
'splash',
array(
'googlesitekit_reset_session' => 1,
)
);
wp_safe_redirect( $redirect_url );
exit();
}
/**
* Modifies the base data to pass to JS.
*
* @since 1.2.0
*
* @param array $data Inline JS data.
* @return array Filtered $data.
*/
private function inline_js_base_data( $data ) {
$data['isOwner'] = $this->owner_id->get() === get_current_user_id();
$data['isFirstAdmin'] = $data['isOwner'] || ( ! $this->owner_id->get() && current_user_can( Permissions::MANAGE_OPTIONS ) );
$data['splashURL'] = esc_url_raw( $this->context->admin_url( 'splash' ) );
$data['proxySetupURL'] = '';
$data['proxyPermissionsURL'] = '';
$data['usingProxy'] = false;
if ( $this->credentials->using_proxy() ) {
$auth_client = $this->get_oauth_client();
$data['proxySetupURL'] = esc_url_raw( $this->get_proxy_setup_url() );
$data['proxyPermissionsURL'] = esc_url_raw( $this->get_proxy_permissions_url() );
$data['usingProxy'] = true;
}
$version = get_bloginfo( 'version' );
list( $major, $minor ) = explode( '.', $version );
$data['wpVersion'] = array(
'version' => $version,
'major' => (int) $major,
'minor' => (int) $minor,
);
return $data;
}
/**
* Modifies the admin data to pass to JS.
*
* @since 1.0.0
*
* @param array $data Inline JS data.
* @return array Filtered $data.
*/
private function inline_js_admin_data( $data ) {
$data['connectURL'] = esc_url_raw( $this->get_connect_url() );
$data['disconnectURL'] = esc_url_raw( $this->get_disconnect_url() );
return $data;
}
/**
* Modifies the setup data to pass to JS.
*
* @since 1.0.0
*
* @param array $data Inline JS data.
* @return array Filtered $data.
*/
private function inline_js_setup_data( $data ) {
$auth_client = $this->get_oauth_client();
$is_authenticated = $this->is_authenticated();
$data['isSiteKitConnected'] = $this->credentials->has();
$data['isResettable'] = $this->options->has( Credentials::OPTION );
$data['isAuthenticated'] = $is_authenticated;
$data['requiredScopes'] = $auth_client->get_required_scopes();
$data['grantedScopes'] = $is_authenticated ? $auth_client->get_granted_scopes() : array();
$data['unsatisfiedScopes'] = $is_authenticated ? $auth_client->get_unsatisfied_scopes() : array();
$data['needReauthenticate'] = $auth_client->needs_reauthentication();
if ( $this->credentials->using_proxy() ) {
$error_code = $this->user_options->get( OAuth_Client::OPTION_ERROR_CODE );
if ( ! empty( $error_code ) ) {
$data['errorMessage'] = $auth_client->get_error_message( $error_code );
}
}
// All admins need to go through site verification process.
if ( current_user_can( Permissions::MANAGE_OPTIONS ) ) {
$data['isVerified'] = $this->verification->has();
} else {
$data['isVerified'] = false;
}
// The actual data for this is passed in from the Search Console module.
if ( ! isset( $data['hasSearchConsoleProperty'] ) ) {
$data['hasSearchConsoleProperty'] = false;
}
return $data;
}
/**
* Add allowed redirect host to safe wp_safe_redirect
*
* @since 1.0.0
*
* @param array $hosts Array of safe hosts to redirect to.
*
* @return array
*/
private function allowed_redirect_hosts( $hosts ) {
$hosts[] = 'accounts.google.com';
$hosts[] = wp_parse_url( $this->google_proxy->url(), PHP_URL_HOST );
return $hosts;
}
/**
* Gets related REST routes.
*
* @since 1.3.0
*
* @return array List of REST_Route objects.
*/
private function get_rest_routes() {
$can_setup = function() {
return current_user_can( Permissions::SETUP );
};
$can_authenticate = function() {
return current_user_can( Permissions::AUTHENTICATE );
};
return array(
new REST_Route(
'core/site/data/connection',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function( WP_REST_Request $request ) {
$data = array(
'connected' => $this->credentials->has(),
'resettable' => $this->options->has( Credentials::OPTION ),
'setupCompleted' => $this->is_setup_completed(),
'hasConnectedAdmins' => $this->has_connected_admins->get(),
'hasMultipleAdmins' => $this->has_multiple_admins->get(),
'ownerID' => $this->owner_id->get(),
);
return new WP_REST_Response( $data );
},
'permission_callback' => $can_setup,
),
)
),
new REST_Route(
'core/user/data/authentication',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function( WP_REST_Request $request ) {
$oauth_client = $this->get_oauth_client();
$is_authenticated = $this->is_authenticated();
$data = array(
'authenticated' => $is_authenticated,
'requiredScopes' => $oauth_client->get_required_scopes(),
'grantedScopes' => $is_authenticated ? $oauth_client->get_granted_scopes() : array(),
'unsatisfiedScopes' => $is_authenticated ? $oauth_client->get_unsatisfied_scopes() : array(),
'needsReauthentication' => $oauth_client->needs_reauthentication(),
'disconnectedReason' => $this->disconnected_reason->get(),
);
return new WP_REST_Response( $data );
},
'permission_callback' => $can_authenticate,
),
)
),
new REST_Route(
'core/user/data/disconnect',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => function( WP_REST_Request $request ) {
$this->disconnect();
return new WP_REST_Response( true );
},
'permission_callback' => $can_authenticate,
),
)
),
);
}
/**
* Shows admin notification for authentication related issues.
*
* @since 1.0.0
*
* @param array $notices Array of admin notices.
*
* @return array Array of admin notices.
*/
private function authentication_admin_notices( $notices ) {
// Only include notices if in the correct admin panel.
if ( $this->context->is_network_mode() !== is_network_admin() ) {
return $notices;
}
$notices[] = $this->get_reauthentication_needed_notice();
$notices[] = $this->get_authentication_oauth_error_notice();
$notices[] = $this->get_reconnect_after_url_mismatch_notice();
return $notices;
}
/**
* Gets reconnect notice.
*
* @since 1.17.0
*
* @return Notice Notice object.
*/
private function get_reconnect_after_url_mismatch_notice() {
return new Notice(
'reconnect_after_url_mismatch',
array(
'content' => function() {
return sprintf(
'<p>%s <a href="%s">%s</a></p>',
esc_html__( 'Looks like the URL of your site has changed. In order to continue using Site Kit, you’ll need to reconnect, so that your plugin settings are updated with the new URL.', 'google-site-kit' ),
esc_url( $this->get_proxy_setup_url() ),
esc_html__( 'Reconnect', 'google-site-kit' )
);
},
'type' => Notice::TYPE_INFO,
'active_callback' => function() {
return $this->disconnected_reason->get() === Disconnected_Reason::REASON_CONNECTED_URL_MISMATCH
&& $this->credentials->has();
},
)
);
}