PluginProbe
Speculative Loading / 1.7.0
Speculative Loading v1.7.0
1.7.0 1.1.0 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 trunk 1.0.0 1.0.1
speculation-rules / hooks.php

hooks.php in Speculative Loading 1.7.0, at hooks.php

89 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Hook callbacks used for Speculative Loading.
4 *
5 * @package speculation-rules
6 * @since 1.0.0
7 */
8
9 declare( strict_types = 1 );
10
11 // @codeCoverageIgnoreStart
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15 // @codeCoverageIgnoreEnd
16
17 /**
18 * Determines whether Speculative Loading is enabled.
19 *
20 * @since 1.6.0
21 *
22 * @return bool Whether enabled.
23 */
24 function plsr_is_speculative_loading_enabled(): bool {
25 $option = plsr_get_stored_setting_value();
26
27 // Disabled if the user is logged in, unless the setting explicitly allows the current user's role.
28 if (
29 is_user_logged_in()
30 &&
31 'any' !== $option['authentication']
32 &&
33 ( ! current_user_can( 'manage_options' ) || 'logged_out_and_admins' !== $option['authentication'] )
34 ) {
35 return false;
36 }
37
38 // Disable if pretty permalinks are not enabled, unless explicitly overridden by the filter.
39 if (
40 ! (bool) get_option( 'permalink_structure' )
41 &&
42 /**
43 * Filters whether speculative loading should be enabled even though the site does not use pretty permalinks.
44 *
45 * Since query parameters are commonly used by plugins for dynamic behavior that can change state, ideally any
46 * such URLs are excluded from speculative loading. If the site does not use pretty permalinks though, they are
47 * impossible to recognize. Therefore, speculative loading is disabled by default for those sites.
48 *
49 * For site owners of sites without pretty permalinks that are certain their site is not using such a pattern,
50 * this filter can be used to still enable speculative loading at their own risk.
51 *
52 * @since 1.4.0
53 *
54 * @param bool $enabled Whether speculative loading is enabled even without pretty permalinks.
55 */
56 ! apply_filters( 'plsr_enabled_without_pretty_permalinks', false )
57 ) {
58 return false;
59 }
60
61 return true;
62 }
63
64 // Conditionally use either the WordPress Core API, or load the plugin's API implementation otherwise.
65 if ( function_exists( 'wp_get_speculation_rules_configuration' ) ) {
66 require_once __DIR__ . '/wp-core-api.php';
67
68 add_filter( 'wp_speculation_rules_configuration', 'plsr_filter_speculation_rules_configuration' );
69 add_filter( 'wp_speculation_rules_href_exclude_paths', 'plsr_filter_speculation_rules_exclude_paths', 10, 2 );
70 } else {
71 require_once __DIR__ . '/class-plsr-url-pattern-prefixer.php';
72 require_once __DIR__ . '/plugin-api.php';
73
74 add_action( 'wp_footer', 'plsr_print_speculation_rules' );
75 }
76
77 /**
78 * Displays the HTML generator meta tag for the Speculative Loading plugin.
79 *
80 * See {@see 'wp_head'}.
81 *
82 * @since 1.1.0
83 */
84 function plsr_render_generator_meta_tag(): void {
85 // Use the plugin slug as it is immutable.
86 echo '<meta name="generator" content="speculation-rules ' . esc_attr( SPECULATION_RULES_VERSION ) . '">' . "\n";
87 }
88 add_action( 'wp_head', 'plsr_render_generator_meta_tag' );
89