Skip to:
Content

BuddyPress.org

Changeset 9301


Ignore:
Timestamp:
01/06/2015 03:51:19 AM (10 years ago)
Author:
r-a-y
Message:

Messages: Refactor bp_has_message_threads() stack to use array-style parameters.

Props Mamaduka.

See #3767.

Location:
trunk/src/bp-messages
Files:
2 edited

Legend:

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

    r9216 r9301  
    320320     * @since BuddyPress (1.0.0)
    321321     *
    322      * @param int    $user_id The user ID.
    323      * @param string $box  The type of mailbox to get. Either 'inbox' or 'sentbox'.
    324      *                     Defaults to 'inbox'.
    325      * @param string $type The type of messages to get. Either 'all' or 'unread'
    326      *                     or 'read'. Defaults to 'all'.
    327      * @param int    $limit The number of messages to get. Defaults to null.
    328      * @param int    $page  The page number to get. Defaults to null.
    329      * @param string $search_terms The search term to use. Defaults to ''.
     322     * @param array $args {
     323     *     Array of arguments.
     324     *     @type int    $user_id      The user ID.
     325     *     @type string $box          The type of mailbox to get. Either 'inbox' or 'sentbox'.
     326     *                                Defaults to 'inbox'.
     327     *     @type string $type         The type of messages to get. Either 'all' or 'unread'
     328     *                                or 'read'. Defaults to 'all'.
     329     *     @type int    $limit        The number of messages to get. Defaults to null.
     330     *     @type int    $page         The page number to get. Defaults to null.
     331     *     @type string $search_terms The search term to use. Defaults to ''.
     332     *     @type array  $meta_query   Meta query arguments. See WP_Meta_Query for more details.
     333     * }
    330334     * @return array|bool Array on success. Boolean false on failure.
    331335     */
    332     public static function get_current_threads_for_user( $user_id, $box = 'inbox', $type = 'all', $limit = null, $page = null, $search_terms = '' ) {
    333         global $wpdb, $bp;
     336    public static function get_current_threads_for_user( $args = array() ) {
     337        global $wpdb, $bp;
     338
     339        // Backward compatibility with old method of passing arguments
     340        if ( ! is_array( $args ) || func_num_args() > 1 ) {
     341            _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__ ) );
     342
     343            $old_args_keys = array(
     344                0 => 'user_id',
     345                1 => 'box',
     346                2 => 'type',
     347                3 => 'limit',
     348                4 => 'page',
     349                5 => 'search_terms',
     350            );
     351
     352            $func_args = func_get_args();
     353            $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
     354        }
     355
     356        $defaults = array(
     357            'user_id'      => false,
     358            'box'          => 'inbox',
     359            'type'         => 'all',
     360            'limit'        => null,
     361            'page'         => null,
     362            'search_terms' => ''
     363        );
     364        $r = wp_parse_args( $args, $defaults );
    334365
    335366        $user_id_sql = $pag_sql = $type_sql = $search_sql = '';
    336367
    337         if ( $limit && $page ) {
    338             $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    339         }
    340 
    341         if ( $type == 'unread' ) {
     368        if ( $r['limit'] && $r['page'] ) {
     369            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) );
     370        }
     371
     372        if ( $r['type'] == 'unread' ) {
    342373            $type_sql = " AND r.unread_count != 0 ";
    343         } elseif ( $type == 'read' ) {
     374        } elseif ( $r['type'] == 'read' ) {
    344375            $type_sql = " AND r.unread_count = 0 ";
    345376        }
    346377
    347         if ( ! empty( $search_terms ) ) {
    348             $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%';
     378        if ( ! empty( $r['search_terms'] ) ) {
     379            $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
    349380            $search_sql        = $wpdb->prepare( "AND ( subject LIKE %s OR message LIKE %s )", $search_terms_like, $search_terms_like );
    350381        }
    351382
    352         if ( 'sentbox' == $box ) {
    353             $user_id_sql = $wpdb->prepare( 'm.sender_id = %d', $user_id );
     383        if ( 'sentbox' == $r['box'] ) {
     384            $user_id_sql = $wpdb->prepare( 'm.sender_id = %d', $r['user_id'] );
    354385            $thread_ids  = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND {$user_id_sql} AND r.is_deleted = 0 {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
    355386            $total_threads = $wpdb->get_var( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND {$user_id_sql} AND r.is_deleted = 0 {$search_sql} " );
    356387        } else {
    357             $user_id_sql = $wpdb->prepare( 'r.user_id = %d', $user_id );
     388            $user_id_sql = $wpdb->prepare( 'r.user_id = %d', $r['user_id'] );
    358389            $thread_ids = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND {$user_id_sql} AND r.sender_only = 0 {$type_sql} {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
    359390            $total_threads = $wpdb->get_var( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND {$user_id_sql} AND r.sender_only = 0 {$type_sql} {$search_sql}" );
  • trunk/src/bp-messages/bp-messages-template.php

    r9260 r9301  
    115115     * Constructor method.
    116116     *
    117      * @param int $user_id ID of the user whose Messages box is being
    118      *        viewed.
    119      * @param string $box Type of box being viewed ('notices', 'sentbox',
    120      *        'inbox').
    121      * @param int $per_page Number of thread to return per page of results.
    122      * @param int $max Max number of results to return.
    123      * @param string $type Type of results to return. 'unread', 'read',
    124      *        or 'all'.
    125      * @param string $search_terms Search terms for limiting results.
    126      * @param string $page_arg Optional. URL argument for pagination
    127      *        parameter. Default: 'mpage'.
    128      */
    129     public function __construct( $user_id, $box, $per_page, $max, $type, $search_terms, $page_arg = 'mpage' ) {
    130         $this->pag_page = isset( $_GET[$page_arg] ) ? intval( $_GET[$page_arg] ) : 1;
    131         $this->pag_num  = isset( $_GET['num'] )   ? intval( $_GET['num'] )   : $per_page;
    132 
    133         $this->user_id      = $user_id;
    134         $this->box          = $box;
    135         $this->type         = $type;
    136         $this->search_terms = $search_terms;
     117     * @param array $args {
     118     *     Array of arguments. See bp_has_message_threads() for full description.
     119     * }
     120     */
     121    public function __construct( $args = array() ) {
     122        global $wpdb, $bp;
     123
     124        // Backward compatibility with old method of passing arguments
     125        if ( ! is_array( $args ) || func_num_args() > 1 ) {
     126            _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__ ) );
     127
     128            $old_args_keys = array(
     129                0 => 'user_id',
     130                1 => 'box',
     131                2 => 'per_page',
     132                3 => 'max',
     133                4 => 'type',
     134                5 => 'search_terms',
     135                6 => 'page_arg'
     136            );
     137
     138            $func_args = func_get_args();
     139            $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
     140        }
     141
     142        $r = wp_parse_args( $args, array(
     143            'user_id'  => bp_loggedin_user_id(),
     144            'box'      => 'inbox',
     145            'per_page' => 10,
     146            'max'      => false,
     147            'type'     => 'all',
     148            'search_terms' => '',
     149            'page_arg'     => 'mpage',
     150            'meta_query'   => array(),
     151        ) );
     152
     153        $this->pag_page = isset( $_GET[ $r['page_arg'] ] ) ? intval( $_GET[ $r['page_arg'] ] ) : 1;
     154        $this->pag_num  = isset( $_GET['num'] ) ? intval( $_GET['num'] ) : $r['per_page'];
     155
     156        $this->user_id      = $r['user_id'];
     157        $this->box          = $r['box'];
     158        $this->type         = $r['type'];
     159        $this->search_terms = $r['search_terms'];
    137160
    138161        if ( 'notices' == $this->box ) {
     
    142165            ) );
    143166        } else {
    144             $threads = BP_Messages_Thread::get_current_threads_for_user( $this->user_id, $this->box, $this->type, $this->pag_num, $this->pag_page, $this->search_terms );
     167            $threads = BP_Messages_Thread::get_current_threads_for_user( array(
     168                'user_id' => $this->user_id,
     169                'box'     => $this->box,
     170                'type'    => $this->type,
     171                'limit'   => $this->pag_num,
     172                'page'    => $this->pag_page,
     173                'search_terms' => $this->search_terms,
     174                'meta_query'   => $r['meta_query'],
     175            ) );
    145176
    146177            $this->threads            = $threads['threads'];
     
    154185            $total_notice_count = BP_Messages_Notice::get_total_notice_count();
    155186
    156             if ( !$max || $max >= (int) $total_notice_count ) {
     187            if ( ! $r['max'] || $r['max'] >= (int) $total_notice_count ) {
    157188                if ( 'notices' == $this->box ) {
    158189                    $this->total_thread_count = (int) $total_notice_count;
    159190                }
    160191            } else {
    161                 $this->total_thread_count = (int) $max;
     192                $this->total_thread_count = (int) $r['max'];
    162193            }
    163194
    164             if ( $max ) {
     195            if ( $r['max'] ) {
    165196                if ( $max >= count( $this->threads ) ) {
    166197                    $this->thread_count = count( $this->threads );
    167198                } else {
    168                     $this->thread_count = (int) $max;
     199                    $this->thread_count = (int) $r['max'];
    169200                }
    170201            } else {
     
    175206        if ( (int) $this->total_thread_count && (int) $this->pag_num ) {
    176207            $pag_args = array(
    177                 $page_arg => '%#%',
     208                $r['page_arg'] => '%#%',
    178209            );
    179210
     
    383414
    384415    // Load the messages loop global up with messages
    385     $messages_template = new BP_Messages_Box_Template(
    386         $r['user_id'],
    387         $r['box'],
    388         $r['per_page'],
    389         $r['max'],
    390         $r['type'],
    391         $r['search_terms'],
    392         $r['page_arg']
    393     );
     416    $messages_template = new BP_Messages_Box_Template( $r );
    394417
    395418    /**
Note: See TracChangeset for help on using the changeset viewer.