[ 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   * @param int $thread_id ID of the thread.
 328   *
 329   * @return false|int Number of threads marked as read or false on error.
 330   */
 331  function messages_mark_thread_read( $thread_id ) {
 332      return BP_Messages_Thread::mark_as_read( $thread_id );
 333  }
 334  
 335  /**
 336   * Mark a thread as unread.
 337   *
 338   * Wrapper for {@link BP_Messages_Thread::mark_as_unread()}.
 339   *
 340   * @param int $thread_id ID of the thread.
 341   *
 342   * @return false|int Number of threads marked as unread or false on error.
 343   */
 344  function messages_mark_thread_unread( $thread_id ) {
 345      return BP_Messages_Thread::mark_as_unread( $thread_id );
 346  }
 347  
 348  /**
 349   * Set messages-related cookies.
 350   *
 351   * Saves the 'bp_messages_send_to', 'bp_messages_subject', and
 352   * 'bp_messages_content' cookies, which are used when setting up the default
 353   * values on the messages page.
 354   *
 355   * @param string $recipients Comma-separated list of recipient usernames.
 356   * @param string $subject    Subject of the message.
 357   * @param string $content    Content of the message.
 358   */
 359  function messages_add_callback_values( $recipients, $subject, $content ) {
 360      @setcookie( 'bp_messages_send_to', $recipients, time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 361      @setcookie( 'bp_messages_subject', $subject,    time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 362      @setcookie( 'bp_messages_content', $content,    time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 363  }
 364  
 365  /**
 366   * Unset messages-related cookies.
 367   *
 368   * @see messages_add_callback_values()
 369   */
 370  function messages_remove_callback_values() {
 371      @setcookie( 'bp_messages_send_to', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 372      @setcookie( 'bp_messages_subject', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 373      @setcookie( 'bp_messages_content', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 374  }
 375  
 376  /**
 377   * Get the unread messages count for a user.
 378   *
 379   * @param int $user_id Optional. ID of the user. Default: ID of the logged-in user.
 380   * @return int
 381   */
 382  function messages_get_unread_count( $user_id = 0 ) {
 383      return BP_Messages_Thread::get_inbox_count( $user_id );
 384  }
 385  
 386  /**
 387   * Check whether a user is the sender of a message.
 388   *
 389   * @param int $user_id    ID of the user.
 390   * @param int $message_id ID of the message.
 391   * @return int|null Returns the ID of the message if the user is the
 392   *                  sender, otherwise null.
 393   */
 394  function messages_is_user_sender( $user_id, $message_id ) {
 395      return BP_Messages_Message::is_user_sender( $user_id, $message_id );
 396  }
 397  
 398  /**
 399   * Get the ID of the sender of a message.
 400   *
 401   * @param int $message_id ID of the message.
 402   * @return int|null The ID of the sender if found, otherwise null.
 403   */
 404  function messages_get_message_sender( $message_id ) {
 405      return BP_Messages_Message::get_message_sender( $message_id );
 406  }
 407  
 408  /**
 409   * Check whether a message thread exists.
 410   *
 411   * @param int $thread_id ID of the thread.
 412   * @return false|int|null The message thread ID on success, null on failure.
 413   */
 414  function messages_is_valid_thread( $thread_id ) {
 415      return BP_Messages_Thread::is_valid( $thread_id );
 416  }
 417  
 418  /**
 419   * Get the thread ID from a message ID.
 420   *
 421   * @since 2.3.0
 422   *
 423   * @param  int $message_id ID of the message.
 424   * @return int The ID of the thread if found, otherwise 0.
 425   */
 426  function messages_get_message_thread_id( $message_id = 0 ) {
 427      global $wpdb;
 428  
 429      $bp = buddypress();
 430  
 431      return (int) $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) );
 432  }
 433  
 434  /** Messages Meta *******************************************************/
 435  
 436  /**
 437   * Delete metadata for a message.
 438   *
 439   * If $meta_key is false, this will delete all meta for the message ID.
 440   *
 441   * @since 2.2.0
 442   *
 443   * @see delete_metadata() for full documentation excluding $meta_type variable.
 444   *
 445   * @param int         $message_id ID of the message to have meta deleted for.
 446   * @param string|bool $meta_key   Meta key to delete. Default false.
 447   * @param string|bool $meta_value Meta value to delete. Default false.
 448   * @param bool        $delete_all Whether or not to delete all meta data.
 449   *
 450   * @return bool True on successful delete, false on failure.
 451   */
 452  function bp_messages_delete_meta( $message_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
 453      global $wpdb;
 454  
 455      // Legacy - if no meta_key is passed, delete all for the item.
 456      if ( empty( $meta_key ) ) {
 457          $table_name = buddypress()->messages->table_name_meta;
 458          $sql        = "SELECT meta_key FROM {$table_name} WHERE message_id = %d";
 459          $query      = $wpdb->prepare( $sql, $message_id );
 460          $keys       = $wpdb->get_col( $query );
 461  
 462          // With no meta_key, ignore $delete_all.
 463          $delete_all = false;
 464      } else {
 465          $keys = array( $meta_key );
 466      }
 467  
 468      $retval = false;
 469  
 470      // No keys, so stop now!
 471      if ( empty( $keys ) ) {
 472          return $retval;
 473      }
 474  
 475      add_filter( 'query', 'bp_filter_metaid_column_name' );
 476  
 477      foreach ( $keys as $key ) {
 478          $retval = delete_metadata( 'message', $message_id, $key, $meta_value, $delete_all );
 479      }
 480  
 481      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 482  
 483      return $retval;
 484  }
 485  
 486  /**
 487   * Get a piece of message metadata.
 488   *
 489   * @since 2.2.0
 490   *
 491   * @see get_metadata() for full documentation excluding $meta_type variable.
 492   *
 493   * @param int    $message_id ID of the message to retrieve meta for.
 494   * @param string $meta_key   Meta key to retrieve. Default empty string.
 495   * @param bool   $single     Whether or not to fetch all or a single value.
 496   * @return mixed
 497   */
 498  function bp_messages_get_meta( $message_id, $meta_key = '', $single = true ) {
 499      add_filter( 'query', 'bp_filter_metaid_column_name' );
 500      $retval = get_metadata( 'message', $message_id, $meta_key, $single );
 501      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 502  
 503      return $retval;
 504  }
 505  
 506  /**
 507   * Update a piece of message metadata.
 508   *
 509   * @since 2.2.0
 510   *
 511   * @see update_metadata() for full documentation excluding $meta_type variable.
 512   *
 513   * @param int         $message_id ID of the message to have meta deleted for.
 514   * @param string|bool $meta_key   Meta key to update.
 515   * @param string|bool $meta_value Meta value to update.
 516   * @param string      $prev_value If specified, only update existing metadata entries with
 517   *                                the specified value. Otherwise, update all entries.
 518   * @return mixed
 519   */
 520  function bp_messages_update_meta( $message_id, $meta_key, $meta_value, $prev_value = '' ) {
 521      add_filter( 'query', 'bp_filter_metaid_column_name' );
 522      $retval = update_metadata( 'message', $message_id, $meta_key, $meta_value, $prev_value );
 523      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 524  
 525      return $retval;
 526  }
 527  
 528  /**
 529   * Add a piece of message metadata.
 530   *
 531   * @since 2.2.0
 532   *
 533   * @see add_metadata() for full documentation excluding $meta_type variable.
 534   *
 535   * @param int         $message_id ID of the message to have meta deleted for.
 536   * @param string|bool $meta_key   Meta key to update.
 537   * @param string|bool $meta_value Meta value to update.
 538   * @param bool        $unique     Whether the specified metadata key should be
 539   *                                unique for the object. If true, and the object
 540   *                                already has a value for the specified metadata key,
 541   *                                no change will be made.
 542   * @return mixed
 543   */
 544  function bp_messages_add_meta( $message_id, $meta_key, $meta_value, $unique = false ) {
 545      add_filter( 'query', 'bp_filter_metaid_column_name' );
 546      $retval = add_metadata( 'message', $message_id, $meta_key, $meta_value, $unique );
 547      remove_filter( 'query', 'bp_filter_metaid_column_name' );
 548  
 549      return $retval;
 550  }
 551  
 552  /** Email *********************************************************************/
 553  
 554  /**
 555   * Email message recipients to alert them of a new unread private message.
 556   *
 557   * @since 1.0.0
 558   *
 559   * @param array|BP_Messages_Message $raw_args {
 560   *     Array of arguments. Also accepts a BP_Messages_Message object.
 561   *     @type array  $recipients    User IDs of recipients.
 562   *     @type string $email_subject Subject line of message.
 563   *     @type string $email_content Content of message.
 564   *     @type int    $sender_id     User ID of sender.
 565   * }
 566   */
 567  function messages_notification_new_message( $raw_args = array() ) {
 568      if ( is_object( $raw_args ) ) {
 569          $args = (array) $raw_args;
 570      } else {
 571          $args = $raw_args;
 572      }
 573  
 574      // These should be extracted below.
 575      $recipients    = array();
 576      $email_subject = $email_content = '';
 577      $sender_id     = 0;
 578  
 579      // Barf.
 580      extract( $args );
 581  
 582      if ( empty( $recipients ) ) {
 583          return;
 584      }
 585  
 586      $sender_name = bp_core_get_user_displayname( $sender_id );
 587  
 588      if ( isset( $message ) ) {
 589          $message = wpautop( $message );
 590      } else {
 591          $message = '';
 592      }
 593  
 594      // Send an email to each recipient.
 595      foreach ( $recipients as $recipient ) {
 596          if ( $sender_id == $recipient->user_id || 'no' == bp_get_user_meta( $recipient->user_id, 'notification_messages_new_message', true ) ) {
 597              continue;
 598          }
 599  
 600          // User data and links.
 601          $ud = get_userdata( $recipient->user_id );
 602          if ( empty( $ud ) ) {
 603              continue;
 604          }
 605  
 606          $unsubscribe_args = array(
 607              'user_id'           => $recipient->user_id,
 608              'notification_type' => 'messages-unread',
 609          );
 610  
 611          bp_send_email( 'messages-unread', $ud, array(
 612              'tokens' => array(
 613                  'usermessage' => wp_strip_all_tags( stripslashes( $message ) ),
 614                  'message.url' => esc_url( bp_core_get_user_domain( $recipient->user_id ) . bp_get_messages_slug() . '/view/' . $thread_id . '/' ),
 615                  'sender.name' => $sender_name,
 616                  'usersubject' => sanitize_text_field( stripslashes( $subject ) ),
 617                  'unsubscribe' => esc_url( bp_email_get_unsubscribe_link( $unsubscribe_args ) ),
 618              ),
 619          ) );
 620      }
 621  
 622      /**
 623       * Fires after the sending of a new message email notification.
 624       *
 625       * @since 1.5.0
 626       * @deprecated 2.5.0 Use the filters in BP_Email.
 627       *                   $email_subject and $email_content arguments unset and deprecated.
 628       *
 629       * @param array  $recipients    User IDs of recipients.
 630       * @param string $email_subject Deprecated in 2.5; now an empty string.
 631       * @param string $email_content Deprecated in 2.5; now an empty string.
 632       * @param array  $args          Array of originally provided arguments.
 633       */
 634      do_action( 'bp_messages_sent_notification_email', $recipients, '', '', $args );
 635  }
 636  add_action( 'messages_message_sent', 'messages_notification_new_message', 10 );
 637  
 638  /**
 639   * Finds and exports personal data associated with an email address from the Messages tables.
 640   *
 641   * @since 4.0.0
 642   *
 643   * @param string $email_address  The user's email address.
 644   * @param int    $page           Batch number.
 645   * @return array An array of personal data.
 646   */
 647  function bp_messages_personal_data_exporter( $email_address, $page ) {
 648      $number = 10;
 649  
 650      $email_address = trim( $email_address );
 651  
 652      $data_to_export = array();
 653  
 654      $user = get_user_by( 'email', $email_address );
 655  
 656      if ( ! $user ) {
 657          return array(
 658              'data' => array(),
 659              'done' => true,
 660          );
 661      }
 662  
 663      $user_data_to_export = array();
 664  
 665      $user_threads = BP_Messages_Thread::get_current_threads_for_user( array(
 666          'user_id' => $user->ID,
 667          'box'     => 'sentbox',
 668          'type'    => null,
 669          'limit'   => $number,
 670          'page'    => $page,
 671      ) );
 672  
 673      if ( empty( $user_threads ) ) {
 674          return array(
 675              'data' => $data_to_export,
 676              'done' => true,
 677          );
 678      }
 679  
 680      foreach ( $user_threads['threads'] as $thread ) {
 681          $recipient_links = array();
 682          foreach ( $thread->recipients as $recipient ) {
 683              if ( $recipient->user_id === $user->ID ) {
 684                  continue;
 685              }
 686  
 687              $recipient_links[] = bp_core_get_userlink( $recipient->user_id );
 688          }
 689          $recipients = implode( ', ', $recipient_links );
 690  
 691          $thread_link = bp_get_message_thread_view_link( $thread->thread_id, $user->ID );
 692  
 693          foreach ( $thread->messages as $message_index => $message ) {
 694              // Only include messages written by the user.
 695              if ( $user->ID !== $message->sender_id ) {
 696                  continue;
 697              }
 698  
 699              $message_data = array(
 700                  array(
 701                      'name'  => __( 'Message Subject', 'buddypress' ),
 702                      'value' => $message->subject,
 703                  ),
 704                  array(
 705                      'name'  => __( 'Message Content', 'buddypress' ),
 706                      'value' => $message->message,
 707                  ),
 708                  array(
 709                      'name'  => __( 'Date Sent', 'buddypress' ),
 710                      'value' => $message->date_sent,
 711                  ),
 712                  array(
 713                      'name' => __( 'Recipients', 'buddypress' ),
 714                      'value' => $recipients,
 715                  ),
 716                  array(
 717                      'name'  => __( 'Thread URL', 'buddypress' ),
 718                      'value' => $thread_link,
 719                  ),
 720              );
 721  
 722              $data_to_export[] = array(
 723                  'group_id'    => 'bp_messages',
 724                  'group_label' => __( 'Private Messages', 'buddypress' ),
 725                  'item_id'     => "bp-messages-{$message->id}",
 726                  'data'        => $message_data,
 727              );
 728          }
 729      }
 730  
 731      return array(
 732          'data' => $data_to_export,
 733          'done' => true,
 734      );
 735  }


Generated: Thu Apr 9 01:01:38 2020 Cross-referenced by PHPXref 0.7.1