-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug_Data.php
More file actions
685 lines (616 loc) · 18.5 KB
/
Copy pathDebug_Data.php
File metadata and controls
685 lines (616 loc) · 18.5 KB
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
<?php
/**
* Class Google\Site_Kit\Core\Site_Health\Debug_Data
*
* @package Google\Site_Kit\Core\Util
* @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\Site_Health;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Authentication\Authentication;
use Google\Site_Kit\Core\Authentication\Clients\OAuth_Client;
use Google\Site_Kit\Core\Conversion_Tracking\Conversion_Tracking;
use Google\Site_Kit\Core\Key_Metrics\Key_Metrics_Settings;
use Google\Site_Kit\Core\Key_Metrics\Key_Metrics_Setup_Completed_By;
use Google\Site_Kit\Core\Modules\Module;
use Google\Site_Kit\Core\Modules\Module_With_Debug_Fields;
use Google\Site_Kit\Core\Modules\Modules;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Util\Feature_Flags;
use Google\Site_Kit\Core\Util\Scopes;
/**
* Class for integrating debug information with Site Health.
*
* @since 1.5.0
* @access private
* @ignore
*/
class Debug_Data {
/**
* Context instance.
*
* @since 1.5.0
* @var Context
*/
private $context;
/**
* Options instance.
*
* @since 1.5.0
* @var Options
*/
private $options;
/**
* User_Options instance.
*
* @since 1.5.0
* @var User_Options
*/
private $user_options;
/**
* Authentication instance.
*
* @since 1.5.0
* @var Authentication
*/
private $authentication;
/**
* Modules instance.
*
* @since 1.5.0
* @var Modules
*/
private $modules;
/**
* Permissions instance.
*
* @since 1.69.0
* @var Permissions
*/
private $permissions;
/**
* Constructor.
*
* @since 1.5.0
*
* @param Context $context Context instance.
* @param Options $options Options instance.
* @param User_Options $user_options User_Options instance.
* @param Authentication $authentication Authentication instance.
* @param Modules $modules Modules instance.
* @param Permissions $permissions Permissions instance.
*/
public function __construct(
Context $context,
Options $options,
User_Options $user_options,
Authentication $authentication,
Modules $modules,
Permissions $permissions
) {
$this->context = $context;
$this->options = $options;
$this->user_options = $user_options;
$this->authentication = $authentication;
$this->modules = $modules;
$this->permissions = $permissions;
}
/**
* Registers debug information with Site Health.
*
* @since 1.5.0
*/
public function register() {
add_filter(
'debug_information',
function ( $info ) {
$info['google-site-kit'] = array(
'label' => __( 'Site Kit by Google', 'google-site-kit' ),
'fields' => $this->get_fields(),
);
return $info;
}
);
}
/**
* Redacts the given string by overwriting a portion with a mask character.
*
* @since 1.5.0
*
* @param string $input_string Input string to redact.
* @param int $mask_start Starting position of redaction and length of preserved characters.
* If positive, characters are redacted from the end, preserving the first X characters.
* If negative, characters are redacted from the beginning preserving the last X characters.
* @return string
*/
public static function redact_debug_value( $input_string, $mask_start = 4 ) {
if ( ! is_scalar( $input_string ) ) {
return '';
}
$input_string = (string) $input_string;
if ( $mask_start < 0 ) {
$redacted = substr( $input_string, 0, $mask_start );
$unmasked = substr( $input_string, $mask_start );
return str_repeat( '•', strlen( $redacted ) ) . $unmasked;
} else {
$redacted = substr( $input_string, $mask_start );
$unmasked = substr( $input_string, 0, $mask_start );
return $unmasked . str_repeat( '•', strlen( $redacted ) );
}
}
/**
* Gets all fields.
*
* @since 1.5.0
*
* @return array
*/
protected function get_fields() {
$fields = array(
'version' => array(
'label' => __( 'Version', 'google-site-kit' ),
'value' => GOOGLESITEKIT_VERSION,
),
'php_version' => array(
'label' => __( 'PHP Version', 'google-site-kit' ),
'value' => PHP_VERSION,
),
'wp_version' => array(
'label' => __( 'WordPress Version', 'google-site-kit' ),
'value' => get_bloginfo( 'version' ),
),
'reference_url' => array(
'label' => __( 'Reference Site URL', 'google-site-kit' ),
'value' => $this->context->get_reference_site_url(),
),
'amp_mode' => $this->get_amp_mode_field(),
'site_status' => $this->get_site_status_field(),
'user_status' => $this->get_user_status_field(),
'verification_status' => $this->get_verification_status_field(),
'connected_user_count' => $this->get_connected_user_count_field(),
'active_modules' => $this->get_active_modules_field(),
'recoverable_modules' => $this->get_recoverable_modules_field(),
'required_scopes' => $this->get_required_scopes_field(),
'capabilities' => $this->get_capabilities_field(),
'enabled_features' => $this->get_feature_fields(),
);
$fields = array_merge( $fields, $this->get_active_conversion_event_provider_fields() );
$fields = array_merge( $fields, $this->get_consent_mode_fields() );
$fields = array_merge( $fields, $this->get_module_sharing_settings_fields() );
$fields = array_merge( $fields, $this->get_key_metrics_fields() );
$fields = array_filter(
array_merge(
$fields,
$this->get_module_fields()
)
);
$none = __( 'None', 'google-site-kit' );
return array_map(
function ( $field ) use ( $none ) {
if ( empty( $field['value'] ) ) {
$field['value'] = $none;
$field['debug'] = 'none';
}
return $field;
},
$fields
);
}
/**
* Gets the field definition for the amp_mode field.
*
* @since 1.5.0
*
* @return array
*/
private function get_amp_mode_field() {
$mode = $this->context->get_amp_mode();
$mode_map = array(
'primary' => __( 'Primary', 'google-site-kit' ),
'secondary' => __( 'Secondary', 'google-site-kit' ),
);
return array(
'label' => __( 'AMP Mode', 'google-site-kit' ),
'value' => isset( $mode_map[ $mode ] ) ? $mode_map[ $mode ] : __( 'No', 'google-site-kit' ),
'debug' => isset( $mode_map[ $mode ] ) ? $mode : 'no',
);
}
/**
* Gets the field definition for the site_status field.
*
* @since 1.5.0
*
* @return array
*/
private function get_site_status_field() {
$is_connected = $this->authentication->credentials()->has();
$using_proxy = $this->authentication->credentials()->using_proxy();
$status_map = array(
'connected-site' => __( 'Connected through site credentials', 'google-site-kit' ),
'connected-oauth' => __( 'Connected through OAuth client credentials', 'google-site-kit' ),
'not-connected' => __( 'Not connected', 'google-site-kit' ),
);
if ( $is_connected && $using_proxy ) {
$status = 'connected-site';
} elseif ( $is_connected && ! $using_proxy ) {
$status = 'connected-oauth';
} else {
$status = 'not-connected';
}
return array(
'label' => __( 'Site Status', 'google-site-kit' ),
'value' => $status_map[ $status ],
'debug' => $status,
);
}
/**
* Gets the field definition for the user_status field.
*
* @since 1.5.0
*
* @return array
*/
private function get_user_status_field() {
$is_connected = $this->authentication->is_authenticated();
return array(
'label' => __( 'User Status', 'google-site-kit' ),
'value' => $is_connected
? __( 'Authenticated', 'google-site-kit' )
: __( 'Not authenticated', 'google-site-kit' ),
'debug' => $is_connected ? 'authenticated' : 'not authenticated',
);
}
/**
* Gets the field definition for the verification_status field.
*
* @since 1.37.0
*
* @return array
*/
private function get_verification_status_field() {
$label = __( 'Verification Status', 'google-site-kit' );
$is_verified = $this->authentication->verification()->get();
$is_verified_by_file_token = $this->authentication->verification_file()->get();
$is_verified_by_meta_tag = $this->authentication->verification_meta()->get();
if ( ! $is_verified ) {
return array(
'label' => $label,
'value' => __( 'Not verified', 'google-site-kit' ),
'debug' => 'not-verified',
);
}
if ( $is_verified_by_file_token ) {
return array(
'label' => $label,
'value' => __( 'Verified through file', 'google-site-kit' ),
'debug' => 'verified-file',
);
}
if ( $is_verified_by_meta_tag ) {
return array(
'label' => $label,
'value' => __( 'Verified through meta tag', 'google-site-kit' ),
'debug' => 'verified-meta',
);
}
return array(
'label' => $label,
'value' => __( 'Verified outside of Site Kit', 'google-site-kit' ),
'debug' => 'verified-non-site-kit',
);
}
/**
* Gets the number of users with a Site Kit token.
*
* @since 1.16.0
*
* @return array
*/
private function get_connected_user_count_field() {
$users = new \WP_User_Query(
array(
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_key' => $this->user_options->get_meta_key( OAuth_Client::OPTION_ACCESS_TOKEN ),
'fields' => 'ID',
'compare' => 'EXISTS',
)
);
return array(
'label' => __( 'Connected user count', 'google-site-kit' ),
'value' => $users->get_total(),
);
}
/**
* Gets the field definition for the active_modules field.
*
* @since 1.5.0
*
* @return array
*/
private function get_active_modules_field() {
$active_modules = $this->modules->get_active_modules();
return array(
'label' => __( 'Active Modules', 'google-site-kit' ),
'value' => join(
/* translators: used between list items, there is a space after the comma. */
__( ', ', 'google-site-kit' ),
wp_list_pluck( $active_modules, 'name' )
),
'debug' => join( ', ', wp_list_pluck( $active_modules, 'slug' ) ),
);
}
/**
* Gets the field definition for the recoverable_modules field.
*
* @since 1.78.0
*
* @return array
*/
private function get_recoverable_modules_field() {
$recoverable_modules = $this->modules->get_recoverable_modules();
return array(
'label' => __( 'Recoverable Modules', 'google-site-kit' ),
'value' => join(
/* translators: used between list items, there is a space after the comma. */
__( ', ', 'google-site-kit' ),
wp_list_pluck( $recoverable_modules, 'name' )
),
'debug' => join( ', ', wp_list_pluck( $recoverable_modules, 'slug' ) ),
);
}
/**
* Gets the field definition for the module_sharing_settings field.
*
* @since 1.78.0
*
* @return array
*/
private function get_module_sharing_settings_fields() {
$sharing_settings = $this->modules->get_module_sharing_settings();
$fields = array();
foreach ( $this->modules->get_shareable_modules() as $module_slug => $module ) {
$module_settings = $sharing_settings->get_module( $module_slug );
$fields[ "{$module_slug}_shared_roles" ] = array_merge(
array(
/* translators: %s: module name */
'label' => sprintf( __( '%s: Shared Roles', 'google-site-kit' ), $module->name ),
),
$this->get_module_shared_role_names( $module_settings['sharedRoles'] )
);
$fields[ "{$module_slug}_management" ] = array_merge(
array(
/* translators: %s: module name */
'label' => sprintf( __( '%s: Management', 'google-site-kit' ), $module->name ),
),
$this->get_module_management( $module_settings['management'] )
);
}
return $fields;
}
/**
* Gets the comma separated list of shared role names for module_sharing_settings.
*
* @since 1.78.0
*
* @param array $role_slugs List of role slugs.
*
* @return array $role_names Comma separated list of role names for module_sharing_settings within value and debug keys.
*/
private function get_module_shared_role_names( $role_slugs ) {
if ( ! $role_slugs ) {
return array(
'value' => __( 'None', 'google-site-kit' ),
'debug' => 'none',
);
}
$wp_role_names = wp_roles()->get_names();
$shared_role_names = array_filter(
$wp_role_names,
function ( $key ) use ( $role_slugs ) {
return in_array( $key, $role_slugs, true );
},
ARRAY_FILTER_USE_KEY
);
return array(
'value' => join(
/* translators: used between list items, there is a space after the comma. */
__( ', ', 'google-site-kit' ),
$shared_role_names
),
'debug' => join( ', ', $role_slugs ),
);
}
/**
* Gets the user friendly and debug values for module management used in module_sharing_settings.
*
* @since 1.78.0
*
* @param string $management The module sharing settings management value. Can be either `owner` or `all_admins`.
*
* @return array User friendly and debug values for module management used in module_sharing_settings within value and debug keys.
*/
private function get_module_management( $management ) {
switch ( $management ) {
case 'all_admins':
return array(
'value' => __( 'Any admin signed in with Google', 'google-site-kit' ),
'debug' => 'all_admins',
);
default:
return array(
'value' => __( 'Owner', 'google-site-kit' ),
'debug' => 'owner',
);
}
}
/**
* Gets the field definition for the required_scopes field.
*
* @since 1.5.0
*
* @return array
*/
private function get_required_scopes_field() {
$required_scopes = $this->authentication->get_oauth_client()->get_required_scopes();
$granted_scopes = $this->authentication->get_oauth_client()->get_granted_scopes();
$value = array();
foreach ( $required_scopes as $scope ) {
$satisfied = Scopes::is_satisfied_by( $scope, $granted_scopes );
$value[ $scope ] = $satisfied ? '✅' : '⭕';
}
return array(
'label' => __( 'Required scopes', 'google-site-kit' ),
'value' => $value,
);
}
/**
* Gets capabilities for the current user.
*
* @since 1.21.0
*
* @return array
*/
private function get_capabilities_field() {
$value = array();
foreach ( $this->permissions->check_all_for_current_user() as $permission => $granted ) {
$value[ $permission ] = $granted ? '✅' : '⭕';
}
return array(
'label' => __( 'User Capabilities', 'google-site-kit' ),
'value' => $value,
);
}
/**
* Gets field definitions for each active module that supports debug fields.
*
* @since 1.5.0
*
* @return array A flat array of all module debug fields.
*/
private function get_module_fields() {
$modules_with_debug_fields = array_filter(
$this->modules->get_active_modules(),
function ( Module $module ) {
return $module instanceof Module_With_Debug_Fields;
}
);
$fields_by_module = array_map(
function ( Module_With_Debug_Fields $module ) {
return $module->get_debug_fields();
},
array_values( $modules_with_debug_fields )
);
return array_merge( array(), ...$fields_by_module );
}
/**
* Gets the available features.
*
* @since 1.26.0
*
* @return array
*/
private function get_feature_fields() {
$value = array();
$available_features = Feature_Flags::get_available_features();
foreach ( $available_features as $available_feature ) {
$enabled_feature = Feature_Flags::enabled( $available_feature );
$value[ $available_feature ] = $enabled_feature ? '✅' : '⭕';
}
return array(
'label' => __( 'Features', 'google-site-kit' ),
'value' => $value,
);
}
/**
* Gets the consent mode fields.
*
* @since 1.125.0
*
* @return array
*/
private function get_consent_mode_fields() {
/**
* Filters the status of consent mode in Site Kit.
*
* @since 1.125.0
*
* @param string $status The consent mode status. Default: 'disabled'.
*/
$consent_mode_status = apply_filters( 'googlesitekit_consent_mode_status', 'disabled' );
$consent_api_active = function_exists( 'wp_set_consent' );
return array(
'consent_mode' => array(
'label' => __( 'Consent Mode', 'google-site-kit' ),
'value' => 'enabled' === $consent_mode_status ? __( 'Enabled', 'google-site-kit' ) : __( 'Disabled', 'google-site-kit' ),
'debug' => $consent_mode_status,
),
'consent_api' => array(
'label' => __( 'WP Consent API', 'google-site-kit' ),
'value' => $consent_api_active ? __( 'Detected', 'google-site-kit' ) : __( 'Not detected', 'google-site-kit' ),
'debug' => $consent_api_active ? 'detected' : 'not-detected',
),
);
}
/**
* Gets the conversion event names registered by the currently supported
* active plugins.
*
* @since 1.127.0
*
* @return array
*/
private function get_active_conversion_event_provider_fields() {
$value = array();
$conversion_tracking = new Conversion_Tracking( $this->context );
$active_providers = $conversion_tracking->get_active_providers();
foreach ( $active_providers as $active_provider_slug => $active_provider ) {
$value[ $active_provider_slug ] = implode( ', ', $active_provider->get_event_names() );
}
return array(
'active_conversion_event_providers' => array(
'label' => __( 'Active conversion event providers', 'google-site-kit' ),
'value' => $value,
),
);
}
/**
* Gets the key metrics status fields.
*
* @since 1.141.0
*
* @return array
*/
private function get_key_metrics_fields() {
$is_setup_complete = ( new Key_Metrics_Setup_Completed_By( $this->options ) )->get();
$key_metrics_settings = ( new Key_Metrics_Settings( $this->user_options ) )->get();
if ( ! $is_setup_complete ) {
return array(
'key_metrics_status' => array(
'label' => __( 'Key Metrics Status', 'google-site-kit' ),
'value' => __( 'Not setup', 'google-site-kit' ),
),
);
}
$key_metrics_status = isset( $key_metrics_settings['isWidgetHidden'] ) && $key_metrics_settings['isWidgetHidden'] ?
__( 'Setup and Disabled', 'google-site-kit' ) :
__( 'Setup and Enabled', 'google-site-kit' );
// A minimum of 2 key metrics need to be saved to prevent "default" tailored metrics to render.
$key_metrics_source = isset( $key_metrics_settings['widgetSlugs'] ) && count( $key_metrics_settings['widgetSlugs'] ) > 1 ?
__( 'Manual Selection', 'google-site-kit' ) :
__( 'Tailored Metrics', 'google-site-kit' );
return array(
'key_metrics_status' => array(
'label' => __( 'Key Metrics Status', 'google-site-kit' ),
'value' => $key_metrics_status,
),
'key_metrics_source' => array(
'label' => __( 'Key Metrics Source', 'google-site-kit' ),
'value' => $key_metrics_source,
),
);
}
}