PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
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 27.6, at admin/class-admin-asset-manager.php

756 lines 20.6 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 public 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 $asset_location ??= self::create_default_location();
45
46 $this->asset_location = $asset_location;
47 $this->prefix = $prefix;
48 }
49
50 /**
51 * Enqueues scripts.
52 *
53 * @param string $script The name of the script to enqueue.
54 *
55 * @return void
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 * @return void
67 */
68 public function enqueue_style( $style ) {
69 wp_enqueue_style( $this->prefix . $style );
70 }
71
72 /**
73 * Enqueues the appropriate language for the user.
74 *
75 * @return void
76 */
77 public function enqueue_user_language_script() {
78 $this->enqueue_script( 'language-' . YoastSEO()->helpers->language->get_researcher_language() );
79 }
80
81 /**
82 * Registers scripts based on it's parameters.
83 *
84 * @param WPSEO_Admin_Asset $script The script to register.
85 *
86 * @return void
87 */
88 public function register_script( WPSEO_Admin_Asset $script ) {
89 $url = $script->get_src() ? $this->get_url( $script, WPSEO_Admin_Asset::TYPE_JS ) : false;
90 $args = [
91 'in_footer' => $script->is_in_footer(),
92 ];
93
94 if ( $script->get_strategy() !== '' ) {
95 $args['strategy'] = $script->get_strategy();
96 }
97
98 wp_register_script(
99 $this->prefix . $script->get_name(),
100 $url,
101 $script->get_deps(),
102 $script->get_version(),
103 $args,
104 );
105
106 if ( in_array( 'wp-i18n', $script->get_deps(), true ) ) {
107 wp_set_script_translations( $this->prefix . $script->get_name(), 'wordpress-seo' );
108 }
109 }
110
111 /**
112 * Registers styles based on it's parameters.
113 *
114 * @param WPSEO_Admin_Asset $style The style to register.
115 *
116 * @return void
117 */
118 public function register_style( WPSEO_Admin_Asset $style ) {
119 wp_register_style(
120 $this->prefix . $style->get_name(),
121 $this->get_url( $style, WPSEO_Admin_Asset::TYPE_CSS ),
122 $style->get_deps(),
123 $style->get_version(),
124 $style->get_media(),
125 );
126 }
127
128 /**
129 * Calls the functions that register scripts and styles with the scripts and styles to be registered as arguments.
130 *
131 * @return void
132 */
133 public function register_assets() {
134 $this->register_scripts( $this->scripts_to_be_registered() );
135 $this->register_styles( $this->styles_to_be_registered() );
136 }
137
138 /**
139 * Registers all the scripts passed to it.
140 *
141 * @param array $scripts The scripts passed to it.
142 *
143 * @return void
144 */
145 public function register_scripts( $scripts ) {
146 foreach ( $scripts as $script ) {
147 $script = new WPSEO_Admin_Asset( $script );
148 $this->register_script( $script );
149 }
150 }
151
152 /**
153 * Registers all the styles it receives.
154 *
155 * @param array $styles Styles that need to be registered.
156 *
157 * @return void
158 */
159 public function register_styles( $styles ) {
160 foreach ( $styles as $style ) {
161 $style = new WPSEO_Admin_Asset( $style );
162 $this->register_style( $style );
163 }
164 }
165
166 /**
167 * Localizes the script.
168 *
169 * @param string $handle The script handle.
170 * @param string $object_name The object name.
171 * @param array $data The l10n data.
172 *
173 * @return void
174 */
175 public function localize_script( $handle, $object_name, $data ) {
176 wp_localize_script( $this->prefix . $handle, $object_name, $data );
177 }
178
179 /**
180 * Adds an inline script.
181 *
182 * @param string $handle The script handle.
183 * @param string $data The l10n data.
184 * @param string $position Optional. Whether to add the inline script before the handle or after.
185 *
186 * @return void
187 */
188 public function add_inline_script( $handle, $data, $position = 'after' ) {
189 wp_add_inline_script( $this->prefix . $handle, $data, $position );
190 }
191
192 /**
193 * A list of styles that shouldn't be registered but are needed in other locations in the plugin.
194 *
195 * @return array
196 */
197 public function special_styles() {
198 $flat_version = $this->flatten_version( WPSEO_VERSION );
199 $asset_args = [
200 'name' => 'inside-editor',
201 'src' => 'inside-editor-' . $flat_version,
202 ];
203
204 return [ 'inside-editor' => new WPSEO_Admin_Asset( $asset_args ) ];
205 }
206
207 /**
208 * Flattens a version number for use in a filename.
209 *
210 * @param string $version The original version number.
211 *
212 * @return string The flattened version number.
213 */
214 public function flatten_version( $version ) {
215 $parts = explode( '.', $version );
216
217 if ( count( $parts ) === 2 && preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
218 $parts[] = '0';
219 }
220
221 return implode( '', $parts );
222 }
223
224 /**
225 * Creates a default location object for use in the admin asset manager.
226 *
227 * @return WPSEO_Admin_Asset_Location The location to use in the asset manager.
228 */
229 public static function create_default_location() {
230 if ( defined( 'YOAST_SEO_DEV_SERVER' ) && YOAST_SEO_DEV_SERVER ) {
231 $url = defined( 'YOAST_SEO_DEV_SERVER_URL' ) ? YOAST_SEO_DEV_SERVER_URL : WPSEO_Admin_Asset_Dev_Server_Location::DEFAULT_URL;
232
233 return new WPSEO_Admin_Asset_Dev_Server_Location( $url );
234 }
235
236 return new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE, false );
237 }
238
239 /**
240 * Checks if the given script is enqueued.
241 *
242 * @param string $script The script to check.
243 *
244 * @return bool True when the script is enqueued.
245 */
246 public function is_script_enqueued( $script ) {
247 return wp_script_is( $this->prefix . $script );
248 }
249
250 /**
251 * Gets the list of Elementor dependencies.
252 *
253 * @return array<string> The array of elementor dependencies.
254 */
255 protected function get_elementor_dependencies() {
256 $dependencies = [
257 'backbone-marionette',
258 'elementor-common-modules',
259 self::PREFIX . 'api-client',
260 self::PREFIX . 'externals-components',
261 self::PREFIX . 'externals-contexts',
262 self::PREFIX . 'externals-redux',
263 ];
264 // Conditionally add Elementor v2 dependency if available.
265 if ( wp_script_is( 'elementor-v2-editor-app-bar', 'registered' ) ) {
266 $dependencies[] = 'elementor-v2-editor-app-bar';
267 }
268 return $dependencies;
269 }
270
271 /**
272 * Returns the scripts that need to be registered.
273 *
274 * @todo Data format is not self-documenting. Needs explanation inline. R.
275 *
276 * @return array The scripts that need to be registered.
277 */
278 protected function scripts_to_be_registered() {
279 $header_scripts = [
280 'admin-global',
281 'block-editor',
282 'classic-editor',
283 'post-edit',
284 'help-scout-beacon',
285 'redirect-old-features-tab',
286 ];
287 $elementor_dependencies = $this->get_elementor_dependencies();
288 $additional_dependencies = [
289 'analysis-worker' => [ self::PREFIX . 'analysis-package' ],
290 'api-client' => [ 'wp-api' ],
291 'crawl-settings' => [ 'jquery' ],
292 'dashboard-widget' => [ self::PREFIX . 'api-client' ],
293 'wincher-dashboard-widget' => [ self::PREFIX . 'api-client' ],
294 'editor-modules' => [ 'jquery' ],
295 'elementor' => $elementor_dependencies,
296 'indexation' => [
297 'jquery-ui-core',
298 'jquery-ui-progressbar',
299 ],
300 'first-time-configuration' => [
301 self::PREFIX . 'api-client',
302 self::PREFIX . 'externals-components',
303 self::PREFIX . 'externals-contexts',
304 self::PREFIX . 'externals-redux',
305 ],
306 'integrations-page' => [
307 self::PREFIX . 'api-client',
308 self::PREFIX . 'externals-components',
309 self::PREFIX . 'externals-contexts',
310 self::PREFIX . 'externals-redux',
311 ],
312 'post-edit' => [
313 self::PREFIX . 'api-client',
314 self::PREFIX . 'block-editor',
315 self::PREFIX . 'externals-components',
316 self::PREFIX . 'externals-contexts',
317 self::PREFIX . 'externals-redux',
318 ],
319 'reindex-links' => [
320 'jquery-ui-core',
321 'jquery-ui-progressbar',
322 ],
323 'settings' => [
324 'jquery-ui-core',
325 'jquery-ui-progressbar',
326 self::PREFIX . 'api-client',
327 self::PREFIX . 'externals-components',
328 self::PREFIX . 'externals-contexts',
329 self::PREFIX . 'externals-redux',
330 ],
331 'term-edit' => [
332 self::PREFIX . 'api-client',
333 self::PREFIX . 'classic-editor',
334 self::PREFIX . 'externals-components',
335 self::PREFIX . 'externals-contexts',
336 self::PREFIX . 'externals-redux',
337 ],
338 'general-page' => [
339 self::PREFIX . 'api-client',
340 ],
341 ];
342
343 $plugin_scripts = $this->load_generated_asset_file(
344 [
345 'asset_file' => __DIR__ . '/../src/generated/assets/plugin.php',
346 'ext_length' => 3,
347 'additional_deps' => $additional_dependencies,
348 'header_scripts' => $header_scripts,
349 ],
350 );
351 $external_scripts = $this->load_generated_asset_file(
352 [
353 'asset_file' => __DIR__ . '/../src/generated/assets/externals.php',
354 'ext_length' => 3,
355 'suffix' => '-package',
356 'base_dir' => 'externals/',
357 'additional_deps' => $additional_dependencies,
358 'header_scripts' => $header_scripts,
359 ],
360 );
361 $language_scripts = $this->load_generated_asset_file(
362 [
363 'asset_file' => __DIR__ . '/../src/generated/assets/languages.php',
364 'ext_length' => 3,
365 'suffix' => '-language',
366 'base_dir' => 'languages/',
367 'additional_deps' => $additional_dependencies,
368 'header_scripts' => $header_scripts,
369 ],
370 );
371 $renamed_scripts = $this->load_renamed_scripts();
372
373 $scripts = array_merge(
374 $plugin_scripts,
375 $external_scripts,
376 $language_scripts,
377 $renamed_scripts,
378 );
379
380 $scripts['installation-success'] = [
381 'name' => 'installation-success',
382 'src' => 'installation-success.js',
383 'deps' => [
384 'wp-a11y',
385 'wp-dom-ready',
386 'wp-components',
387 'wp-element',
388 'wp-i18n',
389 self::PREFIX . 'components-new-package',
390 self::PREFIX . 'externals-components',
391 ],
392 'version' => $scripts['installation-success']['version'],
393 ];
394
395 $scripts['post-edit-classic'] = [
396 'name' => 'post-edit-classic',
397 'src' => $scripts['post-edit']['src'],
398 'deps' => array_map(
399 static function ( $dep ) {
400 if ( $dep === self::PREFIX . 'block-editor' ) {
401 return self::PREFIX . 'classic-editor';
402 }
403 return $dep;
404 },
405 $scripts['post-edit']['deps'],
406 ),
407 'in_footer' => ! in_array( 'post-edit-classic', $header_scripts, true ),
408 'version' => $scripts['post-edit']['version'],
409 ];
410
411 $scripts['workouts'] = [
412 'name' => 'workouts',
413 'src' => 'workouts.js',
414 'deps' => [
415 'clipboard',
416 'lodash',
417 'wp-api-fetch',
418 'wp-a11y',
419 'wp-components',
420 'wp-compose',
421 'wp-data',
422 'wp-dom-ready',
423 'wp-element',
424 'wp-i18n',
425 self::PREFIX . 'externals-components',
426 self::PREFIX . 'externals-contexts',
427 self::PREFIX . 'externals-redux',
428 self::PREFIX . 'analysis',
429 self::PREFIX . 'components-new-package',
430 ],
431 'version' => $scripts['workouts']['version'],
432 ];
433
434 // Add the current language to every script that requires the analysis package.
435 foreach ( $scripts as $name => $script ) {
436 if ( substr( $name, -8 ) === 'language' ) {
437 continue;
438 }
439 if ( in_array( self::PREFIX . 'analysis-package', $script['deps'], true ) ) {
440 $scripts[ $name ]['deps'][] = self::PREFIX . YoastSEO()->helpers->language->get_researcher_language() . '-language';
441 }
442 }
443
444 return $scripts;
445 }
446
447 /**
448 * Loads a generated asset file.
449 *
450 * @param array $args {
451 * The arguments.
452 *
453 * @type string $asset_file The asset file to load.
454 * @type int $ext_length The length of the extension, including suffix, of the filename.
455 * @type string $suffix Optional. The suffix of the asset name.
456 * @type array<string, string[]> $additional_deps Optional. The additional dependencies assets may have.
457 * @type string $base_dir Optional. The base directory of the asset.
458 * @type string[] $header_scripts Optional. The script names that should be in the header.
459 * }
460 *
461 * @return array {
462 * The scripts to be registered.
463 *
464 * @type string $name The name of the asset.
465 * @type string $src The src of the asset.
466 * @type string[] $deps The dependenies of the asset.
467 * @type bool $in_footer Whether or not the asset should be in the footer.
468 * }
469 */
470 protected function load_generated_asset_file( $args ) {
471 $args = wp_parse_args(
472 $args,
473 [
474 'suffix' => '',
475 'additional_deps' => [],
476 'base_dir' => '',
477 'header_scripts' => [],
478 ],
479 );
480 $scripts = [];
481 $assets = require $args['asset_file'];
482 foreach ( $assets as $file => $data ) {
483 $name = substr( $file, 0, -$args['ext_length'] );
484 $name = strtolower( preg_replace( '/([A-Z])/', '-$1', $name ) );
485 $name .= $args['suffix'];
486
487 $deps = $data['dependencies'];
488 if ( isset( $args['additional_deps'][ $name ] ) ) {
489 $deps = array_merge( $deps, $args['additional_deps'][ $name ] );
490 }
491
492 $scripts[ $name ] = [
493 'name' => $name,
494 'src' => $args['base_dir'] . $file,
495 'deps' => $deps,
496 'in_footer' => ! in_array( $name, $args['header_scripts'], true ),
497 'version' => $data['version'],
498 ];
499 }
500
501 return $scripts;
502 }
503
504 /**
505 * Loads the scripts that should be renamed for BC.
506 *
507 * @return array {
508 * The scripts to be registered.
509 *
510 * @type string $name The name of the asset.
511 * @type string $src The src of the asset.
512 * @type string[] $deps The dependenies of the asset.
513 * @type bool $in_footer Whether or not the asset should be in the footer.
514 * }
515 */
516 protected function load_renamed_scripts() {
517 $scripts = [];
518 $renamed_scripts = [
519 'admin-global-script' => 'admin-global',
520 'analysis' => 'analysis-package',
521 'analysis-report' => 'analysis-report-package',
522 'api' => 'api-client',
523 'commons' => 'commons-package',
524 'edit-page' => 'edit-page-script',
525 'draft-js' => 'draft-js-package',
526 'feature-flag' => 'feature-flag-package',
527 'helpers' => 'helpers-package',
528 'jed' => 'jed-package',
529 'chart.js' => 'chart.js-package',
530 'network-admin-script' => 'network-admin',
531 'redux' => 'redux-package',
532 'replacement-variable-editor' => 'replacement-variable-editor-package',
533 'search-metadata-previews' => 'search-metadata-previews-package',
534 'social-metadata-forms' => 'social-metadata-forms-package',
535 'styled-components' => 'styled-components-package',
536 'style-guide' => 'style-guide-package',
537 'yoast-components' => 'components-new-package',
538 ];
539
540 foreach ( $renamed_scripts as $original => $replacement ) {
541 $scripts[] = [
542 'name' => $original,
543 'src' => false,
544 'deps' => [ self::PREFIX . $replacement ],
545 ];
546 }
547
548 return $scripts;
549 }
550
551 /**
552 * Returns the styles that need to be registered.
553 *
554 * @todo Data format is not self-documenting. Needs explanation inline. R.
555 *
556 * @return array Styles that need to be registered.
557 */
558 protected function styles_to_be_registered() {
559 $flat_version = $this->flatten_version( WPSEO_VERSION );
560
561 return [
562 [
563 'name' => 'admin-css',
564 'src' => 'yst_plugin_tools-' . $flat_version,
565 'deps' => [ self::PREFIX . 'toggle-switch' ],
566 ],
567 [
568 'name' => 'toggle-switch',
569 'src' => 'toggle-switch-' . $flat_version,
570 ],
571 [
572 'name' => 'dismissible',
573 'src' => 'wpseo-dismissible-' . $flat_version,
574 ],
575 [
576 'name' => 'notifications',
577 'src' => 'notifications-' . $flat_version,
578 ],
579 [
580 'name' => 'alert',
581 'src' => 'alerts-' . $flat_version,
582 ],
583 [
584 'name' => 'edit-page',
585 'src' => 'edit-page-' . $flat_version,
586 ],
587 [
588 'name' => 'featured-image',
589 'src' => 'featured-image-' . $flat_version,
590 ],
591 [
592 'name' => 'metabox-css',
593 'src' => 'metabox-' . $flat_version,
594 'deps' => [
595 self::PREFIX . 'admin-css',
596 self::PREFIX . 'tailwind',
597 'wp-components',
598 ],
599 ],
600 [
601 'name' => 'block-editor',
602 'src' => 'block-editor-' . $flat_version,
603 ],
604 [
605 'name' => 'ai-generator',
606 'src' => 'ai-generator-' . $flat_version,
607 'deps' => [
608 self::PREFIX . 'ai-frontend',
609 self::PREFIX . 'tailwind',
610 self::PREFIX . 'introductions',
611 ],
612 ],
613 [
614 'name' => 'ai-fix-assessments',
615 'src' => 'ai-fix-assessments-' . $flat_version,
616 ],
617 [
618 'name' => 'ai-frontend',
619 'src' => 'ai-frontend-' . $flat_version,
620 ],
621 [
622 'name' => 'introductions',
623 'src' => 'introductions-' . $flat_version,
624 'deps' => [ self::PREFIX . 'tailwind' ],
625 ],
626 [
627 'name' => 'wp-dashboard',
628 'src' => 'dashboard-' . $flat_version,
629 ],
630 [
631 'name' => 'scoring',
632 'src' => 'yst_seo_score-' . $flat_version,
633 ],
634 [
635 'name' => 'adminbar',
636 'src' => 'adminbar-' . $flat_version,
637 'deps' => [
638 'admin-bar',
639 ],
640 ],
641 [
642 'name' => 'primary-category',
643 'src' => 'metabox-primary-category-' . $flat_version,
644 ],
645 [
646 'name' => 'admin-global',
647 'src' => 'admin-global-' . $flat_version,
648 'deps' => [ self::PREFIX . 'tailwind' ],
649 ],
650 [
651 'name' => 'filter-explanation',
652 'src' => 'filter-explanation-' . $flat_version,
653 ],
654 [
655 'name' => 'monorepo',
656 'src' => 'monorepo-' . $flat_version,
657 ],
658 [
659 'name' => 'structured-data-blocks',
660 'src' => 'structured-data-blocks-' . $flat_version,
661 'deps' => [
662 'dashicons',
663 'forms',
664 'wp-edit-blocks',
665 ],
666 ],
667 [
668 'name' => 'elementor',
669 'src' => 'elementor-' . $flat_version,
670 ],
671 [
672 'name' => 'tailwind',
673 'src' => 'tailwind-' . $flat_version,
674 // Note: The RTL suffix is not added here.
675 // Tailwind and our UI library provide styling that should be standalone compatible with RTL.
676 // To make it easier we should use the logical properties and values when possible.
677 // If there are exceptions, we can use the Tailwind modifier, e.g. `rtl:yst-space-x-reverse`.
678 'rtl' => false,
679 ],
680 [
681 'name' => 'new-settings',
682 'src' => 'new-settings-' . $flat_version,
683 'deps' => [ self::PREFIX . 'tailwind' ],
684 ],
685 [
686 'name' => 'redirects',
687 'src' => 'redirects-' . $flat_version,
688 'deps' => [ self::PREFIX . 'tailwind' ],
689 ],
690 [
691 'name' => 'black-friday-banner',
692 'src' => 'black-friday-banner-' . $flat_version,
693 'deps' => [ self::PREFIX . 'tailwind' ],
694 ],
695 [
696 'name' => 'academy',
697 'src' => 'academy-' . $flat_version,
698 'deps' => [ self::PREFIX . 'tailwind' ],
699 ],
700 [
701 'name' => 'general-page',
702 'src' => 'general-page-' . $flat_version,
703 'deps' => [ self::PREFIX . 'tailwind' ],
704 ],
705 [
706 'name' => 'installation-success',
707 'src' => 'installation-success-' . $flat_version,
708 'deps' => [ self::PREFIX . 'tailwind' ],
709 ],
710 [
711 'name' => 'support',
712 'src' => 'support-' . $flat_version,
713 'deps' => [ self::PREFIX . 'tailwind' ],
714 ],
715 [
716 'name' => 'workouts',
717 'src' => 'workouts-' . $flat_version,
718 'deps' => [
719 self::PREFIX . 'monorepo',
720 ],
721 ],
722 [
723 'name' => 'first-time-configuration',
724 'src' => 'first-time-configuration-' . $flat_version,
725 'deps' => [ self::PREFIX . 'tailwind' ],
726 ],
727 [
728 'name' => 'inside-editor',
729 'src' => 'inside-editor-' . $flat_version,
730 ],
731 [
732 'name' => 'plans',
733 'src' => 'plans-' . $flat_version,
734 'deps' => [ self::PREFIX . 'tailwind' ],
735 ],
736 ];
737 }
738
739 /**
740 * Determines the URL of the asset.
741 *
742 * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
743 * @param string $type The type of asset. Usually JS or CSS.
744 *
745 * @return string The URL of the asset.
746 */
747 protected function get_url( WPSEO_Admin_Asset $asset, $type ) {
748 $scheme = wp_parse_url( $asset->get_src(), PHP_URL_SCHEME );
749 if ( in_array( $scheme, [ 'http', 'https' ], true ) ) {
750 return $asset->get_src();
751 }
752
753 return $this->asset_location->get_url( $asset, $type );
754 }
755 }
756