1010 */
1111class CustomFieldsPermalink {
1212
13- const PARAM_CUSTOMFIELD_KEY = 'custom_field_key ' ;
14- const PARAM_CUSTOMFIELD_VALUE = 'custom_field_value ' ;
13+ const PARAM_CUSTOMFIELD_PARAMES = 'custom_field_params ' ;
1514
1615 /**
1716 * Filters the permalink structure for a post before token replacement occurs..
@@ -95,7 +94,7 @@ public static function link_rewrite_fields_extract( $post, $field_name ) {
9594 * @return mixed
9695 */
9796 public static function register_extra_query_vars ( $ public_query_vars ) {
98- array_push ( $ public_query_vars , self ::PARAM_CUSTOMFIELD_KEY , self :: PARAM_CUSTOMFIELD_VALUE );
97+ array_push ( $ public_query_vars , self ::PARAM_CUSTOMFIELD_PARAMES );
9998
10099 return $ public_query_vars ;
101100 }
@@ -140,34 +139,36 @@ public static function pre_handle_404( $preempt, $wp_query ) {
140139 $ post = $ wp_query ->post ;
141140
142141 // Analyse only if custom field used in query.
143- if ( ! array_key_exists ( self ::PARAM_CUSTOMFIELD_KEY , $ wp_query ->query_vars ) ) {
142+ if ( ! array_key_exists ( self ::PARAM_CUSTOMFIELD_PARAMES , $ wp_query ->query_vars )
143+ || ! is_array ( $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMES ] ) ) {
144144 return false ;
145145 }
146146
147+ $ query_meta_params = $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMES ];
148+
147149 $ raise_404 = false ;
148150
149151 $ post_meta = self ::get_post_meta ( $ post );
150152
151- $ query_meta_key = $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_KEY ];
152-
153- if ( ! array_key_exists ( $ query_meta_key , $ post_meta ) ) {
154- $ raise_404 = true ;
155- } else {
156- $ query_meta_value = $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_VALUE ];
157-
158- // Look for at least one value match.
159- $ value_matched = false ;
160- foreach ( $ post_meta [ $ query_meta_key ] as $ post_meta_value ) {
161- $ post_meta_value_sanitized = sanitize_title ( $ post_meta_value );
162-
163- if ( $ query_meta_value == $ post_meta_value_sanitized ) {
164- $ value_matched = true ;
165- break ;
153+ foreach ( $ query_meta_params as $ query_meta_key => $ query_meta_value ) {
154+ if ( ! array_key_exists ( $ query_meta_key , $ post_meta ) ) {
155+ $ raise_404 = true ;
156+ break ;
157+ } else {
158+ // Look for at least one value match.
159+ $ value_matched = false ;
160+ foreach ( $ post_meta [ $ query_meta_key ] as $ post_meta_value ) {
161+ $ post_meta_value_sanitized = sanitize_title ( $ post_meta_value );
162+
163+ if ( $ query_meta_value == $ post_meta_value_sanitized ) {
164+ $ value_matched = true ;
165+ break ;
166+ }
166167 }
167- }
168168
169- if ( ! $ value_matched ) {
170- $ raise_404 = true ;
169+ if ( ! $ value_matched ) {
170+ $ raise_404 = true ;
171+ }
171172 }
172173 }
173174
@@ -194,14 +195,9 @@ public static function pre_handle_404( $preempt, $wp_query ) {
194195 * @return array
195196 */
196197 public static function rewrite_rules_array_filter ( $ rules ) {
197- $ keys = array_keys ( $ rules );
198- $ tmp = $ rules ;
199- $ rules = array ();
200-
201- $ j = sizeof ( $ keys );
202- for ( $ i = 0 ; $ i < $ j ; ++ $ i ) {
203- $ key = $ keys [ $ i ];
198+ $ new_rules = array ();
204199
200+ foreach ( $ rules as $ key => $ rule ) {
205201 if ( preg_match ( '/%field_([^%]*?)%/ ' , $ key ) ) {
206202 $ key_new = preg_replace (
207203 '/%field_([^%]*?)%/ ' ,
@@ -210,19 +206,19 @@ public static function rewrite_rules_array_filter( $rules ) {
210206 // Detect them automatically and add next $matches indices.
211207 $ key
212208 );
213- $ rules [ $ key_new ] = preg_replace (
209+ $ new_rules [ $ key_new ] = preg_replace (
214210 '/%field_([^%]*?)%/ ' ,
215- sprintf ( '%s=$1&%s = ' , self ::PARAM_CUSTOMFIELD_KEY , self :: PARAM_CUSTOMFIELD_VALUE ),
211+ sprintf ( '%s[$1] = ' , self ::PARAM_CUSTOMFIELD_PARAMES ),
216212 // Here on the end will be pasted $matches[$i] from $keyNew,
217213 // so we can grab it it the future in self::PARAM_CUSTOMFIELD_VALUE parameter.
218- $ tmp [ $ key ]
214+ $ rule
219215 );
220216 } else {
221- $ rules [ $ key ] = $ tmp [ $ key ] ;
217+ $ new_rules [ $ key ] = $ rule ;
222218 }
223219 }
224220
225- return $ rules ;
221+ return $ new_rules ;
226222 }
227223
228224 /**
@@ -256,4 +252,30 @@ private static function get_post_meta( $post ) {
256252
257253 return $ filtered_post_meta ;
258254 }
255+
256+ /**
257+ * This hook is called once any activated plugins have been loaded.
258+ *
259+ * @link https://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded
260+ */
261+ public static function on_init () {
262+ $ version_option_name = '_wordpress_custom_fields_permalink_plugin_version ' ;
263+ $ version_from = get_option ( $ version_option_name , null );
264+ $ version_to = WORDPRESS_CUSTOM_FIELDS_PERMALINK_PLUGIN_VERSION ;
265+
266+ if ( $ version_from != $ version_to ) {
267+ self ::update_plugin ( $ version_from , $ version_to );
268+ update_option ( $ version_option_name , $ version_to , true );
269+ }
270+ }
271+
272+ /**
273+ * Upgrades the plugin.
274+ *
275+ * @param string $version_from Currently running version.
276+ * @param string $version_to Version upgrade to.
277+ */
278+ public static function update_plugin ( $ version_from , $ version_to ) {
279+ flush_rewrite_rules ();
280+ }
259281}
0 commit comments