Ticket #6063: 6063-assoc-array-args.2.diff
File 6063-assoc-array-args.2.diff, 10.2 KB (added by , 10 years ago) |
---|
-
src/bp-messages/bp-messages-classes.php
diff --git src/bp-messages/bp-messages-classes.php src/bp-messages/bp-messages-classes.php index 1db2769..6b3bea6 100644
class BP_Messages_Thread { 319 319 * 320 320 * @since BuddyPress (1.0.0) 321 321 * 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 * } 330 334 * @return array|bool Array on success. Boolean false on failure. 331 335 */ 332 public static function get_current_threads_for_user( $ user_id, $box = 'inbox', $type = 'all', $limit = null, $page = null, $search_terms = '') {336 public static function get_current_threads_for_user( $args = array() ) { 333 337 global $wpdb, $bp; 334 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 ); 365 335 366 $user_id_sql = $pag_sql = $type_sql = $search_sql = ''; 336 367 337 if ( $ limit && $page) {338 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $ page - 1 ) * $limit), intval( $limit) );368 if ( $r['limit'] && $r['page'] ) { 369 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['limit'] ), intval( $r['limit'] ) ); 339 370 } 340 371 341 if ( $ type== 'unread' ) {372 if ( $r['type'] == 'unread' ) { 342 373 $type_sql = " AND r.unread_count != 0 "; 343 } elseif ( $ type== 'read' ) {374 } elseif ( $r['type'] == 'read' ) { 344 375 $type_sql = " AND r.unread_count = 0 "; 345 376 } 346 377 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'] ) . '%'; 349 380 $search_sql = $wpdb->prepare( "AND ( subject LIKE %s OR message LIKE %s )", $search_terms_like, $search_terms_like ); 350 381 } 351 382 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'] ); 354 385 $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}" ); 355 386 $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} " ); 356 387 } 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'] ); 358 389 $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}" ); 359 390 $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}" ); 360 391 } -
src/bp-messages/bp-messages-template.php
diff --git src/bp-messages/bp-messages-template.php src/bp-messages/bp-messages-template.php index fe9e1bd..6d25c0e 100644
class BP_Messages_Box_Template { 114 114 /** 115 115 * Constructor method. 116 116 * 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']; 137 160 138 161 if ( 'notices' == $this->box ) { 139 162 $this->threads = BP_Messages_Notice::get_notices( array( … … class BP_Messages_Box_Template { 141 164 'pag_page' => $this->pag_page 142 165 ) ); 143 166 } 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 ) ); 145 176 146 177 $this->threads = $threads['threads']; 147 178 $this->total_thread_count = $threads['total']; … … class BP_Messages_Box_Template { 153 184 } else { 154 185 $total_notice_count = BP_Messages_Notice::get_total_notice_count(); 155 186 156 if ( ! $max || $max>= (int) $total_notice_count ) {187 if ( ! $r['max'] || $r['max'] >= (int) $total_notice_count ) { 157 188 if ( 'notices' == $this->box ) { 158 189 $this->total_thread_count = (int) $total_notice_count; 159 190 } 160 191 } else { 161 $this->total_thread_count = (int) $ max;192 $this->total_thread_count = (int) $r['max']; 162 193 } 163 194 164 if ( $ max) {195 if ( $r['max'] ) { 165 196 if ( $max >= count( $this->threads ) ) { 166 197 $this->thread_count = count( $this->threads ); 167 198 } else { 168 $this->thread_count = (int) $ max;199 $this->thread_count = (int) $r['max']; 169 200 } 170 201 } else { 171 202 $this->thread_count = count( $this->threads ); … … class BP_Messages_Box_Template { 174 205 175 206 if ( (int) $this->total_thread_count && (int) $this->pag_num ) { 176 207 $pag_args = array( 177 $ page_arg=> '%#%',208 $r['page_arg'] => '%#%', 178 209 ); 179 210 180 211 if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { … … function bp_has_message_threads( $args = '' ) { 382 413 } 383 414 384 415 // 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 ); 394 417 395 418 /** 396 419 * Filters if there are any message threads to display in inbox/sentbox/notices.