14
14
*/
15
15
class WP_Request_Processor {
16
16
17
- const PARAM_CUSTOMFIELD_PARAMES = 'custom_field_params ' ;
17
+ const PARAM_CUSTOMFIELD_PARAMS = 'custom_field_params ' ;
18
+ const PARAM_CUSTOMFIELD_PARAMS_ATTR = 'custom_field_params_attr ' ;
18
19
19
20
/**
20
21
* Post meta provider.
@@ -43,7 +44,7 @@ public function __construct( WP_Post_Meta $post_meta ) {
43
44
* @return mixed
44
45
*/
45
46
public function register_extra_query_vars ( $ public_query_vars ) {
46
- array_push ( $ public_query_vars , self ::PARAM_CUSTOMFIELD_PARAMES );
47
+ array_push ( $ public_query_vars , self ::PARAM_CUSTOMFIELD_PARAMS , self :: PARAM_CUSTOMFIELD_PARAMS_ATTR );
47
48
48
49
return $ public_query_vars ;
49
50
}
@@ -71,7 +72,7 @@ public function process_request( $query_vars ) {
71
72
* 1. Custom field key does not exists or
72
73
* 2. Custom field value does not matches.
73
74
*
74
- * @param bool $preempt Whether to short-circuit default header status handling. Default false.
75
+ * @param bool $preempt Whether to short-circuit default header status handling. Default false.
75
76
* @param WP_Query $wp_query WordPress Query object.
76
77
*
77
78
* @return bool Returning a non-false value from the filter will short-circuit the handling
@@ -88,25 +89,33 @@ public function pre_handle_404( $preempt, $wp_query ) {
88
89
$ post = $ wp_query ->post ;
89
90
90
91
// Analyse only if custom field used in query.
91
- if ( ! array_key_exists ( self ::PARAM_CUSTOMFIELD_PARAMES , $ wp_query ->query_vars )
92
- || ! is_array ( $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMES ] ) ) {
92
+ if ( ! array_key_exists ( self ::PARAM_CUSTOMFIELD_PARAMS , $ wp_query ->query_vars )
93
+ || ! is_array ( $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMS ] )
94
+ ) {
93
95
return false ;
94
96
}
95
97
96
- $ query_meta_params = $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMES ];
98
+ $ query_meta_params = $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMS ];
99
+ $ query_meta_params_attr = $ this ->get_param_attr ( $ wp_query );
97
100
98
101
$ raise_404 = false ;
99
102
100
- $ post_meta = $ this ->post_meta ->get_post_meta ( $ post );
101
-
102
103
foreach ( $ query_meta_params as $ query_meta_key => $ query_meta_value ) {
103
- if ( ! array_key_exists ( $ query_meta_key , $ post_meta ) ) {
104
+ if ( array_key_exists ( $ query_meta_key , $ query_meta_params_attr ) ) {
105
+ $ field_attr = $ query_meta_params_attr [ $ query_meta_key ];
106
+ } else {
107
+ $ field_attr = array ();
108
+ }
109
+
110
+ $ post_meta_values = $ this ->post_meta ->get_post_meta_single ( $ post , $ query_meta_key , $ field_attr );
111
+
112
+ if ( null === $ post_meta_values || ! $ post_meta_values ) {
104
113
$ raise_404 = true ;
105
114
break ;
106
115
} else {
107
116
// Look for at least one value match.
108
117
$ value_matched = false ;
109
- foreach ( $ post_meta [ $ query_meta_key ] as $ post_meta_value ) {
118
+ foreach ( $ post_meta_values as $ post_meta_value ) {
110
119
$ post_meta_value_sanitized = sanitize_title ( $ post_meta_value );
111
120
112
121
if ( $ query_meta_value == $ post_meta_value_sanitized ) {
@@ -132,4 +141,33 @@ public function pre_handle_404( $preempt, $wp_query ) {
132
141
133
142
return false ;
134
143
}
144
+
145
+ /**
146
+ * Gets custom fields parameters attributes from WP_Query.
147
+ *
148
+ * @param WP_Query $wp_query WordPress Query object.
149
+ *
150
+ * @access private
151
+ * @return array
152
+ */
153
+ private function get_param_attr ( WP_Query $ wp_query ) {
154
+ if ( ! isset ( $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMS_ATTR ] ) ) {
155
+ return array ();
156
+ }
157
+
158
+ $ attrs = array ();
159
+ $ query_meta_params_attr = $ wp_query ->query_vars [ self ::PARAM_CUSTOMFIELD_PARAMS_ATTR ];
160
+
161
+ foreach ( $ query_meta_params_attr as $ attr_field_and_key => $ attr_value ) {
162
+ list ( $ field_name , $ field_attr_name ) = explode ( ':: ' , $ attr_field_and_key );
163
+
164
+ if ( ! array_key_exists ( $ field_name , $ attrs ) ) {
165
+ $ attrs [ $ field_name ] = array ();
166
+ }
167
+
168
+ $ attrs [ $ field_name ][ $ field_attr_name ] = $ attr_value ;
169
+ }
170
+
171
+ return $ attrs ;
172
+ }
135
173
}
0 commit comments