<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][8787] trunk: Add ability to query activity items by date.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/8787">8787</a></dd>
<dt>Author</dt> <dd>r-a-y</dd>
<dt>Date</dt> <dd>2014-08-11 15:13:23 +0000 (Mon, 11 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/5803">#5803</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpactivitybpactivityclassesphp">trunk/src/bp-activity/bp-activity-classes.php</a></li>
<li><a href="#trunksrcbpactivitybpactivityfunctionsphp">trunk/src/bp-activity/bp-activity-functions.php</a></li>
<li><a href="#trunksrcbpactivitybpactivitytemplatephp">trunk/src/bp-activity/bp-activity-template.php</a></li>
<li><a href="#trunksrcbpcorebpcoreclassesphp">trunk/src/bp-core/bp-core-classes.php</a></li>
<li><a href="#trunktestsphpunittestcasesactivityclassBP_Activity_Activityphp">trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php</a></li>
<li><a href="#trunktestsphpunittestcasesactivitytemplatephp">trunk/tests/phpunit/testcases/activity/template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpactivitybpactivityclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-activity/bp-activity-classes.php (8786 => 8787)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-activity/bp-activity-classes.php    2014-08-10 20:34:54 UTC (rev 8786)
+++ trunk/src/bp-activity/bp-activity-classes.php       2014-08-11 15:13:23 UTC (rev 8787)
</span><span class="lines">@@ -257,6 +257,9 @@
</span><span class="cx">   *                     Default: false.
</span><span class="cx">   *     @type array $meta_query An array of meta_query conditions.
</span><span class="cx">   *                             See WP_Meta_Query::queries for description.
</span><ins>+        *     @type array $date_query An array of date_query conditions.
+        *                             See first parameter of WP_Date_Query::__construct()
+        *                             for description.
</ins><span class="cx">    *     @type array $filter See BP_Activity_Activity::get_filter_sql().
</span><span class="cx">   *     @type string $search_terms Limit results by a search term.
</span><span class="cx">   *                                Default: false.
</span><span class="lines">@@ -308,6 +311,7 @@
</span><span class="cx">                  'exclude'           => false,      // Array of ids to exclude
</span><span class="cx">                  'in'                => false,      // Array of ids to limit query by (IN)
</span><span class="cx">                  'meta_query'        => false,      // Filter by activitymeta
</span><ins>+                       'date_query'        => false,      // Filter by date
</ins><span class="cx">                   'filter'            => false,      // See self::get_filter_sql()
</span><span class="cx">                  'search_terms'      => false,      // Terms to search by
</span><span class="cx">                  'display_comments'  => false,      // Whether to include activity comments
</span><span class="lines">@@ -376,6 +380,13 @@
</span><span class="cx">                  $where_conditions[] = $meta_query_sql['where'];
</span><span class="cx">          }
</span><span class="cx"> 
</span><ins>+               // Process date_query into SQL
+               $date_query_sql = self::get_date_query_sql( $date_query );
+
+               if ( ! empty( $date_query_sql ) ) {
+                       $where_conditions['date'] = $date_query_sql;
+               }
+
</ins><span class="cx">           // Alter the query based on whether we want to show activity item
</span><span class="cx">          // comments in the stream like normal comments or threaded below
</span><span class="cx">          // the activity.
</span><span class="lines">@@ -685,6 +696,33 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Get the SQL for the 'date_query' param in BP_Activity_Activity::get().
+        *
+        * We use BP_Date_Query, which extends WP_Date_Query, to do the heavy lifting
+        * of parsing the date_query array and creating the necessary SQL clauses.
+        * However, since BP_Activity_Activity::get() builds its SQL differently than
+        * WP_Query, we have to alter the return value (stripping the leading AND
+        * keyword from the query).
+        *
+        * @since BuddyPress (2.1.0)
+        *
+        * @param array $date_query An array of date_query parameters. See the
+        *        documentation for the first parameter of WP_Date_Query.
+        * @return string
+        */
+       public static function get_date_query_sql( $date_query = array() ) {
+               $sql = '';
+
+               // Date query
+               if ( ! empty( $date_query ) && is_array( $date_query ) && class_exists( 'BP_Date_Query' ) ) {
+                       $date_query = new BP_Date_Query( $date_query, 'date_recorded' );
+                       $sql = preg_replace( '/^\sAND/', '', $date_query->get_sql() );
+               }
+
+               return $sql;
+       }
+
+       /**
</ins><span class="cx">    * In BuddyPress 1.2.x, this was used to retrieve specific activity stream items (for example, on an activity's permalink page).
</span><span class="cx">   *
</span><span class="cx">   * As of 1.5.x, use BP_Activity_Activity::get() with an 'in' parameter instead.
</span></span></pre></div>
<a id="trunksrcbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-activity/bp-activity-functions.php (8786 => 8787)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-activity/bp-activity-functions.php  2014-08-10 20:34:54 UTC (rev 8786)
+++ trunk/src/bp-activity/bp-activity-functions.php     2014-08-11 15:13:23 UTC (rev 8787)
</span><span class="lines">@@ -1019,6 +1019,7 @@
</span><span class="cx"> 
</span><span class="cx">          'search_terms'      => false,        // Pass search terms as a string
</span><span class="cx">          'meta_query'        => false,        // Filter by activity meta. See WP_Meta_Query for format
</span><ins>+               'date_query'        => false,        // Filter by date. See first parameter of WP_Date_Query for format
</ins><span class="cx">           'show_hidden'       => false,        // Show activity items that are hidden site-wide?
</span><span class="cx">          'exclude'           => false,        // Comma-separated list of activity IDs to exclude
</span><span class="cx">          'in'                => false,        // Comma-separated list or array of activity IDs to which you want to limit the query
</span><span class="lines">@@ -1040,7 +1041,7 @@
</span><span class="cx">  ) );
</span><span class="cx"> 
</span><span class="cx">  // Attempt to return a cached copy of the first page of sitewide activity.
</span><del>-       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'] ) ) {
</del><ins>+        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'] ) ) {
</ins><span class="cx"> 
</span><span class="cx">          $activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' );
</span><span class="cx">          if ( false === $activity ) {
</span><span class="lines">@@ -1052,6 +1053,7 @@
</span><span class="cx">                          'sort'              => $r['sort'],
</span><span class="cx">                          'search_terms'      => $r['search_terms'],
</span><span class="cx">                          'meta_query'        => $r['meta_query'],
</span><ins>+                               'date_query'        => $r['date_query'],
</ins><span class="cx">                           'filter'            => $r['filter'],
</span><span class="cx">                          'display_comments'  => $r['display_comments'],
</span><span class="cx">                          'show_hidden'       => $r['show_hidden'],
</span><span class="lines">@@ -1071,6 +1073,7 @@
</span><span class="cx">                  'sort'             => $r['sort'],
</span><span class="cx">                  'search_terms'     => $r['search_terms'],
</span><span class="cx">                  'meta_query'       => $r['meta_query'],
</span><ins>+                       'date_query'       => $r['date_query'],
</ins><span class="cx">                   'filter'           => $r['filter'],
</span><span class="cx">                  'display_comments' => $r['display_comments'],
</span><span class="cx">                  'show_hidden'      => $r['show_hidden'],
</span></span></pre></div>
<a id="trunksrcbpactivitybpactivitytemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-activity/bp-activity-template.php (8786 => 8787)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-activity/bp-activity-template.php   2014-08-10 20:34:54 UTC (rev 8786)
+++ trunk/src/bp-activity/bp-activity-template.php      2014-08-11 15:13:23 UTC (rev 8787)
</span><span class="lines">@@ -178,6 +178,7 @@
</span><span class="cx">                  'filter'            => false,
</span><span class="cx">                  'search_terms'      => false,
</span><span class="cx">                  'meta_query'        => false,
</span><ins>+                       'date_query'        => false,
</ins><span class="cx">                   'display_comments'  => 'threaded',
</span><span class="cx">                  'show_hidden'       => false,
</span><span class="cx">                  'spam'              => 'ham_only',
</span><span class="lines">@@ -220,6 +221,7 @@
</span><span class="cx">                          'sort'              => $sort,
</span><span class="cx">                          'search_terms'      => $search_terms,
</span><span class="cx">                          'meta_query'        => $meta_query,
</span><ins>+                               'date_query'        => $date_query,
</ins><span class="cx">                           'filter'            => $filter,
</span><span class="cx">                          'show_hidden'       => $show_hidden,
</span><span class="cx">                          'exclude'           => $exclude,
</span><span class="lines">@@ -567,6 +569,7 @@
</span><span class="cx">          'since'             => false,        // return only items recorded since this Y-m-d H:i:s date
</span><span class="cx"> 
</span><span class="cx">          'meta_query'        => false,        // filter on activity meta. See WP_Meta_Query for format
</span><ins>+               'date_query'        => false,        // filter by date. See first parameter of WP_Date_Query for format
</ins><span class="cx"> 
</span><span class="cx">          // Searching
</span><span class="cx">          'search_terms'      => false,        // specify terms to search on
</span><span class="lines">@@ -680,6 +683,7 @@
</span><span class="cx">          'filter'            => $filter,
</span><span class="cx">          'search_terms'      => $search_terms,
</span><span class="cx">          'meta_query'        => $meta_query,
</span><ins>+               'date_query'        => $date_query,
</ins><span class="cx">           'display_comments'  => $display_comments,
</span><span class="cx">          'show_hidden'       => $show_hidden,
</span><span class="cx">          'spam'              => $spam,
</span></span></pre></div>
<a id="trunksrcbpcorebpcoreclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-core/bp-core-classes.php (8786 => 8787)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-core/bp-core-classes.php    2014-08-10 20:34:54 UTC (rev 8786)
+++ trunk/src/bp-core/bp-core-classes.php       2014-08-11 15:13:23 UTC (rev 8787)
</span><span class="lines">@@ -1507,7 +1507,61 @@
</span><span class="cx">  }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+if ( class_exists( 'WP_Date_Query' ) ) :
+/**
+ * BuddyPress date query class.
+ *
+ * Extends the {@link WP_Date_Query} class for use with BuddyPress.
+ *
+ * @since BuddyPress (2.1.0)
+ *
+ * @param array $date_query {
+ *     Date query arguments.  See first parameter of {@link WP_Date_Query::__construct()}.
+ * }
+ * @param string $column The DB column to query against.
+ */
+class BP_Date_Query extends WP_Date_Query {
+       /**
+        * The column to query against. Can be changed via the query arguments.
+        *
+        * @var string
+        */
+       public $column;
</ins><span class="cx"> 
</span><ins>+       /**
+        * Constructor.
+        *
+        * @see WP_Date_Query::__construct()
+        */
+       public function __construct( $date_query, $column = '' ) {
+               if ( ! empty( $column ) ) {
+                       $this->column = $column;
+                       add_filter( 'date_query_valid_columns', array( $this, 'register_date_column' ) );
+               }
+
+               parent::__construct( $date_query, $column );
+       }
+
+       /**
+        * Destructor.
+        */
+       public function __destruct() {
+               remove_filter( 'date_query_valid_columns', array( $this, 'register_date_column' ) );    
+       }
+
+       /**
+        * Registers our date column with WP Date Query to pass validation.
+        *
+        * @param array $retval Current DB columns
+        * @return array
+        */
+       public function register_date_column( $retval = array() ) {
+               $retval[] = $this->column;
+               return $retval;
+       }
+}
+endif;
+
</ins><span class="cx"> /**
</span><span class="cx">  * BP_Core_Notification is deprecated.
</span><span class="cx">  *
</span></span></pre></div>
<a id="trunktestsphpunittestcasesactivityclassBP_Activity_Activityphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php (8786 => 8787)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php    2014-08-10 20:34:54 UTC (rev 8786)
+++ trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php       2014-08-11 15:13:23 UTC (rev 8787)
</span><span class="lines">@@ -145,7 +145,59 @@
</span><span class="cx"> 
</span><span class="cx">  /**
</span><span class="cx">   * @group get
</span><ins>+        * @group date_query
</ins><span class="cx">    */
</span><ins>+       public function test_get_with_date_query() {
+               if ( ! class_exists( 'WP_Date_Query' ) ) {
+                       return;
+               }
+
+               $a1 = $this->factory->activity->create();
+               $a2 = $this->factory->activity->create( array(
+                       'recorded_time' => '2001-01-01 12:00'
+               ) );
+               $a3 = $this->factory->activity->create( array(
+                       'recorded_time' => '2005-01-01 12:00'
+               ) );
+
+               // 'date_query' before test
+               $query = BP_Activity_Activity::get( array(
+                       'date_query' => array( array(
+                               'before' => array(
+                                       'year'  => 2004,
+                                       'month' => 1,
+                                       'day'   => 1,
+                               ),
+                       ) )
+               ) );
+               $this->assertEquals( array( $a2 ), wp_list_pluck( $query['activities'], 'id' ) );
+
+               // 'date_query' range test
+               $query = BP_Activity_Activity::get( array(
+                       'date_query' => array( array(
+                               'after'  => 'January 2nd, 2001',
+                               'before' => array(
+                                       'year'  => 2013,
+                                       'month' => 1,
+                                       'day'   => 1,
+                               ),
+                               'inclusive' => true,
+                       ) )
+               ) );
+               $this->assertEquals( array( $a3 ), wp_list_pluck( $query['activities'], 'id' ) );
+
+               // 'date_query' after and relative test
+               $query = BP_Activity_Activity::get( array(
+                       'date_query' => array( array(
+                               'after' => '1 day ago'
+                       ) )
+               ) );
+               $this->assertEquals( array( $a1 ), wp_list_pluck( $query['activities'], 'id' ) );
+       }
+
+       /**
+        * @group get
+        */
</ins><span class="cx">   public function test_get_with_search_terms() {
</span><span class="cx">          $a1 = $this->factory->activity->create( array(
</span><span class="cx">                  'content' => 'Boone is a cool guy',
</span></span></pre></div>
<a id="trunktestsphpunittestcasesactivitytemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/testcases/activity/template.php (8786 => 8787)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/testcases/activity/template.php      2014-08-10 20:34:54 UTC (rev 8786)
+++ trunk/tests/phpunit/testcases/activity/template.php 2014-08-11 15:13:23 UTC (rev 8787)
</span><span class="lines">@@ -512,4 +512,33 @@
</span><span class="cx">                  $activities_template = null;
</span><span class="cx">          }
</span><span class="cx">  }
</span><ins>+
+       /**
+        * Integration test for 'date_query' param
+        *
+        * @group date_query
+        */
+       function test_bp_has_activities_with_date_query() {
+               if ( ! class_exists( 'WP_Date_Query' ) ) {
+                       return;
+               }
+
+               $a1 = $this->factory->activity->create();
+               $a2 = $this->factory->activity->create( array(
+                       'recorded_time' => '2001-01-01 12:00'
+               ) );
+               $a3 = $this->factory->activity->create( array(
+                       'recorded_time' => '2005-01-01 12:00'
+               ) );
+
+               global $activities_template;
+               bp_has_activities( array(
+                       'date_query' => array( array(
+                               'after' => '1 day ago'
+                       ) )
+               ) );
+
+               $ids = wp_list_pluck( $activities_template->activities, 'id' );
+               $this->assertEquals( $ids, array( $a1 ) );
+       }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>

</body>
</html>