-
Notifications
You must be signed in to change notification settings - Fork 293
/
Assets.php
1059 lines (968 loc) · 27.2 KB
/
Assets.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\Assets\Assets
*
* @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\Assets;
use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Modules\Module_Sharing_Settings;
use Google\Site_Kit\Core\Permissions\Permissions;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Util\BC_Functions;
use Google\Site_Kit\Core\Util\Feature_Flags;
use WP_Dependencies;
/**
* Class managing assets.
*
* @since 1.0.0
* @access private
* @ignore
*/
final class Assets {
/**
* Plugin context.
*
* @since 1.0.0
* @var Context
*/
private $context;
/**
* Lazy-loaded assets as $handle => $instance pairs.
*
* @since 1.0.0
* @var array
*/
private $assets = array();
/**
* Internal flag for whether assets have been registered yet.
*
* @since 1.2.0
* @var bool
*/
private $assets_registered = false;
/**
* Internal list of print callbacks already done.
*
* @since 1.2.0
* @var array
*/
private $print_callbacks_done = array();
/**
* Constructor.
*
* @since 1.0.0
*
* @param Context $context Plugin context.
*/
public function __construct( Context $context ) {
$this->context = $context;
}
/**
* Registers functionality through WordPress hooks.
*
* @since 1.0.0
* @since 1.37.0 Enqueues Block Editor assets.
*/
public function register() {
$register_callback = function() {
if ( ! is_admin() ) {
return;
}
if ( $this->assets_registered ) {
return;
}
$this->assets_registered = true;
$this->register_assets();
};
add_action( 'admin_enqueue_scripts', $register_callback );
add_action( 'wp_enqueue_scripts', $register_callback );
add_filter(
'script_loader_tag',
function( $tag, $handle ) {
return $this->add_async_defer_attribute( $tag, $handle );
},
10,
2
);
// All other asset-related general logic should only be active when the
// current user can actually use Site Kit.
if ( false === (
current_user_can( Permissions::VIEW_SPLASH ) || current_user_can( Permissions::VIEW_DASHBOARD )
)
) {
return;
}
$this->add_amp_dev_mode_attributes( $this->get_assets() );
add_action(
'admin_print_scripts-edit.php',
function() {
global $post_type;
if ( 'post' !== $post_type ) {
// For CONTEXT_ADMIN_POSTS we only load scripts for the 'post' post type.
return;
}
$assets = $this->get_assets();
array_walk(
$assets,
function( Asset $asset ) {
if ( $asset->has_context( Asset::CONTEXT_ADMIN_POSTS ) ) {
$this->enqueue_asset( $asset->get_handle() );
}
}
);
}
);
add_action(
'enqueue_block_editor_assets',
function() {
$assets = $this->get_assets();
array_walk(
$assets,
function( $asset ) {
if ( $asset->has_context( Asset::CONTEXT_ADMIN_POST_EDITOR ) ) {
$this->enqueue_asset( $asset->get_handle() );
}
}
);
}
);
$scripts_print_callback = function() {
$scripts = wp_scripts();
$this->run_before_print_callbacks( $scripts, $scripts->queue );
};
add_action( 'wp_print_scripts', $scripts_print_callback );
add_action( 'admin_print_scripts', $scripts_print_callback );
$styles_print_callback = function() {
$styles = wp_styles();
$this->run_before_print_callbacks( $styles, $styles->queue );
};
add_action( 'wp_print_styles', $styles_print_callback );
add_action( 'admin_print_styles', $styles_print_callback );
}
/**
* Enqueues the given plugin asset (script or stylesheet).
*
* The asset must already be registered in order to be enqueued.
*
* @since 1.0.0
*
* @param string $handle Asset handle.
*/
public function enqueue_asset( $handle ) {
// Register assets on-the-fly if necessary (currently the case for admin bar in frontend).
if ( ! $this->assets_registered ) {
$this->assets_registered = true;
$this->register_assets();
}
$assets = $this->get_assets();
if ( empty( $assets[ $handle ] ) ) {
return;
}
$assets[ $handle ]->enqueue();
}
/**
* Enqueues Google fonts.
*
* @since 1.0.0
* @deprecated 1.41.0 This method is no longer used as fonts are loaded as a normal style dependency now.
*/
public function enqueue_fonts() {
_deprecated_function( __METHOD__, '1.41.0' );
$assets = $this->get_assets();
if ( ! empty( $assets['googlesitekit-fonts'] ) && $assets['googlesitekit-fonts'] instanceof Asset ) {
$assets['googlesitekit-fonts']->enqueue();
}
}
/**
* Get Google fonts src for CSS.
*
* @since 1.41.0
*
* @return string String URL src.
*/
protected function get_fonts_src() {
$font_families = array(
'Google+Sans+Text:400,500',
'Google+Sans+Display:400,500,700',
);
if ( Feature_Flags::enabled( 'gm3Components' ) ) {
$font_families[] = 'Roboto:300,400,500';
}
$filtered_font_families = apply_filters( 'googlesitekit_font_families', $font_families );
if ( empty( $filtered_font_families ) ) {
return '';
}
return add_query_arg(
array(
'family' => implode( '|', $filtered_font_families ),
'subset' => 'latin-ext',
'display' => 'fallback',
),
'https://fonts.googleapis.com/css'
);
}
/**
* Registers all plugin assets.
*
* @since 1.0.0
*/
private function register_assets() {
$assets = $this->get_assets();
foreach ( $assets as $asset ) {
$asset->register( $this->context );
}
}
/**
* Add data-ampdevmode attributes to assets.
*
* @todo What about dependencies?
*
* @param Asset[] $assets Assets.
*/
private function add_amp_dev_mode_attributes( $assets ) {
add_filter(
'script_loader_tag',
function ( $tag, $handle ) use ( $assets ) {
// TODO: 'hoverintent-js' can be removed from here at some point, see https://github.com/ampproject/amp-wp/pull/3928.
if ( $this->context->is_amp() && ( isset( $assets[ $handle ] ) && $assets[ $handle ] instanceof Script || 'hoverintent-js' === $handle ) ) {
$tag = preg_replace( '/(?<=<script)(?=\s|>)/i', ' data-ampdevmode', $tag );
}
return $tag;
},
10,
2
);
add_filter(
'style_loader_tag',
function ( $tag, $handle ) use ( $assets ) {
if ( $this->context->is_amp() && isset( $assets[ $handle ] ) && $assets[ $handle ] instanceof Stylesheet ) {
$tag = preg_replace( '/(?<=<link)(?=\s|>)/i', ' data-ampdevmode', $tag );
}
return $tag;
},
10,
2
);
}
/**
* Forms an array of dependencies based on the necessary context.
*
* @since 1.87.0
*
* @param string $context The context for which dependencies should be formed.
* @return array The array of dependencies.
*/
private function get_asset_dependencies( $context = '' ) {
$dependencies = array(
'googlesitekit-tracking-data',
'googlesitekit-runtime',
'googlesitekit-i18n',
'googlesitekit-vendor',
'googlesitekit-commons',
'googlesitekit-data',
'googlesitekit-datastore-forms',
'googlesitekit-datastore-location',
'googlesitekit-datastore-site',
'googlesitekit-datastore-user',
'googlesitekit-datastore-ui',
'googlesitekit-widgets',
);
if ( 'dashboard' === $context || 'dashboard-sharing' === $context ) {
array_push( $dependencies, 'googlesitekit-components' );
}
if ( 'dashboard-sharing' === $context && Feature_Flags::enabled( 'dashboardSharing' ) ) {
array_push( $dependencies, 'googlesitekit-dashboard-sharing-data' );
}
return $dependencies;
}
/**
* Gets all plugin assets.
*
* The method will lazy-load assets in an internal property so that the processing only happens once.
*
* @since 1.0.0
*
* @return Asset[] Associative array of asset $handle => $instance pairs.
*/
private function get_assets() {
if ( $this->assets ) {
return $this->assets;
}
$base_url = $this->context->url( 'dist/assets/' );
$dependencies = $this->get_asset_dependencies();
// Register plugin scripts.
$assets = array(
new Script_Data(
'googlesitekit-commons',
array(
'global' => '_googlesitekitLegacyData',
'data_callback' => function () {
return $this->get_inline_data();
},
)
),
new Script_Data(
'googlesitekit-base-data',
array(
'global' => '_googlesitekitBaseData',
'data_callback' => function () {
return $this->get_inline_base_data();
},
)
),
new Script_Data(
'googlesitekit-entity-data',
array(
'global' => '_googlesitekitEntityData',
'data_callback' => function () {
return $this->get_inline_entity_data();
},
)
),
new Script_Data(
'googlesitekit-user-data',
array(
'global' => '_googlesitekitUserData',
'data_callback' => function() {
return $this->get_inline_user_data();
},
)
),
new Script_Data(
'googlesitekit-apifetch-data',
array(
'global' => '_googlesitekitAPIFetchData',
'data_callback' => function () {
/**
* Preload common data by specifying an array of REST API paths that will be preloaded.
*
* Filters the array of paths that will be preloaded.
*
* @since 1.7.0
*
* @param array $preload_paths Array of paths to preload.
*/
$preload_paths = apply_filters( 'googlesitekit_apifetch_preload_paths', array() );
$preloaded = array_reduce(
array_unique( $preload_paths ),
'rest_preload_api_request',
array()
);
return array(
'nonce' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ),
'nonceEndpoint' => admin_url( 'admin-ajax.php?action=rest-nonce' ),
'preloadedData' => $preloaded,
'rootURL' => esc_url_raw( get_rest_url() ),
);
},
)
),
new Script_Data(
'googlesitekit-dashboard-sharing-data',
array(
'global' => '_googlesitekitDashboardSharingData',
'data_callback' => function() {
return $this->get_inline_dashboard_sharing_data();
},
)
),
new Script_Data(
'googlesitekit-tracking-data',
array(
'global' => '_googlesitekitTrackingData',
'data_callback' => function() {
return $this->get_inline_tracking_data();
},
)
),
new Script_Data(
'googlesitekit-modules-data',
array(
'global' => '_googlesitekitModulesData',
'data_callback' => function() {
return $this->get_inline_modules_data();
},
)
),
new Script(
'googlesitekit-runtime',
array(
'src' => $base_url . 'js/runtime.js',
)
),
new Script(
'googlesitekit-polyfills',
array(
'src' => $base_url . 'js/googlesitekit-polyfills.js',
'dependencies' => array(
'googlesitekit-base-data',
),
)
),
new Script(
'googlesitekit-i18n',
array(
'src' => $base_url . 'js/googlesitekit-i18n.js',
)
),
new Script(
'googlesitekit-vendor',
array(
'src' => $base_url . 'js/googlesitekit-vendor.js',
'dependencies' => array(
'googlesitekit-i18n',
'googlesitekit-runtime',
'googlesitekit-polyfills',
),
)
),
// Admin assets.
new Script(
'googlesitekit-components',
array(
'src' => $base_url . (
Feature_Flags::enabled( 'gm3Components' )
? 'js/googlesitekit-components-gm3.js'
: 'js/googlesitekit-components-gm2.js'
),
)
),
new Script(
'googlesitekit-activation',
array(
'src' => $base_url . 'js/googlesitekit-activation.js',
'dependencies' => $this->get_asset_dependencies( 'dashboard' ),
)
),
// Begin JSR Assets.
new Script(
'googlesitekit-api',
array(
'src' => $base_url . 'js/googlesitekit-api.js',
'dependencies' => array(
'googlesitekit-vendor',
'googlesitekit-apifetch-data',
),
)
),
new Script(
'googlesitekit-data',
array(
'src' => $base_url . 'js/googlesitekit-data.js',
'dependencies' => array(
'googlesitekit-vendor',
'googlesitekit-api',
),
)
),
new Script(
'googlesitekit-datastore-user',
array(
'src' => $base_url . 'js/googlesitekit-datastore-user.js',
'dependencies' => array(
'googlesitekit-data',
'googlesitekit-api',
'googlesitekit-user-data',
),
)
),
new Script(
'googlesitekit-datastore-location',
array(
'src' => $base_url . 'js/googlesitekit-datastore-location.js',
'dependencies' => array(
'googlesitekit-vendor',
'googlesitekit-data',
),
)
),
new Script(
'googlesitekit-datastore-site',
array(
'src' => $base_url . 'js/googlesitekit-datastore-site.js',
'dependencies' => array(
'googlesitekit-vendor',
'googlesitekit-api',
'googlesitekit-data',
'googlesitekit-base-data',
'googlesitekit-entity-data',
),
)
),
new Script(
'googlesitekit-datastore-forms',
array(
'src' => $base_url . 'js/googlesitekit-datastore-forms.js',
'dependencies' => array(
'googlesitekit-data',
),
)
),
new Script(
'googlesitekit-datastore-ui',
array(
'src' => $base_url . 'js/googlesitekit-datastore-ui.js',
'dependencies' => array(
'googlesitekit-data',
),
)
),
new Script(
'googlesitekit-modules',
array(
'src' => $base_url . 'js/googlesitekit-modules.js',
'dependencies' => array(
'googlesitekit-vendor',
'googlesitekit-api',
'googlesitekit-data',
'googlesitekit-datastore-site',
'googlesitekit-datastore-user',
),
)
),
new Script(
'googlesitekit-widgets',
array(
'src' => $base_url . 'js/googlesitekit-widgets.js',
'dependencies' => array(
'googlesitekit-data',
'googlesitekit-i18n',
'googlesitekit-components',
),
)
),
new Script(
'googlesitekit-user-input',
array(
'src' => $base_url . 'js/googlesitekit-user-input.js',
'dependencies' => $this->get_asset_dependencies( 'dashboard' ),
)
),
// End JSR Assets.
new Script(
'googlesitekit-splash',
array(
'src' => $base_url . 'js/googlesitekit-splash.js',
'dependencies' => $this->get_asset_dependencies( 'dashboard' ),
)
),
new Script(
'googlesitekit-entity-dashboard',
array(
'src' => $base_url . 'js/googlesitekit-entity-dashboard.js',
'dependencies' => $this->get_asset_dependencies( 'dashboard-sharing' ),
)
),
new Script(
'googlesitekit-main-dashboard',
array(
'src' => $base_url . 'js/googlesitekit-main-dashboard.js',
'dependencies' => $this->get_asset_dependencies( 'dashboard-sharing' ),
)
),
new Script(
'googlesitekit-settings',
array(
'src' => $base_url . 'js/googlesitekit-settings.js',
'dependencies' => $this->get_asset_dependencies( 'dashboard' ),
)
),
new Stylesheet(
'googlesitekit-admin-css',
array(
'src' => $base_url . 'css/googlesitekit-admin-css.css',
'dependencies' => array(
'googlesitekit-fonts',
),
)
),
// WP Dashboard assets.
new Script(
'googlesitekit-wp-dashboard',
array(
'src' => $base_url . 'js/googlesitekit-wp-dashboard.js',
'dependencies' => $dependencies,
'execution' => 'defer',
)
),
new Stylesheet(
'googlesitekit-wp-dashboard-css',
array(
'src' => $base_url . 'css/googlesitekit-wp-dashboard-css.css',
'dependencies' => array(
'googlesitekit-fonts',
),
)
),
// Admin bar assets.
new Script(
'googlesitekit-adminbar',
array(
'src' => $base_url . 'js/googlesitekit-adminbar.js',
'dependencies' => $dependencies,
'execution' => 'defer',
)
),
new Stylesheet(
'googlesitekit-adminbar-css',
array(
'src' => $base_url . 'css/googlesitekit-adminbar-css.css',
'dependencies' => array(
'googlesitekit-fonts',
),
)
),
new Stylesheet(
'googlesitekit-fonts',
array(
'src' => $this->get_fonts_src(),
'version' => null,
)
),
);
/**
* Filters the list of assets that Site Kit should register.
*
* This filter covers both scripts and stylesheets.
*
* @since 1.7.0
*
* @param Asset[] $assets List of Asset objects.
*/
$assets = apply_filters( 'googlesitekit_assets', $assets );
$this->assets = array();
foreach ( $assets as $asset ) {
$this->assets[ $asset->get_handle() ] = $asset;
}
return $this->assets;
}
/**
* Gets the most basic inline data needed for JS files.
*
* This should not include anything remotely expensive to compute.
*
* @since 1.2.0
*
* @return array The base inline data to be output.
*/
private function get_inline_base_data() {
global $wpdb;
$site_url = $this->context->get_reference_site_url();
$inline_data = array(
'homeURL' => trailingslashit( $this->context->get_canonical_home_url() ),
'referenceSiteURL' => esc_url_raw( trailingslashit( $site_url ) ),
'adminURL' => esc_url_raw( trailingslashit( admin_url() ) ),
'assetsURL' => esc_url_raw( $this->context->url( 'dist/assets/' ) ),
'widgetsAdminURL' => esc_url_raw( $this->get_widgets_admin_url() ),
'blogPrefix' => $wpdb->get_blog_prefix(),
'ampMode' => $this->context->get_amp_mode(),
'isNetworkMode' => $this->context->is_network_mode(),
'timezone' => get_option( 'timezone_string' ),
'siteName' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
'enabledFeatures' => Feature_Flags::get_enabled_features(),
'webStoriesActive' => defined( 'WEBSTORIES_VERSION' ),
'postTypes' => $this->get_post_types(),
'storagePrefix' => $this->get_storage_prefix(),
'referenceDate' => apply_filters( 'googlesitekit_reference_date', null ),
);
/**
* Filters the most basic inline data to pass to JS.
*
* This should not include anything remotely expensive to compute.
*
* @since 1.2.0
*
* @param array $data Base data.
*/
return apply_filters( 'googlesitekit_inline_base_data', $inline_data );
}
/**
* Gets the available public post type slugs and their labels.
*
* @since 1.81.0
*
* @return array Available post types array with their respective slugs and labels.
*/
private function get_post_types() {
$post_types = array();
$all_post_types = get_post_types( array( 'public' => true ), 'objects' );
foreach ( $all_post_types as $post_type_slug => $post_type_obj ) {
$post_types[] = array(
'slug' => $post_type_slug,
'label' => $post_type_obj->label,
);
}
return $post_types;
}
/**
* Gets the widgets admin edit page or block editor URL depending
* on the current theme.
*
* Themes which have FSE support do not have the old widgets admin screen. Such
* themes only have the option to edit widgets directly in the block editor.
*
* @since 1.81.0
*
* @return string The admin widgets page or block editor URL.
*/
private function get_widgets_admin_url() {
$current_theme = wp_get_theme();
if ( method_exists( $current_theme, 'is_block_theme' ) && $current_theme->is_block_theme() ) {
return admin_url( 'site-editor.php' );
}
if ( count( $GLOBALS['wp_registered_sidebars'] ) > 0 ) {
return admin_url( 'widgets.php' );
}
return null;
}
/**
* Gets the inline data specific to the current entity.
*
* @since 1.7.0
*
* @return array The site inline data to be output.
*/
private function get_inline_entity_data() {
$current_entity = $this->context->get_reference_entity();
return array(
'currentEntityURL' => $current_entity ? $current_entity->get_url() : null,
'currentEntityType' => $current_entity ? $current_entity->get_type() : null,
'currentEntityTitle' => $current_entity ? $current_entity->get_title() : null,
'currentEntityID' => $current_entity ? $current_entity->get_id() : null,
);
}
/**
* Gets the inline data specific to the current user
*
* @since 1.9.0
*
* @return array The user inline data to be output.
*/
private function get_inline_user_data() {
$current_user = wp_get_current_user();
$inline_data = array(
'user' => array(
'id' => $current_user->ID,
'email' => $current_user->user_email,
'name' => $current_user->display_name,
'picture' => get_avatar_url( $current_user->user_email ),
),
);
/**
* Filters the user inline data to pass to JS.
*
* This should not include anything remotely expensive to compute.
*
* @since 1.9.0
*
* @param array $data User data.
*/
return apply_filters( 'googlesitekit_user_data', $inline_data );
}
/**
* Gets the inline dashboard sharing data
*
* @since 1.49.0
*
* @return array The dashboard sharing inline data to be output.
*/
private function get_inline_dashboard_sharing_data() {
$all_roles = wp_roles()->roles;
$inline_data = array( 'roles' => array() );
foreach ( $all_roles as $role_slug => $role_details ) {
$role = get_role( $role_slug );
// Filter the role that has `edit_posts` capability.
if ( $role->has_cap( 'edit_posts' ) ) {
$inline_data['roles'][] = array(
'id' => $role_slug,
'displayName' => translate_user_role( $role_details['name'] ),
);
}
}
$settings = new Module_Sharing_Settings( new Options( $this->context ) );
$inline_data['settings'] = $settings->get();
/**
* Filters the dashboard sharing inline data to pass to JS.
*
* @since 1.49.0
*
* @param array $data dashboard sharing data.
*/
return apply_filters( 'googlesitekit_dashboard_sharing_data', $inline_data );
}
/**
* Gets data relevant for `trackEvent` calls.
*
* @since 1.78.0
*
* @return array The tracking inline data to be output.
*/
private function get_inline_tracking_data() {
$site_url = $this->context->get_reference_site_url();
$current_user = wp_get_current_user();
$inline_data = array(
'referenceSiteURL' => esc_url_raw( trailingslashit( $site_url ) ),
'userIDHash' => md5( $site_url . $current_user->ID ),
);
/**
* Filters the data relevant to trackEvent calls to pass to JS.
*
* @since 1.78.0
*
* @param array $inline_data Tracking data.
*/
return apply_filters( 'googlesitekit_inline_tracking_data', $inline_data );
}
/**
* Gets the inline data needed for core plugin scripts.
*
* @since 1.0.0
*
* @return array The inline data to be output.
*/
private function get_inline_data() {
$site_url = $this->context->get_reference_site_url();
$input = $this->context->input();
$admin_data = array(
'siteURL' => esc_url_raw( $site_url ),
'resetSession' => $input->filter( INPUT_GET, 'googlesitekit_reset_session', FILTER_VALIDATE_BOOLEAN ),
);
return array(
/**
* Filters the admin data to pass to JS.
*
* @since 1.0.0
*
* @param array $data Admin data.
*/
'admin' => apply_filters( 'googlesitekit_admin_data', $admin_data ),
'locale' => $this->context->get_locale( 'user' ),
/**
* Filters the setup data to pass to JS, needed during the dashboard page load.
*
* Get the setup data from the options table.
*
* @since 1.0.0
*
* @param array $data Authentication Data.
*/
'setup' => apply_filters( 'googlesitekit_setup_data', array() ),
);
}
/**
* Gets inline modules data.
*
* @since 1.96.0
*
* @return array The inline modules data to be output.
*/
private function get_inline_modules_data() {
/**
* Filters the inline modules data to pass to JS.
*
* @since 1.96.0
*
* @param array $data Modules data.
*/
return apply_filters( 'googlesitekit_inline_modules_data', array() );
}
/**
* Adds support for async and defer attributes to enqueued scripts.
*
* @since 1.0.0
*
* @param string $tag The script tag.
* @param string $handle The script handle.
* @return string Modified script tag.
*/
private function add_async_defer_attribute( $tag, $handle ) {
$script_execution = wp_scripts()->get_data( $handle, 'script_execution' );
if ( ! $script_execution ) {
return $tag;
}
if ( 'async' !== $script_execution && 'defer' !== $script_execution ) {
return $tag;
}
// Abort adding async/defer for scripts that have this script as a dependency.
foreach ( wp_scripts()->registered as $script ) {
if ( in_array( $handle, $script->deps, true ) ) {
return $tag;
}
}
// Add the attribute if it hasn't already been added.
if ( ! preg_match( ":\s$script_execution(=|>|\s):", $tag ) ) {
$tag = preg_replace( ':(?=></script>):', " $script_execution", $tag, 1 );
}
return $tag;
}
/**
* Executes all extra callbacks before printing a list of dependencies.
*
* This method ensures that such callbacks that run e.g. `wp_add_inline_script()` are executed just-in-time,
* only when the asset is actually loaded in the current request.
*
* This method works recursively, also looking at dependencies, and supports both scripts and stylesheets.
*
* @since 1.2.0
*
* @param WP_Dependencies $dependencies WordPress dependencies class instance.
* @param array $handles List of handles to run before print callbacks for.
*/
private function run_before_print_callbacks( WP_Dependencies $dependencies, array $handles ) {
$is_amp = $this->context->is_amp();