-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtiny-nav-menu-cache.php
194 lines (159 loc) · 5.73 KB
/
tiny-nav-menu-cache.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Plugin Name: Tiny navigation menu cache (MU)
* Description: Cache nav menu's HTML content in persistent object cache.
* Version: 0.2.1
* Constants: TINY_CACHE_NAV_MENU_EXCLUDES
*/
class Tiny_Nav_Menu_Cache {
/**
* @var string Name of the cache group.
*/
private const GROUP = 'navmenu';
/**
* @var array<string> List of whitelisted query string fields (these do not prevent cache write).
*/
private $whitelisted_query_string_fields = [
// https://support.google.com/searchads/answer/7342044
'gclid',
'gclsrc',
// https://www.facebook.com/business/help/330994334179410 "URL in ad can't contain Facebook Click ID" section
'fbclid',
// https://en.wikipedia.org/wiki/UTM_parameters
'utm_campaign',
'utm_content',
'utm_medium',
'utm_source',
'utm_term',
];
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* @return void
*/
public function init() {
// Detect object cache
if ( ! wp_using_ext_object_cache() ) {
return;
}
add_action( 'save_post', array( $this, 'flush_all' ) );
add_action( 'wp_create_nav_menu', array( $this, 'flush_all' ) );
add_action( 'wp_update_nav_menu', array( $this, 'flush_all' ) );
add_action( 'wp_delete_nav_menu', array( $this, 'flush_all' ) );
add_action( 'split_shared_term', array( $this, 'flush_all' ) );
// Learned from W3TC Page Cache rules and WP Super Cache rules
if ( is_user_logged_in() /* User is logged in */
|| ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) /* Not a GET request */ // WPCS: input var OK.
|| ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) /* DO-NOT-CACHE tag present */
) {
return;
}
// Add user-defined query parameters to the whitelist. Define the parameters you
// want whitelisted in wp-config in the following way:
//
// define('TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS', 'XDEBUG_TRIGGER|do_xhprof_profile');
if ( defined( 'TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS' ) ) {
$fields = array_map( 'trim', explode( '|', TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS ) );
$this->whitelisted_query_string_fields = array_merge( $this->whitelisted_query_string_fields, $fields );
}
add_filter( 'pre_wp_nav_menu', array( $this, 'get_nav_menu' ), 30, 2 );
add_filter( 'wp_nav_menu', array( $this, 'save_nav_menu' ), PHP_INT_MAX, 2 );
}
/**
* @param string $nav_menu_html
* @param object $args
* @return string
*/
public function get_nav_menu( $nav_menu_html, $args ) {
if ( $this->is_enabled( $args ) ) {
$found = null;
$cache = wp_cache_get( $this->get_cache_key( $args ), self::GROUP, false, $found );
if ( $found ) {
/** @var string $cache */
return $cache;
}
}
return $nav_menu_html;
}
/**
* @param string $nav_menu_html
* @param object $args
* @return string
*/
public function save_nav_menu( $nav_menu_html, $args ) {
if ( $this->is_enabled( $args ) ) {
$key = $this->get_cache_key( $args );
wp_cache_set( $key, $nav_menu_html, self::GROUP, DAY_IN_SECONDS );
$this->remember_key( $key );
}
return $nav_menu_html;
}
/**
* @return void
*/
public function flush_all() {
foreach ( $this->get_all_keys() as $key ) {
wp_cache_delete( $key, self::GROUP );
}
wp_cache_delete( 'key_list', self::GROUP );
}
/**
* @param string $key
* @return void
*/
private function remember_key( $key ) {
// @TODO Not atomic
$found = false;
$key_list = wp_cache_get( 'key_list', self::GROUP, false, $found );
$key_list = $found ? $key_list . '|' . $key : $key;
wp_cache_set( 'key_list', $key_list, self::GROUP, DAY_IN_SECONDS );
}
/**
* @return array<string>
*/
private function get_all_keys() {
$found = null;
/** @var string $key_list */
$key_list = wp_cache_get( 'key_list', self::GROUP, false, $found );
if ( ! $found ) {
$key_list = '';
}
return explode( '|', $key_list );
}
/**
* Check excluded nav menus and the query string.
*
* @param object $args
* @return bool
*/
private function is_enabled( $args ) {
// Excluded theme locations.
if ( defined( 'TINY_CACHE_NAV_MENU_EXCLUDES' ) && TINY_CACHE_NAV_MENU_EXCLUDES ) {
$excludes = explode( '|', TINY_CACHE_NAV_MENU_EXCLUDES );
if ( property_exists( $args, 'theme_location' )
&& ! empty( $args->theme_location )
&& in_array( $args->theme_location, $excludes, true )
) {
return false;
}
}
// Do not cache requests with query string except whitelisted ones.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( [] !== array_diff( array_keys( $_GET ), $this->whitelisted_query_string_fields ) ) {
return false;
}
return true;
}
/**
* @param object $args
* @return string
*/
private function get_cache_key( $args ) {
$request_uri = isset( $_SERVER['REQUEST_URI'] )
? $_SERVER['REQUEST_URI']
: ''; // WPCS: sanitization, input var OK.
return md5( 'nav_menu-' . $args->menu_id . $request_uri );
}
}
new Tiny_Nav_Menu_Cache();