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

720 lines 19.5 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 $scripts['first-time-configuration'] = [
377 'name' => 'first-time-configuration',
378 'src' => 'first-time-configuration.js',
379 'deps' => [
380 'lodash',
381 'wp-api-fetch',
382 'wp-a11y',
383 'wp-components',
384 'wp-compose',
385 'wp-data',
386 'wp-dom-ready',
387 'wp-element',
388 'wp-i18n',
389 self::PREFIX . 'api-client',
390 self::PREFIX . 'externals-components',
391 self::PREFIX . 'externals-contexts',
392 self::PREFIX . 'externals-redux',
393 self::PREFIX . 'analysis',
394 self::PREFIX . 'react-select',
395 self::PREFIX . 'yoast-components',
396 ],
397 'version' => $scripts['first-time-configuration']['version'],
398 ];
399
400 // Add the current language to every script that requires the analysis package.
401 foreach ( $scripts as $name => $script ) {
402 if ( substr( $name, -8 ) === 'language' ) {
403 continue;
404 }
405 if ( in_array( self::PREFIX . 'analysis-package', $script['deps'], true ) ) {
406 $scripts[ $name ]['deps'][] = self::PREFIX . YoastSEO()->helpers->language->get_researcher_language() . '-language';
407 }
408 }
409
410 return $scripts;
411 }
412
413 /**
414 * Loads a generated asset file.
415 *
416 * @param array $args {
417 * The arguments.
418 *
419 * @type string $asset_file The asset file to load.
420 * @type int $ext_length The length of the extension, including suffix, of the filename.
421 * @type string $suffix Optional. The suffix of the asset name.
422 * @type array<string, string[]> $additional_deps Optional. The additional dependencies assets may have.
423 * @type string $base_dir Optional. The base directory of the asset.
424 * @type string[] $header_scripts Optional. The script names that should be in the header.
425 * }
426 *
427 * @return array {
428 * The scripts to be registered.
429 *
430 * @type string $name The name of the asset.
431 * @type string $src The src of the asset.
432 * @type string[] $deps The dependenies of the asset.
433 * @type bool $in_footer Whether or not the asset should be in the footer.
434 * }
435 */
436 protected function load_generated_asset_file( $args ) {
437 $args = wp_parse_args(
438 $args,
439 [
440 'suffix' => '',
441 'additional_deps' => [],
442 'base_dir' => '',
443 'header_scripts' => [],
444 ]
445 );
446 $scripts = [];
447 $assets = require $args['asset_file'];
448 foreach ( $assets as $file => $data ) {
449 $name = substr( $file, 0, -$args['ext_length'] );
450 $name = strtolower( preg_replace( '/([A-Z])/', '-$1', $name ) );
451 $name .= $args['suffix'];
452
453 $deps = $data['dependencies'];
454 if ( isset( $args['additional_deps'][ $name ] ) ) {
455 $deps = array_merge( $deps, $args['additional_deps'][ $name ] );
456 }
457
458 $scripts[ $name ] = [
459 'name' => $name,
460 'src' => $args['base_dir'] . $file,
461 'deps' => $deps,
462 'in_footer' => ! in_array( $name, $args['header_scripts'], true ),
463 'version' => $data['version'],
464 ];
465 }
466
467 return $scripts;
468 }
469
470 /**
471 * Loads the select2 scripts.
472 *
473 * @return array {
474 * The scripts to be registered.
475 *
476 * @type string $name The name of the asset.
477 * @type string $src The src of the asset.
478 * @type string[] $deps The dependenies of the asset.
479 * @type bool $in_footer Whether or not the asset should be in the footer.
480 * }
481 */
482 protected function load_select2_scripts() {
483 $scripts = [];
484 $select2_language = 'en';
485 $user_locale = \get_user_locale();
486 $language = WPSEO_Language_Utils::get_language( $user_locale );
487
488 if ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$user_locale}.js" ) ) {
489 $select2_language = $user_locale; // Chinese and some others use full locale.
490 }
491 elseif ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$language}.js" ) ) {
492 $select2_language = $language;
493 }
494
495 $scripts['select2'] = [
496 'name' => 'select2',
497 'src' => false,
498 'deps' => [
499 self::PREFIX . 'select2-translations',
500 self::PREFIX . 'select2-core',
501 ],
502 ];
503 $scripts['select2-core'] = [
504 'name' => 'select2-core',
505 'src' => 'select2/select2.full.min.js',
506 'deps' => [
507 'jquery',
508 ],
509 'version' => '4.0.13',
510 ];
511 $scripts['select2-translations'] = [
512 'name' => 'select2-translations',
513 'src' => 'select2/i18n/' . $select2_language . '.js',
514 'deps' => [
515 'jquery',
516 self::PREFIX . 'select2-core',
517 ],
518 'version' => '4.0.13',
519 ];
520
521 return $scripts;
522 }
523
524 /**
525 * Loads the scripts that should be renamed for BC.
526 *
527 * @return array {
528 * The scripts to be registered.
529 *
530 * @type string $name The name of the asset.
531 * @type string $src The src of the asset.
532 * @type string[] $deps The dependenies of the asset.
533 * @type bool $in_footer Whether or not the asset should be in the footer.
534 * }
535 */
536 protected function load_renamed_scripts() {
537 $scripts = [];
538 $renamed_scripts = [
539 'admin-global-script' => 'admin-global',
540 'analysis' => 'analysis-package',
541 'analysis-report' => 'analysis-report-package',
542 'api' => 'api-client',
543 'commons' => 'commons-package',
544 'edit-page' => 'edit-page-script',
545 'draft-js' => 'draft-js-package',
546 'feature-flag' => 'feature-flag-package',
547 'helpers' => 'helpers-package',
548 'jed' => 'jed-package',
549 'legacy-components' => 'components-package',
550 'network-admin-script' => 'network-admin',
551 'redux' => 'redux-package',
552 'replacement-variable-editor' => 'replacement-variable-editor-package',
553 'search-metadata-previews' => 'search-metadata-previews-package',
554 'social-metadata-forms' => 'social-metadata-forms-package',
555 'styled-components' => 'styled-components-package',
556 'style-guide' => 'style-guide-package',
557 'yoast-components' => 'components-new-package',
558 ];
559
560 foreach ( $renamed_scripts as $original => $replacement ) {
561 $scripts[] = [
562 'name' => $original,
563 'src' => false,
564 'deps' => [ self::PREFIX . $replacement ],
565 ];
566 }
567
568 return $scripts;
569 }
570
571 /**
572 * Returns the styles that need to be registered.
573 *
574 * @todo Data format is not self-documenting. Needs explanation inline. R.
575 *
576 * @return array Styles that need to be registered.
577 */
578 protected function styles_to_be_registered() {
579 $flat_version = $this->flatten_version( WPSEO_VERSION );
580
581 return [
582 [
583 'name' => 'admin-css',
584 'src' => 'yst_plugin_tools-' . $flat_version,
585 'deps' => [ self::PREFIX . 'toggle-switch' ],
586 ],
587 [
588 'name' => 'toggle-switch',
589 'src' => 'toggle-switch-' . $flat_version,
590 ],
591 [
592 'name' => 'dismissible',
593 'src' => 'wpseo-dismissible-' . $flat_version,
594 ],
595 [
596 'name' => 'notifications',
597 'src' => 'notifications-' . $flat_version,
598 ],
599 [
600 'name' => 'alert',
601 'src' => 'alerts-' . $flat_version,
602 ],
603 [
604 'name' => 'edit-page',
605 'src' => 'edit-page-' . $flat_version,
606 ],
607 [
608 'name' => 'featured-image',
609 'src' => 'featured-image-' . $flat_version,
610 ],
611 [
612 'name' => 'metabox-css',
613 'src' => 'metabox-' . $flat_version,
614 'deps' => [
615 self::PREFIX . 'select2',
616 self::PREFIX . 'admin-css',
617 'wp-components',
618 ],
619 ],
620 [
621 'name' => 'wp-dashboard',
622 'src' => 'dashboard-' . $flat_version,
623 ],
624 [
625 'name' => 'scoring',
626 'src' => 'yst_seo_score-' . $flat_version,
627 ],
628 [
629 'name' => 'adminbar',
630 'src' => 'adminbar-' . $flat_version,
631 'deps' => [
632 'admin-bar',
633 ],
634 ],
635 [
636 'name' => 'primary-category',
637 'src' => 'metabox-primary-category-' . $flat_version,
638 ],
639 [
640 'name' => 'select2',
641 'src' => 'select2/select2',
642 'suffix' => '.min',
643 'version' => '4.0.13',
644 'rtl' => false,
645 ],
646 [
647 'name' => 'admin-global',
648 'src' => 'admin-global-' . $flat_version,
649 ],
650 [
651 'name' => 'yoast-components',
652 'src' => 'yoast-components-' . $flat_version,
653 ],
654 [
655 'name' => 'extensions',
656 'src' => 'yoast-extensions-' . $flat_version,
657 'deps' => [
658 'wp-components',
659 ],
660 ],
661 [
662 'name' => 'filter-explanation',
663 'src' => 'filter-explanation-' . $flat_version,
664 ],
665 [
666 'name' => 'search-appearance',
667 'src' => 'search-appearance-' . $flat_version,
668 'deps' => [
669 self::PREFIX . 'monorepo',
670 ],
671 ],
672 [
673 'name' => 'monorepo',
674 'src' => 'monorepo-' . $flat_version,
675 ],
676 [
677 'name' => 'structured-data-blocks',
678 'src' => 'structured-data-blocks-' . $flat_version,
679 'deps' => [ 'wp-edit-blocks' ],
680 ],
681 [
682 'name' => 'schema-blocks',
683 'src' => 'schema-blocks-' . $flat_version,
684 ],
685 [
686 'name' => 'elementor',
687 'src' => 'elementor-' . $flat_version,
688 ],
689 [
690 'name' => 'tailwind',
691 'src' => 'tailwind-' . $flat_version,
692 ],
693 [
694 'name' => 'workouts',
695 'src' => 'workouts-' . $flat_version,
696 'deps' => [
697 self::PREFIX . 'monorepo',
698 ],
699 ],
700 ];
701 }
702
703 /**
704 * Determines the URL of the asset.
705 *
706 * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
707 * @param string $type The type of asset. Usually JS or CSS.
708 *
709 * @return string The URL of the asset.
710 */
711 protected function get_url( WPSEO_Admin_Asset $asset, $type ) {
712 $scheme = wp_parse_url( $asset->get_src(), PHP_URL_SCHEME );
713 if ( in_array( $scheme, [ 'http', 'https' ], true ) ) {
714 return $asset->get_src();
715 }
716
717 return $this->asset_location->get_url( $asset, $type );
718 }
719 }
720