PluginProbe ʕ •ᴥ•ʔ
MainWP Child Reports / 1.3
MainWP Child Reports v1.3
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 10 years ago admin.php 9 years ago class-wp-stream-author.php 10 years ago connector.php 9 years ago connectors.php 9 years ago context-query.php 10 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 9 years ago live-update.php 9 years ago log.php 10 years ago network.php 10 years ago query.php 9 years ago settings.php 9 years ago
query.php
416 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 $where .= $wpdb->prepare( " AND DATE($wpdb->mainwp_reports.created) = %s", $args['date'] );
146 } else {
147 if ( isset($args['date_from']) && !empty($args['date_from']) ) {
148 $where .= $wpdb->prepare( " AND DATE($wpdb->mainwp_reports.created) >= %s", $args['date_from'] );
149 }
150 if ( isset($args['date_to']) && !empty($args['date_to']) ) {
151 $where .= $wpdb->prepare( " AND DATE($wpdb->mainwp_reports.created) <= %s", $args['date_to'] );
152 }
153 if ( isset($args['datetime_from']) && !empty($args['datetime_from']) ) {
154 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.created >= %s", $args['datetime_from'] );
155 }
156 }
157
158 /**
159 * PARSE __IN PARAM FAMILY
160 */
161 if ( $args['record_greater_than'] ) {
162 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ID > %d", (int) $args['record_greater_than'] );
163 }
164
165 if ( $args['created_greater_than'] ) {
166 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.created > %s", date('Y-m-d H:i:s', $args['created_greater_than'] ) );
167 }
168
169 if ( $args['record__in'] ) {
170 $record__in = array_filter( (array) $args['record__in'], 'is_numeric' );
171 if ( ! empty( $record__in ) ) {
172 $record__in_format = '(' . join( ',', array_fill( 0, count( $record__in ), '%d' ) ) . ')';
173 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ID IN {$record__in_format}", $record__in );
174 }
175 }
176
177 if ( $args['record__not_in'] ) {
178 $record__not_in = array_filter( (array) $args['record__not_in'], 'is_numeric' );
179 if ( ! empty( $record__not_in ) ) {
180 $record__not_in_format = '(' . join( ',', array_fill( 0, count( $record__not_in ), '%d' ) ) . ')';
181 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ID NOT IN {$record__not_in_format}", $record__not_in );
182 }
183 }
184
185 if ( $args['record_parent'] ) {
186 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.parent = %d", (int) $args['record_parent'] );
187 }
188
189 if ( $args['record_parent__in'] ) {
190 $record_parent__in = array_filter( (array) $args['record_parent__in'], 'is_numeric' );
191 if ( ! empty( $record_parent__in ) ) {
192 $record_parent__in_format = '(' . join( ',', array_fill( 0, count( $record_parent__in ), '%d' ) ) . ')';
193 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.parent IN {$record_parent__in_format}", $record_parent__in );
194 }
195 }
196
197 if ( $args['record_parent__not_in'] ) {
198 $record_parent__not_in = array_filter( (array) $args['record_parent__not_in'], 'is_numeric' );
199 if ( ! empty( $record_parent__not_in ) ) {
200 $record_parent__not_in_format = '(' . join( ',', array_fill( 0, count( $record_parent__not_in ), '%d' ) ) . ')';
201 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.parent NOT IN {$record_parent__not_in_format}", $record_parent__not_in );
202 }
203 }
204
205 if ( $args['author__in'] ) {
206 $author__in = array_filter( (array) $args['author__in'], 'is_numeric' );
207 if ( ! empty( $author__in ) ) {
208 $author__in_format = '(' . join( ',', array_fill( 0, count( $author__in ), '%d' ) ) . ')';
209 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author IN {$author__in_format}", $author__in );
210 }
211 }
212
213 if ( $args['author__not_in'] ) {
214 $author__not_in = array_filter( (array) $args['author__not_in'], 'is_numeric' );
215 if ( ! empty( $author__not_in ) ) {
216 $author__not_in_format = '(' . join( ',', array_fill( 0, count( $author__not_in ), '%d' ) ) . ')';
217 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author NOT IN {$author__not_in_format}", $author__not_in );
218 }
219 }
220
221 if ( $args['author_role__in'] ) {
222 if ( ! empty( $args['author_role__in'] ) ) {
223 $author_role__in = '(' . join( ',', array_fill( 0, count( $args['author_role__in'] ), '%s' ) ) . ')';
224 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author_role IN {$author_role__in}", $args['author_role__in'] );
225 }
226 }
227
228 if ( $args['author_role__not_in'] ) {
229 if ( ! empty( $args['author_role__not_in'] ) ) {
230 $author_role__not_in = '(' . join( ',', array_fill( 0, count( $args['author_role__not_in'] ), '%s' ) ) . ')';
231 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.author_role NOT IN {$author_role__not_in}", $args['author_role__not_in'] );
232 }
233 }
234
235 if ( $args['ip__in'] ) {
236 if ( ! empty( $args['ip__in'] ) ) {
237 $ip__in = '(' . join( ',', array_fill( 0, count( $args['ip__in'] ), '%s' ) ) . ')';
238 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ip IN {$ip__in}", $args['ip__in'] );
239 }
240 }
241
242 if ( $args['ip__not_in'] ) {
243 if ( ! empty( $args['ip__not_in'] ) ) {
244 $ip__not_in = '(' . join( ',', array_fill( 0, count( $args['ip__not_in'] ), '%s' ) ) . ')';
245 $where .= $wpdb->prepare( " AND $wpdb->mainwp_reports.ip NOT IN {$ip__not_in}", $args['ip__not_in'] );
246 }
247 }
248
249 /**
250 * PARSE META QUERY PARAMS
251 */
252 $meta_query = new WP_Meta_Query;
253 $meta_query->parse_query_vars( $args );
254
255 if ( ! empty( $meta_query->queries ) ) {
256 $mclauses = $meta_query->get_sql( 'mainwp-child-reports', $wpdb->mainwp_reports, 'ID' );
257 $join .= str_replace( 'stream_id', 'record_id', $mclauses['join'] );
258 $where .= str_replace( 'stream_id', 'record_id', $mclauses['where'] );
259 }
260
261 /**
262 * PARSE CONTEXT PARAMS
263 */
264 if ( ! $args['ignore_context'] ) {
265 $context_query = new MainWP_WP_Stream_Context_Query( $args );
266 $cclauses = $context_query->get_sql();
267 $join .= $cclauses['join'];
268 $where .= $cclauses['where'];
269 }
270
271 /**
272 * PARSE PAGINATION PARAMS
273 */
274 $page = intval( $args['paged'] );
275 $perpage = intval( $args['records_per_page'] );
276
277 if ( $perpage >= 0 ) {
278 $offset = ($page - 1) * $perpage;
279 $limits = "LIMIT $offset, {$perpage}";
280 } else {
281 $limits = '';
282 }
283
284 /**
285 * PARSE ORDER PARAMS
286 */
287 $order = esc_sql( $args['order'] );
288 $orderby = esc_sql( $args['orderby'] );
289 $orderable = array( 'ID', 'site_id', 'blog_id', 'object_id', 'author', 'author_role', 'summary', 'visibility', 'parent', 'type', 'created' );
290
291 if ( in_array( $orderby, $orderable ) ) {
292 $orderby = $wpdb->mainwp_reports . '.' . $orderby;
293 } elseif ( in_array( $orderby, array( 'connector', 'context', 'action' ) ) ) {
294 $orderby = $wpdb->mainwp_reportscontext . '.' . $orderby;
295 } elseif ( 'meta_value_num' === $orderby && ! empty( $args['meta_key'] ) ) {
296 $orderby = "CAST($wpdb->mainwp_reportsmeta.meta_value AS SIGNED)";
297 } elseif ( 'meta_value' === $orderby && ! empty( $args['meta_key'] ) ) {
298 $orderby = "$wpdb->mainwp_reportsmeta.meta_value";
299 } else {
300 $orderby = "$wpdb->mainwp_reports.ID";
301 }
302 $orderby = 'ORDER BY ' . $orderby . ' ' . $order;
303
304 /**
305 * PARSE FIELDS PARAMETER
306 */
307 $fields = $args['fields'];
308 $select = "$wpdb->mainwp_reports.*";
309
310 if ( ! $args['ignore_context'] ) {
311 $select .= ", $wpdb->mainwp_reportscontext.context, $wpdb->mainwp_reportscontext.action, $wpdb->mainwp_reportscontext.connector";
312 }
313
314 if ( 'ID' === $fields ) {
315 $select = "$wpdb->mainwp_reports.ID";
316 } elseif ( 'summary' === $fields ) {
317 $select = "$wpdb->mainwp_reports.summary, $wpdb->mainwp_reports.ID";
318 }
319
320 /**
321 * BUILD UP THE FINAL QUERY
322 */
323 $sql = "SELECT SQL_CALC_FOUND_ROWS $select
324 FROM $wpdb->mainwp_reports
325 $join
326 WHERE 1=1 $where
327 $orderby
328 $limits";
329
330 $sql = apply_filters( 'mainwp_wp_stream_query', $sql, $args );
331
332 //error_log($sql);
333
334 $results = $wpdb->get_results( $sql );
335
336 if ( 'with-meta' === $fields && is_array( $results ) && $results ) {
337 $ids = array_map( 'absint', wp_list_pluck( $results, 'ID' ) );
338 $sql_meta = sprintf(
339 "SELECT * FROM $wpdb->mainwp_reportsmeta WHERE record_id IN ( %s )",
340 implode( ',', $ids )
341 );
342
343 $meta = $wpdb->get_results( $sql_meta );
344 $ids_f = array_flip( $ids );
345
346 foreach ( $meta as $meta_record ) {
347 $results[ $ids_f[ $meta_record->record_id ] ]->meta[ $meta_record->meta_key ][] = $meta_record->meta_value;
348 }
349 }
350
351 return $results;
352 }
353
354 public static function add_excluded_record_args( $args ) {
355 // Remove record of excluded connector
356 $args['connector__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'connectors' );
357
358 // Remove record of excluded context
359 $args['context__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'contexts' );
360
361 // Remove record of excluded actions
362 $args['action__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'actions' );
363
364 // Remove record of excluded author
365 $args['author__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'authors' );
366
367 // Remove record of excluded author role
368 $args['author_role__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'roles' );
369
370 // Remove record of excluded ip
371 $args['ip__not_in'] = MainWP_WP_Stream_Settings::get_excluded_by_key( 'ip_addresses' );
372
373 return $args;
374 }
375
376 }
377
378 function mainwp_wp_stream_query( $args = array() ) {
379 return MainWP_WP_Stream_Query::get_instance()->query( $args );
380 }
381
382 function mainwp_wp_stream_get_meta( $record_id, $key = '', $single = false ) {
383 return maybe_unserialize( get_metadata( 'record', $record_id, $key, $single ) );
384 }
385
386 function mainwp_wp_stream_update_meta( $record_id, $meta_key, $meta_value, $prev_value = '' ) {
387 return update_metadata( 'record', $record_id, $meta_key, $meta_value, $prev_value );
388 }
389
390 function mainwp_wp_stream_existing_records( $column, $table = '' ) {
391 global $wpdb;
392
393 switch ( $table ) {
394 case 'stream' :
395 $rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->mainwp_reports} GROUP BY {$column}", 'ARRAY_A' );
396 break;
397 case 'meta' :
398 $rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->mainwp_reportsmeta} GROUP BY {$column}", 'ARRAY_A' );
399 break;
400 default :
401 $rows = $wpdb->get_results( "SELECT {$column} FROM {$wpdb->mainwp_reportscontext} GROUP BY {$column}", 'ARRAY_A' );
402 }
403
404 if ( is_array( $rows ) && ! empty( $rows ) ) {
405 foreach ( $rows as $row ) {
406 foreach ( $row as $cell => $value ) {
407 $output_array[ $value ] = $value;
408 }
409 }
410 return (array) $output_array;
411 } else {
412 $column = sprintf( 'stream_%s', $column );
413 return isset( MainWP_WP_Stream_Connectors::$term_labels[ $column ] ) ? MainWP_WP_Stream_Connectors::$term_labels[ $column ] : array();
414 }
415 }
416