@@ -70,104 +70,75 @@ public function query( $args ) {
70
70
$ join = '' ;
71
71
$ where = '' ;
72
72
73
- /**
74
- * PARSE CORE PARAMS
75
- */
76
- if ( is_numeric ( $ args ['site_id ' ] ) ) {
77
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .site_id = %d " , $ args ['site_id ' ] );
78
- }
79
-
80
- if ( is_numeric ( $ args ['blog_id ' ] ) ) {
81
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .blog_id = %d " , $ args ['blog_id ' ] );
82
- }
83
-
84
- if ( is_numeric ( $ args ['object_id ' ] ) ) {
85
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .object_id = %d " , $ args ['object_id ' ] );
86
- }
87
-
88
- if ( is_numeric ( $ args ['user_id ' ] ) ) {
89
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .user_id = %d " , $ args ['user_id ' ] );
90
- }
91
-
92
- if ( ! empty ( $ args ['user_role ' ] ) ) {
93
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .user_role = %s " , $ args ['user_role ' ] );
94
- }
95
-
96
- if ( ! empty ( $ args ['search ' ] ) ) {
97
- $ field = ! empty ( $ args ['search_field ' ] ) ? $ args ['search_field ' ] : 'summary ' ;
98
- $ field = $ this ->lookup_field_validated ( $ field );
99
-
100
- // Sanitize field.
101
- $ allowed_fields = array ( 'ID ' , 'site_id ' , 'blog_id ' , 'object_id ' , 'user_id ' , 'user_role ' , 'created ' , 'summary ' , 'connector ' , 'context ' , 'action ' , 'ip ' );
102
- if ( in_array ( $ field , $ allowed_fields , true ) ) {
103
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream . {$ field } LIKE %s " , "% {$ args ['search ' ]}% " ); // @codingStandardsIgnoreLine can't prepare column name
73
+ foreach ( $ args as $ query_arg => $ query_value ) {
74
+ if ( empty ( $ query_value ) ) {
75
+ continue ;
104
76
}
105
- }
106
-
107
- if ( ! empty ( $ args ['connector ' ] ) ) {
108
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .connector = %s " , $ args ['connector ' ] );
109
- }
110
-
111
- if ( ! empty ( $ args ['context ' ] ) ) {
112
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .context = %s " , $ args ['context ' ] );
113
- }
114
-
115
- if ( ! empty ( $ args ['action ' ] ) ) {
116
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .action = %s " , $ args ['action ' ] );
117
- }
118
-
119
- if ( ! empty ( $ args ['ip ' ] ) ) {
120
- $ where .= $ wpdb ->prepare ( " AND $ wpdb ->stream .ip = %s " , wp_stream_filter_var ( $ args ['ip ' ], FILTER_VALIDATE_IP ) );
121
- }
122
-
123
- /**
124
- * PARSE DATE PARAM FAMILY
125
- */
126
- if ( ! empty ( $ args ['date ' ] ) ) {
127
- $ args ['date_from ' ] = $ args ['date ' ];
128
- $ args ['date_to ' ] = $ args ['date ' ];
129
- }
130
-
131
- if ( ! empty ( $ args ['date_from ' ] ) ) {
132
- $ date = get_gmt_from_date ( gmdate ( 'Y-m-d H:i:s ' , strtotime ( $ args ['date_from ' ] . ' 00:00:00 ' ) ) );
133
- $ where .= $ wpdb ->prepare ( " AND DATE( $ wpdb ->stream .created) >= %s " , $ date );
134
- }
135
-
136
- if ( ! empty ( $ args ['date_to ' ] ) ) {
137
- $ date = get_gmt_from_date ( gmdate ( 'Y-m-d H:i:s ' , strtotime ( $ args ['date_to ' ] . ' 23:59:59 ' ) ) );
138
- $ where .= $ wpdb ->prepare ( " AND DATE( $ wpdb ->stream .created) <= %s " , $ date );
139
- }
140
-
141
- if ( ! empty ( $ args ['date_after ' ] ) ) {
142
- $ date = get_gmt_from_date ( gmdate ( 'Y-m-d H:i:s ' , strtotime ( $ args ['date_after ' ] ) ) );
143
- $ where .= $ wpdb ->prepare ( " AND DATE( $ wpdb ->stream .created) > %s " , $ date );
144
- }
145
-
146
- if ( ! empty ( $ args ['date_before ' ] ) ) {
147
- $ date = get_gmt_from_date ( gmdate ( 'Y-m-d H:i:s ' , strtotime ( $ args ['date_before ' ] ) ) );
148
- $ where .= $ wpdb ->prepare ( " AND DATE( $ wpdb ->stream .created) < %s " , $ date );
149
- }
150
-
151
- /**
152
- * Parse __in and __not_in queries.
153
- */
154
- foreach ( $ args as $ key => $ value ) {
155
- $ field = $ this ->key_to_field ( $ key );
156
77
157
- if ( ! empty ( $ field ) ) {
158
- $ values_prepared = implode ( ', ' , $ this ->db_prepare_list ( $ value ) );
159
-
160
- if ( $ this ->key_is_in_lookup ( $ key ) ) {
161
- $ where .= sprintf ( " AND $ wpdb ->stream .%s IN (%s) " , $ field , $ values_prepared );
162
- } elseif ( $ this ->key_is_in_not_lookup ( $ key ) ) {
163
- $ where .= sprintf ( " AND $ wpdb ->stream .%s NOT IN (%s) " , $ field , $ values_prepared );
164
- }
78
+ switch ( $ query_arg ) {
79
+ // Process core params.
80
+ case 'site_id ' :
81
+ case 'blog_id ' :
82
+ case 'object_id ' :
83
+ case 'user_id ' :
84
+ case 'user_role ' :
85
+ case 'connector ' :
86
+ case 'context ' :
87
+ case 'action ' :
88
+ case 'ip ' :
89
+ $ where .= $ this ->and_where ( $ query_arg , $ query_value );
90
+ break ;
91
+
92
+ // Process "search*" params.
93
+ case 'search ' :
94
+ $ field = ! empty ( $ args ['search_field ' ] ) ? $ args ['search_field ' ] : 'summary ' ;
95
+ $ field = $ this ->lookup_field_validated ( $ field );
96
+
97
+ if ( ! empty ( $ field ) ) {
98
+ $ where .= $ this ->and_where ( $ field , "% {$ query_value }% " , 'LIKE ' );
99
+ }
100
+ break ;
101
+
102
+ // Process "date*" params.
103
+ case 'date ' :
104
+ $ args ['date_from ' ] = $ args ['date ' ];
105
+ $ args ['date_to ' ] = $ args ['date ' ];
106
+ break ;
107
+ case 'date_from ' :
108
+ case 'date_to ' :
109
+ case 'date_after ' :
110
+ case 'date_before ' :
111
+ if ( 'date_from ' === $ query_arg ) {
112
+ $ time = '00:00:00 ' ;
113
+ } elseif ( 'date_to ' === $ query_arg ) {
114
+ $ time = '23:59:59 ' ;
115
+ }
116
+
117
+ $ compare = $ this ->get_date_compare ( $ query_arg );
118
+
119
+ $ date = isset ( $ time ) ? strtotime ( "{$ query_value } {$ time }" ) : strtotime ( $ query_value );
120
+ $ date = get_gmt_from_date ( gmdate ( 'Y-m-d H:i:s ' , $ date ) );
121
+ $ where .= $ this ->and_where ( 'created ' , $ date , $ compare , true );
122
+ break ;
123
+
124
+ // Process all other valid params except "fields", "order" and "pagination" params.
125
+ default :
126
+ $ field = $ this ->lookup_field_validated ( $ query_arg );
127
+
128
+ if ( ! empty ( $ field ) && ! empty ( $ query_value ) ) {
129
+ $ values_prepared = implode ( ', ' , $ this ->db_prepare_list ( $ query_value ) );
130
+
131
+ if ( $ this ->key_is_in_lookup ( $ query_arg ) ) {
132
+ $ where .= sprintf ( " AND $ wpdb ->stream .%s IN (%s) " , $ field , $ values_prepared );
133
+ } elseif ( $ this ->key_is_not_in_lookup ( $ query_arg ) ) {
134
+ $ where .= sprintf ( " AND $ wpdb ->stream .%s NOT IN (%s) " , $ field , $ values_prepared );
135
+ }
136
+ }
137
+ break ;
165
138
}
166
139
}
167
140
168
- /**
169
- * PARSE PAGINATION PARAMS
170
- */
141
+ // Process pagination params.
171
142
$ limits = '' ;
172
143
$ page = absint ( $ args ['paged ' ] );
173
144
$ per_page = absint ( $ args ['records_per_page ' ] );
@@ -177,9 +148,7 @@ public function query( $args ) {
177
148
$ limits = "LIMIT {$ offset }, {$ per_page }" ;
178
149
}
179
150
180
- /**
181
- * PARSE ORDER PARAMS
182
- */
151
+ // Process order params.
183
152
$ order = esc_sql ( $ args ['order ' ] );
184
153
$ orderby = esc_sql ( $ args ['orderby ' ] );
185
154
$ orderable = array ( 'ID ' , 'site_id ' , 'blog_id ' , 'object_id ' , 'user_id ' , 'user_role ' , 'summary ' , 'created ' , 'connector ' , 'context ' , 'action ' );
@@ -196,9 +165,7 @@ public function query( $args ) {
196
165
197
166
$ orderby = "ORDER BY {$ orderby } {$ order }" ;
198
167
199
- /**
200
- * PARSE FIELDS PARAMETER
201
- */
168
+ // Process "fields" parameters.
202
169
$ fields = (array ) $ args ['fields ' ];
203
170
$ selects = array ();
204
171
@@ -217,9 +184,7 @@ public function query( $args ) {
217
184
218
185
$ select = implode ( ', ' , $ selects );
219
186
220
- /**
221
- * BUILD THE FINAL QUERY
222
- */
187
+ // Build the final query.
223
188
$ query = "SELECT SQL_CALC_FOUND_ROWS {$ select }
224
189
FROM $ wpdb ->stream
225
190
{$ join }
@@ -238,9 +203,8 @@ public function query( $args ) {
238
203
$ query = apply_filters ( 'wp_stream_db_query ' , $ query , $ args );
239
204
240
205
$ result = array ();
241
- /**
242
- * QUERY THE DATABASE FOR RESULTS
243
- */
206
+
207
+ // Execute query and return results.
244
208
$ result ['items ' ] = $ wpdb ->get_results ( $ query ); // @codingStandardsIgnoreLine $query already prepared
245
209
$ result ['count ' ] = $ result ['items ' ] ? absint ( $ wpdb ->get_var ( 'SELECT FOUND_ROWS() ' ) ) : 0 ;
246
210
@@ -305,7 +269,7 @@ protected function key_to_field( $key ) {
305
269
if ( $ this ->key_is_in_lookup ( $ key ) || $ this ->key_is_not_in_lookup ( $ key ) ) {
306
270
$ field = str_replace ( array ( 'record_ ' , '__in ' , '__not_in ' ), '' , $ key );
307
271
308
- $ this -> lookup_field ( $ field ) ;
272
+ return $ field ;
309
273
}
310
274
311
275
return null ;
@@ -319,10 +283,62 @@ protected function key_to_field( $key ) {
319
283
* @return string|null
320
284
*/
321
285
protected function lookup_field_validated ( $ field ) {
322
- if ( in_array ( $ field , $ this ->lookup_fields , true ) ) {
286
+ $ field = $ this ->key_to_field ( $ field );
287
+ if ( ! empty ( $ field ) && in_array ( $ field , $ this ->lookup_fields , true ) ) {
323
288
return $ field ;
324
289
}
325
290
326
291
return null ;
327
292
}
293
+
294
+ /**
295
+ * Return partial of prepare WHERE statement.
296
+ *
297
+ * @param string $field Field being evaluated.
298
+ * @param string|integer $value Value being compared.
299
+ * @param string $compare String representation of how value should be compare (Eg. =, <=, ...).
300
+ * @param bool $as_date A type for the value to be cast to.
301
+ *
302
+ * @return string
303
+ */
304
+ protected function and_where ( $ field , $ value , $ compare = '= ' , $ as_date = false ) {
305
+ if ( empty ( $ value ) ) {
306
+ return '' ;
307
+ }
308
+
309
+ $ field = "{$ this ->db ->stream }. {$ field }" ;
310
+ if ( $ as_date ) {
311
+ $ field = "DATE( {$ field }) " ;
312
+ }
313
+
314
+ if ( is_numeric ( $ value ) ) {
315
+ $ placeholder = '%d ' ;
316
+ } else {
317
+ $ placeholder = '%s ' ;
318
+ }
319
+
320
+ return $ this ->db ->prepare ( " AND {$ field } {$ compare } {$ placeholder }" , $ value );
321
+ }
322
+
323
+ /**
324
+ * Return the proper compare operator for the date comparing type provided.
325
+ *
326
+ * @param string $date_type Date type.
327
+ *
328
+ * @return string|null
329
+ */
330
+ protected function get_date_compare ( $ date_type ) {
331
+ switch ( $ date_type ) {
332
+ case 'date_from ' :
333
+ return '>= ' ;
334
+ case 'date_to ' :
335
+ return '<= ' ;
336
+ case 'date_after ' :
337
+ return '> ' ;
338
+ case 'date_before ' :
339
+ return '< ' ;
340
+ }
341
+
342
+ return null ;
343
+ }
328
344
}
0 commit comments