@@ -111,19 +111,98 @@ public static function get_keys() {
111111 }
112112
113113 /**
114- * Get the meta key based on a list of parameters.
114+ * Get the cache key based on a list of parameters.
115115 *
116116 * @since 3.3.0
117117 *
118118 * @param mixed $attr Array of attributes typically.
119119 * @param string $context Context of the cache key to be set.
120120 * @return string Cache meta key
121121 */
122- public static function get_key ( $ attr , $ context = 'query ' ) {
122+ public static function get_key ( $ attr , $ context = 'query ' ): string {
123+ $ args = (array ) $ attr ;
123124
124- $ key = sprintf ( 'bs_cache_%1$s_%2$s ' , md5 ( wp_json_encode ( $ attr ) ), $ context );
125+ static $ setting_types = null ;
126+ if ( null === $ setting_types ) {
127+ $ setting_types = function_exists ( 'bsearch_get_registered_settings_types ' ) ? bsearch_get_registered_settings_types () : array ();
128+ }
129+
130+ // Remove args that don't affect query results.
131+ $ exclude_keys = array (
132+ 'echo ' ,
133+ 'extra_class ' ,
134+ 'heading ' ,
135+ 'is_block ' ,
136+ 'is_manual ' ,
137+ 'is_shortcode ' ,
138+ 'is_widget ' ,
139+ 'other_attributes ' ,
140+ );
141+
142+ foreach ( $ exclude_keys as $ key ) {
143+ unset( $ args [ $ key ] );
144+ }
145+
146+ // Remove any keys ending in _header or _desc, or with type 'header'.
147+ foreach ( $ args as $ key => $ value ) {
148+ if ( '_header ' === substr ( $ key , -7 ) || '_desc ' === substr ( $ key , -5 ) ) {
149+ unset( $ args [ $ key ] );
150+ continue ;
151+ }
152+
153+ if ( isset ( $ setting_types [ $ key ] ) && 'header ' === $ setting_types [ $ key ] ) {
154+ unset( $ args [ $ key ] );
155+ }
156+ }
157+
158+ // Define categories of types for normalization.
159+ $ id_array_types = array ( 'postids ' , 'numbercsv ' , 'taxonomies ' );
160+ $ string_array_types = array ( 'posttypes ' , 'csv ' , 'multicheck ' );
161+ $ numeric_types = array ( 'number ' , 'checkbox ' , 'select ' , 'radio ' , 'radiodesc ' );
162+
163+ // Process arguments based on their registered types.
164+ foreach ( $ args as $ key => $ value ) {
165+ $ type = $ setting_types [ $ key ] ?? '' ;
166+
167+ if ( in_array ( $ type , $ numeric_types , true ) && is_numeric ( $ value ) ) {
168+ $ args [ $ key ] = (int ) $ value ;
169+ } elseif ( in_array ( $ type , $ id_array_types , true ) ) {
170+ $ args [ $ key ] = is_array ( $ value ) ? $ value : wp_parse_id_list ( $ value );
171+ $ args [ $ key ] = array_unique ( array_map ( 'absint ' , $ args [ $ key ] ) );
172+ $ args [ $ key ] = array_filter ( $ args [ $ key ] );
173+ sort ( $ args [ $ key ] );
174+ if ( empty ( $ args [ $ key ] ) ) {
175+ unset( $ args [ $ key ] );
176+ }
177+ } elseif ( in_array ( $ type , $ string_array_types , true ) ) {
178+ if ( is_string ( $ value ) && strpos ( $ value , '= ' ) !== false ) {
179+ parse_str ( $ value , $ parsed );
180+ $ value = array_keys ( $ parsed );
181+ } elseif ( is_string ( $ value ) ) {
182+ $ value = explode ( ', ' , $ value );
183+ }
184+ $ args [ $ key ] = is_array ( $ value ) ? $ value : array ( $ value );
185+ $ args [ $ key ] = array_unique ( array_map ( 'strval ' , $ args [ $ key ] ) );
186+ $ args [ $ key ] = array_filter ( $ args [ $ key ] );
187+ sort ( $ args [ $ key ] );
188+ if ( empty ( $ args [ $ key ] ) ) {
189+ unset( $ args [ $ key ] );
190+ }
191+ }
192+ }
193+
194+ // Sort top-level arguments.
195+ ksort ( $ args );
196+
197+ // Remove any remaining empty strings or null values.
198+ foreach ( $ args as $ key => $ value ) {
199+ if ( '' === $ value || null === $ value ) {
200+ unset( $ args [ $ key ] );
201+ }
202+ }
125203
126- return $ key ;
204+ // Generate cache key.
205+ return sprintf ( 'bs_cache_%1$s_%2$s ' , md5 ( wp_json_encode ( $ args ) ), $ context );
127206 }
128207
129208 /**
0 commit comments