Skip to:
Content

BuddyPress.org

Changeset 9183


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.

Location:
trunk/src/bp-messages
Files:
2 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
  • trunk/src/bp-messages/bp-messages-template.php

    r9161 r9183  
    14681468     * Constructor method.
    14691469     *
    1470      * @param int $thread_id ID of the message thread.
    1471      * @param string $order 'ASC' or 'DESC'.
    1472      */
    1473     public function __construct( $thread_id, $order ) {
    1474         $this->thread        = new BP_Messages_Thread( $thread_id, $order );
     1470     * @see BP_Messages_Thread::populate() for full parameter info
     1471     */
     1472    public function __construct( $thread_id, $order, $args ) {
     1473        $this->thread        = new BP_Messages_Thread( $thread_id, $order, $args );
    14751474        $this->message_count = count( $this->thread->messages );
    14761475
     
    15731572 *           the URL (bp_action_variable( 0 )).
    15741573 *     @type string $order 'ASC' or 'DESC'. Default: 'ASC'.
     1574 *     @type bool $update_meta_cache Whether to pre-fetch metadata for
     1575 *           queried message items. Default: true.
    15751576 * }
    15761577 * @return bool True if there are messages to display, otherwise false.
     
    15801581
    15811582    $r = bp_parse_args( $args, array(
    1582         'thread_id' => false,
    1583         'order'     => 'ASC'
     1583        'thread_id'         => false,
     1584        'order'             => 'ASC',
     1585        'update_meta_cache' => true,
    15841586    ), 'thread_has_messages' );
    15851587
     
    15881590    }
    15891591
    1590     $thread_template = new BP_Messages_Thread_Template( $r['thread_id'], $r['order'] );
     1592    // Set up extra args
     1593    $extra_args = $r;
     1594    unset( $extra_args['thread_id'], $extra_args['order'] );
     1595
     1596    $thread_template = new BP_Messages_Thread_Template( $r['thread_id'], $r['order'], $extra_args );
    15911597
    15921598    return $thread_template->has_messages();
Note: See TracChangeset for help on using the changeset viewer.