Edit File by line
/home/zeestwma/redstone.../wp-inclu...
File: speculative-loading.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Speculative loading functions.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Speculative Loading
[5] Fix | Delete
* @since 6.8.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Returns the speculation rules configuration.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 6.8.0
[12] Fix | Delete
*
[13] Fix | Delete
* @return array<string, string>|null Associative array with 'mode' and 'eagerness' keys, or null if speculative
[14] Fix | Delete
* loading is disabled.
[15] Fix | Delete
*/
[16] Fix | Delete
function wp_get_speculation_rules_configuration(): ?array {
[17] Fix | Delete
// By default, speculative loading is only enabled for sites with pretty permalinks when no user is logged in.
[18] Fix | Delete
if ( ! is_user_logged_in() && get_option( 'permalink_structure' ) ) {
[19] Fix | Delete
$config = array(
[20] Fix | Delete
'mode' => 'auto',
[21] Fix | Delete
'eagerness' => 'auto',
[22] Fix | Delete
);
[23] Fix | Delete
} else {
[24] Fix | Delete
$config = null;
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Filters the way that speculation rules are configured.
[29] Fix | Delete
*
[30] Fix | Delete
* The Speculation Rules API is a web API that allows to automatically prefetch or prerender certain URLs on the
[31] Fix | Delete
* page, which can lead to near-instant page load times. This is also referred to as speculative loading.
[32] Fix | Delete
*
[33] Fix | Delete
* There are two aspects to the configuration:
[34] Fix | Delete
* * The "mode" (whether to "prefetch" or "prerender" URLs).
[35] Fix | Delete
* * The "eagerness" (whether to speculatively load URLs in an "eager", "moderate", or "conservative" way).
[36] Fix | Delete
*
[37] Fix | Delete
* By default, the speculation rules configuration is decided by WordPress Core ("auto"). This filter can be used
[38] Fix | Delete
* to force a certain configuration, which could for instance load URLs more or less eagerly.
[39] Fix | Delete
*
[40] Fix | Delete
* For logged-in users or for sites that are not configured to use pretty permalinks, the default value is `null`,
[41] Fix | Delete
* indicating that speculative loading is entirely disabled.
[42] Fix | Delete
*
[43] Fix | Delete
* @since 6.8.0
[44] Fix | Delete
* @see https://developer.chrome.com/docs/web-platform/prerender-pages
[45] Fix | Delete
*
[46] Fix | Delete
* @param array<string, string>|null $config Associative array with 'mode' and 'eagerness' keys, or `null`. The
[47] Fix | Delete
* default value for both of the keys is 'auto'. Other possible values
[48] Fix | Delete
* for 'mode' are 'prefetch' and 'prerender'. Other possible values for
[49] Fix | Delete
* 'eagerness' are 'eager', 'moderate', and 'conservative'. The value
[50] Fix | Delete
* `null` is used to disable speculative loading entirely.
[51] Fix | Delete
*/
[52] Fix | Delete
$config = apply_filters( 'wp_speculation_rules_configuration', $config );
[53] Fix | Delete
[54] Fix | Delete
// Allow the value `null` to indicate that speculative loading is disabled.
[55] Fix | Delete
if ( null === $config ) {
[56] Fix | Delete
return null;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
// Sanitize the configuration and replace 'auto' with current defaults.
[60] Fix | Delete
$default_mode = 'prefetch';
[61] Fix | Delete
$default_eagerness = 'conservative';
[62] Fix | Delete
if ( ! is_array( $config ) ) {
[63] Fix | Delete
return array(
[64] Fix | Delete
'mode' => $default_mode,
[65] Fix | Delete
'eagerness' => $default_eagerness,
[66] Fix | Delete
);
[67] Fix | Delete
}
[68] Fix | Delete
if (
[69] Fix | Delete
! isset( $config['mode'] ) ||
[70] Fix | Delete
'auto' === $config['mode'] ||
[71] Fix | Delete
! WP_Speculation_Rules::is_valid_mode( $config['mode'] )
[72] Fix | Delete
) {
[73] Fix | Delete
$config['mode'] = $default_mode;
[74] Fix | Delete
}
[75] Fix | Delete
if (
[76] Fix | Delete
! isset( $config['eagerness'] ) ||
[77] Fix | Delete
'auto' === $config['eagerness'] ||
[78] Fix | Delete
! WP_Speculation_Rules::is_valid_eagerness( $config['eagerness'] ) ||
[79] Fix | Delete
// 'immediate' is a valid eagerness, but for safety WordPress does not allow it for document-level rules.
[80] Fix | Delete
'immediate' === $config['eagerness']
[81] Fix | Delete
) {
[82] Fix | Delete
$config['eagerness'] = $default_eagerness;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
return array(
[86] Fix | Delete
'mode' => $config['mode'],
[87] Fix | Delete
'eagerness' => $config['eagerness'],
[88] Fix | Delete
);
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Returns the full speculation rules data based on the configuration.
[93] Fix | Delete
*
[94] Fix | Delete
* Plugins with features that rely on frontend URLs to exclude from prefetching or prerendering should use the
[95] Fix | Delete
* {@see 'wp_speculation_rules_href_exclude_paths'} filter to ensure those URL patterns are excluded.
[96] Fix | Delete
*
[97] Fix | Delete
* Additional speculation rules other than the default rule from WordPress Core can be provided by using the
[98] Fix | Delete
* {@see 'wp_load_speculation_rules'} action and amending the passed WP_Speculation_Rules object.
[99] Fix | Delete
*
[100] Fix | Delete
* @since 6.8.0
[101] Fix | Delete
* @access private
[102] Fix | Delete
*
[103] Fix | Delete
* @return WP_Speculation_Rules|null Object representing the speculation rules to use, or null if speculative loading
[104] Fix | Delete
* is disabled in the current context.
[105] Fix | Delete
*/
[106] Fix | Delete
function wp_get_speculation_rules(): ?WP_Speculation_Rules {
[107] Fix | Delete
$configuration = wp_get_speculation_rules_configuration();
[108] Fix | Delete
if ( null === $configuration ) {
[109] Fix | Delete
return null;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
$mode = $configuration['mode'];
[113] Fix | Delete
$eagerness = $configuration['eagerness'];
[114] Fix | Delete
[115] Fix | Delete
$prefixer = new WP_URL_Pattern_Prefixer();
[116] Fix | Delete
[117] Fix | Delete
$base_href_exclude_paths = array(
[118] Fix | Delete
$prefixer->prefix_path_pattern( '/wp-*.php', 'site' ),
[119] Fix | Delete
$prefixer->prefix_path_pattern( '/wp-admin/*', 'site' ),
[120] Fix | Delete
$prefixer->prefix_path_pattern( '/*', 'uploads' ),
[121] Fix | Delete
$prefixer->prefix_path_pattern( '/*', 'content' ),
[122] Fix | Delete
$prefixer->prefix_path_pattern( '/*', 'plugins' ),
[123] Fix | Delete
$prefixer->prefix_path_pattern( '/*', 'template' ),
[124] Fix | Delete
$prefixer->prefix_path_pattern( '/*', 'stylesheet' ),
[125] Fix | Delete
);
[126] Fix | Delete
[127] Fix | Delete
/*
[128] Fix | Delete
* If pretty permalinks are enabled, exclude any URLs with query parameters.
[129] Fix | Delete
* Otherwise, exclude specifically the URLs with a `_wpnonce` query parameter or any other query parameter
[130] Fix | Delete
* containing the word `nonce`.
[131] Fix | Delete
*/
[132] Fix | Delete
if ( get_option( 'permalink_structure' ) ) {
[133] Fix | Delete
$base_href_exclude_paths[] = $prefixer->prefix_path_pattern( '/*\\?(.+)', 'home' );
[134] Fix | Delete
} else {
[135] Fix | Delete
$base_href_exclude_paths[] = $prefixer->prefix_path_pattern( '/*\\?*(^|&)*nonce*=*', 'home' );
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Filters the paths for which speculative loading should be disabled.
[140] Fix | Delete
*
[141] Fix | Delete
* All paths should start in a forward slash, relative to the root document. The `*` can be used as a wildcard.
[142] Fix | Delete
* If the WordPress site is in a subdirectory, the exclude paths will automatically be prefixed as necessary.
[143] Fix | Delete
*
[144] Fix | Delete
* Note that WordPress always excludes certain path patterns such as `/wp-login.php` and `/wp-admin/*`, and those
[145] Fix | Delete
* cannot be modified using the filter.
[146] Fix | Delete
*
[147] Fix | Delete
* @since 6.8.0
[148] Fix | Delete
*
[149] Fix | Delete
* @param string[] $href_exclude_paths Additional path patterns to disable speculative loading for.
[150] Fix | Delete
* @param string $mode Mode used to apply speculative loading. Either 'prefetch' or 'prerender'.
[151] Fix | Delete
*/
[152] Fix | Delete
$href_exclude_paths = (array) apply_filters( 'wp_speculation_rules_href_exclude_paths', array(), $mode );
[153] Fix | Delete
[154] Fix | Delete
// Ensure that:
[155] Fix | Delete
// 1. There are no duplicates.
[156] Fix | Delete
// 2. The base paths cannot be removed.
[157] Fix | Delete
// 3. The array has sequential keys (i.e. array_is_list()).
[158] Fix | Delete
$href_exclude_paths = array_values(
[159] Fix | Delete
array_unique(
[160] Fix | Delete
array_merge(
[161] Fix | Delete
$base_href_exclude_paths,
[162] Fix | Delete
array_map(
[163] Fix | Delete
static function ( string $href_exclude_path ) use ( $prefixer ): string {
[164] Fix | Delete
return $prefixer->prefix_path_pattern( $href_exclude_path );
[165] Fix | Delete
},
[166] Fix | Delete
$href_exclude_paths
[167] Fix | Delete
)
[168] Fix | Delete
)
[169] Fix | Delete
)
[170] Fix | Delete
);
[171] Fix | Delete
[172] Fix | Delete
$speculation_rules = new WP_Speculation_Rules();
[173] Fix | Delete
[174] Fix | Delete
$main_rule_conditions = array(
[175] Fix | Delete
// Include any URLs within the same site.
[176] Fix | Delete
array(
[177] Fix | Delete
'href_matches' => $prefixer->prefix_path_pattern( '/*' ),
[178] Fix | Delete
),
[179] Fix | Delete
// Except for excluded paths.
[180] Fix | Delete
array(
[181] Fix | Delete
'not' => array(
[182] Fix | Delete
'href_matches' => $href_exclude_paths,
[183] Fix | Delete
),
[184] Fix | Delete
),
[185] Fix | Delete
// Also exclude rel=nofollow links, as certain plugins use that on their links that perform an action.
[186] Fix | Delete
array(
[187] Fix | Delete
'not' => array(
[188] Fix | Delete
'selector_matches' => 'a[rel~="nofollow"]',
[189] Fix | Delete
),
[190] Fix | Delete
),
[191] Fix | Delete
// Also exclude links that are explicitly marked to opt out, either directly or via a parent element.
[192] Fix | Delete
array(
[193] Fix | Delete
'not' => array(
[194] Fix | Delete
'selector_matches' => ".no-{$mode}, .no-{$mode} a",
[195] Fix | Delete
),
[196] Fix | Delete
),
[197] Fix | Delete
);
[198] Fix | Delete
[199] Fix | Delete
// If using 'prerender', also exclude links that opt out of 'prefetch' because it's part of 'prerender'.
[200] Fix | Delete
if ( 'prerender' === $mode ) {
[201] Fix | Delete
$main_rule_conditions[] = array(
[202] Fix | Delete
'not' => array(
[203] Fix | Delete
'selector_matches' => '.no-prefetch, .no-prefetch a',
[204] Fix | Delete
),
[205] Fix | Delete
);
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
$speculation_rules->add_rule(
[209] Fix | Delete
$mode,
[210] Fix | Delete
'main',
[211] Fix | Delete
array(
[212] Fix | Delete
'source' => 'document',
[213] Fix | Delete
'where' => array(
[214] Fix | Delete
'and' => $main_rule_conditions,
[215] Fix | Delete
),
[216] Fix | Delete
'eagerness' => $eagerness,
[217] Fix | Delete
)
[218] Fix | Delete
);
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* Fires when speculation rules data is loaded, allowing to amend the rules.
[222] Fix | Delete
*
[223] Fix | Delete
* @since 6.8.0
[224] Fix | Delete
*
[225] Fix | Delete
* @param WP_Speculation_Rules $speculation_rules Object representing the speculation rules to use.
[226] Fix | Delete
*/
[227] Fix | Delete
do_action( 'wp_load_speculation_rules', $speculation_rules );
[228] Fix | Delete
[229] Fix | Delete
return $speculation_rules;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/**
[233] Fix | Delete
* Prints the speculation rules.
[234] Fix | Delete
*
[235] Fix | Delete
* For browsers that do not support speculation rules yet, the `script[type="speculationrules"]` tag will be ignored.
[236] Fix | Delete
*
[237] Fix | Delete
* @since 6.8.0
[238] Fix | Delete
* @access private
[239] Fix | Delete
*/
[240] Fix | Delete
function wp_print_speculation_rules(): void {
[241] Fix | Delete
$speculation_rules = wp_get_speculation_rules();
[242] Fix | Delete
if ( null === $speculation_rules ) {
[243] Fix | Delete
return;
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
wp_print_inline_script_tag(
[247] Fix | Delete
(string) wp_json_encode(
[248] Fix | Delete
$speculation_rules,
[249] Fix | Delete
JSON_HEX_TAG | JSON_UNESCAPED_SLASHES
[250] Fix | Delete
),
[251] Fix | Delete
array( 'type' => 'speculationrules' )
[252] Fix | Delete
);
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function