| @@ 274-374 (lines=101) @@ | ||
| 271 | * |
|
| 272 | * @return string |
|
| 273 | */ |
|
| 274 | public function get_sql( $args = array() ) { |
|
| 275 | /* @var WPDB $wpdb */ |
|
| 276 | global $wpdb; |
|
| 277 | ||
| 278 | $defaults = array( |
|
| 279 | 'number' => 20, |
|
| 280 | 'offset' => 0, |
|
| 281 | 'paged' => 0, |
|
| 282 | 'orderby' => 'date', |
|
| 283 | 'order' => 'DESC', |
|
| 284 | 'fields' => 'all', |
|
| 285 | 'count' => false, |
|
| 286 | ); |
|
| 287 | ||
| 288 | $args = wp_parse_args( $args, $defaults ); |
|
| 289 | ||
| 290 | // validate params. |
|
| 291 | $this->validate_params( $args ); |
|
| 292 | ||
| 293 | if ( $args['number'] < 1 ) { |
|
| 294 | $args['number'] = 99999999999; |
|
| 295 | } |
|
| 296 | ||
| 297 | // Where clause for primary table. |
|
| 298 | $where = ''; |
|
| 299 | ||
| 300 | // Get sql query for meta. |
|
| 301 | if ( ! empty( $args['meta_query'] ) ) { |
|
| 302 | $meta_query_object = new WP_Meta_Query( $args['meta_query'] ); |
|
| 303 | $meta_query = $meta_query_object->get_sql( 'give_comment', $this->table_name, 'comment_ID' ); |
|
| 304 | $where = implode( '', $meta_query ); |
|
| 305 | } |
|
| 306 | ||
| 307 | $where .= ' WHERE 1=1 '; |
|
| 308 | ||
| 309 | // Set offset. |
|
| 310 | if ( empty( $args['offset'] ) && ( 0 < $args['paged'] ) ) { |
|
| 311 | $args['offset'] = $args['number'] * ( $args['paged'] - 1 ); |
|
| 312 | } |
|
| 313 | ||
| 314 | // Set fields. |
|
| 315 | $fields = "{$this->table_name}.*"; |
|
| 316 | if ( is_string( $args['fields'] ) && ( 'all' !== $args['fields'] ) ) { |
|
| 317 | $fields = "{$this->table_name}.{$args['fields']}"; |
|
| 318 | } |
|
| 319 | ||
| 320 | // Set count. |
|
| 321 | if ( $args['count'] ) { |
|
| 322 | $fields = "COUNT({$fields})"; |
|
| 323 | } |
|
| 324 | ||
| 325 | // Specific comments. |
|
| 326 | if ( ! empty( $args['comment_ID'] ) ) { |
|
| 327 | ||
| 328 | if ( ! is_array( $args['comment_ID'] ) ) { |
|
| 329 | $args['comment_ID'] = explode( ',', $args['comment_ID'] ); |
|
| 330 | } |
|
| 331 | $comment_ids = implode( ',', array_map( 'intval', $args['comment_ID'] ) ); |
|
| 332 | ||
| 333 | $where .= " AND {$this->table_name}.comment_ID IN( {$comment_ids} ) "; |
|
| 334 | } |
|
| 335 | ||
| 336 | // Comments created for a specific date or in a date range |
|
| 337 | if ( ! empty( $args['date_query'] ) ) { |
|
| 338 | $date_query_object = new WP_Date_Query( $args['date_query'], "{$this->table_name}.comment_date" ); |
|
| 339 | $where .= $date_query_object->get_sql(); |
|
| 340 | } |
|
| 341 | ||
| 342 | // Comments create for specific parent. |
|
| 343 | if ( ! empty( $args['comment_parent'] ) ) { |
|
| 344 | if ( ! is_array( $args['comment_parent'] ) ) { |
|
| 345 | $args['comment_parent'] = explode( ',', $args['comment_parent'] ); |
|
| 346 | } |
|
| 347 | $parent_ids = implode( ',', array_map( 'intval', $args['comment_parent'] ) ); |
|
| 348 | ||
| 349 | $where .= " AND {$this->table_name}.comment_parent IN( {$parent_ids} ) "; |
|
| 350 | } |
|
| 351 | ||
| 352 | // Comments create for specific type. |
|
| 353 | // is_array check is for backward compatibility. |
|
| 354 | if ( ! empty( $args['comment_type'] ) && ! is_array( $args['comment_type'] ) ) { |
|
| 355 | if ( ! is_array( $args['comment_type'] ) ) { |
|
| 356 | $args['comment_type'] = explode( ',', $args['comment_type'] ); |
|
| 357 | } |
|
| 358 | ||
| 359 | $comment_types = implode( '\',\'', array_map( 'trim', $args['comment_type'] ) ); |
|
| 360 | ||
| 361 | $where .= " AND {$this->table_name}.comment_type IN( '{$comment_types}' ) "; |
|
| 362 | } |
|
| 363 | ||
| 364 | $args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'comment_date' : $args['orderby']; |
|
| 365 | ||
| 366 | $args['orderby'] = esc_sql( $args['orderby'] ); |
|
| 367 | $args['order'] = esc_sql( $args['order'] ); |
|
| 368 | ||
| 369 | return $wpdb->prepare( |
|
| 370 | "SELECT {$fields} FROM {$this->table_name} {$where} ORDER BY {$this->table_name}.{$args['orderby']} {$args['order']} LIMIT %d,%d;", |
|
| 371 | absint( $args['offset'] ), |
|
| 372 | absint( $args['number'] ) |
|
| 373 | ); |
|
| 374 | } |
|
| 375 | ||
| 376 | ||
| 377 | /** |
|
| @@ 270-370 (lines=101) @@ | ||
| 267 | * |
|
| 268 | * @return string |
|
| 269 | */ |
|
| 270 | public function get_sql( $args = array() ) { |
|
| 271 | /* @var WPDB $wpdb */ |
|
| 272 | global $wpdb; |
|
| 273 | ||
| 274 | $defaults = array( |
|
| 275 | 'number' => 20, |
|
| 276 | 'offset' => 0, |
|
| 277 | 'paged' => 0, |
|
| 278 | 'orderby' => 'date', |
|
| 279 | 'order' => 'DESC', |
|
| 280 | 'fields' => 'all', |
|
| 281 | 'count' => false, |
|
| 282 | ); |
|
| 283 | ||
| 284 | $args = wp_parse_args( $args, $defaults ); |
|
| 285 | ||
| 286 | // validate params. |
|
| 287 | $this->validate_params( $args ); |
|
| 288 | ||
| 289 | if ( $args['number'] < 1 ) { |
|
| 290 | $args['number'] = 99999999999; |
|
| 291 | } |
|
| 292 | ||
| 293 | // Where clause for primary table. |
|
| 294 | $where = ''; |
|
| 295 | ||
| 296 | // Get sql query for meta. |
|
| 297 | if ( ! empty( $args['meta_query'] ) ) { |
|
| 298 | $meta_query_object = new WP_Meta_Query( $args['meta_query'] ); |
|
| 299 | $meta_query = $meta_query_object->get_sql( 'log', $this->table_name, 'id' ); |
|
| 300 | $where = implode( '', $meta_query ); |
|
| 301 | } |
|
| 302 | ||
| 303 | $where .= ' WHERE 1=1 '; |
|
| 304 | ||
| 305 | // Set offset. |
|
| 306 | if ( empty( $args['offset'] ) && ( 0 < $args['paged'] ) ) { |
|
| 307 | $args['offset'] = $args['number'] * ( $args['paged'] - 1 ); |
|
| 308 | } |
|
| 309 | ||
| 310 | // Set fields. |
|
| 311 | $fields = "{$this->table_name}.*"; |
|
| 312 | if ( is_string( $args['fields'] ) && ( 'all' !== $args['fields'] ) ) { |
|
| 313 | $fields = "{$this->table_name}.{$args['fields']}"; |
|
| 314 | } |
|
| 315 | ||
| 316 | // Set count. |
|
| 317 | if ( $args['count'] ) { |
|
| 318 | $fields = "COUNT({$fields})"; |
|
| 319 | } |
|
| 320 | ||
| 321 | // Specific logs. |
|
| 322 | if ( ! empty( $args['ID'] ) ) { |
|
| 323 | ||
| 324 | if ( ! is_array( $args['ID'] ) ) { |
|
| 325 | $args['ID'] = explode( ',', $args['ID'] ); |
|
| 326 | } |
|
| 327 | $log_ids = implode( ',', array_map( 'intval', $args['ID'] ) ); |
|
| 328 | ||
| 329 | $where .= " AND {$this->table_name}.ID IN( {$log_ids} ) "; |
|
| 330 | } |
|
| 331 | ||
| 332 | // Logs created for a specific date or in a date range |
|
| 333 | if ( ! empty( $args['date_query'] ) ) { |
|
| 334 | $date_query_object = new WP_Date_Query( $args['date_query'], "{$this->table_name}.log_date" ); |
|
| 335 | $where .= $date_query_object->get_sql(); |
|
| 336 | } |
|
| 337 | ||
| 338 | // Logs create for specific parent. |
|
| 339 | if ( ! empty( $args['log_parent'] ) ) { |
|
| 340 | if ( ! is_array( $args['log_parent'] ) ) { |
|
| 341 | $args['log_parent'] = explode( ',', $args['log_parent'] ); |
|
| 342 | } |
|
| 343 | $parent_ids = implode( ',', array_map( 'intval', $args['log_parent'] ) ); |
|
| 344 | ||
| 345 | $where .= " AND {$this->table_name}.log_parent IN( {$parent_ids} ) "; |
|
| 346 | } |
|
| 347 | ||
| 348 | // Logs create for specific type. |
|
| 349 | // is_array check is for backward compatibility. |
|
| 350 | if ( ! empty( $args['log_type'] ) && ! is_array( $args['log_type'] ) ) { |
|
| 351 | if ( ! is_array( $args['log_type'] ) ) { |
|
| 352 | $args['log_type'] = explode( ',', $args['log_type'] ); |
|
| 353 | } |
|
| 354 | ||
| 355 | $log_types = implode( '\',\'', array_map( 'trim', $args['log_type'] ) ); |
|
| 356 | ||
| 357 | $where .= " AND {$this->table_name}.log_type IN( '{$log_types}' ) "; |
|
| 358 | } |
|
| 359 | ||
| 360 | $args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'log_date' : $args['orderby']; |
|
| 361 | ||
| 362 | $args['orderby'] = esc_sql( $args['orderby'] ); |
|
| 363 | $args['order'] = esc_sql( $args['order'] ); |
|
| 364 | ||
| 365 | return $wpdb->prepare( |
|
| 366 | "SELECT {$fields} FROM {$this->table_name} {$where} ORDER BY {$this->table_name}.{$args['orderby']} {$args['order']} LIMIT %d,%d;", |
|
| 367 | absint( $args['offset'] ), |
|
| 368 | absint( $args['number'] ) |
|
| 369 | ); |
|
| 370 | } |
|
| 371 | ||
| 372 | ||
| 373 | /** |
|