PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / class-admin-asset-manager.php

class-admin-asset-manager.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4, at admin/class-admin-asset-manager.php

695 lines 19.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin
6 */
7
8 /**
9 * This class registers all the necessary styles and scripts.
10 *
11 * Also has methods for the enqueing of scripts and styles.
12 * It automatically adds a prefix to the handle.
13 */
14 class WPSEO_Admin_Asset_Manager {
15
16 /**
17 * Prefix for naming the assets.
18 *
19 * @var string
20 */
21 const PREFIX = 'yoast-seo-';
22
23 /**
24 * Class that manages the assets' location.
25 *
26 * @var WPSEO_Admin_Asset_Location
27 */
28 protected $asset_location;
29
30 /**
31 * Prefix for naming the assets.
32 *
33 * @var string
34 */
35 private $prefix;
36
37 /**
38 * Constructs a manager of assets. Needs a location to know where to register assets at.
39 *
40 * @param WPSEO_Admin_Asset_Location|null $asset_location The provider of the asset location.
41 * @param string $prefix The prefix for naming assets.
42 */
43 public function __construct( WPSEO_Admin_Asset_Location $asset_location = null, $prefix = self::PREFIX ) {
44 if ( $asset_location === null ) {
45 $asset_location = self::create_default_location();
46 }
47
48 $this->asset_location = $asset_location;
49 $this->prefix = $prefix;
50 }
51
52 /**
53 * Enqueues scripts.
54 *
55 * @param string $script The name of the script to enqueue.
56 */
57 public function enqueue_script( $script ) {
58 wp_enqueue_script( $this->prefix . $script );
59 }
60
61 /**
62 * Enqueues styles.
63 *
64 * @param string $style The name of the style to enqueue.
65 */
66 public function enqueue_style( $style ) {
67 wp_enqueue_style( $this->prefix . $style );
68 }
69
70 /**
71 * Enqueues the appropriate language for the user.
72 */
73 public function enqueue_user_language_script() {
74 $this->enqueue_script( 'language-' . YoastSEO()->helpers->language->get_researcher_language() );
75 }
76
77 /**
78 * Registers scripts based on it's parameters.
79 *
80 * @param WPSEO_Admin_Asset $script The script to register.
81 */
82 public function register_script( WPSEO_Admin_Asset $script ) {
83 $url = $script->get_src() ? $this->get_url( $script, WPSEO_Admin_Asset::TYPE_JS ) : false;
84
85 wp_register_script(
86 $this->prefix . $script->get_name(),
87 $url,
88 $script->get_deps(),
89 $script->get_version(),
90 $script->is_in_footer()
91 );
92
93 if ( in_array( 'wp-i18n', $script->get_deps(), true ) ) {
94 wp_set_script_translations( $this->prefix . $script->get_name(), 'wordpress-seo' );
95 }
96 }
97
98 /**
99 * Registers styles based on it's parameters.
100 *
101 * @param WPSEO_Admin_Asset $style The style to register.
102 */
103 public function register_style( WPSEO_Admin_Asset $style ) {
104 wp_register_style(
105 $this->prefix . $style->get_name(),
106 $this->get_url( $style, WPSEO_Admin_Asset::TYPE_CSS ),
107 $style->get_deps(),
108 $style->get_version(),
109 $style->get_media()
110 );
111 }
112
113 /**
114 * Calls the functions that register scripts and styles with the scripts and styles to be registered as arguments.
115 */
116 public function register_assets() {
117 $this->register_scripts( $this->scripts_to_be_registered() );
118 $this->register_styles( $this->styles_to_be_registered() );
119 }
120
121 /**
122 * Registers all the scripts passed to it.
123 *
124 * @param array $scripts The scripts passed to it.
125 */
126 public function register_scripts( $scripts ) {
127 foreach ( $scripts as $script ) {
128 $script = new WPSEO_Admin_Asset( $script );
129 $this->register_script( $script );
130 }
131 }
132
133 /**
134 * Registers all the styles it receives.
135 *
136 * @param array $styles Styles that need to be registered.
137 */
138 public function register_styles( $styles ) {
139 foreach ( $styles as $style ) {
140 $style = new WPSEO_Admin_Asset( $style );
141 $this->register_style( $style );
142 }
143 }
144
145 /**
146 * Localizes the script.
147 *
148 * @param string $handle The script handle.
149 * @param string $object_name The object name.
150 * @param array $data The l10n data.
151 */
152 public function localize_script( $handle, $object_name, $data ) {
153 \wp_localize_script( $this->prefix . $handle, $object_name, $data );
154 }
155
156 /**
157 * Adds an inline script.
158 *
159 * @param string $handle The script handle.
160 * @param string $data The l10n data.
161 * @param string $position Optional. Whether to add the inline script before the handle or after.
162 */
163 public function add_inline_script( $handle, $data, $position = 'after' ) {
164 \wp_add_inline_script( $this->prefix . $handle, $data, $position );
165 }
166
167 /**
168 * A list of styles that shouldn't be registered but are needed in other locations in the plugin.
169 *
170 * @return array
171 */
172 public function special_styles() {
173 $flat_version = $this->flatten_version( WPSEO_VERSION );
174 $asset_args = [
175 'name' => 'inside-editor',
176 'src' => 'inside-editor-' . $flat_version,
177 ];
178
179 return [ 'inside-editor' => new WPSEO_Admin_Asset( $asset_args ) ];
180 }
181
182 /**
183 * Flattens a version number for use in a filename.
184 *
185 * @param string $version The original version number.
186 *
187 * @return string The flattened version number.
188 */
189 public function flatten_version( $version ) {
190 $parts = explode( '.', $version );
191
192 if ( count( $parts ) === 2 && preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
193 $parts[] = '0';
194 }
195
196 return implode( '', $parts );
197 }
198
199 /**
200 * Creates a default location object for use in the admin asset manager.
201 *
202 * @return WPSEO_Admin_Asset_Location The location to use in the asset manager.
203 */
204 public static function create_default_location() {
205 if ( defined( 'YOAST_SEO_DEV_SERVER' ) && YOAST_SEO_DEV_SERVER ) {
206 $url = defined( 'YOAST_SEO_DEV_SERVER_URL' ) ? YOAST_SEO_DEV_SERVER_URL : WPSEO_Admin_Asset_Dev_Server_Location::DEFAULT_URL;
207
208 return new WPSEO_Admin_Asset_Dev_Server_Location( $url );
209 }
210
211 return new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE, false );
212 }
213
214 /**
215 * Checks if the given script is enqueued.
216 *
217 * @param string $script The script to check.
218 *
219 * @return bool True when the script is enqueued.
220 */
221 public function is_script_enqueued( $script ) {
222 return \wp_script_is( $this->prefix . $script );
223 }
224
225 /**
226 * Returns the scripts that need to be registered.
227 *
228 * @todo Data format is not self-documenting. Needs explanation inline. R.
229 *
230 * @return array The scripts that need to be registered.
231 */
232 protected function scripts_to_be_registered() {
233 $header_scripts = [
234 'admin-global',
235 'block-editor',
236 'classic-editor',
237 'post-edit',
238 'help-scout-beacon',
239 ];
240 $additional_dependencies = [
241 'analysis-worker' => [ self::PREFIX . 'analysis-package' ],
242 'api-client' => [ 'wp-api' ],
243 'dashboard-widget' => [ self::PREFIX . 'api-client' ],
244 'elementor' => [
245 self::PREFIX . 'api-client',
246 self::PREFIX . 'externals-components',
247 self::PREFIX . 'externals-contexts',
248 self::PREFIX . 'externals-redux',
249 ],
250 'indexation' => [
251 'jquery-ui-core',
252 'jquery-ui-progressbar',
253 ],
254 'post-edit' => [
255 self::PREFIX . 'api-client',
256 self::PREFIX . 'block-editor',
257 self::PREFIX . 'externals-components',
258 self::PREFIX . 'externals-contexts',
259 self::PREFIX . 'externals-redux',
260 self::PREFIX . 'select2',
261 ],
262 'reindex-links' => [
263 'jquery-ui-core',
264 'jquery-ui-progressbar',
265 ],
266 'settings' => [
267 'jquery-ui-core',
268 'jquery-ui-progressbar',
269 self::PREFIX . 'api-client',
270 self::PREFIX . 'select2',
271 ],
272 'term-edit' => [
273 self::PREFIX . 'api-client',
274 self::PREFIX . 'classic-editor',
275 self::PREFIX . 'externals-components',
276 self::PREFIX . 'externals-contexts',
277 self::PREFIX . 'externals-redux',
278 self::PREFIX . 'select2',
279 ],
280 ];
281
282 $plugin_scripts = $this->load_generated_asset_file(
283 [
284 'asset_file' => __DIR__ . '/../src/generated/assets/plugin.php',
285 'ext_length' => 3,
286 'additional_deps' => $additional_dependencies,
287 'header_scripts' => $header_scripts,
288 ]
289 );
290 $external_scripts = $this->load_generated_asset_file(
291 [
292 'asset_file' => __DIR__ . '/../src/generated/assets/externals.php',
293 'ext_length' => 3,
294 'suffix' => '-package',
295 'base_dir' => 'externals/',
296 'additional_deps' => $additional_dependencies,
297 'header_scripts' => $header_scripts,
298 ]
299 );
300 $language_scripts = $this->load_generated_asset_file(
301 [
302 'asset_file' => __DIR__ . '/../src/generated/assets/languages.php',
303 'ext_length' => 3,
304 'suffix' => '-language',
305 'base_dir' => 'languages/',
306 'additional_deps' => $additional_dependencies,
307 'header_scripts' => $header_scripts,
308 ]
309 );
310 $select2_scripts = $this->load_select2_scripts();
311 $renamed_scripts = $this->load_renamed_scripts();
312
313 $scripts = array_merge(
314 $plugin_scripts,
315 $external_scripts,
316 $language_scripts,
317 $select2_scripts,
318 $renamed_scripts
319 );
320
321 $scripts['installation-success'] = [
322 'name' => 'installation-success',
323 'src' => 'installation-success.js',
324 'deps' => [
325 'wp-a11y',
326 'wp-dom-ready',
327 'wp-components',
328 'wp-element',
329 'wp-i18n',
330 self::PREFIX . 'yoast-components',
331 self::PREFIX . 'externals-components',
332 ],
333 'version' => $scripts['installation-success']['version'],
334 ];
335
336 $scripts['post-edit-classic'] = [
337 'name' => 'post-edit-classic',
338 'src' => $scripts['post-edit']['src'],
339 'deps' => array_map(
340 static function( $dep ) {
341 if ( $dep === self::PREFIX . 'block-editor' ) {
342 return self::PREFIX . 'classic-editor';
343 }
344 return $dep;
345 },
346 $scripts['post-edit']['deps']
347 ),
348 'in_footer' => ! in_array( 'post-edit-classic', $header_scripts, true ),
349 'version' => $scripts['post-edit']['version'],
350 ];
351
352 $scripts['workouts'] = [
353 'name' => 'workouts',
354 'src' => 'workouts.js',
355 'deps' => [
356 'clipboard',
357 'lodash',
358 'wp-api-fetch',
359 'wp-a11y',
360 'wp-components',
361 'wp-compose',
362 'wp-data',
363 'wp-dom-ready',
364 'wp-element',
365 'wp-i18n',
366 self::PREFIX . 'externals-components',
367 self::PREFIX . 'externals-contexts',
368 self::PREFIX . 'externals-redux',
369 self::PREFIX . 'analysis',
370 self::PREFIX . 'react-select',
371 self::PREFIX . 'yoast-components',
372 ],
373 'version' => $scripts['workouts']['version'],
374 ];
375
376 // Add the current language to every script that requires the analysis package.
377 foreach ( $scripts as $name => $script ) {
378 if ( substr( $name, -8 ) === 'language' ) {
379 continue;
380 }
381 if ( in_array( self::PREFIX . 'analysis-package', $script['deps'], true ) ) {
382 $scripts[ $name ]['deps'][] = self::PREFIX . YoastSEO()->helpers->language->get_researcher_language() . '-language';
383 }
384 }
385
386 return $scripts;
387 }
388
389 /**
390 * Loads a generated asset file.
391 *
392 * @param array $args {
393 * The arguments.
394 *
395 * @type string $asset_file The asset file to load.
396 * @type int $ext_length The length of the extension, including suffix, of the filename.
397 * @type string $suffix Optional. The suffix of the asset name.
398 * @type array<string, string[]> $additional_deps Optional. The additional dependencies assets may have.
399 * @type string $base_dir Optional. The base directory of the asset.
400 * @type string[] $header_scripts Optional. The script names that should be in the header.
401 * }
402 *
403 * @return array {
404 * The scripts to be registered.
405 *
406 * @type string $name The name of the asset.
407 * @type string $src The src of the asset.
408 * @type string[] $deps The dependenies of the asset.
409 * @type bool $in_footer Whether or not the asset should be in the footer.
410 * }
411 */
412 protected function load_generated_asset_file( $args ) {
413 $args = wp_parse_args(
414 $args,
415 [
416 'suffix' => '',
417 'additional_deps' => [],
418 'base_dir' => '',
419 'header_scripts' => [],
420 ]
421 );
422 $scripts = [];
423 $assets = require $args['asset_file'];
424 foreach ( $assets as $file => $data ) {
425 $name = substr( $file, 0, -$args['ext_length'] );
426 $name = strtolower( preg_replace( '/([A-Z])/', '-$1', $name ) );
427 $name .= $args['suffix'];
428
429 $deps = $data['dependencies'];
430 if ( isset( $args['additional_deps'][ $name ] ) ) {
431 $deps = array_merge( $deps, $args['additional_deps'][ $name ] );
432 }
433
434 $scripts[ $name ] = [
435 'name' => $name,
436 'src' => $args['base_dir'] . $file,
437 'deps' => $deps,
438 'in_footer' => ! in_array( $name, $args['header_scripts'], true ),
439 'version' => $data['version'],
440 ];
441 }
442
443 return $scripts;
444 }
445
446 /**
447 * Loads the select2 scripts.
448 *
449 * @return array {
450 * The scripts to be registered.
451 *
452 * @type string $name The name of the asset.
453 * @type string $src The src of the asset.
454 * @type string[] $deps The dependenies of the asset.
455 * @type bool $in_footer Whether or not the asset should be in the footer.
456 * }
457 */
458 protected function load_select2_scripts() {
459 $scripts = [];
460 $select2_language = 'en';
461 $user_locale = \get_user_locale();
462 $language = WPSEO_Language_Utils::get_language( $user_locale );
463
464 if ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$user_locale}.js" ) ) {
465 $select2_language = $user_locale; // Chinese and some others use full locale.
466 }
467 elseif ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$language}.js" ) ) {
468 $select2_language = $language;
469 }
470
471 $scripts['select2'] = [
472 'name' => 'select2',
473 'src' => false,
474 'deps' => [
475 self::PREFIX . 'select2-translations',
476 self::PREFIX . 'select2-core',
477 ],
478 ];
479 $scripts['select2-core'] = [
480 'name' => 'select2-core',
481 'src' => 'select2/select2.full.min.js',
482 'deps' => [
483 'jquery',
484 ],
485 'version' => '4.1.0-rc.0',
486 ];
487 $scripts['select2-translations'] = [
488 'name' => 'select2-translations',
489 'src' => 'select2/i18n/' . $select2_language . '.js',
490 'deps' => [
491 'jquery',
492 self::PREFIX . 'select2-core',
493 ],
494 'version' => '4.1.0-rc.0',
495 ];
496
497 return $scripts;
498 }
499
500 /**
501 * Loads the scripts that should be renamed for BC.
502 *
503 * @return array {
504 * The scripts to be registered.
505 *
506 * @type string $name The name of the asset.
507 * @type string $src The src of the asset.
508 * @type string[] $deps The dependenies of the asset.
509 * @type bool $in_footer Whether or not the asset should be in the footer.
510 * }
511 */
512 protected function load_renamed_scripts() {
513 $scripts = [];
514 $renamed_scripts = [
515 'admin-global-script' => 'admin-global',
516 'analysis' => 'analysis-package',
517 'analysis-report' => 'analysis-report-package',
518 'api' => 'api-client',
519 'commons' => 'commons-package',
520 'edit-page' => 'edit-page-script',
521 'draft-js' => 'draft-js-package',
522 'feature-flag' => 'feature-flag-package',
523 'helpers' => 'helpers-package',
524 'jed' => 'jed-package',
525 'legacy-components' => 'components-package',
526 'network-admin-script' => 'network-admin',
527 'redux' => 'redux-package',
528 'replacement-variable-editor' => 'replacement-variable-editor-package',
529 'search-metadata-previews' => 'search-metadata-previews-package',
530 'social-metadata-forms' => 'social-metadata-forms-package',
531 'styled-components' => 'styled-components-package',
532 'style-guide' => 'style-guide-package',
533 'yoast-components' => 'components-new-package',
534 ];
535
536 foreach ( $renamed_scripts as $original => $replacement ) {
537 $scripts[] = [
538 'name' => $original,
539 'src' => false,
540 'deps' => [ self::PREFIX . $replacement ],
541 ];
542 }
543
544 return $scripts;
545 }
546
547 /**
548 * Returns the styles that need to be registered.
549 *
550 * @todo Data format is not self-documenting. Needs explanation inline. R.
551 *
552 * @return array Styles that need to be registered.
553 */
554 protected function styles_to_be_registered() {
555 $flat_version = $this->flatten_version( WPSEO_VERSION );
556
557 return [
558 [
559 'name' => 'admin-css',
560 'src' => 'yst_plugin_tools-' . $flat_version,
561 'deps' => [ self::PREFIX . 'toggle-switch' ],
562 ],
563 [
564 'name' => 'toggle-switch',
565 'src' => 'toggle-switch-' . $flat_version,
566 ],
567 [
568 'name' => 'dismissible',
569 'src' => 'wpseo-dismissible-' . $flat_version,
570 ],
571 [
572 'name' => 'notifications',
573 'src' => 'notifications-' . $flat_version,
574 ],
575 [
576 'name' => 'alert',
577 'src' => 'alerts-' . $flat_version,
578 ],
579 [
580 'name' => 'edit-page',
581 'src' => 'edit-page-' . $flat_version,
582 ],
583 [
584 'name' => 'featured-image',
585 'src' => 'featured-image-' . $flat_version,
586 ],
587 [
588 'name' => 'metabox-css',
589 'src' => 'metabox-' . $flat_version,
590 'deps' => [
591 self::PREFIX . 'select2',
592 self::PREFIX . 'admin-css',
593 'wp-components',
594 ],
595 ],
596 [
597 'name' => 'wp-dashboard',
598 'src' => 'dashboard-' . $flat_version,
599 ],
600 [
601 'name' => 'scoring',
602 'src' => 'yst_seo_score-' . $flat_version,
603 ],
604 [
605 'name' => 'adminbar',
606 'src' => 'adminbar-' . $flat_version,
607 'deps' => [
608 'admin-bar',
609 ],
610 ],
611 [
612 'name' => 'primary-category',
613 'src' => 'metabox-primary-category-' . $flat_version,
614 ],
615 [
616 'name' => 'select2',
617 'src' => 'select2/select2',
618 'suffix' => '.min',
619 'version' => '4.1.0-rc.0',
620 'rtl' => false,
621 ],
622 [
623 'name' => 'admin-global',
624 'src' => 'admin-global-' . $flat_version,
625 ],
626 [
627 'name' => 'yoast-components',
628 'src' => 'yoast-components-' . $flat_version,
629 ],
630 [
631 'name' => 'extensions',
632 'src' => 'yoast-extensions-' . $flat_version,
633 'deps' => [
634 'wp-components',
635 ],
636 ],
637 [
638 'name' => 'filter-explanation',
639 'src' => 'filter-explanation-' . $flat_version,
640 ],
641 [
642 'name' => 'search-appearance',
643 'src' => 'search-appearance-' . $flat_version,
644 'deps' => [
645 self::PREFIX . 'monorepo',
646 ],
647 ],
648 [
649 'name' => 'monorepo',
650 'src' => 'monorepo-' . $flat_version,
651 ],
652 [
653 'name' => 'structured-data-blocks',
654 'src' => 'structured-data-blocks-' . $flat_version,
655 'deps' => [ 'wp-edit-blocks' ],
656 ],
657 [
658 'name' => 'schema-blocks',
659 'src' => 'schema-blocks-' . $flat_version,
660 ],
661 [
662 'name' => 'elementor',
663 'src' => 'elementor-' . $flat_version,
664 ],
665 [
666 'name' => 'workouts',
667 'src' => 'workouts-' . $flat_version,
668 'deps' => [ self::PREFIX . 'monorepo' ],
669 ],
670 [
671 'name' => 'installation-success',
672 'src' => 'installation-success-' . $flat_version,
673 'deps' => [ self::PREFIX . 'monorepo' ],
674 ],
675 ];
676 }
677
678 /**
679 * Determines the URL of the asset.
680 *
681 * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
682 * @param string $type The type of asset. Usually JS or CSS.
683 *
684 * @return string The URL of the asset.
685 */
686 protected function get_url( WPSEO_Admin_Asset $asset, $type ) {
687 $scheme = wp_parse_url( $asset->get_src(), PHP_URL_SCHEME );
688 if ( in_array( $scheme, [ 'http', 'https' ], true ) ) {
689 return $asset->get_src();
690 }
691
692 return $this->asset_location->get_url( $asset, $type );
693 }
694 }
695