-
Notifications
You must be signed in to change notification settings - Fork 87
/
hooks.php
59 lines (53 loc) · 1.61 KB
/
hooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Hook callbacks used for Speculative Loading.
*
* @package speculation-rules
* @since 1.0.0
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Prints the speculation rules.
*
* For browsers that do not support speculation rules yet, the `script[type="speculationrules"]` tag will be ignored.
*
* @since 1.0.0
*/
function plsr_print_speculation_rules() {
$rules = plsr_get_speculation_rules();
if ( empty( $rules ) ) {
return;
}
// This workaround is needed for WP 6.4. See <https://core.trac.wordpress.org/ticket/60320>.
$needs_html5_workaround = (
! current_theme_supports( 'html5', 'script' ) &&
version_compare( strtok( get_bloginfo( 'version' ), '-' ), '6.4', '>=' ) &&
version_compare( strtok( get_bloginfo( 'version' ), '-' ), '6.5', '<' )
);
if ( $needs_html5_workaround ) {
$backup_wp_theme_features = $GLOBALS['_wp_theme_features'];
add_theme_support( 'html5', array( 'script' ) );
}
wp_print_inline_script_tag(
wp_json_encode( $rules ),
array( 'type' => 'speculationrules' )
);
if ( $needs_html5_workaround ) {
$GLOBALS['_wp_theme_features'] = $backup_wp_theme_features; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
add_action( 'wp_footer', 'plsr_print_speculation_rules' );
/**
* Displays the HTML generator meta tag for the Speculative Loading plugin.
*
* See {@see 'wp_head'}.
*
* @since 1.1.0
*/
function plsr_render_generator_meta_tag() {
echo '<meta name="generator" content="Speculative Loading ' . esc_attr( SPECULATION_RULES_VERSION ) . '">' . "\n";
}
add_action( 'wp_head', 'plsr_render_generator_meta_tag' );