Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/11/2014 03:13:23 PM (12 years ago)
Author:
r-a-y
Message:

Add ability to query activity items by date.

This commit:

  • Introduces the BP_Date_Query class. This class extends WP_Date_Query, which will enable us to easily add support to query various BuddyPress items by date.
  • Adds a new 'date_query' parameter to BP_Activity_Activity::get(). This parameter utilizes the new BP_Date_Query class and is an example of how to use the class in BuddyPress. (Will add support for the Groups component in a later commit.)
  • Adds unit tests.

Due to the dependency on the WP_Date_Query class, this functionality is
only available in WordPress 3.7+.

Fixes #5803.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-classes.php

    r8708 r8787  
    258258     *     @type array $meta_query An array of meta_query conditions.
    259259     *                             See WP_Meta_Query::queries for description.
     260     *     @type array $date_query An array of date_query conditions.
     261     *                             See first parameter of WP_Date_Query::__construct()
     262     *                             for description.
    260263     *     @type array $filter See BP_Activity_Activity::get_filter_sql().
    261264     *     @type string $search_terms Limit results by a search term.
     
    309312            'in'                => false,      // Array of ids to limit query by (IN)
    310313            'meta_query'        => false,      // Filter by activitymeta
     314            'date_query'        => false,      // Filter by date
    311315            'filter'            => false,      // See self::get_filter_sql()
    312316            'search_terms'      => false,      // Terms to search by
     
    377381        }
    378382
     383        // Process date_query into SQL
     384        $date_query_sql = self::get_date_query_sql( $date_query );
     385
     386        if ( ! empty( $date_query_sql ) ) {
     387            $where_conditions['date'] = $date_query_sql;
     388        }
     389
    379390        // Alter the query based on whether we want to show activity item
    380391        // comments in the stream like normal comments or threaded below
     
    683694
    684695        return $sql_array;
     696    }
     697
     698    /**
     699     * Get the SQL for the 'date_query' param in BP_Activity_Activity::get().
     700     *
     701     * We use BP_Date_Query, which extends WP_Date_Query, to do the heavy lifting
     702     * of parsing the date_query array and creating the necessary SQL clauses.
     703     * However, since BP_Activity_Activity::get() builds its SQL differently than
     704     * WP_Query, we have to alter the return value (stripping the leading AND
     705     * keyword from the query).
     706     *
     707     * @since BuddyPress (2.1.0)
     708     *
     709     * @param array $date_query An array of date_query parameters. See the
     710     *        documentation for the first parameter of WP_Date_Query.
     711     * @return string
     712     */
     713    public static function get_date_query_sql( $date_query = array() ) {
     714        $sql = '';
     715
     716        // Date query
     717        if ( ! empty( $date_query ) && is_array( $date_query ) && class_exists( 'BP_Date_Query' ) ) {
     718            $date_query = new BP_Date_Query( $date_query, 'date_recorded' );
     719            $sql = preg_replace( '/^\sAND/', '', $date_query->get_sql() );
     720        }
     721
     722        return $sql;
    685723    }
    686724
Note: See TracChangeset for help on using the changeset viewer.