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


Generated: Sun Nov 17 01:01:36 2019 Cross-referenced by PHPXref 0.7.1