Ejemplo n.º 1
0
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');
                    }
                }
            }
        }
    }
}
Ejemplo n.º 2
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_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;
}