Skip to:
Content

BuddyPress.org

Ticket #6271: 6271.01.patch

File 6271.01.patch, 9.4 KB (added by johnjamesjacoby, 11 years ago)
  • src/bp-activity/classes/class-bp-activity-activity.php

     
    339339                        'count_total'       => false,
    340340                ) );
    341341
    342                 // Select conditions
     342                // Metadata query?
     343                $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
     344
     345                // SELECT
    343346                $select_sql = "SELECT DISTINCT a.id";
    344347
     348                // FROM
    345349                $from_sql   = " FROM {$bp->activity->table_name} a";
    346350
    347                 $join_sql   = '';
     351                // JOIN
     352                $join_sql   = ! empty( $meta_query_sql['join'] ) ? $meta_query_sql['join'] : '';
    348353
    349                 // Where conditions
    350                 $where_conditions = array();
     354                // WHERE
     355                $where_sql  = self::get_where_sql( $r, $select_sql, $from_sql, $join_sql, $meta_query_sql );
    351356
    352                 // Excluded types
    353                 $excluded_types = array();
    354 
    355                 // Scope takes precedence
    356                 if ( ! empty( $r['scope'] ) ) {
    357                         $scope_query = self::get_scope_query_sql( $r['scope'], $r );
    358 
    359                         // Add our SQL conditions if matches were found
    360                         if ( ! empty( $scope_query['sql'] ) ) {
    361                                 $where_conditions['scope_query_sql'] = $scope_query['sql'];
    362                         }
    363 
    364                         // override some arguments if needed
    365                         if ( ! empty( $scope_query['override'] ) ) {
    366                                 $r = self::array_replace_recursive( $r, $scope_query['override'] );
    367                         }
    368 
    369                 // Advanced filtering
    370                 } elseif ( ! empty( $r['filter_query'] ) ) {
    371                         $filter_query = new BP_Activity_Query( $r['filter_query'] );
    372                         $sql          = $filter_query->get_sql();
    373                         if ( ! empty( $sql ) ) {
    374                                 $where_conditions['filter_query_sql'] = $sql;
    375                         }
    376                 }
    377 
    378                 // Regular filtering
    379                 if ( $r['filter'] && $filter_sql = BP_Activity_Activity::get_filter_sql( $r['filter'] ) ) {
    380                         $where_conditions['filter_sql'] = $filter_sql;
    381                 }
    382 
    383                 // Spam
    384                 if ( 'ham_only' == $r['spam'] ) {
    385                         $where_conditions['spam_sql'] = 'a.is_spam = 0';
    386                 } elseif ( 'spam_only' == $r['spam'] ) {
    387                         $where_conditions['spam_sql'] = 'a.is_spam = 1';
    388                 }
    389 
    390                 // Searching
    391                 if ( $r['search_terms'] ) {
    392                         $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
    393                         $where_conditions['search_sql'] = $wpdb->prepare( 'a.content LIKE %s', $search_terms_like );
    394                 }
    395 
    396                 // Sorting
    397                 $sort = $r['sort'];
    398                 if ( $sort != 'ASC' && $sort != 'DESC' ) {
    399                         $sort = 'DESC';
    400                 }
    401 
    402                 // Hide Hidden Items?
    403                 if ( ! $r['show_hidden'] ) {
    404                         $where_conditions['hidden_sql'] = "a.hide_sitewide = 0";
    405                 }
    406 
    407                 // Exclude specified items
    408                 if ( ! empty( $r['exclude'] ) ) {
    409                         $exclude = implode( ',', wp_parse_id_list( $r['exclude'] ) );
    410                         $where_conditions['exclude'] = "a.id NOT IN ({$exclude})";
    411                 }
    412 
    413                 // The specific ids to which you want to limit the query
    414                 if ( ! empty( $r['in'] ) ) {
    415                         $in = implode( ',', wp_parse_id_list( $r['in'] ) );
    416                         $where_conditions['in'] = "a.id IN ({$in})";
    417                 }
    418 
    419                 // Process meta_query into SQL
    420                 $meta_query_sql = self::get_meta_query_sql( $r['meta_query'] );
    421 
    422                 if ( ! empty( $meta_query_sql['join'] ) ) {
    423                         $join_sql .= $meta_query_sql['join'];
    424                 }
    425 
    426                 if ( ! empty( $meta_query_sql['where'] ) ) {
    427                         $where_conditions[] = $meta_query_sql['where'];
    428                 }
    429 
    430                 // Process date_query into SQL
    431                 $date_query_sql = self::get_date_query_sql( $r['date_query'] );
    432 
    433                 if ( ! empty( $date_query_sql ) ) {
    434                         $where_conditions['date'] = $date_query_sql;
    435                 }
    436 
    437                 // Alter the query based on whether we want to show activity item
    438                 // comments in the stream like normal comments or threaded below
    439                 // the activity.
    440                 if ( false === $r['display_comments'] || 'threaded' === $r['display_comments'] ) {
    441                         $excluded_types[] = 'activity_comment';
    442                 }
    443 
    444                 // Exclude 'last_activity' items unless the 'action' filter has
    445                 // been explicitly set
    446                 if ( empty( $r['filter']['object'] ) ) {
    447                         $excluded_types[] = 'last_activity';
    448                 }
    449 
    450                 // Build the excluded type sql part
    451                 if ( ! empty( $excluded_types ) ) {
    452                         $not_in = "'" . implode( "', '", esc_sql( $excluded_types ) ) . "'";
    453                         $where_conditions['excluded_types'] = "a.type NOT IN ({$not_in})";
    454                 }
    455 
    456357                /**
    457                  * Filters the MySQL WHERE conditions for the Activity items get method.
    458                  *
    459                  * @since BuddyPress (1.9.0)
    460                  *
    461                  * @param array  $where_conditions Current conditions for MySQL WHERE statement.
    462                  * @param array  $r Parsed arguments passed into method.
    463                  * @param string $select_sql Current SELECT MySQL statement at point of execution.
    464                  * @param string $from_sql Current FROM MySQL statement at point of execution.
    465                  * @param string $join_sql Current INNER JOIN MySQL statement at point of execution.
    466                  */
    467                 $where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql );
    468 
    469                 // Join the where conditions together
    470                 $where_sql = 'WHERE ' . join( ' AND ', $where_conditions );
    471 
    472                 /**
    473358                 * Filters the preferred order of indexes for activity item.
    474359                 *
    475360                 * @since BuddyPress (1.6.0)
     
    495380                $page     = absint( $r['page']     );
    496381                $per_page = absint( $r['per_page'] );
    497382
     383                // Sorting
     384                $sort = $r['sort'];
     385                if ( ( $sort !== 'ASC' ) && ( $sort !== 'DESC' ) ) {
     386                        $sort = 'DESC';
     387                }
     388
    498389                $retval = array(
    499390                        'activities'     => null,
    500391                        'total'          => null,
     
    775666        }
    776667
    777668        /**
     669         * Assemble the WHERE clause of a get() SQL statement.
     670         *
     671         * Used by BP_Activity_Activity::get() to create its WHERE clause.
     672         *
     673         * @since BuddyPress (2.3.0)
     674         *
     675         * @param array $args See {@link BP_Activity_Activity::get()} for more details.
     676         *
     677         * @return string WHERE clause.
     678         */
     679        protected static function get_where_sql( $args = array(), $select_sql = '', $from_sql = '', $join_sql = '', $meta_query_sql = '' ) {
     680                global $wpdb;
     681
     682                // Where conditions
     683                $where_conditions = array();
     684
     685                // Excluded types
     686                $excluded_types = array();
     687
     688                // Scope takes precedence
     689                if ( ! empty( $args['scope'] ) ) {
     690                        $scope_query = self::get_scope_query_sql( $args['scope'], $args );
     691
     692                        // Add our SQL conditions if matches were found
     693                        if ( ! empty( $scope_query['sql'] ) ) {
     694                                $where_conditions['scope_query_sql'] = $scope_query['sql'];
     695                        }
     696
     697                        // override some arguments if needed
     698                        if ( ! empty( $scope_query['override'] ) ) {
     699                                $args = self::array_replace_recursive( $args, $scope_query['override'] );
     700                        }
     701
     702                // Advanced filtering
     703                } elseif ( ! empty( $args['filter_query'] ) ) {
     704                        $filter_query = new BP_Activity_Query( $args['filter_query'] );
     705                        $sql          = $filter_query->get_sql();
     706                        if ( ! empty( $sql ) ) {
     707                                $where_conditions['filter_query_sql'] = $sql;
     708                        }
     709                }
     710
     711                // Regular filtering
     712                if ( ! empty( $args['filter'] ) && $filter_sql = self::get_filter_sql( $args['filter'] ) ) {
     713                        $where_conditions['filter_sql'] = $filter_sql;
     714                }
     715
     716                // Spam
     717                if ( 'ham_only' === $args['spam'] ) {
     718                        $where_conditions['spam_sql'] = 'a.is_spam = 0';
     719                } elseif ( 'spam_only' === $args['spam'] ) {
     720                        $where_conditions['spam_sql'] = 'a.is_spam = 1';
     721                }
     722
     723                // Searching
     724                if ( $args['search_terms'] ) {
     725                        $search_terms_like = '%' . bp_esc_like( $args['search_terms'] ) . '%';
     726                        $where_conditions['search_sql'] = $wpdb->prepare( 'a.content LIKE %s', $search_terms_like );
     727                }
     728
     729                // Hide Hidden Items?
     730                if ( empty( $args['show_hidden'] ) ) {
     731                        $where_conditions['hidden_sql'] = "a.hide_sitewide = 0";
     732                }
     733
     734                // Exclude specified items
     735                if ( ! empty( $args['exclude'] ) ) {
     736                        $exclude = implode( ',', wp_parse_id_list( $args['exclude'] ) );
     737                        $where_conditions['exclude'] = "a.id NOT IN ({$exclude})";
     738                }
     739
     740                // The specific ids to which you want to limit the query
     741                if ( ! empty( $args['in'] ) ) {
     742                        $in = implode( ',', wp_parse_id_list( $args['in'] ) );
     743                        $where_conditions['in'] = "a.id IN ({$in})";
     744                }
     745
     746                if ( ! empty( $meta_query_sql['where'] ) ) {
     747                        $where_conditions[] = $meta_query_sql['where'];
     748                }
     749
     750                // Process date_query into SQL
     751                $date_query_sql = self::get_date_query_sql( $args['date_query'] );
     752
     753                if ( ! empty( $date_query_sql ) ) {
     754                        $where_conditions['date'] = $date_query_sql;
     755                }
     756
     757                // Alter the query based on whether we want to show activity item
     758                // comments in the stream like normal comments or threaded below
     759                // the activity.
     760                if ( false === $args['display_comments'] || 'threaded' === $args['display_comments'] ) {
     761                        $excluded_types[] = 'activity_comment';
     762                }
     763
     764                // Exclude 'last_activity' items unless the 'action' filter has
     765                // been explicitly set
     766                if ( empty( $args['filter']['object'] ) ) {
     767                        $excluded_types[] = 'last_activity';
     768                }
     769
     770                // Build the excluded type sql part
     771                if ( ! empty( $excluded_types ) ) {
     772                        $not_in = "'" . implode( "', '", esc_sql( $excluded_types ) ) . "'";
     773                        $where_conditions['excluded_types'] = "a.type NOT IN ({$not_in})";
     774                }
     775
     776                /**
     777                 * Filters the MySQL WHERE conditions for the Activity items get method.
     778                 *
     779                 * @since BuddyPress (1.9.0)
     780                 *
     781                 * @param array  $where_conditions Current conditions for MySQL WHERE statement.
     782                 * @param array  $args Parsed arguments passed into method.
     783                 * @param string $select_sql Current SELECT MySQL statement at point of execution.
     784                 * @param string $from_sql Current FROM MySQL statement at point of execution.
     785                 * @param string $join_sql Current INNER JOIN MySQL statement at point of execution.
     786                 */
     787                $where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $args, $select_sql, $from_sql, $join_sql, $meta_query_sql );
     788
     789                // Join the where conditions together
     790                return 'WHERE ' . join( ' AND ', $where_conditions );
     791        }
     792
     793        /**