Skip to:
Content

BuddyPress.org

Changeset 8787


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.

Location:
trunk
Files:
6 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
  • trunk/src/bp-activity/bp-activity-functions.php

    r8783 r8787  
    10201020                'search_terms'      => false,        // Pass search terms as a string
    10211021                'meta_query'        => false,        // Filter by activity meta. See WP_Meta_Query for format
     1022                'date_query'        => false,        // Filter by date. See first parameter of WP_Date_Query for format
    10221023                'show_hidden'       => false,        // Show activity items that are hidden site-wide?
    10231024                'exclude'           => false,        // Comma-separated list of activity IDs to exclude
     
    10411042
    10421043        // Attempt to return a cached copy of the first page of sitewide activity.
    1043         if ( ( 1 === (int) $r['page'] ) && empty( $r['max'] ) && empty( $r['search_terms'] ) && empty( $r['meta_query'] ) && empty( $r['filter'] ) && empty( $r['exclude'] ) && empty( $r['in'] ) && ( 'DESC' === $r['sort'] ) && empty( $r['exclude'] ) && ( 'ham_only' === $r['spam'] ) ) {
     1044        if ( ( 1 === (int) $r['page'] ) && empty( $r['max'] ) && empty( $r['search_terms'] ) && empty( $r['meta_query'] ) && empty( $r['date_query'] ) && empty( $r['filter'] ) && empty( $r['exclude'] ) && empty( $r['in'] ) && ( 'DESC' === $r['sort'] ) && empty( $r['exclude'] ) && ( 'ham_only' === $r['spam'] ) ) {
    10441045
    10451046                $activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' );
     
    10531054                                'search_terms'      => $r['search_terms'],
    10541055                                'meta_query'        => $r['meta_query'],
     1056                                'date_query'        => $r['date_query'],
    10551057                                'filter'            => $r['filter'],
    10561058                                'display_comments'  => $r['display_comments'],
     
    10721074                        'search_terms'     => $r['search_terms'],
    10731075                        'meta_query'       => $r['meta_query'],
     1076                        'date_query'       => $r['date_query'],
    10741077                        'filter'           => $r['filter'],
    10751078                        'display_comments' => $r['display_comments'],
  • trunk/src/bp-activity/bp-activity-template.php

    r8784 r8787  
    179179                        'search_terms'      => false,
    180180                        'meta_query'        => false,
     181                        'date_query'        => false,
    181182                        'display_comments'  => 'threaded',
    182183                        'show_hidden'       => false,
     
    221222                                'search_terms'      => $search_terms,
    222223                                'meta_query'        => $meta_query,
     224                                'date_query'        => $date_query,
    223225                                'filter'            => $filter,
    224226                                'show_hidden'       => $show_hidden,
     
    568570
    569571                'meta_query'        => false,        // filter on activity meta. See WP_Meta_Query for format
     572                'date_query'        => false,        // filter by date. See first parameter of WP_Date_Query for format
    570573
    571574                // Searching
     
    681684                'search_terms'      => $search_terms,
    682685                'meta_query'        => $meta_query,
     686                'date_query'        => $date_query,
    683687                'display_comments'  => $display_comments,
    684688                'show_hidden'       => $show_hidden,
  • trunk/src/bp-core/bp-core-classes.php

    r8753 r8787  
    15081508}
    15091509
     1510if ( class_exists( 'WP_Date_Query' ) ) :
     1511/**
     1512 * BuddyPress date query class.
     1513 *
     1514 * Extends the {@link WP_Date_Query} class for use with BuddyPress.
     1515 *
     1516 * @since BuddyPress (2.1.0)
     1517 *
     1518 * @param array $date_query {
     1519 *     Date query arguments.  See first parameter of {@link WP_Date_Query::__construct()}.
     1520 * }
     1521 * @param string $column The DB column to query against.
     1522 */
     1523class BP_Date_Query extends WP_Date_Query {
     1524        /**
     1525         * The column to query against. Can be changed via the query arguments.
     1526         *
     1527         * @var string
     1528         */
     1529        public $column;
     1530
     1531        /**
     1532         * Constructor.
     1533         *
     1534         * @see WP_Date_Query::__construct()
     1535         */
     1536        public function __construct( $date_query, $column = '' ) {
     1537                if ( ! empty( $column ) ) {
     1538                        $this->column = $column;
     1539                        add_filter( 'date_query_valid_columns', array( $this, 'register_date_column' ) );
     1540                }
     1541
     1542                parent::__construct( $date_query, $column );
     1543        }
     1544
     1545        /**
     1546         * Destructor.
     1547         */
     1548        public function __destruct() {
     1549                remove_filter( 'date_query_valid_columns', array( $this, 'register_date_column' ) );   
     1550        }
     1551
     1552        /**
     1553         * Registers our date column with WP Date Query to pass validation.
     1554         *
     1555         * @param array $retval Current DB columns
     1556         * @return array
     1557         */
     1558        public function register_date_column( $retval = array() ) {
     1559                $retval[] = $this->column;
     1560                return $retval;
     1561        }
     1562}
     1563endif;
    15101564
    15111565/**
  • trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php

    r8499 r8787  
    146146        /**
    147147         * @group get
     148         * @group date_query
     149         */
     150        public function test_get_with_date_query() {
     151                if ( ! class_exists( 'WP_Date_Query' ) ) {
     152                        return;
     153                }
     154
     155                $a1 = $this->factory->activity->create();
     156                $a2 = $this->factory->activity->create( array(
     157                        'recorded_time' => '2001-01-01 12:00'
     158                ) );
     159                $a3 = $this->factory->activity->create( array(
     160                        'recorded_time' => '2005-01-01 12:00'
     161                ) );
     162
     163                // 'date_query' before test
     164                $query = BP_Activity_Activity::get( array(
     165                        'date_query' => array( array(
     166                                'before' => array(
     167                                        'year'  => 2004,
     168                                        'month' => 1,
     169                                        'day'   => 1,
     170                                ),
     171                        ) )
     172                ) );
     173                $this->assertEquals( array( $a2 ), wp_list_pluck( $query['activities'], 'id' ) );
     174
     175                // 'date_query' range test
     176                $query = BP_Activity_Activity::get( array(
     177                        'date_query' => array( array(
     178                                'after'  => 'January 2nd, 2001',
     179                                'before' => array(
     180                                        'year'  => 2013,
     181                                        'month' => 1,
     182                                        'day'   => 1,
     183                                ),
     184                                'inclusive' => true,
     185                        ) )
     186                ) );
     187                $this->assertEquals( array( $a3 ), wp_list_pluck( $query['activities'], 'id' ) );
     188
     189                // 'date_query' after and relative test
     190                $query = BP_Activity_Activity::get( array(
     191                        'date_query' => array( array(
     192                                'after' => '1 day ago'
     193                        ) )
     194                ) );
     195                $this->assertEquals( array( $a1 ), wp_list_pluck( $query['activities'], 'id' ) );
     196        }
     197
     198        /**
     199         * @group get
    148200         */
    149201        public function test_get_with_search_terms() {
  • trunk/tests/phpunit/testcases/activity/template.php

    r8747 r8787  
    513513                }
    514514        }
     515
     516        /**
     517         * Integration test for 'date_query' param
     518         *
     519         * @group date_query
     520         */
     521        function test_bp_has_activities_with_date_query() {
     522                if ( ! class_exists( 'WP_Date_Query' ) ) {
     523                        return;
     524                }
     525
     526                $a1 = $this->factory->activity->create();
     527                $a2 = $this->factory->activity->create( array(
     528                        'recorded_time' => '2001-01-01 12:00'
     529                ) );
     530                $a3 = $this->factory->activity->create( array(
     531                        'recorded_time' => '2005-01-01 12:00'
     532                ) );
     533
     534                global $activities_template;
     535                bp_has_activities( array(
     536                        'date_query' => array( array(
     537                                'after' => '1 day ago'
     538                        ) )
     539                ) );
     540
     541                $ids = wp_list_pluck( $activities_template->activities, 'id' );
     542                $this->assertEquals( $ids, array( $a1 ) );
     543        }
    515544}
Note: See TracChangeset for help on using the changeset viewer.