<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][8618] trunk/src/bp-messages/bp-messages-functions.php: Clean up `messages_new_message()` to use `bp_parse_args()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/8618">8618</a></dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2014-07-15 22:03:53 +0000 (Tue, 15 Jul 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Clean up `messages_new_message()` to use `bp_parse_args()`.
Also re-allows previous functionality where members could respond to threads they are alone in. If someone wants to talk to themselves, who are we to stop them?</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpmessagesbpmessagesfunctionsphp">trunk/src/bp-messages/bp-messages-functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpmessagesbpmessagesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-messages/bp-messages-functions.php (8617 => 8618)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-messages/bp-messages-functions.php 2014-07-15 22:00:13 UTC (rev 8617)
+++ trunk/src/bp-messages/bp-messages-functions.php 2014-07-15 22:03:53 UTC (rev 8618)
</span><span class="lines">@@ -38,64 +38,76 @@
</span><span class="cx"> */
</span><span class="cx"> function messages_new_message( $args = '' ) {
</span><span class="cx">
</span><del>- $defaults = array (
</del><ins>+ // Parse the default arguments
+ $r = bp_parse_args( $args, array(
</ins><span class="cx"> 'sender_id' => bp_loggedin_user_id(),
</span><del>- 'thread_id' => false, // false for a new message, thread id for a reply to a thread.
- 'recipients' => false, // Can be an array of usernames, user_ids or mixed.
</del><ins>+ '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.
</ins><span class="cx"> 'subject' => false,
</span><span class="cx"> 'content' => false,
</span><span class="cx"> 'date_sent' => bp_core_current_time()
</span><del>- );
</del><ins>+ ), 'messages_new_message' );
</ins><span class="cx">
</span><del>- $r = wp_parse_args( $args, $defaults );
- extract( $r, EXTR_SKIP );
-
- if ( empty( $sender_id ) || empty( $content ) )
</del><ins>+ // Bail if no sender or no content
+ if ( empty( $r['sender_id'] ) || empty( $r['content'] ) ) {
</ins><span class="cx"> return false;
</span><ins>+ }
</ins><span class="cx">
</span><span class="cx"> // Create a new message object
</span><span class="cx"> $message = new BP_Messages_Message;
</span><del>- $message->thread_id = $thread_id;
- $message->sender_id = $sender_id;
- $message->subject = $subject;
- $message->message = $content;
- $message->date_sent = $date_sent;
</del><ins>+ $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'];
</ins><span class="cx">
</span><del>- // 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 );
</del><ins>+ // If we have a thread ID...
+ if ( ! empty( $r['thread_id'] ) ) {
+
+ // ...use the existing recipients
+ $thread = new BP_Messages_Thread( $r['thread_id'] );
</ins><span class="cx"> $message->recipients = $thread->get_recipients();
</span><span class="cx">
</span><del>- // Strip the sender from the recipient list if they exist
- if ( isset( $message->recipients[$sender_id] ) )
- unset( $message->recipients[$sender_id] );
</del><ins>+ // 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'] ] );
+ }
</ins><span class="cx">
</span><del>- if ( empty( $message->subject ) )
</del><ins>+ // Set a default reply subject if none was sent
+ if ( empty( $message->subject ) ) {
</ins><span class="cx"> $message->subject = sprintf( __( 'Re: %s', 'buddypress' ), $thread->messages[0]->subject );
</span><ins>+ }
</ins><span class="cx">
</span><del>- // No thread ID, so make some adjustments
</del><ins>+ // ...otherwise use the recipients passed
</ins><span class="cx"> } else {
</span><del>- if ( empty( $recipients ) )
</del><ins>+
+ // Bail if no recipients
+ if ( empty( $r['recipients'] ) ) {
</ins><span class="cx"> return false;
</span><ins>+ }
</ins><span class="cx">
</span><del>- if ( empty( $message->subject ) )
</del><ins>+ // Set a default subject if none exists
+ if ( empty( $message->subject ) ) {
</ins><span class="cx"> $message->subject = __( 'No Subject', 'buddypress' );
</span><ins>+ }
</ins><span class="cx">
</span><ins>+ // Setup the recipients array
</ins><span class="cx"> $recipient_ids = array();
</span><span class="cx">
</span><span class="cx"> // Invalid recipients are added to an array, for future enhancements
</span><span class="cx"> $invalid_recipients = array();
</span><span class="cx">
</span><span class="cx"> // Loop the recipients and convert all usernames to user_ids where needed
</span><del>- foreach( (array) $recipients as $recipient ) {
</del><ins>+ foreach( (array) $r['recipients'] as $recipient ) {
+
+ // Trim spaces and skip if empty
</ins><span class="cx"> $recipient = trim( $recipient );
</span><del>-
- if ( empty( $recipient ) )
</del><ins>+ if ( empty( $recipient ) ) {
</ins><span class="cx"> continue;
</span><ins>+ }
</ins><span class="cx">
</span><del>- $recipient_id = false;
-
- // check user_login / nicename columns first
</del><ins>+ // Check user_login / nicename columns first
</ins><span class="cx"> // @see http://buddypress.trac.wordpress.org/ticket/5151
</span><span class="cx"> if ( bp_is_username_compatibility_mode() ) {
</span><span class="cx"> $recipient_id = bp_core_get_userid( urldecode( $recipient ) );
</span><span class="lines">@@ -103,29 +115,33 @@
</span><span class="cx"> $recipient_id = bp_core_get_userid_from_nicename( $recipient );
</span><span class="cx"> }
</span><span class="cx">
</span><del>- // check against user ID column if no match and if passed recipient is numeric
- if ( ! $recipient_id && is_numeric( $recipient ) ) {
</del><ins>+ // Check against user ID column if no match and if passed recipient is numeric
+ if ( empty( $recipient_id ) && is_numeric( $recipient ) ) {
</ins><span class="cx"> if ( bp_core_get_core_userdata( (int) $recipient ) ) {
</span><span class="cx"> $recipient_id = (int) $recipient;
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- if ( ! $recipient_id ) {
</del><ins>+ // Decide which group to add this recipient to
+ if ( empty( $recipient_id ) ) {
</ins><span class="cx"> $invalid_recipients[] = $recipient;
</span><span class="cx"> } else {
</span><span class="cx"> $recipient_ids[] = (int) $recipient_id;
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- // Strip the sender from the recipient list if they exist
- if ( $key = array_search( $sender_id, (array) $recipient_ids ) )
- unset( $recipient_ids[$key] );
</del><ins>+ // 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 ] );
+ }
</ins><span class="cx">
</span><del>- // Remove duplicates
- $recipient_ids = array_unique( (array) $recipient_ids );
-
- if ( empty( $recipient_ids ) )
</del><ins>+ // Remove duplicates & bail if no recipients
+ $recipient_ids = array_unique( $recipient_ids );
+ if ( empty( $recipient_ids ) ) {
</ins><span class="cx"> return false;
</span><ins>+ }
</ins><span class="cx">
</span><span class="cx"> // Format this to match existing recipients
</span><span class="cx"> foreach( (array) $recipient_ids as $i => $recipient_id ) {
</span><span class="lines">@@ -134,13 +150,16 @@
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- if ( $message->send() ) {
- do_action_ref_array( 'messages_message_sent', array( &$message ) );
-
- return $message->thread_id;
</del><ins>+ // Bail if message failed to send
+ if ( ! $message->send() ) {
+ return false;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- return false;
</del><ins>+ // Allow additional actions when a message is sent successfully
+ do_action_ref_array( 'messages_message_sent', array( &$message ) );
+
+ // Return the thread ID
+ return $message->thread_id;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre>
</div>
</div>
</body>
</html>