Skip to:
Content

BuddyPress.org


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

BP_Messages_Thread: return the total number of messages in a thread.

Introducing a new property $messages_total_count to the BP_Messages_Thread class which returns the total number of messages in a thread.

Closes https://github.com/buddypress/buddypress/pull/51
Fixews #8697

File:
1 edited

Legend:

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

    r13395 r13401  
    2727
    2828    /**
    29      * The current messages.
     29     * The current messages in the message thread.
    3030     *
    3131     * @since 1.0.0
     
    3333     */
    3434    public $messages;
     35
     36    /**
     37     * The current messages count in the message thread.
     38     *
     39     * @since 12.0.0
     40     * @var int
     41     */
     42    public $messages_total_count;
    3543
    3644    /**
     
    189197            return false;
    190198        }
     199
     200        // Messages total count.
     201        $this->messages_total_count = self::get_total_thread_message_count( $this->thread_id );
    191202
    192203        $last_message_index         = count( $this->messages ) - 1;
     
    941952
    942953    /**
     954     * Returns the total number of messages in a thread.
     955     *
     956     * @since 12.0.0
     957     *
     958     * @global wpdb $wpdb WordPress database object.
     959     *
     960     * @param integer $thread_id The message thread ID.
     961     * @return integer Total thread message count
     962     */
     963    public static function get_total_thread_message_count( $thread_id ) {
     964        global $wpdb;
     965
     966        $cache_key   = "{$thread_id}_bp_messages_thread_total_count";
     967        $total_count = wp_cache_get( $cache_key, 'bp_messages_threads' );
     968
     969        if ( false === $total_count ) {
     970            $bp = buddypress();
     971
     972            $total_count = (int) $wpdb->get_var(
     973                $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id )
     974            );
     975
     976            wp_cache_set( $cache_key, $total_count, 'bp_messages_threads' );
     977        }
     978
     979        /**
     980         * Thread messages count.
     981         *
     982         * @since 12.0.0
     983         *
     984         * @param integer $total_count Total thread messages count.
     985         * @param integer $thread_id   ID of the thread.
     986         */
     987        return (int) apply_filters( 'messages_thread_get_total_message_count', $total_count, (int) $thread_id );
     988    }
     989
     990    /**
    943991     * Determine if the logged-in user is a sender of any message in a thread.
    944992     *
Note: See TracChangeset for help on using the changeset viewer.