Edit File by line
/home/zeestwma/redstone.../wp-inclu...
File: class-wp-script-modules.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Script Modules API: WP_Script_Modules class.
[2] Fix | Delete
*
[3] Fix | Delete
* Native support for ES Modules and Import Maps.
[4] Fix | Delete
*
[5] Fix | Delete
* @package WordPress
[6] Fix | Delete
* @subpackage Script Modules
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Core class used to register script modules.
[11] Fix | Delete
*
[12] Fix | Delete
* @since 6.5.0
[13] Fix | Delete
*/
[14] Fix | Delete
class WP_Script_Modules {
[15] Fix | Delete
/**
[16] Fix | Delete
* Holds the registered script modules, keyed by script module identifier.
[17] Fix | Delete
*
[18] Fix | Delete
* @since 6.5.0
[19] Fix | Delete
* @var array<string, array<string, mixed>>
[20] Fix | Delete
*/
[21] Fix | Delete
private $registered = array();
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* An array of IDs for queued script modules.
[25] Fix | Delete
*
[26] Fix | Delete
* @since 6.9.0
[27] Fix | Delete
* @var string[]
[28] Fix | Delete
*/
[29] Fix | Delete
private $queue = array();
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Holds the script module identifiers that have been printed.
[33] Fix | Delete
*
[34] Fix | Delete
* @since 6.9.0
[35] Fix | Delete
* @var string[]
[36] Fix | Delete
*/
[37] Fix | Delete
private $done = array();
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Tracks whether the @wordpress/a11y script module is available.
[41] Fix | Delete
*
[42] Fix | Delete
* Some additional HTML is required on the page for the module to work. Track
[43] Fix | Delete
* whether it's available to print at the appropriate time.
[44] Fix | Delete
*
[45] Fix | Delete
* @since 6.7.0
[46] Fix | Delete
* @var bool
[47] Fix | Delete
*/
[48] Fix | Delete
private $a11y_available = false;
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Holds a mapping of dependents (as IDs) for a given script ID.
[52] Fix | Delete
* Used to optimize recursive dependency tree checks.
[53] Fix | Delete
*
[54] Fix | Delete
* @since 6.9.0
[55] Fix | Delete
* @var array<string, string[]>
[56] Fix | Delete
*/
[57] Fix | Delete
private $dependents_map = array();
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Holds the valid values for fetchpriority.
[61] Fix | Delete
*
[62] Fix | Delete
* @since 6.9.0
[63] Fix | Delete
* @var string[]
[64] Fix | Delete
*/
[65] Fix | Delete
private $priorities = array(
[66] Fix | Delete
'low',
[67] Fix | Delete
'auto',
[68] Fix | Delete
'high',
[69] Fix | Delete
);
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* Registers the script module if no script module with that script module
[73] Fix | Delete
* identifier has already been registered.
[74] Fix | Delete
*
[75] Fix | Delete
* @since 6.5.0
[76] Fix | Delete
* @since 6.9.0 Added the $args parameter.
[77] Fix | Delete
*
[78] Fix | Delete
* @param string $id The identifier of the script module. Should be unique. It will be used in the
[79] Fix | Delete
* final import map.
[80] Fix | Delete
* @param string $src Optional. Full URL of the script module, or path of the script module relative
[81] Fix | Delete
* to the WordPress root directory. If it is provided and the script module has
[82] Fix | Delete
* not been registered yet, it will be registered.
[83] Fix | Delete
* @param array $deps {
[84] Fix | Delete
* Optional. List of dependencies.
[85] Fix | Delete
*
[86] Fix | Delete
* @type string|array ...$0 {
[87] Fix | Delete
* An array of script module identifiers of the dependencies of this script
[88] Fix | Delete
* module. The dependencies can be strings or arrays. If they are arrays,
[89] Fix | Delete
* they need an `id` key with the script module identifier, and can contain
[90] Fix | Delete
* an `import` key with either `static` or `dynamic`. By default,
[91] Fix | Delete
* dependencies that don't contain an `import` key are considered static.
[92] Fix | Delete
*
[93] Fix | Delete
* @type string $id The script module identifier.
[94] Fix | Delete
* @type string $import Optional. Import type. May be either `static` or
[95] Fix | Delete
* `dynamic`. Defaults to `static`.
[96] Fix | Delete
* }
[97] Fix | Delete
* }
[98] Fix | Delete
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
[99] Fix | Delete
* It is added to the URL as a query string for cache busting purposes. If $version
[100] Fix | Delete
* is set to false, the version number is the currently installed WordPress version.
[101] Fix | Delete
* If $version is set to null, no version is added.
[102] Fix | Delete
* @param array $args {
[103] Fix | Delete
* Optional. An array of additional args. Default empty array.
[104] Fix | Delete
*
[105] Fix | Delete
* @type bool $in_footer Whether to print the script module in the footer. Only relevant to block themes. Default 'false'. Optional.
[106] Fix | Delete
* @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
[107] Fix | Delete
* }
[108] Fix | Delete
*/
[109] Fix | Delete
public function register( string $id, string $src, array $deps = array(), $version = false, array $args = array() ) {
[110] Fix | Delete
if ( '' === $id ) {
[111] Fix | Delete
_doing_it_wrong( __METHOD__, __( 'Non-empty string required for id.' ), '6.9.0' );
[112] Fix | Delete
return;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
if ( ! isset( $this->registered[ $id ] ) ) {
[116] Fix | Delete
$dependencies = array();
[117] Fix | Delete
foreach ( $deps as $dependency ) {
[118] Fix | Delete
if ( is_array( $dependency ) ) {
[119] Fix | Delete
if ( ! isset( $dependency['id'] ) || ! is_string( $dependency['id'] ) ) {
[120] Fix | Delete
_doing_it_wrong( __METHOD__, __( 'Missing required id key in entry among dependencies array.' ), '6.5.0' );
[121] Fix | Delete
continue;
[122] Fix | Delete
}
[123] Fix | Delete
$dependencies[] = array(
[124] Fix | Delete
'id' => $dependency['id'],
[125] Fix | Delete
'import' => isset( $dependency['import'] ) && 'dynamic' === $dependency['import'] ? 'dynamic' : 'static',
[126] Fix | Delete
);
[127] Fix | Delete
} elseif ( is_string( $dependency ) ) {
[128] Fix | Delete
$dependencies[] = array(
[129] Fix | Delete
'id' => $dependency,
[130] Fix | Delete
'import' => 'static',
[131] Fix | Delete
);
[132] Fix | Delete
} else {
[133] Fix | Delete
_doing_it_wrong( __METHOD__, __( 'Entries in dependencies array must be either strings or arrays with an id key.' ), '6.5.0' );
[134] Fix | Delete
}
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
$in_footer = isset( $args['in_footer'] ) && (bool) $args['in_footer'];
[138] Fix | Delete
[139] Fix | Delete
$fetchpriority = 'auto';
[140] Fix | Delete
if ( isset( $args['fetchpriority'] ) ) {
[141] Fix | Delete
if ( $this->is_valid_fetchpriority( $args['fetchpriority'] ) ) {
[142] Fix | Delete
$fetchpriority = $args['fetchpriority'];
[143] Fix | Delete
} else {
[144] Fix | Delete
_doing_it_wrong(
[145] Fix | Delete
__METHOD__,
[146] Fix | Delete
sprintf(
[147] Fix | Delete
/* translators: 1: $fetchpriority, 2: $id */
[148] Fix | Delete
__( 'Invalid fetchpriority `%1$s` defined for `%2$s` during script registration.' ),
[149] Fix | Delete
is_string( $args['fetchpriority'] ) ? $args['fetchpriority'] : gettype( $args['fetchpriority'] ),
[150] Fix | Delete
$id
[151] Fix | Delete
),
[152] Fix | Delete
'6.9.0'
[153] Fix | Delete
);
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
$this->registered[ $id ] = array(
[158] Fix | Delete
'src' => $src,
[159] Fix | Delete
'version' => $version,
[160] Fix | Delete
'dependencies' => $dependencies,
[161] Fix | Delete
'in_footer' => $in_footer,
[162] Fix | Delete
'fetchpriority' => $fetchpriority,
[163] Fix | Delete
);
[164] Fix | Delete
}
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
/**
[168] Fix | Delete
* Gets IDs for queued script modules.
[169] Fix | Delete
*
[170] Fix | Delete
* @since 6.9.0
[171] Fix | Delete
*
[172] Fix | Delete
* @return string[] Script module IDs.
[173] Fix | Delete
*/
[174] Fix | Delete
public function get_queue(): array {
[175] Fix | Delete
return $this->queue;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
/**
[179] Fix | Delete
* Checks if the provided fetchpriority is valid.
[180] Fix | Delete
*
[181] Fix | Delete
* @since 6.9.0
[182] Fix | Delete
*
[183] Fix | Delete
* @param string|mixed $priority Fetch priority.
[184] Fix | Delete
* @return bool Whether valid fetchpriority.
[185] Fix | Delete
*/
[186] Fix | Delete
private function is_valid_fetchpriority( $priority ): bool {
[187] Fix | Delete
return in_array( $priority, $this->priorities, true );
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Sets the fetch priority for a script module.
[192] Fix | Delete
*
[193] Fix | Delete
* @since 6.9.0
[194] Fix | Delete
*
[195] Fix | Delete
* @param string $id Script module identifier.
[196] Fix | Delete
* @param 'auto'|'low'|'high' $priority Fetch priority for the script module.
[197] Fix | Delete
* @return bool Whether setting the fetchpriority was successful.
[198] Fix | Delete
*/
[199] Fix | Delete
public function set_fetchpriority( string $id, string $priority ): bool {
[200] Fix | Delete
if ( ! isset( $this->registered[ $id ] ) ) {
[201] Fix | Delete
return false;
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
if ( '' === $priority ) {
[205] Fix | Delete
$priority = 'auto';
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
if ( ! $this->is_valid_fetchpriority( $priority ) ) {
[209] Fix | Delete
_doing_it_wrong(
[210] Fix | Delete
__METHOD__,
[211] Fix | Delete
/* translators: %s: Invalid fetchpriority. */
[212] Fix | Delete
sprintf( __( 'Invalid fetchpriority: %s' ), $priority ),
[213] Fix | Delete
'6.9.0'
[214] Fix | Delete
);
[215] Fix | Delete
return false;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
$this->registered[ $id ]['fetchpriority'] = $priority;
[219] Fix | Delete
return true;
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Sets whether a script module should be printed in the footer.
[224] Fix | Delete
*
[225] Fix | Delete
* This is only relevant in block themes.
[226] Fix | Delete
*
[227] Fix | Delete
* @since 6.9.0
[228] Fix | Delete
*
[229] Fix | Delete
* @param string $id Script module identifier.
[230] Fix | Delete
* @param bool $in_footer Whether to print in the footer.
[231] Fix | Delete
* @return bool Whether setting the printing location was successful.
[232] Fix | Delete
*/
[233] Fix | Delete
public function set_in_footer( string $id, bool $in_footer ): bool {
[234] Fix | Delete
if ( ! isset( $this->registered[ $id ] ) ) {
[235] Fix | Delete
return false;
[236] Fix | Delete
}
[237] Fix | Delete
$this->registered[ $id ]['in_footer'] = $in_footer;
[238] Fix | Delete
return true;
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
/**
[242] Fix | Delete
* Marks the script module to be enqueued in the page.
[243] Fix | Delete
*
[244] Fix | Delete
* If a src is provided and the script module has not been registered yet, it
[245] Fix | Delete
* will be registered.
[246] Fix | Delete
*
[247] Fix | Delete
* @since 6.5.0
[248] Fix | Delete
* @since 6.9.0 Added the $args parameter.
[249] Fix | Delete
*
[250] Fix | Delete
* @param string $id The identifier of the script module. Should be unique. It will be used in the
[251] Fix | Delete
* final import map.
[252] Fix | Delete
* @param string $src Optional. Full URL of the script module, or path of the script module relative
[253] Fix | Delete
* to the WordPress root directory. If it is provided and the script module has
[254] Fix | Delete
* not been registered yet, it will be registered.
[255] Fix | Delete
* @param array $deps {
[256] Fix | Delete
* Optional. List of dependencies.
[257] Fix | Delete
*
[258] Fix | Delete
* @type string|array ...$0 {
[259] Fix | Delete
* An array of script module identifiers of the dependencies of this script
[260] Fix | Delete
* module. The dependencies can be strings or arrays. If they are arrays,
[261] Fix | Delete
* they need an `id` key with the script module identifier, and can contain
[262] Fix | Delete
* an `import` key with either `static` or `dynamic`. By default,
[263] Fix | Delete
* dependencies that don't contain an `import` key are considered static.
[264] Fix | Delete
*
[265] Fix | Delete
* @type string $id The script module identifier.
[266] Fix | Delete
* @type string $import Optional. Import type. May be either `static` or
[267] Fix | Delete
* `dynamic`. Defaults to `static`.
[268] Fix | Delete
* }
[269] Fix | Delete
* }
[270] Fix | Delete
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
[271] Fix | Delete
* It is added to the URL as a query string for cache busting purposes. If $version
[272] Fix | Delete
* is set to false, the version number is the currently installed WordPress version.
[273] Fix | Delete
* If $version is set to null, no version is added.
[274] Fix | Delete
* @param array $args {
[275] Fix | Delete
* Optional. An array of additional args. Default empty array.
[276] Fix | Delete
*
[277] Fix | Delete
* @type bool $in_footer Whether to print the script module in the footer. Only relevant to block themes. Default 'false'. Optional.
[278] Fix | Delete
* @type 'auto'|'low'|'high' $fetchpriority Fetch priority. Default 'auto'. Optional.
[279] Fix | Delete
* }
[280] Fix | Delete
*/
[281] Fix | Delete
public function enqueue( string $id, string $src = '', array $deps = array(), $version = false, array $args = array() ) {
[282] Fix | Delete
if ( '' === $id ) {
[283] Fix | Delete
_doing_it_wrong( __METHOD__, __( 'Non-empty string required for id.' ), '6.9.0' );
[284] Fix | Delete
return;
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
if ( ! in_array( $id, $this->queue, true ) ) {
[288] Fix | Delete
$this->queue[] = $id;
[289] Fix | Delete
}
[290] Fix | Delete
if ( ! isset( $this->registered[ $id ] ) && $src ) {
[291] Fix | Delete
$this->register( $id, $src, $deps, $version, $args );
[292] Fix | Delete
}
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Unmarks the script module so it will no longer be enqueued in the page.
[297] Fix | Delete
*
[298] Fix | Delete
* @since 6.5.0
[299] Fix | Delete
*
[300] Fix | Delete
* @param string $id The identifier of the script module.
[301] Fix | Delete
*/
[302] Fix | Delete
public function dequeue( string $id ) {
[303] Fix | Delete
$this->queue = array_values( array_diff( $this->queue, array( $id ) ) );
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
/**
[307] Fix | Delete
* Removes a registered script module.
[308] Fix | Delete
*
[309] Fix | Delete
* @since 6.5.0
[310] Fix | Delete
*
[311] Fix | Delete
* @param string $id The identifier of the script module.
[312] Fix | Delete
*/
[313] Fix | Delete
public function deregister( string $id ) {
[314] Fix | Delete
$this->dequeue( $id );
[315] Fix | Delete
unset( $this->registered[ $id ] );
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Adds the hooks to print the import map, enqueued script modules and script
[320] Fix | Delete
* module preloads.
[321] Fix | Delete
*
[322] Fix | Delete
* In classic themes, the script modules used by the blocks are not yet known
[323] Fix | Delete
* when the `wp_head` actions is fired, so it needs to print everything in the
[324] Fix | Delete
* footer.
[325] Fix | Delete
*
[326] Fix | Delete
* @since 6.5.0
[327] Fix | Delete
*/
[328] Fix | Delete
public function add_hooks() {
[329] Fix | Delete
$is_block_theme = wp_is_block_theme();
[330] Fix | Delete
$position = $is_block_theme ? 'wp_head' : 'wp_footer';
[331] Fix | Delete
add_action( $position, array( $this, 'print_import_map' ) );
[332] Fix | Delete
if ( $is_block_theme ) {
[333] Fix | Delete
/*
[334] Fix | Delete
* Modules can only be printed in the head for block themes because only with
[335] Fix | Delete
* block themes will import map be fully populated by modules discovered by
[336] Fix | Delete
* rendering the block template. In classic themes, modules are enqueued during
[337] Fix | Delete
* template rendering, thus the import map must be printed in the footer,
[338] Fix | Delete
* followed by all enqueued modules.
[339] Fix | Delete
*/
[340] Fix | Delete
add_action( 'wp_head', array( $this, 'print_head_enqueued_script_modules' ) );
[341] Fix | Delete
}
[342] Fix | Delete
add_action( 'wp_footer', array( $this, 'print_enqueued_script_modules' ) );
[343] Fix | Delete
add_action( $position, array( $this, 'print_script_module_preloads' ) );
[344] Fix | Delete
[345] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_import_map' ) );
[346] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_enqueued_script_modules' ) );
[347] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_preloads' ) );
[348] Fix | Delete
[349] Fix | Delete
add_action( 'wp_footer', array( $this, 'print_script_module_data' ) );
[350] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_data' ) );
[351] Fix | Delete
add_action( 'wp_footer', array( $this, 'print_a11y_script_module_html' ), 20 );
[352] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_a11y_script_module_html' ), 20 );
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
/**
[356] Fix | Delete
* Gets the highest fetch priority for the provided script IDs.
[357] Fix | Delete
*
[358] Fix | Delete
* @since 6.9.0
[359] Fix | Delete
*
[360] Fix | Delete
* @param string[] $ids Script module IDs.
[361] Fix | Delete
* @return 'auto'|'low'|'high' Highest fetch priority for the provided script module IDs.
[362] Fix | Delete
*/
[363] Fix | Delete
private function get_highest_fetchpriority( array $ids ): string {
[364] Fix | Delete
static $high_priority_index = null;
[365] Fix | Delete
if ( null === $high_priority_index ) {
[366] Fix | Delete
$high_priority_index = count( $this->priorities ) - 1;
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
$highest_priority_index = 0;
[370] Fix | Delete
foreach ( $ids as $id ) {
[371] Fix | Delete
if ( isset( $this->registered[ $id ] ) ) {
[372] Fix | Delete
$highest_priority_index = (int) max(
[373] Fix | Delete
$highest_priority_index,
[374] Fix | Delete
(int) array_search( $this->registered[ $id ]['fetchpriority'], $this->priorities, true )
[375] Fix | Delete
);
[376] Fix | Delete
if ( $high_priority_index === $highest_priority_index ) {
[377] Fix | Delete
break;
[378] Fix | Delete
}
[379] Fix | Delete
}
[380] Fix | Delete
}
[381] Fix | Delete
[382] Fix | Delete
return $this->priorities[ $highest_priority_index ];
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
/**
[386] Fix | Delete
* Prints the enqueued script modules in head.
[387] Fix | Delete
*
[388] Fix | Delete
* This is only used in block themes.
[389] Fix | Delete
*
[390] Fix | Delete
* @since 6.9.0
[391] Fix | Delete
*/
[392] Fix | Delete
public function print_head_enqueued_script_modules() {
[393] Fix | Delete
foreach ( $this->get_sorted_dependencies( $this->queue ) as $id ) {
[394] Fix | Delete
if (
[395] Fix | Delete
isset( $this->registered[ $id ] ) &&
[396] Fix | Delete
! $this->registered[ $id ]['in_footer']
[397] Fix | Delete
) {
[398] Fix | Delete
// If any dependency is set to be printed in footer, skip printing this module in head.
[399] Fix | Delete
$dependencies = array_keys( $this->get_dependencies( array( $id ) ) );
[400] Fix | Delete
foreach ( $dependencies as $dependency_id ) {
[401] Fix | Delete
if (
[402] Fix | Delete
in_array( $dependency_id, $this->queue, true ) &&
[403] Fix | Delete
isset( $this->registered[ $dependency_id ] ) &&
[404] Fix | Delete
$this->registered[ $dependency_id ]['in_footer']
[405] Fix | Delete
) {
[406] Fix | Delete
continue 2;
[407] Fix | Delete
}
[408] Fix | Delete
}
[409] Fix | Delete
$this->print_script_module( $id );
[410] Fix | Delete
}
[411] Fix | Delete
}
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
/**
[415] Fix | Delete
* Prints the enqueued script modules in footer.
[416] Fix | Delete
*
[417] Fix | Delete
* @since 6.5.0
[418] Fix | Delete
*/
[419] Fix | Delete
public function print_enqueued_script_modules() {
[420] Fix | Delete
foreach ( $this->get_sorted_dependencies( $this->queue ) as $id ) {
[421] Fix | Delete
$this->print_script_module( $id );
[422] Fix | Delete
}
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
/**
[426] Fix | Delete
* Prints the enqueued script module using script tags with type="module"
[427] Fix | Delete
* attributes.
[428] Fix | Delete
*
[429] Fix | Delete
* @since 6.9.0
[430] Fix | Delete
*
[431] Fix | Delete
* @param string $id The script module identifier.
[432] Fix | Delete
*/
[433] Fix | Delete
private function print_script_module( string $id ) {
[434] Fix | Delete
if ( in_array( $id, $this->done, true ) || ! in_array( $id, $this->queue, true ) ) {
[435] Fix | Delete
return;
[436] Fix | Delete
}
[437] Fix | Delete
[438] Fix | Delete
$this->done[] = $id;
[439] Fix | Delete
[440] Fix | Delete
$src = $this->get_src( $id );
[441] Fix | Delete
if ( '' === $src ) {
[442] Fix | Delete
return;
[443] Fix | Delete
}
[444] Fix | Delete
[445] Fix | Delete
$attributes = array(
[446] Fix | Delete
'type' => 'module',
[447] Fix | Delete
'src' => $src,
[448] Fix | Delete
'id' => $id . '-js-module',
[449] Fix | Delete
);
[450] Fix | Delete
[451] Fix | Delete
$script_module = $this->registered[ $id ];
[452] Fix | Delete
$dependents = $this->get_recursive_dependents( $id );
[453] Fix | Delete
$fetchpriority = $this->get_highest_fetchpriority( array_merge( array( $id ), $dependents ) );
[454] Fix | Delete
if ( 'auto' !== $fetchpriority ) {
[455] Fix | Delete
$attributes['fetchpriority'] = $fetchpriority;
[456] Fix | Delete
}
[457] Fix | Delete
if ( $fetchpriority !== $script_module['fetchpriority'] ) {
[458] Fix | Delete
$attributes['data-wp-fetchpriority'] = $script_module['fetchpriority'];
[459] Fix | Delete
}
[460] Fix | Delete
wp_print_script_tag( $attributes );
[461] Fix | Delete
}
[462] Fix | Delete
[463] Fix | Delete
/**
[464] Fix | Delete
* Prints the static dependencies of the enqueued script modules using
[465] Fix | Delete
* link tags with rel="modulepreload" attributes.
[466] Fix | Delete
*
[467] Fix | Delete
* If a script module is marked for enqueue, it will not be preloaded.
[468] Fix | Delete
*
[469] Fix | Delete
* @since 6.5.0
[470] Fix | Delete
*/
[471] Fix | Delete
public function print_script_module_preloads() {
[472] Fix | Delete
$dependency_ids = $this->get_sorted_dependencies( $this->queue, array( 'static' ) );
[473] Fix | Delete
foreach ( $dependency_ids as $id ) {
[474] Fix | Delete
// Don't preload if it's marked for enqueue.
[475] Fix | Delete
if ( in_array( $id, $this->queue, true ) ) {
[476] Fix | Delete
continue;
[477] Fix | Delete
}
[478] Fix | Delete
[479] Fix | Delete
$src = $this->get_src( $id );
[480] Fix | Delete
if ( '' === $src ) {
[481] Fix | Delete
continue;
[482] Fix | Delete
}
[483] Fix | Delete
[484] Fix | Delete
$enqueued_dependents = array_intersect( $this->get_recursive_dependents( $id ), $this->queue );
[485] Fix | Delete
$highest_fetchpriority = $this->get_highest_fetchpriority( $enqueued_dependents );
[486] Fix | Delete
printf(
[487] Fix | Delete
'<link rel="modulepreload" href="%s" id="%s"',
[488] Fix | Delete
esc_url( $src ),
[489] Fix | Delete
esc_attr( $id . '-js-modulepreload' )
[490] Fix | Delete
);
[491] Fix | Delete
if ( 'auto' !== $highest_fetchpriority ) {
[492] Fix | Delete
printf( ' fetchpriority="%s"', esc_attr( $highest_fetchpriority ) );
[493] Fix | Delete
}
[494] Fix | Delete
if ( $highest_fetchpriority !== $this->registered[ $id ]['fetchpriority'] && 'auto' !== $this->registered[ $id ]['fetchpriority'] ) {
[495] Fix | Delete
printf( ' data-wp-fetchpriority="%s"', esc_attr( $this->registered[ $id ]['fetchpriority'] ) );
[496] Fix | Delete
}
[497] Fix | Delete
echo ">\n";
[498] Fix | Delete
}
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function