/** * Get all messages * * @param WP_REST_Request $request * @return array|WP_Error */ public function get_items($request) { /* filter messages */ $args = array(); $args['box'] = $request['box']; if ($request['box'] == 'starred') { $args['meta_query'] = array(array('key' => 'starred_by_user', 'value' => bp_loggedin_user_id())); } if (isset($request['filter'])) { $args = array_merge($args, $request['filter']); unset($args['filter']); } global $messages_template; $data = array(); if (bp_has_message_threads($args)) { while (bp_message_threads()) { bp_message_thread(); $single_msg = array('thread_id' => $messages_template->thread->thread_id, 'read' => bp_message_thread_has_unread() ? false : true, 'total_messages' => bp_get_message_thread_total_count($messages_template->thread->thread_id), 'unread_messages' => bp_get_message_thread_unread_count($messages_template->thread->thread_id), 'avatar' => bp_core_fetch_avatar(array('item_id' => $messages_template->thread->last_sender_id, 'width' => 25, 'height' => 25, 'html' => false)), 'from' => bp_core_get_username($messages_template->thread->last_sender_id), 'from_id' => $messages_template->thread->last_sender_id, 'last_message_date' => $messages_template->thread->last_message_date, 'subject' => bp_get_message_thread_subject(), 'excerpt' => bp_get_message_thread_excerpt(), 'content' => bp_get_message_thread_content()); if (bp_is_active('messages', 'star')) { $single_msg['star'] = strpos(bp_get_the_message_star_action_link(array('thread_id' => bp_get_message_thread_id())), 'unstar') ? true : false; } if ($args['box'] == 'sentbox') { foreach ($messages_template->thread->recipients as $user => $userdata) { if ((int) $user !== bp_loggedin_user_id()) { $single_msg['to'][$user]['name'] = bp_core_get_username($user); } } } $links['self'] = array('href' => rest_url(sprintf(BP_API_SLUG . '/messages/%d', $messages_template->thread->thread_id))); if ($args['box'] == 'sentbox') { $links['collection'] = array('href' => rest_url(BP_API_SLUG . '/messages?box=sentbox')); } else { $links['collection'] = array('href' => rest_url(BP_API_SLUG . '/messages/')); } $single_msg['_links'] = $links; $data[] = $single_msg; } } else { return new WP_Error('bp_json_messages', __('No Messages Found.', BP_API_PLUGIN_SLUG), array('status' => 200)); } $data = apply_filters('bp_json_prepare_messages', $data); return new WP_REST_Response($data, 200); }
* Fires inside the messages box table row to add a new column. * * This is to primarily add a <td> cell to the message box table. Use the * related 'bp_messages_inbox_list_header' hook to add a <th> header cell. * * @since 1.1.0 */ do_action('bp_messages_inbox_list_item'); ?> <?php if (bp_is_active('messages', 'star')) { ?> <td class="thread-star"> <?php bp_the_message_star_action_link(array('thread_id' => bp_get_message_thread_id())); ?> </td> <?php } ?> <td class="thread-options"> <?php if (bp_message_thread_has_unread()) { ?> <a class="read" href="<?php bp_the_message_thread_mark_read_url(); ?> "><?php _e('Read', 'buddypress');
function bp_message_thread_id() { echo bp_get_message_thread_id(); }
/** * Get markup for the current thread's total and unread count. * * @param int|bool $thread_id Optional. ID of the thread. Default: current thread ID. * @return string Markup displaying the total and unread count for the thread. */ function bp_get_message_thread_total_and_unread_count($thread_id = false) { if (false === $thread_id) { $thread_id = bp_get_message_thread_id(); } $total = bp_get_message_thread_total_count($thread_id); $unread = bp_get_message_thread_unread_count($thread_id); return sprintf('<span class="thread-count">(%1$s)</span> <span class="bp-screen-reader-text">%2$s</span>', number_format_i18n($total), sprintf(_n('%d unread', '%d unread', $unread, 'buddypress'), number_format_i18n($unread))); }
/** * Return the link or raw URL for starring or unstarring a message. * * @since 2.3.0 * * @param array $args { * Array of arguments. * @type int $user_id The user ID. Defaults to the logged-in user ID. * @type int $thread_id The message thread ID. Default: 0. If not zero, this takes precedence over * $message_id. * @type int $message_id The individual message ID. If on a single thread page, defaults to the * current message ID in the message loop. * @type bool $url_only Whether to return the URL only. If false, returns link with markup. * Default: false. * @type string $text_unstar Link text for the 'unstar' action. Only applicable if $url_only is false. * @type string $text_star Link text for the 'star' action. Only applicable if $url_only is false. * @type string $title_unstar Link title for the 'unstar' action. Only applicable if $url_only is false. * @type string $title_star Link title for the 'star' action. Only applicable if $url_only is false. * @type string $title_unstar_thread Link title for the 'unstar' action when displayed in a thread loop. * Only applicable if $message_id is set and if $url_only is false. * @type string $title_star_thread Link title for the 'star' action when displayed in a thread loop. * Only applicable if $message_id is set and if $url_only is false. * } * @return string */ function bp_get_the_message_star_action_link($args = array()) { // Default user ID. $user_id = bp_displayed_user_id() ? bp_displayed_user_id() : bp_loggedin_user_id(); $r = bp_parse_args($args, array('user_id' => (int) $user_id, 'thread_id' => 0, 'message_id' => (int) bp_get_the_thread_message_id(), 'url_only' => false, 'text_unstar' => __('Unstar', 'buddypress'), 'text_star' => __('Star', 'buddypress'), 'title_unstar' => __('Starred', 'buddypress'), 'title_star' => __('Not starred', 'buddypress'), 'title_unstar_thread' => __('Remove all starred messages in this thread', 'buddypress'), 'title_star_thread' => __('Star the first message in this thread', 'buddypress')), 'messages_star_action_link'); // Check user ID and determine base user URL. switch ($r['user_id']) { // Current user. case bp_loggedin_user_id(): $user_domain = bp_loggedin_user_domain(); break; // Displayed user. // Displayed user. case bp_displayed_user_id(): $user_domain = bp_displayed_user_domain(); break; // Empty or other. // Empty or other. default: $user_domain = bp_core_get_user_domain($r['user_id']); break; } // Bail if no user domain was calculated. if (empty($user_domain)) { return ''; } // Define local variables. $retval = $bulk_attr = ''; // Thread ID. if ((int) $r['thread_id'] > 0) { // See if we're in the loop. if (bp_get_message_thread_id() == $r['thread_id']) { // Grab all message ids. $mids = wp_list_pluck($GLOBALS['messages_template']->thread->messages, 'id'); // Make sure order is ASC. // Order is DESC when used in the thread loop by default. $mids = array_reverse($mids); // Pull up the thread. } else { $thread = new BP_Messages_Thread($r['thread_id']); $mids = wp_list_pluck($thread->messages, 'id'); } $is_starred = false; $message_id = 0; foreach ($mids as $mid) { // Try to find the first msg that is starred in a thread. if (true === bp_messages_is_message_starred($mid)) { $is_starred = true; $message_id = $mid; break; } } // No star, so default to first message in thread. if (empty($message_id)) { $message_id = $mids[0]; } $message_id = (int) $message_id; // Nonce. $nonce = wp_create_nonce("bp-messages-star-{$message_id}"); if (true === $is_starred) { $action = 'unstar'; $bulk_attr = ' data-star-bulk="1"'; $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/'; } else { $action = 'star'; $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; } $title = $r["title_{$action}_thread"]; // Message ID. } else { $message_id = (int) $r['message_id']; $is_starred = bp_messages_is_message_starred($message_id); $nonce = wp_create_nonce("bp-messages-star-{$message_id}"); if (true === $is_starred) { $action = 'unstar'; $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/'; } else { $action = 'star'; $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; } $title = $r["title_{$action}"]; } /** * Filters the star action URL for starring / unstarring a message. * * @since 2.3.0 * * @param string $retval URL for starring / unstarring a message. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). */ $retval = esc_url(apply_filters('bp_get_the_message_star_action_urlonly', $retval, $r)); if (true === (bool) $r['url_only']) { return $retval; } /** * Filters the star action link, including markup. * * @since 2.3.0 * * @param string $retval Link for starring / unstarring a message, including markup. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). */ return apply_filters('bp_get_the_message_star_action_link', '<a title="' . esc_attr($title) . '" class="message-action-' . esc_attr($action) . '" data-star-status="' . esc_attr($action) . '" data-star-nonce="' . esc_attr($nonce) . '"' . $bulk_attr . ' data-message-id="' . esc_attr((int) $message_id) . '" href="' . $retval . '"><span class="icon"></span> <span class="bp-screen-reader-text">' . $r['text_' . $action] . '</span></a>', $r); }
/** * Return the link or raw URL for starring or unstarring a message. * * @since BuddyPress (2.3.0) * * @param array $args { * Array of arguments. * @type int $user_id The user ID. Defaults to the logged-in user ID. * @type int $thread_id The message thread ID. Default: 0. If not zero, this takes precedence over * $message_id. * @type int $message_id The individual message ID. If on a single thread page, defaults to the * current message ID in the message loop. * @type bool $url_only Whether to return the URL only. If false, returns link with markup. * Default: false. * @type string $text_unstar Link text for the 'unstar' action. Only applicable if $url_only is false. * @type string $text_star Link text for the 'star' action. Only applicable if $url_only is false. * @type string $title_unstar Link title for the 'unstar' action. Only applicable if $url_only is false. * @type string $title_star Link title for the 'star' action. Only applicable if $url_only is false. * @type string $title_unstar_thread Link title for the 'unstar' action when displayed in a thread loop. * Only applicable if $message_id is set and if $url_only is false. * @type string $title_star_thread Link title for the 'star' action when displayed in a thread loop. * Only applicable if $message_id is set and if $url_only is false. * } * @return string */ function bp_get_the_message_star_action_link($args = array()) { $r = bp_parse_args($args, array('user_id' => bp_loggedin_user_id(), 'thread_id' => 0, 'message_id' => (int) bp_get_the_thread_message_id(), 'url_only' => false, 'text_unstar' => __('Unstar', 'buddypress'), 'text_star' => __('Star', 'buddypress'), 'title_unstar' => __('Starred', 'buddypress'), 'title_star' => __('Not starred', 'buddypress'), 'title_unstar_thread' => __('Remove all starred messages in this thread', 'buddypress'), 'title_star_thread' => __('Star the first message in this thread', 'buddypress')), 'messages_star_action_link'); $retval = $bulk_attr = ''; if (0 === $r['user_id']) { return $retval; } // get user domain if ($r['user_id'] == bp_loggedin_user_id()) { $user_domain = bp_loggedin_user_domain(); } elseif ($r['user_id'] == bp_displayed_user_domain()) { $user_domain = bp_displayed_user_domain(); } else { $user_domain = bp_core_get_user_domain($user_id); } // thread ID if ((int) $r['thread_id'] > 0) { // see if we're in the loop if (bp_get_message_thread_id() == $r['thread_id']) { // grab all message ids $mids = wp_list_pluck($GLOBALS['messages_template']->thread->messages, 'id'); // make sure order is ASC // order is DESC when used in the thread loop by default $mids = array_reverse($mids); // pull up the thread } else { $thread = new BP_Messages_thread($r['thread_id']); $mids = wp_list_pluck($thread->messages, 'id'); } $is_starred = false; $message_id = 0; foreach ($mids as $mid) { // try to find the first msg that is starred in a thread if (true === bp_messages_is_message_starred($mid)) { $is_starred = true; $message_id = $mid; break; } } // no star, so default to first message in thread if (empty($message_id)) { $message_id = $mids[0]; } // nonce $nonce = wp_create_nonce("bp-messages-star-{$message_id}"); if ($is_starred) { $action = 'unstar'; $bulk_attr = ' data-star-bulk="1"'; $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/'; } else { $action = 'star'; $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; } $title = $r["title_{$action}_thread"]; // message ID } else { $message_id = (int) $r['message_id']; $is_starred = bp_messages_is_message_starred($message_id); $nonce = wp_create_nonce("bp-messages-star-{$message_id}"); if ($is_starred) { $action = 'unstar'; $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/'; } else { $action = 'star'; $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/'; } $title = $r["title_{$action}"]; } /** * Filters the star action URL for starring / unstarring a message. * * @since BuddyPress (2.3.0) * * @param string $retval URL for starring / unstarring a message. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). */ $retval = esc_url(apply_filters('bp_get_the_message_star_action_urlonly', $retval, $r)); if (true === (bool) $r['url_only']) { return $retval; } /** * Filters the star action link, including markup. * * @since BuddyPress (2.3.0) * * @param string $retval Link for starring / unstarring a message, including markup. * @param array $r Parsed link arguments. See $args in bp_get_the_message_star_action_link(). */ return apply_filters('bp_get_the_message_star_action_link', '<a title="' . esc_attr($title) . '" class="message-action-' . $action . '" data-star-status="' . $action . '" data-star-nonce="' . $nonce . '"' . $bulk_attr . ' data-message-id="' . esc_attr((int) $message_id) . '" href="' . $retval . '"><span class="icon"></span> <span class="bp-screen-reader-text">' . $r['text_' . $action] . '</span></a>', $r); }
/** * Returns an object with messages for the current user * @return Object Messages */ public function get_messages() { /* Possible parameters: * String box: the box you the messages are in (possible values are 'inbox', 'sentbox', 'notices', default is 'inbox') * int per_page: items to be displayed per page (default 10) * boolean limit: maximum numbers of emtries (default no limit) */ $this->initVars('message'); $oReturn = new stdClass(); $aParams['box'] = $this->box; $aParams['per_page'] = $this->per_page; $aParams['max'] = $this->limit; if (bp_has_message_threads($aParams)) { while (bp_message_threads()) { bp_message_thread(); $aTemp = new stdClass(); $aTemp->id = bp_get_message_thread_id(); $aTemp->from = bp_get_message_thread_from(); $aTemp->to = bp_get_message_thread_to(); $aTemp->subject = bp_get_message_thread_subject(); $aTemp->excerpt = bp_get_message_thread_excerpt(); $aTemp->link = bp_get_message_thread_view_link(); $oReturn->messages[] = $aTemp; } } else { return $this->error('message'); } return $oReturn; }