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

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