-
Notifications
You must be signed in to change notification settings - Fork 116
/
query.php
362 lines (314 loc) · 10.6 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
<?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' => 10,
'paged' => 1,
// Search params
'search' => null,
// Stream core fields filtering
'type' => 'stream',
'object_id' => null,
'ip' => null,
// Author param
'author' => 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(),
'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,
);
$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( 'stream_query_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 ( $args['search'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.summary LIKE %s", "%{$args['search']}%" );
}
if ( $args['author'] ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author LIKE %d", (int) $args['author'] );
}
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 = implode( ',', array_filter( (array) $args['record__in'], 'is_numeric' ) );
if ( $record__in ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID IN ($record__in)", '' );
}
}
if ( $args['record__not_in'] ) {
$record__not_in = implode( ',', array_filter( (array) $args['record__not_in'], 'is_numeric' ) );
if ( strlen( $record__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID NOT IN ($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 = implode( ',', array_filter( (array) $args['record_parent__in'], 'is_numeric' ) );
if ( strlen( $record_parent__in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent IN ($record_parent__in)", '' );
}
}
if ( $args['record_parent__not_in'] ) {
$record_parent__not_in = implode( ',', array_filter( (array) $args['record_parent__not_in'], 'is_numeric' ) );
if ( strlen( $record_parent__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent NOT IN ($record_parent__not_in)", '' );
}
}
if ( $args[ 'author__in' ] ) {
$author__in = implode( ',', array_filter( (array)$args[ 'author__in' ], 'is_numeric' ) );
if ( $author__in ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author IN ($author__in)", '' );
}
}
if ( $args[ 'author__not_in' ] ) {
$author__not_in = implode( ',', array_filter( (array)$args[ 'author__not_in' ], 'is_numeric' ) );
if ( strlen( $author__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author NOT IN ($author__not_in)", '' );
}
}
if ( $args[ 'ip__in' ] ) {
if ( count( $args[ 'ip__in' ] ) > 0 ) {
$ip__in = '(' . substr( str_repeat( ',%s', count( $args[ 'ip__in' ] ) ), 1 ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.ip IN {$ip__in}", $args[ 'ip__in' ] );
}
}
if ( $args[ 'ip__not_in' ] ) {
if ( count( $args[ 'ip__not_in' ] ) > 0 ) {
$ip__not_in = '(' . substr( str_repeat( ',%s', count( $args[ 'ip__not_in' ] ) ), 1 ) . ')';
$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', 'object_id', 'author', '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 ) ) {
$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 stream_query( $args = array() ) {
return WP_Stream_Query::get_instance()->query( $args );
}
function get_stream_meta( $record_id, $key = '', $single = false ) {
return get_metadata( 'record', $record_id, $key, $single );
}
function update_stream_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 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();
}
}