@@ -16,7 +16,7 @@ class Tiny_Nav_Menu_Cache {
1616 /**
1717 * @var array List of whitelisted query string fields (these do not prevent cache write).
1818 */
19- private const WHITELISTED_QUERY_STRING_FIELDS = [
19+ private $ whitelisted_query_string_fields = [
2020 // https://support.google.com/searchads/answer/7342044
2121 'gclid ' ,
2222 'gclsrc ' ,
@@ -49,13 +49,23 @@ public function init() {
4949 add_action ( 'split_shared_term ' , array ( $ this , 'flush_all ' ) );
5050
5151 // Learned from W3TC Page Cache rules and WP Super Cache rules
52- if ( is_user_logged_in () /* User is logged in */
53- || ! ( isset ( $ _SERVER ['REQUEST_METHOD ' ] ) && 'GET ' === $ _SERVER ['REQUEST_METHOD ' ] ) /* Not a GET request */ // WPCS: input var OK.
52+ if ( is_user_logged_in () /* User is logged in */
53+ || ! ( isset ( $ _SERVER ['REQUEST_METHOD ' ] ) && 'GET ' === $ _SERVER ['REQUEST_METHOD ' ] ) /* Not a GET request */ // WPCS: input var OK.
5454 || ( defined ( 'DONOTCACHEPAGE ' ) && DONOTCACHEPAGE ) /* DO-NOT-CACHE tag present */
5555 ) {
5656 return ;
5757 }
5858
59+ // Add user-defined query parameters to the whitelist. Define the parameters you
60+ // want whitelisted in wp-config in the following way:
61+ //
62+ // define('TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS', 'XDEBUG_TRIGGER|do_xhprof_profile');
63+
64+ if ( defined ( 'TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS ' ) ) {
65+ $ fields = array_map ( 'trim ' , explode ( '| ' , TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS ) );
66+ $ this ->whitelisted_query_string_fields = array_merge ( $ this ->whitelisted_query_string_fields , $ fields );
67+ }
68+
5969 add_filter ( 'pre_wp_nav_menu ' , array ( $ this , 'get_nav_menu ' ), 30 , 2 );
6070 add_filter ( 'wp_nav_menu ' , array ( $ this , 'save_nav_menu ' ), PHP_INT_MAX , 2 );
6171 }
@@ -71,7 +81,6 @@ public function get_nav_menu( $nav_menu_html, $args ) {
7181 $ found = null ;
7282 $ cache = wp_cache_get ( $ this ->get_cache_key ( $ args ), self ::GROUP , false , $ found );
7383 if ( $ found ) {
74-
7584 return $ cache ;
7685 }
7786 }
@@ -152,7 +161,7 @@ private function is_enabled( $args ) {
152161
153162 // Do not cache requests with query string except whitelisted ones.
154163 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
155- if ( [] !== array_diff ( array_keys ( $ _GET ), self :: WHITELISTED_QUERY_STRING_FIELDS ) ) {
164+ if ( [] !== array_diff ( array_keys ( $ _GET ), $ this -> whitelisted_query_string_fields ) ) {
156165 return false ;
157166 }
158167
@@ -169,7 +178,7 @@ private function get_cache_key( $args ) {
169178 ? $ _SERVER ['REQUEST_URI ' ]
170179 : '' ; // WPCS: sanitization, input var OK.
171180
172- return md5 ( wp_json_encode ( $ args ) . $ request_uri );
181+ return md5 ( ' nav_menu- ' . $ args-> menu_id . $ request_uri );
173182 }
174183}
175184
0 commit comments