[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-templates/bp-nouveau/includes/messages/ -> ajax.php (source)

   1  <?php
   2  /**
   3   * Messages Ajax functions
   4   *
   5   * @since 3.0.0
   6   * @version 8.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  add_action( 'admin_init', function() {
  13      $ajax_actions = array(
  14          array( 'messages_send_message'             => array( 'function' => 'bp_nouveau_ajax_messages_send_message', 'nopriv' => false ) ),
  15          array( 'messages_send_reply'               => array( 'function' => 'bp_nouveau_ajax_messages_send_reply', 'nopriv' => false ) ),
  16          array( 'messages_get_user_message_threads' => array( 'function' => 'bp_nouveau_ajax_get_user_message_threads', 'nopriv' => false ) ),
  17          array( 'messages_thread_read'              => array( 'function' => 'bp_nouveau_ajax_messages_thread_read', 'nopriv' => false ) ),
  18          array( 'messages_get_thread_messages'      => array( 'function' => 'bp_nouveau_ajax_get_thread_messages', 'nopriv' => false ) ),
  19          array( 'messages_delete'                   => array( 'function' => 'bp_nouveau_ajax_delete_thread_messages', 'nopriv' => false ) ),
  20          array( 'messages_unstar'                   => array( 'function' => 'bp_nouveau_ajax_star_thread_messages', 'nopriv' => false ) ),
  21          array( 'messages_star'                     => array( 'function' => 'bp_nouveau_ajax_star_thread_messages', 'nopriv' => false ) ),
  22          array( 'messages_unread'                   => array( 'function' => 'bp_nouveau_ajax_readunread_thread_messages', 'nopriv' => false ) ),
  23          array( 'messages_read'                     => array( 'function' => 'bp_nouveau_ajax_readunread_thread_messages', 'nopriv' => false ) ),
  24          array( 'messages_dismiss_sitewide_notice'  => array( 'function' => 'bp_nouveau_ajax_dismiss_sitewide_notice', 'nopriv' => false ) ),
  25      );
  26  
  27      foreach ( $ajax_actions as $ajax_action ) {
  28          $action = key( $ajax_action );
  29  
  30          add_action( 'wp_ajax_' . $action, $ajax_action[ $action ]['function'] );
  31  
  32          if ( ! empty( $ajax_action[ $action ]['nopriv'] ) ) {
  33              add_action( 'wp_ajax_nopriv_' . $action, $ajax_action[ $action ]['function'] );
  34          }
  35      }
  36  }, 12 );
  37  
  38  /**
  39   * @since 3.0.0
  40   */
  41  function bp_nouveau_ajax_messages_send_message() {
  42      $response = array(
  43          'feedback' => __( 'Your message could not be sent. Please try again.', 'buddypress' ),
  44          'type'     => 'error',
  45      );
  46  
  47      // Verify nonce
  48      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'messages_send_message' ) ) {
  49          wp_send_json_error( $response );
  50      }
  51  
  52      // Validate subject and message content
  53      if ( empty( $_POST['subject'] ) || empty( $_POST['message_content'] ) ) {
  54          if ( empty( $_POST['subject'] ) ) {
  55              $response['feedback'] = __( 'Your message was not sent. Please enter a subject line.', 'buddypress' );
  56          } else {
  57              $response['feedback'] = __( 'Your message was not sent. Please enter some content.', 'buddypress' );
  58          }
  59  
  60          wp_send_json_error( $response );
  61      }
  62  
  63      // Validate recipients
  64      if ( empty( $_POST['send_to'] ) || ! is_array( $_POST['send_to'] ) ) {
  65          $response['feedback'] = __( 'Your message was not sent. Please enter at least one username.', 'buddypress' );
  66  
  67          wp_send_json_error( $response );
  68      }
  69  
  70      // Trim @ from usernames
  71      /**
  72       * Filters the results of trimming of `@` characters from usernames for who is set to receive a message.
  73       *
  74       * @since 3.0.0
  75       *
  76       * @param array $value Array of trimmed usernames.
  77       * @param array $value Array of un-trimmed usernames submitted.
  78       */
  79      $recipients = apply_filters( 'bp_messages_recipients', array_map( function( $username ) {
  80          return trim( $username, '@' );
  81      }, $_POST['send_to'] ) );
  82  
  83      // Attempt to send the message.
  84      $send = messages_new_message( array(
  85          'recipients' => $recipients,
  86          'subject'    => $_POST['subject'],
  87          'content'    => $_POST['message_content'],
  88          'error_type' => 'wp_error',
  89      ) );
  90  
  91      // Send the message.
  92      if ( true === is_int( $send ) ) {
  93          wp_send_json_success( array(
  94              'feedback' => __( 'Message successfully sent.', 'buddypress' ),
  95              'type'     => 'success',
  96          ) );
  97  
  98      // Message could not be sent.
  99      } else {
 100          $response['feedback'] = $send->get_error_message();
 101  
 102          wp_send_json_error( $response );
 103      }
 104  }
 105  
 106  /**
 107   * @since 3.0.0
 108   */
 109  function bp_nouveau_ajax_messages_send_reply() {
 110      $response = array(
 111          'feedback' => __( 'There was a problem sending your reply. Please try again.', 'buddypress' ),
 112          'type'     => 'error',
 113      );
 114  
 115      // Verify nonce
 116      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'messages_send_message' ) ) {
 117          wp_send_json_error( $response );
 118      }
 119  
 120      if ( empty( $_POST['content'] ) || empty( $_POST['thread_id'] ) ) {
 121          $response['feedback'] = __( 'Your reply was not sent. Please enter some content.', 'buddypress' );
 122  
 123          wp_send_json_error( $response );
 124      }
 125  
 126      $thread_id = (int) $_POST['thread_id'];
 127  
 128      if ( ! bp_current_user_can( 'bp_moderate' ) && ( ! messages_is_valid_thread( $thread_id ) || ! messages_check_thread_access( $thread_id ) ) ) {
 129          wp_send_json_error( $response );
 130      }
 131  
 132      $new_reply = messages_new_message( array(
 133          'thread_id' => $thread_id,
 134          'subject'   => ! empty( $_POST['subject'] ) ? $_POST['subject'] : false,
 135          'content'   => $_POST['content']
 136      ) );
 137  
 138      // Send the reply.
 139      if ( empty( $new_reply ) ) {
 140          wp_send_json_error( $response );
 141      }
 142  
 143      // Get the message by pretending we're in the message loop.
 144      global $thread_template;
 145  
 146      $bp           = buddypress();
 147      $reset_action = $bp->current_action;
 148  
 149      // Override bp_current_action().
 150      $bp->current_action = 'view';
 151  
 152      bp_thread_has_messages( array( 'thread_id' => $thread_id ) );
 153  
 154      // Set the current message to the 2nd last.
 155      $thread_template->message = end( $thread_template->thread->messages );
 156      $thread_template->message = prev( $thread_template->thread->messages );
 157  
 158      // Set current message to current key.
 159      $thread_template->current_message = key( $thread_template->thread->messages );
 160  
 161      // Now manually iterate message like we're in the loop.
 162      bp_thread_the_message();
 163  
 164      // Manually call oEmbed
 165      // this is needed because we're not at the beginning of the loop.
 166      bp_messages_embed();
 167  
 168      // Output single message template part.
 169      $reply = array(
 170          'id'            => bp_get_the_thread_message_id(),
 171          'content'       => do_shortcode( bp_get_the_thread_message_content() ),
 172          'sender_id'     => bp_get_the_thread_message_sender_id(),
 173          'sender_name'   => esc_html( bp_get_the_thread_message_sender_name() ),
 174          'sender_link'   => bp_get_the_thread_message_sender_link(),
 175          'sender_avatar' => esc_url( bp_core_fetch_avatar( array(
 176              'item_id' => bp_get_the_thread_message_sender_id(),
 177              'object'  => 'user',
 178              'type'    => 'thumb',
 179              'width'   => 32,
 180              'height'  => 32,
 181              'html'    => false,
 182          ) ) ),
 183          'date'          => bp_get_the_thread_message_date_sent() * 1000,
 184          'display_date'  => bp_get_the_thread_message_time_since(),
 185      );
 186  
 187      if ( bp_is_active( 'messages', 'star' ) ) {
 188          $star_link = bp_get_the_message_star_action_link( array(
 189              'message_id' => bp_get_the_thread_message_id(),
 190              'url_only'  => true,
 191          ) );
 192  
 193          $reply['star_link']  = $star_link;
 194          $reply['is_starred'] = array_search( 'unstar', explode( '/', $star_link ) );
 195      }
 196  
 197      $extra_content = bp_nouveau_messages_catch_hook_content( array(
 198          'beforeMeta'    => 'bp_before_message_meta',
 199          'afterMeta'     => 'bp_after_message_meta',
 200          'beforeContent' => 'bp_before_message_content',
 201          'afterContent'  => 'bp_after_message_content',
 202      ) );
 203  
 204      if ( array_filter( $extra_content ) ) {
 205          $reply = array_merge( $reply, $extra_content );
 206      }
 207  
 208      // Clean up the loop.
 209      bp_thread_messages();
 210  
 211      // Remove the bp_current_action() override.
 212      $bp->current_action = $reset_action;
 213  
 214      wp_send_json_success( array(
 215          'messages' => array( $reply ),
 216          'feedback' => __( 'Your reply was sent successfully', 'buddypress' ),
 217          'type'     => 'success',
 218      ) );
 219  }
 220  
 221  /**
 222   * @since 3.0.0
 223   */
 224  function bp_nouveau_ajax_get_user_message_threads() {
 225      global $messages_template;
 226  
 227      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 228          wp_send_json_error( array(
 229              'feedback' => __( 'Unauthorized request.', 'buddypress' ),
 230              'type'     => 'error'
 231          ) );
 232      }
 233  
 234      $bp           = buddypress();
 235      $reset_action = $bp->current_action;
 236  
 237      // Override bp_current_action().
 238      if ( isset( $_POST['box'] ) ) {
 239          $bp->current_action = $_POST['box'];
 240      }
 241  
 242      // Add the message thread filter.
 243      if ( 'starred' === $bp->current_action ) {
 244          add_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
 245      }
 246  
 247      // Simulate the loop.
 248      if ( ! bp_has_message_threads( bp_ajax_querystring( 'messages' ) ) ) {
 249          // Remove the bp_current_action() override.
 250          $bp->current_action = $reset_action;
 251  
 252          wp_send_json_error( array(
 253              'feedback' => __( 'Sorry, no messages were found.', 'buddypress' ),
 254              'type'     => 'info'
 255          ) );
 256      }
 257  
 258      // remove the message thread filter.
 259      if ( 'starred' === $bp->current_action ) {
 260          remove_filter( 'bp_after_has_message_threads_parse_args', 'bp_messages_filter_starred_message_threads' );
 261      }
 262  
 263      $threads       = new stdClass;
 264      $threads->meta = array(
 265          'total_page' => ceil( (int) $messages_template->total_thread_count / (int) $messages_template->pag_num ),
 266          'page'       => $messages_template->pag_page,
 267      );
 268  
 269      $threads->threads = array();
 270      $i                = 0;
 271  
 272      while ( bp_message_threads() ) : bp_message_thread();
 273          $last_message_id = (int) $messages_template->thread->last_message_id;
 274  
 275          $threads->threads[ $i ] = array(
 276              'id'            => bp_get_message_thread_id(),
 277              'message_id'    => (int) $last_message_id,
 278              'subject'       => strip_tags( bp_get_message_thread_subject() ),
 279              'excerpt'       => strip_tags( bp_get_message_thread_excerpt() ),
 280              'content'       => do_shortcode( bp_get_message_thread_content() ),
 281              'unread'        => bp_message_thread_has_unread(),
 282              'sender_name'   => bp_core_get_user_displayname( $messages_template->thread->last_sender_id ),
 283              'sender_link'   => bp_core_get_userlink( $messages_template->thread->last_sender_id, false, true ),
 284              'sender_avatar' => esc_url( bp_core_fetch_avatar( array(
 285                  'item_id' => $messages_template->thread->last_sender_id,
 286                  'object'  => 'user',
 287                  'type'    => 'thumb',
 288                  'width'   => 32,
 289                  'height'  => 32,
 290                  'html'    => false,
 291              ) ) ),
 292              'count'         => bp_get_message_thread_total_count(),
 293              'date'          => strtotime( bp_get_message_thread_last_post_date_raw() ) * 1000,
 294              'display_date'  => bp_nouveau_get_message_date( bp_get_message_thread_last_post_date_raw() ),
 295          );
 296  
 297          if ( is_array( $messages_template->thread->recipients ) ) {
 298              foreach ( $messages_template->thread->recipients as $recipient ) {
 299                  $threads->threads[ $i ]['recipients'][] = array(
 300                      'avatar' => esc_url( bp_core_fetch_avatar( array(
 301                          'item_id' => $recipient->user_id,
 302                          'object'  => 'user',
 303                          'type'    => 'thumb',
 304                          'width'   => 28,
 305                          'height'  => 28,
 306                          'html'    => false,
 307                      ) ) ),
 308                      'user_link' => bp_core_get_userlink( $recipient->user_id, false, true ),
 309                      'user_name' => bp_core_get_username( $recipient->user_id ),
 310                  );
 311              }
 312          }
 313  
 314          if ( bp_is_active( 'messages', 'star' ) ) {
 315              $star_link = bp_get_the_message_star_action_link( array(
 316                  'thread_id' => bp_get_message_thread_id(),
 317                  'url_only'  => true,
 318              ) );
 319  
 320              $threads->threads[ $i ]['star_link']  = $star_link;
 321  
 322              $star_link_data = explode( '/', $star_link );
 323              $threads->threads[ $i ]['is_starred'] = array_search( 'unstar', $star_link_data );
 324  
 325              // Defaults to last
 326              $sm_id = $last_message_id;
 327  
 328              if ( $threads->threads[ $i ]['is_starred'] ) {
 329                  $sm_id = (int) $star_link_data[ $threads->threads[ $i ]['is_starred'] + 1 ];
 330              }
 331  
 332              $threads->threads[ $i ]['star_nonce'] = wp_create_nonce( 'bp-messages-star-' . $sm_id );
 333              $threads->threads[ $i ]['starred_id'] = $sm_id;
 334          }
 335  
 336          $thread_extra_content = bp_nouveau_messages_catch_hook_content( array(
 337              'inboxListItem' => 'bp_messages_inbox_list_item',
 338              'threadOptions' => 'bp_messages_thread_options',
 339          ) );
 340  
 341          if ( array_filter( $thread_extra_content ) ) {
 342              $threads->threads[ $i ] = array_merge( $threads->threads[ $i ], $thread_extra_content );
 343          }
 344  
 345          $i += 1;
 346      endwhile;
 347  
 348      $threads->threads = array_filter( $threads->threads );
 349  
 350      $extra_content = bp_nouveau_messages_catch_hook_content( array(
 351          'beforeLoop' => 'bp_before_member_messages_loop',
 352          'afterLoop'  => 'bp_after_member_messages_loop',
 353      ) );
 354  
 355      if ( array_filter( $extra_content ) ) {
 356          $threads->extraContent = $extra_content;
 357      }
 358  
 359      // Remove the bp_current_action() override.
 360      $bp->current_action = $reset_action;
 361  
 362      // Return the successfull reply.
 363      wp_send_json_success( $threads );
 364  }
 365  
 366  /**
 367   * @since 3.0.0
 368   */
 369  function bp_nouveau_ajax_messages_thread_read() {
 370      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 371          wp_send_json_error();
 372      }
 373  
 374      if ( empty( $_POST['id'] ) || empty( $_POST['message_id'] ) ) {
 375          wp_send_json_error();
 376      }
 377  
 378      $thread_id  = (int) $_POST['id'];
 379      $message_id = (int) $_POST['message_id'];
 380  
 381      if ( ! messages_is_valid_thread( $thread_id ) || ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) ) {
 382          wp_send_json_error();
 383      }
 384  
 385      // Mark thread as read
 386      messages_mark_thread_read( $thread_id );
 387  
 388      // Mark latest message as read
 389      if ( bp_is_active( 'notifications' ) ) {
 390          bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' );
 391      }
 392  
 393      wp_send_json_success();
 394  }
 395  
 396  /**
 397   * @since 3.0.0
 398   */
 399  function bp_nouveau_ajax_get_thread_messages() {
 400      global $thread_template;
 401  
 402      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 403          wp_send_json_error( array(
 404              'feedback' => __( 'Unauthorized request.', 'buddypress' ),
 405              'type'     => 'error'
 406          ) );
 407      }
 408  
 409      $response = array(
 410          'feedback' => __( 'Sorry, no messages were found.', 'buddypress' ),
 411          'type'     => 'info'
 412      );
 413  
 414      if ( empty( $_POST['id'] ) ) {
 415          wp_send_json_error( $response );
 416      }
 417  
 418      $thread_id = (int) $_POST['id'];
 419  
 420      if ( ! messages_is_valid_thread( $thread_id ) || ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) ) {
 421          wp_send_json_error();
 422      }
 423  
 424      $bp           = buddypress();
 425      $reset_action = $bp->current_action;
 426  
 427      // Override bp_current_action().
 428      $bp->current_action = 'view';
 429  
 430      // Simulate the loop.
 431      if ( ! bp_thread_has_messages( array( 'thread_id' => $thread_id ) ) ) {
 432          // Remove the bp_current_action() override.
 433          $bp->current_action = $reset_action;
 434  
 435          wp_send_json_error( $response );
 436      }
 437  
 438      $thread = new stdClass;
 439  
 440      if ( empty( $_POST['js_thread'] ) ) {
 441          $thread->thread = array(
 442              'id'      => bp_get_the_thread_id(),
 443              'subject' => strip_tags( bp_get_the_thread_subject() ),
 444          );
 445  
 446          if ( is_array( $thread_template->thread->recipients ) ) {
 447              foreach ( $thread_template->thread->recipients as $recipient ) {
 448                  $thread->thread['recipients'][] = array(
 449                      'avatar' => esc_url( bp_core_fetch_avatar( array(
 450                          'item_id' => $recipient->user_id,
 451                          'object'  => 'user',
 452                          'type'    => 'thumb',
 453                          'width'   => 28,
 454                          'height'  => 28,
 455                          'html'    => false,
 456                      ) ) ),
 457                      'user_link' => bp_core_get_userlink( $recipient->user_id, false, true ),
 458                      'user_name' => bp_core_get_username( $recipient->user_id ),
 459                  );
 460              }
 461          }
 462      }
 463  
 464      $thread->messages = array();
 465      $i = 0;
 466  
 467      while ( bp_thread_messages() ) : bp_thread_the_message();
 468          $thread->messages[ $i ] = array(
 469              'id'            => bp_get_the_thread_message_id(),
 470              'content'       => do_shortcode( bp_get_the_thread_message_content() ),
 471              'sender_id'     => bp_get_the_thread_message_sender_id(),
 472              'sender_name'   => esc_html( bp_get_the_thread_message_sender_name() ),
 473              'sender_link'   => bp_get_the_thread_message_sender_link(),
 474              'sender_avatar' => esc_url( bp_core_fetch_avatar( array(
 475                  'item_id' => bp_get_the_thread_message_sender_id(),
 476                  'object'  => 'user',
 477                  'type'    => 'thumb',
 478                  'width'   => 32,
 479                  'height'  => 32,
 480                  'html'    => false,
 481              ) ) ),
 482              'date'          => bp_get_the_thread_message_date_sent() * 1000,
 483              'display_date'  => bp_get_the_thread_message_time_since(),
 484          );
 485  
 486          if ( bp_is_active( 'messages', 'star' ) ) {
 487              $star_link = bp_get_the_message_star_action_link( array(
 488                  'message_id' => bp_get_the_thread_message_id(),
 489                  'url_only'  => true,
 490              ) );
 491  
 492              $thread->messages[ $i ]['star_link']  = $star_link;
 493              $thread->messages[ $i ]['is_starred'] = array_search( 'unstar', explode( '/', $star_link ) );
 494              $thread->messages[ $i ]['star_nonce'] = wp_create_nonce( 'bp-messages-star-' . bp_get_the_thread_message_id() );
 495          }
 496  
 497          /**
 498           * Filter here to include your meta data to one or more messages of the thread.
 499           *
 500           * @since 8.0.0
 501           *
 502           * @param array $message_meta An associative array keyed using the meta_key.
 503           * @param array $message The current message in the loop.
 504           */
 505          $message_meta = (array) apply_filters( 'bp_nouveau_ajax_get_thread_message_meta', array(), $thread->messages[ $i ] );
 506  
 507          if ( $message_meta ) {
 508              $thread->messages[ $i ]['meta'] = $message_meta;
 509          }
 510  
 511          $extra_content = bp_nouveau_messages_catch_hook_content( array(
 512              'beforeMeta'    => 'bp_before_message_meta',
 513              'afterMeta'     => 'bp_after_message_meta',
 514              'beforeContent' => 'bp_before_message_content',
 515              'afterContent'  => 'bp_after_message_content',
 516          ) );
 517  
 518          if ( array_filter( $extra_content ) ) {
 519              $thread->messages[ $i ] = array_merge( $thread->messages[ $i ], $extra_content );
 520          }
 521  
 522          $i += 1;
 523      endwhile;
 524  
 525      $thread->messages = array_filter( $thread->messages );
 526  
 527      // Remove the bp_current_action() override.
 528      $bp->current_action = $reset_action;
 529  
 530      /**
 531       * Filter here to include your meta data to the main message of the thread.
 532       *
 533       * @since 8.0.0
 534       *
 535       * @param array $thread_meta An associative array keyed using the meta_key.
 536       * @param array $thread The current message in the loop.
 537       */
 538      $thread_meta = (array) apply_filters( 'bp_nouveau_ajax_get_thread_meta', array(), $thread->thread );
 539  
 540      if ( $thread_meta ) {
 541          $thread->thread['meta'] = $thread_meta;
 542      }
 543  
 544      wp_send_json_success( $thread );
 545  }
 546  
 547  /**
 548   * @since 3.0.0
 549   */
 550  function bp_nouveau_ajax_delete_thread_messages() {
 551      $response = array(
 552          'feedback' => __( 'There was a problem deleting your messages. Please try again.', 'buddypress' ),
 553          'type'     => 'error',
 554      );
 555  
 556      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 557          wp_send_json_error( $response );
 558      }
 559  
 560      if ( empty( $_POST['id'] ) ) {
 561          wp_send_json_error( $response );
 562      }
 563  
 564      $thread_ids = wp_parse_id_list( $_POST['id'] );
 565  
 566      foreach ( $thread_ids as $thread_id ) {
 567          if ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) {
 568              wp_send_json_error( $response );
 569          }
 570  
 571          messages_delete_thread( $thread_id );
 572      }
 573  
 574      wp_send_json_success( array(
 575          'feedback' => __( 'Messages deleted', 'buddypress' ),
 576          'type'     => 'success',
 577      ) );
 578  }
 579  
 580  /**
 581   * @since 3.0.0
 582   */
 583  function bp_nouveau_ajax_star_thread_messages() {
 584      if ( empty( $_POST['action'] ) ) {
 585          wp_send_json_error();
 586      }
 587  
 588      $action = str_replace( 'messages_', '', $_POST['action'] );
 589  
 590      if ( 'star' === $action ) {
 591          $error_message = __( 'There was a problem starring your messages. Please try again.', 'buddypress' );
 592      } else {
 593          $error_message = __( 'There was a problem unstarring your messages. Please try again.', 'buddypress' );
 594      }
 595  
 596      $response = array(
 597          'feedback' => esc_html( $error_message ),
 598          'type'     => 'error',
 599      );
 600  
 601      if ( false === bp_is_active( 'messages', 'star' ) || empty( $_POST['id'] ) ) {
 602          wp_send_json_error( $response );
 603      }
 604  
 605      // Check capability.
 606      if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) {
 607          wp_send_json_error( $response );
 608      }
 609  
 610      $ids      = wp_parse_id_list( $_POST['id'] );
 611      $messages = array();
 612  
 613      // Use global nonce for bulk actions involving more than one id
 614      if ( 1 !== count( $ids ) ) {
 615          if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 616              wp_send_json_error( $response );
 617          }
 618  
 619          foreach ( $ids as $mid ) {
 620              if ( 'star' === $action ) {
 621                  bp_messages_star_set_action( array(
 622                      'action'     => 'star',
 623                      'message_id' => $mid,
 624                  ) );
 625              } else {
 626                  $thread_id = messages_get_message_thread_id( $mid );
 627  
 628                  bp_messages_star_set_action( array(
 629                      'action'    => 'unstar',
 630                      'thread_id' => $thread_id,
 631                      'bulk'      => true
 632                  ) );
 633              }
 634  
 635              $messages[ $mid ] = array(
 636                  'star_link' => bp_get_the_message_star_action_link( array(
 637                      'message_id' => $mid,
 638                      'url_only'  => true,
 639                  ) ),
 640                  'is_starred' => 'star' === $action,
 641              );
 642          }
 643  
 644      // Use global star nonce for bulk actions involving one id or regular action
 645      } else {
 646          $id = reset( $ids );
 647  
 648          if ( empty( $_POST['star_nonce'] ) || ! wp_verify_nonce( $_POST['star_nonce'], 'bp-messages-star-' . $id ) ) {
 649              wp_send_json_error( $response );
 650          }
 651  
 652          bp_messages_star_set_action( array(
 653              'action'     => $action,
 654              'message_id' => $id,
 655          ) );
 656  
 657          $messages[ $id ] = array(
 658              'star_link' => bp_get_the_message_star_action_link( array(
 659                  'message_id' => $id,
 660                  'url_only'  => true,
 661              ) ),
 662              'is_starred' => 'star' === $action,
 663          );
 664      }
 665  
 666      if ( 'star' === $action ) {
 667          $success_message = __( 'Messages successfully starred.', 'buddypress' );
 668      } else {
 669          $success_message = __( 'Messages successfully unstarred.', 'buddypress' );
 670      }
 671  
 672      wp_send_json_success( array(
 673          'feedback' => esc_html( $success_message ),
 674          'type'     => 'success',
 675          'messages' => $messages,
 676      ) );
 677  }
 678  
 679  /**
 680   * @since 3.0.0
 681   */
 682  function bp_nouveau_ajax_readunread_thread_messages() {
 683      if ( empty( $_POST['action'] ) ) {
 684          wp_send_json_error();
 685      }
 686  
 687      $action = str_replace( 'messages_', '', $_POST['action'] );
 688  
 689      $response = array(
 690          'feedback' => __( 'There was a problem marking your messages as read. Please try again.', 'buddypress' ),
 691          'type'     => 'error',
 692      );
 693  
 694      if ( 'unread' === $action ) {
 695          $response = array(
 696              'feedback' => __( 'There was a problem marking your messages as unread. Please try again.', 'buddypress' ),
 697              'type'     => 'error',
 698          );
 699      }
 700  
 701      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 702          wp_send_json_error( $response );
 703      }
 704  
 705      if ( empty( $_POST['id'] ) ) {
 706          wp_send_json_error( $response );
 707      }
 708  
 709      $thread_ids = wp_parse_id_list( $_POST['id'] );
 710  
 711      $response['messages'] = array();
 712  
 713      if ( 'unread' === $action ) {
 714          $response['feedback'] = __( 'Messages marked as unread.', 'buddypress' );
 715      } else {
 716          $response['feedback'] = __( 'Messages marked as read.', 'buddypress' );
 717      }
 718  
 719      foreach ( $thread_ids as $thread_id ) {
 720          if ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) {
 721              wp_send_json_error( $response );
 722          }
 723  
 724          if ( 'unread' === $action ) {
 725              // Mark unread
 726              messages_mark_thread_unread( $thread_id );
 727          } else {
 728              // Mark read
 729              messages_mark_thread_read( $thread_id );
 730          }
 731  
 732          $response['messages'][ $thread_id ] = array(
 733              'unread' => 'unread' === $action,
 734          );
 735      }
 736  
 737      $response['type'] = 'success';
 738  
 739      wp_send_json_success( $response );
 740  }
 741  
 742  /**
 743   * @since 3.0.0
 744   */
 745  function bp_nouveau_ajax_dismiss_sitewide_notice() {
 746      if ( empty( $_POST['action'] ) ) {
 747          wp_send_json_error();
 748      }
 749  
 750      $response = array(
 751          'feedback' => '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>' . __( 'There was a problem dismissing the notice. Please try again.', 'buddypress' ) . '</p></div>',
 752          'type'     => 'error',
 753      );
 754  
 755      if ( false === bp_is_active( 'messages' ) ) {
 756          wp_send_json_error( $response );
 757      }
 758  
 759      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 760          wp_send_json_error( $response );
 761      }
 762  
 763      // Check capability.
 764      if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) {
 765          wp_send_json_error( $response );
 766      }
 767  
 768      // Mark the active notice as closed.
 769      $notice = BP_Messages_Notice::get_active();
 770  
 771      if ( ! empty( $notice->id ) ) {
 772          $user_id = bp_loggedin_user_id();
 773  
 774          $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 775  
 776          if ( empty( $closed_notices ) ) {
 777              $closed_notices = array();
 778          }
 779  
 780          // Add the notice to the array of the user's closed notices.
 781          $closed_notices[] = (int) $notice->id;
 782          bp_update_user_meta( $user_id, 'closed_notices', array_map( 'absint', array_unique( $closed_notices ) ) );
 783  
 784          wp_send_json_success( array(
 785              'feedback' => '<div class="bp-feedback info"><span class="bp-icon" aria-hidden="true"></span><p>' . __( 'Sitewide notice dismissed', 'buddypress' ) . '</p></div>',
 786              'type'     => 'success',
 787          ) );
 788      }
 789  }


Generated: Fri May 14 01:01:37 2021 Cross-referenced by PHPXref 0.7.1