Changeset 9574
- Timestamp:
- 03/01/2015 08:36:34 PM (10 years ago)
- Location:
- trunk/src/bp-notifications
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-notifications/bp-notifications-template.php
r9570 r9574 297 297 'per_page' => 25, 298 298 'max' => null, 299 'meta_query' => false, 300 'date_query' => false 299 301 ) ); 300 302 … … 325 327 'component_name' => $r['component_name'], 326 328 'component_action' => $r['component_action'], 329 'meta_query' => $r['meta_query'], 330 'date_query' => $r['date_query'], 327 331 'is_new' => $this->is_new, 328 332 'search_terms' => $this->search_terms, … … 535 539 'order_by' => 'date_notified', 536 540 'sort_order' => 'DESC', 541 'meta_query' => false, 542 'date_query' => false, 537 543 'page' => 1, 538 544 'per_page' => 25, -
trunk/src/bp-notifications/classes/class-bp-notifications-notification.php
r9486 r9574 283 283 * 284 284 * @param array $args See {@link BP_Notifications_Notification::get()} 285 * for more details.285 * for more details. 286 286 * @return string WHERE clause. 287 287 */ 288 protected static function get_where_sql( $args = array() ) {288 protected static function get_where_sql( $args = array(), $select_sql = '', $from_sql = '', $join_sql = '', $meta_query_sql = '' ) { 289 289 global $wpdb; 290 290 … … 362 362 $where_conditions['search_terms'] = $wpdb->prepare( "( component_name LIKE %s OR component_action LIKE %s )", $search_terms_like, $search_terms_like ); 363 363 } 364 365 // date query 366 if ( ! empty( $args['date_query'] ) ) { 367 $where_conditions['date_query'] = self::get_date_query_sql( $args['date_query'] ); 368 } 369 370 // meta query 371 if ( ! empty( $meta_query_sql['where'] ) ) { 372 $where_conditions['meta_query'] = $meta_query_sql['where']; 373 } 374 375 /** 376 * Filters the MySQL WHERE conditions for the Notifications items get method. 377 * 378 * @since BuddyPress (2.3.0) 379 * 380 * @param array $where_conditions Current conditions for MySQL WHERE statement. 381 * @param array $args Parsed arguments passed into method. 382 * @param string $select_sql Current SELECT MySQL statement at point of execution. 383 * @param string $from_sql Current FROM MySQL statement at point of execution. 384 * @param string $join_sql Current INNER JOIN MySQL statement at point of execution. 385 * @param string $meta_query_sql Current meta query WHERE statement at point of execution. 386 */ 387 $where_conditions = apply_filters( 'bp_notifications_get_where_conditions', $where_conditions, $args, $select_sql, $from_sql, $join_sql, $meta_query_sql ); 364 388 365 389 // Custom WHERE … … 589 613 590 614 // Parse the arguments 591 $r 615 $r = wp_parse_args( $args, array( 592 616 'id' => false, 593 617 'user_id' => false, … … 602 626 'page' => false, 603 627 'per_page' => false, 628 'meta_query' => false, 629 'date_query' => false, 630 'update_meta_cache' => true, 631 'count_total' => false, 604 632 ) ); 633 634 // Get BuddyPress 635 $bp = buddypress(); 636 637 // METADATA 638 $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] ); 605 639 606 640 // SELECT … … 608 642 609 643 // FROM 610 $from_sql = "FROM " . buddypress()->notifications->table_name; 644 $from_sql = "FROM {$bp->notifications->table_name} n "; 645 646 // JOIN 647 $join_sql = $meta_query_sql['join']; 611 648 612 649 // WHERE … … 620 657 'is_new' => $r['is_new'], 621 658 'search_terms' => $r['search_terms'], 622 ) ); 659 'date_query' => $r['date_query'] 660 ), $select_sql, $from_sql, $join_sql, $meta_query_sql ); 623 661 624 662 // ORDER BY … … 634 672 ) ); 635 673 636 $sql = "{$select_sql} {$from_sql} {$where_sql} {$order_sql} {$pag_sql}"; 674 // Concatenate query parts 675 $sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} {$order_sql} {$pag_sql}"; 637 676 638 677 return $wpdb->get_results( $sql ); … … 673 712 // Return the queried results 674 713 return $wpdb->get_var( $sql ); 714 } 715 716 /** 717 * Get the SQL for the 'meta_query' param in BP_Notifications_Notification::get(). 718 * 719 * We use WP_Meta_Query to do the heavy lifting of parsing the 720 * meta_query array and creating the necessary SQL clauses. However, 721 * since BP_Notifications_Notification::get() builds its SQL differently than 722 * WP_Query, we have to alter the return value (stripping the leading 723 * AND keyword from the 'where' clause). 724 * 725 * @since BuddyPress (2.3.0) 726 * 727 * @param array $meta_query An array of meta_query filters. See the 728 * documentation for WP_Meta_Query for details. 729 * @return array $sql_array 'join' and 'where' clauses. 730 */ 731 public static function get_meta_query_sql( $meta_query = array() ) { 732 733 // Default array keys & empty values 734 $sql_array = array( 735 'join' => '', 736 'where' => '', 737 ); 738 739 // Bail if no meta query 740 if ( empty( $meta_query ) ) { 741 return $sql_array; 742 } 743 744 // WP_Meta_Query expects the table name at $wpdb->notificationmeta 745 $GLOBALS['wpdb']->notificationmeta = buddypress()->notifications->table_name_meta; 746 747 $n_meta_query = new WP_Meta_Query( $meta_query ); 748 $meta_sql = $n_meta_query->get_sql( 'notification', 'n', 'id' ); 749 750 // Strip the leading AND - it's handled in get() 751 $sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] ); 752 $sql_array['join'] = $meta_sql['join']; 753 754 return $sql_array; 755 } 756 757 /** 758 * Get the SQL for the 'date_query' param in BP_Notifications_Notification::get(). 759 * 760 * We use BP_Date_Query, which extends WP_Date_Query, to do the heavy lifting 761 * of parsing the date_query array and creating the necessary SQL clauses. 762 * However, since BP_Notifications_Notification::get() builds its SQL 763 * differently than WP_Query, we have to alter the return value (stripping 764 * the leading AND keyword from the query). 765 * 766 * @since BuddyPress (2.3.0) 767 * 768 * @param array $date_query An array of date_query parameters. See the 769 * documentation for the first parameter of WP_Date_Query. 770 * @return string 771 */ 772 public static function get_date_query_sql( $date_query = array() ) { 773 774 // Bail if not a proper date query format 775 if ( empty( $date_query ) || ! is_array( $date_query ) || ! class_exists( 'BP_Date_Query' ) ) { 776 return ''; 777 } 778 779 // Date query 780 $date_query = new BP_Date_Query( $date_query, 'date_recorded' ); 781 782 // Strip the leading AND - it's handled in get() 783 return preg_replace( '/^\sAND/', '', $date_query->get_sql() ); 675 784 } 676 785
Note: See TracChangeset
for help on using the changeset viewer.