function messages_action_view_message() { global $thread_id, $bp; if (!bp_is_messages_component() || !bp_is_current_action('view')) { return false; } $thread_id = (int) bp_action_variable(0); if (!$thread_id || !messages_is_valid_thread($thread_id) || !messages_check_thread_access($thread_id) && !is_super_admin()) { bp_core_redirect(bp_displayed_user_domain() . bp_get_messages_slug()); } // Check if a new reply has been submitted if (isset($_POST['send'])) { // Check the nonce check_admin_referer('messages_send_message', 'send_message_nonce'); // Send the reply if (messages_new_message(array('thread_id' => $thread_id, 'subject' => $_POST['subject'], 'content' => $_POST['content']))) { bp_core_add_message(__('Your reply was sent successfully', 'buddypress')); } else { bp_core_add_message(__('There was a problem sending your reply, please try again', 'buddypress'), 'error'); } bp_core_redirect(bp_displayed_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id . '/'); } // Mark message read messages_mark_thread_read($thread_id); // Decrease the unread count in the nav before it's rendered $name = sprintf(__('Messages <span>%s</span>', 'buddypress'), bp_get_total_unread_messages_count()); $bp->bp_nav[$bp->messages->slug]['name'] = $name; do_action('messages_action_view_message'); bp_core_new_subnav_item(array('name' => sprintf(__('From: %s', 'buddypress'), BP_Messages_Thread::get_last_sender($thread_id)), 'slug' => 'view', 'parent_url' => trailingslashit(bp_displayed_user_domain() . bp_get_messages_slug()), 'parent_slug' => bp_get_messages_slug(), 'screen_function' => true, 'position' => 40, 'user_has_access' => bp_is_my_profile(), 'link' => bp_displayed_user_domain() . bp_get_messages_slug() . '/view/' . (int) $thread_id)); bp_core_load_template(apply_filters('messages_template_view_message', 'members/single/home')); }
function messages_action_conversation() { if (!bp_is_messages_component() || !bp_is_current_action('view')) { return false; } $thread_id = (int) bp_action_variable(0); if (!$thread_id || !messages_is_valid_thread($thread_id) || !messages_check_thread_access($thread_id) && !bp_current_user_can('bp_moderate')) { bp_core_redirect(trailingslashit(bp_displayed_user_domain() . bp_get_messages_slug())); } // Check if a new reply has been submitted if (isset($_POST['send'])) { // Check the nonce check_admin_referer('messages_send_message', 'send_message_nonce'); // Send the reply if (messages_new_message(array('thread_id' => $thread_id, 'subject' => !empty($_POST['subject']) ? $_POST['subject'] : false, 'content' => $_POST['content']))) { bp_core_add_message(__('Your reply was sent successfully', 'buddypress')); } else { bp_core_add_message(__('There was a problem sending your reply, please try again', 'buddypress'), 'error'); } bp_core_redirect(bp_displayed_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id . '/'); } // Mark message read messages_mark_thread_read($thread_id); do_action('messages_action_conversation'); }
/** * @group counts */ public function test_get_unread_count() { $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); // send a private message $t1 = messages_new_message(array('sender_id' => $u1, 'recipients' => array($u2), 'subject' => 'A new message', 'content' => 'Hey there!')); // get unread count for $u2 $this->set_current_user($u2); $this->assertEquals(1, messages_get_unread_count($u2)); // send another message and get recheck unread count $t2 = messages_new_message(array('sender_id' => $u1, 'recipients' => array($u2), 'subject' => 'A new message', 'content' => 'Hey there!')); $this->assertEquals(2, messages_get_unread_count($u2)); // mark one message as read messages_mark_thread_read($t1); // recheck unread count $this->assertEquals(1, messages_get_unread_count($u2)); }
/** * Update a single message * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function update_item($request) { $id = (int) $request['id']; if (!messages_check_thread_access($id)) { return new WP_Error('bp_json_message_invalid_id', __('Message ID is invalid.'), array('status' => 400)); } switch ($request['action']) { case 'read': messages_mark_thread_read($id); break; case 'unread': messages_mark_thread_unread($id); break; case 'star': $thread = new BP_Messages_thread($id); $mids = wp_list_pluck($thread->messages, 'id'); bp_messages_star_set_action(array('action' => 'star', 'message_id' => $mids[0])); break; case 'unstar': bp_messages_star_set_action(array('action' => 'unstar', 'thread_id' => $id, 'bulk' => true)); break; /* Single message */ // case 'star_single': // bp_messages_star_set_action( array( // 'action' => 'star', // 'message_id' => $id, // ) ); // break; // case 'unstar_single': // bp_messages_star_set_action( array( // 'action' => 'unstar', // 'message_id' => $id, // ) ); // break; } $response = $this->get_item(array('id' => $id, 'context' => 'edit')); return rest_ensure_response($response); }
function messages_action_view_message() { global $bp, $thread_id; if ( $bp->current_component != $bp->messages->slug || $bp->current_action != 'view' ) return false; $thread_id = $bp->action_variables[0]; if ( !$thread_id || !messages_is_valid_thread( $thread_id ) || ( !messages_check_thread_access($thread_id) && !is_super_admin() ) ) bp_core_redirect( $bp->displayed_user->domain . $bp->current_component ); /* Check if a new reply has been submitted */ if ( isset( $_POST['send'] ) ) { /* Check the nonce */ check_admin_referer( 'messages_send_message', 'send_message_nonce' ); /* Send the reply */ if ( messages_new_message( array( 'thread_id' => $thread_id, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) ) bp_core_add_message( __( 'Your reply was sent successfully', 'buddypress' ) ); else bp_core_add_message( __( 'There was a problem sending your reply, please try again', 'buddypress' ), 'error' ); bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/view/' . $thread_id . '/' ); } /* Mark message read */ messages_mark_thread_read( $thread_id ); do_action( 'messages_action_view_message' ); bp_core_new_subnav_item( array( 'name' => sprintf( __( 'From: %s', 'buddypress'), BP_Messages_Thread::get_last_sender($thread_id) ), 'slug' => 'view', 'parent_url' => $bp->loggedin_user->domain . $bp->messages->slug . '/', 'parent_slug' => $bp->messages->slug, 'screen_function' => true, 'position' => 40, 'user_has_access' => bp_is_my_profile() ) ); bp_core_load_template( apply_filters( 'messages_template_view_message', 'members/single/home' ) ); }
/** * @group get_recipients * @group cache */ public function test_get_recipients_cache_should_be_busted_when_thread_is_read() { global $wpdb; $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $t1 = $this->factory->message->create(array('sender_id' => $u1, 'recipients' => array($u2), 'subject' => 'Foo')); $thread = new BP_Messages_Thread($t1); $recipients = $thread->get_recipients(); // Verify that the cache is populated. $num_queries = $wpdb->num_queries; $recipients_cached = $thread->get_recipients(); $this->assertEquals($num_queries, $wpdb->num_queries); // Mark thread as read $current_user = get_current_user_id(); $this->set_current_user($u2); messages_mark_thread_read($t1); // Cache should be empty. $this->assertFalse(wp_cache_get('thread_recipients_' . $t1, 'bp_messages')); $this->set_current_user($current_user); }
/** * Handle bulk management (mark as read/unread, delete) of message threads. * * @since BuddyPress (2.2.0) * * @return bool Returns false on failure. Otherwise redirects back to the * message box URL. */ function bp_messages_action_bulk_manage() { if (!bp_is_messages_component() || bp_is_current_action('notices') || !bp_is_action_variable('bulk-manage', 0)) { return false; } $action = !empty($_POST['messages_bulk_action']) ? $_POST['messages_bulk_action'] : ''; $nonce = !empty($_POST['messages_bulk_nonce']) ? $_POST['messages_bulk_nonce'] : ''; $messages = !empty($_POST['message_ids']) ? $_POST['message_ids'] : ''; $messages = wp_parse_id_list($messages); // Bail if no action or no IDs. if (!in_array($action, array('delete', 'read', 'unread')) || empty($messages) || empty($nonce)) { bp_core_redirect(bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/'); } // Check the nonce. if (!wp_verify_nonce($nonce, 'messages_bulk_nonce')) { return false; } // Make sure the user has access to all notifications before managing them. foreach ($messages as $message) { if (!messages_check_thread_access($message)) { bp_core_add_message(__('There was a problem managing your messages.', 'buddypress'), 'error'); bp_core_redirect(bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/'); } } // Delete, mark as read or unread depending on the user 'action'. switch ($action) { case 'delete': foreach ($messages as $message) { messages_delete_thread($message); } bp_core_add_message(__('Messages deleted.', 'buddypress')); break; case 'read': foreach ($messages as $message) { messages_mark_thread_read($message); } bp_core_add_message(__('Messages marked as read', 'buddypress')); break; case 'unread': foreach ($messages as $message) { messages_mark_thread_unread($message); } bp_core_add_message(__('Messages marked as unread.', 'buddypress')); break; } // Redirect back to message box. bp_core_redirect(bp_displayed_user_domain() . bp_get_messages_slug() . '/' . bp_current_action() . '/'); }