<!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][11123] trunk/src/bp-friends: Introduce BP_Friends_Friendships::get_friendships().</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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/11123">11123</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/11123","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dcavins</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-09-20 14:42:10 +0000 (Tue, 20 Sep 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Introduce BP_Friends_Friendships::get_friendships().

Introduce a new function, `BP_Friends_Friendships::get_friendships()`,
for fetching and filtering friendships for a
specific user. Also add a new cache group,
`bp_friends_friendships_for_user` that
`get_friendships()` interacts with.

This is the second step in moving toward a
split cache approach for friendships.

Props dcavins, boonebgorges.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpfriendsbpfriendscachephp">trunk/src/bp-friends/bp-friends-cache.php</a></li>
<li><a href="#trunksrcbpfriendsclassesclassbpfriendscomponentphp">trunk/src/bp-friends/classes/class-bp-friends-component.php</a></li>
<li><a href="#trunksrcbpfriendsclassesclassbpfriendsfriendshipphp">trunk/src/bp-friends/classes/class-bp-friends-friendship.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpfriendsbpfriendscachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-friends/bp-friends-cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-friends/bp-friends-cache.php 2016-09-20 14:42:04 UTC (rev 11122)
+++ trunk/src/bp-friends/bp-friends-cache.php   2016-09-20 14:42:10 UTC (rev 11123)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -45,6 +45,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return bool
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function bp_friends_clear_bp_friends_friendships_cache( $friendship_id, $initiator_user_id, $friend_user_id ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Clear friendship ID cache for each user.
+       wp_cache_delete( $initiator_user_id, 'bp_friends_friendships_for_user' );
+       wp_cache_delete( $friend_user_id,    'bp_friends_friendships_for_user' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Clear the friendship object cache.
</span><span class="cx" style="display: block; padding: 0 10px">        wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -61,6 +65,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @param BP_Friends_Friendship $friendship Friendship object.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function bp_friends_clear_bp_friends_friendships_cache_remove( $friendship_id, BP_Friends_Friendship $friendship ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Clear friendship ID cache for each user.
+       wp_cache_delete( $friendship->initiator_user_id, 'bp_friends_friendships_for_user' );
+       wp_cache_delete( $friendship->friend_user_id,    'bp_friends_friendships_for_user' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Clear the friendship object cache.
</span><span class="cx" style="display: block; padding: 0 10px">        wp_cache_delete( $friendship_id, 'bp_friends_friendships' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcbpfriendsclassesclassbpfriendscomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-friends/classes/class-bp-friends-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-friends/classes/class-bp-friends-component.php       2016-09-20 14:42:04 UTC (rev 11122)
+++ trunk/src/bp-friends/classes/class-bp-friends-component.php 2016-09-20 14:42:10 UTC (rev 11123)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -290,6 +290,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_cache_add_global_groups( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'bp_friends_requests',
</span><span class="cx" style="display: block; padding: 0 10px">                        'bp_friends_friendships', // Individual friendship objects are cached here by ID.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'bp_friends_friendships_for_user' // All friendship IDs for a single user.
</ins><span class="cx" style="display: block; padding: 0 10px">                 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                parent::setup_cache_groups();
</span></span></pre></div>
<a id="trunksrcbpfriendsclassesclassbpfriendsfriendshipphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-friends/classes/class-bp-friends-friendship.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-friends/classes/class-bp-friends-friendship.php      2016-09-20 14:42:04 UTC (rev 11122)
+++ trunk/src/bp-friends/classes/class-bp-friends-friendship.php        2016-09-20 14:42:10 UTC (rev 11123)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -220,6 +220,160 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /** Static Methods ********************************************************/
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Get the friendships for a given user.
+        *
+        * @since 2.6.0
+        *
+        * @param int   $user_id              ID of the user whose friends are being retrieved.
+        * @param array $args {
+        *        Optional. Filter parameters.
+        *        @type int    $id                ID of specific friendship to retrieve.
+        *        @type int    $initiator_user_id ID of friendship initiator.
+        *        @type int    $friend_user_id    ID of specific friendship to retrieve.
+        *        @type int    $is_confirmed      Whether the friendship has been accepted.
+        *        @type int    $is_limited        Whether the friendship is limited.
+        *        @type string $order_by          Column name to order by.
+        *        @type string $sort_order        ASC or DESC. Default DESC.
+        * }
+        * @param string $operator            Optional. Operator to use in `wp_list_filter()`.
+        *
+        * @return array $friendships Array of friendship objects.
+        */
+       public static function get_friendships( $user_id, $args = array(), $operator = 'AND' ) {
+
+               if ( empty( $user_id ) ) {
+                       $user_id = bp_loggedin_user_id();
+               }
+
+               $r = bp_parse_args( $args, array(
+                       'id'                => null,
+                       'initiator_user_id' => null,
+                       'friend_user_id'    => null,
+                       'is_confirmed'      => null,
+                       'is_limited'        => null,
+                       'order_by'          => 'date_created',
+                       'sort_order'        => 'DESC',
+                       'page'              => null,
+                       'per_page'          => null
+               ), 'bp_get_user_friendships' );
+
+               // First, we get all friendships that involve the user.
+               $friendship_ids = wp_cache_get( $user_id, 'bp_friends_friendships_for_user' );
+               if ( false === $friendship_ids ) {
+                       $friendship_ids = self::get_friendship_ids_for_user( $user_id );
+                       wp_cache_set( $user_id, $friendship_ids, 'bp_friends_friendships_for_user' );
+               }
+
+               // Prime the membership cache.
+               $uncached_friendship_ids = bp_get_non_cached_ids( $friendship_ids, 'bp_friends_friendships' );
+               if ( ! empty( $uncached_friendship_ids ) ) {
+                       $uncached_friendships = self::get_friendships_by_id( $uncached_friendship_ids );
+
+                       foreach ( $uncached_friendships as $uncached_friendship ) {
+                               wp_cache_set( $uncached_friendship->id, $uncached_friendship, 'bp_friends_friendships' );
+                       }
+               }
+
+               // Assemble filter array.
+               $filters = wp_array_slice_assoc( $r, array( 'id', 'initiator_user_id', 'friend_user_id', 'is_confirmed', 'is_limited' ) );
+               foreach ( $filters as $filter_name => $filter_value ) {
+                       if ( is_null( $filter_value ) ) {
+                               unset( $filters[ $filter_name ] );
+                       }
+               }
+
+               // Populate friendship array from cache, and normalize.
+               $friendships = array();
+               $int_keys    = array( 'id', 'initiator_user_id', 'friend_user_id' );
+               $bool_keys   = array( 'is_confirmed', 'is_limited' );
+               foreach ( $friendship_ids as $friendship_id ) {
+                       // Create a limited BP_Friends_Friendship object (don't fetch the user details).
+                       $friendship = new BP_Friends_Friendship( $friendship_id, false, false );
+
+                       // Sanity check.
+                       if ( ! isset( $friendship->id ) ) {
+                               continue;
+                       }
+
+                       // Integer values.
+                       foreach ( $int_keys as $index ) {
+                               $friendship->{$index} = intval( $friendship->{$index} );
+                       }
+
+                       // Boolean values.
+                       foreach ( $bool_keys as $index ) {
+                               $friendship->{$index} = (bool) $friendship->{$index};
+                       }
+
+                       // We need to support the same operators as wp_list_filter().
+                       if ( 'OR' == $operator || 'NOT' == $operator ) {
+                               $matched = 0;
+
+                               foreach ( $filters as $filter_name => $filter_value ) {
+                                       if ( isset( $friendship->{$filter_name} ) && $filter_value == $friendship->{$filter_name} ) {
+                                               $matched++;
+                                       }
+                               }
+
+                               if ( ( 'OR' == $operator && $matched > 0 )
+                                 || ( 'NOT' == $operator && 0 == $matched ) ) {
+                                       $friendships[ $friendship->id ] = $friendship;
+                               }
+
+                       } else {
+                               /*
+                                * This is the more typical 'AND' style of filter.
+                                * If any of the filters miss, we move on.
+                                */
+                               foreach ( $filters as $filter_name => $filter_value ) {
+                                       if ( ! isset( $friendship->{$filter_name} ) || $filter_value != $friendship->{$filter_name} ) {
+                                               continue 2;
+                                       }
+                               }
+                               $friendships[ $friendship->id ] = $friendship;
+                       }
+
+               }
+
+               // Sort the results on a column name.
+               if ( in_array( $r['order_by'], array( 'id', 'initiator_user_id', 'friend_user_id' ) ) ) {
+                       $friendships = bp_sort_by_key( $friendships, $r['order_by'], 'num', true );
+               }
+
+               // Adjust the sort direction of the results.
+               if ( 'ASC' === strtoupper( $r['sort_order'] ) ) {
+                       // `true` to preserve keys.
+                       $friendships = array_reverse( $friendships, true );
+               }
+
+               // Paginate the results.
+               if ( $r['per_page'] && $r['page'] ) {
+                       $start       = ( $r['page'] - 1 ) * ( $r['per_page'] );
+                       $friendships = array_slice( $friendships, $start, $r['per_page'] );
+               }
+
+               return $friendships;
+       }
+
+       /**
+        * Get all friendship IDs for a user.
+        *
+        * @since 2.7.0
+        *
+        * @param int $user_id ID of the user.
+        * @return array
+        */
+       public static function get_friendship_ids_for_user( $user_id ) {
+               global $wpdb;
+
+               $bp = buddypress();
+
+               $friendship_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d OR friend_user_id = %d) ORDER BY date_created DESC", $user_id, $user_id ) );
+
+               return $friendship_ids;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get the IDs of a given user's friends.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -713,6 +867,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Get friendship objects by ID (or an array of IDs).
+        *
+        * @since 2.7.0
+        *
+        * @param int|string|array $friendship_ids Single friendship ID or comma-separated/array list of friendship IDs.
+        * @return array
+        */
+       public static function get_friendships_by_id( $friendship_ids ) {
+               global $wpdb;
+
+               $bp = buddypress();
+
+               $friendship_ids = implode( ',', wp_parse_id_list( $friendship_ids ) );
+               return $wpdb->get_results( "SELECT * FROM {$bp->friends->table_name} WHERE id IN ({$friendship_ids})" );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get the friend user IDs for a given friendship.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.0.0
</span></span></pre>
</div>
</div>

</body>
</html>