Skip to:
Content

BuddyPress.org

Changeset 13148


Ignore:
Timestamp:
11/13/2021 06:54:07 PM (3 years ago)
Author:
espellcaste
Message:

Adding support to paginate messages/recipients to the BP_Messages_Box_Template class.

When querying threads using the BP_Messages_Box_Template class, one might set a default of messages/recipients, instead of returning all items.

Fixes #8597

Location:
trunk
Files:
5 edited

Legend:

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

    r13147 r13148  
    2424 * @param array|string $args {
    2525 *     Array of arguments. All are optional.
    26  *     @type int    $user_id      ID of the user whose threads are being loaded.
    27  *                                Default: ID of the logged-in user.
    28  *     @type string $box          Current "box" view. If not provided here, the current
    29  *                                view will be inferred from the URL.
    30  *     @type int    $per_page     Number of results to return per page. Default: 10.
    31  *     @type int    $max          Max results to return. Default: false.
    32  *     @type string $type         Type of messages to return. Values: 'all', 'read', 'unread'
    33  *                                Default: 'all'
    34  *     @type string $search_terms Terms to which to limit results. Default:
    35  *                                the value of $_REQUEST['s'].
    36  *     @type string $page_arg     URL argument used for the pagination param.
    37  *                                Default: 'mpage'.
    38  *     @type array  $meta_query   Meta query arguments. Only applicable if $box is
    39  *                                not 'notices'. See WP_Meta_Query more details.
     26 *     @type int      $user_id             ID of the user whose threads are being loaded.
     27 *                                         Default: ID of the logged-in user.
     28 *     @type string   $box                 Current "box" view. If not provided here, the current
     29 *                                         view will be inferred from the URL.
     30 *     @type int      $per_page            Number of results to return per page. Default: 10.
     31 *     @type int      $max                 Max results to return. Default: false.
     32 *     @type string   $type                Type of messages to return. Values: 'all', 'read', 'unread'
     33 *                                         Default: 'all'
     34 *     @type string   $search_terms        Terms to which to limit results. Default:
     35 *                                         the value of $_REQUEST['s'].
     36 *     @type string   $page_arg            URL argument used for the pagination param.
     37 *                                         Default: 'mpage'.
     38 *     @type array    $meta_query          Meta query arguments. Only applicable if $box is
     39 *                                         not 'notices'. See WP_Meta_Query more details.
     40 *     @type int|null $recipients_page     Page of recipients being requested. Default to null, meaning all.
     41 *     @type int|null $recipients_per_page Recipients to return per page. Defaults to null, meaning all.
     42 *     @type int|null $messages_page       Page of messages being requested. Default to null, meaning all.
     43 *     @type int|null $messages_per_page   Messages to return per page. Defaults to null, meaning all
    4044 * }
    4145 * @return bool True if there are threads to display, otherwise false.
     
    4751    $current_action = bp_current_action();
    4852    switch ( $current_action ) {
    49         case 'sentbox' :
    50         case 'notices' :
    51         case 'inbox'   :
     53        case 'sentbox':
     54        case 'notices':
     55        case 'inbox':
    5256            $default_box = $current_action;
    5357            break;
    54         default :
     58        default:
    5559            $default_box = 'inbox';
    5660            break;
     
    6872        $args,
    6973        array(
    70             'user_id'      => $user_id,
    71             'box'          => $default_box,
    72             'per_page'     => 10,
    73             'max'          => false,
    74             'type'         => 'all',
    75             'search_terms' => $search_terms,
    76             'page_arg'     => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679.
    77             'meta_query'   => array(),
     74            'user_id'             => $user_id,
     75            'box'                 => $default_box,
     76            'per_page'            => 10,
     77            'max'                 => false,
     78            'type'                => 'all',
     79            'search_terms'        => $search_terms,
     80            'page_arg'            => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679.
     81            'meta_query'          => array(),
     82            'recipients_page'     => null,
     83            'recipients_per_page' => null,
     84            'messages_page'       => null,
     85            'messages_per_page'   => null,
    7886        ),
    7987        'has_message_threads'
  • trunk/src/bp-messages/classes/class-bp-messages-box-template.php

    r13108 r13148  
    8080
    8181    /**
    82      * The number of items being requested per page.
     82     * The number of items (threads) being requested per page.
    8383     *
    8484     * @var int
     
    112112        // Backward compatibility with old method of passing arguments.
    113113        if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
    114             _deprecated_argument( __METHOD__, '2.2.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
     114            _deprecated_argument(
     115                __METHOD__,
     116                '2.2.0',
     117                sprintf(
     118                    /* translators: 1: the name of the method. 2: the name of the file. */
     119                    esc_html__( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ),
     120                    __METHOD__,
     121                    __FILE__
     122                )
     123            );
    115124
    116125            $old_args_keys = array(
     
    121130                4 => 'type',
    122131                5 => 'search_terms',
    123                 6 => 'page_arg'
     132                6 => 'page_arg',
    124133            );
    125134
     
    130139            $args,
    131140            array(
    132                 'page'         => 1,
    133                 'per_page'     => 10,
    134                 'page_arg'     => 'mpage',
    135                 'box'          => 'inbox',
    136                 'type'         => 'all',
    137                 'user_id'      => bp_loggedin_user_id(),
    138                 'max'          => false,
    139                 'search_terms' => '',
    140                 'meta_query'   => array(),
     141                'page'                => 1,
     142                'per_page'            => 10,
     143                'page_arg'            => 'mpage',
     144                'box'                 => 'inbox',
     145                'type'                => 'all',
     146                'user_id'             => bp_loggedin_user_id(),
     147                'max'                 => false,
     148                'search_terms'        => '',
     149                'meta_query'          => array(),
     150                'recipients_page'     => null,
     151                'recipients_per_page' => null,
     152                'messages_page'       => null,
     153                'messages_per_page'   => null,
    141154            )
    142155        );
    143156
    144157        $this->pag_arg      = sanitize_key( $r['page_arg'] );
    145         $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    146         $this->pag_num      = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
     158        $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page'] );
     159        $this->pag_num      = bp_sanitize_pagination_arg( 'num', $r['per_page'] );
    147160        $this->user_id      = $r['user_id'];
    148161        $this->box          = $r['box'];
     
    151164
    152165        if ( 'notices' === $this->box ) {
    153             $this->threads = BP_Messages_Notice::get_notices( array(
    154                 'pag_num'  => $this->pag_num,
    155                 'pag_page' => $this->pag_page
    156             ) );
     166            $this->threads = BP_Messages_Notice::get_notices(
     167                array(
     168                    'pag_num'  => $this->pag_num,
     169                    'pag_page' => $this->pag_page,
     170                )
     171            );
    157172        } else {
    158             $threads = BP_Messages_Thread::get_current_threads_for_user( array(
    159                 'user_id'      => $this->user_id,
    160                 'box'          => $this->box,
    161                 'type'         => $this->type,
    162                 'limit'        => $this->pag_num,
    163                 'page'         => $this->pag_page,
    164                 'search_terms' => $this->search_terms,
    165                 'meta_query'   => $r['meta_query'],
    166             ) );
     173            $threads = BP_Messages_Thread::get_current_threads_for_user(
     174                array(
     175                    'user_id'             => $this->user_id,
     176                    'box'                 => $this->box,
     177                    'type'                => $this->type,
     178                    'limit'               => $this->pag_num,
     179                    'page'                => $this->pag_page,
     180                    'search_terms'        => $this->search_terms,
     181                    'meta_query'          => $r['meta_query'],
     182                    'recipients_page'     => $r['recipients_page'],
     183                    'recipients_per_page' => $r['recipients_per_page'],
     184                    'messages_page'       => $r['messages_page'],
     185                    'messages_per_page'   => $r['messages_per_page'],
     186                )
     187            );
    167188
    168189            $this->threads            = isset( $threads['threads'] ) ? $threads['threads'] : array();
     
    170191        }
    171192
    172         if ( !$this->threads ) {
     193        if ( ! $this->threads ) {
    173194            $this->thread_count       = 0;
    174195            $this->total_thread_count = 0;
     
    212233            }
    213234
    214             $this->pag_links = paginate_links( array(
    215                 'base'      => add_query_arg( $pag_args, $base ),
    216                 'format'    => '',
    217                 'total'     => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),
    218                 'current'   => $this->pag_page,
    219                 'prev_text' => _x( '←', 'Message pagination previous text', 'buddypress' ),
    220                 'next_text' => _x( '→', 'Message pagination next text', 'buddypress' ),
    221                 'mid_size'  => 1,
    222                 'add_args'  => $add_args,
    223             ) );
     235            $this->pag_links = paginate_links(
     236                array(
     237                    'base'      => add_query_arg( $pag_args, $base ),
     238                    'format'    => '',
     239                    'total'     => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),
     240                    'current'   => $this->pag_page,
     241                    'prev_text' => _x( '←', 'Message pagination previous text', 'buddypress' ),
     242                    'next_text' => _x( '→', 'Message pagination next text', 'buddypress' ),
     243                    'mid_size'  => 1,
     244                    'add_args'  => $add_args,
     245                )
     246            );
    224247        }
    225248    }
     
    308331            // Set up the last message data.
    309332            if ( count( $this->thread->messages ) > 1 ) {
    310                 if ( 'inbox' == $this->box ) {
     333                if ( 'inbox' === $this->box ) {
    311334                    foreach ( (array) $this->thread->messages as $key => $message ) {
    312                         if ( bp_loggedin_user_id() != $message->sender_id ) {
     335                        if ( bp_loggedin_user_id() !== $message->sender_id ) {
    313336                            $last_message_index = $key;
    314337                            break;
    315338                        }
    316339                    }
    317 
    318                 } elseif ( 'sentbox' == $this->box ) {
     340                } elseif ( 'sentbox' === $this->box ) {
    319341                    foreach ( (array) $this->thread->messages as $key => $message ) {
    320                         if ( bp_loggedin_user_id() == $message->sender_id ) {
     342                        if ( bp_loggedin_user_id() === $message->sender_id ) {
    321343                            $last_message_index = $key;
    322344                            break;
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r13147 r13148  
    545545     * @param array $args {
    546546     *     Array of arguments.
    547      *     @type int    $user_id      The user ID.
    548      *     @type string $box          The type of mailbox to get. Either 'inbox' or 'sentbox'.
    549      *                                Defaults to 'inbox'.
    550      *     @type string $type         The type of messages to get. Either 'all' or 'unread'
    551      *                                or 'read'. Defaults to 'all'.
    552      *     @type int    $limit        The number of messages to get. Defaults to null.
    553      *     @type int    $page         The page number to get. Defaults to null.
    554      *     @type string $search_terms The search term to use. Defaults to ''.
    555      *     @type array  $meta_query   Meta query arguments. See WP_Meta_Query for more details.
     547     *     @type int      $user_id             The user ID.
     548     *     @type string   $box                 The type of mailbox to get. Either 'inbox' or 'sentbox'.
     549     *                                         Defaults to 'inbox'.
     550     *     @type string   $type                The type of messages to get. Either 'all' or 'unread'
     551     *                                         or 'read'. Defaults to 'all'.
     552     *     @type int      $limit               The number of messages to get. Defaults to null.
     553     *     @type int      $page                The page number to get. Defaults to null.
     554     *     @type string   $search_terms        The search term to use. Defaults to ''.
     555     *     @type array    $meta_query          Meta query arguments. See WP_Meta_Query for more details.
     556     *     @type int|null $recipients_page     Page of recipients being requested. Default to null, meaning all.
     557     *     @type int|null $recipients_per_page Recipients to return per page. Defaults to null, meaning all.
     558     *     @type int|null $messages_page       Page of messages being requested. Default to null, meaning all.
     559     *     @type int|null $messages_per_page   Messages to return per page. Defaults to null, meaning all.
    556560     * }
    557      * @return array|bool Array on success. Boolean false on failure.
     561     * @return array|bool Array on success. False on failure.
    558562     */
    559563    public static function get_current_threads_for_user( $args = array() ) {
     
    590594            $args,
    591595            array(
    592                 'user_id'      => false,
    593                 'box'          => 'inbox',
    594                 'type'         => 'all',
    595                 'limit'        => null,
    596                 'page'         => null,
    597                 'search_terms' => '',
    598                 'meta_query'   => array(),
     596                'user_id'             => false,
     597                'box'                 => 'inbox',
     598                'type'                => 'all',
     599                'limit'               => null,
     600                'page'                => null,
     601                'recipients_page'     => null,
     602                'recipients_per_page' => null,
     603                'messages_page'       => null,
     604                'messages_per_page'   => null,
     605                'search_terms'        => '',
     606                'meta_query'          => array(),
    599607            )
    600608        );
     
    603611        $meta_query_sql = array(
    604612            'join'  => '',
    605             'where' => ''
     613            'where' => '',
    606614        );
    607615
     
    627635
    628636        switch ( $r['box'] ) {
    629             case 'sentbox' :
     637            case 'sentbox':
    630638                $user_id_sql = 'AND ' . $wpdb->prepare( 'm.sender_id = %d', $r['user_id'] );
    631639                $sender_sql  = 'AND m.sender_id = r.user_id';
    632640                break;
    633641
    634             case 'inbox' :
     642            case 'inbox':
    635643                $user_id_sql = 'AND ' . $wpdb->prepare( 'r.user_id = %d', $r['user_id'] );
    636644                $sender_sql  = 'AND r.sender_only = 0';
    637645                break;
    638646
    639             default :
     647            default:
    640648                // Omit user-deleted threads from all other custom message boxes.
    641649                $deleted_sql = $wpdb->prepare( '( r.user_id = %d AND r.is_deleted = 0 )', $r['user_id'] );
     
    681689        $threads = array();
    682690        foreach ( (array) $sorted_threads as $thread_id => $date_sent ) {
    683             $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array(
    684                 'update_meta_cache' => false
    685             ) );
     691            $threads[] = new BP_Messages_Thread(
     692                $thread_id,
     693                'ASC',
     694                array(
     695                    'update_meta_cache'   => false,
     696                    'recipients_page'     => $r['recipients_page'],
     697                    'recipients_per_page' => $r['recipients_per_page'],
     698                    'page'                => $r['messages_page'],
     699                    'per_page'            => $r['messages_per_page'],
     700                )
     701            );
    686702        }
    687703
     
    695711         *     @type int   $total_threads Number of threads found by the query.
    696712         * }
     713         *  @param array $r    Array of paremeters.
    697714         */
    698         return apply_filters( 'bp_messages_thread_current_threads', array(
    699             'threads' => &$threads,
    700             'total'   => (int) $total_threads
    701         ) );
     715        return apply_filters(
     716            'bp_messages_thread_current_threads',
     717            array(
     718                'threads' => &$threads,
     719                'total'   => (int) $total_threads,
     720            ),
     721            $r
     722        );
    702723    }
    703724
  • trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php

    r13147 r13148  
    246246
    247247    /**
     248     * @group get_current_threads_for_user
     249     */
     250    public function test_get_current_threads_setting_per_page_messages_and_recipients() {
     251        $u1 = self::factory()->user->create();
     252        $u2 = self::factory()->user->create();
     253
     254        // create initial thread
     255        $message_1 = self::factory()->message->create_and_get(
     256            array(
     257                'sender_id'  => $u1,
     258                'recipients' => array( $u2 ),
     259            )
     260        );
     261
     262        // create some replies to thread
     263        self::factory()->message->create_and_get(
     264            array(
     265                'thread_id'  => $message_1->thread_id,
     266                'sender_id'  => $u2,
     267                'recipients' => array( $u1 ),
     268            )
     269        );
     270
     271        self::factory()->message->create_and_get(
     272            array(
     273                'thread_id'  => $message_1->thread_id,
     274                'sender_id'  => $u2,
     275                'recipients' => array( $u1 ),
     276            )
     277        );
     278
     279        self::factory()->message->create_and_get(
     280            array(
     281                'thread_id'  => $message_1->thread_id,
     282                'sender_id'  => $u1,
     283                'recipients' => array( $u2 ),
     284            )
     285        );
     286
     287        $threads = BP_Messages_Thread::get_current_threads_for_user(
     288            array( 'user_id' => $u1 )
     289        )['threads'];
     290
     291        $this->assertCount( 4, $threads[0]->messages );
     292        $this->assertCount( 2, $threads[0]->recipients );
     293
     294        $threads = BP_Messages_Thread::get_current_threads_for_user(
     295            array(
     296                'user_id'             => $u1,
     297                'messages_page'       => 1,
     298                'messages_per_page'   => 2,
     299                'recipients_page'     => 1,
     300                'recipients_per_page' => 1,
     301            )
     302        )['threads'];
     303
     304        $this->assertCount( 2, $threads[0]->messages );
     305        $this->assertNotCount( 2, $threads[0]->recipients );
     306        $this->assertCount( 1, $threads[0]->recipients );
     307    }
     308
     309    /**
    248310     * @group get_recipients
    249311     */
  • trunk/tests/phpunit/testcases/messages/template.php

    r11737 r13148  
    66 */
    77class BP_Tests_Messages_Template extends BP_UnitTestCase {
     8
    89    /**
    910     * @group bp_has_message_threads
     
    360361        $_REQUEST = $request;
    361362    }
     363
     364    /**
     365     * @group pagination
     366     * @group BP_Messages_Box_Template
     367     */
     368    public function test_setting_per_page_messages_and_recipients() {
     369        $u1 = self::factory()->user->create();
     370        $u2 = self::factory()->user->create();
     371
     372        // create initial thread
     373        $message_1 = self::factory()->message->create_and_get(
     374            array(
     375                'sender_id'  => $u1,
     376                'recipients' => array( $u2 ),
     377            )
     378        );
     379
     380        // create some replies to thread
     381        self::factory()->message->create_and_get(
     382            array(
     383                'thread_id'  => $message_1->thread_id,
     384                'sender_id'  => $u2,
     385                'recipients' => array( $u1 ),
     386            )
     387        );
     388
     389        self::factory()->message->create_and_get(
     390            array(
     391                'thread_id'  => $message_1->thread_id,
     392                'sender_id'  => $u2,
     393                'recipients' => array( $u1 ),
     394            )
     395        );
     396
     397        self::factory()->message->create_and_get(
     398            array(
     399                'thread_id'  => $message_1->thread_id,
     400                'sender_id'  => $u1,
     401                'recipients' => array( $u2 ),
     402            )
     403        );
     404
     405        $messages_template = new BP_Messages_Box_Template(
     406            array( 'user_id' => $u1 )
     407        );
     408
     409        $this->assertSame( array( $message_1->thread_id ), wp_list_pluck( $messages_template->threads, 'thread_id' ) );
     410        $this->assertCount( 4, $messages_template->threads[0]->messages );
     411        $this->assertCount( 2, $messages_template->threads[0]->recipients );
     412
     413        $messages_template = new BP_Messages_Box_Template(
     414            array(
     415                'user_id'             => $u1,
     416                'messages_page'       => 1,
     417                'messages_per_page'   => 2,
     418                'recipients_page'     => 1,
     419                'recipients_per_page' => 1,
     420            )
     421        );
     422
     423        $this->assertCount( 2, $messages_template->threads[0]->messages );
     424        $this->assertNotCount( 2, $messages_template->threads[0]->recipients );
     425        $this->assertCount( 1, $messages_template->threads[0]->recipients );
     426    }
    362427}
Note: See TracChangeset for help on using the changeset viewer.