PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 / src / helpers / asset-helper.php

asset-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at src/helpers/asset-helper.php

99 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Helpers;
4
5 /**
6 * A helper object for author archives.
7 */
8 class Asset_Helper {
9
10 /**
11 * Recursively retrieves all dependency urls of a given handle.
12 *
13 * @param string $handle The handle.
14 *
15 * @return string[] All dependency urls of the given handle.
16 */
17 public function get_dependency_urls_by_handle( $handle ) {
18 $urls = [];
19
20 foreach ( $this->get_dependency_handles( $handle ) as $other_handle ) {
21 $urls[ $other_handle ] = $this->get_asset_url( $other_handle );
22 }
23
24 return $urls;
25 }
26
27 /**
28 * Recursively retrieves all dependencies of a given handle.
29 *
30 * @param string $handle The handle.
31 *
32 * @return string[]|bool All dependencies of the given handle.
33 */
34 public function get_dependency_handles( $handle ) {
35 $scripts = \wp_scripts();
36
37 if ( ! isset( $scripts->registered[ $handle ] ) ) {
38 return false;
39 }
40
41 $obj = $scripts->registered[ $handle ];
42 $deps = $obj->deps;
43 foreach ( $obj->deps as $other_handle ) {
44 $nested_deps = $this->get_dependency_handles( $other_handle );
45 if ( ! $nested_deps ) {
46 continue;
47 }
48
49 // Place nested dependencies before primary dependencies, they need to be loaded first.
50 $deps = \array_merge( $nested_deps, $deps );
51 }
52
53 // Array unique keeps the first of each element so dependencies will be as early as they're required.
54 return \array_values( \array_unique( $deps ) );
55 }
56
57 /**
58 * Gets the URL of a given asset.
59 *
60 * This logic is copied from WP_Scripts::do_item as unfortunately that logic is not properly isolated.
61 *
62 * @param string $handle The handle of the asset.
63 *
64 * @return string|false The URL of the asset or false if the asset does not exist.
65 */
66 public function get_asset_url( $handle ) {
67 $scripts = \wp_scripts();
68
69 if ( ! isset( $scripts->registered[ $handle ] ) ) {
70 return false;
71 }
72
73 $obj = $scripts->registered[ $handle ];
74
75 if ( $obj->ver === null ) {
76 $ver = '';
77 }
78 else {
79 $ver = ( $obj->ver ) ? $obj->ver : $scripts->default_version;
80 }
81 if ( isset( $scripts->args[ $handle ] ) ) {
82 $ver = ( $ver ) ? $ver . '&amp;' . $scripts->args[ $handle ] : $scripts->args[ $handle ];
83 }
84
85 $src = $obj->src;
86
87 if ( ! \preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && \strpos( $src, $scripts->content_url ) === 0 ) ) {
88 $src = $scripts->base_url . $src;
89 }
90
91 if ( ! empty( $ver ) ) {
92 $src = \add_query_arg( 'ver', $ver, $src );
93 }
94
95 /** This filter is documented in wp-includes/class.wp-scripts.php */
96 return \esc_url( \apply_filters( 'script_loader_src', $src, $handle ) );
97 }
98 }
99