<!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][7918] trunk: Pre-fetch blogmeta when in blog loops</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/7918">7918</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-02-18 12:50:11 +0000 (Tue, 18 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Pre-fetch blogmeta when in blog loops

This reduces overhead when querying for blog meta in the context of a
bp_has_blogs() template loop. Mirrors similar functionality in the activity,
groups, and xprofile components.

Also introduces an update_meta_cache parameter to the bp_has_blogs() stack, so
that plugin/theme authors can disable this pre-fetching.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpblogsbpblogscachephp">trunk/bp-blogs/bp-blogs-cache.php</a></li>
<li><a href="#trunkbpblogsbpblogsclassesphp">trunk/bp-blogs/bp-blogs-classes.php</a></li>
<li><a href="#trunkbpblogsbpblogsfunctionsphp">trunk/bp-blogs/bp-blogs-functions.php</a></li>
<li><a href="#trunkbpblogsbpblogstemplatephp">trunk/bp-blogs/bp-blogs-template.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkteststestcasesblogscachephp">trunk/tests/testcases/blogs/cache.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpblogsbpblogscachephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-cache.php (7917 => 7918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-cache.php        2014-02-17 20:35:14 UTC (rev 7917)
+++ trunk/bp-blogs/bp-blogs-cache.php   2014-02-18 12:50:11 UTC (rev 7918)
</span><span class="lines">@@ -14,6 +14,27 @@
</span><span class="cx"> if ( !defined( 'ABSPATH' ) ) exit;
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Slurp up blogmeta for a specified set of blogs.
+ *
+ * It grabs all blogmeta associated with all of the blogs passed
+ * in $blog_ids and adds it to the WP cache. This improves efficiency when
+ * using querying blogmeta inline.
+ *
+ * @param int|str|array $blog_ids Accepts a single blog ID, or a comma-
+ *        separated list or array of blog IDs.
+ */
+function bp_blogs_update_meta_cache( $blog_ids = false ) {
+       $cache_args = array(
+               'object_ids'       => $blog_ids,
+               'object_type'      => buddypress()->blogs->id,
+               'object_column'    => 'blog_id',
+               'cache_group'      => 'blog_meta',
+               'meta_table'       => buddypress()->blogs->table_name_blogmeta,
+       );
+
+       bp_update_meta_cache( $cache_args );
+}
+/**
</ins><span class="cx">  * Clear the blog object cache.
</span><span class="cx">  *
</span><span class="cx">  * @since BuddyPress (1.0.0)
</span></span></pre></div>
<a id="trunkbpblogsbpblogsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-classes.php (7917 => 7918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-classes.php      2014-02-17 20:35:14 UTC (rev 7917)
+++ trunk/bp-blogs/bp-blogs-classes.php 2014-02-18 12:50:11 UTC (rev 7918)
</span><span class="lines">@@ -114,11 +114,13 @@
</span><span class="cx">   *        retrieved. Default: 0.
</span><span class="cx">   * @param string|bool $search_terms Optional. Search by text stored in
</span><span class="cx">   *        blogmeta (such as the blog name). Default: false.
</span><ins>+        * @param bool $update_meta_cache Whether to pre-fetch metadata for
+        *        blogs. Default: true.
</ins><span class="cx">    * @return array Multidimensional results array, structured as follows:
</span><span class="cx">   *           'blogs' - Array of located blog objects
</span><span class="cx">   *           'total' - A count of the total blogs matching the filter params
</span><span class="cx">   */
</span><del>-       public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false ) {
</del><ins>+        public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false, $update_meta_cache = true ) {
</ins><span class="cx">           global $bp, $wpdb;
</span><span class="cx"> 
</span><span class="cx">          if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) )
</span><span class="lines">@@ -161,6 +163,10 @@
</span><span class="cx"> 
</span><span class="cx">          $paged_blogs = BP_Blogs_Blog::get_blog_extras( $paged_blogs, $blog_ids, $type );
</span><span class="cx"> 
</span><ins>+               if ( $update_meta_cache ) {
+                       bp_blogs_update_meta_cache( $blog_ids );
+               }
+
</ins><span class="cx">           return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );
</span><span class="cx">  }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkbpblogsbpblogsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-functions.php (7917 => 7918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-functions.php    2014-02-17 20:35:14 UTC (rev 7917)
+++ trunk/bp-blogs/bp-blogs-functions.php       2014-02-18 12:50:11 UTC (rev 7918)
</span><span class="lines">@@ -37,6 +37,7 @@
</span><span class="cx">  *     @type string|bool $search_terms Default: false.
</span><span class="cx">  *     @type int $per_page Default: 20.
</span><span class="cx">  *     @type int $page Default: 1.
</span><ins>+ *     @type bool $update_meta_cache Whether to pre-fetch blogmeta. Default: true.
</ins><span class="cx">  * }
</span><span class="cx">  * @return array See {@link BP_Blogs_Blog::get()}.
</span><span class="cx">  */
</span><span class="lines">@@ -48,12 +49,13 @@
</span><span class="cx">          'search_terms' => false,    // Limit to blogs that match these search terms
</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><ins>+               'update_meta_cache' => true,
</ins><span class="cx">   );
</span><span class="cx"> 
</span><span class="cx">  $params = wp_parse_args( $args, $defaults );
</span><span class="cx">  extract( $params, EXTR_SKIP );
</span><span class="cx"> 
</span><del>-       return apply_filters( 'bp_blogs_get_blogs', BP_Blogs_Blog::get( $type, $per_page, $page, $user_id, $search_terms ), $params );
</del><ins>+        return apply_filters( 'bp_blogs_get_blogs', BP_Blogs_Blog::get( $type, $per_page, $page, $user_id, $search_terms, $update_meta_cache ), $params );
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunkbpblogsbpblogstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-template.php (7917 => 7918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-template.php     2014-02-17 20:35:14 UTC (rev 7917)
+++ trunk/bp-blogs/bp-blogs-template.php        2014-02-18 12:50:11 UTC (rev 7918)
</span><span class="lines">@@ -169,8 +169,10 @@
</span><span class="cx">   * @param string $search_terms See {@link BP_Blogs_Blog::get()}.
</span><span class="cx">   * @param string $page_arg The string used as a query parameter in
</span><span class="cx">   *        pagination links. Default: 'bpage'.
</span><ins>+        * @param bool $update_meta_cache Whether to pre-fetch metadata for
+        *        queried blogs.
</ins><span class="cx">    */
</span><del>-       function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage' ) {
</del><ins>+        function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true ) {
</ins><span class="cx"> 
</span><span class="cx">          $this->pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;
</span><span class="cx">          $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;
</span><span class="lines">@@ -178,7 +180,7 @@
</span><span class="cx">          if ( isset( $_REQUEST['letter'] ) && '' != $_REQUEST['letter'] )
</span><span class="cx">                  $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page );
</span><span class="cx">          else
</span><del>-                       $this->blogs = bp_blogs_get_blogs( array( 'type' => $type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'search_terms' => $search_terms ) );
</del><ins>+                        $this->blogs = bp_blogs_get_blogs( array( 'type' => $type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'search_terms' => $search_terms, 'update_meta_cache' => $update_meta_cache, ) );
</ins><span class="cx"> 
</span><span class="cx">          if ( !$max || $max >= (int) $this->blogs['total'] )
</span><span class="cx">                  $this->total_blog_count = (int) $this->blogs['total'];
</span><span class="lines">@@ -359,7 +361,8 @@
</span><span class="cx">          'page_arg'     => 'bpage',        // See https://buddypress.trac.wordpress.org/ticket/3679
</span><span class="cx"> 
</span><span class="cx">          'user_id'      => $user_id,       // Pass a user_id to limit to only blogs this user has higher than subscriber access to
</span><del>-               'search_terms' => $search_terms   // Pass search terms to filter on the blog title or description.
</del><ins>+                'search_terms' => $search_terms,  // Pass search terms to filter on the blog title or description.
+               'update_meta_cache' => true,
</ins><span class="cx">   );
</span><span class="cx"> 
</span><span class="cx">  $r = wp_parse_args( $args, $defaults );
</span><span class="lines">@@ -378,7 +381,7 @@
</span><span class="cx">          }
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       $blogs_template = new BP_Blogs_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg );
</del><ins>+        $blogs_template = new BP_Blogs_Template( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg, $update_meta_cache );
</ins><span class="cx">   return apply_filters( 'bp_has_blogs', $blogs_template->has_blogs(), $blogs_template );
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkteststestcasesblogscachephp"></a>
<div class="addfile"><h4>Added: trunk/tests/testcases/blogs/cache.php (0 => 7918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/blogs/cache.php                            (rev 0)
+++ trunk/tests/testcases/blogs/cache.php       2014-02-18 12:50:11 UTC (rev 7918)
</span><span class="lines">@@ -0,0 +1,216 @@
</span><ins>+<?php
+
+/**
+ * @group blogs
+ * @group cache
+ */
+class BP_Tests_Blogs_Cache extends BP_UnitTestCase {
+       /**
+        * @group bp_blogs_update_meta_cache
+        */
+       public function test_bp_blogs_update_meta_cache() {
+               if ( ! is_multisite() ) {
+                       return;
+               }
+
+               $b1 = $this->factory->blog->create();
+               $b2 = $this->factory->blog->create();
+
+               bp_blogs_add_blogmeta( $b1, 'foo', 'bar' );
+               bp_blogs_add_blogmeta( $b1, 'foo2', 'bar2' );
+               bp_blogs_add_blogmeta( $b2, 'foo', 'bar' );
+
+               // Need this info
+               $b1_name = bp_blogs_get_blogmeta( $b1, 'name' );
+               $b1_description = bp_blogs_get_blogmeta( $b1, 'description' );
+               $b1_last_activity = bp_blogs_get_blogmeta( $b1, 'last_activity' );
+
+               $b2_name = bp_blogs_get_blogmeta( $b2, 'name' );
+               $b2_description = bp_blogs_get_blogmeta( $b2, 'description' );
+               $b2_last_activity = bp_blogs_get_blogmeta( $b2, 'last_activity' );
+
+               // Clear caches (due to _get_)
+               wp_cache_delete( $b1, 'blog_meta' );
+               wp_cache_delete( $b2, 'blog_meta' );
+
+               // Caches should be empty
+               $this->assertFalse( wp_cache_get( $b1, 'blog_meta' ) );
+               $this->assertFalse( wp_cache_get( $b2, 'blog_meta' ) );
+
+               bp_blogs_update_meta_cache( array( $b1, $b2 ) );
+
+               $b1_expected = array(
+                       'name' => array(
+                               $b1_name,
+                       ),
+                       'description' => array(
+                               $b1_description,
+                       ),
+                       'last_activity' => array(
+                               $b1_last_activity,
+                       ),
+                       'foo' => array(
+                               'bar',
+                       ),
+                       'foo2' => array(
+                               'bar2',
+                       ),
+               );
+
+               $b2_expected = array(
+                       'name' => array(
+                               $b2_name,
+                       ),
+                       'description' => array(
+                               $b2_description,
+                       ),
+                       'last_activity' => array(
+                               $b2_last_activity,
+                       ),
+                       'foo' => array(
+                               'bar',
+                       ),
+               );
+
+               $this->assertSame( $b1_expected, wp_cache_get( $b1, 'blog_meta' ) );
+               $this->assertSame( $b2_expected, wp_cache_get( $b2, 'blog_meta' ) );
+       }
+
+       /**
+        * @group bp_blogs_update_meta_cache
+        * @group bp_has_blogs
+        */
+       public function test_bp_blogs_update_meta_cache_bp_has_blogs() {
+               if ( ! is_multisite() ) {
+                       return;
+               }
+
+               $u = $this->create_user();
+
+               // Switch user so we have access to non-public blogs
+               $old_user = get_current_user_id();
+               $this->set_current_user( $u );
+
+               $b1 = $this->factory->blog->create();
+               $b2 = $this->factory->blog->create();
+
+               bp_blogs_record_blog( $b1, $u, true );
+               bp_blogs_record_blog( $b2, $u, true );
+
+               bp_blogs_add_blogmeta( $b1, 'foo', 'bar' );
+               bp_blogs_add_blogmeta( $b1, 'foo2', 'bar2' );
+               bp_blogs_add_blogmeta( $b2, 'foo', 'bar' );
+
+               // Need this info
+               $b1_name = bp_blogs_get_blogmeta( $b1, 'name' );
+               $b1_description = bp_blogs_get_blogmeta( $b1, 'description' );
+               $b1_last_activity = bp_blogs_get_blogmeta( $b1, 'last_activity' );
+
+               $b2_name = bp_blogs_get_blogmeta( $b2, 'name' );
+               $b2_description = bp_blogs_get_blogmeta( $b2, 'description' );
+               $b2_last_activity = bp_blogs_get_blogmeta( $b2, 'last_activity' );
+
+               // Clear caches (due to _get_)
+               wp_cache_delete( $b1, 'blog_meta' );
+               wp_cache_delete( $b2, 'blog_meta' );
+
+               // Caches should be empty
+               $this->assertFalse( wp_cache_get( $b1, 'blog_meta' ) );
+               $this->assertFalse( wp_cache_get( $b2, 'blog_meta' ) );
+
+               bp_has_blogs( array(
+                       'user_id' => $u,
+               ) );
+
+               $b1_expected = array(
+                       'name' => array(
+                               $b1_name,
+                       ),
+                       'description' => array(
+                               $b1_description,
+                       ),
+                       'last_activity' => array(
+                               $b1_last_activity,
+                       ),
+                       'foo' => array(
+                               'bar',
+                       ),
+                       'foo2' => array(
+                               'bar2',
+                       ),
+               );
+
+               $b2_expected = array(
+                       'name' => array(
+                               $b2_name,
+                       ),
+                       'description' => array(
+                               $b2_description,
+                       ),
+                       'last_activity' => array(
+                               $b2_last_activity,
+                       ),
+                       'foo' => array(
+                               'bar',
+                       ),
+               );
+
+               $this->assertSame( $b1_expected, wp_cache_get( $b1, 'blog_meta' ) );
+               $this->assertSame( $b2_expected, wp_cache_get( $b2, 'blog_meta' ) );
+
+               $this->set_current_user( $old_user );
+       }
+
+       /**
+        * @group bp_blogs_update_meta_cache
+        * @group bp_has_blogs
+        */
+       public function test_bp_blogs_update_meta_cache_bp_has_blogs_false() {
+               if ( ! is_multisite() ) {
+                       return;
+               }
+
+               $u = $this->create_user();
+
+               // Switch user so we have access to non-public blogs
+               $old_user = get_current_user_id();
+               $this->set_current_user( $u );
+
+               $b1 = $this->factory->blog->create();
+               $b2 = $this->factory->blog->create();
+
+               bp_blogs_record_blog( $b1, $u, true );
+               bp_blogs_record_blog( $b2, $u, true );
+
+               bp_blogs_add_blogmeta( $b1, 'foo', 'bar' );
+               bp_blogs_add_blogmeta( $b1, 'foo2', 'bar2' );
+               bp_blogs_add_blogmeta( $b2, 'foo', 'bar' );
+
+               // Need this info
+               $b1_name = bp_blogs_get_blogmeta( $b1, 'name' );
+               $b1_description = bp_blogs_get_blogmeta( $b1, 'description' );
+               $b1_last_activity = bp_blogs_get_blogmeta( $b1, 'last_activity' );
+
+               $b2_name = bp_blogs_get_blogmeta( $b2, 'name' );
+               $b2_description = bp_blogs_get_blogmeta( $b2, 'description' );
+               $b2_last_activity = bp_blogs_get_blogmeta( $b2, 'last_activity' );
+
+               // Clear caches (due to _get_)
+               wp_cache_delete( $b1, 'blog_meta' );
+               wp_cache_delete( $b2, 'blog_meta' );
+
+               // Caches should be empty
+               $this->assertFalse( wp_cache_get( $b1, 'blog_meta' ) );
+               $this->assertFalse( wp_cache_get( $b2, 'blog_meta' ) );
+
+               bp_has_blogs( array(
+                       'update_meta_cache' => false,
+               ) );
+
+               // Caches should be empty
+               $this->assertFalse( wp_cache_get( $b1, 'blog_meta' ) );
+               $this->assertFalse( wp_cache_get( $b2, 'blog_meta' ) );
+
+               $this->set_current_user( $old_user );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>