<!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][6950] trunk: Introduces a 'meta_query' parameter for the bp_has_groups() 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/6950">6950</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2013-04-26 18:50:47 +0000 (Fri, 26 Apr 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Introduces a 'meta_query' parameter for the bp_has_groups() stack

This will allow plugin and theme authors to customize their group directories
based on data stored in the groupmeta table, using a powerful syntax familiar
from WP_Query.

Also adds unit tests for BP_Groups_Group::get() for the meta_query parameter,
as well as an integration test for the entire bp_has_groups() chain of
functions.

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/3521">#3521</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpgroupsbpgroupsclassesphp">trunk/bp-groups/bp-groups-classes.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsfunctionsphp">trunk/bp-groups/bp-groups-functions.php</a></li>
<li><a href="#trunkbpgroupsbpgroupstemplatephp">trunk/bp-groups/bp-groups-template.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>trunk/tests/testcases/groups/</li>
<li><a href="#trunkteststestcasesgroupsclassesphp">trunk/tests/testcases/groups/classes.php</a></li>
<li><a href="#trunkteststestcasesgroupstemplatephp">trunk/tests/testcases/groups/template.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpgroupsbpgroupsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-classes.php (6949 => 6950)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-classes.php    2013-04-26 18:35:15 UTC (rev 6949)
+++ trunk/bp-groups/bp-groups-classes.php       2013-04-26 18:50:47 UTC (rev 6950)
</span><span class="lines">@@ -334,6 +334,7 @@
</span><span class="cx">                  'page'            => null,
</span><span class="cx">                  'user_id'         => 0,
</span><span class="cx">                  'search_terms'    => false,
</span><ins>+                       'meta_query'      => false,
</ins><span class="cx">                   'include'         => false,
</span><span class="cx">                  'populate_extras' => true,
</span><span class="cx">                  'exclude'         => false,
</span><span class="lines">@@ -367,6 +368,17 @@
</span><span class="cx">                  $sql['search'] = " AND ( g.name LIKE '%%{$search_terms}%%' OR g.description LIKE '%%{$search_terms}%%' )";
</span><span class="cx">          }
</span><span class="cx"> 
</span><ins>+               $meta_query_sql = self::get_meta_query_sql( $meta_query );
+
+               if ( ! empty( $meta_query_sql['join'] ) ) {
+                       $sql['from'] .= $meta_query_sql['join'];
+                       $total_sql['select'] .= $meta_query_sql['join_total'];
+               }
+
+               if ( ! empty( $meta_query_sql['where'] ) ) {
+                       $sql['meta'] = $meta_query_sql['where'];
+               }
+
</ins><span class="cx">           if ( !empty( $user_id ) )
</span><span class="cx">                  $sql['user'] = $wpdb->prepare( " AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $user_id );
</span><span class="cx"> 
</span><span class="lines">@@ -465,6 +477,58 @@
</span><span class="cx">          return array( 'groups' => $paged_groups, 'total' => $total_groups );
</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 ) ) {
+                       $groups_meta_query = new WP_Meta_Query( $meta_query );
+
+                       // WP_Meta_Query expects the table name at
+                       // $wpdb->group
+                       $wpdb->groupmeta = buddypress()->groups->table_name_groupmeta;
+
+                       $meta_sql = $groups_meta_query->get_sql( 'group', 'g', 'id' );
+
+                       // BP_Groups_Group::get uses the comma syntax for table
+                       // joins, which means that we have to do some regex to
+                       // convert the INNER JOIN and move the ON clause to a
+                       // WHERE condition
+                       //
+                       // @todo It may be better in the long run to refactor
+                       // the more general query syntax to accord better with
+                       // BP/WP convention
+                       preg_match( '/INNER JOIN (.*) ON/', $meta_sql['join'], $matches_a );
+                       preg_match( '/ON \((.*)\)$/', $meta_sql['join'], $matches_b );
+                       if ( ! empty( $matches_a[1] ) && ! empty( $matches_b[1] ) ) {
+                               $sql_array['join']  = $matches_a[1] . ', ';
+                               $sql_array['where'] = preg_replace( '/^(\sAND\s+[\(\s]+)/', '$1' . $matches_b[1] . ' AND ', $meta_sql['where'] );
+                       }
+               }
+
+               return $sql_array;
+       }
+
+
</ins><span class="cx">   function get_by_most_forum_topics( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) {
</span><span class="cx">          global $wpdb, $bp, $bbdb;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-functions.php (6949 => 6950)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-functions.php  2013-04-26 18:35:15 UTC (rev 6949)
+++ trunk/bp-groups/bp-groups-functions.php     2013-04-26 18:50:47 UTC (rev 6950)
</span><span class="lines">@@ -418,6 +418,7 @@
</span><span class="cx">          'include'         => false,    // Only include these specific groups (group_ids)
</span><span class="cx">          'exclude'         => false,    // Do not include these specific groups (group_ids)
</span><span class="cx">          'search_terms'    => false,    // Limit to groups that match these search terms
</span><ins>+               'meta_query'      => false,    // Filter by groupmeta. See WP_Meta_Query for syntax
</ins><span class="cx">           'show_hidden'     => false,    // Show hidden groups to non-admins
</span><span class="cx">          'per_page'        => 20,       // The number of results to return per page
</span><span class="cx">          'page'            => 1,        // The page to return if limiting per page
</span><span class="lines">@@ -432,6 +433,7 @@
</span><span class="cx">          'include'         => $r['include'],
</span><span class="cx">          'exclude'         => $r['exclude'],
</span><span class="cx">          'search_terms'    => $r['search_terms'],
</span><ins>+               'meta_query'      => $r['meta_query'],
</ins><span class="cx">           'show_hidden'     => $r['show_hidden'],
</span><span class="cx">          'per_page'        => $r['per_page'],
</span><span class="cx">          'page'            => $r['page'],
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-template.php (6949 => 6950)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-template.php   2013-04-26 18:35:15 UTC (rev 6949)
+++ trunk/bp-groups/bp-groups-template.php      2013-04-26 18:50:47 UTC (rev 6950)
</span><span class="lines">@@ -144,6 +144,7 @@
</span><span class="cx">                  'include'         => false,
</span><span class="cx">                  'exclude'         => false,
</span><span class="cx">                  'search_terms'    => '',
</span><ins>+                       'meta_query'      => false,
</ins><span class="cx">                   'populate_extras' => true
</span><span class="cx">          );
</span><span class="cx"> 
</span><span class="lines">@@ -169,6 +170,7 @@
</span><span class="cx">                          'page'            => $this->pag_page,
</span><span class="cx">                          'user_id'         => $user_id,
</span><span class="cx">                          'search_terms'    => $search_terms,
</span><ins>+                               'meta_query'      => $meta_query,
</ins><span class="cx">                           'include'         => $include,
</span><span class="cx">                          'exclude'         => $exclude,
</span><span class="cx">                          'populate_extras' => $populate_extras,
</span><span class="lines">@@ -307,6 +309,7 @@
</span><span class="cx">          'user_id'         => $user_id, // Pass a user ID to limit to groups this user has joined
</span><span class="cx">          'slug'            => $slug,    // Pass a group slug to only return that group
</span><span class="cx">          'search_terms'    => '',       // Pass search terms to return only matching groups
</span><ins>+               'meta_query'      => false,    // Filter by groupmeta. See WP_Meta_Query for format
</ins><span class="cx">           'include'         => false,    // Pass comma separated list or array of group ID's to return only these groups
</span><span class="cx">          'exclude'         => false,    // Pass comma separated list or array of group ID's to exclude these groups
</span><span class="cx"> 
</span><span class="lines">@@ -334,6 +337,7 @@
</span><span class="cx">          'user_id'         => (int) $r['user_id'],
</span><span class="cx">          'slug'            => $r['slug'],
</span><span class="cx">          'search_terms'    => $r['search_terms'],
</span><ins>+               'meta_query'      => $r['meta_query'],
</ins><span class="cx">           'include'         => $r['include'],
</span><span class="cx">          'exclude'         => $r['exclude'],
</span><span class="cx">          'populate_extras' => (bool) $r['populate_extras']
</span></span></pre></div>
<a id="trunkteststestcasesgroupsclassesphp"></a>
<div class="addfile"><h4>Added: trunk/tests/testcases/groups/classes.php (0 => 6950)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/classes.php                         (rev 0)
+++ trunk/tests/testcases/groups/classes.php    2013-04-26 18:50:47 UTC (rev 6950)
</span><span class="lines">@@ -0,0 +1,41 @@
</span><ins>+<?php
+/**
+ * @group groups
+ */
+class BP_Tests_Groups_Classes extends BP_UnitTestCase {
+       public function setUp() {
+               parent::setUp();
+       }
+
+       public function tearDown() {
+               parent::tearDown();
+       }
+
+       public function test_get_meta_query() {
+               $g1 = $this->factory->group->create();
+               $g2 = $this->factory->group->create();
+               groups_update_groupmeta( $g1->id, 'foo', 'bar' );
+
+               $groups = BP_Groups_Group::get( array(
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+               $ids = wp_list_pluck( $groups['groups'], 'id' );
+               $this->assertEquals( $ids, array( $g1->id ) );
+       }
+       public function test_get_empty_meta_query() {
+               $g1 = $this->factory->group->create();
+               $g2 = $this->factory->group->create();
+               groups_update_groupmeta( $g1->id, 'foo', 'bar' );
+
+               $groups = BP_Groups_Group::get( array(
+                       'meta_query' => array(),
+               ) );
+               $ids = wp_list_pluck( $groups['groups'], 'id' );
+               $this->assertEquals( $ids, array( $g1->id, $g2->id, ) );
+       }
+}
</ins></span></pre></div>
<a id="trunkteststestcasesgroupstemplatephp"></a>
<div class="addfile"><h4>Added: trunk/tests/testcases/groups/template.php (0 => 6950)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/template.php                                (rev 0)
+++ trunk/tests/testcases/groups/template.php   2013-04-26 18:50:47 UTC (rev 6950)
</span><span class="lines">@@ -0,0 +1,32 @@
</span><ins>+<?php
+/**
+ * @group groups
+ */
+class BP_Tests_Groups_Template extends BP_UnitTestCase {
+       public function setUp() {
+               parent::setUp();
+       }
+
+       public function tearDown() {
+               parent::tearDown();
+       }
+
+       public function test_bp_has_groups_with_meta_query() {
+               $g1 = $this->factory->group->create();
+               $g2 = $this->factory->group->create();
+               groups_update_groupmeta( $g1->id, 'foo', 'bar' );
+
+               global $groups_template;
+               bp_has_groups( array(
+                       'meta_query' => array(
+                               array(
+                                       'key' => 'foo',
+                                       'value' => 'bar',
+                               ),
+                       ),
+               ) );
+
+               $ids = wp_list_pluck( $groups_template->groups, 'id' );
+               $this->assertEquals( $ids, array( $g1->id, ) );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>