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 | } |