Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/26/2014 06:08:23 AM (10 years ago)
Author:
r-a-y
Message:

Prefetch message meta when in a message loop.

This reduces overhead when querying for message meta in the context of a
bp_thread_has_messages() loop.

Also introduces an 'update_meta_cache' parameter to the bp_has_thread_messages()
stack, so the message meta cache prefetch can be disabled by developers.

See #3083.

File:
1 edited

Legend:

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

    r9166 r9183  
    110110     * @since BuddyPress (1.0.0)
    111111     *
    112      * @param bool|int $thread_id The message thread ID.
    113      * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'.
    114      */
    115     public function __construct( $thread_id = false, $order = 'ASC' ) {
     112     * @see BP_Messages_Thread::populate() for full description of parameters
     113     */
     114    public function __construct( $thread_id = false, $order = 'ASC', $args = array() ) {
    116115        if ( $thread_id ) {
    117             $this->populate( $thread_id, $order );
     116            $this->populate( $thread_id, $order, $args );
    118117        }
    119118    }
     
    128127     * @param int $thread_id The message thread ID.
    129128     * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'.
     129     * @param array $args {
     130     *     Array of arguments.
     131     *     @type bool $update_meta_cache Whether to pre-fetch metadata for
     132     *           queried message items. Default: true.
     133     * }
    130134     * @return bool False on failure.
    131135     */
    132     public function populate( $thread_id, $order ) {
     136    public function populate( $thread_id = 0, $order = 'ASC', $args = array() ) {
    133137        global $wpdb, $bp;
    134138
    135139        if( 'ASC' != $order && 'DESC' != $order ) {
    136             $order= 'ASC';
    137         }
     140            $order = 'ASC';
     141        }
     142
     143        // merge $args with our defaults
     144        $r = wp_parse_args( $args, array(
     145            'update_meta_cache' => true
     146        ) );
    138147
    139148        $this->messages_order = $order;
     
    154163        if ( isset( $this->recipients[bp_loggedin_user_id()] ) ) {
    155164            $this->unread_count = $this->recipients[bp_loggedin_user_id()]->unread_count;
     165        }
     166
     167        // Grab all message meta
     168        if ( true === (bool) $r['update_meta_cache'] ) {
     169            bp_messages_update_meta_cache( wp_list_pluck( $this->messages, 'id' ) );
    156170        }
    157171
     
    326340        $threads = false;
    327341        foreach ( (array) $sorted_threads as $thread_id => $date_sent ) {
    328             $threads[] = new BP_Messages_Thread( $thread_id );
     342            $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array(
     343                'update_meta_cache' => false
     344            ) );
    329345        }
    330346
Note: See TracChangeset for help on using the changeset viewer.