Repository navigation
Expand file tree
/
Copy pathclass-query.php
More file actions
280 lines (232 loc) · 7.49 KB
/
Copy pathclass-query.php
File metadata and controls
280 lines (232 loc) · 7.49 KB
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
<?php
/**
* Queries the database for stream records.
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class - Query
*/
class Query {
/**
* Hold the number of records found
*
* @var int
*/
public $found_records = 0;
/**
* Query records
*
* @param array $args Arguments to filter the records by.
*
* @return array Stream Records
*/
public function query( $args ) {
global $wpdb;
$join = '';
$where = '';
/**
* PARSE CORE PARAMS
*/
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 ( is_numeric( $args['object_id'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.object_id = %d", $args['object_id'] );
}
if ( is_numeric( $args['user_id'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.user_id = %d", $args['user_id'] );
}
if ( ! empty( $args['user_role'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.user_role = %s", $args['user_role'] );
}
if ( ! empty( $args['search'] ) ) {
$field = ! empty( $args['search_field'] ) ? $args['search_field'] : 'summary';
// Sanitize field.
$allowed_fields = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'created', 'summary', 'connector', 'context', 'action', 'ip' );
if ( in_array( $field, $allowed_fields, true ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.{$field} LIKE %s", "%{$args['search']}%" ); // @codingStandardsIgnoreLine can't prepare column name
}
}
if ( ! empty( $args['connector'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.connector = %s", $args['connector'] );
}
if ( ! empty( $args['context'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.context = %s", $args['context'] );
}
if ( ! empty( $args['action'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.action = %s", $args['action'] );
}
if ( ! empty( $args['ip'] ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ip = %s", wp_stream_filter_var( $args['ip'], FILTER_VALIDATE_IP ) );
}
/**
* PARSE DATE PARAM FAMILY
*/
if ( ! empty( $args['date'] ) ) {
$args['date_from'] = $args['date'];
$args['date_to'] = $args['date'];
}
if ( ! empty( $args['date_from'] ) ) {
$date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_from'] . ' 00:00:00' ) ) );
$where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) >= %s", $date );
}
if ( ! empty( $args['date_to'] ) ) {
$date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_to'] . ' 23:59:59' ) ) );
$where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) <= %s", $date );
}
if ( ! empty( $args['date_after'] ) ) {
$date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_after'] ) ) );
$where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) > %s", $date );
}
if ( ! empty( $args['date_before'] ) ) {
$date = get_gmt_from_date( gmdate( 'Y-m-d H:i:s', strtotime( $args['date_before'] ) ) );
$where .= $wpdb->prepare( " AND DATE($wpdb->stream.created) < %s", $date );
}
/**
* PARSE __IN PARAM FAMILY
*/
$ins = array();
foreach ( $args as $arg => $value ) {
if ( '__in' === substr( $arg, -4 ) ) {
$ins[ $arg ] = $value;
}
}
if ( ! empty( $ins ) ) {
foreach ( $ins as $key => $value ) {
if ( empty( $value ) || ! is_array( $value ) ) {
continue;
}
$field = str_replace( array( 'record_', '__in' ), '', $key );
$field = empty( $field ) ? 'ID' : $field;
$type = is_numeric( array_shift( $value ) ) ? '%d' : '%s';
if ( ! empty( $value ) ) {
$format = '(' . join( ',', array_fill( 0, count( $value ), $type ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.%s IN {$format}", $field, $value ); // @codingStandardsIgnoreLine prepare okay
}
}
}
/**
* PARSE __NOT_IN PARAM FAMILY
*/
$not_ins = array();
foreach ( $args as $arg => $value ) {
if ( '__not_in' === substr( $arg, -8 ) ) {
$not_ins[ $arg ] = $value;
}
}
if ( ! empty( $not_ins ) ) {
foreach ( $not_ins as $key => $value ) {
if ( empty( $value ) || ! is_array( $value ) ) {
continue;
}
$field = str_replace( array( 'record_', '__not_in' ), '', $key );
$field = empty( $field ) ? 'ID' : $field;
$type = is_numeric( array_shift( $value ) ) ? '%d' : '%s';
if ( ! empty( $value ) ) {
$format = '(' . join( ',', array_fill( 0, count( $value ), $type ) ) . ')';
$where .= $wpdb->prepare( " AND $wpdb->stream.%s NOT IN {$format}", $field, $value ); // @codingStandardsIgnoreLine prepare okay
}
}
}
/**
* PARSE PAGINATION PARAMS
*/
$limits = '';
$page = absint( $args['paged'] );
$per_page = absint( $args['records_per_page'] );
if ( $per_page >= 0 ) {
$offset = absint( ( $page - 1 ) * $per_page );
$limits = "LIMIT {$offset}, {$per_page}";
}
/**
* PARSE ORDER PARAMS
*/
$orderable = array( 'ID', 'site_id', 'blog_id', 'object_id', 'user_id', 'user_role', 'summary', 'created', 'connector', 'context', 'action' );
// Default to sorting by record ID.
$orderby = "$wpdb->stream.ID";
if ( in_array( $args['orderby'], $orderable, true ) ) {
$orderby = sprintf( '%s.%s', $wpdb->stream, $args['orderby'] );
} elseif ( 'meta_value_num' === $args['orderby'] && ! empty( $args['meta_key'] ) ) {
$orderby = "CAST($wpdb->streammeta.meta_value AS SIGNED)";
} elseif ( 'meta_value' === $args['orderby'] && ! empty( $args['meta_key'] ) ) {
$orderby = "$wpdb->streammeta.meta_value";
}
// Show the recent records first by default.
$order = 'DESC';
if ( 'ASC' === strtoupper( $args['order'] ) ) {
$order = 'ASC';
}
$orderby = sprintf( 'ORDER BY %s %s', $orderby, $order );
/**
* PARSE FIELDS PARAMETER
*/
$fields = (array) $args['fields'];
$selects = array();
if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
// We'll query the meta table later.
if ( 'meta' === $field ) {
continue;
}
$selects[] = sprintf( "$wpdb->stream.%s", $field );
}
} else {
$selects[] = "$wpdb->stream.*";
}
$select = implode( ', ', $selects );
/**
* Filters query WHERE statement as an alternative to filtering
* the $query using the hook below.
*
* @param string $where WHERE statement.
*
* @return string
*/
$where = apply_filters( 'wp_stream_db_query_where', $where );
/**
* BUILD THE FINAL QUERY
*/
$query = "SELECT {$select}
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}
{$orderby}
{$limits}";
/**
* Filter allows the final query to be modified before execution
*
* @param string $query
* @param array $args
*
* @return string
*/
$query = apply_filters( 'wp_stream_db_query', $query, $args );
// Build result count query.
$count_query = "SELECT COUNT(*) as found
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}";
/**
* Filter allows the result count query to be modified before execution.
*
* @param string $query
* @param array $args
*
* @return string
*/
$count_query = apply_filters( 'wp_stream_db_count_query', $count_query, $args );
/**
* QUERY THE DATABASE FOR RESULTS
*/
$result = array(
'items' => $wpdb->get_results( $query ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
'count' => absint( $wpdb->get_var( $count_query ) ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
);
return $result;
}
}