Edit File by line
/home/zeestwma/ajeebong.../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[]
[20] Fix | Delete
*/
[21] Fix | Delete
private $registered = array();
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Holds the script module identifiers that were enqueued before registered.
[25] Fix | Delete
*
[26] Fix | Delete
* @since 6.5.0
[27] Fix | Delete
* @var array<string, true>
[28] Fix | Delete
*/
[29] Fix | Delete
private $enqueued_before_registered = array();
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Tracks whether the @wordpress/a11y script module is available.
[33] Fix | Delete
*
[34] Fix | Delete
* Some additional HTML is required on the page for the module to work. Track
[35] Fix | Delete
* whether it's available to print at the appropriate time.
[36] Fix | Delete
*
[37] Fix | Delete
* @since 6.7.0
[38] Fix | Delete
* @var bool
[39] Fix | Delete
*/
[40] Fix | Delete
private $a11y_available = false;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Registers the script module if no script module with that script module
[44] Fix | Delete
* identifier has already been registered.
[45] Fix | Delete
*
[46] Fix | Delete
* @since 6.5.0
[47] Fix | Delete
*
[48] Fix | Delete
* @param string $id The identifier of the script module. Should be unique. It will be used in the
[49] Fix | Delete
* final import map.
[50] Fix | Delete
* @param string $src Optional. Full URL of the script module, or path of the script module relative
[51] Fix | Delete
* to the WordPress root directory. If it is provided and the script module has
[52] Fix | Delete
* not been registered yet, it will be registered.
[53] Fix | Delete
* @param array $deps {
[54] Fix | Delete
* Optional. List of dependencies.
[55] Fix | Delete
*
[56] Fix | Delete
* @type string|array ...$0 {
[57] Fix | Delete
* An array of script module identifiers of the dependencies of this script
[58] Fix | Delete
* module. The dependencies can be strings or arrays. If they are arrays,
[59] Fix | Delete
* they need an `id` key with the script module identifier, and can contain
[60] Fix | Delete
* an `import` key with either `static` or `dynamic`. By default,
[61] Fix | Delete
* dependencies that don't contain an `import` key are considered static.
[62] Fix | Delete
*
[63] Fix | Delete
* @type string $id The script module identifier.
[64] Fix | Delete
* @type string $import Optional. Import type. May be either `static` or
[65] Fix | Delete
* `dynamic`. Defaults to `static`.
[66] Fix | Delete
* }
[67] Fix | Delete
* }
[68] Fix | Delete
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
[69] Fix | Delete
* It is added to the URL as a query string for cache busting purposes. If $version
[70] Fix | Delete
* is set to false, the version number is the currently installed WordPress version.
[71] Fix | Delete
* If $version is set to null, no version is added.
[72] Fix | Delete
*/
[73] Fix | Delete
public function register( string $id, string $src, array $deps = array(), $version = false ) {
[74] Fix | Delete
if ( ! isset( $this->registered[ $id ] ) ) {
[75] Fix | Delete
$dependencies = array();
[76] Fix | Delete
foreach ( $deps as $dependency ) {
[77] Fix | Delete
if ( is_array( $dependency ) ) {
[78] Fix | Delete
if ( ! isset( $dependency['id'] ) ) {
[79] Fix | Delete
_doing_it_wrong( __METHOD__, __( 'Missing required id key in entry among dependencies array.' ), '6.5.0' );
[80] Fix | Delete
continue;
[81] Fix | Delete
}
[82] Fix | Delete
$dependencies[] = array(
[83] Fix | Delete
'id' => $dependency['id'],
[84] Fix | Delete
'import' => isset( $dependency['import'] ) && 'dynamic' === $dependency['import'] ? 'dynamic' : 'static',
[85] Fix | Delete
);
[86] Fix | Delete
} elseif ( is_string( $dependency ) ) {
[87] Fix | Delete
$dependencies[] = array(
[88] Fix | Delete
'id' => $dependency,
[89] Fix | Delete
'import' => 'static',
[90] Fix | Delete
);
[91] Fix | Delete
} else {
[92] Fix | Delete
_doing_it_wrong( __METHOD__, __( 'Entries in dependencies array must be either strings or arrays with an id key.' ), '6.5.0' );
[93] Fix | Delete
}
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
$this->registered[ $id ] = array(
[97] Fix | Delete
'src' => $src,
[98] Fix | Delete
'version' => $version,
[99] Fix | Delete
'enqueue' => isset( $this->enqueued_before_registered[ $id ] ),
[100] Fix | Delete
'dependencies' => $dependencies,
[101] Fix | Delete
);
[102] Fix | Delete
}
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Marks the script module to be enqueued in the page.
[107] Fix | Delete
*
[108] Fix | Delete
* If a src is provided and the script module has not been registered yet, it
[109] Fix | Delete
* will be registered.
[110] Fix | Delete
*
[111] Fix | Delete
* @since 6.5.0
[112] Fix | Delete
*
[113] Fix | Delete
* @param string $id The identifier of the script module. Should be unique. It will be used in the
[114] Fix | Delete
* final import map.
[115] Fix | Delete
* @param string $src Optional. Full URL of the script module, or path of the script module relative
[116] Fix | Delete
* to the WordPress root directory. If it is provided and the script module has
[117] Fix | Delete
* not been registered yet, it will be registered.
[118] Fix | Delete
* @param array $deps {
[119] Fix | Delete
* Optional. List of dependencies.
[120] Fix | Delete
*
[121] Fix | Delete
* @type string|array ...$0 {
[122] Fix | Delete
* An array of script module identifiers of the dependencies of this script
[123] Fix | Delete
* module. The dependencies can be strings or arrays. If they are arrays,
[124] Fix | Delete
* they need an `id` key with the script module identifier, and can contain
[125] Fix | Delete
* an `import` key with either `static` or `dynamic`. By default,
[126] Fix | Delete
* dependencies that don't contain an `import` key are considered static.
[127] Fix | Delete
*
[128] Fix | Delete
* @type string $id The script module identifier.
[129] Fix | Delete
* @type string $import Optional. Import type. May be either `static` or
[130] Fix | Delete
* `dynamic`. Defaults to `static`.
[131] Fix | Delete
* }
[132] Fix | Delete
* }
[133] Fix | Delete
* @param string|false|null $version Optional. String specifying the script module version number. Defaults to false.
[134] Fix | Delete
* It is added to the URL as a query string for cache busting purposes. If $version
[135] Fix | Delete
* is set to false, the version number is the currently installed WordPress version.
[136] Fix | Delete
* If $version is set to null, no version is added.
[137] Fix | Delete
*/
[138] Fix | Delete
public function enqueue( string $id, string $src = '', array $deps = array(), $version = false ) {
[139] Fix | Delete
if ( isset( $this->registered[ $id ] ) ) {
[140] Fix | Delete
$this->registered[ $id ]['enqueue'] = true;
[141] Fix | Delete
} elseif ( $src ) {
[142] Fix | Delete
$this->register( $id, $src, $deps, $version );
[143] Fix | Delete
$this->registered[ $id ]['enqueue'] = true;
[144] Fix | Delete
} else {
[145] Fix | Delete
$this->enqueued_before_registered[ $id ] = true;
[146] Fix | Delete
}
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
/**
[150] Fix | Delete
* Unmarks the script module so it will no longer be enqueued in the page.
[151] Fix | Delete
*
[152] Fix | Delete
* @since 6.5.0
[153] Fix | Delete
*
[154] Fix | Delete
* @param string $id The identifier of the script module.
[155] Fix | Delete
*/
[156] Fix | Delete
public function dequeue( string $id ) {
[157] Fix | Delete
if ( isset( $this->registered[ $id ] ) ) {
[158] Fix | Delete
$this->registered[ $id ]['enqueue'] = false;
[159] Fix | Delete
}
[160] Fix | Delete
unset( $this->enqueued_before_registered[ $id ] );
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
/**
[164] Fix | Delete
* Removes a registered script module.
[165] Fix | Delete
*
[166] Fix | Delete
* @since 6.5.0
[167] Fix | Delete
*
[168] Fix | Delete
* @param string $id The identifier of the script module.
[169] Fix | Delete
*/
[170] Fix | Delete
public function deregister( string $id ) {
[171] Fix | Delete
unset( $this->registered[ $id ] );
[172] Fix | Delete
unset( $this->enqueued_before_registered[ $id ] );
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Adds the hooks to print the import map, enqueued script modules and script
[177] Fix | Delete
* module preloads.
[178] Fix | Delete
*
[179] Fix | Delete
* In classic themes, the script modules used by the blocks are not yet known
[180] Fix | Delete
* when the `wp_head` actions is fired, so it needs to print everything in the
[181] Fix | Delete
* footer.
[182] Fix | Delete
*
[183] Fix | Delete
* @since 6.5.0
[184] Fix | Delete
*/
[185] Fix | Delete
public function add_hooks() {
[186] Fix | Delete
$position = wp_is_block_theme() ? 'wp_head' : 'wp_footer';
[187] Fix | Delete
add_action( $position, array( $this, 'print_import_map' ) );
[188] Fix | Delete
add_action( $position, array( $this, 'print_enqueued_script_modules' ) );
[189] Fix | Delete
add_action( $position, array( $this, 'print_script_module_preloads' ) );
[190] Fix | Delete
[191] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_import_map' ) );
[192] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_enqueued_script_modules' ) );
[193] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_preloads' ) );
[194] Fix | Delete
[195] Fix | Delete
add_action( 'wp_footer', array( $this, 'print_script_module_data' ) );
[196] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_data' ) );
[197] Fix | Delete
add_action( 'wp_footer', array( $this, 'print_a11y_script_module_html' ), 20 );
[198] Fix | Delete
add_action( 'admin_print_footer_scripts', array( $this, 'print_a11y_script_module_html' ), 20 );
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
/**
[202] Fix | Delete
* Prints the enqueued script modules using script tags with type="module"
[203] Fix | Delete
* attributes.
[204] Fix | Delete
*
[205] Fix | Delete
* @since 6.5.0
[206] Fix | Delete
*/
[207] Fix | Delete
public function print_enqueued_script_modules() {
[208] Fix | Delete
foreach ( $this->get_marked_for_enqueue() as $id => $script_module ) {
[209] Fix | Delete
wp_print_script_tag(
[210] Fix | Delete
array(
[211] Fix | Delete
'type' => 'module',
[212] Fix | Delete
'src' => $this->get_src( $id ),
[213] Fix | Delete
'id' => $id . '-js-module',
[214] Fix | Delete
)
[215] Fix | Delete
);
[216] Fix | Delete
}
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/**
[220] Fix | Delete
* Prints the the static dependencies of the enqueued script modules using
[221] Fix | Delete
* link tags with rel="modulepreload" attributes.
[222] Fix | Delete
*
[223] Fix | Delete
* If a script module is marked for enqueue, it will not be preloaded.
[224] Fix | Delete
*
[225] Fix | Delete
* @since 6.5.0
[226] Fix | Delete
*/
[227] Fix | Delete
public function print_script_module_preloads() {
[228] Fix | Delete
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ), array( 'static' ) ) as $id => $script_module ) {
[229] Fix | Delete
// Don't preload if it's marked for enqueue.
[230] Fix | Delete
if ( true !== $script_module['enqueue'] ) {
[231] Fix | Delete
echo sprintf(
[232] Fix | Delete
'<link rel="modulepreload" href="%s" id="%s">',
[233] Fix | Delete
esc_url( $this->get_src( $id ) ),
[234] Fix | Delete
esc_attr( $id . '-js-modulepreload' )
[235] Fix | Delete
);
[236] Fix | Delete
}
[237] Fix | Delete
}
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
/**
[241] Fix | Delete
* Prints the import map using a script tag with a type="importmap" attribute.
[242] Fix | Delete
*
[243] Fix | Delete
* @since 6.5.0
[244] Fix | Delete
*/
[245] Fix | Delete
public function print_import_map() {
[246] Fix | Delete
$import_map = $this->get_import_map();
[247] Fix | Delete
if ( ! empty( $import_map['imports'] ) ) {
[248] Fix | Delete
wp_print_inline_script_tag(
[249] Fix | Delete
wp_json_encode( $import_map, JSON_HEX_TAG | JSON_HEX_AMP ),
[250] Fix | Delete
array(
[251] Fix | Delete
'type' => 'importmap',
[252] Fix | Delete
'id' => 'wp-importmap',
[253] Fix | Delete
)
[254] Fix | Delete
);
[255] Fix | Delete
}
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
/**
[259] Fix | Delete
* Returns the import map array.
[260] Fix | Delete
*
[261] Fix | Delete
* @since 6.5.0
[262] Fix | Delete
*
[263] Fix | Delete
* @return array Array with an `imports` key mapping to an array of script module identifiers and their respective
[264] Fix | Delete
* URLs, including the version query.
[265] Fix | Delete
*/
[266] Fix | Delete
private function get_import_map(): array {
[267] Fix | Delete
$imports = array();
[268] Fix | Delete
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) {
[269] Fix | Delete
$imports[ $id ] = $this->get_src( $id );
[270] Fix | Delete
}
[271] Fix | Delete
return array( 'imports' => $imports );
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
/**
[275] Fix | Delete
* Retrieves the list of script modules marked for enqueue.
[276] Fix | Delete
*
[277] Fix | Delete
* @since 6.5.0
[278] Fix | Delete
*
[279] Fix | Delete
* @return array[] Script modules marked for enqueue, keyed by script module identifier.
[280] Fix | Delete
*/
[281] Fix | Delete
private function get_marked_for_enqueue(): array {
[282] Fix | Delete
$enqueued = array();
[283] Fix | Delete
foreach ( $this->registered as $id => $script_module ) {
[284] Fix | Delete
if ( true === $script_module['enqueue'] ) {
[285] Fix | Delete
$enqueued[ $id ] = $script_module;
[286] Fix | Delete
}
[287] Fix | Delete
}
[288] Fix | Delete
return $enqueued;
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
/**
[292] Fix | Delete
* Retrieves all the dependencies for the given script module identifiers,
[293] Fix | Delete
* filtered by import types.
[294] Fix | Delete
*
[295] Fix | Delete
* It will consolidate an array containing a set of unique dependencies based
[296] Fix | Delete
* on the requested import types: 'static', 'dynamic', or both. This method is
[297] Fix | Delete
* recursive and also retrieves dependencies of the dependencies.
[298] Fix | Delete
*
[299] Fix | Delete
* @since 6.5.0
[300] Fix | Delete
*
[301] Fix | Delete
* @param string[] $ids The identifiers of the script modules for which to gather dependencies.
[302] Fix | Delete
* @param string[] $import_types Optional. Import types of dependencies to retrieve: 'static', 'dynamic', or both.
[303] Fix | Delete
* Default is both.
[304] Fix | Delete
* @return array[] List of dependencies, keyed by script module identifier.
[305] Fix | Delete
*/
[306] Fix | Delete
private function get_dependencies( array $ids, array $import_types = array( 'static', 'dynamic' ) ) {
[307] Fix | Delete
return array_reduce(
[308] Fix | Delete
$ids,
[309] Fix | Delete
function ( $dependency_script_modules, $id ) use ( $import_types ) {
[310] Fix | Delete
$dependencies = array();
[311] Fix | Delete
foreach ( $this->registered[ $id ]['dependencies'] as $dependency ) {
[312] Fix | Delete
if (
[313] Fix | Delete
in_array( $dependency['import'], $import_types, true ) &&
[314] Fix | Delete
isset( $this->registered[ $dependency['id'] ] ) &&
[315] Fix | Delete
! isset( $dependency_script_modules[ $dependency['id'] ] )
[316] Fix | Delete
) {
[317] Fix | Delete
$dependencies[ $dependency['id'] ] = $this->registered[ $dependency['id'] ];
[318] Fix | Delete
}
[319] Fix | Delete
}
[320] Fix | Delete
return array_merge( $dependency_script_modules, $dependencies, $this->get_dependencies( array_keys( $dependencies ), $import_types ) );
[321] Fix | Delete
},
[322] Fix | Delete
array()
[323] Fix | Delete
);
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Gets the versioned URL for a script module src.
[328] Fix | Delete
*
[329] Fix | Delete
* If $version is set to false, the version number is the currently installed
[330] Fix | Delete
* WordPress version. If $version is set to null, no version is added.
[331] Fix | Delete
* Otherwise, the string passed in $version is used.
[332] Fix | Delete
*
[333] Fix | Delete
* @since 6.5.0
[334] Fix | Delete
*
[335] Fix | Delete
* @param string $id The script module identifier.
[336] Fix | Delete
* @return string The script module src with a version if relevant.
[337] Fix | Delete
*/
[338] Fix | Delete
private function get_src( string $id ): string {
[339] Fix | Delete
if ( ! isset( $this->registered[ $id ] ) ) {
[340] Fix | Delete
return '';
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
$script_module = $this->registered[ $id ];
[344] Fix | Delete
$src = $script_module['src'];
[345] Fix | Delete
[346] Fix | Delete
if ( false === $script_module['version'] ) {
[347] Fix | Delete
$src = add_query_arg( 'ver', get_bloginfo( 'version' ), $src );
[348] Fix | Delete
} elseif ( null !== $script_module['version'] ) {
[349] Fix | Delete
$src = add_query_arg( 'ver', $script_module['version'], $src );
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* Filters the script module source.
[354] Fix | Delete
*
[355] Fix | Delete
* @since 6.5.0
[356] Fix | Delete
*
[357] Fix | Delete
* @param string $src Module source URL.
[358] Fix | Delete
* @param string $id Module identifier.
[359] Fix | Delete
*/
[360] Fix | Delete
$src = apply_filters( 'script_module_loader_src', $src, $id );
[361] Fix | Delete
[362] Fix | Delete
return $src;
[363] Fix | Delete
}
[364] Fix | Delete
[365] Fix | Delete
/**
[366] Fix | Delete
* Print data associated with Script Modules.
[367] Fix | Delete
*
[368] Fix | Delete
* The data will be embedded in the page HTML and can be read by Script Modules on page load.
[369] Fix | Delete
*
[370] Fix | Delete
* @since 6.7.0
[371] Fix | Delete
*
[372] Fix | Delete
* Data can be associated with a Script Module via the
[373] Fix | Delete
* {@see "script_module_data_{$module_id}"} filter.
[374] Fix | Delete
*
[375] Fix | Delete
* The data for a Script Module will be serialized as JSON in a script tag with an ID of the
[376] Fix | Delete
* form `wp-script-module-data-{$module_id}`.
[377] Fix | Delete
*/
[378] Fix | Delete
public function print_script_module_data(): void {
[379] Fix | Delete
$modules = array();
[380] Fix | Delete
foreach ( array_keys( $this->get_marked_for_enqueue() ) as $id ) {
[381] Fix | Delete
if ( '@wordpress/a11y' === $id ) {
[382] Fix | Delete
$this->a11y_available = true;
[383] Fix | Delete
}
[384] Fix | Delete
$modules[ $id ] = true;
[385] Fix | Delete
}
[386] Fix | Delete
foreach ( array_keys( $this->get_import_map()['imports'] ) as $id ) {
[387] Fix | Delete
if ( '@wordpress/a11y' === $id ) {
[388] Fix | Delete
$this->a11y_available = true;
[389] Fix | Delete
}
[390] Fix | Delete
$modules[ $id ] = true;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
foreach ( array_keys( $modules ) as $module_id ) {
[394] Fix | Delete
/**
[395] Fix | Delete
* Filters data associated with a given Script Module.
[396] Fix | Delete
*
[397] Fix | Delete
* Script Modules may require data that is required for initialization or is essential
[398] Fix | Delete
* to have immediately available on page load. These are suitable use cases for
[399] Fix | Delete
* this data.
[400] Fix | Delete
*
[401] Fix | Delete
* The dynamic portion of the hook name, `$module_id`, refers to the Script Module ID
[402] Fix | Delete
* that the data is associated with.
[403] Fix | Delete
*
[404] Fix | Delete
* This is best suited to pass essential data that must be available to the module for
[405] Fix | Delete
* initialization or immediately on page load. It does not replace the REST API or
[406] Fix | Delete
* fetching data from the client.
[407] Fix | Delete
*
[408] Fix | Delete
* Example:
[409] Fix | Delete
*
[410] Fix | Delete
* add_filter(
[411] Fix | Delete
* 'script_module_data_MyScriptModuleID',
[412] Fix | Delete
* function ( array $data ): array {
[413] Fix | Delete
* $data['dataForClient'] = 'ok';
[414] Fix | Delete
* return $data;
[415] Fix | Delete
* }
[416] Fix | Delete
* );
[417] Fix | Delete
*
[418] Fix | Delete
* If the filter returns no data (an empty array), nothing will be embedded in the page.
[419] Fix | Delete
*
[420] Fix | Delete
* The data for a given Script Module, if provided, will be JSON serialized in a script
[421] Fix | Delete
* tag with an ID of the form `wp-script-module-data-{$module_id}`.
[422] Fix | Delete
*
[423] Fix | Delete
* The data can be read on the client with a pattern like this:
[424] Fix | Delete
*
[425] Fix | Delete
* Example:
[426] Fix | Delete
*
[427] Fix | Delete
* const dataContainer = document.getElementById( 'wp-script-module-data-MyScriptModuleID' );
[428] Fix | Delete
* let data = {};
[429] Fix | Delete
* if ( dataContainer ) {
[430] Fix | Delete
* try {
[431] Fix | Delete
* data = JSON.parse( dataContainer.textContent );
[432] Fix | Delete
* } catch {}
[433] Fix | Delete
* }
[434] Fix | Delete
* // data.dataForClient === 'ok';
[435] Fix | Delete
* initMyScriptModuleWithData( data );
[436] Fix | Delete
*
[437] Fix | Delete
* @since 6.7.0
[438] Fix | Delete
*
[439] Fix | Delete
* @param array $data The data associated with the Script Module.
[440] Fix | Delete
*/
[441] Fix | Delete
$data = apply_filters( "script_module_data_{$module_id}", array() );
[442] Fix | Delete
[443] Fix | Delete
if ( is_array( $data ) && array() !== $data ) {
[444] Fix | Delete
/*
[445] Fix | Delete
* This data will be printed as JSON inside a script tag like this:
[446] Fix | Delete
* <script type="application/json"></script>
[447] Fix | Delete
*
[448] Fix | Delete
* A script tag must be closed by a sequence beginning with `</`. It's impossible to
[449] Fix | Delete
* close a script tag without using `<`. We ensure that `<` is escaped and `/` can
[450] Fix | Delete
* remain unescaped, so `</script>` will be printed as `\u003C/script\u00E3`.
[451] Fix | Delete
*
[452] Fix | Delete
* - JSON_HEX_TAG: All < and > are converted to \u003C and \u003E.
[453] Fix | Delete
* - JSON_UNESCAPED_SLASHES: Don't escape /.
[454] Fix | Delete
*
[455] Fix | Delete
* If the page will use UTF-8 encoding, it's safe to print unescaped unicode:
[456] Fix | Delete
*
[457] Fix | Delete
* - JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (instead of as `\uXXXX`).
[458] Fix | Delete
* - JSON_UNESCAPED_LINE_TERMINATORS: The line terminators are kept unescaped when
[459] Fix | Delete
* JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was
[460] Fix | Delete
* before PHP 7.1 without this constant. Available as of PHP 7.1.0.
[461] Fix | Delete
*
[462] Fix | Delete
* The JSON specification requires encoding in UTF-8, so if the generated HTML page
[463] Fix | Delete
* is not encoded in UTF-8 then it's not safe to include those literals. They must
[464] Fix | Delete
* be escaped to avoid encoding issues.
[465] Fix | Delete
*
[466] Fix | Delete
* @see https://www.rfc-editor.org/rfc/rfc8259.html for details on encoding requirements.
[467] Fix | Delete
* @see https://www.php.net/manual/en/json.constants.php for details on these constants.
[468] Fix | Delete
* @see https://html.spec.whatwg.org/#script-data-state for details on script tag parsing.
[469] Fix | Delete
*/
[470] Fix | Delete
$json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS;
[471] Fix | Delete
if ( ! is_utf8_charset() ) {
[472] Fix | Delete
$json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES;
[473] Fix | Delete
}
[474] Fix | Delete
[475] Fix | Delete
wp_print_inline_script_tag(
[476] Fix | Delete
wp_json_encode(
[477] Fix | Delete
$data,
[478] Fix | Delete
$json_encode_flags
[479] Fix | Delete
),
[480] Fix | Delete
array(
[481] Fix | Delete
'type' => 'application/json',
[482] Fix | Delete
'id' => "wp-script-module-data-{$module_id}",
[483] Fix | Delete
)
[484] Fix | Delete
);
[485] Fix | Delete
}
[486] Fix | Delete
}
[487] Fix | Delete
}
[488] Fix | Delete
[489] Fix | Delete
/**
[490] Fix | Delete
* @access private This is only intended to be called by the registered actions.
[491] Fix | Delete
*
[492] Fix | Delete
* @since 6.7.0
[493] Fix | Delete
*/
[494] Fix | Delete
public function print_a11y_script_module_html() {
[495] Fix | Delete
if ( ! $this->a11y_available ) {
[496] Fix | Delete
return;
[497] Fix | Delete
}
[498] Fix | Delete
echo '<div style="position:absolute;margin:-1px;padding:0;height:1px;width:1px;overflow:hidden;clip-path:inset(50%);border:0;word-wrap:normal !important;">'
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function