-
Notifications
You must be signed in to change notification settings - Fork 879
/
class-admin-asset-manager.php
705 lines (659 loc) · 19 KB
/
class-admin-asset-manager.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
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin
*/
/**
* This class registers all the necessary styles and scripts.
*
* It automatically adds a prefix to the handle.
*/
class WPSEO_Admin_Asset_Manager {
/**
* Prefix for naming the assets.
*
* @var string
*/
public const PREFIX = 'yoast-seo-';
/**
* Class that manages the assets' location.
*
* @var WPSEO_Admin_Asset_Location
*/
protected $asset_location;
/**
* Prefix for naming the assets.
*
* @var string
*/
private $prefix;
/**
* Constructs a manager of assets. Needs a location to know where to register assets at.
*
* @param WPSEO_Admin_Asset_Location|null $asset_location The provider of the asset location.
* @param string $prefix The prefix for naming assets.
*/
public function __construct( ?WPSEO_Admin_Asset_Location $asset_location = null, $prefix = self::PREFIX ) {
if ( $asset_location === null ) {
$asset_location = self::create_default_location();
}
$this->asset_location = $asset_location;
$this->prefix = $prefix;
}
/**
* Enqueues scripts.
*
* @param string $script The name of the script to enqueue.
*
* @return void
*/
public function enqueue_script( $script ) {
wp_enqueue_script( $this->prefix . $script );
}
/**
* Enqueues styles.
*
* @param string $style The name of the style to enqueue.
*
* @return void
*/
public function enqueue_style( $style ) {
wp_enqueue_style( $this->prefix . $style );
}
/**
* Enqueues the appropriate language for the user.
*
* @return void
*/
public function enqueue_user_language_script() {
$this->enqueue_script( 'language-' . YoastSEO()->helpers->language->get_researcher_language() );
}
/**
* Registers scripts based on it's parameters.
*
* @param WPSEO_Admin_Asset $script The script to register.
*
* @return void
*/
public function register_script( WPSEO_Admin_Asset $script ) {
$url = $script->get_src() ? $this->get_url( $script, WPSEO_Admin_Asset::TYPE_JS ) : false;
wp_register_script(
$this->prefix . $script->get_name(),
$url,
$script->get_deps(),
$script->get_version(),
$script->is_in_footer()
);
if ( in_array( 'wp-i18n', $script->get_deps(), true ) ) {
wp_set_script_translations( $this->prefix . $script->get_name(), 'wordpress-seo' );
}
}
/**
* Registers styles based on it's parameters.
*
* @param WPSEO_Admin_Asset $style The style to register.
*
* @return void
*/
public function register_style( WPSEO_Admin_Asset $style ) {
wp_register_style(
$this->prefix . $style->get_name(),
$this->get_url( $style, WPSEO_Admin_Asset::TYPE_CSS ),
$style->get_deps(),
$style->get_version(),
$style->get_media()
);
}
/**
* Calls the functions that register scripts and styles with the scripts and styles to be registered as arguments.
*
* @return void
*/
public function register_assets() {
$this->register_scripts( $this->scripts_to_be_registered() );
$this->register_styles( $this->styles_to_be_registered() );
}
/**
* Registers all the scripts passed to it.
*
* @param array $scripts The scripts passed to it.
*
* @return void
*/
public function register_scripts( $scripts ) {
foreach ( $scripts as $script ) {
$script = new WPSEO_Admin_Asset( $script );
$this->register_script( $script );
}
}
/**
* Registers all the styles it receives.
*
* @param array $styles Styles that need to be registered.
*
* @return void
*/
public function register_styles( $styles ) {
foreach ( $styles as $style ) {
$style = new WPSEO_Admin_Asset( $style );
$this->register_style( $style );
}
}
/**
* Localizes the script.
*
* @param string $handle The script handle.
* @param string $object_name The object name.
* @param array $data The l10n data.
*
* @return void
*/
public function localize_script( $handle, $object_name, $data ) {
wp_localize_script( $this->prefix . $handle, $object_name, $data );
}
/**
* Adds an inline script.
*
* @param string $handle The script handle.
* @param string $data The l10n data.
* @param string $position Optional. Whether to add the inline script before the handle or after.
*
* @return void
*/
public function add_inline_script( $handle, $data, $position = 'after' ) {
wp_add_inline_script( $this->prefix . $handle, $data, $position );
}
/**
* A list of styles that shouldn't be registered but are needed in other locations in the plugin.
*
* @return array
*/
public function special_styles() {
$flat_version = $this->flatten_version( WPSEO_VERSION );
$asset_args = [
'name' => 'inside-editor',
'src' => 'inside-editor-' . $flat_version,
];
return [ 'inside-editor' => new WPSEO_Admin_Asset( $asset_args ) ];
}
/**
* Flattens a version number for use in a filename.
*
* @param string $version The original version number.
*
* @return string The flattened version number.
*/
public function flatten_version( $version ) {
$parts = explode( '.', $version );
if ( count( $parts ) === 2 && preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
$parts[] = '0';
}
return implode( '', $parts );
}
/**
* Creates a default location object for use in the admin asset manager.
*
* @return WPSEO_Admin_Asset_Location The location to use in the asset manager.
*/
public static function create_default_location() {
if ( defined( 'YOAST_SEO_DEV_SERVER' ) && YOAST_SEO_DEV_SERVER ) {
$url = defined( 'YOAST_SEO_DEV_SERVER_URL' ) ? YOAST_SEO_DEV_SERVER_URL : WPSEO_Admin_Asset_Dev_Server_Location::DEFAULT_URL;
return new WPSEO_Admin_Asset_Dev_Server_Location( $url );
}
return new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE, false );
}
/**
* Checks if the given script is enqueued.
*
* @param string $script The script to check.
*
* @return bool True when the script is enqueued.
*/
public function is_script_enqueued( $script ) {
return wp_script_is( $this->prefix . $script );
}
/**
* Returns the scripts that need to be registered.
*
* @todo Data format is not self-documenting. Needs explanation inline. R.
*
* @return array The scripts that need to be registered.
*/
protected function scripts_to_be_registered() {
$header_scripts = [
'admin-global',
'block-editor',
'classic-editor',
'post-edit',
'help-scout-beacon',
'redirect-old-features-tab',
];
$additional_dependencies = [
'analysis-worker' => [ self::PREFIX . 'analysis-package' ],
'api-client' => [ 'wp-api' ],
'crawl-settings' => [ 'jquery' ],
'dashboard-widget' => [ self::PREFIX . 'api-client' ],
'wincher-dashboard-widget' => [ self::PREFIX . 'api-client' ],
'editor-modules' => [ 'jquery' ],
'elementor' => [
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
],
'indexation' => [
'jquery-ui-core',
'jquery-ui-progressbar',
],
'first-time-configuration' => [
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
],
'integrations-page' => [
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
],
'post-edit' => [
self::PREFIX . 'api-client',
self::PREFIX . 'block-editor',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
],
'reindex-links' => [
'jquery-ui-core',
'jquery-ui-progressbar',
],
'settings' => [
'jquery-ui-core',
'jquery-ui-progressbar',
self::PREFIX . 'api-client',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
],
'term-edit' => [
self::PREFIX . 'api-client',
self::PREFIX . 'classic-editor',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
],
];
$plugin_scripts = $this->load_generated_asset_file(
[
'asset_file' => __DIR__ . '/../src/generated/assets/plugin.php',
'ext_length' => 3,
'additional_deps' => $additional_dependencies,
'header_scripts' => $header_scripts,
]
);
$external_scripts = $this->load_generated_asset_file(
[
'asset_file' => __DIR__ . '/../src/generated/assets/externals.php',
'ext_length' => 3,
'suffix' => '-package',
'base_dir' => 'externals/',
'additional_deps' => $additional_dependencies,
'header_scripts' => $header_scripts,
]
);
$language_scripts = $this->load_generated_asset_file(
[
'asset_file' => __DIR__ . '/../src/generated/assets/languages.php',
'ext_length' => 3,
'suffix' => '-language',
'base_dir' => 'languages/',
'additional_deps' => $additional_dependencies,
'header_scripts' => $header_scripts,
]
);
$renamed_scripts = $this->load_renamed_scripts();
$scripts = array_merge(
$plugin_scripts,
$external_scripts,
$language_scripts,
$renamed_scripts
);
$scripts['installation-success'] = [
'name' => 'installation-success',
'src' => 'installation-success.js',
'deps' => [
'wp-a11y',
'wp-dom-ready',
'wp-components',
'wp-element',
'wp-i18n',
self::PREFIX . 'components-new-package',
self::PREFIX . 'externals-components',
],
'version' => $scripts['installation-success']['version'],
];
$scripts['post-edit-classic'] = [
'name' => 'post-edit-classic',
'src' => $scripts['post-edit']['src'],
'deps' => array_map(
static function ( $dep ) {
if ( $dep === self::PREFIX . 'block-editor' ) {
return self::PREFIX . 'classic-editor';
}
return $dep;
},
$scripts['post-edit']['deps']
),
'in_footer' => ! in_array( 'post-edit-classic', $header_scripts, true ),
'version' => $scripts['post-edit']['version'],
];
$scripts['workouts'] = [
'name' => 'workouts',
'src' => 'workouts.js',
'deps' => [
'clipboard',
'lodash',
'wp-api-fetch',
'wp-a11y',
'wp-components',
'wp-compose',
'wp-data',
'wp-dom-ready',
'wp-element',
'wp-i18n',
self::PREFIX . 'externals-components',
self::PREFIX . 'externals-contexts',
self::PREFIX . 'externals-redux',
self::PREFIX . 'analysis',
self::PREFIX . 'react-select',
self::PREFIX . 'components-new-package',
],
'version' => $scripts['workouts']['version'],
];
// Add the current language to every script that requires the analysis package.
foreach ( $scripts as $name => $script ) {
if ( substr( $name, -8 ) === 'language' ) {
continue;
}
if ( in_array( self::PREFIX . 'analysis-package', $script['deps'], true ) ) {
$scripts[ $name ]['deps'][] = self::PREFIX . YoastSEO()->helpers->language->get_researcher_language() . '-language';
}
}
return $scripts;
}
/**
* Loads a generated asset file.
*
* @param array $args {
* The arguments.
*
* @type string $asset_file The asset file to load.
* @type int $ext_length The length of the extension, including suffix, of the filename.
* @type string $suffix Optional. The suffix of the asset name.
* @type array<string, string[]> $additional_deps Optional. The additional dependencies assets may have.
* @type string $base_dir Optional. The base directory of the asset.
* @type string[] $header_scripts Optional. The script names that should be in the header.
* }
*
* @return array {
* The scripts to be registered.
*
* @type string $name The name of the asset.
* @type string $src The src of the asset.
* @type string[] $deps The dependenies of the asset.
* @type bool $in_footer Whether or not the asset should be in the footer.
* }
*/
protected function load_generated_asset_file( $args ) {
$args = wp_parse_args(
$args,
[
'suffix' => '',
'additional_deps' => [],
'base_dir' => '',
'header_scripts' => [],
]
);
$scripts = [];
$assets = require $args['asset_file'];
foreach ( $assets as $file => $data ) {
$name = substr( $file, 0, -$args['ext_length'] );
$name = strtolower( preg_replace( '/([A-Z])/', '-$1', $name ) );
$name .= $args['suffix'];
$deps = $data['dependencies'];
if ( isset( $args['additional_deps'][ $name ] ) ) {
$deps = array_merge( $deps, $args['additional_deps'][ $name ] );
}
$scripts[ $name ] = [
'name' => $name,
'src' => $args['base_dir'] . $file,
'deps' => $deps,
'in_footer' => ! in_array( $name, $args['header_scripts'], true ),
'version' => $data['version'],
];
}
return $scripts;
}
/**
* Loads the scripts that should be renamed for BC.
*
* @return array {
* The scripts to be registered.
*
* @type string $name The name of the asset.
* @type string $src The src of the asset.
* @type string[] $deps The dependenies of the asset.
* @type bool $in_footer Whether or not the asset should be in the footer.
* }
*/
protected function load_renamed_scripts() {
$scripts = [];
$renamed_scripts = [
'admin-global-script' => 'admin-global',
'analysis' => 'analysis-package',
'analysis-report' => 'analysis-report-package',
'api' => 'api-client',
'commons' => 'commons-package',
'edit-page' => 'edit-page-script',
'draft-js' => 'draft-js-package',
'feature-flag' => 'feature-flag-package',
'helpers' => 'helpers-package',
'jed' => 'jed-package',
'chart.js' => 'chart.js-package',
'network-admin-script' => 'network-admin',
'redux' => 'redux-package',
'replacement-variable-editor' => 'replacement-variable-editor-package',
'search-metadata-previews' => 'search-metadata-previews-package',
'social-metadata-forms' => 'social-metadata-forms-package',
'styled-components' => 'styled-components-package',
'style-guide' => 'style-guide-package',
'yoast-components' => 'components-new-package',
];
foreach ( $renamed_scripts as $original => $replacement ) {
$scripts[] = [
'name' => $original,
'src' => false,
'deps' => [ self::PREFIX . $replacement ],
];
}
return $scripts;
}
/**
* Returns the styles that need to be registered.
*
* @todo Data format is not self-documenting. Needs explanation inline. R.
*
* @return array Styles that need to be registered.
*/
protected function styles_to_be_registered() {
$flat_version = $this->flatten_version( WPSEO_VERSION );
return [
[
'name' => 'admin-css',
'src' => 'yst_plugin_tools-' . $flat_version,
'deps' => [ self::PREFIX . 'toggle-switch' ],
],
[
'name' => 'toggle-switch',
'src' => 'toggle-switch-' . $flat_version,
],
[
'name' => 'dismissible',
'src' => 'wpseo-dismissible-' . $flat_version,
],
[
'name' => 'notifications',
'src' => 'notifications-' . $flat_version,
],
[
'name' => 'alert',
'src' => 'alerts-' . $flat_version,
],
[
'name' => 'edit-page',
'src' => 'edit-page-' . $flat_version,
],
[
'name' => 'featured-image',
'src' => 'featured-image-' . $flat_version,
],
[
'name' => 'metabox-css',
'src' => 'metabox-' . $flat_version,
'deps' => [
self::PREFIX . 'admin-css',
self::PREFIX . 'tailwind',
'wp-components',
self::PREFIX . 'additional-mentions',
],
],
[
'name' => 'additional-mentions',
'src' => 'additional-mentions-' . $flat_version,
'deps' => [
self::PREFIX . 'tailwind',
],
],
[
'name' => 'ai-generator',
'src' => 'ai-generator-' . $flat_version,
'deps' => [
self::PREFIX . 'tailwind',
self::PREFIX . 'introductions',
],
],
[
'name' => 'introductions',
'src' => 'introductions-' . $flat_version,
'deps' => [ self::PREFIX . 'tailwind' ],
],
[
'name' => 'wp-dashboard',
'src' => 'dashboard-' . $flat_version,
],
[
'name' => 'scoring',
'src' => 'yst_seo_score-' . $flat_version,
],
[
'name' => 'adminbar',
'src' => 'adminbar-' . $flat_version,
'deps' => [
'admin-bar',
],
],
[
'name' => 'primary-category',
'src' => 'metabox-primary-category-' . $flat_version,
],
[
'name' => 'admin-global',
'src' => 'admin-global-' . $flat_version,
],
[
'name' => 'extensions',
'src' => 'yoast-extensions-' . $flat_version,
'deps' => [
'wp-components',
],
],
[
'name' => 'filter-explanation',
'src' => 'filter-explanation-' . $flat_version,
],
[
'name' => 'monorepo',
'src' => 'monorepo-' . $flat_version,
],
[
'name' => 'structured-data-blocks',
'src' => 'structured-data-blocks-' . $flat_version,
'deps' => [ 'wp-edit-blocks' ],
],
[
'name' => 'elementor',
'src' => 'elementor-' . $flat_version,
],
[
'name' => 'tailwind',
'src' => 'tailwind-' . $flat_version,
],
[
'name' => 'new-settings',
'src' => 'new-settings-' . $flat_version,
'deps' => [
self::PREFIX . 'tailwind',
self::PREFIX . 'additional-mentions',
],
],
[
'name' => 'black-friday-banner',
'src' => 'black-friday-banner-' . $flat_version,
'deps' => [ self::PREFIX . 'tailwind' ],
],
[
'name' => 'academy',
'src' => 'academy-' . $flat_version,
'deps' => [ self::PREFIX . 'tailwind' ],
],
[
'name' => 'support',
'src' => 'support-' . $flat_version,
'deps' => [ self::PREFIX . 'tailwind' ],
],
[
'name' => 'workouts',
'src' => 'workouts-' . $flat_version,
'deps' => [
self::PREFIX . 'monorepo',
],
],
[
'name' => 'first-time-configuration',
'src' => 'first-time-configuration-' . $flat_version,
'deps' => [ self::PREFIX . 'tailwind' ],
],
[
'name' => 'inside-editor',
'src' => 'inside-editor-' . $flat_version,
],
];
}
/**
* Determines the URL of the asset.
*
* @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
* @param string $type The type of asset. Usually JS or CSS.
*
* @return string The URL of the asset.
*/
protected function get_url( WPSEO_Admin_Asset $asset, $type ) {
$scheme = wp_parse_url( $asset->get_src(), PHP_URL_SCHEME );
if ( in_array( $scheme, [ 'http', 'https' ], true ) ) {
return $asset->get_src();
}
return $this->asset_location->get_url( $asset, $type );
}
}