Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/08/2023 11:12:01 PM (19 months ago)
Author:
espellcaste
Message:

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 #8696

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r13401 r13402  
    191191        $this->thread_id      = (int) $thread_id;
    192192
     193        // Get latest message.
     194        $latest_message = self::get_latest_thread_message( $this->thread_id );
     195
     196        // Bail early if no thread message is found.
     197        if ( empty( $latest_message ) ) {
     198            return false;
     199        }
     200
     201        // Set latest message data.
     202        $this->last_message_id      = $latest_message->id;
     203        $this->last_message_date    = $latest_message->date_sent;
     204        $this->last_sender_id       = $latest_message->sender_id;
     205        $this->last_message_subject = $latest_message->subject;
     206        $this->last_message_content = $latest_message->message;
     207
    193208        // Get messages for thread.
    194209        $this->messages = self::get_messages( $this->thread_id, $r );
    195210
    196         if ( empty( $this->messages ) ) {
    197             return false;
    198         }
    199 
    200211        // Messages total count.
    201212        $this->messages_total_count = self::get_total_thread_message_count( $this->thread_id );
    202 
    203         $last_message_index         = count( $this->messages ) - 1;
    204         $this->last_message_id      = $this->messages[ $last_message_index ]->id;
    205         $this->last_message_date    = $this->messages[ $last_message_index ]->date_sent;
    206         $this->last_sender_id       = $this->messages[ $last_message_index ]->sender_id;
    207         $this->last_message_subject = $this->messages[ $last_message_index ]->subject;
    208         $this->last_message_content = $this->messages[ $last_message_index ]->message;
    209213
    210214        foreach ( (array) $this->messages as $key => $message ) {
     
    427431         */
    428432        return apply_filters( 'bp_messages_thread_get_messages', (array) $messages, (int) $thread_id, (array) $r );
     433    }
     434
     435    /**
     436     * Get latest thread message.
     437     *
     438     * @since 12.0.0
     439     *
     440     * @global wpdb $wpdb WordPress database object.
     441     *
     442     * @param integer $thread_id The message thread ID.
     443     * @return object|null
     444     */
     445    public static function get_latest_thread_message( $thread_id ) {
     446        global $wpdb;
     447
     448        $thread_id = (int) $thread_id;
     449        $cache_key = "{$thread_id}_bp_messages_thread_latest_message";
     450        $message   = wp_cache_get( $cache_key, 'bp_messages_threads' );
     451
     452        // Get latest message and cache it.
     453        if ( empty( $message ) ) {
     454            $bp      = buddypress();
     455            $message = $wpdb->get_row(
     456                $wpdb->prepare(
     457                    "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent DESC",
     458                    $thread_id
     459                )
     460            );
     461
     462            // Cast integers.
     463            if ( ! empty( $message ) ) {
     464                $message->id        = (int) $message->id;
     465                $message->sender_id = (int) $message->sender_id;
     466                $message->thread_id = (int) $message->thread_id;
     467
     468                // Cache message.
     469                wp_cache_set( $cache_key, $message, 'bp_messages_threads' );
     470            }
     471        }
     472
     473        /**
     474         * Latest thread message.
     475         *
     476         * @since 12.0.0
     477         *
     478         * @param object|null $message   Latest thread message or null.
     479         * @param integer     $thread_id ID of the thread.
     480         */
     481        return apply_filters( 'messages_thread_get_latest_message', $message, $thread_id );
    429482    }
    430483
Note: See TracChangeset for help on using the changeset viewer.