[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-messages/ -> bp-messages-functions.php (source)

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


Generated: Wed Aug 4 01:01:42 2021 Cross-referenced by PHPXref 0.7.1