Skip to:
Content

BuddyPress.org

Changeset 13401


Ignore:
Timestamp:
01/08/2023 11:03:12 PM (19 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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-cache.php

    r13196 r13401  
    5858    wp_cache_delete( $message->thread_id, 'bp_messages_threads' );
    5959
     60    // Delete thread messages count.
     61    wp_cache_delete( "{$message->thread_id}_bp_messages_thread_total_count", 'bp_messages_threads' );
     62
    6063    // Delete unread count for each recipient.
    6164    foreach ( (array) $message->recipients as $recipient ) {
     
    8285        wp_cache_delete( $thread_id, 'bp_messages_threads' );
    8386        wp_cache_delete( "thread_recipients_{$thread_id}", 'bp_messages' );
     87
     88        // Delete thread messages count.
     89        wp_cache_delete( "{$thread_id}_bp_messages_thread_total_count", 'bp_messages_threads' );
    8490    }
    8591
  • 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     *
  • trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php

    r13223 r13401  
    8989        $this->assertEquals( $u1, $messages[0]->sender_id );
    9090        $this->assertEquals( 'Last Message', $messages[0]->subject );
     91    }
     92
     93    public function test_get_messages_total_count() {
     94        $u1 = self::factory()->user->create();
     95        $u2 = self::factory()->user->create();
     96        $m1 = self::factory()->message->create_and_get(
     97            [
     98                'sender_id'  => $u1,
     99                'recipients' => [ $u2 ],
     100                'subject'    => 'Foo',
     101            ]
     102        );
     103
     104        self::factory()->message->create_many(
     105            98,
     106            [
     107                'thread_id'  => $m1->thread_id,
     108                'sender_id'  => $u2,
     109                'recipients' => [ $u1 ],
     110                'subject'    => 'Bar',
     111            ]
     112        );
     113
     114        // Last message
     115        self::factory()->message->create(
     116            [
     117                'thread_id'  => $m1->thread_id,
     118                'sender_id'  => $u1,
     119                'recipients' => [ $u2 ],
     120                'subject'    => 'Last Message',
     121            ]
     122        );
     123
     124        $count = BP_Messages_Thread::get_total_thread_message_count( $m1->thread_id );
     125        $this->assertSame( 100, $count );
     126
     127        $thread = new BP_Messages_Thread( $m1->thread_id );
     128        $this->assertSame( 100, $thread->messages_total_count );
     129
     130        $thread = new BP_Messages_Thread( $m1->thread_id, 'ASC', [ 'page' => 1, 'per_page' => 10 ] );
     131        $this->assertCount( 10, $thread->messages );
     132        $this->assertSame( 100, $thread->messages_total_count );
     133    }
     134
     135    /**
     136     * @group cache
     137     */
     138    public function test_get_messages_total_count_cached() {
     139        $u1 = self::factory()->user->create();
     140        $u2 = self::factory()->user->create();
     141        $m1 = self::factory()->message->create_and_get(
     142            [
     143                'sender_id'  => $u1,
     144                'recipients' => [ $u2 ],
     145                'subject'    => 'Foo',
     146            ]
     147        );
     148
     149        $m2 = self::factory()->message->create(
     150            [
     151                'thread_id'  => $m1->thread_id,
     152                'sender_id'  => $u2,
     153                'recipients' => [ $u1 ],
     154                'subject'    => 'Middle Message',
     155            ]
     156        );
     157
     158        $cache_key = "{$m1->thread_id}_bp_messages_thread_total_count";
     159        $count     = BP_Messages_Thread::get_total_thread_message_count( $m1->thread_id );
     160
     161        $this->assertSame( 2, $count );
     162        $this->assertSame( 2, wp_cache_get( $cache_key, 'bp_messages_threads' ) );
     163
     164        self::factory()->message->create(
     165            [
     166                'thread_id'  => $m1->thread_id,
     167                'sender_id'  => $u2,
     168                'recipients' => [ $u1 ],
     169                'subject'    => 'Last Message',
     170            ]
     171        );
     172
     173        $this->assertSame( 3, wp_cache_get( $cache_key, 'bp_messages_threads' ) );
     174
     175        // Delete thread.
     176        messages_delete_thread( $m1->thread_id );
     177
     178        $this->assertFalse( wp_cache_get( $cache_key, 'bp_messages_threads' ) );
    91179    }
    92180
Note: See TracChangeset for help on using the changeset viewer.