PluginProbe ʕ •ᴥ•ʔ
MainWP Child Reports / 1.8
MainWP Child Reports v1.8
0.0.1 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9.1 1.9.2 1.9.3 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1 2.1.1 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.3 2.3.1 trunk
mainwp-child-reports / includes / query.php
mainwp-child-reports / includes Last commit date
vendor 7 years ago admin.php 7 years ago class-wp-stream-author.php 10 years ago connector.php 7 years ago connectors.php 7 years ago context-query.php 7 years ago dashboard.php 10 years ago date-interval.php 10 years ago db.php 9 years ago filter-input.php 10 years ago functions.php 10 years ago install.php 9 years ago list-table.php 7 years ago live-update.php 9 years ago log.php 10 years ago network.php 7 years ago query.php 7 years ago settings.php 7 years ago
query.php
421 lines
1 <?php
2
3 class MainWP_WP_Stream_Query {
4
5 public static $instance;
6
7 public static function get_instance() {
8 if ( ! self::$instance ) {
9 $class = __CLASS__;
10 self::$instance = new $class;
11 }
12
13 return self::$instance;
14 }
15
16 public function query( $args ) {
17 global $wpdb;
18
19 $defaults = array(
20 // Pagination params
21 'records_per_page' => get_option( 'posts_per_page' ),
22 'paged' => 1,
23 // Search param
24 'search' => null,
25 // Stream core fields filtering
26 'type' => 'stream',
27 'object_id' => null,
28 'ip' => null,
29 'site_id' => is_multisite() ? get_current_site()->id : 1,
30 'blog_id' => is_network_admin() ? null : get_current_blog_id(),
31 // Author params
32 'author' => null,
33 'author_role' => null,
34 // Date-based filters
35 'date' => null,
36 'date_from' => null,
37 'date_to' => null,
38 // Visibility filters
39 'visibility' => null,
40 // __in params
41 'record_greater_than' => null,
42 'created_greater_than' => null,
43 'record__in' => array(),
44 'record__not_in' => array(),
45 'record_parent' => '',
46 'record_parent__in' => array(),
47 'record_parent__not_in' => array(),
48 'author__in' => array(),
49 'author__not_in' => array(),
50 'author_role__in' => array(),
51 'author_role__not_in' => array(),
52 'ip__in' => array(),
53 'ip__not_in' => array(),
54 // Order
55 'order' => 'desc',
56 'orderby' => 'created',
57 // Meta/Taxonomy sub queries
58 'meta_query' => array(),
59 'context_query' => array(),
60 // Fields selection
61 'fields' => '',
62 'ignore_context' => null,
63 // Hide records that match the exclude rules
64 'hide_excluded' => ! empty( MainWP_WP_Stream_Settings::$options['exclude_hide_previous_records'] ),
65 );
66
67 $args = wp_parse_args( $args, $defaults );
68
69 $args = apply_filters( 'mainwp_wp_stream_query_args', $args );
70
71 if ( true === $args['hide_excluded'] ) {
72 $args = self::add_excluded_record_args( $args );
73 }
74
75 $join = '';
76 $where = '';
77
78 // Only join with context table for correct types of records
79 if ( ! $args['ignore_context'] ) {
80 $join = sprintf(
81 ' INNER JOIN %1$s ON ( %1$s.record_id = %2$s.ID )',
82 $wpdb->mainwp_reportscontext,
83 $wpdb->mainwp_reports
84 );
85 }
86
87 /**
88 * PARSE CORE FILTERS
89 */
90 if ( $args['object_id'] ) {
91 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.object_id = %d", $args['object_id'] );
92 }
93
94 if ( $args['type'] ) {
95 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.type = %s", $args['type'] );
96 }
97
98 if ( $args['ip'] ) {
99 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ip = %s", mainwp_wp_stream_filter_var( $args['ip'], FILTER_VALIDATE_IP ) );
100 }
101
102 if ( is_numeric( $args['site_id'] ) ) {
103 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.site_id = %d", $args['site_id'] );
104 }
105
106 if ( is_numeric( $args['blog_id'] ) ) {
107 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.blog_id = %d", $args['blog_id'] );
108 }
109
110 if ( $args['search'] ) {
111 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.summary LIKE %s", "%{$args['search']}%" );
112 }
113
114 if ( $args['author'] || '0' === $args['author'] ) {
115 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author = %d", (int) $args['author'] );
116 }
117
118 if ( $args['author_role'] ) {
119 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author_role = %s", $args['author_role'] );
120 }
121
122 if ( $args['visibility'] ) {
123 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.visibility = %s", $args['visibility'] );
124 }
125
126 if (isset($args['hide_child_reports']) && $args['hide_child_reports']) {
127 $child_record_ids = array();
128 $sql_meta = "SELECT record_id FROM $wpdb->mainwp_reportsmeta WHERE meta_key = 'slug' AND (meta_value = 'mainwp-child/mainwp-child.php' OR meta_value = 'mainwp-child-reports/mainwp-child-reports.php')";
129 $ret = $wpdb->get_results( $sql_meta, 'ARRAY_A' );
130
131 if (is_array($ret) && count($ret)> 0) {
132 foreach($ret as $val) {
133 $child_record_ids[] = $val['record_id'];
134 }
135 }
136 if (count($child_record_ids) > 0) {
137 $where .= " AND $wpdb->mainwp_reports.ID NOT IN (" . implode(",", $child_record_ids). ") ";
138 }
139 }
140
141 /**
142 * PARSE DATE FILTERS
143 */
144 if ( isset( $args['date'] ) && !empty( $args['date'] ) ) {
145 $date = get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( $args['date'] ) ) );
146 $where .= " AND (DATE($wpdb->mainwp_reports.created) = STR_TO_DATE(" . $wpdb->prepare('%s', $date) . ", '%Y-%m-%d %H:%i:%s'))";
147 } else {
148 if ( isset($args['date_from']) && !empty($args['date_from']) ) {
149 $date = get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( $args['date_from'] ) ) );
150 $where .= " AND ($wpdb->mainwp_reports.created >= STR_TO_DATE(" . $wpdb->prepare('%s', $date) . ", '%Y-%m-%d %H:%i:%s'))";
151 }
152 if ( isset($args['date_to']) && !empty($args['date_to']) ) {
153 $date = get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( $args['date_to'] ) ) );
154 $where .= " AND ($wpdb->mainwp_reports.created <= STR_TO_DATE(" . $wpdb->prepare('%s', $date) . ", '%Y-%m-%d %H:%i:%s'))";
155 }
156 if ( isset($args['datetime_from']) && !empty($args['datetime_from']) ) {
157 $date = get_gmt_from_date( date( 'Y-m-d H:i:s', strtotime( $args['datetime_from'] ) ) );
158 $where .= " AND ($wpdb->mainwp_reports.created >= STR_TO_DATE(" . $wpdb->prepare('%s', $date) . ", '%Y-%m-%d %H:%i:%s'))";
159 }
160 }
161
162 /**
163 * PARSE __IN PARAM FAMILY
164 */
165 if ( $args['record_greater_than'] ) {
166 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ID > %d", (int) $args['record_greater_than'] );
167 }
168
169 if ( $args['created_greater_than'] ) {
170 $date = date( 'Y-m-d H:i:s', $args['created_greater_than'] );
171 $where .= " AND ($wpdb->mainwp_reports.created > STR_TO_DATE(" . $wpdb->prepare('%s', $date) . ", '%Y-%m-%d %H:%i:%s'))";
172 }
173
174 if ( $args['record__in'] ) {
175 $record__in = array_filter( (array) $args['record__in'], 'is_numeric' );
176 if ( ! empty( $record__in ) ) {
177 $record__in_format = '(' . join( ',', array_fill( 0, count( $record__in ), '%d' ) ) . ')';
178 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ID IN {$record__in_format}", $record__in );
179 }
180 }
181
182 if ( $args['record__not_in'] ) {
183 $record__not_in = array_filter( (array) $args['record__not_in'], 'is_numeric' );
184 if ( ! empty( $record__not_in ) ) {
185 $record__not_in_format = '(' . join( ',', array_fill( 0, count( $record__not_in ), '%d' ) ) . ')';
186 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ID NOT IN {$record__not_in_format}", $record__not_in );
187 }
188 }
189
190 if ( $args['record_parent'] ) {
191 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.parent = %d", (int) $args['record_parent'] );
192 }
193
194 if ( $args['record_parent__in'] ) {
195 $record_parent__in = array_filter( (array) $args['record_parent__in'], 'is_numeric' );
196 if ( ! empty( $record_parent__in ) ) {
197 $record_parent__in_format = '(' . join( ',', array_fill( 0, count( $record_parent__in ), '%d' ) ) . ')';
198 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.parent IN {$record_parent__in_format}", $record_parent__in );
199 }
200 }
201
202 if ( $args['record_parent__not_in'] ) {
203 $record_parent__not_in = array_filter( (array) $args['record_parent__not_in'], 'is_numeric' );
204 if ( ! empty( $record_parent__not_in ) ) {
205 $record_parent__not_in_format = '(' . join( ',', array_fill( 0, count( $record_parent__not_in ), '%d' ) ) . ')';
206 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.parent NOT IN {$record_parent__not_in_format}", $record_parent__not_in );
207 }
208 }
209
210 if ( $args['author__in'] ) {
211 $author__in = array_filter( (array) $args['author__in'], 'is_numeric' );
212 if ( ! empty( $author__in ) ) {
213 $author__in_format = '(' . join( ',', array_fill( 0, count( $author__in ), '%d' ) ) . ')';
214 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author IN {$author__in_format}", $author__in );
215 }
216 }
217
218 if ( $args['author__not_in'] ) {
219 $author__not_in = array_filter( (array) $args['author__not_in'], 'is_numeric' );
220 if ( ! empty( $author__not_in ) ) {
221 $author__not_in_format = '(' . join( ',', array_fill( 0, count( $author__not_in ), '%d' ) ) . ')';
222 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author NOT IN {$author__not_in_format}", $author__not_in );
223 }
224 }
225
226 if ( $args['author_role__in'] ) {
227 if ( ! empty( $args['author_role__in'] ) ) {
228 $author_role__in = '(' . join( ',', array_fill( 0, count( $args['author_role__in'] ), '%s' ) ) . ')';
229 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author_role IN {$author_role__in}", $args['author_role__in'] );
230 }
231 }
232
233 if ( $args['author_role__not_in'] ) {
234 if ( ! empty( $args['author_role__not_in'] ) ) {
235 $author_role__not_in = '(' . join( ',', array_fill( 0, count( $args['author_role__not_in'] ), '%s' ) ) . ')';
236 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author_role NOT IN {$author_role__not_in}", $args['author_role__not_in'] );
237 }
238 }
239
240 if ( $args['ip__in'] ) {
241 if ( ! empty( $args['ip__in'] ) ) {
242 $ip__in = '(' . join( ',', array_fill( 0, count( $args['ip__in'] ), '%s' ) ) . ')';
243 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ip IN {$ip__in}", $args['ip__in'] );
244 }
245 }
246
247 if ( $args['ip__not_in'] ) {
248 if ( ! empty( $args['ip__not_in'] ) ) {
249 $ip__not_in = '(' . join( ',', array_fill( 0, count( $args['ip__not_in'] ), '%s' ) ) . ')';
250 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ip NOT IN {$ip__not_in}", $args['ip__not_in'] );
251 }
252 }
253
254 /**
255 * PARSE META QUERY PARAMS
256 */
257 $meta_query = new WP_Meta_Query;
258 $meta_query->parse_query_vars( $args );
259
260 if ( ! empty( $meta_query->queries ) ) {
261 $mclauses = $meta_query->get_sql( 'mainwp-child-reports', $wpdb->mainwp_reports, 'ID' );
262 $join .= str_replace( 'stream_id', 'record_id', $mclauses['join'] );
263 $where .= str_replace( 'stream_id', 'record_id', $mclauses['where'] );
264 }
265
266 /**
267 * PARSE CONTEXT PARAMS
268 */
269 if ( ! $args['ignore_context'] ) {
270 $context_query = new MainWP_WP_Stream_Context_Query( $args );
271 $cclauses = $context_query->get_sql();
272 $join .= $cclauses['join'];
273 $where .= $cclauses['where'];
274 }
275
276 /**
277 * PARSE PAGINATION PARAMS
278 */
279 $page = intval( $args['paged'] );
280 $perpage = intval( $args['records_per_page'] );
281
282 if ( $perpage >= 0 ) {
283 $offset = ($page - 1) * $perpage;
284 $limits = "LIMIT $offset, {$perpage}";
285 } else {
286 $limits = '';
287 }
288
289 /**
290 * PARSE ORDER PARAMS
291 */
292 $order = esc_sql( $args['order'] );
293 $orderby = esc_sql( $args['orderby'] );
294 $orderable = array( 'ID', 'site_id', 'blog_id', 'object_id', 'author', 'author_role', 'summary', 'visibility', 'parent', 'type', 'created' );
295
296 if ( in_array( $orderby, $orderable ) ) {
297 $orderby = $wpdb->mainwp_reports . '.' . $orderby;
298 } elseif ( in_array( $orderby, array( 'connector', 'context', 'action' ) ) ) {
299 $orderby = $wpdb->mainwp_reportscontext . '.' . $orderby;
300 } elseif ( 'meta_value_num' === $orderby && ! empty( $args['meta_key'] ) ) {
301 $orderby = "CAST($wpdb->mainwp_reportsmeta.meta_value AS SIGNED)";
302 } elseif ( 'meta_value' === $orderby && ! empty( $args['meta_key'] ) ) {
303 $orderby = "$wpdb->mainwp_reportsmeta.meta_value";
304 } else {
305 $orderby = "$wpdb->mainwp_reports.ID";
306 }
307 $orderby = 'ORDER BY ' . $orderby . ' ' . $order;
308
309 /**
310 * PARSE FIELDS PARAMETER
311 */
312 $fields = $args['fields'];
313 $select = "$wpdb->mainwp_reports.*";
314
315 if ( ! $args['ignore_context'] ) {
316 $select .= ", $wpdb->mainwp_reportscontext.context, $wpdb->mainwp_reportscontext.action, $wpdb->mainwp_reportscontext.connector";
317 }
318
319 if ( 'ID' === $fields ) {
320 $select = "$wpdb->mainwp_reports.ID";
321 } elseif ( 'summary' === $fields ) {
322 $select = "$wpdb->mainwp_reports.summary, $wpdb->mainwp_reports.ID";
323 }
324
325 /**
326 * BUILD UP THE FINAL QUERY
327 */
328 $sql = "SELECT SQL_CALC_FOUND_ROWS $select
329 FROM $wpdb->mainwp_reports
330 $join
331 WHERE 1=1 $where
332 $orderby
333 $limits";
334
335 $sql = apply_filters( 'mainwp_wp_stream_query', $sql, $args );
336
337 //error_log($sql);
338
339 $results = $wpdb->get_results( $sql );
340
341 if ( 'with-meta' === $fields && is_array( $results ) && $results ) {
342 $ids = array_map( 'absint', wp_list_pluck( $results, 'ID' ) );
343 $sql_meta = sprintf(
344 "SELECT * FROM $wpdb->mainwp_reportsmeta WHERE record_id IN ( %s )",
345 implode( ',', $ids )
346 );
347
348 $meta = $wpdb->get_results( $sql_meta );
349 $ids_f = array_flip( $ids );
350
351 foreach ( $meta as $meta_record ) {
352 $results[ $ids_f[ $meta_record->record_id ] ]->meta[ $meta_record->meta_key ][] = $meta_record->meta_value;
353 }
354 }
355
356 return $results;
357 }
358
359 public static function add_excluded_record_args( $args ) {
360 // Remove record of excluded connector
361 $args['connector__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'connectors' );
362
363 // Remove record of excluded context
364 $args['context__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'contexts' );
365
366 // Remove record of excluded actions
367 $args['action__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'actions' );
368
369 // Remove record of excluded author
370 $args['author__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'authors' );
371
372 // Remove record of excluded author role
373 $args['author_role__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'roles' );
374
375 // Remove record of excluded ip
376 $args['ip__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'ip_addresses' );
377
378 return $args;
379 }
380
381 }
382
383 function mainwp_wp_stream_query( $args = array() ) {
384 return MainWP_WP_Stream_Query::get_instance()->query( $args );
385 }
386
387 function mainwp_wp_stream_get_meta( $record_id, $key = '', $single = false ) {
388 return maybe_unserialize( get_metadata( 'record', $record_id, $key, $single ) );
389 }
390
391 function mainwp_wp_stream_update_meta( $record_id, $meta_key, $meta_value, $prev_value = '' ) {
392 return update_metadata( 'record', $record_id, $meta_key, $meta_value, $prev_value );
393 }
394
395 function mainwp_wp_stream_existing_records( $column, $table = '' ) {
396 global $wpdb;
397
398 switch ( $table ) {
399 case 'stream' :
400 $rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->mainwp_reports} GROUP BY {$column}", 'ARRAY_A' );
401 break;
402 case 'meta' :
403 $rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->mainwp_reportsmeta} GROUP BY {$column}", 'ARRAY_A' );
404 break;
405 default :
406 $rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->mainwp_reportscontext} GROUP BY {$column}", 'ARRAY_A' );
407 }
408
409 if ( is_array( $rows ) && ! empty( $rows ) ) {
410 foreach ( $rows as $row ) {
411 foreach ( $row as $cell => $value ) {
412 $output_array[ $value ] = $value;
413 }
414 }
415 return (array) $output_array;
416 } else {
417 $column = sprintf( 'stream_%s', $column );
418 return isset( MainWP_WP_Stream_Connectors::$term_labels[ $column ] ) ? MainWP_WP_Stream_Connectors::$term_labels[ $column ] : array();
419 }
420 }
421