PluginProbe
Speculative Loading / 1.2.2
Speculative Loading v1.2.2
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 / helper.php

helper.php in Speculative Loading 1.2.2, at helper.php

121 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper functions used for Speculative Loading.
4 *
5 * @package speculation-rules
6 * @since 1.0.0
7 */
8
9 // Exit if accessed directly.
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Returns the speculation rules.
16 *
17 * Plugins with features that rely on frontend URLs to exclude from prefetching or prerendering should use the
18 * {@see 'plsr_speculation_rules_href_exclude_paths'} filter to ensure those URL patterns are excluded.
19 *
20 * @since 1.0.0
21 *
22 * @return array Associative array of speculation rules by type.
23 */
24 function plsr_get_speculation_rules() {
25 $option = get_option( 'plsr_speculation_rules' );
26
27 /*
28 * This logic is only relevant for edge-cases where the setting may not be registered,
29 * a.k.a. defensive coding.
30 */
31 if ( ! $option || ! is_array( $option ) ) {
32 $option = plsr_get_setting_default();
33 } else {
34 $option = array_merge( plsr_get_setting_default(), $option );
35 }
36
37 $mode = $option['mode'];
38 $eagerness = $option['eagerness'];
39
40 $prefixer = new PLSR_URL_Pattern_Prefixer();
41
42 $base_href_exclude_paths = array(
43 $prefixer->prefix_path_pattern( '/wp-login.php', 'site' ),
44 $prefixer->prefix_path_pattern( '/wp-admin/*', 'site' ),
45 $prefixer->prefix_path_pattern( '/*\\?*(^|&)_wpnonce=*', 'home' ),
46 );
47
48 /**
49 * Filters the paths for which speculative prerendering should be disabled.
50 *
51 * All paths should start in a forward slash, relative to the root document. The `*` can be used as a wildcard.
52 * By default, the array includes `/wp-login.php` and `/wp-admin/*`.
53 *
54 * If the WordPress site is in a subdirectory, the exclude paths will automatically be prefixed as necessary.
55 *
56 * @since 1.0.0
57 * @since 1.1.0 The $mode parameter was added.
58 *
59 * @param string[] $href_exclude_paths Additional paths to disable speculative prerendering for. The base exclude paths,
60 * such as for wp-admin, cannot be removed.
61 * @param string $mode Mode used to apply speculative prerendering. Either 'prefetch' or 'prerender'.
62 */
63 $href_exclude_paths = (array) apply_filters( 'plsr_speculation_rules_href_exclude_paths', array(), $mode );
64
65 // Ensure that:
66 // 1. There are no duplicates.
67 // 2. The base paths cannot be removed.
68 // 3. The array has sequential keys (i.e. array_is_list()).
69 $href_exclude_paths = array_values(
70 array_unique(
71 array_merge(
72 $base_href_exclude_paths,
73 array_map(
74 static function ( string $href_exclude_path ) use ( $prefixer ): string {
75 return $prefixer->prefix_path_pattern( $href_exclude_path );
76 },
77 $href_exclude_paths
78 )
79 )
80 )
81 );
82
83 $rules = array(
84 array(
85 'source' => 'document',
86 'where' => array(
87 'and' => array(
88 // Include any URLs within the same site.
89 array(
90 'href_matches' => $prefixer->prefix_path_pattern( '/*' ),
91 ),
92 // Except for WP login and admin URLs.
93 array(
94 'not' => array(
95 'href_matches' => $href_exclude_paths,
96 ),
97 ),
98 // Also exclude rel=nofollow links, as plugins like WooCommerce use that on their add-to-cart links.
99 array(
100 'not' => array(
101 'selector_matches' => 'a[rel=nofollow]',
102 ),
103 ),
104 ),
105 ),
106 'eagerness' => $eagerness,
107 ),
108 );
109
110 // Allow adding a class on any links to prevent prerendering.
111 if ( 'prerender' === $mode ) {
112 $rules[0]['where']['and'][] = array(
113 'not' => array(
114 'selector_matches' => '.no-prerender',
115 ),
116 );
117 }
118
119 return array( $mode => $rules );
120 }
121