Changeset 8787
- Timestamp:
- 08/11/2014 03:13:23 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-activity/bp-activity-classes.php
r8708 r8787 258 258 * @type array $meta_query An array of meta_query conditions. 259 259 * 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. 260 263 * @type array $filter See BP_Activity_Activity::get_filter_sql(). 261 264 * @type string $search_terms Limit results by a search term. … … 309 312 'in' => false, // Array of ids to limit query by (IN) 310 313 'meta_query' => false, // Filter by activitymeta 314 'date_query' => false, // Filter by date 311 315 'filter' => false, // See self::get_filter_sql() 312 316 'search_terms' => false, // Terms to search by … … 377 381 } 378 382 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 379 390 // Alter the query based on whether we want to show activity item 380 391 // comments in the stream like normal comments or threaded below … … 683 694 684 695 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; 685 723 } 686 724 -
trunk/src/bp-activity/bp-activity-functions.php
r8783 r8787 1020 1020 'search_terms' => false, // Pass search terms as a string 1021 1021 '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 1022 1023 'show_hidden' => false, // Show activity items that are hidden site-wide? 1023 1024 'exclude' => false, // Comma-separated list of activity IDs to exclude … … 1041 1042 1042 1043 // 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'] ) ) { 1044 1045 1045 1046 $activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ); … … 1053 1054 'search_terms' => $r['search_terms'], 1054 1055 'meta_query' => $r['meta_query'], 1056 'date_query' => $r['date_query'], 1055 1057 'filter' => $r['filter'], 1056 1058 'display_comments' => $r['display_comments'], … … 1072 1074 'search_terms' => $r['search_terms'], 1073 1075 'meta_query' => $r['meta_query'], 1076 'date_query' => $r['date_query'], 1074 1077 'filter' => $r['filter'], 1075 1078 'display_comments' => $r['display_comments'], -
trunk/src/bp-activity/bp-activity-template.php
r8784 r8787 179 179 'search_terms' => false, 180 180 'meta_query' => false, 181 'date_query' => false, 181 182 'display_comments' => 'threaded', 182 183 'show_hidden' => false, … … 221 222 'search_terms' => $search_terms, 222 223 'meta_query' => $meta_query, 224 'date_query' => $date_query, 223 225 'filter' => $filter, 224 226 'show_hidden' => $show_hidden, … … 568 570 569 571 '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 570 573 571 574 // Searching … … 681 684 'search_terms' => $search_terms, 682 685 'meta_query' => $meta_query, 686 'date_query' => $date_query, 683 687 'display_comments' => $display_comments, 684 688 'show_hidden' => $show_hidden, -
trunk/src/bp-core/bp-core-classes.php
r8753 r8787 1508 1508 } 1509 1509 1510 if ( 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 */ 1523 class 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 } 1563 endif; 1510 1564 1511 1565 /** -
trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php
r8499 r8787 146 146 /** 147 147 * @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 148 200 */ 149 201 public function test_get_with_search_terms() { -
trunk/tests/phpunit/testcases/activity/template.php
r8747 r8787 513 513 } 514 514 } 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 } 515 544 }
Note: See TracChangeset
for help on using the changeset viewer.