[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Activity 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              'activity_filter' => array(
  16                  'function' => 'bp_nouveau_ajax_object_template_loader',
  17                  'nopriv'   => true,
  18              ),
  19          ),
  20          array(
  21              'get_single_activity_content' => array(
  22                  'function' => 'bp_nouveau_ajax_get_single_activity_content',
  23                  'nopriv'   => true,
  24              ),
  25          ),
  26          array(
  27              'activity_mark_fav' => array(
  28                  'function' => 'bp_nouveau_ajax_mark_activity_favorite',
  29                  'nopriv'   => false,
  30              ),
  31          ),
  32          array(
  33              'activity_mark_unfav' => array(
  34                  'function' => 'bp_nouveau_ajax_unmark_activity_favorite',
  35                  'nopriv'   => false,
  36              ),
  37          ),
  38          array(
  39              'activity_clear_new_mentions' => array(
  40                  'function' => 'bp_nouveau_ajax_clear_new_mentions',
  41                  'nopriv'   => false,
  42              ),
  43          ),
  44          array(
  45              'delete_activity' => array(
  46                  'function' => 'bp_nouveau_ajax_delete_activity',
  47                  'nopriv'   => false,
  48              ),
  49          ),
  50          array(
  51              'new_activity_comment' => array(
  52                  'function' => 'bp_nouveau_ajax_new_activity_comment',
  53                  'nopriv'   => false,
  54              ),
  55          ),
  56          array(
  57              'bp_nouveau_get_activity_objects' => array(
  58                  'function' => 'bp_nouveau_ajax_get_activity_objects',
  59                  'nopriv'   => false,
  60              ),
  61          ),
  62          array(
  63              'post_update' => array(
  64                  'function' => 'bp_nouveau_ajax_post_update',
  65                  'nopriv'   => false,
  66              ),
  67          ),
  68          array(
  69              'bp_spam_activity' => array(
  70                  'function' => 'bp_nouveau_ajax_spam_activity',
  71                  'nopriv'   => false,
  72              ),
  73          ),
  74      );
  75  
  76      foreach ( $ajax_actions as $ajax_action ) {
  77          $action = key( $ajax_action );
  78  
  79          add_action( 'wp_ajax_' . $action, $ajax_action[ $action ]['function'] );
  80  
  81          if ( ! empty( $ajax_action[ $action ]['nopriv'] ) ) {
  82              add_action( 'wp_ajax_nopriv_' . $action, $ajax_action[ $action ]['function'] );
  83          }
  84      }
  85  }, 12 );
  86  
  87  /**
  88   * Mark an activity as a favourite via a POST request.
  89   *
  90   * @since 3.0.0
  91   *
  92   * @return string JSON reply
  93   */
  94  function bp_nouveau_ajax_mark_activity_favorite() {
  95      if ( ! bp_is_post_request() ) {
  96          wp_send_json_error();
  97      }
  98  
  99      // Nonce check!
 100      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
 101          wp_send_json_error();
 102      }
 103  
 104      $activity_id   = (int) $_POST['id'];
 105      $activity_item = new BP_Activity_Activity( $activity_id );
 106      if ( ! bp_activity_user_can_read( $activity_item, bp_loggedin_user_id() ) ) {
 107          wp_send_json_error();
 108      }
 109  
 110      if ( bp_activity_add_user_favorite( $_POST['id'] ) ) {
 111          $response = array( 'content' => __( 'Remove Favorite', 'buddypress' ) );
 112  
 113          if ( ! bp_is_user() ) {
 114              $fav_count = (int) bp_get_total_favorite_count_for_user( bp_loggedin_user_id() );
 115  
 116              if ( 1 === $fav_count ) {
 117                  $response['directory_tab'] = '<li id="activity-favorites" data-bp-scope="favorites" data-bp-object="activity">
 118                      <a href="' . bp_loggedin_user_domain() . bp_nouveau_get_component_slug( 'activity' ) . '/favorites/">
 119                          ' . esc_html__( 'My Favorites', 'buddypress' ) . '
 120                      </a>
 121                  </li>';
 122              } else {
 123                  $response['fav_count'] = $fav_count;
 124              }
 125          }
 126  
 127          wp_send_json_success( $response );
 128      } else {
 129          wp_send_json_error();
 130      }
 131  }
 132  
 133  /**
 134   * Un-favourite an activity via a POST request.
 135   *
 136   * @since 3.0.0
 137   *
 138   * @return string JSON reply
 139   */
 140  function bp_nouveau_ajax_unmark_activity_favorite() {
 141      if ( ! bp_is_post_request() ) {
 142          wp_send_json_error();
 143      }
 144  
 145      // Nonce check!
 146      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
 147          wp_send_json_error();
 148      }
 149  
 150      if ( bp_activity_remove_user_favorite( $_POST['id'] ) ) {
 151          $response = array( 'content' => __( 'Mark as Favorite', 'buddypress' ) );
 152  
 153          $fav_count = (int) bp_get_total_favorite_count_for_user( bp_loggedin_user_id() );
 154  
 155          if ( 0 === $fav_count && ! bp_is_single_activity() ) {
 156              $response['no_favorite'] = '<li><div class="bp-feedback bp-messages info">
 157                  ' . __( 'Sorry, there was no activity found. Please try a different filter.', 'buddypress' ) . '
 158              </div></li>';
 159          } else {
 160              $response['fav_count'] = $fav_count;
 161          }
 162  
 163          wp_send_json_success( $response );
 164      } else {
 165          wp_send_json_error();
 166      }
 167  }
 168  
 169  /**
 170   * Clear mentions if the directory tab is clicked
 171   *
 172   * @since 3.0.0
 173   *
 174   * @return string JSON reply
 175   */
 176  function bp_nouveau_ajax_clear_new_mentions() {
 177      if ( ! bp_is_post_request() ) {
 178          wp_send_json_error();
 179      }
 180  
 181      // Nonce check!
 182      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
 183          wp_send_json_error();
 184      }
 185  
 186      bp_activity_clear_new_mentions( bp_loggedin_user_id() );
 187      wp_send_json_success();
 188  }
 189  
 190  /**
 191   * Deletes an Activity item/Activity comment item received via a POST request.
 192   *
 193   * @since 3.0.0
 194   *
 195   * @return string JSON reply.
 196   */
 197  function bp_nouveau_ajax_delete_activity() {
 198      $response = array(
 199          'feedback' => sprintf(
 200              '<div class="bp-feedback bp-messages error">%s</div>',
 201              esc_html__( 'There was a problem when deleting. Please try again.', 'buddypress' )
 202          ),
 203      );
 204  
 205      // Bail if not a POST action.
 206      if ( ! bp_is_post_request() ) {
 207          wp_send_json_error( $response );
 208      }
 209  
 210      // Nonce check!
 211      if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bp_activity_delete_link' ) ) {
 212          wp_send_json_error( $response );
 213      }
 214  
 215      if ( ! is_user_logged_in() ) {
 216          wp_send_json_error( $response );
 217      }
 218  
 219      if ( empty( $_POST['id'] ) || ! is_numeric( $_POST['id'] ) ) {
 220          wp_send_json_error( $response );
 221      }
 222  
 223      $activity = new BP_Activity_Activity( (int) $_POST['id'] );
 224  
 225      // Check access.
 226      if ( ! bp_activity_user_can_delete( $activity ) ) {
 227          wp_send_json_error( $response );
 228      }
 229  
 230      /** This action is documented in bp-activity/bp-activity-actions.php */
 231      do_action( 'bp_activity_before_action_delete_activity', $activity->id, $activity->user_id );
 232  
 233      // Deleting an activity comment.
 234      if ( ! empty( $_POST['is_comment'] ) ) {
 235          // Get replies before they are deleted.
 236          $replies   = (array) BP_Activity_Activity::get_child_comments( $activity->id );
 237          $reply_ids = wp_list_pluck( $replies, 'id' );
 238  
 239          if ( ! bp_activity_delete_comment( $activity->item_id, $activity->id ) ) {
 240              wp_send_json_error( $response );
 241  
 242              // The comment and its replies has been deleted successfully.
 243          } else {
 244              $response = array(
 245                  'deleted' => array_merge(
 246                      array( $activity->id ),
 247                      $reply_ids
 248                  ),
 249              );
 250          }
 251  
 252      // Deleting an activity.
 253      } else {
 254          if ( ! bp_activity_delete( array( 'id' => $activity->id, 'user_id' => $activity->user_id ) ) ) {
 255              wp_send_json_error( $response );
 256  
 257              // The activity has been deleted successfully.
 258          } else {
 259              $response = array(
 260                  'deleted' => array( $activity->id ),
 261              );
 262          }
 263      }
 264  
 265      /** This action is documented in bp-activity/bp-activity-actions.php */
 266      do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id );
 267  
 268      // If on a single activity redirect to user's home.
 269      if ( ! empty( $_POST['is_single'] ) ) {
 270          $response['redirect'] = bp_core_get_user_domain( $activity->user_id );
 271          bp_core_add_message( __( 'Activity deleted successfully', 'buddypress' ) );
 272      }
 273  
 274      wp_send_json_success( $response );
 275  }
 276  
 277  /**
 278   * Fetches an activity's full, non-excerpted content via a POST request.
 279   * Used for the 'Read More' link on long activity items.
 280   *
 281   * @since 3.0.0
 282   *
 283   * @return string JSON reply
 284   */
 285  function bp_nouveau_ajax_get_single_activity_content() {
 286      $response = array(
 287          'feedback' => sprintf(
 288              '<div class="bp-feedback bp-messages error">%s</div>',
 289              esc_html__( 'There was a problem displaying the content. Please try again.', 'buddypress' )
 290          ),
 291      );
 292  
 293      // Bail if not a POST action.
 294      if ( ! bp_is_post_request() ) {
 295          wp_send_json_error( $response );
 296      }
 297  
 298      // Nonce check!
 299      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
 300          wp_send_json_error( $response );
 301      }
 302  
 303      $activity_array = bp_activity_get_specific(
 304          array(
 305              'activity_ids'     => $_POST['id'],
 306              'display_comments' => 'stream',
 307          )
 308      );
 309  
 310      if ( empty( $activity_array['activities'][0] ) ) {
 311          wp_send_json_error( $response );
 312      }
 313  
 314      $activity = $activity_array['activities'][0];
 315  
 316      /**
 317       * Fires before the return of an activity's full, non-excerpted content via a POST request.
 318       *
 319       * @since 3.0.0
 320       *
 321       * @param string $activity Activity content. Passed by reference.
 322       */
 323      do_action_ref_array( 'bp_nouveau_get_single_activity_content', array( &$activity ) );
 324  
 325      // Activity content retrieved through AJAX should run through normal filters, but not be truncated.
 326      remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
 327  
 328      /** This filter is documented in bp-activity/bp-activity-template.php */
 329      $content = apply_filters_ref_array(
 330          'bp_get_activity_content_body',
 331          array(
 332              $activity->content,
 333              &$activity
 334          )
 335      );
 336  
 337      wp_send_json_success( array( 'contents' => $content ) );
 338  }
 339  
 340  /**
 341   * Posts new Activity comments received via a POST request.
 342   *
 343   * @since 3.0.0
 344   *
 345   * @global BP_Activity_Template $activities_template
 346   *
 347   * @return string JSON reply
 348   */
 349  function bp_nouveau_ajax_new_activity_comment() {
 350      global $activities_template;
 351      $bp = buddypress();
 352  
 353      $response = array(
 354          'feedback' => sprintf(
 355              '<div class="bp-feedback bp-messages error">%s</div>',
 356              esc_html__( 'There was an error posting your reply. Please try again.', 'buddypress' )
 357          ),
 358      );
 359  
 360      // Bail if not a POST action.
 361      if ( ! bp_is_post_request() ) {
 362          wp_send_json_error( $response );
 363      }
 364  
 365      // Nonce check!
 366      if ( empty( $_POST['_wpnonce_new_activity_comment'] ) || ! wp_verify_nonce( $_POST['_wpnonce_new_activity_comment'], 'new_activity_comment' ) ) {
 367          wp_send_json_error( $response );
 368      }
 369  
 370      if ( ! is_user_logged_in() ) {
 371          wp_send_json_error( $response );
 372      }
 373  
 374      if ( empty( $_POST['content'] ) ) {
 375          wp_send_json_error( array( 'feedback' => sprintf(
 376              '<div class="bp-feedback bp-messages error">%s</div>',
 377              esc_html__( 'Please do not leave the comment area blank.', 'buddypress' )
 378          ) ) );
 379      }
 380  
 381      if ( empty( $_POST['form_id'] ) || empty( $_POST['comment_id'] ) || ! is_numeric( $_POST['form_id'] ) || ! is_numeric( $_POST['comment_id'] ) ) {
 382          wp_send_json_error( $response );
 383      }
 384  
 385      $activity_id   = (int) $_POST['form_id'];
 386      $activity_item = new BP_Activity_Activity( $activity_id );
 387      if ( ! bp_activity_user_can_read( $activity_item ) ) {
 388          wp_send_json_error( $response );
 389      }
 390  
 391      $comment_id = bp_activity_new_comment( array(
 392          'activity_id' => $_POST['form_id'],
 393          'content'     => $_POST['content'],
 394          'parent_id'   => $_POST['comment_id'],
 395      ) );
 396  
 397      if ( ! $comment_id ) {
 398          if ( ! empty( $bp->activity->errors['new_comment'] ) && is_wp_error( $bp->activity->errors['new_comment'] ) ) {
 399              $response = array( 'feedback' => sprintf(
 400                  '<div class="bp-feedback bp-messages error">%s</div>',
 401                  esc_html( $bp->activity->errors['new_comment']->get_error_message() )
 402              ) );
 403              unset( $bp->activity->errors['new_comment'] );
 404          }
 405  
 406          wp_send_json_error( $response );
 407      }
 408  
 409      // Load the new activity item into the $activities_template global.
 410      bp_has_activities(
 411          array(
 412              'display_comments' => 'stream',
 413              'hide_spam'        => false,
 414              'show_hidden'      => true,
 415              'include'          => $comment_id,
 416          )
 417      );
 418  
 419      // Swap the current comment with the activity item we just loaded.
 420      if ( isset( $activities_template->activities[0] ) ) {
 421          $activities_template->activity                  = new stdClass();
 422          $activities_template->activity->id              = $activities_template->activities[0]->item_id;
 423          $activities_template->activity->current_comment = $activities_template->activities[0];
 424  
 425          // Because the whole tree has not been loaded, we manually
 426          // determine depth.
 427          $depth     = 1;
 428          $parent_id = (int) $activities_template->activities[0]->secondary_item_id;
 429          while ( $parent_id !== (int) $activities_template->activities[0]->item_id ) {
 430              $depth++;
 431              $p_obj     = new BP_Activity_Activity( $parent_id );
 432              $parent_id = (int) $p_obj->secondary_item_id;
 433          }
 434          $activities_template->activity->current_comment->depth = $depth;
 435      }
 436  
 437      ob_start();
 438      // Get activity comment template part.
 439      bp_get_template_part( 'activity/comment' );
 440      $response = array( 'contents' => ob_get_contents() );
 441      ob_end_clean();
 442  
 443      unset( $activities_template );
 444  
 445      wp_send_json_success( $response );
 446  }
 447  
 448  /**
 449   * Get items to attach the activity to.
 450   *
 451   * This is used within the activity post form autocomplete field.
 452   *
 453   * @since 3.0.0
 454   *
 455   * @return string JSON reply
 456   */
 457  function bp_nouveau_ajax_get_activity_objects() {
 458      $response = array();
 459  
 460      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
 461          wp_send_json_error( $response );
 462      }
 463  
 464      if ( 'group' === $_POST['type'] ) {
 465          $groups = groups_get_groups(
 466              array(
 467                  'user_id'      => bp_loggedin_user_id(),
 468                  'search_terms' => $_POST['search'],
 469                  'show_hidden'  => true,
 470                  'per_page'     => 2,
 471              )
 472          );
 473  
 474          wp_send_json_success( array_map( 'bp_nouveau_prepare_group_for_js', $groups['groups'] ) );
 475      } else {
 476  
 477          /**
 478           * Filters the response for custom activity objects.
 479           *
 480           * @since 3.0.0
 481           *
 482           * @param array $response Array of custom response objects to send to AJAX return.
 483           * @param array $value    Activity object type from $_POST global.
 484           */
 485          $response = apply_filters( 'bp_nouveau_get_activity_custom_objects', $response, $_POST['type'] );
 486      }
 487  
 488      if ( empty( $response ) ) {
 489          wp_send_json_error( array( 'error' => __( 'No activities were found.', 'buddypress' ) ) );
 490      } else {
 491          wp_send_json_success( $response );
 492      }
 493  }
 494  
 495  /**
 496   * Processes Activity updates received via a POST request.
 497   *
 498   * @since 3.0.0
 499   *
 500   * @return string JSON reply
 501   */
 502  function bp_nouveau_ajax_post_update() {
 503      $bp = buddypress();
 504  
 505      if ( ! is_user_logged_in() || empty( $_POST['_wpnonce_post_update'] ) || ! wp_verify_nonce( $_POST['_wpnonce_post_update'], 'post_update' ) ) {
 506          wp_send_json_error();
 507      }
 508  
 509      if ( empty( $_POST['content'] ) ) {
 510          wp_send_json_error(
 511              array(
 512                  'message' => __( 'Please enter some content to post.', 'buddypress' ),
 513              )
 514          );
 515      }
 516  
 517      $activity_id = 0;
 518      $item_id     = 0;
 519      $object      = '';
 520      $is_private  = false;
 521  
 522      // Try to get the item id from posted variables.
 523      if ( ! empty( $_POST['item_id'] ) ) {
 524          $item_id = (int) $_POST['item_id'];
 525      }
 526  
 527      // Try to get the object from posted variables.
 528      if ( ! empty( $_POST['object'] ) ) {
 529          $object = sanitize_key( $_POST['object'] );
 530  
 531      // If the object is not set and we're in a group, set the item id and the object
 532      } elseif ( bp_is_group() ) {
 533          $item_id = bp_get_current_group_id();
 534          $object  = 'group';
 535          $status  = groups_get_current_group()->status;
 536      }
 537  
 538      if ( 'user' === $object && bp_is_active( 'activity' ) ) {
 539          $activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
 540  
 541      } elseif ( 'group' === $object ) {
 542          if ( $item_id && bp_is_active( 'groups' ) ) {
 543              // This function is setting the current group!
 544              $activity_id = groups_post_update(
 545                  array(
 546                      'content'  => $_POST['content'],
 547                      'group_id' => $item_id,
 548                  )
 549              );
 550  
 551              if ( empty( $status ) ) {
 552                  if ( ! empty( $bp->groups->current_group->status ) ) {
 553                      $status = $bp->groups->current_group->status;
 554                  } else {
 555                      $group  = groups_get_group( array( 'group_id' => $item_id ) );
 556                      $status = $group->status;
 557                  }
 558  
 559                  $is_private = 'public' !== $status;
 560              }
 561          }
 562  
 563      } else {
 564          /** This filter is documented in bp-activity/actions/post.php */
 565          $activity_id = apply_filters( 'bp_activity_custom_update', false, $object, $item_id, $_POST['content'] );
 566      }
 567  
 568      if ( empty( $activity_id ) ) {
 569          wp_send_json_error(
 570              array(
 571                  'message' => __( 'There was a problem posting your update. Please try again.', 'buddypress' ),
 572              )
 573          );
 574      }
 575  
 576      ob_start();
 577      if ( bp_has_activities( array( 'include' => $activity_id, 'show_hidden' => $is_private ) ) ) {
 578          while ( bp_activities() ) {
 579              bp_the_activity();
 580              bp_get_template_part( 'activity/entry' );
 581          }
 582      }
 583      $activity = ob_get_contents();
 584      ob_end_clean();
 585  
 586      wp_send_json_success( array(
 587          'id'           => $activity_id,
 588          'message'      => esc_html__( 'Update posted.', 'buddypress' ) . ' ' . sprintf( '<a href="%s" class="just-posted">%s</a>', esc_url( bp_activity_get_permalink( $activity_id ) ), esc_html__( 'View activity.', 'buddypress' ) ),
 589          'activity'     => $activity,
 590  
 591          /**
 592           * Filters whether or not an AJAX post update is private.
 593           *
 594           * @since 3.0.0
 595           *
 596           * @param string/bool $is_private Privacy status for the update.
 597           */
 598          'is_private'   => apply_filters( 'bp_nouveau_ajax_post_update_is_private', $is_private ),
 599          'is_directory' => bp_is_activity_directory(),
 600      ) );
 601  }
 602  
 603  /**
 604   * AJAX spam an activity item or comment.
 605   *
 606   * @since 3.0.0
 607   *
 608   * @return string JSON reply.
 609   */
 610  function bp_nouveau_ajax_spam_activity() {
 611      $bp = buddypress();
 612  
 613      $response = array(
 614          'feedback' => sprintf(
 615              '<div class="bp-feedback bp-messages error">%s</div>',
 616              esc_html__( 'There was a problem marking this activity as spam. Please try again.', 'buddypress' )
 617          ),
 618      );
 619  
 620      // Bail if not a POST action.
 621      if ( ! bp_is_post_request() ) {
 622          wp_send_json_error( $response );
 623      }
 624  
 625      if ( ! is_user_logged_in() || ! bp_is_active( 'activity' ) || empty( $bp->activity->akismet ) ) {
 626          wp_send_json_error( $response );
 627      }
 628  
 629      if ( empty( $_POST['id'] ) || ! is_numeric( $_POST['id'] ) ) {
 630          wp_send_json_error( $response );
 631      }
 632  
 633      // Is the current user allowed to spam items?
 634      if ( ! bp_activity_user_can_mark_spam() ) {
 635          wp_send_json_error( $response );
 636      }
 637  
 638      $activity = new BP_Activity_Activity( (int) $_POST['id'] );
 639  
 640      if ( empty( $activity->component ) ) {
 641          wp_send_json_error( $response );
 642      }
 643  
 644      // Nonce check!
 645      if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bp_activity_akismet_spam_' . $activity->id ) ) {
 646          wp_send_json_error( $response );
 647      }
 648  
 649      /** This action is documented in bp-activity/bp-activity-actions.php */
 650      do_action( 'bp_activity_before_action_spam_activity', $activity->id, $activity );
 651  
 652      // Mark as spam.
 653      bp_activity_mark_as_spam( $activity );
 654      $activity->save();
 655  
 656      /** This action is documented in bp-activity/bp-activity-actions.php */
 657      do_action( 'bp_activity_action_spam_activity', $activity->id, $activity->user_id );
 658  
 659      // Prepare the successfull reply
 660      $response = array( 'spammed' => $activity->id );
 661  
 662      // If on a single activity redirect to user's home.
 663      if ( ! empty( $_POST['is_single'] ) ) {
 664          $response['redirect'] = bp_core_get_user_domain( $activity->user_id );
 665          bp_core_add_message( __( 'This activity has been marked as spam and is no longer visible.', 'buddypress' ) );
 666      }
 667  
 668      // Send the json reply
 669      wp_send_json_success( $response );
 670  }


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