-
Notifications
You must be signed in to change notification settings - Fork 116
/
query.php
executable file
·427 lines (365 loc) · 13.7 KB
/
query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
class WP_Stream_Query {
public static $instance;
public static function get_instance() {
if ( ! self::$instance ) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
/**
* Query Stream records
*
* @param array|string $args Query args
* @return array Stream Records
*/
public function query( $args ) {
global $wpdb;
$defaults = array(
// Pagination params
'records_per_page' => get_option( 'posts_per_page' ),
'paged' => 1,
// Search param
'search' => null,
// Stream core fields filtering
'type' => 'stream',
'object_id' => null,
'ip' => null,
'site_id' => is_multisite() ? get_current_site()->id : 1,
'blog_id' => is_network_admin() ? null : get_current_blog_id(),
// Author params
'author' => null,
'author_role' => null,
// Date-based filters
'date' => null,
'date_from' => null,
'date_to' => null,
// Visibility filters
'visibility' => null,
// __in params
'record_greater_than' => null,
'record__in' => array(),
'record__not_in' => array(),
'record_parent' => '',
'record_parent__in' => array(),
'record_parent__not_in' => array(),
'author__in' => array(),
'author__not_in' => array(),
'author_role__in' => array(),
'author_role__not_in' => array(),
'ip__in' => array(),
'ip__not_in' => array(),
// Order
'order' => 'desc',
'orderby' => 'ID',
// Meta/Taxonomy sub queries
'meta_query' => array(),
'context_query' => array(),
// Fields selection
'fields' => '',
'ignore_context' => null,
// Hide records that match the exclude rules
'hide_excluded' => ! empty( WP_Stream_Settings::$options['exclude_hide_previous_records'] ),
);
$args = wp_parse_args( $args, $defaults );
/**
* Filter allows additional arguments to query $args
*
* @param array Array of query arguments
* @return array Updated array of query arguments
*/
$args = apply_filters( 'wp_stream_query_args', $args );
if ( true === $args['hide_excluded'] ) {
$args = self::add_excluded_record_args( $args );
}
$join = '';
$where = '';
// Only join with context table for correct types of records
if ( ! $args['ignore_context'] ) {
$join = sprintf(
' INNER JOIN %1$s ON ( %1$s.record_id = %2$s.ID )',
$wpdb->streamcontext,
$wpdb->stream
);
}
/**
* PARSE CORE FILTERS
*/
if ( $args['object_id'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.object_id = %d", $args['object_id'] );
}
if ( $args['type'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.type = %s", $args['type'] );
}
if ( $args['ip'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ip = %s", wp_stream_filter_var( $args['ip'], FILTER_VALIDATE_IP ) );
}
if ( is_numeric( $args['site_id'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.site_id = %d", $args['site_id'] );
}
if ( is_numeric( $args['blog_id'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.blog_id = %d", $args['blog_id'] );
}
if ( $args['search'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.summary LIKE %s", "%{$args['search']}%" );
}
if ( $args['author'] || '0' === $args['author'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author = %d", (int) $args['author'] );
}
if ( $args['author_role'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author_role = %s", $args['author_role'] );
}
if ( $args['visibility'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.visibility = %s", $args['visibility'] );
}
/**
* PARSE DATE FILTERS
*/
if ( $args['date'] ) {
$where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) = %s", $args['date'] );
} else {
if ( $args['date_from'] ) {
$where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) >= %s", $args['date_from'] );
}
if ( $args['date_to'] ) {
$where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) <= %s", $args['date_to'] );
}
}
/**
* PARSE __IN PARAM FAMILY
*/
if ( $args['record_greater_than'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID > %d", (int) $args['record_greater_than'] );
}
if ( $args['record__in'] ) {
$record__in = array_filter( (array) $args['record__in'], 'is_numeric' );
if ( ! empty( $record__in ) ) {
$record__in_format = '(' . join( ',', array_fill( 0, count( $record__in ), '%d' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ID IN {$record__in_format}", $record__in );
}
}
if ( $args['record__not_in'] ) {
$record__not_in = array_filter( (array) $args['record__not_in'], 'is_numeric' );
if ( ! empty( $record__not_in ) ) {
$record__not_in_format = '(' . join( ',', array_fill( 0, count( $record__not_in ), '%d' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ID NOT IN {$record__not_in_format}", $record__not_in );
}
}
if ( $args['record_parent'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent = %d", (int) $args['record_parent'] );
}
if ( $args['record_parent__in'] ) {
$record_parent__in = array_filter( (array) $args['record_parent__in'], 'is_numeric' );
if ( ! empty( $record_parent__in ) ) {
$record_parent__in_format = '(' . join( ',', array_fill( 0, count( $record_parent__in ), '%d' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.parent IN {$record_parent__in_format}", $record_parent__in );
}
}
if ( $args['record_parent__not_in'] ) {
$record_parent__not_in = array_filter( (array) $args['record_parent__not_in'], 'is_numeric' );
if ( ! empty( $record_parent__not_in ) ) {
$record_parent__not_in_format = '(' . join( ',', array_fill( 0, count( $record_parent__not_in ), '%d' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.parent NOT IN {$record_parent__not_in_format}", $record_parent__not_in );
}
}
if ( $args['author__in'] ) {
$author__in = array_filter( (array) $args['author__in'], 'is_numeric' );
if ( ! empty( $author__in ) ) {
$author__in_format = '(' . join( ',', array_fill( 0, count( $author__in ), '%d' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.author IN {$author__in_format}", $author__in );
}
}
if ( $args['author__not_in'] ) {
$author__not_in = array_filter( (array) $args['author__not_in'], 'is_numeric' );
if ( ! empty( $author__not_in ) ) {
$author__not_in_format = '(' . join( ',', array_fill( 0, count( $author__not_in ), '%d' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.author NOT IN {$author__not_in_format}", $author__not_in );
}
}
if ( $args['author_role__in'] ) {
if ( ! empty( $args['author_role__in'] ) ) {
$author_role__in = '(' . join( ',', array_fill( 0, count( $args['author_role__in'] ), '%s' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.author_role IN {$author_role__in}", $args['author_role__in'] );
}
}
if ( $args['author_role__not_in'] ) {
if ( ! empty( $args['author_role__not_in'] ) ) {
$author_role__not_in = '(' . join( ',', array_fill( 0, count( $args['author_role__not_in'] ), '%s' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.author_role NOT IN {$author_role__not_in}", $args['author_role__not_in'] );
}
}
if ( $args['ip__in'] ) {
if ( ! empty( $args['ip__in'] ) ) {
$ip__in = '(' . join( ',', array_fill( 0, count( $args['ip__in'] ), '%s' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ip IN {$ip__in}", $args['ip__in'] );
}
}
if ( $args['ip__not_in'] ) {
if ( ! empty( $args['ip__not_in'] ) ) {
$ip__not_in = '(' . join( ',', array_fill( 0, count( $args['ip__not_in'] ), '%s' ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ip NOT IN {$ip__not_in}", $args['ip__not_in'] );
}
}
/**
* PARSE META QUERY PARAMS
*/
$meta_query = new WP_Meta_Query;
$meta_query->parse_query_vars( $args );
if ( ! empty( $meta_query->queries ) ) {
$mclauses = $meta_query->get_sql( 'stream', $wpdb->stream, 'ID' );
$join .= str_replace( 'stream_id', 'record_id', $mclauses['join'] );
$where .= str_replace( 'stream_id', 'record_id', $mclauses['where'] );
}
/**
* PARSE CONTEXT PARAMS
*/
if ( ! $args['ignore_context'] ) {
$context_query = new WP_Stream_Context_Query( $args );
$cclauses = $context_query->get_sql();
$join .= $cclauses['join'];
$where .= $cclauses['where'];
}
/**
* PARSE PAGINATION PARAMS
*/
$page = intval( $args['paged'] );
$perpage = intval( $args['records_per_page'] );
if ( $perpage >= 0 ) {
$offset = ($page - 1) * $perpage;
$limits = "LIMIT $offset, {$perpage}";
} else {
$limits = '';
}
/**
* PARSE ORDER PARAMS
*/
$order = esc_sql( $args['order'] );
$orderby = esc_sql( $args['orderby'] );
$orderable = array( 'ID', 'site_id', 'blog_id', 'object_id', 'author', 'author_role', 'summary', 'visibility', 'parent', 'type', 'created' );
if ( in_array( $orderby, $orderable ) ) {
$orderby = $wpdb->stream . '.' . $orderby;
} elseif ( in_array( $orderby, array( 'connector', 'context', 'action' ) ) ) {
$orderby = $wpdb->streamcontext . '.' . $orderby;
} elseif ( 'meta_value_num' === $orderby && ! empty( $args['meta_key'] ) ) {
$orderby = "CAST($wpdb->streammeta.meta_value AS SIGNED)";
} elseif ( 'meta_value' === $orderby && ! empty( $args['meta_key'] ) ) {
$orderby = "$wpdb->streammeta.meta_value";
} else {
$orderby = "$wpdb->stream.ID";
}
$orderby = 'ORDER BY ' . $orderby . ' ' . $order;
/**
* PARSE FIELDS PARAMETER
*/
$fields = $args['fields'];
$select = "$wpdb->stream.*";
if ( ! $args['ignore_context'] ) {
$select .= ", $wpdb->streamcontext.context, $wpdb->streamcontext.action, $wpdb->streamcontext.connector";
}
if ( 'ID' === $fields ) {
$select = "$wpdb->stream.ID";
} elseif ( 'summary' === $fields ) {
$select = "$wpdb->stream.summary, $wpdb->stream.ID";
}
/**
* BUILD UP THE FINAL QUERY
*/
$sql = "SELECT SQL_CALC_FOUND_ROWS $select
FROM $wpdb->stream
$join
WHERE 1=1 $where
$orderby
$limits";
/**
* Allows developers to change final SQL of Stream Query
*
* @param string $sql SQL statement
* @param array $args Arguments passed to query
* @return string
*/
$sql = apply_filters( 'wp_stream_query', $sql, $args );
$results = $wpdb->get_results( $sql );
if ( 'with-meta' === $fields && is_array( $results ) && $results ) {
$ids = array_map( 'absint', wp_list_pluck( $results, 'ID' ) );
$sql_meta = sprintf(
"SELECT * FROM $wpdb->streammeta WHERE record_id IN ( %s )",
implode( ',', $ids )
);
$meta = $wpdb->get_results( $sql_meta );
$ids_f = array_flip( $ids );
foreach ( $meta as $meta_record ) {
$results[ $ids_f[ $meta_record->record_id ] ]->meta[ $meta_record->meta_key ][] = $meta_record->meta_value;
}
}
return $results;
}
/**
* Function will add excluded settings args into stream query
*
* @param $args array query args passed to stream_query
*
* @return array
*/
public static function add_excluded_record_args( $args ) {
// Remove record of excluded connector
$args['connector__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'connectors' );
// Remove record of excluded context
$args['context__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'contexts' );
// Remove record of excluded actions
$args['action__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'actions' );
// Remove record of excluded author
$args['author__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'authors_and_roles' );
// Remove record of excluded ip
$args['ip__not_in'] = WP_Stream_Settings::get_excluded_by_key( 'ip_addresses' );
return $args;
}
}
function wp_stream_query( $args = array() ) {
return WP_Stream_Query::get_instance()->query( $args );
}
function wp_stream_get_meta( $record_id, $key = '', $single = false ) {
return maybe_unserialize( get_metadata( 'record', $record_id, $key, $single ) );
}
function wp_stream_update_meta( $record_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'record', $record_id, $meta_key, $meta_value, $prev_value );
}
/**
* Returns array of existing values for requested column.
* Used to fill search filters with only used items, instead of all items.
*
* GROUP BY allows query to find just the first occurance of each value in the column,
* increasing the efficiency of the query.
*
* @todo increase security against injections
*
* @see assemble_records
* @since 1.0.4
* @param string Requested Column (i.e., 'context')
* @param string Requested Table
* @return array Array of items to be output to select dropdowns
*/
function wp_stream_existing_records( $column, $table = '' ) {
global $wpdb;
switch ( $table ) {
case 'stream' :
$rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->stream} GROUP BY {$column}", 'ARRAY_A' );
break;
case 'meta' :
$rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->streammeta} GROUP BY {$column}", 'ARRAY_A' );
break;
default :
$rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->streamcontext} GROUP BY {$column}", 'ARRAY_A' );
}
if ( is_array( $rows ) && ! empty( $rows ) ) {
foreach ( $rows as $row ) {
foreach ( $row as $cell => $value ) {
$output_array[ $value ] = $value;
}
}
return (array) $output_array;
} else {
$column = sprintf( 'stream_%s', $column );
return isset( WP_Stream_Connectors::$term_labels[ $column ] ) ? WP_Stream_Connectors::$term_labels[ $column ] : array();
}
}