<!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][6948] trunk: Introduces meta_query paramater for bp_has_activities() stack</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/6948">6948</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2013-04-26 17:46:44 +0000 (Fri, 26 Apr 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Introduces meta_query paramater for bp_has_activities() stack
This will allow plugin and theme authors to filter the contents of the activity
stream based on information stored in the activitymeta table, using the
powerful meta_query syntax familiar from the 'meta_query' WP_Query param.
Also adds unit test for 'meta_query' in BP_Activity_Activity::get(), and
integration test for bp_has_activities(). These tests are primarily designed
to ensure that our param gets passed properly down the stack; we rely on WP
to get the meta_query logic right.
See <a href="http://buddypress.trac.wordpress.org/ticket/3521">#3521</a>
Props ericlewis</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityclassesphp">trunk/bp-activity/bp-activity-classes.php</a></li>
<li><a href="#trunkbpactivitybpactivityfunctionsphp">trunk/bp-activity/bp-activity-functions.php</a></li>
<li><a href="#trunkbpactivitybpactivitytemplatephp">trunk/bp-activity/bp-activity-template.php</a></li>
<li><a href="#trunkteststestcasesactivityclassBP_Activity_Activityphp">trunk/tests/testcases/activity/class.BP_Activity_Activity.php</a></li>
<li><a href="#trunkteststestcasesactivitytemplatephp">trunk/tests/testcases/activity/template.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-classes.php (6947 => 6948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-classes.php 2013-04-26 13:49:12 UTC (rev 6947)
+++ trunk/bp-activity/bp-activity-classes.php 2013-04-26 17:46:44 UTC (rev 6948)
</span><span class="lines">@@ -144,6 +144,7 @@
</span><span class="cx"> 'sort' => 'DESC', // ASC or DESC
</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><ins>+ 'meta_query' => false, // Filter by activitymeta
</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">@@ -158,6 +159,8 @@
</span><span class="cx">
</span><span class="cx"> $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
</span><span class="cx">
</span><ins>+ $join_sql = '';
+
</ins><span class="cx"> // Where conditions
</span><span class="cx"> $where_conditions = array();
</span><span class="cx">
</span><span class="lines">@@ -197,6 +200,17 @@
</span><span class="cx"> $where_conditions['in'] = "a.id IN ({$in})";
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ // Process meta_query into SQL
+ $meta_query_sql = self::get_meta_query_sql( $meta_query );
+
+ if ( ! empty( $meta_query_sql['join'] ) ) {
+ $join_sql .= $meta_query_sql['join'];
+ }
+
+ if ( ! empty( $meta_query_sql['where'] ) ) {
+ $where_conditions[] = $meta_query_sql['where'];
+ }
+
</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">@@ -228,12 +242,12 @@
</span><span class="cx"> $per_page = absint( $per_page );
</span><span class="cx">
</span><span class="cx"> $pag_sql = $wpdb->prepare( "LIMIT %d, %d", absint( ( $page - 1 ) * $per_page ), $per_page );
</span><del>- $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
</del><ins>+ $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort} {$pag_sql}", $select_sql, $from_sql, $where_sql, $sort, $pag_sql ) );
</ins><span class="cx"> } else {
</span><del>- $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $select_sql, $from_sql, $where_sql, $sort ) );
</del><ins>+ $activities = $wpdb->get_results( apply_filters( 'bp_activity_get_user_join_filter', "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $select_sql, $from_sql, $where_sql, $sort ) );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $where_sql, $sort );
</del><ins>+ $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $where_sql, $sort );
</ins><span class="cx">
</span><span class="cx"> $total_activities = $wpdb->get_var( $total_activities_sql );
</span><span class="cx">
</span><span class="lines">@@ -285,6 +299,46 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get()
+ *
+ * We use WP_Meta_Query to do the heavy lifting of parsing the
+ * meta_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 'where' clause).
+ *
+ * @since 1.8
+ *
+ * @param array $meta_query An array of meta_query filters. See the
+ * documentation for WP_Meta_Query for details.
+ * @return array $sql_array 'join' and 'where' clauses
+ */
+ public static function get_meta_query_sql( $meta_query = array() ) {
+ global $wpdb;
+
+ $sql_array = array(
+ 'join' => '',
+ 'where' => '',
+ );
+
+ if ( ! empty( $meta_query ) ) {
+ $activity_meta_query = new WP_Meta_Query( $meta_query );
+
+ // WP_Meta_Query expects the table name at
+ // $wpdb->activitymeta
+ $wpdb->activitymeta = buddypress()->activity->table_name_meta;
+
+ $meta_sql = $activity_meta_query->get_sql( 'activity', 'a', 'id' );
+
+ // Strip the leading AND - BP handles it in get()
+ $sql_array['where'] = preg_replace( '/^\sAND/', '', $meta_sql['where'] );
+ $sql_array['join'] = $meta_sql['join'];
+ }
+
+ return $sql_array;
+ }
+
+ /**
</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"> * As of 1.5.x, use BP_Activity_Activity::get() with an 'in' parameter instead.
</span><span class="cx"> *
</span></span></pre></div>
<a id="trunkbpactivitybpactivityfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-functions.php (6947 => 6948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-functions.php 2013-04-26 13:49:12 UTC (rev 6947)
+++ trunk/bp-activity/bp-activity-functions.php 2013-04-26 17:46:44 UTC (rev 6948)
</span><span class="lines">@@ -881,6 +881,7 @@
</span><span class="cx"> 'display_comments' => false, // false for no comments. 'stream' for within stream display, 'threaded' for below each activity item
</span><span class="cx">
</span><span class="cx"> 'search_terms' => false, // Pass search terms as a string
</span><ins>+ 'meta_query' => false, // Filter by activity meta. See WP_Meta_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">@@ -902,7 +903,7 @@
</span><span class="cx"> extract( $r, EXTR_SKIP );
</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) $page && empty( $max ) && empty( $search_terms ) && empty( $filter ) && empty( $exclude ) && empty( $in ) && 'DESC' == $sort && empty( $exclude ) && 'ham_only' == $spam ) {
</del><ins>+ if ( 1 == (int) $page && empty( $max ) && empty( $search_terms ) && empty( $meta_query ) && empty( $filter ) && empty( $exclude ) && empty( $in ) && 'DESC' == $sort && empty( $exclude ) && 'ham_only' == $spam ) {
</ins><span class="cx"> if ( !$activity = wp_cache_get( 'bp_activity_sitewide_front', 'bp' ) ) {
</span><span class="cx"> $args = array(
</span><span class="cx"> 'page' => $page,
</span><span class="lines">@@ -910,6 +911,7 @@
</span><span class="cx"> 'max' => $max,
</span><span class="cx"> 'sort' => $sort,
</span><span class="cx"> 'search_terms' => $search_terms,
</span><ins>+ 'meta_query' => $meta_query,
</ins><span class="cx"> 'filter' => $filter,
</span><span class="cx"> 'display_comments' => $display_comments,
</span><span class="cx"> 'show_hidden' => $show_hidden,
</span><span class="lines">@@ -926,6 +928,7 @@
</span><span class="cx"> 'max' => $max,
</span><span class="cx"> 'sort' => $sort,
</span><span class="cx"> 'search_terms' => $search_terms,
</span><ins>+ 'meta_query' => $meta_query,
</ins><span class="cx"> 'filter' => $filter,
</span><span class="cx"> 'display_comments' => $display_comments,
</span><span class="cx"> 'show_hidden' => $show_hidden,
</span></span></pre></div>
<a id="trunkbpactivitybpactivitytemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-template.php (6947 => 6948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-template.php 2013-04-26 13:49:12 UTC (rev 6947)
+++ trunk/bp-activity/bp-activity-template.php 2013-04-26 17:46:44 UTC (rev 6948)
</span><span class="lines">@@ -150,6 +150,7 @@
</span><span class="cx"> 'in' => false,
</span><span class="cx"> 'filter' => false,
</span><span class="cx"> 'search_terms' => false,
</span><ins>+ 'meta_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">@@ -172,7 +173,7 @@
</span><span class="cx">
</span><span class="cx"> // Fetch all activity items
</span><span class="cx"> else
</span><del>- $this->activities = bp_activity_get( array( 'display_comments' => $display_comments, 'max' => $max, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'sort' => $sort, 'search_terms' => $search_terms, 'filter' => $filter, 'show_hidden' => $show_hidden, 'exclude' => $exclude, 'in' => $in, 'spam' => $spam ) );
</del><ins>+ $this->activities = bp_activity_get( array( 'display_comments' => $display_comments, 'max' => $max, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'sort' => $sort, 'search_terms' => $search_terms, 'meta_query' => $meta_query, 'filter' => $filter, 'show_hidden' => $show_hidden, 'exclude' => $exclude, 'in' => $in, 'spam' => $spam ) );
</ins><span class="cx">
</span><span class="cx"> if ( !$max || $max >= (int) $this->activities['total'] )
</span><span class="cx"> $this->total_activity_count = (int) $this->activities['total'];
</span><span class="lines">@@ -361,6 +362,8 @@
</span><span class="cx"> 'primary_id' => $primary_id, // object ID to filter on e.g. a group_id or forum_id or blog_id etc.
</span><span class="cx"> 'secondary_id' => false, // secondary object ID to filter on e.g. a post_id
</span><span class="cx">
</span><ins>+ 'meta_query' => false, // filter on activity meta. See WP_Meta_Query for format
+
</ins><span class="cx"> // Searching
</span><span class="cx"> 'search_terms' => false // specify terms to search on
</span><span class="cx"> );
</span><span class="lines">@@ -454,6 +457,7 @@
</span><span class="cx"> 'in' => $in,
</span><span class="cx"> 'filter' => $filter,
</span><span class="cx"> 'search_terms' => $search_terms,
</span><ins>+ 'meta_query' => $meta_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="trunkteststestcasesactivityclassBP_Activity_Activityphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/testcases/activity/class.BP_Activity_Activity.php (6947 => 6948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/activity/class.BP_Activity_Activity.php 2013-04-26 13:49:12 UTC (rev 6947)
+++ trunk/tests/testcases/activity/class.BP_Activity_Activity.php 2013-04-26 17:46:44 UTC (rev 6948)
</span><span class="lines">@@ -72,4 +72,21 @@
</span><span class="cx"> ) );
</span><span class="cx"> $this->assertEquals( $activity['activities'][0]->hide_sitewide, 1 );
</span><span class="cx"> }
</span><ins>+
+ public function test_get_meta_query() {
+ $a1 = $this->factory->activity->create();
+ $a2 = $this->factory->activity->create();
+ bp_activity_update_meta( $a1->id, 'foo', 'bar' );
+
+ $activity = BP_Activity_Activity::get( array(
+ 'meta_query' => array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ ),
+ ) );
+ $ids = wp_list_pluck( $activity['activities'], 'id' );
+ $this->assertEquals( $ids, array( $a1->id ) );
+ }
</ins><span class="cx"> }
</span></span></pre></div>
<a id="trunkteststestcasesactivitytemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/testcases/activity/template.php (6947 => 6948)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/activity/template.php 2013-04-26 13:49:12 UTC (rev 6947)
+++ trunk/tests/testcases/activity/template.php 2013-04-26 17:46:44 UTC (rev 6948)
</span><span class="lines">@@ -110,4 +110,26 @@
</span><span class="cx">
</span><span class="cx"> $activities_template = null;
</span><span class="cx"> }
</span><ins>+
+ /**
+ * Integration test for 'meta_query' param
+ */
+ function test_bp_has_activities_with_meta_query() {
+ $a1 = $this->factory->activity->create();
+ $a2 = $this->factory->activity->create();
+ bp_activity_update_meta( $a1->id, 'foo', 'bar' );
+
+ global $activities_template;
+ bp_has_activities( array(
+ 'meta_query' => array(
+ array(
+ 'key' => 'foo',
+ 'value' => 'bar',
+ ),
+ ),
+ ) );
+
+ $ids = wp_list_pluck( $activities_template->activities, 'id' );
+ $this->assertEquals( $ids, array( $a1->id ) );
+ }
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>
</body>
</html>