[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Messages Functions. 4 * 5 * Business functions are where all the magic happens in BuddyPress. They will 6 * handle the actual saving or manipulation of information. Usually they will 7 * hand off to a database class for data access, then return 8 * true or false on success or failure. 9 * 10 * @package BuddyPress 11 * @subpackage MessagesFunctions 12 * @since 1.5.0 13 */ 14 15 // Exit if accessed directly. 16 defined( 'ABSPATH' ) || exit; 17 18 /** 19 * Create a new message. 20 * 21 * @since 2.4.0 Added 'error_type' as an additional $args parameter. 22 * 23 * @param array|string $args { 24 * Array of arguments. 25 * @type int $sender_id Optional. ID of the user who is sending the 26 * message. Default: ID of the logged-in user. 27 * @type int $thread_id Optional. ID of the parent thread. Leave blank to 28 * create a new thread for the message. 29 * @type array $recipients IDs or usernames of message recipients. If this 30 * is an existing thread, it is unnecessary to pass a $recipients 31 * argument - existing thread recipients will be assumed. 32 * @type string $subject Optional. Subject line for the message. For 33 * existing threads, the existing subject will be used. For new 34 * threads, 'No Subject' will be used if no $subject is provided. 35 * @type string $content Content of the message. Cannot be empty. 36 * @type string $date_sent Date sent, in 'Y-m-d H:i:s' format. Default: current date/time. 37 * @type string $error_type Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'. 38 * } 39 * 40 * @return int|bool|WP_Error ID of the message thread on success, false on failure. 41 */ 42 function messages_new_message( $args = '' ) { 43 44 // Parse the default arguments. 45 $r = bp_parse_args( 46 $args, 47 array( 48 'sender_id' => bp_loggedin_user_id(), 49 'thread_id' => false, // False for a new message, thread id for a reply to a thread. 50 'recipients' => array(), // Can be an array of usernames, user_ids or mixed. 51 'subject' => false, 52 'content' => false, 53 'date_sent' => bp_core_current_time(), 54 'error_type' => 'bool', 55 ), 56 'messages_new_message' 57 ); 58 59 // Bail if no sender or no content. 60 if ( empty( $r['sender_id'] ) || empty( $r['content'] ) ) { 61 if ( 'wp_error' === $r['error_type'] ) { 62 if ( empty( $r['sender_id'] ) ) { 63 $error_code = 'messages_empty_sender'; 64 $feedback = __( 'Your message was not sent. Please use a valid sender.', 'buddypress' ); 65 } else { 66 $error_code = 'messages_empty_content'; 67 $feedback = __( 'Your message was not sent. Please enter some content.', 'buddypress' ); 68 } 69 70 return new WP_Error( $error_code, $feedback ); 71 72 } else { 73 return false; 74 } 75 } 76 77 // Create a new message object. 78 $message = new BP_Messages_Message; 79 $message->thread_id = $r['thread_id']; 80 $message->sender_id = $r['sender_id']; 81 $message->subject = $r['subject']; 82 $message->message = $r['content']; 83 $message->date_sent = $r['date_sent']; 84 85 // If we have a thread ID... 86 if ( ! empty( $r['thread_id'] ) ) { 87 88 // ...use the existing recipients 89 $thread = new BP_Messages_Thread( $r['thread_id'] ); 90 $message->recipients = $thread->get_recipients(); 91 92 // Strip the sender from the recipient list, and unset them if they are 93 // not alone. If they are alone, let them talk to themselves. 94 if ( isset( $message->recipients[ $r['sender_id'] ] ) && ( count( $message->recipients ) > 1 ) ) { 95 unset( $message->recipients[ $r['sender_id'] ] ); 96 } 97 98 // Set a default reply subject if none was sent. 99 if ( empty( $message->subject ) ) { 100 /* translators: %s: message subject */ 101 $message->subject = sprintf( __( 'Re: %s', 'buddypress' ), $thread->messages[0]->subject ); 102 } 103 104 // ...otherwise use the recipients passed 105 } else { 106 107 // Bail if no recipients. 108 if ( empty( $r['recipients'] ) ) { 109 if ( 'wp_error' === $r['error_type'] ) { 110 return new WP_Error( 'message_empty_recipients', __( 'Message could not be sent. Please enter a recipient.', 'buddypress' ) ); 111 } else { 112 return false; 113 } 114 } 115 116 // Set a default subject if none exists. 117 if ( empty( $message->subject ) ) { 118 $message->subject = __( 'No Subject', 'buddypress' ); 119 } 120 121 // Setup the recipients array. 122 $recipient_ids = array(); 123 124 // Invalid recipients are added to an array, for future enhancements. 125 $invalid_recipients = array(); 126 127 // Loop the recipients and convert all usernames to user_ids where needed. 128 foreach ( (array) $r['recipients'] as $recipient ) { 129 130 // Trim spaces and skip if empty. 131 $recipient = trim( $recipient ); 132 if ( empty( $recipient ) ) { 133 continue; 134 } 135 136 // Check user_login / nicename columns first 137 // @see http://buddypress.trac.wordpress.org/ticket/5151. 138 if ( bp_is_username_compatibility_mode() ) { 139 $recipient_id = bp_core_get_userid( urldecode( $recipient ) ); 140 } else { 141 $recipient_id = bp_core_get_userid_from_nicename( $recipient ); 142 } 143 144 // Check against user ID column if no match and if passed recipient is numeric. 145 if ( empty( $recipient_id ) && is_numeric( $recipient ) ) { 146 if ( bp_core_get_core_userdata( (int) $recipient ) ) { 147 $recipient_id = (int) $recipient; 148 } 149 } 150 151 // Decide which group to add this recipient to. 152 if ( empty( $recipient_id ) ) { 153 $invalid_recipients[] = $recipient; 154 } else { 155 $recipient_ids[] = (int) $recipient_id; 156 } 157 } 158 159 // Strip the sender from the recipient list, and unset them if they are 160 // not alone. If they are alone, let them talk to themselves. 161 $self_send = array_search( $r['sender_id'], $recipient_ids ); 162 if ( ! empty( $self_send ) && ( count( $recipient_ids ) > 1 ) ) { 163 unset( $recipient_ids[ $self_send ] ); 164 } 165 166 // Remove duplicates & bail if no recipients. 167 $recipient_ids = array_unique( $recipient_ids ); 168 if ( empty( $recipient_ids ) ) { 169 if ( 'wp_error' === $r['error_type'] ) { 170 return new WP_Error( 'message_invalid_recipients', __( 'Message could not be sent because you have entered an invalid username. Please try again.', 'buddypress' ) ); 171 } else { 172 return false; 173 } 174 } 175 176 // Format this to match existing recipients. 177 foreach ( (array) $recipient_ids as $i => $recipient_id ) { 178 $message->recipients[ $i ] = new stdClass; 179 $message->recipients[ $i ]->user_id = $recipient_id; 180 } 181 } 182 183 // Bail if message failed to send. 184 $send = $message->send(); 185 if ( false === is_int( $send ) ) { 186 if ( 'wp_error' === $r['error_type'] ) { 187 if ( is_wp_error( $send ) ) { 188 return $send; 189 } else { 190 return new WP_Error( 'message_generic_error', __( 'Message was not sent. Please try again.', 'buddypress' ) ); 191 } 192 } 193 194 return false; 195 } 196 197 /** 198 * Fires after a message has been successfully sent. 199 * 200 * @since 1.1.0 201 * 202 * @param BP_Messages_Message $message Message object. Passed by reference. 203 */ 204 do_action_ref_array( 'messages_message_sent', array( &$message ) ); 205 206 // Return the thread ID. 207 return $message->thread_id; 208 } 209 210 /** 211 * Send a notice. 212 * 213 * @param string $subject Subject of the notice. 214 * @param string $message Content of the notice. 215 * @return bool True on success, false on failure. 216 */ 217 function messages_send_notice( $subject, $message ) { 218 219 if ( ! bp_current_user_can( 'bp_moderate' ) || empty( $subject ) || empty( $message ) ) { 220 return false; 221 } 222 223 $notice = new BP_Messages_Notice; 224 $notice->subject = $subject; 225 $notice->message = $message; 226 $notice->date_sent = bp_core_current_time(); 227 $notice->is_active = 1; 228 $notice->save(); // Send it. 229 230 /** 231 * Fires after a notice has been successfully sent. 232 * 233 * @since 1.0.0 234 * 235 * @param string $subject Subject of the notice. 236 * @param string $message Content of the notice. 237 * @param BP_Messages_Notice $notice Notice object sent. 238 */ 239 do_action_ref_array( 'messages_send_notice', array( $subject, $message, $notice ) ); 240 241 return true; 242 } 243 244 /** 245 * Deletes message thread(s) for a given user. 246 * 247 * Note that "deleting" a thread for a user means removing it from the user's 248 * message boxes. A thread is not deleted from the database until it's been 249 * "deleted" by all recipients. 250 * 251 * @since 2.7.0 The $user_id parameter was added. Previously the current user 252 * was always assumed. 253 * 254 * @param int|array $thread_ids Thread ID or array of thread IDs. 255 * @param int $user_id ID of the user to delete the threads for. Defaults 256 * to the current logged-in user. 257 * @return bool True on success, false on failure. 258 */ 259 function messages_delete_thread( $thread_ids, $user_id = 0 ) { 260 261 if ( empty( $user_id ) ) { 262 $user_id = 263 bp_displayed_user_id() ? 264 bp_displayed_user_id() : 265 bp_loggedin_user_id(); 266 } 267 268 /** 269 * Fires before specified thread IDs have been deleted. 270 * 271 * @since 1.5.0 272 * @since 2.7.0 The $user_id parameter was added. 273 * 274 * @param int|array $thread_ids Thread ID or array of thread IDs to be deleted. 275 * @param int $user_id ID of the user the threads are being deleted for. 276 */ 277 do_action( 'messages_before_delete_thread', $thread_ids, $user_id ); 278 279 if ( is_array( $thread_ids ) ) { 280 $error = 0; 281 for ( $i = 0, $count = count( $thread_ids ); $i < $count; ++$i ) { 282 if ( ! BP_Messages_Thread::delete( $thread_ids[ $i ], $user_id ) ) { 283 $error = 1; 284 } 285 } 286 287 if ( ! empty( $error ) ) { 288 return false; 289 } 290 291 /** 292 * Fires after specified thread IDs have been deleted. 293 * 294 * @since 1.0.0 295 * @since 2.7.0 The $user_id parameter was added. 296 * 297 * @param int|array Thread ID or array of thread IDs that were deleted. 298 * @param int ID of the user that the threads were deleted for. 299 */ 300 do_action( 'messages_delete_thread', $thread_ids, $user_id ); 301 302 return true; 303 } else { 304 if ( ! BP_Messages_Thread::delete( $thread_ids, $user_id ) ) { 305 return false; 306 } 307 308 /** This action is documented in bp-messages/bp-messages-functions.php */ 309 do_action( 'messages_delete_thread', $thread_ids, $user_id ); 310 311 return true; 312 } 313 } 314 315 /** 316 * Check whether a user has access to a thread. 317 * 318 * @param int $thread_id ID of the thread. 319 * @param int $user_id Optional. ID of the user. Default: ID of the logged-in user. 320 * @return int|null Message ID if the user has access, otherwise null. 321 */ 322 function messages_check_thread_access( $thread_id, $user_id = 0 ) { 323 return BP_Messages_Thread::check_access( $thread_id, $user_id ); 324 } 325 326 /** 327 * Mark a thread as read. 328 * 329 * Wrapper for {@link BP_Messages_Thread::mark_as_read()}. 330 * 331 * @since 9.0.0 Added the `user_id` parameter. 332 * 333 * @param int $thread_id The message thread ID. 334 * @param int $user_id Optional. The user the thread will be marked as read. 335 * 336 * @return false|int Number of threads marked as read or false on error. 337 */ 338 function messages_mark_thread_read( $thread_id, $user_id = 0 ) { 339 return BP_Messages_Thread::mark_as_read( $thread_id, $user_id ); 340 } 341 342 /** 343 * Mark a thread as unread. 344 * 345 * Wrapper for {@link BP_Messages_Thread::mark_as_unread()}. 346 * 347 * @since 9.0.0 Added the `user_id` parameter. 348 * 349 * @param int $thread_id The message thread ID. 350 * @param int $user_id Optional. The user the thread will be marked as unread. 351 * 352 * @return false|int Number of threads marked as unread or false on error. 353 */ 354 function messages_mark_thread_unread( $thread_id, $user_id = 0 ) { 355 return BP_Messages_Thread::mark_as_unread( $thread_id, $user_id ); 356 } 357 358 /** 359 * Set messages-related cookies. 360 * 361 * Saves the 'bp_messages_send_to', 'bp_messages_subject', and 362 * 'bp_messages_content' cookies, which are used when setting up the default 363 * values on the messages page. 364 * 365 * @param string $recipients Comma-separated list of recipient usernames. 366 * @param string $subject Subject of the message. 367 * @param string $content Content of the message. 368 */ 369 function messages_add_callback_values( $recipients, $subject, $content ) { 370 @setcookie( 'bp_messages_send_to', $recipients, time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 371 @setcookie( 'bp_messages_subject', $subject, time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 372 @setcookie( 'bp_messages_content', $content, time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 373 } 374 375 /** 376 * Unset messages-related cookies. 377 * 378 * @see messages_add_callback_values() 379 */ 380 function messages_remove_callback_values() { 381 @setcookie( 'bp_messages_send_to', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 382 @setcookie( 'bp_messages_subject', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 383 @setcookie( 'bp_messages_content', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 384 } 385 386 /** 387 * Get the unread messages count for a user. 388 * 389 * @param int $user_id Optional. ID of the user. Default: ID of the logged-in user. 390 * @return int 391 */ 392 function messages_get_unread_count( $user_id = 0 ) { 393 return BP_Messages_Thread::get_inbox_count( $user_id ); 394 } 395 396 /** 397 * Check whether a user is the sender of a message. 398 * 399 * @param int $user_id ID of the user. 400 * @param int $message_id ID of the message. 401 * @return int|null Returns the ID of the message if the user is the 402 * sender, otherwise null. 403 */ 404 function messages_is_user_sender( $user_id, $message_id ) { 405 return BP_Messages_Message::is_user_sender( $user_id, $message_id ); 406 } 407 408 /** 409 * Get the ID of the sender of a message. 410 * 411 * @param int $message_id ID of the message. 412 * @return int|null The ID of the sender if found, otherwise null. 413 */ 414 function messages_get_message_sender( $message_id ) { 415 return BP_Messages_Message::get_message_sender( $message_id ); 416 } 417 418 /** 419 * Check whether a message thread exists. 420 * 421 * @param int $thread_id ID of the thread. 422 * @return false|int|null The message thread ID on success, null on failure. 423 */ 424 function messages_is_valid_thread( $thread_id ) { 425 return BP_Messages_Thread::is_valid( $thread_id ); 426 } 427 428 /** 429 * Get the thread ID from a message ID. 430 * 431 * @since 2.3.0 432 * 433 * @global BuddyPress $bp The one true BuddyPress instance. 434 * @global wpdb $wpdb WordPress database object. 435 * 436 * @param int $message_id ID of the message. 437 * @return int The ID of the thread if found, otherwise 0. 438 */ 439 function messages_get_message_thread_id( $message_id = 0 ) { 440 global $wpdb; 441 442 $bp = buddypress(); 443 444 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) ); 445 } 446 447 /** Messages Meta *******************************************************/ 448 449 /** 450 * Delete metadata for a message. 451 * 452 * If $meta_key is false, this will delete all meta for the message ID. 453 * 454 * @since 2.2.0 455 * 456 * @global wpdb $wpdb WordPress database object. 457 * 458 * @see delete_metadata() for full documentation excluding $meta_type variable. 459 * 460 * @param int $message_id ID of the message to have meta deleted for. 461 * @param string|bool $meta_key Meta key to delete. Default false. 462 * @param string|bool $meta_value Meta value to delete. Default false. 463 * @param bool $delete_all Whether or not to delete all meta data. 464 * 465 * @return bool True on successful delete, false on failure. 466 */ 467 function bp_messages_delete_meta( $message_id, $meta_key = false, $meta_value = false, $delete_all = false ) { 468 global $wpdb; 469 470 // Legacy - if no meta_key is passed, delete all for the item. 471 if ( empty( $meta_key ) ) { 472 $table_name = buddypress()->messages->table_name_meta; 473 $sql = "SELECT meta_key FROM {$table_name} WHERE message_id = %d"; 474 $query = $wpdb->prepare( $sql, $message_id ); 475 $keys = $wpdb->get_col( $query ); 476 477 // With no meta_key, ignore $delete_all. 478 $delete_all = false; 479 } else { 480 $keys = array( $meta_key ); 481 } 482 483 $retval = false; 484 485 // No keys, so stop now! 486 if ( empty( $keys ) ) { 487 return $retval; 488 } 489 490 add_filter( 'query', 'bp_filter_metaid_column_name' ); 491 492 foreach ( $keys as $key ) { 493 $retval = delete_metadata( 'message', $message_id, $key, $meta_value, $delete_all ); 494 } 495 496 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 497 498 return $retval; 499 } 500 501 /** 502 * Get a piece of message metadata. 503 * 504 * @since 2.2.0 505 * 506 * @see get_metadata() for full documentation excluding $meta_type variable. 507 * 508 * @param int $message_id ID of the message to retrieve meta for. 509 * @param string $meta_key Meta key to retrieve. Default empty string. 510 * @param bool $single Whether or not to fetch all or a single value. 511 * @return mixed 512 */ 513 function bp_messages_get_meta( $message_id, $meta_key = '', $single = true ) { 514 add_filter( 'query', 'bp_filter_metaid_column_name' ); 515 $retval = get_metadata( 'message', $message_id, $meta_key, $single ); 516 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 517 518 return $retval; 519 } 520 521 /** 522 * Update a piece of message metadata. 523 * 524 * @since 2.2.0 525 * 526 * @see update_metadata() for full documentation excluding $meta_type variable. 527 * 528 * @param int $message_id ID of the message to have meta deleted for. 529 * @param string|bool $meta_key Meta key to update. 530 * @param string|bool $meta_value Meta value to update. 531 * @param string $prev_value If specified, only update existing metadata entries with 532 * the specified value. Otherwise, update all entries. 533 * @return mixed 534 */ 535 function bp_messages_update_meta( $message_id, $meta_key, $meta_value, $prev_value = '' ) { 536 add_filter( 'query', 'bp_filter_metaid_column_name' ); 537 $retval = update_metadata( 'message', $message_id, $meta_key, $meta_value, $prev_value ); 538 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 539 540 return $retval; 541 } 542 543 /** 544 * Add a piece of message metadata. 545 * 546 * @since 2.2.0 547 * 548 * @see add_metadata() for full documentation excluding $meta_type variable. 549 * 550 * @param int $message_id ID of the message to have meta deleted for. 551 * @param string|bool $meta_key Meta key to update. 552 * @param string|bool $meta_value Meta value to update. 553 * @param bool $unique Whether the specified metadata key should be 554 * unique for the object. If true, and the object 555 * already has a value for the specified metadata key, 556 * no change will be made. 557 * @return mixed 558 */ 559 function bp_messages_add_meta( $message_id, $meta_key, $meta_value, $unique = false ) { 560 add_filter( 'query', 'bp_filter_metaid_column_name' ); 561 $retval = add_metadata( 'message', $message_id, $meta_key, $meta_value, $unique ); 562 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 563 564 return $retval; 565 } 566 567 /** Email *********************************************************************/ 568 569 /** 570 * Email message recipients to alert them of a new unread private message. 571 * 572 * @since 1.0.0 573 * 574 * @param array|BP_Messages_Message $raw_args { 575 * Array of arguments. Also accepts a BP_Messages_Message object. 576 * @type array $recipients User IDs of recipients. 577 * @type string $email_subject Subject line of message. 578 * @type string $email_content Content of message. 579 * @type int $sender_id User ID of sender. 580 * } 581 */ 582 function messages_notification_new_message( $raw_args = array() ) { 583 if ( is_object( $raw_args ) ) { 584 $args = (array) $raw_args; 585 } else { 586 $args = $raw_args; 587 } 588 589 // These should be extracted below. 590 $recipients = array(); 591 $sender_id = 0; 592 593 // Barf. 594 extract( $args ); 595 596 if ( empty( $recipients ) ) { 597 return; 598 } 599 600 $sender_name = bp_core_get_user_displayname( $sender_id ); 601 602 if ( isset( $message ) ) { 603 $message = wpautop( $message ); 604 } else { 605 $message = ''; 606 } 607 608 // Send an email to each recipient. 609 foreach ( $recipients as $recipient ) { 610 if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) ) { 611 continue; 612 } 613 614 // User data and links. 615 $ud = get_userdata( $recipient->user_id ); 616 if ( empty( $ud ) ) { 617 continue; 618 } 619 620 $unsubscribe_args = array( 621 'user_id' => $recipient->user_id, 622 'notification_type' => 'messages-unread', 623 ); 624 625 bp_send_email( 'messages-unread', $ud, array( 626 'tokens' => array( 627 'usermessage' => wp_strip_all_tags( stripslashes( $message ) ), 628 'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ), 629 'sender.name' => $sender_name, 630 'usersubject' => sanitize_text_field( stripslashes( $subject ) ), 631 'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ), 632 ), 633 ) ); 634 } 635 636 /** 637 * Fires after the sending of a new message email notification. 638 * 639 * @since 1.5.0 640 * @deprecated 2.5.0 Use the filters in BP_Email. 641 * $email_subject and $email_content arguments unset and deprecated. 642 * 643 * @param array $recipients User IDs of recipients. 644 * @param string $email_subject Deprecated in 2.5; now an empty string. 645 * @param string $email_content Deprecated in 2.5; now an empty string. 646 * @param array $args Array of originally provided arguments. 647 */ 648 do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args ); 649 } 650 add_action( 'messages_message_sent', 'messages_notification_new_message', 10 ); 651 652 /** 653 * Finds and exports personal data associated with an email address from the Messages tables. 654 * 655 * @since 4.0.0 656 * 657 * @param string $email_address The user's email address. 658 * @param int $page Batch number. 659 * @return array An array of personal data. 660 */ 661 function bp_messages_personal_data_exporter( $email_address, $page ) { 662 $number = 10; 663 664 $email_address = trim( $email_address ); 665 666 $data_to_export = array(); 667 668 $user = get_user_by( 'email', $email_address ); 669 670 if ( ! $user ) { 671 return array( 672 'data' => array(), 673 'done' => true, 674 ); 675 } 676 677 $user_threads = BP_Messages_Thread::get_current_threads_for_user( array( 678 'user_id' => $user->ID, 679 'box' => 'sentbox', 680 'type' => null, 681 'limit' => $number, 682 'page' => $page, 683 ) ); 684 685 if ( empty( $user_threads ) ) { 686 return array( 687 'data' => $data_to_export, 688 'done' => true, 689 ); 690 } 691 692 foreach ( $user_threads['threads'] as $thread ) { 693 $recipient_links = array(); 694 foreach ( $thread->recipients as $recipient ) { 695 if ( $recipient->user_id === $user->ID ) { 696 continue; 697 } 698 699 $recipient_links[] = bp_core_get_userlink( $recipient->user_id ); 700 } 701 $recipients = implode( ', ', $recipient_links ); 702 703 $thread_link = bp_get_message_thread_view_link( $thread->thread_id, $user->ID ); 704 705 foreach ( $thread->messages as $message_index => $message ) { 706 // Only include messages written by the user. 707 if ( $user->ID !== $message->sender_id ) { 708 continue; 709 } 710 711 $message_data = array( 712 array( 713 'name' => __( 'Message Subject', 'buddypress' ), 714 'value' => $message->subject, 715 ), 716 array( 717 'name' => __( 'Message Content', 'buddypress' ), 718 'value' => $message->message, 719 ), 720 array( 721 'name' => __( 'Date Sent', 'buddypress' ), 722 'value' => $message->date_sent, 723 ), 724 array( 725 'name' => __( 'Recipients', 'buddypress' ), 726 'value' => $recipients, 727 ), 728 array( 729 'name' => __( 'Thread URL', 'buddypress' ), 730 'value' => $thread_link, 731 ), 732 ); 733 734 $data_to_export[] = array( 735 'group_id' => 'bp_messages', 736 'group_label' => __( 'Private Messages', 'buddypress' ), 737 'item_id' => "bp-messages-{$message->id}", 738 'data' => $message_data, 739 ); 740 } 741 } 742 743 return array( 744 'data' => $data_to_export, 745 'done' => true, 746 ); 747 } 748 749 /** 750 * Dismiss a sitewide notice for a user. 751 * 752 * @since 9.0.0 753 * 754 * @param int $user_id ID of the user to dismiss the notice for. 755 * Defaults to the logged-in user. 756 * @param int $notice_id ID of the notice to be dismissed. 757 * Defaults to the currently active notice. 758 * @return bool False on failure, true if notice is dismissed 759 * (or was already dismissed). 760 */ 761 function bp_messages_dismiss_sitewide_notice( $user_id = 0, $notice_id = 0 ) { 762 $retval = false; 763 if ( ! $user_id ) { 764 $user_id = bp_loggedin_user_id(); 765 } 766 767 // Bail if no user is set. 768 if ( ! $user_id ) { 769 return $retval; 770 } 771 772 if ( $notice_id ) { 773 $notice = new BP_Messages_Notice( $notice_id ); 774 } else { 775 $notice = BP_Messages_Notice::get_active(); 776 } 777 778 // Bail if no notice is set. 779 if ( empty( $notice->id ) ) { 780 return $retval; 781 } 782 783 // Fetch the user's closed notices and add the new item. 784 $closed_notices = (array) bp_get_user_meta( $user_id, 'closed_notices', true ); 785 $closed_notices = array_filter( $closed_notices ); 786 787 if ( in_array( (int) $notice->id, $closed_notices, true ) ) { 788 // The notice has already been dismissed, so there's nothing to do. 789 $retval = true; 790 } else { 791 // Add the notice to the closed_notices meta. 792 $closed_notices[] = (int) $notice->id; 793 $closed_notices = array_map( 'absint', array_unique( $closed_notices ) ); 794 $success = bp_update_user_meta( $user_id, 'closed_notices', $closed_notices ); 795 796 // The return value from update_user_meta() could be an integer or a boolean. 797 $retval = (bool) $success; 798 } 799 800 return $retval; 801 } 802 803 /** 804 * Exit one or more message thread(s) for a given user. 805 * 806 * @since 10.0.0 807 * 808 * @param int|array $thread_ids Thread ID or array of thread IDs. 809 * @param int $user_id ID of the user to delete the threads for. Defaults 810 * to the current logged-in user. 811 * @return bool True on success, false on failure. 812 */ 813 function bp_messages_exit_thread( $thread_ids, $user_id = 0 ) { 814 815 if ( empty( $user_id ) ) { 816 $user_id = bp_loggedin_user_id(); 817 818 if ( bp_displayed_user_id() ) { 819 $user_id = bp_displayed_user_id(); 820 } 821 } 822 823 /** 824 * Fires before a user exits specified thread IDs. 825 * 826 * @since 10.0.0 827 * 828 * @param int|array $thread_ids Thread ID or array of thread IDs to be deleted. 829 * @param int $user_id ID of the user the threads are being deleted for. 830 */ 831 do_action( 'bp_messages_before_exit_thread', $thread_ids, $user_id ); 832 833 if ( is_array( $thread_ids ) ) { 834 $error = 0; 835 for ( $i = 0, $count = count( $thread_ids ); $i < $count; ++$i ) { 836 if ( ! BP_Messages_Thread::exit_thread( $thread_ids[ $i ], $user_id ) ) { 837 $error = 1; 838 } 839 } 840 841 if ( ! empty( $error ) ) { 842 return false; 843 } 844 845 /** 846 * Fires after a user exited the specified thread IDs. 847 * 848 * @since 10.0.0 849 * 850 * @param int|array Thread ID or array of thread IDs that were deleted. 851 * @param int ID of the user that the threads were deleted for. 852 */ 853 do_action( 'bp_messages_exit_thread', $thread_ids, $user_id ); 854 855 return true; 856 } else { 857 if ( ! BP_Messages_Thread::exit_thread( $thread_ids, $user_id ) ) { 858 return false; 859 } 860 861 /** This action is documented in bp-messages/bp-messages-functions.php */ 862 do_action( 'bp_messages_exit_thread', $thread_ids, $user_id ); 863 864 return true; 865 } 866 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |