Skip to:
Content

BuddyPress.org

Changeset 9574


Ignore:
Timestamp:
03/01/2015 08:36:34 PM (12 years ago)
Author:
johnjamesjacoby
Message:

Notifications: Wire up meta queries so they return accurate results, largely lifted from Activity component. More to fine-tune here. See #6257.

Location:
trunk/src/bp-notifications
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/bp-notifications-template.php

    r9570 r9574  
    297297                        'per_page'          => 25,
    298298                        'max'               => null,
     299                        'meta_query'        => false,
     300                        'date_query'        => false
    299301                ) );
    300302
    … …  
    325327                        'component_name'    => $r['component_name'],
    326328                        'component_action'  => $r['component_action'],
     329                        'meta_query'        => $r['meta_query'],
     330                        'date_query'        => $r['date_query'],
    327331                        'is_new'            => $this->is_new,
    328332                        'search_terms'      => $this->search_terms,
    … …  
    535539                'order_by'          => 'date_notified',
    536540                'sort_order'        => 'DESC',
     541                'meta_query'        => false,
     542                'date_query'        => false,
    537543                'page'              => 1,
    538544                'per_page'          => 25,
  • trunk/src/bp-notifications/classes/class-bp-notifications-notification.php

    r9486 r9574  
    283283         *
    284284         * @param array $args See {@link BP_Notifications_Notification::get()}
    285          *        for more details.
     285         *                    for more details.
    286286         * @return string WHERE clause.
    287287         */
    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 = '' ) {
    289289                global $wpdb;
    290290
    … …  
    362362                        $where_conditions['search_terms'] = $wpdb->prepare( "( component_name LIKE %s OR component_action LIKE %s )", $search_terms_like, $search_terms_like );
    363363                }
     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 );
    364388
    365389                // Custom WHERE
    … …  
    589613
    590614                // Parse the arguments
    591                 $r  = wp_parse_args( $args, array(
     615                $r = wp_parse_args( $args, array(
    592616                        'id'                => false,
    593617                        'user_id'           => false,
    … …  
    602626                        'page'              => false,
    603627                        'per_page'          => false,
     628                        'meta_query'        => false,
     629                        'date_query'        => false,
     630                        'update_meta_cache' => true,
     631                        'count_total'       => false,
    604632                ) );
     633
     634                // Get BuddyPress
     635                $bp = buddypress();
     636
     637                // METADATA
     638                $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
    605639
    606640                // SELECT
    … …  
    608642
    609643                // 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'];
    611648
    612649                // WHERE
    … …  
    620657                        'is_new'            => $r['is_new'],
    621658                        'search_terms'      => $r['search_terms'],
    622                 ) );
     659                        'date_query'        => $r['date_query']
     660                ), $select_sql, $from_sql, $join_sql, $meta_query_sql );
    623661
    624662                // ORDER BY
    … …  
    634672                ) );
    635673
    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}";
    637676
    638677                return $wpdb->get_results( $sql );
    … …  
    673712                // Return the queried results
    674713                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() );
    675784        }
    676785
Note: See TracChangeset for help on using the changeset viewer.