Skip to:
Content

BuddyPress.org

Changeset 10522


Ignore:
Timestamp:
02/05/2016 05:22:55 AM (8 years ago)
Author:
boonebgorges
Message:

Move bp-messages classes to their own files.

See #6870.

Location:
trunk/src/bp-messages
Files:
3 edited
4 copied

Legend:

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

    r10417 r10522  
    1313defined( 'ABSPATH' ) || exit;
    1414
    15 /**
    16  * Implementation of BP_Component for the Messages component.
    17  *
    18  * @since 1.5.0
    19  */
    20 class BP_Messages_Component extends BP_Component {
    21 
    22     /**
    23      * If this is true, the Message autocomplete will return friends only, unless
    24      * this is set to false, in which any matching users will be returned.
    25      *
    26      * @since 1.5.0
    27      * @var bool
    28      */
    29     public $autocomplete_all;
    30 
    31     /**
    32      * Start the messages component creation process.
    33      *
    34      * @since 1.5.0
    35      */
    36     public function __construct() {
    37         parent::start(
    38             'messages',
    39             __( 'Private Messages', 'buddypress' ),
    40             buddypress()->plugin_dir,
    41             array(
    42                 'adminbar_myaccount_order' => 50,
    43                 'features'                 => array( 'star' )
    44             )
    45         );
    46     }
    47 
    48     /**
    49      * Include files.
    50      *
    51      * @since 1.5.0
    52      *
    53      * @param array $includes See {BP_Component::includes()} for details.
    54      */
    55     public function includes( $includes = array() ) {
    56 
    57         // Files to include.
    58         $includes = array(
    59             'cssjs',
    60             'cache',
    61             'actions',
    62             'screens',
    63             'classes',
    64             'filters',
    65             'template',
    66             'functions',
    67             'notifications',
    68             'widgets',
    69         );
    70 
    71         // Conditional includes.
    72         if ( bp_is_active( $this->id, 'star' ) ) {
    73             $includes[] = 'star';
    74         }
    75 
    76         parent::includes( $includes );
    77     }
    78 
    79     /**
    80      * Set up globals for the Messages component.
    81      *
    82      * The BP_MESSAGES_SLUG constant is deprecated, and only used here for
    83      * backwards compatibility.
    84      *
    85      * @since 1.5.0
    86      *
    87      * @param array $args Not used.
    88      */
    89     public function setup_globals( $args = array() ) {
    90         $bp = buddypress();
    91 
    92         // Define a slug, if necessary.
    93         if ( ! defined( 'BP_MESSAGES_SLUG' ) ) {
    94             define( 'BP_MESSAGES_SLUG', $this->id );
    95         }
    96 
    97         // Global tables for messaging component.
    98         $global_tables = array(
    99             'table_name_notices'    => $bp->table_prefix . 'bp_messages_notices',
    100             'table_name_messages'   => $bp->table_prefix . 'bp_messages_messages',
    101             'table_name_recipients' => $bp->table_prefix . 'bp_messages_recipients',
    102             'table_name_meta'       => $bp->table_prefix . 'bp_messages_meta',
    103         );
    104 
    105         // Metadata tables for messaging component.
    106         $meta_tables = array(
    107             'message' => $bp->table_prefix . 'bp_messages_meta',
    108         );
    109 
    110         $this->autocomplete_all = defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' );
    111 
    112         // All globals for messaging component.
    113         // Note that global_tables is included in this array.
    114         parent::setup_globals( array(
    115             'slug'                  => BP_MESSAGES_SLUG,
    116             'has_directory'         => false,
    117             'notification_callback' => 'messages_format_notifications',
    118             'search_string'         => __( 'Search Messages...', 'buddypress' ),
    119             'global_tables'         => $global_tables,
    120             'meta_tables'           => $meta_tables
    121         ) );
    122     }
    123 
    124     /**
    125      * Set up navigation for user pages.
    126      *
    127      * @param array $main_nav See {BP_Component::setup_nav()} for details.
    128      * @param array $sub_nav  See {BP_Component::setup_nav()} for details.
    129      */
    130     public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    131 
    132         // Determine user to use.
    133         if ( bp_displayed_user_domain() ) {
    134             $user_domain = bp_displayed_user_domain();
    135         } elseif ( bp_loggedin_user_domain() ) {
    136             $user_domain = bp_loggedin_user_domain();
    137         } else {
    138             return;
    139         }
    140 
    141         $access        = bp_core_can_edit_settings();
    142         $slug          = bp_get_messages_slug();
    143         $messages_link = trailingslashit( $user_domain . $slug );
    144 
    145         // Only grab count if we're on a user page and current user has access.
    146         if ( bp_is_user() && bp_user_has_access() ) {
    147             $count    = bp_get_total_unread_messages_count();
    148             $class    = ( 0 === $count ) ? 'no-count' : 'count';
    149             $nav_name = sprintf( __( 'Messages <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), bp_core_number_format( $count ) );
    150         } else {
    151             $nav_name = __( 'Messages', 'buddypress' );
    152         }
    153 
    154         // Add 'Messages' to the main navigation.
    155         $main_nav = array(
    156             'name'                    => $nav_name,
    157             'slug'                    => $slug,
    158             'position'                => 50,
    159             'show_for_displayed_user' => $access,
    160             'screen_function'         => 'messages_screen_inbox',
    161             'default_subnav_slug'     => 'inbox',
    162             'item_css_id'             => $this->id
    163         );
    164 
    165         // Add the subnav items to the profile.
    166         $sub_nav[] = array(
    167             'name'            => __( 'Inbox', 'buddypress' ),
    168             'slug'            => 'inbox',
    169             'parent_url'      => $messages_link,
    170             'parent_slug'     => $slug,
    171             'screen_function' => 'messages_screen_inbox',
    172             'position'        => 10,
    173             'user_has_access' => $access
    174         );
    175 
    176         if ( bp_is_active( $this->id, 'star' ) ) {
    177             $sub_nav[] = array(
    178                 'name'            => __( 'Starred', 'buddypress' ),
    179                 'slug'            => bp_get_messages_starred_slug(),
    180                 'parent_url'      => $messages_link,
    181                 'parent_slug'     => $slug,
    182                 'screen_function' => 'bp_messages_star_screen',
    183                 'position'        => 11,
    184                 'user_has_access' => $access
    185             );
    186         }
    187 
    188         $sub_nav[] = array(
    189             'name'            => __( 'Sent', 'buddypress' ),
    190             'slug'            => 'sentbox',
    191             'parent_url'      => $messages_link,
    192             'parent_slug'     => $slug,
    193             'screen_function' => 'messages_screen_sentbox',
    194             'position'        => 20,
    195             'user_has_access' => $access
    196         );
    197 
    198         $sub_nav[] = array(
    199             'name'            => __( 'Compose', 'buddypress' ),
    200             'slug'            => 'compose',
    201             'parent_url'      => $messages_link,
    202             'parent_slug'     => $slug,
    203             'screen_function' => 'messages_screen_compose',
    204             'position'        => 30,
    205             'user_has_access' => $access
    206         );
    207 
    208         if ( bp_current_user_can( 'bp_moderate' ) ) {
    209             $sub_nav[] = array(
    210                 'name'            => __( 'Notices', 'buddypress' ),
    211                 'slug'            => 'notices',
    212                 'parent_url'      => $messages_link,
    213                 'parent_slug'     => $slug,
    214                 'screen_function' => 'messages_screen_notices',
    215                 'position'        => 90,
    216                 'user_has_access' => true
    217             );
    218         }
    219 
    220         parent::setup_nav( $main_nav, $sub_nav );
    221     }
    222 
    223     /**
    224      * Set up the Toolbar.
    225      *
    226      * @param array $wp_admin_nav See {BP_Component::setup_admin_bar()} for details.
    227      */
    228     public function setup_admin_bar( $wp_admin_nav = array() ) {
    229 
    230         // Menus for logged in user.
    231         if ( is_user_logged_in() ) {
    232 
    233             // Setup the logged in user variables.
    234             $messages_link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
    235 
    236             // Unread message count.
    237             $count = messages_get_unread_count();
    238             if ( !empty( $count ) ) {
    239                 $title = sprintf( __( 'Messages <span class="count">%s</span>', 'buddypress' ), bp_core_number_format( $count ) );
    240                 $inbox = sprintf( __( 'Inbox <span class="count">%s</span>',    'buddypress' ), bp_core_number_format( $count ) );
    241             } else {
    242                 $title = __( 'Messages', 'buddypress' );
    243                 $inbox = __( 'Inbox',    'buddypress' );
    244             }
    245 
    246             // Add main Messages menu.
    247             $wp_admin_nav[] = array(
    248                 'parent' => buddypress()->my_account_menu_id,
    249                 'id'     => 'my-account-' . $this->id,
    250                 'title'  => $title,
    251                 'href'   => $messages_link
    252             );
    253 
    254             // Inbox.
    255             $wp_admin_nav[] = array(
    256                 'parent' => 'my-account-' . $this->id,
    257                 'id'     => 'my-account-' . $this->id . '-inbox',
    258                 'title'  => $inbox,
    259                 'href'   => $messages_link
    260             );
    261 
    262             // Starred.
    263             if ( bp_is_active( $this->id, 'star' ) ) {
    264                 $wp_admin_nav[] = array(
    265                     'parent' => 'my-account-' . $this->id,
    266                     'id'     => 'my-account-' . $this->id . '-starred',
    267                     'title'  => __( 'Starred', 'buddypress' ),
    268                     'href'   => trailingslashit( $messages_link . bp_get_messages_starred_slug() )
    269                 );
    270             }
    271 
    272             // Sent Messages.
    273             $wp_admin_nav[] = array(
    274                 'parent' => 'my-account-' . $this->id,
    275                 'id'     => 'my-account-' . $this->id . '-sentbox',
    276                 'title'  => __( 'Sent', 'buddypress' ),
    277                 'href'   => trailingslashit( $messages_link . 'sentbox' )
    278             );
    279 
    280             // Compose Message.
    281             $wp_admin_nav[] = array(
    282                 'parent' => 'my-account-' . $this->id,
    283                 'id'     => 'my-account-' . $this->id . '-compose',
    284                 'title'  => __( 'Compose', 'buddypress' ),
    285                 'href'   => trailingslashit( $messages_link . 'compose' )
    286             );
    287 
    288             // Site Wide Notices.
    289             if ( bp_current_user_can( 'bp_moderate' ) ) {
    290                 $wp_admin_nav[] = array(
    291                     'parent' => 'my-account-' . $this->id,
    292                     'id'     => 'my-account-' . $this->id . '-notices',
    293                     'title'  => __( 'All Member Notices', 'buddypress' ),
    294                     'href'   => trailingslashit( $messages_link . 'notices' )
    295                 );
    296             }
    297         }
    298 
    299         parent::setup_admin_bar( $wp_admin_nav );
    300     }
    301 
    302     /**
    303      * Set up the title for pages and <title>.
    304      */
    305     public function setup_title() {
    306 
    307         if ( bp_is_messages_component() ) {
    308             $bp = buddypress();
    309 
    310             if ( bp_is_my_profile() ) {
    311                 $bp->bp_options_title = __( 'My Messages', 'buddypress' );
    312             } else {
    313                 $bp->bp_options_avatar = bp_core_fetch_avatar( array(
    314                     'item_id' => bp_displayed_user_id(),
    315                     'type'    => 'thumb',
    316                     'alt'     => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() )
    317                 ) );
    318                 $bp->bp_options_title = bp_get_displayed_user_fullname();
    319             }
    320         }
    321 
    322         parent::setup_title();
    323     }
    324 
    325     /**
    326      * Setup cache groups
    327      *
    328      * @since 2.2.0
    329      */
    330     public function setup_cache_groups() {
    331 
    332         // Global groups.
    333         wp_cache_add_global_groups( array(
    334             'bp_messages',
    335             'bp_messages_threads',
    336             'bp_messages_unread_count',
    337             'message_meta'
    338         ) );
    339 
    340         parent::setup_cache_groups();
    341     }
    342 }
     15require dirname( __FILE__ ) . '/classes/class-bp-messages-component.php';
    34316
    34417/**
  • trunk/src/bp-messages/bp-messages-template.php

    r10421 r10522  
    1111defined( 'ABSPATH' ) || exit;
    1212
    13 /**
    14  * Message Box Template Class
    15  */
    16 class BP_Messages_Box_Template {
    17 
    18     /**
    19      * The loop iterator.
    20      *
    21      * @var int
    22      */
    23     public $current_thread = -1;
    24 
    25     /**
    26      * The number of threads returned by the paged query.
    27      *
    28      * @var int
    29      */
    30     public $current_thread_count = 0;
    31 
    32     /**
    33      * Total number of threads matching the query params.
    34      *
    35      * @var int
    36      */
    37     public $total_thread_count = 0;
    38 
    39     /**
    40      * Array of threads located by the query.
    41      *
    42      * @var array
    43      */
    44     public $threads = array();
    45 
    46     /**
    47      * The thread object currently being iterated on.
    48      *
    49      * @var object
    50      */
    51     public $thread = false;
    52 
    53     /**
    54      * A flag for whether the loop is currently being iterated.
    55      *
    56      * @var bool
    57      */
    58     public $in_the_loop = false;
    59 
    60     /**
    61      * User ID of the current inbox.
    62      *
    63      * @var int
    64      */
    65     public $user_id = 0;
    66 
    67     /**
    68      * The current "box" view ('notices', 'sentbox', 'inbox').
    69      *
    70      * @var string
    71      */
    72     public $box = 'inbox';
    73 
    74     /**
    75      * The page number being requested.
    76      *
    77      * @var int
    78      */
    79     public $pag_page = 1;
    80 
    81     /**
    82      * The number of items being requested per page.
    83      *
    84      * @var int
    85      */
    86     public $pag_num = 10;
    87 
    88     /**
    89      * An HTML string containing pagination links.
    90      *
    91      * @var string
    92      */
    93     public $pag_links = '';
    94 
    95     /**
    96      * Search terms for limiting the thread query.
    97      *
    98      * @var string
    99      */
    100     public $search_terms = '';
    101 
    102     /**
    103      * Constructor method.
    104      *
    105      * @param array $args {
    106      *     Array of arguments. See bp_has_message_threads() for full description.
    107      * }
    108      */
    109     public function __construct( $args = array() ) {
    110 
    111         // Backward compatibility with old method of passing arguments.
    112         if ( ! is_array( $args ) || func_num_args() > 1 ) {
    113             _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 
    115             $old_args_keys = array(
    116                 0 => 'user_id',
    117                 1 => 'box',
    118                 2 => 'per_page',
    119                 3 => 'max',
    120                 4 => 'type',
    121                 5 => 'search_terms',
    122                 6 => 'page_arg'
    123             );
    124 
    125             $func_args = func_get_args();
    126             $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
    127         }
    128 
    129         $r = wp_parse_args( $args, array(
    130             'page'         => 1,
    131             'per_page'     => 10,
    132             'page_arg'     => 'mpage',
    133             'box'          => 'inbox',
    134             'type'         => 'all',
    135             'user_id'      => bp_loggedin_user_id(),
    136             'max'          => false,
    137             'search_terms' => '',
    138             'meta_query'   => array(),
    139         ) );
    140 
    141         $this->pag_arg      = sanitize_key( $r['page_arg'] );
    142         $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    143         $this->pag_num      = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
    144         $this->user_id      = $r['user_id'];
    145         $this->box          = $r['box'];
    146         $this->type         = $r['type'];
    147         $this->search_terms = $r['search_terms'];
    148 
    149         if ( 'notices' === $this->box ) {
    150             $this->threads = BP_Messages_Notice::get_notices( array(
    151                 'pag_num'  => $this->pag_num,
    152                 'pag_page' => $this->pag_page
    153             ) );
    154         } else {
    155             $threads = BP_Messages_Thread::get_current_threads_for_user( array(
    156                 'user_id'      => $this->user_id,
    157                 'box'          => $this->box,
    158                 'type'         => $this->type,
    159                 'limit'        => $this->pag_num,
    160                 'page'         => $this->pag_page,
    161                 'search_terms' => $this->search_terms,
    162                 'meta_query'   => $r['meta_query'],
    163             ) );
    164 
    165             $this->threads            = $threads['threads'];
    166             $this->total_thread_count = $threads['total'];
    167         }
    168 
    169         if ( !$this->threads ) {
    170             $this->thread_count       = 0;
    171             $this->total_thread_count = 0;
    172         } else {
    173             $total_notice_count = BP_Messages_Notice::get_total_notice_count();
    174 
    175             if ( empty( $r['max'] ) || ( (int) $r['max'] >= (int) $total_notice_count ) ) {
    176                 if ( 'notices' === $this->box ) {
    177                     $this->total_thread_count = (int) $total_notice_count;
    178                 }
    179             } else {
    180                 $this->total_thread_count = (int) $r['max'];
    181             }
    182 
    183             if ( ! empty( $r['max'] ) ) {
    184                 if ( (int) $r['max'] >= count( $this->threads ) ) {
    185                     $this->thread_count = count( $this->threads );
    186                 } else {
    187                     $this->thread_count = (int) $r['max'];
    188                 }
    189             } else {
    190                 $this->thread_count = count( $this->threads );
    191             }
    192         }
    193 
    194         if ( (int) $this->total_thread_count && (int) $this->pag_num ) {
    195             $pag_args = array(
    196                 $r['page_arg'] => '%#%',
    197             );
    198 
    199             if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
    200                 $base = remove_query_arg( 's', wp_get_referer() );
    201             } else {
    202                 $base = '';
    203             }
    204 
    205             $add_args = array();
    206 
    207             if ( ! empty( $this->search_terms ) ) {
    208                 $add_args['s'] = $this->search_terms;
    209             }
    210 
    211             $this->pag_links = paginate_links( array(
    212                 'base'      => add_query_arg( $pag_args, $base ),
    213                 'format'    => '',
    214                 'total'     => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),
    215                 'current'   => $this->pag_page,
    216                 'prev_text' => _x( '&larr;', 'Message pagination previous text', 'buddypress' ),
    217                 'next_text' => _x( '&rarr;', 'Message pagination next text', 'buddypress' ),
    218                 'mid_size'  => 1,
    219                 'add_args'  => $add_args,
    220             ) );
    221         }
    222     }
    223 
    224     /**
    225      * Whether there are threads available in the loop.
    226      *
    227      * @see bp_has_message_threads()
    228      *
    229      * @return bool True if there are items in the loop, otherwise false.
    230      */
    231     public function has_threads() {
    232         if ( $this->thread_count ) {
    233             return true;
    234         }
    235 
    236         return false;
    237     }
    238 
    239     /**
    240      * Set up the next member and iterate index.
    241      *
    242      * @return object The next member to iterate over.
    243      */
    244     public function next_thread() {
    245         $this->current_thread++;
    246         $this->thread = $this->threads[$this->current_thread];
    247 
    248         return $this->thread;
    249     }
    250 
    251     /**
    252      * Rewind the threads and reset thread index.
    253      */
    254     public function rewind_threads() {
    255         $this->current_thread = -1;
    256         if ( $this->thread_count > 0 ) {
    257             $this->thread = $this->threads[0];
    258         }
    259     }
    260 
    261     /**
    262      * Whether there are threads left in the loop to iterate over.
    263      *
    264      * This method is used by {@link bp_message_threads()} as part of the
    265      * while loop that controls iteration inside the threads loop, eg:
    266      *     while ( bp_message_threads() ) { ...
    267      *
    268      * @see bp_message_threads()
    269      *
    270      * @return bool True if there are more threads to show, otherwise false.
    271      */
    272     function message_threads() {
    273         if ( $this->current_thread + 1 < $this->thread_count ) {
    274             return true;
    275         } elseif ( $this->current_thread + 1 == $this->thread_count ) {
    276 
    277             /**
    278              * Fires when at the end of threads to iterate over.
    279              *
    280              * @since 1.5.0
    281              */
    282             do_action( 'messages_box_loop_end' );
    283             // Do some cleaning up after the loop.
    284             $this->rewind_threads();
    285         }
    286 
    287         $this->in_the_loop = false;
    288         return false;
    289     }
    290 
    291     /**
    292      * Set up the current thread inside the loop.
    293      *
    294      * Used by {@link bp_message_thread()} to set up the current thread data
    295      * while looping, so that template tags used during that iteration make
    296      * reference to the current thread.
    297      *
    298      * @see bp_message_thread()
    299      */
    300     public function the_message_thread() {
    301 
    302         $this->in_the_loop = true;
    303         $this->thread      = $this->next_thread();
    304 
    305         if ( ! bp_is_current_action( 'notices' ) ) {
    306             $last_message_index     = count( $this->thread->messages ) - 1;
    307             $this->thread->messages = array_reverse( (array) $this->thread->messages );
    308 
    309             // Set up the last message data.
    310             if ( count($this->thread->messages) > 1 ) {
    311                 if ( 'inbox' == $this->box ) {
    312                     foreach ( (array) $this->thread->messages as $key => $message ) {
    313                         if ( bp_loggedin_user_id() != $message->sender_id ) {
    314                             $last_message_index = $key;
    315                             break;
    316                         }
    317                     }
    318 
    319                 } elseif ( 'sentbox' == $this->box ) {
    320                     foreach ( (array) $this->thread->messages as $key => $message ) {
    321                         if ( bp_loggedin_user_id() == $message->sender_id ) {
    322                             $last_message_index = $key;
    323                             break;
    324                         }
    325                     }
    326                 }
    327             }
    328 
    329             $this->thread->last_message_id      = $this->thread->messages[ $last_message_index ]->id;
    330             $this->thread->last_message_date    = $this->thread->messages[ $last_message_index ]->date_sent;
    331             $this->thread->last_sender_id       = $this->thread->messages[ $last_message_index ]->sender_id;
    332             $this->thread->last_message_subject = $this->thread->messages[ $last_message_index ]->subject;
    333             $this->thread->last_message_content = $this->thread->messages[ $last_message_index ]->message;
    334         }
    335 
    336         // Loop has just started.
    337         if ( 0 == $this->current_thread ) {
    338 
    339             /**
    340              * Fires if at the start of the message thread loop.
    341              *
    342              * @since 1.5.0
    343              */
    344             do_action( 'messages_box_loop_start' );
    345         }
    346     }
    347 }
     13require dirname( __FILE__ ) . '/classes/class-bp-messages-box-template.php';
     14require dirname( __FILE__ ) . '/classes/class-bp-messages-thread-template.php';
    34815
    34916/**
     
    17531420
    17541421/**
    1755  * Message Thread Template Class
    1756  */
    1757 class BP_Messages_Thread_Template {
    1758 
    1759     /**
    1760      * The loop iterator.
    1761      *
    1762      * @var int
    1763      */
    1764     public $current_message = -1;
    1765 
    1766     /**
    1767      * Number of messages returned by the paged query.
    1768      *
    1769      * @var int
    1770      */
    1771     public $message_count = 0;
    1772 
    1773     /**
    1774      * The message object currently being iterated on.
    1775      *
    1776      * @var object
    1777      */
    1778     public $message;
    1779 
    1780     /**
    1781      * Thread that the current messages belong to.
    1782      *
    1783      * @var BP_Messages_Thread
    1784      */
    1785     public $thread;
    1786 
    1787     /**
    1788      * A flag for whether the loop is currently being iterated.
    1789      *
    1790      * @var bool
    1791      */
    1792     public $in_the_loop = false;
    1793 
    1794     /**
    1795      * The page number being requested.
    1796      *
    1797      * @var int
    1798      */
    1799     public $pag_page = 1;
    1800 
    1801     /**
    1802      * The number of items being requested per page.
    1803      *
    1804      * @var int
    1805      */
    1806     public $pag_num = 10;
    1807 
    1808     /**
    1809      * An HTML string containing pagination links.
    1810      *
    1811      * @var string
    1812      */
    1813     public $pag_links = '';
    1814 
    1815     /**
    1816      * The total number of messages matching the query.
    1817      *
    1818      * @var int
    1819      */
    1820     public $total_message_count = 0;
    1821 
    1822     /**
    1823      * Constructor method.
    1824      *
    1825      * @see BP_Messages_Thread::populate() for full parameter info.
    1826      *
    1827      * @param int    $thread_id ID of the message thread to display.
    1828      * @param string $order     Order to show the thread's messages in.
    1829      * @param array  $args      Array of arguments for the query.
    1830      */
    1831     public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) {
    1832         $this->thread        = new BP_Messages_Thread( $thread_id, $order, $args );
    1833         $this->message_count = count( $this->thread->messages );
    1834     }
    1835 
    1836     /**
    1837      * Whether there are messages available in the loop.
    1838      *
    1839      * @see bp_thread_has_messages()
    1840      *
    1841      * @return bool True if there are items in the loop, otherwise false.
    1842      */
    1843     public function has_messages() {
    1844         if ( ! empty( $this->message_count ) ) {
    1845             return true;
    1846         }
    1847 
    1848         return false;
    1849     }
    1850 
    1851     /**
    1852      * Set up the next member and iterate index.
    1853      *
    1854      * @return object The next member to iterate over.
    1855      */
    1856     public function next_message() {
    1857         $this->current_message++;
    1858         $this->message = $this->thread->messages[ $this->current_message ];
    1859 
    1860         return $this->message;
    1861     }
    1862 
    1863     /**
    1864      * Rewind the messages and reset message index.
    1865      */
    1866     public function rewind_messages() {
    1867         $this->current_message = -1;
    1868         if ( $this->message_count > 0 ) {
    1869             $this->message = $this->thread->messages[0];
    1870         }
    1871     }
    1872 
    1873     /**
    1874      * Whether there are messages left in the loop to iterate over.
    1875      *
    1876      * This method is used by {@link bp_thread_messages()} as part of the
    1877      * while loop that controls iteration inside the messages loop, eg:
    1878      *     while ( bp_thread_messages() ) { ...
    1879      *
    1880      * @see bp_thread_messages()
    1881      *
    1882      * @return bool True if there are more messages to show, otherwise false.
    1883      */
    1884     public function messages() {
    1885         if ( ( $this->current_message + 1 ) < $this->message_count ) {
    1886             return true;
    1887         } elseif ( ( $this->current_message + 1 ) === $this->message_count ) {
    1888 
    1889             /**
    1890              * Fires when at the end of messages to iterate over.
    1891              *
    1892              * @since 1.1.0
    1893              */
    1894             do_action( 'thread_loop_end' );
    1895             // Do some cleaning up after the loop.
    1896             $this->rewind_messages();
    1897         }
    1898 
    1899         $this->in_the_loop = false;
    1900         return false;
    1901     }
    1902 
    1903     /**
    1904      * Set up the current message inside the loop.
    1905      *
    1906      * Used by {@link bp_thread_the_message()} to set up the current
    1907      * message data while looping, so that template tags used during
    1908      * that iteration make reference to the current message.
    1909      *
    1910      * @see bp_thread_the_message()
    1911      */
    1912     public function the_message() {
    1913         $this->in_the_loop = true;
    1914         $this->message     = $this->next_message();
    1915 
    1916         // Loop has just started.
    1917         if ( 0 === $this->current_message ) {
    1918 
    1919             /**
    1920              * Fires if at the start of the message loop.
    1921              *
    1922              * @since 1.1.0
    1923              */
    1924             do_action( 'thread_loop_start' );
    1925         }
    1926     }
    1927 }
    1928 
    1929 /**
    19301422 * Initialize the messages template loop for a specific thread.
    19311423 *
  • trunk/src/bp-messages/bp-messages-widgets.php

    r10417 r10522  
    1111defined( 'ABSPATH' ) || exit;
    1212
     13require dirname( __FILE__ ) . '/classes/class-bp-messages-sitewide-notices-widget.php';
     14
    1315/**
    1416 * Register widgets for the Messages component.
     
    2022}
    2123add_action( 'bp_register_widgets', 'bp_messages_register_widgets' );
    22 
    23 /**
    24  * A widget that displays sitewide notices.
    25  *
    26  * @since 1.9.0
    27  */
    28 class BP_Messages_Sitewide_Notices_Widget extends WP_Widget {
    29 
    30     /**
    31      * Constructor method.
    32      */
    33     function __construct() {
    34         parent::__construct(
    35             'bp_messages_sitewide_notices_widget',
    36             __( '(BuddyPress) Sitewide Notices', 'buddypress' ),
    37             array(
    38                 'classname'   => 'widget_bp_core_sitewide_messages buddypress widget',
    39                 'description' => __( 'Display Sitewide Notices posted by the site administrator', 'buddypress' ),
    40             )
    41         );
    42     }
    43 
    44     /**
    45      * Render the widget.
    46      *
    47      * @see WP_Widget::widget() for a description of parameters.
    48      *
    49      * @param array $args     See {@WP_Widget::widget()}.
    50      * @param array $instance See {@WP_Widget::widget()}.
    51      */
    52     public function widget( $args, $instance ) {
    53 
    54         if ( ! is_user_logged_in() ) {
    55             return;
    56         }
    57 
    58         // Don't display the widget if there are no Notices to show.
    59         $notices = BP_Messages_Notice::get_active();
    60         if ( empty( $notices ) ) {
    61             return;
    62         }
    63 
    64         extract( $args );
    65 
    66         $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
    67 
    68         /**
    69          * Filters the title of the Messages widget.
    70          *
    71          * @since 1.9.0
    72          * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter.
    73          *
    74          * @param string $title    The widget title.
    75          * @param array  $instance The settings for the particular instance of the widget.
    76          * @param string $id_base  Root ID for all widgets of this type.
    77          */
    78         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    79 
    80         echo $before_widget;
    81         echo $before_title . $title . $after_title; ?>
    82 
    83         <div class="bp-site-wide-message">
    84             <?php bp_message_get_notices(); ?>
    85         </div>
    86 
    87         <?php
    88 
    89         echo $after_widget;
    90     }
    91 
    92     /**
    93      * Process the saved settings for the widget.
    94      *
    95      * @see WP_Widget::update() for a description of parameters and
    96      *      return values.
    97      *
    98      * @param array $new_instance See {@WP_Widget::update()}.
    99      * @param array $old_instance See {@WP_Widget::update()}.
    100      * @return array $instance See {@WP_Widget::update()}.
    101      */
    102     public function update( $new_instance, $old_instance ) {
    103         $instance = $old_instance;
    104         $instance['title'] = strip_tags( $new_instance['title'] );
    105         return $instance;
    106     }
    107 
    108     /**
    109      * Render the settings form for Appearance > Widgets.
    110      *
    111      * @see WP_Widget::form() for a description of parameters.
    112      *
    113      * @param array $instance See {@WP_Widget::form()}.
    114      *
    115      * @return string Widget form output.
    116      */
    117     public function form( $instance ) {
    118         $instance = wp_parse_args( (array) $instance, array(
    119             'title' => '',
    120         ) );
    121 
    122         $title = strip_tags( $instance['title'] ); ?>
    123 
    124         <p>
    125             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'buddypress' ); ?></label>
    126             <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
    127         </p>
    128 
    129         <?php
    130     }
    131 }
  • trunk/src/bp-messages/classes/class-bp-messages-box-template.php

    r10515 r10522  
    11<?php
    22/**
    3  * BuddyPress Messages Template Tags.
     3 * BuddyPress Messages Box Template Class.
    44 *
    55 * @package BuddyPress
     
    346346    }
    347347}
    348 
    349 /**
    350  * Retrieve private message threads for display in inbox/sentbox/notices.
    351  *
    352  * Similar to WordPress's have_posts() function, this function is responsible
    353  * for querying the database and retrieving private messages for display inside
    354  * the theme via individual template parts for a member's inbox/sentbox/notices.
    355  *
    356  * @since 1.0.0
    357  *
    358  * @global BP_Messages_Box_Template $messages_template
    359  *
    360  * @param array|string $args {
    361  *     Array of arguments. All are optional.
    362  *     @type int    $user_id      ID of the user whose threads are being loaded.
    363  *                                Default: ID of the logged-in user.
    364  *     @type string $box          Current "box" view. If not provided here, the current
    365  *                                view will be inferred from the URL.
    366  *     @type int    $per_page     Number of results to return per page. Default: 10.
    367  *     @type int    $max          Max results to return. Default: false.
    368  *     @type string $type         Type of messages to return. Values: 'all', 'read', 'unread'
    369  *                                Default: 'all'
    370  *     @type string $search_terms Terms to which to limit results. Default:
    371  *                                the value of $_REQUEST['s'].
    372  *     @type string $page_arg     URL argument used for the pagination param.
    373  *                                Default: 'mpage'.
    374  *     @type array  $meta_query   Meta query arguments. Only applicable if $box is
    375  *                                not 'notices'. See WP_Meta_Query more details.
    376  * }
    377  * @return bool True if there are threads to display, otherwise false.
    378  */
    379 function bp_has_message_threads( $args = array() ) {
    380     global $messages_template;
    381 
    382     // The default box the user is looking at.
    383     $current_action = bp_current_action();
    384     switch ( $current_action ) {
    385         case 'sentbox' :
    386         case 'notices' :
    387         case 'inbox'   :
    388             $default_box = $current_action;
    389             break;
    390         default :
    391             $default_box = 'inbox';
    392             break;
    393     }
    394 
    395     // User ID
    396     // @todo displayed user for moderators that get this far?
    397     $user_id = bp_loggedin_user_id();
    398 
    399     // Search Terms.
    400     $search_terms = isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '';
    401 
    402     // Parse the arguments.
    403     $r = bp_parse_args( $args, array(
    404         'user_id'      => $user_id,
    405         'box'          => $default_box,
    406         'per_page'     => 10,
    407         'max'          => false,
    408         'type'         => 'all',
    409         'search_terms' => $search_terms,
    410         'page_arg'     => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679.
    411         'meta_query'   => array()
    412     ), 'has_message_threads' );
    413 
    414     // Load the messages loop global up with messages.
    415     $messages_template = new BP_Messages_Box_Template( $r );
    416 
    417     /**
    418      * Filters if there are any message threads to display in inbox/sentbox/notices.
    419      *
    420      * @since 1.1.0
    421      *
    422      * @param bool                     $value             Whether or not the message has threads.
    423      * @param BP_Messages_Box_Template $messages_template Current message box template object.
    424      * @param array                    $r                 Array of parsed arguments passed into function.
    425      */
    426     return apply_filters( 'bp_has_message_threads', $messages_template->has_threads(), $messages_template, $r );
    427 }
    428 
    429 /**
    430  * Check whether there are more threads to iterate over.
    431  *
    432  * @return bool
    433  */
    434 function bp_message_threads() {
    435     global $messages_template;
    436     return $messages_template->message_threads();
    437 }
    438 
    439 /**
    440  * Set up the current thread inside the loop.
    441  *
    442  * @return object
    443  */
    444 function bp_message_thread() {
    445     global $messages_template;
    446     return $messages_template->the_message_thread();
    447 }
    448 
    449 /**
    450  * Output the ID of the current thread in the loop.
    451  */
    452 function bp_message_thread_id() {
    453     echo bp_get_message_thread_id();
    454 }
    455     /**
    456      * Get the ID of the current thread in the loop.
    457      *
    458      * @return int
    459      */
    460     function bp_get_message_thread_id() {
    461         global $messages_template;
    462 
    463         /**
    464          * Filters the ID of the current thread in the loop.
    465          *
    466          * @since 1.0.0
    467          *
    468          * @param int $thread_id ID of the current thread in the loop.
    469          */
    470         return apply_filters( 'bp_get_message_thread_id', $messages_template->thread->thread_id );
    471     }
    472 
    473 /**
    474  * Output the subject of the current thread in the loop.
    475  */
    476 function bp_message_thread_subject() {
    477     echo bp_get_message_thread_subject();
    478 }
    479     /**
    480      * Get the subject of the current thread in the loop.
    481      *
    482      * @return string
    483      */
    484     function bp_get_message_thread_subject() {
    485         global $messages_template;
    486 
    487         /**
    488          * Filters the subject of the current thread in the loop.
    489          *
    490          * @since 1.1.0
    491          *
    492          * @param string $value Subject of the current thread in the loop.
    493          */
    494         return apply_filters( 'bp_get_message_thread_subject', $messages_template->thread->last_message_subject );
    495     }
    496 
    497 /**
    498  * Output an excerpt from the current message in the loop.
    499  */
    500 function bp_message_thread_excerpt() {
    501     echo bp_get_message_thread_excerpt();
    502 }
    503     /**
    504      * Generate an excerpt from the current message in the loop.
    505      *
    506      * @return string
    507      */
    508     function bp_get_message_thread_excerpt() {
    509         global $messages_template;
    510 
    511         /**
    512          * Filters the excerpt of the current thread in the loop.
    513          *
    514          * @since 1.0.0
    515          *
    516          * @param string $value Excerpt of the current thread in the loop.
    517          */
    518         return apply_filters( 'bp_get_message_thread_excerpt', strip_tags( bp_create_excerpt( $messages_template->thread->last_message_content, 75 ) ) );
    519     }
    520 
    521 /**
    522  * Output the thread's last message content.
    523  *
    524  * When viewing your Inbox, the last message is the most recent message in
    525  * the thread of which you are *not* the author.
    526  *
    527  * When viewing your Sentbox, last message is the most recent message in
    528  * the thread of which you *are* the member.
    529  *
    530  * @since 2.0.0
    531  */
    532 function bp_message_thread_content() {
    533     echo bp_get_message_thread_content();
    534 }
    535     /**
    536      * Return the thread's last message content.
    537      *
    538      * When viewing your Inbox, the last message is the most recent message in
    539      * the thread of which you are *not* the author.
    540      *
    541      * When viewing your Sentbox, last message is the most recent message in
    542      * the thread of which you *are* the member.
    543      *
    544      * @since 2.0.0
    545      *
    546      * @return string The raw content of the last message in the thread.
    547      */
    548     function bp_get_message_thread_content() {
    549         global $messages_template;
    550 
    551         /**
    552          * Filters the content of the last message in the thread.
    553          *
    554          * @since 2.0.0
    555          *
    556          * @param string $last_message_content Content of the last message in the thread.
    557          */
    558         return apply_filters( 'bp_get_message_thread_content', $messages_template->thread->last_message_content );
    559     }
    560 
    561 /**
    562  * Output a link to the page of the current thread's last author.
    563  */
    564 function bp_message_thread_from() {
    565     echo bp_get_message_thread_from();
    566 }
    567     /**
    568      * Get a link to the page of the current thread's last author.
    569      *
    570      * @return string
    571      */
    572     function bp_get_message_thread_from() {
    573         global $messages_template;
    574 
    575         /**
    576          * Filters the link to the page of the current thread's last author.
    577          *
    578          * @since 1.0.0
    579          *
    580          * @param string $value Link to the page of the current thread's last author.
    581          */
    582         return apply_filters( 'bp_get_message_thread_from', bp_core_get_userlink( $messages_template->thread->last_sender_id ) );
    583     }
    584 
    585 /**
    586  * Output links to the pages of the current thread's recipients.
    587  */
    588 function bp_message_thread_to() {
    589     echo bp_get_message_thread_to();
    590 }
    591     /**
    592      * Generate HTML links to the pages of the current thread's recipients.
    593      *
    594      * @return string
    595      */
    596     function bp_get_message_thread_to() {
    597         global $messages_template;
    598 
    599         /**
    600          * Filters the HTML links to the pages of the current thread's recipients.
    601          *
    602          * @since 1.0.0
    603          *
    604          * @param string $value HTML links to the pages of the current thread's recipients.
    605          */
    606         return apply_filters( 'bp_message_thread_to', BP_Messages_Thread::get_recipient_links($messages_template->thread->recipients ) );
    607     }
    608 
    609 /**
    610  * Output the permalink for a particular thread.
    611  *
    612  * @param int $thread_id Optional. ID of the thread. Default: current thread
    613  *                       being iterated on in the loop.
    614  */
    615 function bp_message_thread_view_link( $thread_id = 0 ) {
    616     echo bp_get_message_thread_view_link( $thread_id );
    617 }
    618     /**
    619      * Get the permalink of a particular thread.
    620      *
    621      * @param int $thread_id Optional. ID of the thread. Default: current
    622      *                       thread being iterated on in the loop.
    623      * @return string
    624      */
    625     function bp_get_message_thread_view_link( $thread_id = 0 ) {
    626         global $messages_template;
    627 
    628         if ( empty( $messages_template ) && (int) $thread_id > 0 ) {
    629             $thread_id = (int) $thread_id;
    630         } elseif ( ! empty( $messages_template->thread->thread_id ) ) {
    631             $thread_id = $messages_template->thread->thread_id;
    632         }
    633 
    634         /**
    635          * Filters the permalink of a particular thread.
    636          *
    637          * @since 1.0.0
    638          *
    639          * @param string $value permalink of a particular thread.
    640          */
    641         return apply_filters( 'bp_get_message_thread_view_link', trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id ) );
    642     }
    643 
    644 /**
    645  * Output the URL for deleting the current thread.
    646  */
    647 function bp_message_thread_delete_link() {
    648     echo esc_url( bp_get_message_thread_delete_link() );
    649 }
    650     /**
    651      * Generate the URL for deleting the current thread.
    652      *
    653      * @return string
    654      */
    655     function bp_get_message_thread_delete_link() {
    656         global $messages_template;
    657 
    658         /**
    659          * Filters the URL for deleting the current thread.
    660          *
    661          * @since 1.0.0
    662          *
    663          * @param string $value URL for deleting the current thread.
    664          * @param string $value Text indicating action being executed.
    665          */
    666         return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/delete/' . $messages_template->thread->thread_id ), 'messages_delete_thread' ) );
    667     }
    668 
    669 /**
    670  * Output the URL used for marking a single message thread as unread.
    671  *
    672  * Since this function directly outputs a URL, it is escaped.
    673  *
    674  * @since 2.2.0
    675  */
    676 function bp_the_message_thread_mark_unread_url() {
    677     echo esc_url( bp_get_the_message_thread_mark_unread_url() );
    678 }
    679     /**
    680      * Return the URL used for marking a single message thread as unread.
    681      *
    682      * @since 2.2.0
    683      *
    684      * @return string
    685      */
    686     function bp_get_the_message_thread_mark_unread_url() {
    687 
    688         // Get the message ID.
    689         $id = bp_get_message_thread_id();
    690 
    691         // Get the args to add to the URL.
    692         $args = array(
    693             'action'     => 'unread',
    694             'message_id' => $id
    695         );
    696 
    697         // Base unread URL.
    698         $url = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/unread' );
    699 
    700         // Add the args to the URL.
    701         $url = add_query_arg( $args, $url );
    702 
    703         // Add the nonce.
    704         $url = wp_nonce_url( $url, 'bp_message_thread_mark_unread_' . $id );
    705 
    706         /**
    707          * Filters the URL used for marking a single message thread as unread.
    708          *
    709          * @since 2.2.0
    710          *
    711          * @param string $url URL used for marking a single message thread as unread.
    712          */
    713         return apply_filters( 'bp_get_the_message_thread_mark_unread_url', $url );
    714     }
    715 
    716 /**
    717  * Output the URL used for marking a single message thread as read.
    718  *
    719  * Since this function directly outputs a URL, it is escaped.
    720  *
    721  * @since 2.2.0
    722  */
    723 function bp_the_message_thread_mark_read_url() {
    724     echo esc_url( bp_get_the_message_thread_mark_read_url() );
    725 }
    726     /**
    727      * Return the URL used for marking a single message thread as read.
    728      *
    729      * @since 2.2.0
    730      *
    731      * @return string
    732      */
    733     function bp_get_the_message_thread_mark_read_url() {
    734 
    735         // Get the message ID.
    736         $id = bp_get_message_thread_id();
    737 
    738         // Get the args to add to the URL.
    739         $args = array(
    740             'action'     => 'read',
    741             'message_id' => $id
    742         );
    743 
    744         // Base read URL.
    745         $url = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/read' );
    746 
    747         // Add the args to the URL.
    748         $url = add_query_arg( $args, $url );
    749 
    750         // Add the nonce.
    751         $url = wp_nonce_url( $url, 'bp_message_thread_mark_read_' . $id );
    752 
    753         /**
    754          * Filters the URL used for marking a single message thread as read.
    755          *
    756          * @since 2.2.0
    757          *
    758          * @param string $url URL used for marking a single message thread as read.
    759          */
    760         return apply_filters( 'bp_get_the_message_thread_mark_read_url', $url );
    761     }
    762 
    763 /**
    764  * Output the CSS class for the current thread.
    765  */
    766 function bp_message_css_class() {
    767     echo esc_attr( bp_get_message_css_class() );
    768 }
    769     /**
    770      * Generate the CSS class for the current thread.
    771      *
    772      * @return string
    773      */
    774     function bp_get_message_css_class() {
    775         global $messages_template;
    776 
    777         $class = false;
    778 
    779         if ( $messages_template->current_thread % 2 == 1 ) {
    780             $class .= 'alt';
    781         }
    782 
    783         /**
    784          * Filters the CSS class for the current thread.
    785          *
    786          * @since 1.2.10
    787          *
    788          * @param string $class Class string to be added to the list of classes.
    789          */
    790         return apply_filters( 'bp_get_message_css_class', trim( $class ) );
    791     }
    792 
    793 /**
    794  * Check whether the current thread has unread items.
    795  *
    796  * @return bool True if there are unread items, otherwise false.
    797  */
    798 function bp_message_thread_has_unread() {
    799     global $messages_template;
    800 
    801     $retval = ! empty( $messages_template->thread->unread_count )
    802         ? true
    803         : false;
    804 
    805     /**
    806      * Filters whether or not a message thread has unread items.
    807      *
    808      * @since 2.1.0
    809      *
    810      * @param bool $retval Whether or not a message thread has unread items.
    811      */
    812     return apply_filters( 'bp_message_thread_has_unread', $retval );
    813 }
    814 
    815 /**
    816  * Output the current thread's unread count.
    817  */
    818 function bp_message_thread_unread_count() {
    819     echo esc_html( bp_get_message_thread_unread_count() );
    820 }
    821     /**
    822      * Get the current thread's unread count.
    823      *
    824      * @return int
    825      */
    826     function bp_get_message_thread_unread_count() {
    827         global $messages_template;
    828 
    829         $count = ! empty( $messages_template->thread->unread_count )
    830             ? (int) $messages_template->thread->unread_count
    831             : false;
    832 
    833         /**
    834          * Filters the current thread's unread count.
    835          *
    836          * @since 1.0.0
    837          *
    838          * @param int $count Current thread unread count.
    839          */
    840         return apply_filters( 'bp_get_message_thread_unread_count', $count );
    841     }
    842 
    843 /**
    844  * Output a thread's total message count.
    845  *
    846  * @since 2.2.0
    847  *
    848  * @param int|bool $thread_id Optional. ID of the thread. Defaults to current thread ID.
    849  */
    850 function bp_message_thread_total_count( $thread_id = false ) {
    851     echo bp_get_message_thread_total_count( $thread_id );
    852 }
    853     /**
    854      * Get the current thread's total message count.
    855      *
    856      * @since 2.2.0
    857      *
    858      * @param int|bool $thread_id Optional. ID of the thread.
    859      *                            Defaults to current thread ID.
    860      * @return int
    861      */
    862     function bp_get_message_thread_total_count( $thread_id = false ) {
    863         if ( false === $thread_id ) {
    864             $thread_id = bp_get_message_thread_id();
    865         }
    866 
    867         $thread_template = new BP_Messages_Thread_Template( $thread_id, 'ASC', array(
    868             'update_meta_cache' => false
    869         ) );
    870 
    871         $count = 0;
    872         if ( ! empty( $thread_template->message_count ) ) {
    873             $count = intval( $thread_template->message_count );
    874         }
    875 
    876         /**
    877          * Filters the current thread's total message count.
    878          *
    879          * @since 2.2.0
    880          *
    881          * @param int $count Current thread total message count.
    882          */
    883         return apply_filters( 'bp_get_message_thread_total_count', $count );
    884     }
    885 
    886 /**
    887  * Output markup for the current thread's total and unread count.
    888  *
    889  * @since 2.2.0
    890  *
    891  * @param int|bool $thread_id Optional. ID of the thread. Default: current thread ID.
    892  */
    893 function bp_message_thread_total_and_unread_count( $thread_id = false ) {
    894     echo bp_get_message_thread_total_and_unread_count( $thread_id );
    895 }
    896     /**
    897      * Get markup for the current thread's total and unread count.
    898      *
    899      * @param int|bool $thread_id Optional. ID of the thread. Default: current thread ID.
    900      * @return string Markup displaying the total and unread count for the thread.
    901      */
    902     function bp_get_message_thread_total_and_unread_count( $thread_id = false ) {
    903         if ( false === $thread_id ) {
    904             $thread_id = bp_get_message_thread_id();
    905         }
    906 
    907         $total  = bp_get_message_thread_total_count( $thread_id );
    908         $unread = bp_get_message_thread_unread_count( $thread_id );
    909 
    910         return sprintf(
    911             '<span class="thread-count">(%1$s)</span> <span class="bp-screen-reader-text">%2$s</span>',
    912             number_format_i18n( $total ),
    913             sprintf( _n( '%d unread', '%d unread', $unread, 'buddypress' ), number_format_i18n( $unread ) )
    914         );
    915     }
    916 
    917 /**
    918  * Output the unformatted date of the last post in the current thread.
    919  */
    920 function bp_message_thread_last_post_date_raw() {
    921     echo bp_get_message_thread_last_post_date_raw();
    922 }
    923     /**
    924      * Get the unformatted date of the last post in the current thread.
    925      *
    926      * @return string
    927      */
    928     function bp_get_message_thread_last_post_date_raw() {
    929         global $messages_template;
    930 
    931         /**
    932          * Filters the unformatted date of the last post in the current thread.
    933          *
    934          * @since 2.1.0
    935          *
    936          * @param string $last_message_date Unformatted date of the last post in the current thread.
    937          */
    938         return apply_filters( 'bp_get_message_thread_last_message_date', $messages_template->thread->last_message_date );
    939     }
    940 
    941 /**
    942  * Output the nicely formatted date of the last post in the current thread.
    943  */
    944 function bp_message_thread_last_post_date() {
    945     echo bp_get_message_thread_last_post_date();
    946 }
    947     /**
    948      * Get the nicely formatted date of the last post in the current thread.
    949      *
    950      * @return string
    951      */
    952     function bp_get_message_thread_last_post_date() {
    953 
    954         /**
    955          * Filters the nicely formatted date of the last post in the current thread.
    956          *
    957          * @since 2.1.0
    958          *
    959          * @param string $value Formatted date of the last post in the current thread.
    960          */
    961         return apply_filters( 'bp_get_message_thread_last_post_date', bp_format_time( strtotime( bp_get_message_thread_last_post_date_raw() ) ) );
    962     }
    963 
    964 /**
    965  * Output the avatar for the last sender in the current message thread.
    966  *
    967  * @see bp_get_message_thread_avatar() for a description of arguments.
    968  *
    969  * @param array|string $args See {@link bp_get_message_thread_avatar()}.
    970  */
    971 function bp_message_thread_avatar( $args = '' ) {
    972     echo bp_get_message_thread_avatar( $args );
    973 }
    974     /**
    975      * Return the avatar for the last sender in the current message thread.
    976      *
    977      * @see bp_core_fetch_avatar() For a description of arguments and
    978      *      return values.
    979      *
    980      * @param array|string $args {
    981      *     Arguments are listed here with an explanation of their defaults.
    982      *     For more information about the arguments, see
    983      *     {@link bp_core_fetch_avatar()}.
    984      *     @type string      $type   Default: 'thumb'.
    985      *     @type int|bool    $width  Default: false.
    986      *     @type int|bool    $height Default: false.
    987      *     @type string      $class  Default: 'avatar'.
    988      *     @type string|bool $id     Default: false.
    989      *     @type string      $alt    Default: 'Profile picture of [display name]'.
    990      * }
    991      * @return string User avatar string.
    992      */
    993     function bp_get_message_thread_avatar( $args = '' ) {
    994         global $messages_template;
    995 
    996         $fullname = bp_core_get_user_displayname( $messages_template->thread->last_sender_id );
    997         $alt      = sprintf( __( 'Profile picture of %s', 'buddypress' ), $fullname );
    998 
    999         $r = bp_parse_args( $args, array(
    1000             'type'   => 'thumb',
    1001             'width'  => false,
    1002             'height' => false,
    1003             'class'  => 'avatar',
    1004             'id'     => false,
    1005             'alt'    => $alt
    1006         ) );
    1007 
    1008         /**
    1009          * Filters the avatar for the last sender in the current message thread.
    1010          *
    1011          * @since 1.0.0
    1012          *
    1013          * @param string $value User avatar string.
    1014          */
    1015         return apply_filters( 'bp_get_message_thread_avatar', bp_core_fetch_avatar( array(
    1016             'item_id' => $messages_template->thread->last_sender_id,
    1017             'type'    => $r['type'],
    1018             'alt'     => $r['alt'],
    1019             'css_id'  => $r['id'],
    1020             'class'   => $r['class'],
    1021             'width'   => $r['width'],
    1022             'height'  => $r['height'],
    1023         ) ) );
    1024     }
    1025 
    1026 /**
    1027  * Output the unread messages count for the current inbox.
    1028  */
    1029 function bp_total_unread_messages_count() {
    1030     echo bp_get_total_unread_messages_count();
    1031 }
    1032     /**
    1033      * Get the unread messages count for the current inbox.
    1034      *
    1035      * @return int
    1036      */
    1037     function bp_get_total_unread_messages_count() {
    1038 
    1039         /**
    1040          * Filters the unread messages count for the current inbox.
    1041          *
    1042          * @since 1.0.0
    1043          *
    1044          * @param int $value Unread messages count for the current inbox.
    1045          */
    1046         return apply_filters( 'bp_get_total_unread_messages_count', BP_Messages_Thread::get_inbox_count() );
    1047     }
    1048 
    1049 /**
    1050  * Output the pagination HTML for the current thread loop.
    1051  */
    1052 function bp_messages_pagination() {
    1053     echo bp_get_messages_pagination();
    1054 }
    1055     /**
    1056      * Get the pagination HTML for the current thread loop.
    1057      *
    1058      * @return string
    1059      */
    1060     function bp_get_messages_pagination() {
    1061         global $messages_template;
    1062 
    1063         /**
    1064          * Filters the pagination HTML for the current thread loop.
    1065          *
    1066          * @since 1.0.0
    1067          *
    1068          * @param int $pag_links Pagination HTML for the current thread loop.
    1069          */
    1070         return apply_filters( 'bp_get_messages_pagination', $messages_template->pag_links );
    1071     }
    1072 
    1073 /**
    1074  * Generate the "Viewing message x to y (of z messages)" string for a loop.
    1075  */
    1076 function bp_messages_pagination_count() {
    1077     global $messages_template;
    1078 
    1079     $start_num = intval( ( $messages_template->pag_page - 1 ) * $messages_template->pag_num ) + 1;
    1080     $from_num  = bp_core_number_format( $start_num );
    1081     $to_num    = bp_core_number_format( ( $start_num + ( $messages_template->pag_num - 1 ) > $messages_template->total_thread_count ) ? $messages_template->total_thread_count : $start_num + ( $messages_template->pag_num - 1 ) );
    1082     $total     = bp_core_number_format( $messages_template->total_thread_count );
    1083 
    1084     if ( 1 == $messages_template->total_thread_count ) {
    1085         $message = __( 'Viewing 1 message', 'buddypress' );
    1086     } else {
    1087         $message = sprintf( _n( 'Viewing %1$s - %2$s of %3$s message', 'Viewing %1$s - %2$s of %3$s messages', $messages_template->total_thread_count, 'buddypress' ), $from_num, $to_num, $total );
    1088     }
    1089 
    1090     echo esc_html( $message );
    1091 }
    1092 
    1093 /**
    1094  * Output the Private Message search form.
    1095  *
    1096  * @todo  Move markup to template part in: /members/single/messages/search.php
    1097  * @since 1.6.0
    1098  */
    1099 function bp_message_search_form() {
    1100 
    1101     // Get the default search text.
    1102     $default_search_value = bp_get_search_default_text( 'messages' );
    1103 
    1104     // Setup a few values based on what's being searched for.
    1105     $search_submitted     = ! empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : $default_search_value;
    1106     $search_placeholder   = ( $search_submitted === $default_search_value ) ? ' placeholder="' .  esc_attr( $search_submitted ) . '"' : '';
    1107     $search_value         = ( $search_submitted !== $default_search_value ) ? ' value="'       .  esc_attr( $search_submitted ) . '"' : '';
    1108 
    1109     // Start the output buffer, so form can be filtered.
    1110     ob_start(); ?>
    1111 
    1112     <form action="" method="get" id="search-message-form">
    1113         <label for="messages_search" class="bp-screen-reader-text"><?php esc_html_e( 'Search Messages', 'buddypress' ); ?></label>
    1114         <input type="text" name="s" id="messages_search"<?php echo $search_placeholder . $search_value; ?> />
    1115         <input type="submit" class="button" id="messages_search_submit" name="messages_search_submit" value="<?php esc_html_e( 'Search', 'buddypress' ); ?>" />
    1116     </form>
    1117 
    1118     <?php
    1119 
    1120     // Get the search form from the above output buffer.
    1121     $search_form_html = ob_get_clean();
    1122 
    1123     /**
    1124      * Filters the private message component search form.
    1125      *
    1126      * @since 2.2.0
    1127      *
    1128      * @param string $search_form_html HTML markup for the message search form.
    1129      */
    1130     echo apply_filters( 'bp_message_search_form', $search_form_html );
    1131 }
    1132 
    1133 /**
    1134  * Echo the form action for Messages HTML forms.
    1135  */
    1136 function bp_messages_form_action() {
    1137     echo esc_url( bp_get_messages_form_action() );
    1138 }
    1139     /**
    1140      * Return the form action for Messages HTML forms.
    1141      *
    1142      * @return string The form action.
    1143      */
    1144     function bp_get_messages_form_action() {
    1145 
    1146         /**
    1147          * Filters the form action for Messages HTML forms.
    1148          *
    1149          * @since 1.0.0
    1150          *
    1151          * @param string $value The form action.
    1152          */
    1153         return apply_filters( 'bp_get_messages_form_action', trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/' . bp_action_variable( 0 ) ) );
    1154     }
    1155 
    1156 /**
    1157  * Output the default username for the recipient box.
    1158  */
    1159 function bp_messages_username_value() {
    1160     echo esc_attr( bp_get_messages_username_value() );
    1161 }
    1162     /**
    1163      * Get the default username for the recipient box.
    1164      *
    1165      * @return string
    1166      */
    1167     function bp_get_messages_username_value() {
    1168         if ( isset( $_COOKIE['bp_messages_send_to'] ) ) {
    1169 
    1170             /**
    1171              * Filters the default username for the recipient box.
    1172              *
    1173              * Value passed into filter is dependent on if the 'bp_messages_send_to'
    1174              * cookie or 'r' $_GET parameter is set.
    1175              *
    1176              * @since 1.0.0
    1177              *
    1178              * @param string $value Default user name.
    1179              */
    1180             return apply_filters( 'bp_get_messages_username_value', $_COOKIE['bp_messages_send_to'] );
    1181         } elseif ( isset( $_GET['r'] ) && !isset( $_COOKIE['bp_messages_send_to'] ) ) {
    1182             /** This filter is documented in bp-messages-template.php */
    1183             return apply_filters( 'bp_get_messages_username_value', $_GET['r'] );
    1184         }
    1185     }
    1186 
    1187 /**
    1188  * Output the default value for the Subject field.
    1189  */
    1190 function bp_messages_subject_value() {
    1191     echo esc_attr( bp_get_messages_subject_value() );
    1192 }
    1193     /**
    1194      * Get the default value for the Subject field.
    1195      *
    1196      * Will get a value out of $_POST['subject'] if available (ie after a
    1197      * failed submission).
    1198      *
    1199      * @return string
    1200      */
    1201     function bp_get_messages_subject_value() {
    1202 
    1203         // Sanitized in bp-messages-filters.php.
    1204         $subject = ! empty( $_POST['subject'] )
    1205             ? $_POST['subject']
    1206             : '';
    1207 
    1208         /**
    1209          * Filters the default value for the subject field.
    1210          *
    1211          * @since 1.0.0
    1212          *
    1213          * @param string $subject The default value for the subject field.
    1214          */
    1215         return apply_filters( 'bp_get_messages_subject_value', $subject );
    1216     }
    1217 
    1218 /**
    1219  * Output the default value for the Compose content field.
    1220  */
    1221 function bp_messages_content_value() {
    1222     echo esc_textarea( bp_get_messages_content_value() );
    1223 }
    1224     /**
    1225      * Get the default value fo the Compose content field.
    1226      *
    1227      * Will get a value out of $_POST['content'] if available (ie after a
    1228      * failed submission).
    1229      *
    1230      * @return string
    1231      */
    1232     function bp_get_messages_content_value() {
    1233 
    1234         // Sanitized in bp-messages-filters.php.
    1235         $content = ! empty( $_POST['content'] )
    1236             ? $_POST['content']
    1237             : '';
    1238 
    1239         /**
    1240          * Filters the default value for the content field.
    1241          *
    1242          * @since 1.0.0
    1243          *
    1244          * @param string $content The default value for the content field.
    1245          */
    1246         return apply_filters( 'bp_get_messages_content_value', $content );
    1247     }
    1248 
    1249 /**
    1250  * Output the markup for the message type dropdown.
    1251  */
    1252 function bp_messages_options() {
    1253 ?>
    1254 
    1255     <label for="message-type-select" class="bp-screen-reader-text">
    1256         <?php _e( 'Select:', 'buddypress' ) ?>
    1257      </label>
    1258 
    1259     <select name="message-type-select" id="message-type-select">
    1260         <option value=""><?php _e( 'Select', 'buddypress' ); ?></option>
    1261         <option value="read"><?php _ex('Read', 'Message dropdown filter', 'buddypress') ?></option>
    1262         <option value="unread"><?php _ex('Unread', 'Message dropdown filter', 'buddypress') ?></option>
    1263         <option value="all"><?php _ex('All', 'Message dropdown filter', 'buddypress') ?></option>
    1264     </select> &nbsp;
    1265 
    1266     <?php if ( ! bp_is_current_action( 'sentbox' ) && ! bp_is_current_action( 'notices' ) ) : ?>
    1267 
    1268         <a href="#" id="mark_as_read"><?php _ex('Mark as Read', 'Message management markup', 'buddypress') ?></a> &nbsp;
    1269         <a href="#" id="mark_as_unread"><?php _ex('Mark as Unread', 'Message management markup', 'buddypress') ?></a> &nbsp;
    1270 
    1271     <?php endif; ?>
    1272 
    1273     <a href="#" id="delete_<?php echo bp_current_action(); ?>_messages"><?php _e( 'Delete Selected', 'buddypress' ); ?></a> &nbsp;
    1274 
    1275 <?php
    1276 }
    1277 
    1278 /**
    1279  * Output the dropdown for bulk management of messages.
    1280  *
    1281  * @since 2.2.0
    1282  */
    1283 function bp_messages_bulk_management_dropdown() {
    1284     ?>
    1285     <label class="bp-screen-reader-text" for="messages-select"><?php _e( 'Select Bulk Action', 'buddypress' ); ?></label>
    1286     <select name="messages_bulk_action" id="messages-select">
    1287         <option value="" selected="selected"><?php _e( 'Bulk Actions', 'buddypress' ); ?></option>
    1288         <option value="read"><?php _e( 'Mark read', 'buddypress' ); ?></option>
    1289         <option value="unread"><?php _e( 'Mark unread', 'buddypress' ); ?></option>
    1290         <option value="delete"><?php _e( 'Delete', 'buddypress' ); ?></option>
    1291         <?php
    1292             /**
    1293              * Action to add additional options to the messages bulk management dropdown.
    1294              *
    1295              * @since 2.3.0
    1296              */
    1297             do_action( 'bp_messages_bulk_management_dropdown' );
    1298         ?>
    1299     </select>
    1300     <input type="submit" id="messages-bulk-manage" class="button action" value="<?php esc_attr_e( 'Apply', 'buddypress' ); ?>">
    1301     <?php
    1302 }
    1303 
    1304 /**
    1305  * Return whether or not the notice is currently active.
    1306  *
    1307  * @since 1.6.0
    1308  *
    1309  * @return bool
    1310  */
    1311 function bp_messages_is_active_notice() {
    1312     global $messages_template;
    1313 
    1314     $retval = ! empty( $messages_template->thread->is_active )
    1315         ? true
    1316         : false;
    1317 
    1318     /**
    1319      * Filters whether or not the notice is currently active.
    1320      *
    1321      * @since 2.1.0
    1322      *
    1323      * @param bool $retval Whether or not the notice is currently active.
    1324      */
    1325     return apply_filters( 'bp_messages_is_active_notice', $retval );
    1326 }
    1327 
    1328 /**
    1329  * Output a string for the active notice.
    1330  *
    1331  * Since 1.6 this function has been deprecated in favor of text in the theme.
    1332  *
    1333  * @since 1.0.0
    1334  * @deprecated 1.6.0
    1335  * @uses bp_get_message_is_active_notice()
    1336  * @return bool
    1337  */
    1338 function bp_message_is_active_notice() {
    1339     echo bp_get_message_is_active_notice();
    1340 }
    1341     /**
    1342      * Returns a string for the active notice.
    1343      *
    1344      * Since 1.6 this function has been deprecated in favor of text in the
    1345      * theme.
    1346      *
    1347      * @since 1.0.0
    1348      * @deprecated 1.6.0
    1349      * @uses bp_messages_is_active_notice()
    1350      */
    1351     function bp_get_message_is_active_notice() {
    1352 
    1353         $string = bp_messages_is_active_notice()
    1354             ? __( 'Currently Active', 'buddypress' )
    1355             : '';
    1356 
    1357         return apply_filters( 'bp_get_message_is_active_notice', $string );
    1358     }
    1359 
    1360 /**
    1361  * Output the ID of the current notice in the loop.
    1362  */
    1363 function bp_message_notice_id() {
    1364     echo (int) bp_get_message_notice_id();
    1365 }
    1366     /**
    1367      * Get the ID of the current notice in the loop.
    1368      *
    1369      * @return int
    1370      */
    1371     function bp_get_message_notice_id() {
    1372         global $messages_template;
    1373 
    1374         /**
    1375          * Filters the ID of the current notice in the loop.
    1376          *
    1377          * @since 1.5.0
    1378          *
    1379          * @param int $id ID of the current notice in the loop.
    1380          */
    1381         return apply_filters( 'bp_get_message_notice_id', $messages_template->thread->id );
    1382     }
    1383 
    1384 /**
    1385  * Output the post date of the current notice in the loop.
    1386  */
    1387 function bp_message_notice_post_date() {
    1388     echo bp_get_message_notice_post_date();
    1389 }
    1390     /**
    1391      * Get the post date of the current notice in the loop.
    1392      *
    1393      * @return string
    1394      */
    1395     function bp_get_message_notice_post_date() {
    1396         global $messages_template;
    1397 
    1398         /**
    1399          * Filters the post date of the current notice in the loop.
    1400          *
    1401          * @since 1.0.0
    1402          *
    1403          * @param string $value Formatted post date of the current notice in the loop.
    1404          */
    1405         return apply_filters( 'bp_get_message_notice_post_date', bp_format_time( strtotime( $messages_template->thread->date_sent ) ) );
    1406     }
    1407 
    1408 /**
    1409  * Output the subject of the current notice in the loop.
    1410  */
    1411 function bp_message_notice_subject() {
    1412     echo bp_get_message_notice_subject();
    1413 }
    1414     /**
    1415      * Get the subject of the current notice in the loop.
    1416      *
    1417      * @return string
    1418      */
    1419     function bp_get_message_notice_subject() {
    1420         global $messages_template;
    1421 
    1422         /**
    1423          * Filters the subject of the current notice in the loop.
    1424          *
    1425          * @since 1.0.0
    1426          *
    1427          * @param string $subject Subject of the current notice in the loop.
    1428          */
    1429         return apply_filters( 'bp_get_message_notice_subject', $messages_template->thread->subject );
    1430     }
    1431 
    1432 /**
    1433  * Output the text of the current notice in the loop.
    1434  */
    1435 function bp_message_notice_text() {
    1436     echo bp_get_message_notice_text();
    1437 }
    1438     /**
    1439      * Get the text of the current notice in the loop.
    1440      *
    1441      * @return string
    1442      */
    1443     function bp_get_message_notice_text() {
    1444         global $messages_template;
    1445 
    1446         /**
    1447          * Filters the text of the current notice in the loop.
    1448          *
    1449          * @since 1.0.0
    1450          *
    1451          * @param string $message Text for the current notice in the loop.
    1452          */
    1453         return apply_filters( 'bp_get_message_notice_text', $messages_template->thread->message );
    1454     }
    1455 
    1456 /**
    1457  * Output the URL for deleting the current notice.
    1458  */
    1459 function bp_message_notice_delete_link() {
    1460     echo esc_url( bp_get_message_notice_delete_link() );
    1461 }
    1462     /**
    1463      * Get the URL for deleting the current notice.
    1464      *
    1465      * @return string Delete URL.
    1466      */
    1467     function bp_get_message_notice_delete_link() {
    1468         global $messages_template;
    1469 
    1470         /**
    1471          * Filters the URL for deleting the current notice.
    1472          *
    1473          * @since 1.0.0
    1474          *
    1475          * @param string $value URL for deleting the current notice.
    1476          * @param string $value Text indicating action being executed.
    1477          */
    1478         return apply_filters( 'bp_get_message_notice_delete_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/delete/' . $messages_template->thread->id, 'messages_delete_thread' ) );
    1479     }
    1480 
    1481 /**
    1482  * Output the URL for deactivating the current notice.
    1483  */
    1484 function bp_message_activate_deactivate_link() {
    1485     echo esc_url( bp_get_message_activate_deactivate_link() );
    1486 }
    1487     /**
    1488      * Get the URL for deactivating the current notice.
    1489      *
    1490      * @return string
    1491      */
    1492     function bp_get_message_activate_deactivate_link() {
    1493         global $messages_template;
    1494 
    1495         if ( 1 === (int) $messages_template->thread->is_active ) {
    1496             $link = wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/deactivate/' . $messages_template->thread->id ), 'messages_deactivate_notice' );
    1497         } else {
    1498             $link = wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/activate/' . $messages_template->thread->id ), 'messages_activate_notice' );
    1499         }
    1500 
    1501         /**
    1502          * Filters the URL for deactivating the current notice.
    1503          *
    1504          * @since 1.0.0
    1505          *
    1506          * @param string $link URL for deactivating the current notice.
    1507          */
    1508         return apply_filters( 'bp_get_message_activate_deactivate_link', $link );
    1509     }
    1510 
    1511 /**
    1512  * Output the Deactivate/Activate text for the notice action link.
    1513  */
    1514 function bp_message_activate_deactivate_text() {
    1515     echo esc_html( bp_get_message_activate_deactivate_text() );
    1516 }
    1517     /**
    1518      * Generate the text ('Deactivate' or 'Activate') for the notice action link.
    1519      *
    1520      * @return string
    1521      */
    1522     function bp_get_message_activate_deactivate_text() {
    1523         global $messages_template;
    1524 
    1525         if ( 1 === (int) $messages_template->thread->is_active  ) {
    1526             $text = __('Deactivate', 'buddypress');
    1527         } else {
    1528             $text = __('Activate', 'buddypress');
    1529         }
    1530 
    1531         /**
    1532          * Filters the "Deactivate" or "Activate" text for notice action links.
    1533          *
    1534          * @since 1.0.0
    1535          *
    1536          * @param string $text Text used for notice action links.
    1537          */
    1538         return apply_filters( 'bp_message_activate_deactivate_text', $text );
    1539     }
    1540 
    1541 /**
    1542  * Output the messages component slug.
    1543  *
    1544  * @since 1.5.0
    1545  *
    1546  * @uses bp_get_messages_slug()
    1547  */
    1548 function bp_messages_slug() {
    1549     echo bp_get_messages_slug();
    1550 }
    1551     /**
    1552      * Return the messages component slug.
    1553      *
    1554      * @since 1.5.0
    1555      *
    1556      * @return string
    1557      */
    1558     function bp_get_messages_slug() {
    1559 
    1560         /**
    1561          * Filters the messages component slug.
    1562          *
    1563          * @since 1.5.0
    1564          *
    1565          * @param string $slug Messages component slug.
    1566          */
    1567         return apply_filters( 'bp_get_messages_slug', buddypress()->messages->slug );
    1568     }
    1569 
    1570 /**
    1571  * Generate markup for currently active notices.
    1572  */
    1573 function bp_message_get_notices() {
    1574     $notice = BP_Messages_Notice::get_active();
    1575 
    1576     if ( empty( $notice ) ) {
    1577         return false;
    1578     }
    1579 
    1580     $closed_notices = bp_get_user_meta( bp_loggedin_user_id(), 'closed_notices', true );
    1581 
    1582     if ( empty( $closed_notices ) ) {
    1583         $closed_notices = array();
    1584     }
    1585 
    1586     if ( is_array( $closed_notices ) ) {
    1587         if ( !in_array( $notice->id, $closed_notices ) && $notice->id ) {
    1588             ?>
    1589             <div id="message" class="info notice" rel="n-<?php echo esc_attr( $notice->id ); ?>">
    1590                 <p>
    1591                     <strong><?php echo stripslashes( wp_filter_kses( $notice->subject ) ) ?></strong><br />
    1592                     <?php echo stripslashes( wp_filter_kses( $notice->message) ) ?>
    1593                     <a href="#" id="close-notice"><?php _e( 'Close', 'buddypress' ) ?></a>
    1594                 </p>
    1595             </div>
    1596             <?php
    1597         }
    1598     }
    1599 }
    1600 
    1601 /**
    1602  * Output the URL for the Private Message link in member profile headers.
    1603  */
    1604 function bp_send_private_message_link() {
    1605     echo esc_url( bp_get_send_private_message_link() );
    1606 }
    1607     /**
    1608      * Generate the URL for the Private Message link in member profile headers.
    1609      *
    1610      * @return bool|string False on failure, otherwise the URL.
    1611      */
    1612     function bp_get_send_private_message_link() {
    1613 
    1614         if ( bp_is_my_profile() || ! is_user_logged_in() ) {
    1615             return false;
    1616         }
    1617 
    1618         /**
    1619          * Filters the URL for the Private Message link in member profile headers.
    1620          *
    1621          * @since 1.2.10
    1622          *
    1623          * @param string $value URL for the Private Message link in member profile headers.
    1624          */
    1625         return apply_filters( 'bp_get_send_private_message_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . bp_core_get_username( bp_displayed_user_id() ) ) );
    1626     }
    1627 
    1628 /**
    1629  * Output the 'Private Message' button for member profile headers.
    1630  *
    1631  * Explicitly named function to avoid confusion with public messages.
    1632  *
    1633  * @since 1.2.6
    1634  *
    1635  * @uses bp_get_send_message_button()
    1636  */
    1637 function bp_send_private_message_button() {
    1638     echo bp_get_send_message_button();
    1639 }
    1640 
    1641 /**
    1642  * Output the 'Private Message' button for member profile headers.
    1643  */
    1644 function bp_send_message_button() {
    1645     echo bp_get_send_message_button();
    1646 }
    1647     /**
    1648      * Generate the 'Private Message' button for member profile headers.
    1649      *
    1650      * @return string
    1651      */
    1652     function bp_get_send_message_button() {
    1653         // Note: 'bp_get_send_message_button' is a legacy filter. Use
    1654         // 'bp_get_send_message_button_args' instead. See #4536.
    1655         return apply_filters( 'bp_get_send_message_button',
    1656 
    1657             /**
    1658              * Filters the "Private Message" button for member profile headers.
    1659              *
    1660              * @since 1.8.0
    1661              *
    1662              * @param array $value See {@link BP_Button}.
    1663              */
    1664             bp_get_button( apply_filters( 'bp_get_send_message_button_args', array(
    1665                 'id'                => 'private_message',
    1666                 'component'         => 'messages',
    1667                 'must_be_logged_in' => true,
    1668                 'block_self'        => true,
    1669                 'wrapper_id'        => 'send-private-message',
    1670                 'link_href'         => bp_get_send_private_message_link(),
    1671                 'link_title'        => __( 'Send a private message to this user.', 'buddypress' ),
    1672                 'link_text'         => __( 'Private Message', 'buddypress' ),
    1673                 'link_class'        => 'send-message',
    1674             ) ) )
    1675         );
    1676     }
    1677 
    1678 /**
    1679  * Output the URL of the Messages AJAX loader gif.
    1680  */
    1681 function bp_message_loading_image_src() {
    1682     echo esc_url( bp_get_message_loading_image_src() );
    1683 }
    1684     /**
    1685      * Get the URL of the Messages AJAX loader gif.
    1686      *
    1687      * @return string
    1688      */
    1689     function bp_get_message_loading_image_src() {
    1690 
    1691         /**
    1692          * Filters the URL of the Messages AJAX loader gif.
    1693          *
    1694          * @since 1.0.0
    1695          *
    1696          * @param string $value URL of the Messages AJAX loader gif.
    1697          */
    1698         return apply_filters( 'bp_get_message_loading_image_src', buddypress()->messages->image_base . '/ajax-loader.gif' );
    1699     }
    1700 
    1701 /**
    1702  * Output the markup for the message recipient tabs.
    1703  */
    1704 function bp_message_get_recipient_tabs() {
    1705     $recipients = explode( ' ', bp_get_message_get_recipient_usernames() );
    1706 
    1707     foreach ( $recipients as $recipient ) {
    1708 
    1709         $user_id = bp_is_username_compatibility_mode()
    1710             ? bp_core_get_userid( $recipient )
    1711             : bp_core_get_userid_from_nicename( $recipient );
    1712 
    1713         if ( ! empty( $user_id ) ) : ?>
    1714 
    1715             <li id="un-<?php echo esc_attr( $recipient ); ?>" class="friend-tab">
    1716                 <span><?php
    1717                     echo bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'width' => 15, 'height' => 15 ) );
    1718                     echo bp_core_get_userlink( $user_id );
    1719                 ?></span>
    1720             </li>
    1721 
    1722         <?php endif;
    1723     }
    1724 }
    1725 
    1726 /**
    1727  * Output recipient usernames for prefilling the 'To' field on the Compose screen.
    1728  */
    1729 function bp_message_get_recipient_usernames() {
    1730     echo esc_attr( bp_get_message_get_recipient_usernames() );
    1731 }
    1732     /**
    1733      * Get the recipient usernames for prefilling the 'To' field on the Compose screen.
    1734      *
    1735      * @return string
    1736      */
    1737     function bp_get_message_get_recipient_usernames() {
    1738 
    1739         // Sanitized in bp-messages-filters.php.
    1740         $recipients = isset( $_GET['r'] )
    1741             ? $_GET['r']
    1742             : '';
    1743 
    1744         /**
    1745          * Filters the recipients usernames for prefilling the 'To' field on the Compose screen.
    1746          *
    1747          * @since 1.0.0
    1748          *
    1749          * @param string $recipients Recipients usernames for 'To' field prefilling.
    1750          */
    1751         return apply_filters( 'bp_get_message_get_recipient_usernames', $recipients );
    1752     }
    1753 
    1754 /**
    1755  * Message Thread Template Class
    1756  */
    1757 class BP_Messages_Thread_Template {
    1758 
    1759     /**
    1760      * The loop iterator.
    1761      *
    1762      * @var int
    1763      */
    1764     public $current_message = -1;
    1765 
    1766     /**
    1767      * Number of messages returned by the paged query.
    1768      *
    1769      * @var int
    1770      */
    1771     public $message_count = 0;
    1772 
    1773     /**
    1774      * The message object currently being iterated on.
    1775      *
    1776      * @var object
    1777      */
    1778     public $message;
    1779 
    1780     /**
    1781      * Thread that the current messages belong to.
    1782      *
    1783      * @var BP_Messages_Thread
    1784      */
    1785     public $thread;
    1786 
    1787     /**
    1788      * A flag for whether the loop is currently being iterated.
    1789      *
    1790      * @var bool
    1791      */
    1792     public $in_the_loop = false;
    1793 
    1794     /**
    1795      * The page number being requested.
    1796      *
    1797      * @var int
    1798      */
    1799     public $pag_page = 1;
    1800 
    1801     /**
    1802      * The number of items being requested per page.
    1803      *
    1804      * @var int
    1805      */
    1806     public $pag_num = 10;
    1807 
    1808     /**
    1809      * An HTML string containing pagination links.
    1810      *
    1811      * @var string
    1812      */
    1813     public $pag_links = '';
    1814 
    1815     /**
    1816      * The total number of messages matching the query.
    1817      *
    1818      * @var int
    1819      */
    1820     public $total_message_count = 0;
    1821 
    1822     /**
    1823      * Constructor method.
    1824      *
    1825      * @see BP_Messages_Thread::populate() for full parameter info.
    1826      *
    1827      * @param int    $thread_id ID of the message thread to display.
    1828      * @param string $order     Order to show the thread's messages in.
    1829      * @param array  $args      Array of arguments for the query.
    1830      */
    1831     public function __construct( $thread_id = 0, $order = 'ASC', $args = array() ) {
    1832         $this->thread        = new BP_Messages_Thread( $thread_id, $order, $args );
    1833         $this->message_count = count( $this->thread->messages );
    1834     }
    1835 
    1836     /**
    1837      * Whether there are messages available in the loop.
    1838      *
    1839      * @see bp_thread_has_messages()
    1840      *
    1841      * @return bool True if there are items in the loop, otherwise false.
    1842      */
    1843     public function has_messages() {
    1844         if ( ! empty( $this->message_count ) ) {
    1845             return true;
    1846         }
    1847 
    1848         return false;
    1849     }
    1850 
    1851     /**
    1852      * Set up the next member and iterate index.
    1853      *
    1854      * @return object The next member to iterate over.
    1855      */
    1856     public function next_message() {
    1857         $this->current_message++;
    1858         $this->message = $this->thread->messages[ $this->current_message ];
    1859 
    1860         return $this->message;
    1861     }
    1862 
    1863     /**
    1864      * Rewind the messages and reset message index.
    1865      */
    1866     public function rewind_messages() {
    1867         $this->current_message = -1;
    1868         if ( $this->message_count > 0 ) {
    1869             $this->message = $this->thread->messages[0];
    1870         }
    1871     }
    1872 
    1873     /**
    1874      * Whether there are messages left in the loop to iterate over.
    1875      *
    1876      * This method is used by {@link bp_thread_messages()} as part of the
    1877      * while loop that controls iteration inside the messages loop, eg:
    1878      *     while ( bp_thread_messages() ) { ...
    1879      *
    1880      * @see bp_thread_messages()
    1881      *
    1882      * @return bool True if there are more messages to show, otherwise false.
    1883      */
    1884     public function messages() {
    1885         if ( ( $this->current_message + 1 ) < $this->message_count ) {
    1886             return true;
    1887         } elseif ( ( $this->current_message + 1 ) === $this->message_count ) {
    1888 
    1889             /**
    1890              * Fires when at the end of messages to iterate over.
    1891              *
    1892              * @since 1.1.0
    1893              */
    1894             do_action( 'thread_loop_end' );
    1895             // Do some cleaning up after the loop.
    1896             $this->rewind_messages();
    1897         }
    1898 
    1899         $this->in_the_loop = false;
    1900         return false;
    1901     }
    1902 
    1903     /**
    1904      * Set up the current message inside the loop.
    1905      *
    1906      * Used by {@link bp_thread_the_message()} to set up the current
    1907      * message data while looping, so that template tags used during
    1908      * that iteration make reference to the current message.
    1909      *
    1910      * @see bp_thread_the_message()
    1911      */
    1912     public function the_message() {
    1913         $this->in_the_loop = true;
    1914         $this->message     = $this->next_message();
    1915 
    1916         // Loop has just started.
    1917         if ( 0 === $this->current_message ) {
    1918 
    1919             /**
    1920              * Fires if at the start of the message loop.
    1921              *
    1922              * @since 1.1.0
    1923              */
    1924             do_action( 'thread_loop_start' );
    1925         }
    1926     }
    1927 }
    1928 
    1929 /**
    1930  * Initialize the messages template loop for a specific thread.
    1931  *
    1932  * @param array|string $args {
    1933  *     Array of arguments. All are optional.
    1934  *     @type int    $thread_id         ID of the thread whose messages you are displaying.
    1935  *                                     Default: if viewing a thread, the thread ID will be parsed from
    1936  *                                     the URL (bp_action_variable( 0 )).
    1937  *     @type string $order             'ASC' or 'DESC'. Default: 'ASC'.
    1938  *     @type bool   $update_meta_cache Whether to pre-fetch metadata for
    1939  *                                     queried message items. Default: true.
    1940  * }
    1941  * @return bool True if there are messages to display, otherwise false.
    1942  */
    1943 function bp_thread_has_messages( $args = '' ) {
    1944     global $thread_template;
    1945 
    1946     $r = bp_parse_args( $args, array(
    1947         'thread_id'         => false,
    1948         'order'             => 'ASC',
    1949         'update_meta_cache' => true,
    1950     ), 'thread_has_messages' );
    1951 
    1952     if ( empty( $r['thread_id'] ) && bp_is_messages_component() && bp_is_current_action( 'view' ) ) {
    1953         $r['thread_id'] = (int) bp_action_variable( 0 );
    1954     }
    1955 
    1956     // Set up extra args.
    1957     $extra_args = $r;
    1958     unset( $extra_args['thread_id'], $extra_args['order'] );
    1959 
    1960     $thread_template = new BP_Messages_Thread_Template( $r['thread_id'], $r['order'], $extra_args );
    1961 
    1962     return $thread_template->has_messages();
    1963 }
    1964 
    1965 /**
    1966  * Output the 'ASC' or 'DESC' messages order string for this loop.
    1967  */
    1968 function bp_thread_messages_order() {
    1969     echo esc_attr( bp_get_thread_messages_order() );
    1970 }
    1971     /**
    1972      * Get the 'ASC' or 'DESC' messages order string for this loop.
    1973      *
    1974      * @return string
    1975      */
    1976     function bp_get_thread_messages_order() {
    1977         global $thread_template;
    1978         return $thread_template->thread->messages_order;
    1979     }
    1980 
    1981 /**
    1982  * Check whether there are more messages to iterate over.
    1983  *
    1984  * @return bool
    1985  */
    1986 function bp_thread_messages() {
    1987     global $thread_template;
    1988 
    1989     return $thread_template->messages();
    1990 }
    1991 
    1992 /**
    1993  * Set up the current thread inside the loop.
    1994  *
    1995  * @return object
    1996  */
    1997 function bp_thread_the_message() {
    1998     global $thread_template;
    1999 
    2000     return $thread_template->the_message();
    2001 }
    2002 
    2003 /**
    2004  * Output the ID of the thread that the current loop belongs to.
    2005  */
    2006 function bp_the_thread_id() {
    2007     echo (int) bp_get_the_thread_id();
    2008 }
    2009     /**
    2010      * Get the ID of the thread that the current loop belongs to.
    2011      *
    2012      * @return int
    2013      */
    2014     function bp_get_the_thread_id() {
    2015         global $thread_template;
    2016 
    2017         /**
    2018          * Filters the ID of the thread that the current loop belongs to.
    2019          *
    2020          * @since 1.1.0
    2021          *
    2022          * @param int $thread_id ID of the thread.
    2023          */
    2024         return apply_filters( 'bp_get_the_thread_id', $thread_template->thread->thread_id );
    2025     }
    2026 
    2027 /**
    2028  * Output the subject of the thread currently being iterated over.
    2029  */
    2030 function bp_the_thread_subject() {
    2031     echo bp_get_the_thread_subject();
    2032 }
    2033     /**
    2034      * Get the subject of the thread currently being iterated over.
    2035      *
    2036      * @return string
    2037      */
    2038     function bp_get_the_thread_subject() {
    2039         global $thread_template;
    2040 
    2041         /**
    2042          * Filters the subject of the thread currently being iterated over.
    2043          *
    2044          * @since 1.1.0
    2045          *
    2046          * @return string $last_message_subject Subject of the thread currently being iterated over.
    2047          */
    2048         return apply_filters( 'bp_get_the_thread_subject', $thread_template->thread->last_message_subject );
    2049     }
    2050 
    2051 /**
    2052  * Get a list of thread recipients or a "x recipients" string.
    2053  *
    2054  * In BuddyPress 2.2.0, this parts of this functionality were moved into the
    2055  * members/single/messages/single.php template. This function is no longer used
    2056  * by BuddyPress.
    2057  *
    2058  * @return string
    2059  */
    2060 function bp_get_the_thread_recipients() {
    2061     if ( 5 <= bp_get_thread_recipients_count() ) {
    2062         $recipients = sprintf( __( '%s recipients', 'buddypress' ), number_format_i18n( bp_get_thread_recipients_count() ) );
    2063     } else {
    2064         $recipients = bp_get_thread_recipients_list();
    2065     }
    2066 
    2067     return apply_filters( 'bp_get_the_thread_recipients', $recipients );
    2068 }
    2069 
    2070 /**
    2071  * Get the number of recipients in the current thread.
    2072  *
    2073  * @since 2.2.0
    2074  *
    2075  * @return int
    2076  */
    2077 function bp_get_thread_recipients_count() {
    2078     global $thread_template;
    2079     return count( $thread_template->thread->recipients );
    2080 }
    2081 
    2082 /**
    2083  * Get the max number of recipients to list in the 'Conversation between...' gloss.
    2084  *
    2085  * @since 2.3.0
    2086  *
    2087  * @return int
    2088  */
    2089 function bp_get_max_thread_recipients_to_list() {
    2090     /**
    2091      * Filters the max number of recipients to list in the 'Conversation between...' gloss.
    2092      *
    2093      * @since 2.3.0
    2094      *
    2095      * @param int $count Recipient count. Default: 5.
    2096      */
    2097     return (int) apply_filters( 'bp_get_max_thread_recipients_to_list', 5 );
    2098 }
    2099 
    2100 /**
    2101  * Output HTML links to recipients in the current thread.
    2102  *
    2103  * @since 2.2.0
    2104  */
    2105 function bp_the_thread_recipients_list() {
    2106     echo bp_get_thread_recipients_list();
    2107 }
    2108     /**
    2109      * Generate HTML links to the profiles of recipients in the current thread.
    2110      *
    2111      * @since 2.2.0
    2112      *
    2113      * @return string
    2114      */
    2115     function bp_get_thread_recipients_list() {
    2116         global $thread_template;
    2117 
    2118         $recipient_links = array();
    2119 
    2120         foreach( (array) $thread_template->thread->recipients as $recipient ) {
    2121             if ( (int) $recipient->user_id !== bp_loggedin_user_id() ) {
    2122                 $recipient_link = bp_core_get_userlink( $recipient->user_id );
    2123 
    2124                 if ( empty( $recipient_link ) ) {
    2125                     $recipient_link = __( 'Deleted User', 'buddypress' );
    2126                 }
    2127 
    2128                 $recipient_links[] = $recipient_link;
    2129             }
    2130         }
    2131 
    2132         /**
    2133          * Filters the HTML links to the profiles of recipients in the current thread.
    2134          *
    2135          * @since 2.2.0
    2136          *
    2137          * @param string $value Comma-separated list of recipient HTML links for current thread.
    2138          */
    2139         return apply_filters( 'bp_get_the_thread_recipients_list', implode( ', ', $recipient_links ) );
    2140     }
    2141 
    2142 /**
    2143  * Echo the ID of the current message in the thread.
    2144  *
    2145  * @since 1.9.0
    2146  */
    2147 function bp_the_thread_message_id() {
    2148     echo (int) bp_get_the_thread_message_id();
    2149 }
    2150     /**
    2151      * Get the ID of the current message in the thread.
    2152      *
    2153      * @since 1.9.0
    2154      *
    2155      * @return int
    2156      */
    2157     function bp_get_the_thread_message_id() {
    2158         global $thread_template;
    2159 
    2160         $thread_message_id = isset( $thread_template->message->id )
    2161             ? (int) $thread_template->message->id
    2162             : null;
    2163 
    2164         /**
    2165          * Filters the ID of the current message in the thread.
    2166          *
    2167          * @since 1.9.0
    2168          *
    2169          * @param int $thread_message_id ID of the current message in the thread.
    2170          */
    2171         return apply_filters( 'bp_get_the_thread_message_id', $thread_message_id );
    2172     }
    2173 
    2174 /**
    2175  * Output the CSS classes for messages within a single thread.
    2176  *
    2177  * @since 2.1.0
    2178  */
    2179 function bp_the_thread_message_css_class() {
    2180     echo esc_attr( bp_get_the_thread_message_css_class() );
    2181 }
    2182     /**
    2183      * Generate the CSS classes for messages within a single thread.
    2184      *
    2185      * @since 2.1.0
    2186      *
    2187      * @return string
    2188      */
    2189     function bp_get_the_thread_message_css_class() {
    2190         global $thread_template;
    2191 
    2192         $classes = array();
    2193 
    2194         // Zebra-striping.
    2195         $classes[] = bp_get_the_thread_message_alt_class();
    2196 
    2197         // ID of the sender.
    2198         $classes[] = 'sent-by-' . intval( $thread_template->message->sender_id );
    2199 
    2200         // Whether the sender is the same as the logged-in user.
    2201         if ( bp_loggedin_user_id() == $thread_template->message->sender_id ) {
    2202             $classes[] = 'sent-by-me';
    2203         }
    2204 
    2205         /**
    2206          * Filters the CSS classes for messages within a single thread.
    2207          *
    2208          * @since 2.1.0
    2209          *
    2210          * @param array $classes Array of classes to add to the HTML class attribute.
    2211          */
    2212         $classes = apply_filters( 'bp_get_the_thread_message_css_class', $classes );
    2213 
    2214         return implode( ' ', $classes );
    2215     }
    2216 
    2217 /**
    2218  * Output the CSS class used for message zebra striping.
    2219  */
    2220 function bp_the_thread_message_alt_class() {
    2221     echo esc_attr( bp_get_the_thread_message_alt_class() );
    2222 }
    2223     /**
    2224      * Get the CSS class used for message zebra striping.
    2225      *
    2226      * @return string
    2227      */
    2228     function bp_get_the_thread_message_alt_class() {
    2229         global $thread_template;
    2230 
    2231         if ( $thread_template->current_message % 2 == 1 ) {
    2232             $class = 'even alt';
    2233         } else {
    2234             $class = 'odd';
    2235         }
    2236 
    2237         /**
    2238          * Filters the CSS class used for message zebra striping.
    2239          *
    2240          * @since 1.1.0
    2241          *
    2242          * @param string $class Class determined to be next for zebra striping effect.
    2243          */
    2244         return apply_filters( 'bp_get_the_thread_message_alt_class', $class );
    2245     }
    2246 
    2247 /**
    2248  * Output the ID for message sender within a single thread.
    2249  *
    2250  * @since 2.1.0
    2251  */
    2252 function bp_the_thread_message_sender_id() {
    2253     echo (int) bp_get_the_thread_message_sender_id();
    2254 }
    2255     /**
    2256      * Return the ID for message sender within a single thread.
    2257      *
    2258      * @since 2.1.0
    2259      *
    2260      * @return string
    2261      */
    2262     function bp_get_the_thread_message_sender_id() {
    2263         global $thread_template;
    2264 
    2265         $user_id = ! empty( $thread_template->message->sender_id )
    2266             ? $thread_template->message->sender_id
    2267             : 0;
    2268 
    2269         /**
    2270          * Filters the ID for message sender within a single thread.
    2271          *
    2272          * @since 2.1.0
    2273          *
    2274          * @param int $user_id ID of the message sender.
    2275          */
    2276         return (int) apply_filters( 'bp_get_the_thread_message_sender_id', (int) $user_id );
    2277     }
    2278 
    2279 /**
    2280  * Output the avatar for the current message sender.
    2281  *
    2282  * @param array|string $args See {@link bp_get_the_thread_message_sender_avatar_thumb()}
    2283  *                           for a description.
    2284  */
    2285 function bp_the_thread_message_sender_avatar( $args = '' ) {
    2286     echo bp_get_the_thread_message_sender_avatar_thumb( $args );
    2287 }
    2288     /**
    2289      * Get the avatar for the current message sender.
    2290      *
    2291      * @param array|string $args {
    2292      *     Array of arguments. See {@link bp_core_fetch_avatar()} for more
    2293      *     complete details. All arguments are optional.
    2294      *     @type string $type   Avatar type. Default: 'thumb'.
    2295      *     @type int    $width  Avatar width. Default: default for your $type.
    2296      *     @type int    $height Avatar height. Default: default for your $type.
    2297      * }
    2298      * @return string <img> tag containing the avatar.
    2299      */
    2300     function bp_get_the_thread_message_sender_avatar_thumb( $args = '' ) {
    2301         global $thread_template;
    2302 
    2303         $r = bp_parse_args( $args, array(
    2304             'type'   => 'thumb',
    2305             'width'  => false,
    2306             'height' => false,
    2307         ) );
    2308 
    2309         /**
    2310          * Filters the avatar for the current message sender.
    2311          *
    2312          * @since 1.1.0
    2313          *
    2314          * @param string $value <img> tag containing the avatar value.
    2315          */
    2316         return apply_filters( 'bp_get_the_thread_message_sender_avatar_thumb', bp_core_fetch_avatar( array(
    2317             'item_id' => $thread_template->message->sender_id,
    2318             'type'    => $r['type'],
    2319             'width'   => $r['width'],
    2320             'height'  => $r['height'],
    2321             'alt'     => bp_core_get_user_displayname( $thread_template->message->sender_id )
    2322         ) ) );
    2323     }
    2324 
    2325 /**
    2326  * Output a link to the sender of the current message.
    2327  *
    2328  * @since 1.1.0
    2329  */
    2330 function bp_the_thread_message_sender_link() {
    2331     echo esc_url( bp_get_the_thread_message_sender_link() );
    2332 }
    2333     /**
    2334      * Get a link to the sender of the current message.
    2335      *
    2336      * @since 1.1.0
    2337      *
    2338      * @return string
    2339      */
    2340     function bp_get_the_thread_message_sender_link() {
    2341         global $thread_template;
    2342 
    2343         /**
    2344          * Filters the link to the sender of the current message.
    2345          *
    2346          * @since 1.1.0
    2347          *
    2348          * @param string $value Link to the sender of the current message.
    2349          */
    2350         return apply_filters( 'bp_get_the_thread_message_sender_link', bp_core_get_userlink( $thread_template->message->sender_id, false, true ) );
    2351     }
    2352 
    2353 /**
    2354  * Output the display name of the sender of the current message.
    2355  *
    2356  * @since 1.1.0
    2357  */
    2358 function bp_the_thread_message_sender_name() {
    2359     echo esc_html( bp_get_the_thread_message_sender_name() );
    2360 }
    2361     /**
    2362      * Get the display name of the sender of the current message.
    2363      *
    2364      * @since 1.1.0
    2365      *
    2366      * @return string
    2367      */
    2368     function bp_get_the_thread_message_sender_name() {
    2369         global $thread_template;
    2370 
    2371         $display_name = bp_core_get_user_displayname( $thread_template->message->sender_id );
    2372 
    2373         if ( empty( $display_name ) ) {
    2374             $display_name = __( 'Deleted User', 'buddypress' );
    2375         }
    2376 
    2377         /**
    2378          * Filters the display name of the sender of the current message.
    2379          *
    2380          * @since 1.1.0
    2381          *
    2382          * @param string $display_name Display name of the sender of the current message.
    2383          */
    2384         return apply_filters( 'bp_get_the_thread_message_sender_name', $display_name );
    2385     }
    2386 
    2387 /**
    2388  * Output the URL for deleting the current thread.
    2389  *
    2390  * @since 1.5.0
    2391  */
    2392 function bp_the_thread_delete_link() {
    2393     echo esc_url( bp_get_the_thread_delete_link() );
    2394 }
    2395     /**
    2396      * Get the URL for deleting the current thread.
    2397      *
    2398      * @since 1.5.0
    2399      *
    2400      * @return string URL
    2401      */
    2402     function bp_get_the_thread_delete_link() {
    2403 
    2404         /**
    2405          * Filters the URL for deleting the current thread.
    2406          *
    2407          * @since 1.0.0
    2408          *
    2409          * @param string $value URL for deleting the current thread.
    2410          * @param string $value Text indicating action being executed.
    2411          */
    2412         return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox/delete/' . bp_get_the_thread_id(), 'messages_delete_thread' ) );
    2413     }
    2414 
    2415 /**
    2416  * Output the 'Sent x hours ago' string for the current message.
    2417  *
    2418  * @since 1.1.0
    2419  */
    2420 function bp_the_thread_message_time_since() {
    2421     echo bp_get_the_thread_message_time_since();
    2422 }
    2423     /**
    2424      * Generate the 'Sent x hours ago' string for the current message.
    2425      *
    2426      * @since 1.1.0
    2427      *
    2428      * @return string
    2429      */
    2430     function bp_get_the_thread_message_time_since() {
    2431 
    2432         /**
    2433          * Filters the 'Sent x hours ago' string for the current message.
    2434          *
    2435          * @since 1.1.0
    2436          *
    2437          * @param string $value Default text of 'Sent x hours ago'.
    2438          */
    2439         return apply_filters( 'bp_get_the_thread_message_time_since', sprintf( __( 'Sent %s', 'buddypress' ), bp_core_time_since( bp_get_the_thread_message_date_sent() ) ) );
    2440     }
    2441 
    2442 /**
    2443  * Output the timestamp for the current message.
    2444  *
    2445  * @since 2.1.0
    2446  */
    2447 function bp_the_thread_message_date_sent() {
    2448     echo bp_get_the_thread_message_date_sent();
    2449 }
    2450     /**
    2451      * Generate the 'Sent x hours ago' string for the current message.
    2452      *
    2453      * @since 2.1.0
    2454      *
    2455      * @uses strtotime() To convert the message string into a usable timestamp.
    2456      *
    2457      * @return int
    2458      */
    2459     function bp_get_the_thread_message_date_sent() {
    2460         global $thread_template;
    2461 
    2462         /**
    2463          * Filters the date sent value for the current message as a timestamp.
    2464          *
    2465          * @since 2.1.0
    2466          *
    2467          * @param string $value Timestamp of the date sent value for the current message.
    2468          */
    2469         return apply_filters( 'bp_get_the_thread_message_date_sent', strtotime( $thread_template->message->date_sent ) );
    2470     }
    2471 
    2472 /**
    2473  * Output the content of the current message in the loop.
    2474  *
    2475  * @since 1.1.0
    2476  */
    2477 function bp_the_thread_message_content() {
    2478     echo bp_get_the_thread_message_content();
    2479 }
    2480     /**
    2481      * Get the content of the current message in the loop.
    2482      *
    2483      * @since 1.1.0
    2484      *
    2485      * @return string
    2486      */
    2487     function bp_get_the_thread_message_content() {
    2488         global $thread_template;
    2489 
    2490         /**
    2491          * Filters the content of the current message in the loop.
    2492          *
    2493          * @since 1.1.0
    2494          *
    2495          * @param string $message The content of the current message in the loop.
    2496          */
    2497         return apply_filters( 'bp_get_the_thread_message_content', $thread_template->message->message );
    2498     }
    2499 
    2500 /** Embeds *******************************************************************/
    2501 
    2502 /**
    2503  * Enable oEmbed support for Messages.
    2504  *
    2505  * @since 1.5.0
    2506  *
    2507  * @see BP_Embed
    2508  */
    2509 function bp_messages_embed() {
    2510     add_filter( 'embed_post_id',         'bp_get_the_thread_message_id' );
    2511     add_filter( 'bp_embed_get_cache',    'bp_embed_message_cache',      10, 3 );
    2512     add_action( 'bp_embed_update_cache', 'bp_embed_message_save_cache', 10, 3 );
    2513 }
    2514 add_action( 'thread_loop_start', 'bp_messages_embed' );
    2515 
    2516 /**
    2517  * Fetch a private message item's cached embeds.
    2518  *
    2519  * Used during {@link BP_Embed::parse_oembed()} via {@link bp_messages_embed()}.
    2520  *
    2521  * @since 2.2.0
    2522  *
    2523  * @param string $cache    An empty string passed by BP_Embed::parse_oembed() for
    2524  *                         functions like this one to filter.
    2525  * @param int    $id       The ID of the message item.
    2526  * @param string $cachekey The cache key generated in BP_Embed::parse_oembed().
    2527  * @return mixed The cached embeds for this message item.
    2528  */
    2529 function bp_embed_message_cache( $cache, $id, $cachekey ) {
    2530     return bp_messages_get_meta( $id, $cachekey );
    2531 }
    2532 
    2533 /**
    2534  * Set a private message item's embed cache.
    2535  *
    2536  * Used during {@link BP_Embed::parse_oembed()} via {@link bp_messages_embed()}.
    2537  *
    2538  * @since 2.2.0
    2539  *
    2540  * @param string $cache    An empty string passed by BP_Embed::parse_oembed() for
    2541  *                         functions like this one to filter.
    2542  * @param string $cachekey The cache key generated in BP_Embed::parse_oembed().
    2543  * @param int    $id       The ID of the message item.
    2544  */
    2545 function bp_embed_message_save_cache( $cache, $cachekey, $id ) {
    2546     bp_messages_update_meta( $id, $cachekey, $cache );
    2547 }
  • trunk/src/bp-messages/classes/class-bp-messages-component.php

    r10515 r10522  
    341341    }
    342342}
    343 
    344 /**
    345  * Bootstrap the Messages component.
    346  */
    347 function bp_setup_messages() {
    348     buddypress()->messages = new BP_Messages_Component();
    349 }
    350 add_action( 'bp_setup_components', 'bp_setup_messages', 6 );
  • trunk/src/bp-messages/classes/class-bp-messages-sitewide-notices-widget.php

    r10515 r10522  
    11<?php
    22/**
    3  * BuddyPress Messages Widgets.
     3 * BuddyPress Messages Sitewide Notices Widget.
    44 *
    55 * @package BuddyPress
     
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    12 
    13 /**
    14  * Register widgets for the Messages component.
    15  *
    16  * @since 1.9.0
    17  */
    18 function bp_messages_register_widgets() {
    19     add_action( 'widgets_init', create_function('', 'return register_widget( "BP_Messages_Sitewide_Notices_Widget" );') );
    20 }
    21 add_action( 'bp_register_widgets', 'bp_messages_register_widgets' );
    2212
    2313/**
  • trunk/src/bp-messages/classes/class-bp-messages-thread-template.php

    r10515 r10522  
    11<?php
    22/**
    3  * BuddyPress Messages Template Tags.
     3 * BuddyPress Messages Thread Template Class.
    44 *
    55 * @package BuddyPress
     
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
    12 
    13 /**
    14  * Message Box Template Class
    15  */
    16 class BP_Messages_Box_Template {
    17 
    18     /**
    19      * The loop iterator.
    20      *
    21      * @var int
    22      */
    23     public $current_thread = -1;
    24 
    25     /**
    26      * The number of threads returned by the paged query.
    27      *
    28      * @var int
    29      */
    30     public $current_thread_count = 0;
    31 
    32     /**
    33      * Total number of threads matching the query params.
    34      *
    35      * @var int
    36      */
    37     public $total_thread_count = 0;
    38 
    39     /**
    40      * Array of threads located by the query.
    41      *
    42      * @var array
    43      */
    44     public $threads = array();
    45 
    46     /**
    47      * The thread object currently being iterated on.
    48      *
    49      * @var object
    50      */
    51     public $thread = false;
    52 
    53     /**
    54      * A flag for whether the loop is currently being iterated.
    55      *
    56      * @var bool
    57      */
    58     public $in_the_loop = false;
    59 
    60     /**
    61      * User ID of the current inbox.
    62      *
    63      * @var int
    64      */
    65     public $user_id = 0;
    66 
    67     /**
    68      * The current "box" view ('notices', 'sentbox', 'inbox').
    69      *
    70      * @var string
    71      */
    72     public $box = 'inbox';
    73 
    74     /**
    75      * The page number being requested.
    76      *
    77      * @var int
    78      */
    79     public $pag_page = 1;
    80 
    81     /**
    82      * The number of items being requested per page.
    83      *
    84      * @var int
    85      */
    86     public $pag_num = 10;
    87 
    88     /**
    89      * An HTML string containing pagination links.
    90      *
    91      * @var string
    92      */
    93     public $pag_links = '';
    94 
    95     /**
    96      * Search terms for limiting the thread query.
    97      *
    98      * @var string
    99      */
    100     public $search_terms = '';
    101 
    102     /**
    103      * Constructor method.
    104      *
    105      * @param array $args {
    106      *     Array of arguments. See bp_has_message_threads() for full description.
    107      * }
    108      */
    109     public function __construct( $args = array() ) {
    110 
    111         // Backward compatibility with old method of passing arguments.
    112         if ( ! is_array( $args ) || func_num_args() > 1 ) {
    113             _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 
    115             $old_args_keys = array(
    116                 0 => 'user_id',
    117                 1 => 'box',
    118                 2 => 'per_page',
    119                 3 => 'max',
    120                 4 => 'type',
    121                 5 => 'search_terms',
    122                 6 => 'page_arg'
    123             );
    124 
    125             $func_args = func_get_args();
    126             $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
    127         }
    128 
    129         $r = wp_parse_args( $args, array(
    130             'page'         => 1,
    131             'per_page'     => 10,
    132             'page_arg'     => 'mpage',
    133             'box'          => 'inbox',
    134             'type'         => 'all',
    135             'user_id'      => bp_loggedin_user_id(),
    136             'max'          => false,
    137             'search_terms' => '',
    138             'meta_query'   => array(),
    139         ) );
    140 
    141         $this->pag_arg      = sanitize_key( $r['page_arg'] );
    142         $this->pag_page     = bp_sanitize_pagination_arg( $this->pag_arg, $r['page']     );
    143         $this->pag_num      = bp_sanitize_pagination_arg( 'num',          $r['per_page'] );
    144         $this->user_id      = $r['user_id'];
    145         $this->box          = $r['box'];
    146         $this->type         = $r['type'];
    147         $this->search_terms = $r['search_terms'];
    148 
    149         if ( 'notices' === $this->box ) {
    150             $this->threads = BP_Messages_Notice::get_notices( array(
    151                 'pag_num'  => $this->pag_num,
    152                 'pag_page' => $this->pag_page
    153             ) );
    154         } else {
    155             $threads = BP_Messages_Thread::get_current_threads_for_user( array(
    156                 'user_id'      => $this->user_id,
    157                 'box'          => $this->box,
    158                 'type'         => $this->type,
    159                 'limit'        => $this->pag_num,
    160                 'page'         => $this->pag_page,
    161                 'search_terms' => $this->search_terms,
    162                 'meta_query'   => $r['meta_query'],
    163             ) );
    164 
    165             $this->threads            = $threads['threads'];
    166             $this->total_thread_count = $threads['total'];
    167         }
    168 
    169         if ( !$this->threads ) {
    170             $this->thread_count       = 0;
    171             $this->total_thread_count = 0;
    172         } else {
    173             $total_notice_count = BP_Messages_Notice::get_total_notice_count();
    174 
    175             if ( empty( $r['max'] ) || ( (int) $r['max'] >= (int) $total_notice_count ) ) {
    176                 if ( 'notices' === $this->box ) {
    177                     $this->total_thread_count = (int) $total_notice_count;
    178                 }
    179             } else {
    180                 $this->total_thread_count = (int) $r['max'];
    181             }
    182 
    183             if ( ! empty( $r['max'] ) ) {
    184                 if ( (int) $r['max'] >= count( $this->threads ) ) {
    185                     $this->thread_count = count( $this->threads );
    186                 } else {
    187                     $this->thread_count = (int) $r['max'];
    188                 }
    189             } else {
    190                 $this->thread_count = count( $this->threads );
    191             }
    192         }
    193 
    194         if ( (int) $this->total_thread_count && (int) $this->pag_num ) {
    195             $pag_args = array(
    196                 $r['page_arg'] => '%#%',
    197             );
    198 
    199             if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) {
    200                 $base = remove_query_arg( 's', wp_get_referer() );
    201             } else {
    202                 $base = '';
    203             }
    204 
    205             $add_args = array();
    206 
    207             if ( ! empty( $this->search_terms ) ) {
    208                 $add_args['s'] = $this->search_terms;
    209             }
    210 
    211             $this->pag_links = paginate_links( array(
    212                 'base'      => add_query_arg( $pag_args, $base ),
    213                 'format'    => '',
    214                 'total'     => ceil( (int) $this->total_thread_count / (int) $this->pag_num ),
    215                 'current'   => $this->pag_page,
    216                 'prev_text' => _x( '&larr;', 'Message pagination previous text', 'buddypress' ),
    217                 'next_text' => _x( '&rarr;', 'Message pagination next text', 'buddypress' ),
    218                 'mid_size'  => 1,
    219                 'add_args'  => $add_args,
    220             ) );
    221         }
    222     }
    223 
    224     /**
    225      * Whether there are threads available in the loop.
    226      *
    227      * @see bp_has_message_threads()
    228      *
    229      * @return bool True if there are items in the loop, otherwise false.
    230      */
    231     public function has_threads() {
    232         if ( $this->thread_count ) {
    233             return true;
    234         }
    235 
    236         return false;
    237     }
    238 
    239     /**
    240      * Set up the next member and iterate index.
    241      *
    242      * @return object The next member to iterate over.
    243      */
    244     public function next_thread() {
    245         $this->current_thread++;
    246         $this->thread = $this->threads[$this->current_thread];
    247 
    248         return $this->thread;
    249     }
    250 
    251     /**
    252      * Rewind the threads and reset thread index.
    253      */
    254     public function rewind_threads() {
    255         $this->current_thread = -1;
    256         if ( $this->thread_count > 0 ) {
    257             $this->thread = $this->threads[0];
    258         }
    259     }
    260 
    261     /**
    262      * Whether there are threads left in the loop to iterate over.
    263      *
    264      * This method is used by {@link bp_message_threads()} as part of the
    265      * while loop that controls iteration inside the threads loop, eg:
    266      *     while ( bp_message_threads() ) { ...
    267      *
    268      * @see bp_message_threads()
    269      *
    270      * @return bool True if there are more threads to show, otherwise false.
    271      */
    272     function message_threads() {
    273         if ( $this->current_thread + 1 < $this->thread_count ) {
    274             return true;
    275         } elseif ( $this->current_thread + 1 == $this->thread_count ) {
    276 
    277             /**
    278              * Fires when at the end of threads to iterate over.
    279              *
    280              * @since 1.5.0
    281              */
    282             do_action( 'messages_box_loop_end' );
    283             // Do some cleaning up after the loop.
    284             $this->rewind_threads();
    285         }
    286 
    287         $this->in_the_loop = false;
    288         return false;
    289     }
    290 
    291     /**
    292      * Set up the current thread inside the loop.
    293      *
    294      * Used by {@link bp_message_thread()} to set up the current thread data
    295      * while looping, so that template tags used during that iteration make
    296      * reference to the current thread.
    297      *
    298      * @see bp_message_thread()
    299      */
    300     public function the_message_thread() {
    301 
    302         $this->in_the_loop = true;
    303         $this->thread      = $this->next_thread();
    304 
    305         if ( ! bp_is_current_action( 'notices' ) ) {
    306             $last_message_index     = count( $this->thread->messages ) - 1;
    307             $this->thread->messages = array_reverse( (array) $this->thread->messages );
    308 
    309             // Set up the last message data.
    310             if ( count($this->thread->messages) > 1 ) {
    311                 if ( 'inbox' == $this->box ) {
    312                     foreach ( (array) $this->thread->messages as $key => $message ) {
    313                         if ( bp_loggedin_user_id() != $message->sender_id ) {
    314                             $last_message_index = $key;
    315                             break;
    316                         }
    317                     }
    318 
    319                 } elseif ( 'sentbox' == $this->box ) {
    320                     foreach ( (array) $this->thread->messages as $key => $message ) {
    321                         if ( bp_loggedin_user_id() == $message->sender_id ) {
    322                             $last_message_index = $key;
    323                             break;
    324                         }
    325                     }
    326                 }
    327             }
    328 
    329             $this->thread->last_message_id      = $this->thread->messages[ $last_message_index ]->id;
    330             $this->thread->last_message_date    = $this->thread->messages[ $last_message_index ]->date_sent;
    331             $this->thread->last_sender_id       = $this->thread->messages[ $last_message_index ]->sender_id;
    332             $this->thread->last_message_subject = $this->thread->messages[ $last_message_index ]->subject;
    333             $this->thread->last_message_content = $this->thread->messages[ $last_message_index ]->message;
    334         }
    335 
    336         // Loop has just started.
    337         if ( 0 == $this->current_thread ) {
    338 
    339             /**
    340              * Fires if at the start of the message thread loop.
    341              *
    342              * @since 1.5.0
    343              */
    344             do_action( 'messages_box_loop_start' );
    345         }
    346     }
    347 }
    348 
    349 /**
    350  * Retrieve private message threads for display in inbox/sentbox/notices.
    351  *
    352  * Similar to WordPress's have_posts() function, this function is responsible
    353  * for querying the database and retrieving private messages for display inside
    354  * the theme via individual template parts for a member's inbox/sentbox/notices.
    355  *
    356  * @since 1.0.0
    357  *
    358  * @global BP_Messages_Box_Template $messages_template
    359  *
    360  * @param array|string $args {
    361  *     Array of arguments. All are optional.
    362  *     @type int    $user_id      ID of the user whose threads are being loaded.
    363  *                                Default: ID of the logged-in user.
    364  *     @type string $box          Current "box" view. If not provided here, the current
    365  *                                view will be inferred from the URL.
    366  *     @type int    $per_page     Number of results to return per page. Default: 10.
    367  *     @type int    $max          Max results to return. Default: false.
    368  *     @type string $type         Type of messages to return. Values: 'all', 'read', 'unread'
    369  *                                Default: 'all'
    370  *     @type string $search_terms Terms to which to limit results. Default:
    371  *                                the value of $_REQUEST['s'].
    372  *     @type string $page_arg     URL argument used for the pagination param.
    373  *                                Default: 'mpage'.
    374  *     @type array  $meta_query   Meta query arguments. Only applicable if $box is
    375  *                                not 'notices'. See WP_Meta_Query more details.
    376  * }
    377  * @return bool True if there are threads to display, otherwise false.
    378  */
    379 function bp_has_message_threads( $args = array() ) {
    380     global $messages_template;
    381 
    382     // The default box the user is looking at.
    383     $current_action = bp_current_action();
    384     switch ( $current_action ) {
    385         case 'sentbox' :
    386         case 'notices' :
    387         case 'inbox'   :
    388             $default_box = $current_action;
    389             break;
    390         default :
    391             $default_box = 'inbox';
    392             break;
    393     }
    394 
    395     // User ID
    396     // @todo displayed user for moderators that get this far?
    397     $user_id = bp_loggedin_user_id();
    398 
    399     // Search Terms.
    400     $search_terms = isset( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : '';
    401 
    402     // Parse the arguments.
    403     $r = bp_parse_args( $args, array(
    404         'user_id'      => $user_id,
    405         'box'          => $default_box,
    406         'per_page'     => 10,
    407         'max'          => false,
    408         'type'         => 'all',
    409         'search_terms' => $search_terms,
    410         'page_arg'     => 'mpage', // See https://buddypress.trac.wordpress.org/ticket/3679.
    411         'meta_query'   => array()
    412     ), 'has_message_threads' );
    413 
    414     // Load the messages loop global up with messages.
    415     $messages_template = new BP_Messages_Box_Template( $r );
    416 
    417     /**
    418      * Filters if there are any message threads to display in inbox/sentbox/notices.
    419      *
    420      * @since 1.1.0
    421      *
    422      * @param bool                     $value             Whether or not the message has threads.
    423      * @param BP_Messages_Box_Template $messages_template Current message box template object.
    424      * @param array                    $r                 Array of parsed arguments passed into function.
    425      */
    426     return apply_filters( 'bp_has_message_threads', $messages_template->has_threads(), $messages_template, $r );
    427 }
    428 
    429 /**
    430  * Check whether there are more threads to iterate over.
    431  *
    432  * @return bool
    433  */
    434 function bp_message_threads() {
    435     global $messages_template;
    436     return $messages_template->message_threads();
    437 }
    438 
    439 /**
    440  * Set up the current thread inside the loop.
    441  *
    442  * @return object
    443  */
    444 function bp_message_thread() {
    445     global $messages_template;
    446     return $messages_template->the_message_thread();
    447 }
    448 
    449 /**
    450  * Output the ID of the current thread in the loop.
    451  */
    452 function bp_message_thread_id() {
    453     echo bp_get_message_thread_id();
    454 }
    455     /**
    456      * Get the ID of the current thread in the loop.
    457      *
    458      * @return int
    459      */
    460     function bp_get_message_thread_id() {
    461         global $messages_template;
    462 
    463         /**
    464          * Filters the ID of the current thread in the loop.
    465          *
    466          * @since 1.0.0
    467          *
    468          * @param int $thread_id ID of the current thread in the loop.
    469          */
    470         return apply_filters( 'bp_get_message_thread_id', $messages_template->thread->thread_id );
    471     }
    472 
    473 /**
    474  * Output the subject of the current thread in the loop.
    475  */
    476 function bp_message_thread_subject() {
    477     echo bp_get_message_thread_subject();
    478 }
    479     /**
    480      * Get the subject of the current thread in the loop.
    481      *
    482      * @return string
    483      */
    484     function bp_get_message_thread_subject() {
    485         global $messages_template;
    486 
    487         /**
    488          * Filters the subject of the current thread in the loop.
    489          *
    490          * @since 1.1.0
    491          *
    492          * @param string $value Subject of the current thread in the loop.
    493          */
    494         return apply_filters( 'bp_get_message_thread_subject', $messages_template->thread->last_message_subject );
    495     }
    496 
    497 /**
    498  * Output an excerpt from the current message in the loop.
    499  */
    500 function bp_message_thread_excerpt() {
    501     echo bp_get_message_thread_excerpt();
    502 }
    503     /**
    504      * Generate an excerpt from the current message in the loop.
    505      *
    506      * @return string
    507      */
    508     function bp_get_message_thread_excerpt() {
    509         global $messages_template;
    510 
    511         /**
    512          * Filters the excerpt of the current thread in the loop.
    513          *
    514          * @since 1.0.0
    515          *
    516          * @param string $value Excerpt of the current thread in the loop.
    517          */
    518         return apply_filters( 'bp_get_message_thread_excerpt', strip_tags( bp_create_excerpt( $messages_template->thread->last_message_content, 75 ) ) );
    519     }
    520 
    521 /**
    522  * Output the thread's last message content.
    523  *
    524  * When viewing your Inbox, the last message is the most recent message in
    525  * the thread of which you are *not* the author.
    526  *
    527  * When viewing your Sentbox, last message is the most recent message in
    528  * the thread of which you *are* the member.
    529  *
    530  * @since 2.0.0
    531  */
    532 function bp_message_thread_content() {
    533     echo bp_get_message_thread_content();
    534 }
    535     /**
    536      * Return the thread's last message content.
    537      *
    538      * When viewing your Inbox, the last message is the most recent message in
    539      * the thread of which you are *not* the author.
    540      *
    541      * When viewing your Sentbox, last message is the most recent message in
    542      * the thread of which you *are* the member.
    543      *
    544      * @since 2.0.0
    545      *
    546      * @return string The raw content of the last message in the thread.
    547      */
    548     function bp_get_message_thread_content() {
    549         global $messages_template;
    550 
    551         /**
    552          * Filters the content of the last message in the thread.
    553          *
    554          * @since 2.0.0
    555          *
    556          * @param string $last_message_content Content of the last message in the thread.
    557          */
    558         return apply_filters( 'bp_get_message_thread_content', $messages_template->thread->last_message_content );
    559     }
    560 
    561 /**
    562  * Output a link to the page of the current thread's last author.
    563  */
    564 function bp_message_thread_from() {
    565     echo bp_get_message_thread_from();
    566 }
    567     /**
    568      * Get a link to the page of the current thread's last author.
    569      *
    570      * @return string
    571      */
    572     function bp_get_message_thread_from() {
    573         global $messages_template;
    574 
    575         /**
    576          * Filters the link to the page of the current thread's last author.
    577          *
    578          * @since 1.0.0
    579          *
    580          * @param string $value Link to the page of the current thread's last author.
    581          */
    582         return apply_filters( 'bp_get_message_thread_from', bp_core_get_userlink( $messages_template->thread->last_sender_id ) );
    583     }
    584 
    585 /**
    586  * Output links to the pages of the current thread's recipients.
    587  */
    588 function bp_message_thread_to() {
    589     echo bp_get_message_thread_to();
    590 }
    591     /**
    592      * Generate HTML links to the pages of the current thread's recipients.
    593      *
    594      * @return string
    595      */
    596     function bp_get_message_thread_to() {
    597         global $messages_template;
    598 
    599         /**
    600          * Filters the HTML links to the pages of the current thread's recipients.
    601          *
    602          * @since 1.0.0
    603          *
    604          * @param string $value HTML links to the pages of the current thread's recipients.
    605          */
    606         return apply_filters( 'bp_message_thread_to', BP_Messages_Thread::get_recipient_links($messages_template->thread->recipients ) );
    607     }
    608 
    609 /**
    610  * Output the permalink for a particular thread.
    611  *
    612  * @param int $thread_id Optional. ID of the thread. Default: current thread
    613  *                       being iterated on in the loop.
    614  */
    615 function bp_message_thread_view_link( $thread_id = 0 ) {
    616     echo bp_get_message_thread_view_link( $thread_id );
    617 }
    618     /**
    619      * Get the permalink of a particular thread.
    620      *
    621      * @param int $thread_id Optional. ID of the thread. Default: current
    622      *                       thread being iterated on in the loop.
    623      * @return string
    624      */
    625     function bp_get_message_thread_view_link( $thread_id = 0 ) {
    626         global $messages_template;
    627 
    628         if ( empty( $messages_template ) && (int) $thread_id > 0 ) {
    629             $thread_id = (int) $thread_id;
    630         } elseif ( ! empty( $messages_template->thread->thread_id ) ) {
    631             $thread_id = $messages_template->thread->thread_id;
    632         }
    633 
    634         /**
    635          * Filters the permalink of a particular thread.
    636          *
    637          * @since 1.0.0
    638          *
    639          * @param string $value permalink of a particular thread.
    640          */
    641         return apply_filters( 'bp_get_message_thread_view_link', trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id ) );
    642     }
    643 
    644 /**
    645  * Output the URL for deleting the current thread.
    646  */
    647 function bp_message_thread_delete_link() {
    648     echo esc_url( bp_get_message_thread_delete_link() );
    649 }
    650     /**
    651      * Generate the URL for deleting the current thread.
    652      *
    653      * @return string
    654      */
    655     function bp_get_message_thread_delete_link() {
    656         global $messages_template;
    657 
    658         /**
    659          * Filters the URL for deleting the current thread.
    660          *
    661          * @since 1.0.0
    662          *
    663          * @param string $value URL for deleting the current thread.
    664          * @param string $value Text indicating action being executed.
    665          */
    666         return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/delete/' . $messages_template->thread->thread_id ), 'messages_delete_thread' ) );
    667     }
    668 
    669 /**
    670  * Output the URL used for marking a single message thread as unread.
    671  *
    672  * Since this function directly outputs a URL, it is escaped.
    673  *
    674  * @since 2.2.0
    675  */
    676 function bp_the_message_thread_mark_unread_url() {
    677     echo esc_url( bp_get_the_message_thread_mark_unread_url() );
    678 }
    679     /**
    680      * Return the URL used for marking a single message thread as unread.
    681      *
    682      * @since 2.2.0
    683      *
    684      * @return string
    685      */
    686     function bp_get_the_message_thread_mark_unread_url() {
    687 
    688         // Get the message ID.
    689         $id = bp_get_message_thread_id();
    690 
    691         // Get the args to add to the URL.
    692         $args = array(
    693             'action'     => 'unread',
    694             'message_id' => $id
    695         );
    696 
    697         // Base unread URL.
    698         $url = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/unread' );
    699 
    700         // Add the args to the URL.
    701         $url = add_query_arg( $args, $url );
    702 
    703         // Add the nonce.
    704         $url = wp_nonce_url( $url, 'bp_message_thread_mark_unread_' . $id );
    705 
    706         /**
    707          * Filters the URL used for marking a single message thread as unread.
    708          *
    709          * @since 2.2.0
    710          *
    711          * @param string $url URL used for marking a single message thread as unread.
    712          */
    713         return apply_filters( 'bp_get_the_message_thread_mark_unread_url', $url );
    714     }
    715 
    716 /**
    717  * Output the URL used for marking a single message thread as read.
    718  *
    719  * Since this function directly outputs a URL, it is escaped.
    720  *
    721  * @since 2.2.0
    722  */
    723 function bp_the_message_thread_mark_read_url() {
    724     echo esc_url( bp_get_the_message_thread_mark_read_url() );
    725 }
    726     /**
    727      * Return the URL used for marking a single message thread as read.
    728      *
    729      * @since 2.2.0
    730      *
    731      * @return string
    732      */
    733     function bp_get_the_message_thread_mark_read_url() {
    734 
    735         // Get the message ID.
    736         $id = bp_get_message_thread_id();
    737 
    738         // Get the args to add to the URL.
    739         $args = array(
    740             'action'     => 'read',
    741             'message_id' => $id
    742         );
    743 
    744         // Base read URL.
    745         $url = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/read' );
    746 
    747         // Add the args to the URL.
    748         $url = add_query_arg( $args, $url );
    749 
    750         // Add the nonce.
    751         $url = wp_nonce_url( $url, 'bp_message_thread_mark_read_' . $id );
    752 
    753         /**
    754          * Filters the URL used for marking a single message thread as read.
    755          *
    756          * @since 2.2.0
    757          *
    758          * @param string $url URL used for marking a single message thread as read.
    759          */
    760         return apply_filters( 'bp_get_the_message_thread_mark_read_url', $url );
    761     }
    762 
    763 /**
    764  * Output the CSS class for the current thread.
    765  */
    766 function bp_message_css_class() {
    767     echo esc_attr( bp_get_message_css_class() );
    768 }
    769     /**
    770      * Generate the CSS class for the current thread.
    771      *
    772      * @return string
    773      */
    774     function bp_get_message_css_class() {
    775         global $messages_template;
    776 
    777         $class = false;
    778 
    779         if ( $messages_template->current_thread % 2 == 1 ) {
    780             $class .= 'alt';
    781         }
    782 
    783         /**
    784          * Filters the CSS class for the current thread.
    785          *
    786          * @since 1.2.10
    787          *
    788          * @param string $class Class string to be added to the list of classes.
    789          */
    790         return apply_filters( 'bp_get_message_css_class', trim( $class ) );
    791     }
    792 
    793 /**
    794  * Check whether the current thread has unread items.
    795  *
    796  * @return bool True if there are unread items, otherwise false.
    797  */
    798 function bp_message_thread_has_unread() {
    799     global $messages_template;
    800 
    801     $retval = ! empty( $messages_template->thread->unread_count )
    802         ? true
    803         : false;
    804 
    805     /**
    806      * Filters whether or not a message thread has unread items.
    807      *
    808      * @since 2.1.0
    809      *
    810      * @param bool $retval Whether or not a message thread has unread items.
    811      */
    812     return apply_filters( 'bp_message_thread_has_unread', $retval );
    813 }
    814 
    815 /**
    816  * Output the current thread's unread count.
    817  */
    818 function bp_message_thread_unread_count() {
    819     echo esc_html( bp_get_message_thread_unread_count() );
    820 }
    821     /**
    822      * Get the current thread's unread count.
    823      *
    824      * @return int
    825      */
    826     function bp_get_message_thread_unread_count() {
    827         global $messages_template;
    828 
    829         $count = ! empty( $messages_template->thread->unread_count )
    830             ? (int) $messages_template->thread->unread_count
    831             : false;
    832 
    833         /**
    834          * Filters the current thread's unread count.
    835          *
    836          * @since 1.0.0
    837          *
    838          * @param int $count Current thread unread count.
    839          */
    840         return apply_filters( 'bp_get_message_thread_unread_count', $count );
    841     }
    842 
    843 /**
    844  * Output a thread's total message count.
    845  *
    846  * @since 2.2.0
    847  *
    848  * @param int|bool $thread_id Optional. ID of the thread. Defaults to current thread ID.
    849  */
    850 function bp_message_thread_total_count( $thread_id = false ) {
    851     echo bp_get_message_thread_total_count( $thread_id );
    852 }
    853     /**
    854      * Get the current thread's total message count.
    855      *
    856      * @since 2.2.0
    857      *
    858      * @param int|bool $thread_id Optional. ID of the thread.
    859      *                            Defaults to current thread ID.
    860      * @return int
    861      */
    862     function bp_get_message_thread_total_count( $thread_id = false ) {
    863         if ( false === $thread_id ) {
    864             $thread_id = bp_get_message_thread_id();
    865         }
    866 
    867         $thread_template = new BP_Messages_Thread_Template( $thread_id, 'ASC', array(
    868             'update_meta_cache' => false
    869         ) );
    870 
    871         $count = 0;
    872         if ( ! empty( $thread_template->message_count ) ) {
    873             $count = intval( $thread_template->message_count );
    874         }
    875 
    876         /**
    877          * Filters the current thread's total message count.
    878          *
    879          * @since 2.2.0
    880          *
    881          * @param int $count Current thread total message count.
    882          */
    883         return apply_filters( 'bp_get_message_thread_total_count', $count );
    884     }
    885 
    886 /**
    887  * Output markup for the current thread's total and unread count.
    888  *
    889  * @since 2.2.0
    890  *
    891  * @param int|bool $thread_id Optional. ID of the thread. Default: current thread ID.
    892  */
    893 function bp_message_thread_total_and_unread_count( $thread_id = false ) {
    894     echo bp_get_message_thread_total_and_unread_count( $thread_id );
    895 }
    896     /**
    897      * Get markup for the current thread's total and unread count.
    898      *
    899      * @param int|bool $thread_id Optional. ID of the thread. Default: current thread ID.
    900      * @return string Markup displaying the total and unread count for the thread.
    901      */
    902     function bp_get_message_thread_total_and_unread_count( $thread_id = false ) {
    903         if ( false === $thread_id ) {
    904             $thread_id = bp_get_message_thread_id();
    905         }
    906 
    907         $total  = bp_get_message_thread_total_count( $thread_id );
    908         $unread = bp_get_message_thread_unread_count( $thread_id );
    909 
    910         return sprintf(
    911             '<span class="thread-count">(%1$s)</span> <span class="bp-screen-reader-text">%2$s</span>',
    912             number_format_i18n( $total ),
    913             sprintf( _n( '%d unread', '%d unread', $unread, 'buddypress' ), number_format_i18n( $unread ) )
    914         );
    915     }
    916 
    917 /**
    918  * Output the unformatted date of the last post in the current thread.
    919  */
    920 function bp_message_thread_last_post_date_raw() {
    921     echo bp_get_message_thread_last_post_date_raw();
    922 }
    923     /**
    924      * Get the unformatted date of the last post in the current thread.
    925      *
    926      * @return string
    927      */
    928     function bp_get_message_thread_last_post_date_raw() {
    929         global $messages_template;
    930 
    931         /**
    932          * Filters the unformatted date of the last post in the current thread.
    933          *
    934          * @since 2.1.0
    935          *
    936          * @param string $last_message_date Unformatted date of the last post in the current thread.
    937          */
    938         return apply_filters( 'bp_get_message_thread_last_message_date', $messages_template->thread->last_message_date );
    939     }
    940 
    941 /**
    942  * Output the nicely formatted date of the last post in the current thread.
    943  */
    944 function bp_message_thread_last_post_date() {
    945     echo bp_get_message_thread_last_post_date();
    946 }
    947     /**
    948      * Get the nicely formatted date of the last post in the current thread.
    949      *
    950      * @return string
    951      */
    952     function bp_get_message_thread_last_post_date() {
    953 
    954         /**
    955          * Filters the nicely formatted date of the last post in the current thread.
    956          *
    957          * @since 2.1.0
    958          *
    959          * @param string $value Formatted date of the last post in the current thread.
    960          */
    961         return apply_filters( 'bp_get_message_thread_last_post_date', bp_format_time( strtotime( bp_get_message_thread_last_post_date_raw() ) ) );
    962     }
    963 
    964 /**
    965  * Output the avatar for the last sender in the current message thread.
    966  *
    967  * @see bp_get_message_thread_avatar() for a description of arguments.
    968  *
    969  * @param array|string $args See {@link bp_get_message_thread_avatar()}.
    970  */
    971 function bp_message_thread_avatar( $args = '' ) {
    972     echo bp_get_message_thread_avatar( $args );
    973 }
    974     /**
    975      * Return the avatar for the last sender in the current message thread.
    976      *
    977      * @see bp_core_fetch_avatar() For a description of arguments and
    978      *      return values.
    979      *
    980      * @param array|string $args {
    981      *     Arguments are listed here with an explanation of their defaults.
    982      *     For more information about the arguments, see
    983      *     {@link bp_core_fetch_avatar()}.
    984      *     @type string      $type   Default: 'thumb'.
    985      *     @type int|bool    $width  Default: false.
    986      *     @type int|bool    $height Default: false.
    987      *     @type string      $class  Default: 'avatar'.
    988      *     @type string|bool $id     Default: false.
    989      *     @type string      $alt    Default: 'Profile picture of [display name]'.
    990      * }
    991      * @return string User avatar string.
    992      */
    993     function bp_get_message_thread_avatar( $args = '' ) {
    994         global $messages_template;
    995 
    996         $fullname = bp_core_get_user_displayname( $messages_template->thread->last_sender_id );
    997         $alt      = sprintf( __( 'Profile picture of %s', 'buddypress' ), $fullname );
    998 
    999         $r = bp_parse_args( $args, array(
    1000             'type'   => 'thumb',
    1001             'width'  => false,
    1002             'height' => false,
    1003             'class'  => 'avatar',
    1004             'id'     => false,
    1005             'alt'    => $alt
    1006         ) );
    1007 
    1008         /**
    1009          * Filters the avatar for the last sender in the current message thread.
    1010          *
    1011          * @since 1.0.0
    1012          *
    1013          * @param string $value User avatar string.
    1014          */
    1015         return apply_filters( 'bp_get_message_thread_avatar', bp_core_fetch_avatar( array(
    1016             'item_id' => $messages_template->thread->last_sender_id,
    1017             'type'    => $r['type'],
    1018             'alt'     => $r['alt'],
    1019             'css_id'  => $r['id'],
    1020             'class'   => $r['class'],
    1021             'width'   => $r['width'],
    1022             'height'  => $r['height'],
    1023         ) ) );
    1024     }
    1025 
    1026 /**
    1027  * Output the unread messages count for the current inbox.
    1028  */
    1029 function bp_total_unread_messages_count() {
    1030     echo bp_get_total_unread_messages_count();
    1031 }
    1032     /**
    1033      * Get the unread messages count for the current inbox.
    1034      *
    1035      * @return int
    1036      */
    1037     function bp_get_total_unread_messages_count() {
    1038 
    1039         /**
    1040          * Filters the unread messages count for the current inbox.
    1041          *
    1042          * @since 1.0.0
    1043          *
    1044          * @param int $value Unread messages count for the current inbox.
    1045          */
    1046         return apply_filters( 'bp_get_total_unread_messages_count', BP_Messages_Thread::get_inbox_count() );
    1047     }
    1048 
    1049 /**
    1050  * Output the pagination HTML for the current thread loop.
    1051  */
    1052 function bp_messages_pagination() {
    1053     echo bp_get_messages_pagination();
    1054 }
    1055     /**
    1056      * Get the pagination HTML for the current thread loop.
    1057      *
    1058      * @return string
    1059      */
    1060     function bp_get_messages_pagination() {
    1061         global $messages_template;
    1062 
    1063         /**
    1064          * Filters the pagination HTML for the current thread loop.
    1065          *
    1066          * @since 1.0.0
    1067          *
    1068          * @param int $pag_links Pagination HTML for the current thread loop.
    1069          */
    1070         return apply_filters( 'bp_get_messages_pagination', $messages_template->pag_links );
    1071     }
    1072 
    1073 /**
    1074  * Generate the "Viewing message x to y (of z messages)" string for a loop.
    1075  */
    1076 function bp_messages_pagination_count() {
    1077     global $messages_template;
    1078 
    1079     $start_num = intval( ( $messages_template->pag_page - 1 ) * $messages_template->pag_num ) + 1;
    1080     $from_num  = bp_core_number_format( $start_num );
    1081     $to_num    = bp_core_number_format( ( $start_num + ( $messages_template->pag_num - 1 ) > $messages_template->total_thread_count ) ? $messages_template->total_thread_count : $start_num + ( $messages_template->pag_num - 1 ) );
    1082     $total     = bp_core_number_format( $messages_template->total_thread_count );
    1083 
    1084     if ( 1 == $messages_template->total_thread_count ) {
    1085         $message = __( 'Viewing 1 message', 'buddypress' );
    1086     } else {
    1087         $message = sprintf( _n( 'Viewing %1$s - %2$s of %3$s message', 'Viewing %1$s - %2$s of %3$s messages', $messages_template->total_thread_count, 'buddypress' ), $from_num, $to_num, $total );
    1088     }
    1089 
    1090     echo esc_html( $message );
    1091 }
    1092 
    1093 /**
    1094  * Output the Private Message search form.
    1095  *
    1096  * @todo  Move markup to template part in: /members/single/messages/search.php
    1097  * @since 1.6.0
    1098  */
    1099 function bp_message_search_form() {
    1100 
    1101     // Get the default search text.
    1102     $default_search_value = bp_get_search_default_text( 'messages' );
    1103 
    1104     // Setup a few values based on what's being searched for.
    1105     $search_submitted     = ! empty( $_REQUEST['s'] ) ? stripslashes( $_REQUEST['s'] ) : $default_search_value;
    1106     $search_placeholder   = ( $search_submitted === $default_search_value ) ? ' placeholder="' .  esc_attr( $search_submitted ) . '"' : '';
    1107     $search_value         = ( $search_submitted !== $default_search_value ) ? ' value="'       .  esc_attr( $search_submitted ) . '"' : '';
    1108 
    1109     // Start the output buffer, so form can be filtered.
    1110     ob_start(); ?>
    1111 
    1112     <form action="" method="get" id="search-message-form">
    1113         <label for="messages_search" class="bp-screen-reader-text"><?php esc_html_e( 'Search Messages', 'buddypress' ); ?></label>
    1114         <input type="text" name="s" id="messages_search"<?php echo $search_placeholder . $search_value; ?> />
    1115         <input type="submit" class="button" id="messages_search_submit" name="messages_search_submit" value="<?php esc_html_e( 'Search', 'buddypress' ); ?>" />
    1116     </form>
    1117 
    1118     <?php
    1119 
    1120     // Get the search form from the above output buffer.
    1121     $search_form_html = ob_get_clean();
    1122 
    1123     /**
    1124      * Filters the private message component search form.
    1125      *
    1126      * @since 2.2.0
    1127      *
    1128      * @param string $search_form_html HTML markup for the message search form.
    1129      */
    1130     echo apply_filters( 'bp_message_search_form', $search_form_html );
    1131 }
    1132 
    1133 /**
    1134  * Echo the form action for Messages HTML forms.
    1135  */
    1136 function bp_messages_form_action() {
    1137     echo esc_url( bp_get_messages_form_action() );
    1138 }
    1139     /**
    1140      * Return the form action for Messages HTML forms.
    1141      *
    1142      * @return string The form action.
    1143      */
    1144     function bp_get_messages_form_action() {
    1145 
    1146         /**
    1147          * Filters the form action for Messages HTML forms.
    1148          *
    1149          * @since 1.0.0
    1150          *
    1151          * @param string $value The form action.
    1152          */
    1153         return apply_filters( 'bp_get_messages_form_action', trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/' . bp_action_variable( 0 ) ) );
    1154     }
    1155 
    1156 /**
    1157  * Output the default username for the recipient box.
    1158  */
    1159 function bp_messages_username_value() {
    1160     echo esc_attr( bp_get_messages_username_value() );
    1161 }
    1162     /**
    1163      * Get the default username for the recipient box.
    1164      *
    1165      * @return string
    1166      */
    1167     function bp_get_messages_username_value() {
    1168         if ( isset( $_COOKIE['bp_messages_send_to'] ) ) {
    1169 
    1170             /**
    1171              * Filters the default username for the recipient box.
    1172              *
    1173              * Value passed into filter is dependent on if the 'bp_messages_send_to'
    1174              * cookie or 'r' $_GET parameter is set.
    1175              *
    1176              * @since 1.0.0
    1177              *
    1178              * @param string $value Default user name.
    1179              */
    1180             return apply_filters( 'bp_get_messages_username_value', $_COOKIE['bp_messages_send_to'] );
    1181         } elseif ( isset( $_GET['r'] ) && !isset( $_COOKIE['bp_messages_send_to'] ) ) {
    1182             /** This filter is documented in bp-messages-template.php */
    1183             return apply_filters( 'bp_get_messages_username_value', $_GET['r'] );
    1184         }
    1185     }
    1186 
    1187 /**
    1188  * Output the default value for the Subject field.
    1189  */
    1190 function bp_messages_subject_value() {
    1191     echo esc_attr( bp_get_messages_subject_value() );
    1192 }
    1193     /**
    1194      * Get the default value for the Subject field.
    1195      *
    1196      * Will get a value out of $_POST['subject'] if available (ie after a
    1197      * failed submission).
    1198      *
    1199      * @return string
    1200      */
    1201     function bp_get_messages_subject_value() {
    1202 
    1203         // Sanitized in bp-messages-filters.php.
    1204         $subject = ! empty( $_POST['subject'] )
    1205             ? $_POST['subject']
    1206             : '';
    1207 
    1208         /**
    1209          * Filters the default value for the subject field.
    1210          *
    1211          * @since 1.0.0
    1212          *
    1213          * @param string $subject The default value for the subject field.
    1214          */
    1215         return apply_filters( 'bp_get_messages_subject_value', $subject );
    1216     }
    1217 
    1218 /**
    1219  * Output the default value for the Compose content field.
    1220  */
    1221 function bp_messages_content_value() {
    1222     echo esc_textarea( bp_get_messages_content_value() );
    1223 }
    1224     /**
    1225      * Get the default value fo the Compose content field.
    1226      *
    1227      * Will get a value out of $_POST['content'] if available (ie after a
    1228      * failed submission).
    1229      *
    1230      * @return string
    1231      */
    1232     function bp_get_messages_content_value() {
    1233 
    1234         // Sanitized in bp-messages-filters.php.
    1235         $content = ! empty( $_POST['content'] )
    1236             ? $_POST['content']
    1237             : '';
    1238 
    1239         /**
    1240          * Filters the default value for the content field.
    1241          *
    1242          * @since 1.0.0
    1243          *
    1244          * @param string $content The default value for the content field.
    1245          */
    1246         return apply_filters( 'bp_get_messages_content_value', $content );
    1247     }
    1248 
    1249 /**
    1250  * Output the markup for the message type dropdown.
    1251  */
    1252 function bp_messages_options() {
    1253 ?>
    1254 
    1255     <label for="message-type-select" class="bp-screen-reader-text">
    1256         <?php _e( 'Select:', 'buddypress' ) ?>
    1257      </label>
    1258 
    1259     <select name="message-type-select" id="message-type-select">
    1260         <option value=""><?php _e( 'Select', 'buddypress' ); ?></option>
    1261         <option value="read"><?php _ex('Read', 'Message dropdown filter', 'buddypress') ?></option>
    1262         <option value="unread"><?php _ex('Unread', 'Message dropdown filter', 'buddypress') ?></option>
    1263         <option value="all"><?php _ex('All', 'Message dropdown filter', 'buddypress') ?></option>
    1264     </select> &nbsp;
    1265 
    1266     <?php if ( ! bp_is_current_action( 'sentbox' ) && ! bp_is_current_action( 'notices' ) ) : ?>
    1267 
    1268         <a href="#" id="mark_as_read"><?php _ex('Mark as Read', 'Message management markup', 'buddypress') ?></a> &nbsp;
    1269         <a href="#" id="mark_as_unread"><?php _ex('Mark as Unread', 'Message management markup', 'buddypress') ?></a> &nbsp;
    1270 
    1271     <?php endif; ?>
    1272 
    1273     <a href="#" id="delete_<?php echo bp_current_action(); ?>_messages"><?php _e( 'Delete Selected', 'buddypress' ); ?></a> &nbsp;
    1274 
    1275 <?php
    1276 }
    1277 
    1278 /**
    1279  * Output the dropdown for bulk management of messages.
    1280  *
    1281  * @since 2.2.0
    1282  */
    1283 function bp_messages_bulk_management_dropdown() {
    1284     ?>
    1285     <label class="bp-screen-reader-text" for="messages-select"><?php _e( 'Select Bulk Action', 'buddypress' ); ?></label>
    1286     <select name="messages_bulk_action" id="messages-select">
    1287         <option value="" selected="selected"><?php _e( 'Bulk Actions', 'buddypress' ); ?></option>
    1288         <option value="read"><?php _e( 'Mark read', 'buddypress' ); ?></option>
    1289         <option value="unread"><?php _e( 'Mark unread', 'buddypress' ); ?></option>
    1290         <option value="delete"><?php _e( 'Delete', 'buddypress' ); ?></option>
    1291         <?php
    1292             /**
    1293              * Action to add additional options to the messages bulk management dropdown.
    1294              *
    1295              * @since 2.3.0
    1296              */
    1297             do_action( 'bp_messages_bulk_management_dropdown' );
    1298         ?>
    1299     </select>
    1300     <input type="submit" id="messages-bulk-manage" class="button action" value="<?php esc_attr_e( 'Apply', 'buddypress' ); ?>">
    1301     <?php
    1302 }
    1303 
    1304 /**
    1305  * Return whether or not the notice is currently active.
    1306  *
    1307  * @since 1.6.0
    1308  *
    1309  * @return bool
    1310  */
    1311 function bp_messages_is_active_notice() {
    1312     global $messages_template;
    1313 
    1314     $retval = ! empty( $messages_template->thread->is_active )
    1315         ? true
    1316         : false;
    1317 
    1318     /**
    1319      * Filters whether or not the notice is currently active.
    1320      *
    1321      * @since 2.1.0
    1322      *
    1323      * @param bool $retval Whether or not the notice is currently active.
    1324      */
    1325     return apply_filters( 'bp_messages_is_active_notice', $retval );
    1326 }
    1327 
    1328 /**
    1329  * Output a string for the active notice.
    1330  *
    1331  * Since 1.6 this function has been deprecated in favor of text in the theme.
    1332  *
    1333  * @since 1.0.0
    1334  * @deprecated 1.6.0
    1335  * @uses bp_get_message_is_active_notice()
    1336  * @return bool
    1337  */
    1338 function bp_message_is_active_notice() {
    1339     echo bp_get_message_is_active_notice();
    1340 }
    1341     /**
    1342      * Returns a string for the active notice.
    1343      *
    1344      * Since 1.6 this function has been deprecated in favor of text in the
    1345      * theme.
    1346      *
    1347      * @since 1.0.0
    1348      * @deprecated 1.6.0
    1349      * @uses bp_messages_is_active_notice()
    1350      */
    1351     function bp_get_message_is_active_notice() {
    1352 
    1353         $string = bp_messages_is_active_notice()
    1354             ? __( 'Currently Active', 'buddypress' )
    1355             : '';
    1356 
    1357         return apply_filters( 'bp_get_message_is_active_notice', $string );
    1358     }
    1359 
    1360 /**
    1361  * Output the ID of the current notice in the loop.
    1362  */
    1363 function bp_message_notice_id() {
    1364     echo (int) bp_get_message_notice_id();
    1365 }
    1366     /**
    1367      * Get the ID of the current notice in the loop.
    1368      *
    1369      * @return int
    1370      */
    1371     function bp_get_message_notice_id() {
    1372         global $messages_template;
    1373 
    1374         /**
    1375          * Filters the ID of the current notice in the loop.
    1376          *
    1377          * @since 1.5.0
    1378          *
    1379          * @param int $id ID of the current notice in the loop.
    1380          */
    1381         return apply_filters( 'bp_get_message_notice_id', $messages_template->thread->id );
    1382     }
    1383 
    1384 /**
    1385  * Output the post date of the current notice in the loop.
    1386  */
    1387 function bp_message_notice_post_date() {
    1388     echo bp_get_message_notice_post_date();
    1389 }
    1390     /**
    1391      * Get the post date of the current notice in the loop.
    1392      *
    1393      * @return string
    1394      */
    1395     function bp_get_message_notice_post_date() {
    1396         global $messages_template;
    1397 
    1398         /**
    1399          * Filters the post date of the current notice in the loop.
    1400          *
    1401          * @since 1.0.0
    1402          *
    1403          * @param string $value Formatted post date of the current notice in the loop.
    1404          */
    1405         return apply_filters( 'bp_get_message_notice_post_date', bp_format_time( strtotime( $messages_template->thread->date_sent ) ) );
    1406     }
    1407 
    1408 /**
    1409  * Output the subject of the current notice in the loop.
    1410  */
    1411 function bp_message_notice_subject() {
    1412     echo bp_get_message_notice_subject();
    1413 }
    1414     /**
    1415      * Get the subject of the current notice in the loop.
    1416      *
    1417      * @return string
    1418      */
    1419     function bp_get_message_notice_subject() {
    1420         global $messages_template;
    1421 
    1422         /**
    1423          * Filters the subject of the current notice in the loop.
    1424          *
    1425          * @since 1.0.0
    1426          *
    1427          * @param string $subject Subject of the current notice in the loop.
    1428          */
    1429         return apply_filters( 'bp_get_message_notice_subject', $messages_template->thread->subject );
    1430     }
    1431 
    1432 /**
    1433  * Output the text of the current notice in the loop.
    1434  */
    1435 function bp_message_notice_text() {
    1436     echo bp_get_message_notice_text();
    1437 }
    1438     /**
    1439      * Get the text of the current notice in the loop.
    1440      *
    1441      * @return string
    1442      */
    1443     function bp_get_message_notice_text() {
    1444         global $messages_template;
    1445 
    1446         /**
    1447          * Filters the text of the current notice in the loop.
    1448          *
    1449          * @since 1.0.0
    1450          *
    1451          * @param string $message Text for the current notice in the loop.
    1452          */
    1453         return apply_filters( 'bp_get_message_notice_text', $messages_template->thread->message );
    1454     }
    1455 
    1456 /**
    1457  * Output the URL for deleting the current notice.
    1458  */
    1459 function bp_message_notice_delete_link() {
    1460     echo esc_url( bp_get_message_notice_delete_link() );
    1461 }
    1462     /**
    1463      * Get the URL for deleting the current notice.
    1464      *
    1465      * @return string Delete URL.
    1466      */
    1467     function bp_get_message_notice_delete_link() {
    1468         global $messages_template;
    1469 
    1470         /**
    1471          * Filters the URL for deleting the current notice.
    1472          *
    1473          * @since 1.0.0
    1474          *
    1475          * @param string $value URL for deleting the current notice.
    1476          * @param string $value Text indicating action being executed.
    1477          */
    1478         return apply_filters( 'bp_get_message_notice_delete_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/delete/' . $messages_template->thread->id, 'messages_delete_thread' ) );
    1479     }
    1480 
    1481 /**
    1482  * Output the URL for deactivating the current notice.
    1483  */
    1484 function bp_message_activate_deactivate_link() {
    1485     echo esc_url( bp_get_message_activate_deactivate_link() );
    1486 }
    1487     /**
    1488      * Get the URL for deactivating the current notice.
    1489      *
    1490      * @return string
    1491      */
    1492     function bp_get_message_activate_deactivate_link() {
    1493         global $messages_template;
    1494 
    1495         if ( 1 === (int) $messages_template->thread->is_active ) {
    1496             $link = wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/deactivate/' . $messages_template->thread->id ), 'messages_deactivate_notice' );
    1497         } else {
    1498             $link = wp_nonce_url( trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() . '/notices/activate/' . $messages_template->thread->id ), 'messages_activate_notice' );
    1499         }
    1500 
    1501         /**
    1502          * Filters the URL for deactivating the current notice.
    1503          *
    1504          * @since 1.0.0
    1505          *
    1506          * @param string $link URL for deactivating the current notice.
    1507          */
    1508         return apply_filters( 'bp_get_message_activate_deactivate_link', $link );
    1509     }
    1510 
    1511 /**
    1512  * Output the Deactivate/Activate text for the notice action link.
    1513  */
    1514 function bp_message_activate_deactivate_text() {
    1515     echo esc_html( bp_get_message_activate_deactivate_text() );
    1516 }
    1517     /**
    1518      * Generate the text ('Deactivate' or 'Activate') for the notice action link.
    1519      *
    1520      * @return string
    1521      */
    1522     function bp_get_message_activate_deactivate_text() {
    1523         global $messages_template;
    1524 
    1525         if ( 1 === (int) $messages_template->thread->is_active  ) {
    1526             $text = __('Deactivate', 'buddypress');
    1527         } else {
    1528             $text = __('Activate', 'buddypress');
    1529         }
    1530 
    1531         /**
    1532          * Filters the "Deactivate" or "Activate" text for notice action links.
    1533          *
    1534          * @since 1.0.0
    1535          *
    1536          * @param string $text Text used for notice action links.
    1537          */
    1538         return apply_filters( 'bp_message_activate_deactivate_text', $text );
    1539     }
    1540 
    1541 /**
    1542  * Output the messages component slug.
    1543  *
    1544  * @since 1.5.0
    1545  *
    1546  * @uses bp_get_messages_slug()
    1547  */
    1548 function bp_messages_slug() {
    1549     echo bp_get_messages_slug();
    1550 }
    1551     /**
    1552      * Return the messages component slug.
    1553      *
    1554      * @since 1.5.0
    1555      *
    1556      * @return string
    1557      */
    1558     function bp_get_messages_slug() {
    1559 
    1560         /**
    1561          * Filters the messages component slug.
    1562          *
    1563          * @since 1.5.0
    1564          *
    1565          * @param string $slug Messages component slug.
    1566          */
    1567         return apply_filters( 'bp_get_messages_slug', buddypress()->messages->slug );
    1568     }
    1569 
    1570 /**
    1571  * Generate markup for currently active notices.
    1572  */
    1573 function bp_message_get_notices() {
    1574     $notice = BP_Messages_Notice::get_active();
    1575 
    1576     if ( empty( $notice ) ) {
    1577         return false;
    1578     }
    1579 
    1580     $closed_notices = bp_get_user_meta( bp_loggedin_user_id(), 'closed_notices', true );
    1581 
    1582     if ( empty( $closed_notices ) ) {
    1583         $closed_notices = array();
    1584     }
    1585 
    1586     if ( is_array( $closed_notices ) ) {
    1587         if ( !in_array( $notice->id, $closed_notices ) && $notice->id ) {
    1588             ?>
    1589             <div id="message" class="info notice" rel="n-<?php echo esc_attr( $notice->id ); ?>">
    1590                 <p>
    1591                     <strong><?php echo stripslashes( wp_filter_kses( $notice->subject ) ) ?></strong><br />
    1592                     <?php echo stripslashes( wp_filter_kses( $notice->message) ) ?>
    1593                     <a href="#" id="close-notice"><?php _e( 'Close', 'buddypress' ) ?></a>
    1594                 </p>
    1595             </div>
    1596             <?php
    1597         }
    1598     }
    1599 }
    1600 
    1601 /**
    1602  * Output the URL for the Private Message link in member profile headers.
    1603  */
    1604 function bp_send_private_message_link() {
    1605     echo esc_url( bp_get_send_private_message_link() );
    1606 }
    1607     /**
    1608      * Generate the URL for the Private Message link in member profile headers.
    1609      *
    1610      * @return bool|string False on failure, otherwise the URL.
    1611      */
    1612     function bp_get_send_private_message_link() {
    1613 
    1614         if ( bp_is_my_profile() || ! is_user_logged_in() ) {
    1615             return false;
    1616         }
    1617 
    1618         /**
    1619          * Filters the URL for the Private Message link in member profile headers.
    1620          *
    1621          * @since 1.2.10
    1622          *
    1623          * @param string $value URL for the Private Message link in member profile headers.
    1624          */
    1625         return apply_filters( 'bp_get_send_private_message_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . bp_core_get_username( bp_displayed_user_id() ) ) );
    1626     }
    1627 
    1628 /**
    1629  * Output the 'Private Message' button for member profile headers.
    1630  *
    1631  * Explicitly named function to avoid confusion with public messages.
    1632  *
    1633  * @since 1.2.6
    1634  *
    1635  * @uses bp_get_send_message_button()
    1636  */
    1637 function bp_send_private_message_button() {
    1638     echo bp_get_send_message_button();
    1639 }
    1640 
    1641 /**
    1642  * Output the 'Private Message' button for member profile headers.
    1643  */
    1644 function bp_send_message_button() {
    1645     echo bp_get_send_message_button();
    1646 }
    1647     /**
    1648      * Generate the 'Private Message' button for member profile headers.
    1649      *
    1650      * @return string
    1651      */
    1652     function bp_get_send_message_button() {
    1653         // Note: 'bp_get_send_message_button' is a legacy filter. Use
    1654         // 'bp_get_send_message_button_args' instead. See #4536.
    1655         return apply_filters( 'bp_get_send_message_button',
    1656 
    1657             /**
    1658              * Filters the "Private Message" button for member profile headers.
    1659              *
    1660              * @since 1.8.0
    1661              *
    1662              * @param array $value See {@link BP_Button}.
    1663              */
    1664             bp_get_button( apply_filters( 'bp_get_send_message_button_args', array(
    1665                 'id'                => 'private_message',
    1666                 'component'         => 'messages',
    1667                 'must_be_logged_in' => true,
    1668                 'block_self'        => true,
    1669                 'wrapper_id'        => 'send-private-message',
    1670                 'link_href'         => bp_get_send_private_message_link(),
    1671                 'link_title'        => __( 'Send a private message to this user.', 'buddypress' ),
    1672                 'link_text'         => __( 'Private Message', 'buddypress' ),
    1673                 'link_class'        => 'send-message',
    1674             ) ) )
    1675         );
    1676     }
    1677 
    1678 /**
    1679  * Output the URL of the Messages AJAX loader gif.
    1680  */
    1681 function bp_message_loading_image_src() {
    1682     echo esc_url( bp_get_message_loading_image_src() );
    1683 }
    1684     /**
    1685      * Get the URL of the Messages AJAX loader gif.
    1686      *
    1687      * @return string
    1688      */
    1689     function bp_get_message_loading_image_src() {
    1690 
    1691         /**
    1692          * Filters the URL of the Messages AJAX loader gif.
    1693          *
    1694          * @since 1.0.0
    1695          *
    1696          * @param string $value URL of the Messages AJAX loader gif.
    1697          */
    1698         return apply_filters( 'bp_get_message_loading_image_src', buddypress()->messages->image_base . '/ajax-loader.gif' );
    1699     }
    1700 
    1701 /**
    1702  * Output the markup for the message recipient tabs.
    1703  */
    1704 function bp_message_get_recipient_tabs() {
    1705     $recipients = explode( ' ', bp_get_message_get_recipient_usernames() );
    1706 
    1707     foreach ( $recipients as $recipient ) {
    1708 
    1709         $user_id = bp_is_username_compatibility_mode()
    1710             ? bp_core_get_userid( $recipient )
    1711             : bp_core_get_userid_from_nicename( $recipient );
    1712 
    1713         if ( ! empty( $user_id ) ) : ?>
    1714 
    1715             <li id="un-<?php echo esc_attr( $recipient ); ?>" class="friend-tab">
    1716                 <span><?php
    1717                     echo bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'width' => 15, 'height' => 15 ) );
    1718                     echo bp_core_get_userlink( $user_id );
    1719                 ?></span>
    1720             </li>
    1721 
    1722         <?php endif;
    1723     }
    1724 }
    1725 
    1726 /**
    1727  * Output recipient usernames for prefilling the 'To' field on the Compose screen.
    1728  */
    1729 function bp_message_get_recipient_usernames() {
    1730     echo esc_attr( bp_get_message_get_recipient_usernames() );
    1731 }
    1732     /**
    1733      * Get the recipient usernames for prefilling the 'To' field on the Compose screen.
    1734      *
    1735      * @return string
    1736      */
    1737     function bp_get_message_get_recipient_usernames() {
    1738 
    1739         // Sanitized in bp-messages-filters.php.
    1740         $recipients = isset( $_GET['r'] )
    1741             ? $_GET['r']
    1742             : '';
    1743 
    1744         /**
    1745          * Filters the recipients usernames for prefilling the 'To' field on the Compose screen.
    1746          *
    1747          * @since 1.0.0
    1748          *
    1749          * @param string $recipients Recipients usernames for 'To' field prefilling.
    1750          */
    1751         return apply_filters( 'bp_get_message_get_recipient_usernames', $recipients );
    1752     }
    175312
    175413/**
     
    1926185    }
    1927186}
    1928 
    1929 /**
    1930  * Initialize the messages template loop for a specific thread.
    1931  *
    1932  * @param array|string $args {
    1933  *     Array of arguments. All are optional.
    1934  *     @type int    $thread_id         ID of the thread whose messages you are displaying.
    1935  *                                     Default: if viewing a thread, the thread ID will be parsed from
    1936  *                                     the URL (bp_action_variable( 0 )).
    1937  *     @type string $order             'ASC' or 'DESC'. Default: 'ASC'.
    1938  *     @type bool   $update_meta_cache Whether to pre-fetch metadata for
    1939  *                                     queried message items. Default: true.
    1940  * }
    1941  * @return bool True if there are messages to display, otherwise false.
    1942  */
    1943 function bp_thread_has_messages( $args = '' ) {
    1944     global $thread_template;
    1945 
    1946     $r = bp_parse_args( $args, array(
    1947         'thread_id'         => false,
    1948         'order'             => 'ASC',
    1949         'update_meta_cache' => true,
    1950     ), 'thread_has_messages' );
    1951 
    1952     if ( empty( $r['thread_id'] ) && bp_is_messages_component() && bp_is_current_action( 'view' ) ) {
    1953         $r['thread_id'] = (int) bp_action_variable( 0 );
    1954     }
    1955 
    1956     // Set up extra args.
    1957     $extra_args = $r;
    1958     unset( $extra_args['thread_id'], $extra_args['order'] );
    1959 
    1960     $thread_template = new BP_Messages_Thread_Template( $r['thread_id'], $r['order'], $extra_args );
    1961 
    1962     return $thread_template->has_messages();
    1963 }
    1964 
    1965 /**
    1966  * Output the 'ASC' or 'DESC' messages order string for this loop.
    1967  */
    1968 function bp_thread_messages_order() {
    1969     echo esc_attr( bp_get_thread_messages_order() );
    1970 }
    1971     /**
    1972      * Get the 'ASC' or 'DESC' messages order string for this loop.
    1973      *
    1974      * @return string
    1975      */
    1976     function bp_get_thread_messages_order() {
    1977         global $thread_template;
    1978         return $thread_template->thread->messages_order;
    1979     }
    1980 
    1981 /**
    1982  * Check whether there are more messages to iterate over.
    1983  *
    1984  * @return bool
    1985  */
    1986 function bp_thread_messages() {
    1987     global $thread_template;
    1988 
    1989     return $thread_template->messages();
    1990 }
    1991 
    1992 /**
    1993  * Set up the current thread inside the loop.
    1994  *
    1995  * @return object
    1996  */
    1997 function bp_thread_the_message() {
    1998     global $thread_template;
    1999 
    2000     return $thread_template->the_message();
    2001 }
    2002 
    2003 /**
    2004  * Output the ID of the thread that the current loop belongs to.
    2005  */
    2006 function bp_the_thread_id() {
    2007     echo (int) bp_get_the_thread_id();
    2008 }
    2009     /**
    2010      * Get the ID of the thread that the current loop belongs to.
    2011      *
    2012      * @return int
    2013      */
    2014     function bp_get_the_thread_id() {
    2015         global $thread_template;
    2016 
    2017         /**
    2018          * Filters the ID of the thread that the current loop belongs to.
    2019          *
    2020          * @since 1.1.0
    2021          *
    2022          * @param int $thread_id ID of the thread.
    2023          */
    2024         return apply_filters( 'bp_get_the_thread_id', $thread_template->thread->thread_id );
    2025     }
    2026 
    2027 /**
    2028  * Output the subject of the thread currently being iterated over.
    2029  */
    2030 function bp_the_thread_subject() {
    2031     echo bp_get_the_thread_subject();
    2032 }
    2033     /**
    2034      * Get the subject of the thread currently being iterated over.
    2035      *
    2036      * @return string
    2037      */
    2038     function bp_get_the_thread_subject() {
    2039         global $thread_template;
    2040 
    2041         /**
    2042          * Filters the subject of the thread currently being iterated over.
    2043          *
    2044          * @since 1.1.0
    2045          *
    2046          * @return string $last_message_subject Subject of the thread currently being iterated over.
    2047          */
    2048         return apply_filters( 'bp_get_the_thread_subject', $thread_template->thread->last_message_subject );
    2049     }
    2050 
    2051 /**
    2052  * Get a list of thread recipients or a "x recipients" string.
    2053  *
    2054  * In BuddyPress 2.2.0, this parts of this functionality were moved into the
    2055  * members/single/messages/single.php template. This function is no longer used
    2056  * by BuddyPress.
    2057  *
    2058  * @return string
    2059  */
    2060 function bp_get_the_thread_recipients() {
    2061     if ( 5 <= bp_get_thread_recipients_count() ) {
    2062         $recipients = sprintf( __( '%s recipients', 'buddypress' ), number_format_i18n( bp_get_thread_recipients_count() ) );
    2063     } else {
    2064         $recipients = bp_get_thread_recipients_list();
    2065     }
    2066 
    2067     return apply_filters( 'bp_get_the_thread_recipients', $recipients );
    2068 }
    2069 
    2070 /**
    2071  * Get the number of recipients in the current thread.
    2072  *
    2073  * @since 2.2.0
    2074  *
    2075  * @return int
    2076  */
    2077 function bp_get_thread_recipients_count() {
    2078     global $thread_template;
    2079     return count( $thread_template->thread->recipients );
    2080 }
    2081 
    2082 /**
    2083  * Get the max number of recipients to list in the 'Conversation between...' gloss.
    2084  *
    2085  * @since 2.3.0
    2086  *
    2087  * @return int
    2088  */
    2089 function bp_get_max_thread_recipients_to_list() {
    2090     /**
    2091      * Filters the max number of recipients to list in the 'Conversation between...' gloss.
    2092      *
    2093      * @since 2.3.0
    2094      *
    2095      * @param int $count Recipient count. Default: 5.
    2096      */
    2097     return (int) apply_filters( 'bp_get_max_thread_recipients_to_list', 5 );
    2098 }
    2099 
    2100 /**
    2101  * Output HTML links to recipients in the current thread.
    2102  *
    2103  * @since 2.2.0
    2104  */
    2105 function bp_the_thread_recipients_list() {
    2106     echo bp_get_thread_recipients_list();
    2107 }
    2108     /**
    2109      * Generate HTML links to the profiles of recipients in the current thread.
    2110      *
    2111      * @since 2.2.0
    2112      *
    2113      * @return string
    2114      */
    2115     function bp_get_thread_recipients_list() {
    2116         global $thread_template;
    2117 
    2118         $recipient_links = array();
    2119 
    2120         foreach( (array) $thread_template->thread->recipients as $recipient ) {
    2121             if ( (int) $recipient->user_id !== bp_loggedin_user_id() ) {
    2122                 $recipient_link = bp_core_get_userlink( $recipient->user_id );
    2123 
    2124                 if ( empty( $recipient_link ) ) {
    2125                     $recipient_link = __( 'Deleted User', 'buddypress' );
    2126                 }
    2127 
    2128                 $recipient_links[] = $recipient_link;
    2129             }
    2130         }
    2131 
    2132         /**
    2133          * Filters the HTML links to the profiles of recipients in the current thread.
    2134          *
    2135          * @since 2.2.0
    2136          *
    2137          * @param string $value Comma-separated list of recipient HTML links for current thread.
    2138          */
    2139         return apply_filters( 'bp_get_the_thread_recipients_list', implode( ', ', $recipient_links ) );
    2140     }
    2141 
    2142 /**
    2143  * Echo the ID of the current message in the thread.
    2144  *
    2145  * @since 1.9.0
    2146  */
    2147 function bp_the_thread_message_id() {
    2148     echo (int) bp_get_the_thread_message_id();
    2149 }
    2150     /**
    2151      * Get the ID of the current message in the thread.
    2152      *
    2153      * @since 1.9.0
    2154      *
    2155      * @return int
    2156      */
    2157     function bp_get_the_thread_message_id() {
    2158         global $thread_template;
    2159 
    2160         $thread_message_id = isset( $thread_template->message->id )
    2161             ? (int) $thread_template->message->id
    2162             : null;
    2163 
    2164         /**
    2165          * Filters the ID of the current message in the thread.
    2166          *
    2167          * @since 1.9.0
    2168          *
    2169          * @param int $thread_message_id ID of the current message in the thread.
    2170          */
    2171         return apply_filters( 'bp_get_the_thread_message_id', $thread_message_id );
    2172     }
    2173 
    2174 /**
    2175  * Output the CSS classes for messages within a single thread.
    2176  *
    2177  * @since 2.1.0
    2178  */
    2179 function bp_the_thread_message_css_class() {
    2180     echo esc_attr( bp_get_the_thread_message_css_class() );
    2181 }
    2182     /**
    2183      * Generate the CSS classes for messages within a single thread.
    2184      *
    2185      * @since 2.1.0
    2186      *
    2187      * @return string
    2188      */
    2189     function bp_get_the_thread_message_css_class() {
    2190         global $thread_template;
    2191 
    2192         $classes = array();
    2193 
    2194         // Zebra-striping.
    2195         $classes[] = bp_get_the_thread_message_alt_class();
    2196 
    2197         // ID of the sender.
    2198         $classes[] = 'sent-by-' . intval( $thread_template->message->sender_id );
    2199 
    2200         // Whether the sender is the same as the logged-in user.
    2201         if ( bp_loggedin_user_id() == $thread_template->message->sender_id ) {
    2202             $classes[] = 'sent-by-me';
    2203         }
    2204 
    2205         /**
    2206          * Filters the CSS classes for messages within a single thread.
    2207          *
    2208          * @since 2.1.0
    2209          *
    2210          * @param array $classes Array of classes to add to the HTML class attribute.
    2211          */
    2212         $classes = apply_filters( 'bp_get_the_thread_message_css_class', $classes );
    2213 
    2214         return implode( ' ', $classes );
    2215     }
    2216 
    2217 /**
    2218  * Output the CSS class used for message zebra striping.
    2219  */
    2220 function bp_the_thread_message_alt_class() {
    2221     echo esc_attr( bp_get_the_thread_message_alt_class() );
    2222 }
    2223     /**
    2224      * Get the CSS class used for message zebra striping.
    2225      *
    2226      * @return string
    2227      */
    2228     function bp_get_the_thread_message_alt_class() {
    2229         global $thread_template;
    2230 
    2231         if ( $thread_template->current_message % 2 == 1 ) {
    2232             $class = 'even alt';
    2233         } else {
    2234             $class = 'odd';
    2235         }
    2236 
    2237         /**
    2238          * Filters the CSS class used for message zebra striping.
    2239          *
    2240          * @since 1.1.0
    2241          *
    2242          * @param string $class Class determined to be next for zebra striping effect.
    2243          */
    2244         return apply_filters( 'bp_get_the_thread_message_alt_class', $class );
    2245     }
    2246 
    2247 /**
    2248  * Output the ID for message sender within a single thread.
    2249  *
    2250  * @since 2.1.0
    2251  */
    2252 function bp_the_thread_message_sender_id() {
    2253     echo (int) bp_get_the_thread_message_sender_id();
    2254 }
    2255     /**
    2256      * Return the ID for message sender within a single thread.
    2257      *
    2258      * @since 2.1.0
    2259      *
    2260      * @return string
    2261      */
    2262     function bp_get_the_thread_message_sender_id() {
    2263         global $thread_template;
    2264 
    2265         $user_id = ! empty( $thread_template->message->sender_id )
    2266             ? $thread_template->message->sender_id
    2267             : 0;
    2268 
    2269         /**
    2270          * Filters the ID for message sender within a single thread.
    2271          *
    2272          * @since 2.1.0
    2273          *
    2274          * @param int $user_id ID of the message sender.
    2275          */
    2276         return (int) apply_filters( 'bp_get_the_thread_message_sender_id', (int) $user_id );
    2277     }
    2278 
    2279 /**
    2280  * Output the avatar for the current message sender.
    2281  *
    2282  * @param array|string $args See {@link bp_get_the_thread_message_sender_avatar_thumb()}
    2283  *                           for a description.
    2284  */
    2285 function bp_the_thread_message_sender_avatar( $args = '' ) {
    2286     echo bp_get_the_thread_message_sender_avatar_thumb( $args );
    2287 }
    2288     /**
    2289      * Get the avatar for the current message sender.
    2290      *
    2291      * @param array|string $args {
    2292      *     Array of arguments. See {@link bp_core_fetch_avatar()} for more
    2293      *     complete details. All arguments are optional.
    2294      *     @type string $type   Avatar type. Default: 'thumb'.
    2295      *     @type int    $width  Avatar width. Default: default for your $type.
    2296      *     @type int    $height Avatar height. Default: default for your $type.
    2297      * }
    2298      * @return string <img> tag containing the avatar.
    2299      */
    2300     function bp_get_the_thread_message_sender_avatar_thumb( $args = '' ) {
    2301         global $thread_template;
    2302 
    2303         $r = bp_parse_args( $args, array(
    2304             'type'   => 'thumb',
    2305             'width'  => false,
    2306             'height' => false,
    2307         ) );
    2308 
    2309         /**
    2310          * Filters the avatar for the current message sender.
    2311          *
    2312          * @since 1.1.0
    2313          *
    2314          * @param string $value <img> tag containing the avatar value.
    2315          */
    2316         return apply_filters( 'bp_get_the_thread_message_sender_avatar_thumb', bp_core_fetch_avatar( array(
    2317             'item_id' => $thread_template->message->sender_id,
    2318             'type'    => $r['type'],
    2319             'width'   => $r['width'],
    2320             'height'  => $r['height'],
    2321             'alt'     => bp_core_get_user_displayname( $thread_template->message->sender_id )
    2322         ) ) );
    2323     }
    2324 
    2325 /**
    2326  * Output a link to the sender of the current message.
    2327  *
    2328  * @since 1.1.0
    2329  */
    2330 function bp_the_thread_message_sender_link() {
    2331     echo esc_url( bp_get_the_thread_message_sender_link() );
    2332 }
    2333     /**
    2334      * Get a link to the sender of the current message.
    2335      *
    2336      * @since 1.1.0
    2337      *
    2338      * @return string
    2339      */
    2340     function bp_get_the_thread_message_sender_link() {
    2341         global $thread_template;
    2342 
    2343         /**
    2344          * Filters the link to the sender of the current message.
    2345          *
    2346          * @since 1.1.0
    2347          *
    2348          * @param string $value Link to the sender of the current message.
    2349          */
    2350         return apply_filters( 'bp_get_the_thread_message_sender_link', bp_core_get_userlink( $thread_template->message->sender_id, false, true ) );
    2351     }
    2352 
    2353 /**
    2354  * Output the display name of the sender of the current message.
    2355  *
    2356  * @since 1.1.0
    2357  */
    2358 function bp_the_thread_message_sender_name() {
    2359     echo esc_html( bp_get_the_thread_message_sender_name() );
    2360 }
    2361     /**
    2362      * Get the display name of the sender of the current message.
    2363      *
    2364      * @since 1.1.0
    2365      *
    2366      * @return string
    2367      */
    2368     function bp_get_the_thread_message_sender_name() {
    2369         global $thread_template;
    2370 
    2371         $display_name = bp_core_get_user_displayname( $thread_template->message->sender_id );
    2372 
    2373         if ( empty( $display_name ) ) {
    2374             $display_name = __( 'Deleted User', 'buddypress' );
    2375         }
    2376 
    2377         /**
    2378          * Filters the display name of the sender of the current message.
    2379          *
    2380          * @since 1.1.0
    2381          *
    2382          * @param string $display_name Display name of the sender of the current message.
    2383          */
    2384         return apply_filters( 'bp_get_the_thread_message_sender_name', $display_name );
    2385     }
    2386 
    2387 /**
    2388  * Output the URL for deleting the current thread.
    2389  *
    2390  * @since 1.5.0
    2391  */
    2392 function bp_the_thread_delete_link() {
    2393     echo esc_url( bp_get_the_thread_delete_link() );
    2394 }
    2395     /**
    2396      * Get the URL for deleting the current thread.
    2397      *
    2398      * @since 1.5.0
    2399      *
    2400      * @return string URL
    2401      */
    2402     function bp_get_the_thread_delete_link() {
    2403 
    2404         /**
    2405          * Filters the URL for deleting the current thread.
    2406          *
    2407          * @since 1.0.0
    2408          *
    2409          * @param string $value URL for deleting the current thread.
    2410          * @param string $value Text indicating action being executed.
    2411          */
    2412         return apply_filters( 'bp_get_message_thread_delete_link', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox/delete/' . bp_get_the_thread_id(), 'messages_delete_thread' ) );
    2413     }
    2414 
    2415 /**
    2416  * Output the 'Sent x hours ago' string for the current message.
    2417  *
    2418  * @since 1.1.0
    2419  */
    2420 function bp_the_thread_message_time_since() {
    2421     echo bp_get_the_thread_message_time_since();
    2422 }
    2423     /**
    2424      * Generate the 'Sent x hours ago' string for the current message.
    2425      *
    2426      * @since 1.1.0
    2427      *
    2428      * @return string
    2429      */
    2430     function bp_get_the_thread_message_time_since() {
    2431 
    2432         /**
    2433          * Filters the 'Sent x hours ago' string for the current message.
    2434          *
    2435          * @since 1.1.0
    2436          *
    2437          * @param string $value Default text of 'Sent x hours ago'.
    2438          */
    2439         return apply_filters( 'bp_get_the_thread_message_time_since', sprintf( __( 'Sent %s', 'buddypress' ), bp_core_time_since( bp_get_the_thread_message_date_sent() ) ) );
    2440     }
    2441 
    2442 /**
    2443  * Output the timestamp for the current message.
    2444  *
    2445  * @since 2.1.0
    2446  */
    2447 function bp_the_thread_message_date_sent() {
    2448     echo bp_get_the_thread_message_date_sent();
    2449 }
    2450     /**
    2451      * Generate the 'Sent x hours ago' string for the current message.
    2452      *
    2453      * @since 2.1.0
    2454      *
    2455      * @uses strtotime() To convert the message string into a usable timestamp.
    2456      *
    2457      * @return int
    2458      */
    2459     function bp_get_the_thread_message_date_sent() {
    2460         global $thread_template;
    2461 
    2462         /**
    2463          * Filters the date sent value for the current message as a timestamp.
    2464          *
    2465          * @since 2.1.0
    2466          *
    2467          * @param string $value Timestamp of the date sent value for the current message.
    2468          */
    2469         return apply_filters( 'bp_get_the_thread_message_date_sent', strtotime( $thread_template->message->date_sent ) );
    2470     }
    2471 
    2472 /**
    2473  * Output the content of the current message in the loop.
    2474  *
    2475  * @since 1.1.0
    2476  */
    2477 function bp_the_thread_message_content() {
    2478     echo bp_get_the_thread_message_content();
    2479 }
    2480     /**
    2481      * Get the content of the current message in the loop.
    2482      *
    2483      * @since 1.1.0
    2484      *
    2485      * @return string
    2486      */
    2487     function bp_get_the_thread_message_content() {
    2488         global $thread_template;
    2489 
    2490         /**
    2491          * Filters the content of the current message in the loop.
    2492          *
    2493          * @since 1.1.0
    2494          *
    2495          * @param string $message The content of the current message in the loop.
    2496          */
    2497         return apply_filters( 'bp_get_the_thread_message_content', $thread_template->message->message );
    2498     }
    2499 
    2500 /** Embeds *******************************************************************/
    2501 
    2502 /**
    2503  * Enable oEmbed support for Messages.
    2504  *
    2505  * @since 1.5.0
    2506  *
    2507  * @see BP_Embed
    2508  */
    2509 function bp_messages_embed() {
    2510     add_filter( 'embed_post_id',         'bp_get_the_thread_message_id' );
    2511     add_filter( 'bp_embed_get_cache',    'bp_embed_message_cache',      10, 3 );
    2512     add_action( 'bp_embed_update_cache', 'bp_embed_message_save_cache', 10, 3 );
    2513 }
    2514 add_action( 'thread_loop_start', 'bp_messages_embed' );
    2515 
    2516 /**
    2517  * Fetch a private message item's cached embeds.
    2518  *
    2519  * Used during {@link BP_Embed::parse_oembed()} via {@link bp_messages_embed()}.
    2520  *
    2521  * @since 2.2.0
    2522  *
    2523  * @param string $cache    An empty string passed by BP_Embed::parse_oembed() for
    2524  *                         functions like this one to filter.
    2525  * @param int    $id       The ID of the message item.
    2526  * @param string $cachekey The cache key generated in BP_Embed::parse_oembed().
    2527  * @return mixed The cached embeds for this message item.
    2528  */
    2529 function bp_embed_message_cache( $cache, $id, $cachekey ) {
    2530     return bp_messages_get_meta( $id, $cachekey );
    2531 }
    2532 
    2533 /**
    2534  * Set a private message item's embed cache.
    2535  *
    2536  * Used during {@link BP_Embed::parse_oembed()} via {@link bp_messages_embed()}.
    2537  *
    2538  * @since 2.2.0
    2539  *
    2540  * @param string $cache    An empty string passed by BP_Embed::parse_oembed() for
    2541  *                         functions like this one to filter.
    2542  * @param string $cachekey The cache key generated in BP_Embed::parse_oembed().
    2543  * @param int    $id       The ID of the message item.
    2544  */
    2545 function bp_embed_message_save_cache( $cache, $cachekey, $id ) {
    2546     bp_messages_update_meta( $id, $cachekey, $cache );
    2547 }
Note: See TracChangeset for help on using the changeset viewer.