<!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][13402] trunk: `BP_Messages_Thread`: return the *right* latest message.</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 { white-space: pre-line; 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/13402">13402</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/13402","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>espellcaste</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-01-08 23:12:01 +0000 (Sun, 08 Jan 2023)</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'>`BP_Messages_Thread`: return the *right* latest message.

Introducing a new method `get_latest_thread_message()` to the `BP_Messages_Thread` class which returns the latest message of a thread.

Closes https://github.com/buddypress/buddypress/pull/52
Fixes <a href="http://buddypress.trac.wordpress.org/ticket/8696">#8696</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpmessagesbpmessagescachephp">trunk/src/bp-messages/bp-messages-cache.php</a></li>
<li><a href="#trunksrcbpmessagesclassesclassbpmessagesthreadphp">trunk/src/bp-messages/classes/class-bp-messages-thread.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryphp">trunk/tests/phpunit/includes/factory.php</a></li>
<li><a href="#trunktestsphpunittestcasesmessagesclassbpmessagesthreadphp">trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpmessagesbpmessagescachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/bp-messages-cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/bp-messages-cache.php       2023-01-08 23:03:12 UTC (rev 13401)
+++ trunk/src/bp-messages/bp-messages-cache.php 2023-01-08 23:12:01 UTC (rev 13402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -65,6 +65,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_cache_delete( $recipient->user_id, 'bp_messages_unread_count' );
</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">+        // Delete thread latest message cached data.
+       wp_cache_delete( "{$message->thread_id}_bp_messages_thread_latest_message", 'bp_messages_threads' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Delete thread recipient cache.
</span><span class="cx" style="display: block; padding: 0 10px">        wp_cache_delete( 'thread_recipients_' . $message->thread_id, 'bp_messages' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,6 +88,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_cache_delete( $thread_id, 'bp_messages_threads' );
</span><span class="cx" style="display: block; padding: 0 10px">                wp_cache_delete( "thread_recipients_{$thread_id}", 'bp_messages' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Delete thread latest message cached data.
+               wp_cache_delete( "{$thread_id}_bp_messages_thread_latest_message", 'bp_messages_threads' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Delete thread messages count.
</span><span class="cx" style="display: block; padding: 0 10px">                wp_cache_delete( "{$thread_id}_bp_messages_thread_total_count", 'bp_messages_threads' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunksrcbpmessagesclassesclassbpmessagesthreadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/classes/class-bp-messages-thread.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/classes/class-bp-messages-thread.php        2023-01-08 23:03:12 UTC (rev 13401)
+++ trunk/src/bp-messages/classes/class-bp-messages-thread.php  2023-01-08 23:12:01 UTC (rev 13402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,23 +190,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->messages_order = $r['order'];
</span><span class="cx" style="display: block; padding: 0 10px">                $this->thread_id      = (int) $thread_id;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Get messages for thread.
-               $this->messages = self::get_messages( $this->thread_id, $r );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Get latest message.
+               $latest_message = self::get_latest_thread_message( $this->thread_id );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( empty( $this->messages ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Bail early if no thread message is found.
+               if ( empty( $latest_message ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return false;
</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">+                // Set latest message data.
+               $this->last_message_id      = $latest_message->id;
+               $this->last_message_date    = $latest_message->date_sent;
+               $this->last_sender_id       = $latest_message->sender_id;
+               $this->last_message_subject = $latest_message->subject;
+               $this->last_message_content = $latest_message->message;
+
+               // Get messages for thread.
+               $this->messages = self::get_messages( $this->thread_id, $r );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Messages total count.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->messages_total_count = self::get_total_thread_message_count( $this->thread_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $last_message_index         = count( $this->messages ) - 1;
-               $this->last_message_id      = $this->messages[ $last_message_index ]->id;
-               $this->last_message_date    = $this->messages[ $last_message_index ]->date_sent;
-               $this->last_sender_id       = $this->messages[ $last_message_index ]->sender_id;
-               $this->last_message_subject = $this->messages[ $last_message_index ]->subject;
-               $this->last_message_content = $this->messages[ $last_message_index ]->message;
-
</del><span class="cx" style="display: block; padding: 0 10px">                 foreach ( (array) $this->messages as $key => $message ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->sender_ids[ $message->sender_id ] = $message->sender_id;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -429,6 +433,55 @@
</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 latest thread message.
+        *
+        * @since 12.0.0
+        *
+        * @global wpdb $wpdb WordPress database object.
+        *
+        * @param integer $thread_id The message thread ID.
+        * @return object|null
+        */
+       public static function get_latest_thread_message( $thread_id ) {
+               global $wpdb;
+
+               $thread_id = (int) $thread_id;
+               $cache_key = "{$thread_id}_bp_messages_thread_latest_message";
+               $message   = wp_cache_get( $cache_key, 'bp_messages_threads' );
+
+               // Get latest message and cache it.
+               if ( empty( $message ) ) {
+                       $bp      = buddypress();
+                       $message = $wpdb->get_row(
+                               $wpdb->prepare(
+                                       "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent DESC",
+                                       $thread_id
+                               )
+                       );
+
+                       // Cast integers.
+                       if ( ! empty( $message ) ) {
+                               $message->id        = (int) $message->id;
+                               $message->sender_id = (int) $message->sender_id;
+                               $message->thread_id = (int) $message->thread_id;
+
+                               // Cache message.
+                               wp_cache_set( $cache_key, $message, 'bp_messages_threads' );
+                       }
+               }
+
+               /**
+                * Latest thread message.
+                *
+                * @since 12.0.0
+                *
+                * @param object|null $message   Latest thread message or null.
+                * @param integer     $thread_id ID of the thread.
+                */
+               return apply_filters( 'messages_thread_get_latest_message', $message, $thread_id );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Static method to get message recipients by thread ID.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.3.0
</span></span></pre></div>
<a id="trunktestsphpunitincludesfactoryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/factory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory.php  2023-01-08 23:03:12 UTC (rev 13401)
+++ trunk/tests/phpunit/includes/factory.php    2023-01-08 23:12:01 UTC (rev 13402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,8 +175,9 @@
</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">                $thread_id = messages_new_message( $args );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $thread = new BP_Messages_Thread( $thread_id );
-               return end( $thread->messages )->id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $message   = BP_Messages_Thread::get_latest_thread_message( $thread_id );
+
+               return $message->id;
</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">        function update_object( $message_id, $fields ) {
</span></span></pre></div>
<a id="trunktestsphpunittestcasesmessagesclassbpmessagesthreadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php       2023-01-08 23:03:12 UTC (rev 13401)
+++ trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php 2023-01-08 23:12:01 UTC (rev 13402)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -65,27 +65,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Default results.
</span><span class="cx" style="display: block; padding: 0 10px">                $messages = BP_Messages_Thread::get_messages( $m1->thread_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( 100 === count( $messages ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 100, $messages );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Get first 10 messages.
</span><span class="cx" style="display: block; padding: 0 10px">                $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 1, 'per_page' => 10 ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( 10 === count( $messages ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 10, $messages );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Get first 10 messages differently.
</span><span class="cx" style="display: block; padding: 0 10px">                $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => 1, 'per_page' => 10 ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( 10 === count( $thread->messages ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 10, $thread->messages );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Get all messages.
</span><span class="cx" style="display: block; padding: 0 10px">                $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => null, 'per_page' => null ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( 100 === count( $messages ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 100, $messages );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Get all mesages differently.
</span><span class="cx" style="display: block; padding: 0 10px">                $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', array( 'page' => null, 'per_page' => null ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( 100 === count( $thread->messages ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 100, $thread->messages );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Get last message.
</span><span class="cx" style="display: block; padding: 0 10px">                $messages = BP_Messages_Thread::get_messages( $m1->thread_id, array( 'page' => 100, 'per_page' => 1 ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertTrue( 1 === count( $messages ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 1, $messages );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( $u1, $messages[0]->sender_id );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'Last Message', $messages[0]->subject );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -195,13 +195,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // create reply
</span><span class="cx" style="display: block; padding: 0 10px">                $message_2 = self::factory()->message->create_and_get( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'thread_id'  => $message_1->thread_id,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'sender_id'  => $u1,
-                       'recipients' => array( $u2 ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'sender_id'  => $u2,
+                       'date_sent'  => '2030-10-27 19:21:40',
+                       'recipients' => array( $u1 ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         'content'    => 'Bar'
</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">                // Default sort from constructor.
</span><span class="cx" style="display: block; padding: 0 10px">                $thread = new BP_Messages_Thread( $message_1->thread_id );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals(
</span><span class="cx" style="display: block; padding: 0 10px">                        array( $message_1->id, $message_2->id ),
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_list_pluck( $thread->messages, 'id' )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -595,6 +597,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $thread_id = $message->thread_id;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Populate cache.
+               $thread = BP_Messages_Thread::get_recipients_for_thread( $thread_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Cache should be populated.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -602,7 +607,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                messages_mark_thread_read( $thread_id, $u2 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Cache should be empty.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertFalse( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $thread = new BP_Messages_Thread( $thread_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -625,6 +630,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $thread_id = $message->thread_id;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Populate cache.
+               $thread = BP_Messages_Thread::get_recipients_for_thread( $thread_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Cache should be populated.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -632,7 +640,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                messages_mark_thread_unread( $thread_id, $u2 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Cache should be empty.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertFalse( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $thread = new BP_Messages_Thread( $thread_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -733,34 +741,97 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @group last_message
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @group latest_message
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_last_message_populated() {
-               $u1 = self::factory()->user->create();
-               $u2 = self::factory()->user->create();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_get_latest_message_data() {
+               $u1      = self::factory()->user->create();
+               $u2      = self::factory()->user->create();
+               $subject = 'Last One';
+               $content = 'Bar and baz';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $date = bp_core_current_time();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $m = self::factory()->message->create_and_get(
+                       [
+                               'sender_id'  => $u1,
+                               'recipients' => [ $u2 ],
+                               'subject'    => 'Foo',
+                               'content'    => 'Bar',
+                       ]
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $message = self::factory()->message->create_and_get( array(
-                       'sender_id' => $u1,
-                       'recipients' => array( $u2 ),
-                       'subject' => 'Foo',
-                       'date_sent' => $date,
-                       'content' => 'Bar and baz.',
-               ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::factory()->message->create_many(
+                       8,
+                       [
+                               'thread_id'  => $m->thread_id,
+                               'sender_id'  => $u2,
+                               'recipients' => [ $u1 ],
+                               'subject'    => 'Bar',
+                       ]
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $t1 = $message->thread_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $date = '2030-10-27 19:21:40';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $thread = new BP_Messages_Thread( $t1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Last(est) message.
+               $m2 = self::factory()->message->create_and_get(
+                       [
+                               'thread_id'  => $m->thread_id,
+                               'sender_id'  => $u1,
+                               'recipients' => [ $u2 ],
+                               'date_sent'  => $date,
+                               'subject'    => $subject,
+                               'content'    => $content,
+                       ]
+               );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertNotNull( $thread->last_message_id );
-               $this->assertEquals( 'Foo', $thread->last_message_subject );
-               $this->assertEquals( $u1, $thread->last_sender_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $thread = new BP_Messages_Thread( $m->thread_id, 'ASC', [ 'page' => 1, 'per_page' => 3 ] );
+
+               $this->assertCount( 3, $thread->messages );
+               $this->assertEquals( $m2->id, $thread->last_message_id );
+               $this->assertEquals( $m2->sender_id, $thread->last_sender_id );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( $date, $thread->last_message_date );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 'Bar and baz.', $thread->last_message_content );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( $subject, $thread->last_message_subject );
+               $this->assertEquals( $content, $thread->last_message_content );
</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">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @group latest_message
+        * @group cache
+        */
+       public function test_get_latest_message_from_cache() {
+               $u1   = self::factory()->user->create();
+               $u2   = self::factory()->user->create();
+               $date = '2030-10-27 19:21:40';
+
+               $m1 = self::factory()->message->create_and_get(
+                       [
+                               'sender_id'  => $u1,
+                               'recipients' => [ $u2 ],
+                               'subject'    => 'Foo',
+                       ]
+               );
+
+               $m2 = self::factory()->message->create_and_get(
+                       [
+                               'thread_id'  => $m1->thread_id,
+                               'sender_id'  => $u1,
+                               'recipients' => [ $u2 ],
+                               'date_sent'  => $date,
+                               'subject'    => 'Last Message',
+                               'content'    => 'Last Message Content',
+                       ]
+               );
+
+               $thread_id      = $m1->thread_id;
+               $latest_message = wp_cache_get( "{$thread_id}_bp_messages_thread_latest_message", 'bp_messages_threads' );
+
+               $this->assertEquals( $thread_id, $latest_message->thread_id );
+               $this->assertEquals( $m2->id, $latest_message->id );
+               $this->assertEquals( $m2->sender_id, $latest_message->sender_id );
+               $this->assertEquals( $date, $latest_message->date_sent );
+               $this->assertEquals( 'Last Message', $latest_message->subject );
+               $this->assertEquals( 'Last Message Content', $latest_message->message );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @group exit
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket BP7540
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre>
</div>
</div>

</body>
</html>