@@ -14,7 +14,7 @@ class Tiny_Nav_Menu_Cache {
1414 private const GROUP = 'navmenu ' ;
1515
1616 /**
17- * @var array List of whitelisted query string fields (these do not prevent cache write).
17+ * @var array<string> List of whitelisted query string fields (these do not prevent cache write).
1818 */
1919 private $ whitelisted_query_string_fields = [
2020 // https://support.google.com/searchads/answer/7342044
@@ -35,6 +35,9 @@ public function __construct() {
3535 add_action ( 'init ' , array ( $ this , 'init ' ) );
3636 }
3737
38+ /**
39+ * @return void
40+ */
3841 public function init () {
3942
4043 // Detect object cache
@@ -49,22 +52,22 @@ public function init() {
4952 add_action ( 'split_shared_term ' , array ( $ this , 'flush_all ' ) );
5053
5154 // 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.
55+ if ( is_user_logged_in () /* User is logged in */
56+ || ! ( isset ( $ _SERVER ['REQUEST_METHOD ' ] ) && 'GET ' === $ _SERVER ['REQUEST_METHOD ' ] ) /* Not a GET request */ // WPCS: input var OK.
5457 || ( defined ( 'DONOTCACHEPAGE ' ) && DONOTCACHEPAGE ) /* DO-NOT-CACHE tag present */
5558 ) {
5659 return ;
5760 }
5861
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');
62+ // Add user-defined query parameters to the whitelist. Define the parameters you
63+ // want whitelisted in wp-config in the following way:
64+ //
65+ // define('TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS', 'XDEBUG_TRIGGER|do_xhprof_profile');
6366
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- }
67+ if ( defined ( 'TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS ' ) ) {
68+ $ fields = array_map ( 'trim ' , explode ( '| ' , TINY_NAV_CACHE_WHITELIST_QUERY_STRING_FIELDS ) );
69+ $ this ->whitelisted_query_string_fields = array_merge ( $ this ->whitelisted_query_string_fields , $ fields );
70+ }
6871
6972 add_filter ( 'pre_wp_nav_menu ' , array ( $ this , 'get_nav_menu ' ), 30 , 2 );
7073 add_filter ( 'wp_nav_menu ' , array ( $ this , 'save_nav_menu ' ), PHP_INT_MAX , 2 );
@@ -81,6 +84,7 @@ public function get_nav_menu( $nav_menu_html, $args ) {
8184 $ found = null ;
8285 $ cache = wp_cache_get ( $ this ->get_cache_key ( $ args ), self ::GROUP , false , $ found );
8386 if ( $ found ) {
87+ /** @var string $cache */
8488 return $ cache ;
8589 }
8690 }
@@ -104,6 +108,9 @@ public function save_nav_menu( $nav_menu_html, $args ) {
104108 return $ nav_menu_html ;
105109 }
106110
111+ /**
112+ * @return void
113+ */
107114 public function flush_all () {
108115
109116 foreach ( $ this ->get_all_keys () as $ key ) {
@@ -114,23 +121,25 @@ public function flush_all() {
114121
115122 /**
116123 * @param string $key
124+ * @return void
117125 */
118126 private function remember_key ( $ key ) {
119127
120128 // @TODO Not atomic
121- $ found = false ;
129+ $ found = false ;
122130 $ key_list = wp_cache_get ( 'key_list ' , self ::GROUP , false , $ found );
123131
124132 $ key_list = $ found ? $ key_list . '| ' . $ key : $ key ;
125133 wp_cache_set ( 'key_list ' , $ key_list , self ::GROUP , DAY_IN_SECONDS );
126134 }
127135
128136 /**
129- * @return array
137+ * @return array<string>
130138 */
131139 private function get_all_keys () {
132140
133- $ found = null ;
141+ $ found = null ;
142+ /** @var string $key_list */
134143 $ key_list = wp_cache_get ( 'key_list ' , self ::GROUP , false , $ found );
135144 if ( ! $ found ) {
136145 $ key_list = '' ;
@@ -161,7 +170,7 @@ private function is_enabled( $args ) {
161170
162171 // Do not cache requests with query string except whitelisted ones.
163172 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
164- if ( [] !== array_diff ( array_keys ( $ _GET ), $ this ->whitelisted_query_string_fields ) ) {
173+ if ( [] !== array_diff ( array_keys ( $ _GET ), $ this ->whitelisted_query_string_fields ) ) {
165174 return false ;
166175 }
167176
0 commit comments