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