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


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1