Exemple #1
0
function messages_new_message( $args = '' ) {
	global $bp;

	$defaults = array(
		'thread_id' => false, // false for a new message, thread id for a reply to a thread.
		'sender_id' => $bp->loggedin_user->id,
		'recipients' => false, // Can be an array of usernames, user_ids or mixed.
		'subject' => false,
		'content' => false,
		'date_sent' => bp_core_current_time()
	);

	$r = wp_parse_args( $args, $defaults );
	extract( $r, EXTR_SKIP );

	if ( !$sender_id || !$content )
		return false;

	/* Create a new message object */
	$message = new BP_Messages_Message;
	$message->thread_id = $thread_id;
	$message->sender_id = $sender_id;
	$message->subject = $subject;
	$message->message = $content;
	$message->date_sent = $date_sent;

	// If we have a thread ID, use the existing recipients, otherwise use the recipients passed
	if ( $thread_id ) {
		$thread = new BP_Messages_Thread( $thread_id );
		$message->recipients = $thread->get_recipients();

		// Strip the sender from the recipient list if they exist
		if ( isset( $message->recipients[$sender_id] ) )
			unset( $message->recipients[$sender_id] );

		if ( empty( $message->subject ) )
			$message->subject = sprintf( __( 'Re: %s', 'buddypress' ), $thread->messages[0]->subject );

	// No thread ID, so make some adjustments
	} else {
		if ( empty( $recipients ) )
			return false;

		if ( empty( $message->subject ) )
			$message->subject = __( 'No Subject', 'buddypress' );

		/* Loop the recipients and convert all usernames to user_ids where needed */
		foreach( (array) $recipients as $recipient ) {
			if ( is_numeric( trim( $recipient ) ) )
				$recipient_ids[] = (int)trim( $recipient );

			if ( $recipient_id = bp_core_get_userid( trim( $recipient ) ) )
				$recipient_ids[] = (int)$recipient_id;
		}

		/* Strip the sender from the recipient list if they exist */
		if ( $key = array_search( $sender_id, (array)$recipient_ids ) )
			unset( $recipient_ids[$key] );

		/* Remove duplicates */
		$recipient_ids = array_unique( (array)$recipient_ids );

		if ( empty( $recipient_ids ) )
			return false;

		/* Format this to match existing recipients */
		foreach( (array)$recipient_ids as $i => $recipient_id ) {
			$message->recipients[$i] = new stdClass;
			$message->recipients[$i]->user_id = $recipient_id;
		}
	}

	if ( $message->send() ) {
		require_once( BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php' );

		// Send screen notifications to the recipients
		foreach ( (array)$message->recipients as $recipient )
			bp_core_add_notification( $message->id, $recipient->user_id, 'messages', 'new_message' );

		// Send email notifications to the recipients
		messages_notification_new_message( array( 'message_id' => $message->id, 'sender_id' => $message->sender_id, 'subject' => $message->subject, 'content' => $message->message, 'recipients' => $message->recipients, 'thread_id' => $message->thread_id) );

		do_action( 'messages_message_sent', &$message );

		return $message->thread_id;
	}

	return false;
}
function messages_send_message($recipients, $subject, $content, $thread_id, $from_ajax = false, $from_template = false, $is_reply = false)
{
    global $pmessage;
    global $message, $type;
    global $bp, $current_user;
    if (!check_admin_referer('messages_send_message')) {
        return false;
    }
    messages_add_callback_values($recipients, $subject, $content);
    if (isset($_POST['send-notice'])) {
        if (messages_send_notice($subject, $content, $from_template)) {
            bp_core_add_message(__('Notice posted successfully.', 'buddypress'));
        } else {
            bp_core_add_message(__('There was an error posting that notice.', 'buddypress'), 'error');
        }
        bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/notices');
        return true;
    }
    $recipients = explode(' ', $recipients);
    // If there are no recipients
    if (count($recipients) < 1) {
        if (!$from_ajax) {
            bp_core_add_message(__('Please enter at least one valid user to send this message to.', 'buddypress'), 'error');
            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
        } else {
            return array('status' => 0, 'message' => __('There was an error sending the reply, please try again.', 'buddypress'));
        }
        // If there is only 1 recipient and it is the logged in user.
    } else {
        if (1 == count($recipients) && $recipients[0] == $current_user->user_login) {
            bp_core_add_message(__('You must send your message to one or more users not including yourself.', 'buddypress'), 'error');
            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
            // If the subject or content boxes are empty.
        } else {
            if (empty($subject) || empty($content)) {
                if (!$from_ajax) {
                    bp_core_add_message(__('Please make sure you fill in all the fields.', 'buddypress'), 'error');
                    bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
                } else {
                    return array('status' => 0, 'message' => __('Please make sure you have typed a message before sending a reply.', 'buddypress'));
                }
                // Passed validation continue.
            } else {
                // Strip the logged in user from the recipient list if they exist
                if ($key = array_search($current_user->user_login, $recipients)) {
                    unset($recipients[$key]);
                }
                $pmessage = new BP_Messages_Message();
                $pmessage->sender_id = $bp->loggedin_user->id;
                $pmessage->subject = $subject;
                $pmessage->message = $content;
                $pmessage->thread_id = $thread_id;
                $pmessage->date_sent = time();
                $pmessage->message_order = 0;
                // TODO
                $pmessage->sender_is_group = 0;
                if ($is_reply) {
                    $thread = new BP_Messages_Thread($thread_id);
                    $pmessage->recipients = $thread->get_recipients();
                } else {
                    $pmessage->recipients = BP_Messages_Message::get_recipient_ids($recipients);
                }
                if (!is_null($pmessage->recipients)) {
                    if (!$pmessage->send()) {
                        $message = __('Message could not be sent, please try again.', 'buddypress');
                        $type = 'error';
                        if ($from_ajax) {
                            return array('status' => 0, 'message' => $message);
                        } else {
                            bp_core_add_message($message, $type);
                            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
                        }
                    } else {
                        $message = __('Message sent successfully!', 'buddypress') . ' <a href="' . $bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $pmessage->thread_id . '">' . __('View Message', 'buddypress') . '</a> &raquo;';
                        $type = 'success';
                        // Send screen notifications to the recipients
                        for ($i = 0; $i < count($pmessage->recipients); $i++) {
                            if ($pmessage->recipients[$i] != $bp->loggedin_user->id) {
                                bp_core_add_notification($pmessage->id, $pmessage->recipients[$i], 'messages', 'new_message');
                            }
                        }
                        // Send email notifications to the recipients
                        require_once BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php';
                        messages_notification_new_message(array('item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => 'messages', 'component_action' => 'message_sent', 'is_private' => 1));
                        do_action('messages_send_message', array('item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => 'messages', 'component_action' => 'message_sent', 'is_private' => 1));
                        if ($from_ajax) {
                            return array('status' => 1, 'message' => $message, 'reply' => $pmessage);
                        } else {
                            bp_core_add_message($message);
                            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/inbox');
                        }
                    }
                } else {
                    $message = __('Message could not be sent, please try again.', 'buddypress');
                    $type = 'error';
                    if ($from_ajax) {
                        return array('status' => 0, 'message' => $message);
                    } else {
                        bp_core_add_message($message, $type);
                        bp_core_redirect($bp->loggedin_user->domain . $bp->messages->slug . '/compose');
                    }
                }
            }
        }
    }
}
/**
 * Create a new message.
 *
 * @since 2.4.0 Added 'error_type' as an additional $args parameter.
 *
 * @param array|string $args {
 *     Array of arguments.
 *     @type int    $sender_id  Optional. ID of the user who is sending the
 *                              message. Default: ID of the logged-in user.
 *     @type int    $thread_id  Optional. ID of the parent thread. Leave blank to
 *                              create a new thread for the message.
 *     @type array  $recipients IDs or usernames of message recipients. If this
 *                              is an existing thread, it is unnecessary to pass a $recipients
 *                              argument - existing thread recipients will be assumed.
 *     @type string $subject    Optional. Subject line for the message. For
 *                              existing threads, the existing subject will be used. For new
 *                              threads, 'No Subject' will be used if no $subject is provided.
 *     @type string $content    Content of the message. Cannot be empty.
 *     @type string $date_sent  Date sent, in 'Y-m-d H:i:s' format. Default: current date/time.
 *     @type string $error_type Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
 * }
 * @return int|bool ID of the message thread on success, false on failure.
 */
function messages_new_message($args = '')
{
    // Parse the default arguments.
    $r = bp_parse_args($args, array('sender_id' => bp_loggedin_user_id(), 'thread_id' => false, 'recipients' => array(), 'subject' => false, 'content' => false, 'date_sent' => bp_core_current_time(), 'error_type' => 'bool'), 'messages_new_message');
    // Bail if no sender or no content.
    if (empty($r['sender_id']) || empty($r['content'])) {
        if ('wp_error' === $r['error_type']) {
            if (empty($r['sender_id'])) {
                $error_code = 'messages_empty_sender';
                $feedback = __('Your message was not sent. Please use a valid sender.', 'buddypress');
            } else {
                $error_code = 'messages_empty_content';
                $feedback = __('Your message was not sent. Please enter some content.', 'buddypress');
            }
            return new WP_Error($error_code, $feedback);
        } else {
            return false;
        }
    }
    // Create a new message object.
    $message = new BP_Messages_Message();
    $message->thread_id = $r['thread_id'];
    $message->sender_id = $r['sender_id'];
    $message->subject = $r['subject'];
    $message->message = $r['content'];
    $message->date_sent = $r['date_sent'];
    // If we have a thread ID...
    if (!empty($r['thread_id'])) {
        // ...use the existing recipients
        $thread = new BP_Messages_Thread($r['thread_id']);
        $message->recipients = $thread->get_recipients();
        // Strip the sender from the recipient list, and unset them if they are
        // not alone. If they are alone, let them talk to themselves.
        if (isset($message->recipients[$r['sender_id']]) && count($message->recipients) > 1) {
            unset($message->recipients[$r['sender_id']]);
        }
        // Set a default reply subject if none was sent.
        if (empty($message->subject)) {
            $message->subject = sprintf(__('Re: %s', 'buddypress'), $thread->messages[0]->subject);
        }
        // ...otherwise use the recipients passed
    } else {
        // Bail if no recipients.
        if (empty($r['recipients'])) {
            if ('wp_error' === $r['error_type']) {
                return new WP_Error('message_empty_recipients', __('Message could not be sent. Please enter a recipient.', 'buddypress'));
            } else {
                return false;
            }
        }
        // Set a default subject if none exists.
        if (empty($message->subject)) {
            $message->subject = __('No Subject', 'buddypress');
        }
        // Setup the recipients array.
        $recipient_ids = array();
        // Invalid recipients are added to an array, for future enhancements.
        $invalid_recipients = array();
        // Loop the recipients and convert all usernames to user_ids where needed.
        foreach ((array) $r['recipients'] as $recipient) {
            // Trim spaces and skip if empty.
            $recipient = trim($recipient);
            if (empty($recipient)) {
                continue;
            }
            // Check user_login / nicename columns first
            // @see http://buddypress.trac.wordpress.org/ticket/5151.
            if (bp_is_username_compatibility_mode()) {
                $recipient_id = bp_core_get_userid(urldecode($recipient));
            } else {
                $recipient_id = bp_core_get_userid_from_nicename($recipient);
            }
            // Check against user ID column if no match and if passed recipient is numeric.
            if (empty($recipient_id) && is_numeric($recipient)) {
                if (bp_core_get_core_userdata((int) $recipient)) {
                    $recipient_id = (int) $recipient;
                }
            }
            // Decide which group to add this recipient to.
            if (empty($recipient_id)) {
                $invalid_recipients[] = $recipient;
            } else {
                $recipient_ids[] = (int) $recipient_id;
            }
        }
        // Strip the sender from the recipient list, and unset them if they are
        // not alone. If they are alone, let them talk to themselves.
        $self_send = array_search($r['sender_id'], $recipient_ids);
        if (!empty($self_send) && count($recipient_ids) > 1) {
            unset($recipient_ids[$self_send]);
        }
        // Remove duplicates & bail if no recipients.
        $recipient_ids = array_unique($recipient_ids);
        if (empty($recipient_ids)) {
            if ('wp_error' === $r['error_type']) {
                return new WP_Error('message_invalid_recipients', __('Message could not be sent because you have entered an invalid username. Please try again.', 'buddypress'));
            } else {
                return false;
            }
        }
        // Format this to match existing recipients.
        foreach ((array) $recipient_ids as $i => $recipient_id) {
            $message->recipients[$i] = new stdClass();
            $message->recipients[$i]->user_id = $recipient_id;
        }
    }
    // Bail if message failed to send.
    $send = $message->send();
    if (false === is_int($send)) {
        if ('wp_error' === $r['error_type']) {
            if (is_wp_error($send)) {
                return $send;
            } else {
                return new WP_Error('message_generic_error', __('Message was not sent. Please try again.', 'buddypress'));
            }
        }
        return false;
    }
    /**
     * Fires after a message has been successfully sent.
     *
     * @since 1.1.0
     *
     * @param BP_Messages_Message $message Message object. Passed by reference.
     */
    do_action_ref_array('messages_message_sent', array(&$message));
    // Return the thread ID.
    return $message->thread_id;
}
function messages_new_message($args = '')
{
    $defaults = array('sender_id' => bp_loggedin_user_id(), 'thread_id' => false, 'recipients' => false, 'subject' => false, 'content' => false, 'date_sent' => bp_core_current_time());
    $r = wp_parse_args($args, $defaults);
    extract($r, EXTR_SKIP);
    if (empty($sender_id) || empty($content)) {
        return false;
    }
    // Create a new message object
    $message = new BP_Messages_Message();
    $message->thread_id = $thread_id;
    $message->sender_id = $sender_id;
    $message->subject = $subject;
    $message->message = $content;
    $message->date_sent = $date_sent;
    // If we have a thread ID, use the existing recipients, otherwise use the recipients passed
    if (!empty($thread_id)) {
        $thread = new BP_Messages_Thread($thread_id);
        $message->recipients = $thread->get_recipients();
        // Strip the sender from the recipient list if they exist
        if (isset($message->recipients[$sender_id])) {
            unset($message->recipients[$sender_id]);
        }
        if (empty($message->subject)) {
            $message->subject = sprintf(__('Re: %s', 'buddypress'), $thread->messages[0]->subject);
        }
        // No thread ID, so make some adjustments
    } else {
        if (empty($recipients)) {
            return false;
        }
        if (empty($message->subject)) {
            $message->subject = __('No Subject', 'buddypress');
        }
        $recipient_ids = array();
        // Invalid recipients are added to an array, for future enhancements
        $invalid_recipients = array();
        // Loop the recipients and convert all usernames to user_ids where needed
        foreach ((array) $recipients as $recipient) {
            $recipient = trim($recipient);
            if (empty($recipient)) {
                continue;
            }
            $recipient_id = false;
            // input was numeric
            if (is_numeric($recipient)) {
                // do a check against the user ID column first
                if (bp_core_get_core_userdata((int) $recipient)) {
                    $recipient_id = (int) $recipient;
                } else {
                    if (bp_is_username_compatibility_mode()) {
                        $recipient_id = bp_core_get_userid((int) $recipient);
                    } else {
                        $recipient_id = bp_core_get_userid_from_nicename((int) $recipient);
                    }
                }
            } else {
                if (bp_is_username_compatibility_mode()) {
                    $recipient_id = bp_core_get_userid($recipient);
                } else {
                    $recipient_id = bp_core_get_userid_from_nicename($recipient);
                }
            }
            if (!$recipient_id) {
                $invalid_recipients[] = $recipient;
            } else {
                $recipient_ids[] = (int) $recipient_id;
            }
        }
        // Strip the sender from the recipient list if they exist
        if ($key = array_search($sender_id, (array) $recipient_ids)) {
            unset($recipient_ids[$key]);
        }
        // Remove duplicates
        $recipient_ids = array_unique((array) $recipient_ids);
        if (empty($recipient_ids)) {
            return false;
        }
        // Format this to match existing recipients
        foreach ((array) $recipient_ids as $i => $recipient_id) {
            $message->recipients[$i] = new stdClass();
            $message->recipients[$i]->user_id = $recipient_id;
        }
    }
    if ($message->send()) {
        // Send screen notifications to the recipients
        foreach ((array) $message->recipients as $recipient) {
            bp_core_add_notification($message->id, $recipient->user_id, 'messages', 'new_message');
        }
        // Send email notifications to the recipients
        messages_notification_new_message(array('message_id' => $message->id, 'sender_id' => $message->sender_id, 'subject' => $message->subject, 'content' => $message->message, 'recipients' => $message->recipients, 'thread_id' => $message->thread_id));
        do_action_ref_array('messages_message_sent', array(&$message));
        return $message->thread_id;
    }
    return false;
}
/**
 * Create a new message.
 *
 * @param array $args {
 *     Array of arguments.
 *     @type int $sender_id Optional. ID of the user who is sending the
 *           message. Default: ID of the logged-in user.
 *     @type int $thread_id Optional. ID of the parent thread. Leave blank to
 *           create a new thread for the message.
 *     @type array $recipients IDs or usernames of message recipients. If this
 *           is an existing thread, it is unnecessary to pass a $recipients
 *           argument - existing thread recipients will be assumed.
 *     @type string $subject Optional. Subject line for the message. For
 *           existing threads, the existing subject will be used. For new
 *           threads, 'No Subject' will be used if no $subject is provided.
 *     @type string $content Content of the message. Cannot be empty.
 *     @type string $date_sent Date sent, in 'Y-m-d H:i:s' format. Default:
 *           current date/time.
 * }
 * @return int|bool ID of the message thread on success, false on failure.
 */
function messages_new_message( $args = '' ) {

	// Parse the default arguments
	$r = bp_parse_args( $args, array(
		'sender_id'  => bp_loggedin_user_id(),
		'thread_id'  => false,   // false for a new message, thread id for a reply to a thread.
		'recipients' => array(), // Can be an array of usernames, user_ids or mixed.
		'subject'    => false,
		'content'    => false,
		'date_sent'  => bp_core_current_time()
	), 'messages_new_message' );

	// Bail if no sender or no content
	if ( empty( $r['sender_id'] ) || empty( $r['content'] ) ) {
		return false;
	}

	// Create a new message object
	$message            = new BP_Messages_Message;
	$message->thread_id = $r['thread_id'];
	$message->sender_id = $r['sender_id'];
	$message->subject   = $r['subject'];
	$message->message   = $r['content'];
	$message->date_sent = $r['date_sent'];

	// If we have a thread ID...
	if ( ! empty( $r['thread_id'] ) ) {

		// ...use the existing recipients
		$thread              = new BP_Messages_Thread( $r['thread_id'] );
		$message->recipients = $thread->get_recipients();

		// Strip the sender from the recipient list, and unset them if they are
		// not alone. If they are alone, let them talk to themselves.
		if ( isset( $message->recipients[ $r['sender_id'] ] ) && ( count( $message->recipients ) > 1 ) ) {
			unset( $message->recipients[ $r['sender_id'] ] );
		}

		// Set a default reply subject if none was sent
		if ( empty( $message->subject ) ) {
			$message->subject = sprintf( __( 'Re: %s', 'buddypress' ), $thread->messages[0]->subject );
		}

	// ...otherwise use the recipients passed
	} else {

		// Bail if no recipients
		if ( empty( $r['recipients'] ) ) {
			return false;
		}

		// Set a default subject if none exists
		if ( empty( $message->subject ) ) {
			$message->subject = __( 'No Subject', 'buddypress' );
		}

		// Setup the recipients array
		$recipient_ids 	    = array();

		// Invalid recipients are added to an array, for future enhancements
		$invalid_recipients = array();

		// Loop the recipients and convert all usernames to user_ids where needed
		foreach( (array) $r['recipients'] as $recipient ) {

			// Trim spaces and skip if empty
			$recipient = trim( $recipient );
			if ( empty( $recipient ) ) {
				continue;
			}

			// Check user_login / nicename columns first
			// @see http://buddypress.trac.wordpress.org/ticket/5151
			if ( bp_is_username_compatibility_mode() ) {
				$recipient_id = bp_core_get_userid( urldecode( $recipient ) );
			} else {
				$recipient_id = bp_core_get_userid_from_nicename( $recipient );
			}

			// Check against user ID column if no match and if passed recipient is numeric
			if ( empty( $recipient_id ) && is_numeric( $recipient ) ) {
				if ( bp_core_get_core_userdata( (int) $recipient ) ) {
					$recipient_id = (int) $recipient;
				}
			}

			// Decide which group to add this recipient to
			if ( empty( $recipient_id ) ) {
				$invalid_recipients[] = $recipient;
			} else {
				$recipient_ids[] = (int) $recipient_id;
			}
		}

		// Strip the sender from the recipient list, and unset them if they are
		// not alone. If they are alone, let them talk to themselves.
		$self_send = array_search( $r['sender_id'], $recipient_ids );
		if ( ! empty( $self_send ) && ( count( $recipient_ids ) > 1 ) ) {
			unset( $recipient_ids[ $self_send ] );
		}

		// Remove duplicates & bail if no recipients
		$recipient_ids = array_unique( $recipient_ids );
		if ( empty( $recipient_ids ) ) {
			return false;
		}

		// Format this to match existing recipients
		foreach( (array) $recipient_ids as $i => $recipient_id ) {
			$message->recipients[$i]          = new stdClass;
			$message->recipients[$i]->user_id = $recipient_id;
		}
	}

	// Bail if message failed to send
	if ( ! $message->send() ) {
		return false;
	}

	/**
	 * Fires after a message has been successfully sent.
	 *
	 * @since BuddyPress (1.1.0)
	 *
	 * @param BP_Messages_Message $message Message object. Passed by reference.
	 */
	do_action_ref_array( 'messages_message_sent', array( &$message ) );

	// Return the thread ID
	return $message->thread_id;
}