Beispiel #1
0
 /**
  * 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);
 }
 /**
  * @group get_recipients
  * @group cache
  */
 public function test_get_recipients_cache_should_be_busted_when_thread_is_unread()
 {
     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 unread
     $current_user = get_current_user_id();
     $this->set_current_user($u2);
     messages_mark_thread_unread($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() . '/');
}