[ 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 3.1.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          $extra_content = bp_nouveau_messages_catch_hook_content( array(
 498              'beforeMeta'    => 'bp_before_message_meta',
 499              'afterMeta'     => 'bp_after_message_meta',
 500              'beforeContent' => 'bp_before_message_content',
 501              'afterContent'  => 'bp_after_message_content',
 502          ) );
 503  
 504          if ( array_filter( $extra_content ) ) {
 505              $thread->messages[ $i ] = array_merge( $thread->messages[ $i ], $extra_content );
 506          }
 507  
 508          $i += 1;
 509      endwhile;
 510  
 511      $thread->messages = array_filter( $thread->messages );
 512  
 513      // Remove the bp_current_action() override.
 514      $bp->current_action = $reset_action;
 515  
 516      wp_send_json_success( $thread );
 517  }
 518  
 519  /**
 520   * @since 3.0.0
 521   */
 522  function bp_nouveau_ajax_delete_thread_messages() {
 523      $response = array(
 524          'feedback' => __( 'There was a problem deleting your messages. Please try again.', 'buddypress' ),
 525          'type'     => 'error',
 526      );
 527  
 528      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 529          wp_send_json_error( $response );
 530      }
 531  
 532      if ( empty( $_POST['id'] ) ) {
 533          wp_send_json_error( $response );
 534      }
 535  
 536      $thread_ids = wp_parse_id_list( $_POST['id'] );
 537  
 538      foreach ( $thread_ids as $thread_id ) {
 539          if ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) {
 540              wp_send_json_error( $response );
 541          }
 542  
 543          messages_delete_thread( $thread_id );
 544      }
 545  
 546      wp_send_json_success( array(
 547          'feedback' => __( 'Messages deleted', 'buddypress' ),
 548          'type'     => 'success',
 549      ) );
 550  }
 551  
 552  /**
 553   * @since 3.0.0
 554   */
 555  function bp_nouveau_ajax_star_thread_messages() {
 556      if ( empty( $_POST['action'] ) ) {
 557          wp_send_json_error();
 558      }
 559  
 560      $action = str_replace( 'messages_', '', $_POST['action'] );
 561  
 562      if ( 'star' === $action ) {
 563          $error_message = __( 'There was a problem starring your messages. Please try again.', 'buddypress' );
 564      } else {
 565          $error_message = __( 'There was a problem unstarring your messages. Please try again.', 'buddypress' );
 566      }
 567  
 568      $response = array(
 569          'feedback' => esc_html( $error_message ),
 570          'type'     => 'error',
 571      );
 572  
 573      if ( false === bp_is_active( 'messages', 'star' ) || empty( $_POST['id'] ) ) {
 574          wp_send_json_error( $response );
 575      }
 576  
 577      // Check capability.
 578      if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) {
 579          wp_send_json_error( $response );
 580      }
 581  
 582      $ids      = wp_parse_id_list( $_POST['id'] );
 583      $messages = array();
 584  
 585      // Use global nonce for bulk actions involving more than one id
 586      if ( 1 !== count( $ids ) ) {
 587          if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 588              wp_send_json_error( $response );
 589          }
 590  
 591          foreach ( $ids as $mid ) {
 592              if ( 'star' === $action ) {
 593                  bp_messages_star_set_action( array(
 594                      'action'     => 'star',
 595                      'message_id' => $mid,
 596                  ) );
 597              } else {
 598                  $thread_id = messages_get_message_thread_id( $mid );
 599  
 600                  bp_messages_star_set_action( array(
 601                      'action'    => 'unstar',
 602                      'thread_id' => $thread_id,
 603                      'bulk'      => true
 604                  ) );
 605              }
 606  
 607              $messages[ $mid ] = array(
 608                  'star_link' => bp_get_the_message_star_action_link( array(
 609                      'message_id' => $mid,
 610                      'url_only'  => true,
 611                  ) ),
 612                  'is_starred' => 'star' === $action,
 613              );
 614          }
 615  
 616      // Use global star nonce for bulk actions involving one id or regular action
 617      } else {
 618          $id = reset( $ids );
 619  
 620          if ( empty( $_POST['star_nonce'] ) || ! wp_verify_nonce( $_POST['star_nonce'], 'bp-messages-star-' . $id ) ) {
 621              wp_send_json_error( $response );
 622          }
 623  
 624          bp_messages_star_set_action( array(
 625              'action'     => $action,
 626              'message_id' => $id,
 627          ) );
 628  
 629          $messages[ $id ] = array(
 630              'star_link' => bp_get_the_message_star_action_link( array(
 631                  'message_id' => $id,
 632                  'url_only'  => true,
 633              ) ),
 634              'is_starred' => 'star' === $action,
 635          );
 636      }
 637  
 638      if ( 'star' === $action ) {
 639          $success_message = __( 'Messages successfully starred.', 'buddypress' );
 640      } else {
 641          $success_message = __( 'Messages successfully unstarred.', 'buddypress' );
 642      }
 643  
 644      wp_send_json_success( array(
 645          'feedback' => esc_html( $success_message ),
 646          'type'     => 'success',
 647          'messages' => $messages,
 648      ) );
 649  }
 650  
 651  /**
 652   * @since 3.0.0
 653   */
 654  function bp_nouveau_ajax_readunread_thread_messages() {
 655      if ( empty( $_POST['action'] ) ) {
 656          wp_send_json_error();
 657      }
 658  
 659      $action = str_replace( 'messages_', '', $_POST['action'] );
 660  
 661      $response = array(
 662          'feedback' => __( 'There was a problem marking your messages as read. Please try again.', 'buddypress' ),
 663          'type'     => 'error',
 664      );
 665  
 666      if ( 'unread' === $action ) {
 667          $response = array(
 668              'feedback' => __( 'There was a problem marking your messages as unread. Please try again.', 'buddypress' ),
 669              'type'     => 'error',
 670          );
 671      }
 672  
 673      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 674          wp_send_json_error( $response );
 675      }
 676  
 677      if ( empty( $_POST['id'] ) ) {
 678          wp_send_json_error( $response );
 679      }
 680  
 681      $thread_ids = wp_parse_id_list( $_POST['id'] );
 682  
 683      $response['messages'] = array();
 684  
 685      if ( 'unread' === $action ) {
 686          $response['feedback'] = __( 'Messages marked as unread.', 'buddypress' );
 687      } else {
 688          $response['feedback'] = __( 'Messages marked as read.', 'buddypress' );
 689      }
 690  
 691      foreach ( $thread_ids as $thread_id ) {
 692          if ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) {
 693              wp_send_json_error( $response );
 694          }
 695  
 696          if ( 'unread' === $action ) {
 697              // Mark unread
 698              messages_mark_thread_unread( $thread_id );
 699          } else {
 700              // Mark read
 701              messages_mark_thread_read( $thread_id );
 702          }
 703  
 704          $response['messages'][ $thread_id ] = array(
 705              'unread' => 'unread' === $action,
 706          );
 707      }
 708  
 709      $response['type'] = 'success';
 710  
 711      wp_send_json_success( $response );
 712  }
 713  
 714  /**
 715   * @since 3.0.0
 716   */
 717  function bp_nouveau_ajax_dismiss_sitewide_notice() {
 718      if ( empty( $_POST['action'] ) ) {
 719          wp_send_json_error();
 720      }
 721  
 722      $response = array(
 723          '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>',
 724          'type'     => 'error',
 725      );
 726  
 727      if ( false === bp_is_active( 'messages' ) ) {
 728          wp_send_json_error( $response );
 729      }
 730  
 731      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
 732          wp_send_json_error( $response );
 733      }
 734  
 735      // Check capability.
 736      if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) {
 737          wp_send_json_error( $response );
 738      }
 739  
 740      // Mark the active notice as closed.
 741      $notice = BP_Messages_Notice::get_active();
 742  
 743      if ( ! empty( $notice->id ) ) {
 744          $user_id = bp_loggedin_user_id();
 745  
 746          $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 747  
 748          if ( empty( $closed_notices ) ) {
 749              $closed_notices = array();
 750          }
 751  
 752          // Add the notice to the array of the user's closed notices.
 753          $closed_notices[] = (int) $notice->id;
 754          bp_update_user_meta( $user_id, 'closed_notices', array_map( 'absint', array_unique( $closed_notices ) ) );
 755  
 756          wp_send_json_success( array(
 757              'feedback' => '<div class="bp-feedback info"><span class="bp-icon" aria-hidden="true"></span><p>' . __( 'Sitewide notice dismissed', 'buddypress' ) . '</p></div>',
 758              'type'     => 'success',
 759          ) );
 760      }
 761  }


Generated: Thu Sep 19 01:01:39 2019 Cross-referenced by PHPXref 0.7.1