[ 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 8.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(
  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          if ( ! bp_activity_delete_comment( $activity->item_id, $activity->id ) ) {
 236              wp_send_json_error( $response );
 237          }
 238  
 239      // Deleting an activity.
 240      } else {
 241          if ( ! bp_activity_delete( array( 'id' => $activity->id, 'user_id' => $activity->user_id ) ) ) {
 242              wp_send_json_error( $response );
 243          }
 244      }
 245  
 246      /** This action is documented in bp-activity/bp-activity-actions.php */
 247      do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id );
 248  
 249      // The activity has been deleted successfully
 250      $response = array( 'deleted' => $activity->id );
 251  
 252      // If on a single activity redirect to user's home.
 253      if ( ! empty( $_POST['is_single'] ) ) {
 254          $response['redirect'] = bp_core_get_user_domain( $activity->user_id );
 255          bp_core_add_message( __( 'Activity deleted successfully', 'buddypress' ) );
 256      }
 257  
 258      wp_send_json_success( $response );
 259  }
 260  
 261  /**
 262   * Fetches an activity's full, non-excerpted content via a POST request.
 263   * Used for the 'Read More' link on long activity items.
 264   *
 265   * @since 3.0.0
 266   *
 267   * @return string JSON reply
 268   */
 269  function bp_nouveau_ajax_get_single_activity_content() {
 270      $response = array(
 271          'feedback' => sprintf(
 272              '<div class="bp-feedback bp-messages error">%s</div>',
 273              esc_html__( 'There was a problem displaying the content. Please try again.', 'buddypress' )
 274          ),
 275      );
 276  
 277      // Bail if not a POST action.
 278      if ( ! bp_is_post_request() ) {
 279          wp_send_json_error( $response );
 280      }
 281  
 282      // Nonce check!
 283      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
 284          wp_send_json_error( $response );
 285      }
 286  
 287      $activity_array = bp_activity_get_specific(
 288          array(
 289              'activity_ids'     => $_POST['id'],
 290              'display_comments' => 'stream',
 291          )
 292      );
 293  
 294      if ( empty( $activity_array['activities'][0] ) ) {
 295          wp_send_json_error( $response );
 296      }
 297  
 298      $activity = $activity_array['activities'][0];
 299  
 300      /**
 301       * Fires before the return of an activity's full, non-excerpted content via a POST request.
 302       *
 303       * @since 3.0.0
 304       *
 305       * @param string $activity Activity content. Passed by reference.
 306       */
 307      do_action_ref_array( 'bp_nouveau_get_single_activity_content', array( &$activity ) );
 308  
 309      // Activity content retrieved through AJAX should run through normal filters, but not be truncated.
 310      remove_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 );
 311  
 312      /** This filter is documented in bp-activity/bp-activity-template.php */
 313      $content = apply_filters_ref_array(
 314          'bp_get_activity_content_body',
 315          array(
 316              $activity->content,
 317              &$activity
 318          )
 319      );
 320  
 321      wp_send_json_success( array( 'contents' => $content ) );
 322  }
 323  
 324  /**
 325   * Posts new Activity comments received via a POST request.
 326   *
 327   * @since 3.0.0
 328   *
 329   * @global BP_Activity_Template $activities_template
 330   *
 331   * @return string JSON reply
 332   */
 333  function bp_nouveau_ajax_new_activity_comment() {
 334      global $activities_template;
 335      $bp = buddypress();
 336  
 337      $response = array(
 338          'feedback' => sprintf(
 339              '<div class="bp-feedback bp-messages error">%s</div>',
 340              esc_html__( 'There was an error posting your reply. Please try again.', 'buddypress' )
 341          ),
 342      );
 343  
 344      // Bail if not a POST action.
 345      if ( ! bp_is_post_request() ) {
 346          wp_send_json_error( $response );
 347      }
 348  
 349      // Nonce check!
 350      if ( empty( $_POST['_wpnonce_new_activity_comment'] ) || ! wp_verify_nonce( $_POST['_wpnonce_new_activity_comment'], 'new_activity_comment' ) ) {
 351          wp_send_json_error( $response );
 352      }
 353  
 354      if ( ! is_user_logged_in() ) {
 355          wp_send_json_error( $response );
 356      }
 357  
 358      if ( empty( $_POST['content'] ) ) {
 359          wp_send_json_error( array( 'feedback' => sprintf(
 360              '<div class="bp-feedback bp-messages error">%s</div>',
 361              esc_html__( 'Please do not leave the comment area blank.', 'buddypress' )
 362          ) ) );
 363      }
 364  
 365      if ( empty( $_POST['form_id'] ) || empty( $_POST['comment_id'] ) || ! is_numeric( $_POST['form_id'] ) || ! is_numeric( $_POST['comment_id'] ) ) {
 366          wp_send_json_error( $response );
 367      }
 368  
 369      $activity_id   = (int) $_POST['form_id'];
 370      $activity_item = new BP_Activity_Activity( $activity_id );
 371      if ( ! bp_activity_user_can_read( $activity_item ) ) {
 372          wp_send_json_error( $response );
 373      }
 374  
 375      $comment_id = bp_activity_new_comment( array(
 376          'activity_id' => $_POST['form_id'],
 377          'content'     => $_POST['content'],
 378          'parent_id'   => $_POST['comment_id'],
 379      ) );
 380  
 381      if ( ! $comment_id ) {
 382          if ( ! empty( $bp->activity->errors['new_comment'] ) && is_wp_error( $bp->activity->errors['new_comment'] ) ) {
 383              $response = array( 'feedback' => sprintf(
 384                  '<div class="bp-feedback bp-messages error">%s</div>',
 385                  esc_html( $bp->activity->errors['new_comment']->get_error_message() )
 386              ) );
 387              unset( $bp->activity->errors['new_comment'] );
 388          }
 389  
 390          wp_send_json_error( $response );
 391      }
 392  
 393      // Load the new activity item into the $activities_template global.
 394      bp_has_activities(
 395          array(
 396              'display_comments' => 'stream',
 397              'hide_spam'        => false,
 398              'show_hidden'      => true,
 399              'include'          => $comment_id,
 400          )
 401      );
 402  
 403      // Swap the current comment with the activity item we just loaded.
 404      if ( isset( $activities_template->activities[0] ) ) {
 405          $activities_template->activity                  = new stdClass();
 406          $activities_template->activity->id              = $activities_template->activities[0]->item_id;
 407          $activities_template->activity->current_comment = $activities_template->activities[0];
 408  
 409          // Because the whole tree has not been loaded, we manually
 410          // determine depth.
 411          $depth     = 1;
 412          $parent_id = (int) $activities_template->activities[0]->secondary_item_id;
 413          while ( $parent_id !== (int) $activities_template->activities[0]->item_id ) {
 414              $depth++;
 415              $p_obj     = new BP_Activity_Activity( $parent_id );
 416              $parent_id = (int) $p_obj->secondary_item_id;
 417          }
 418          $activities_template->activity->current_comment->depth = $depth;
 419      }
 420  
 421      ob_start();
 422      // Get activity comment template part.
 423      bp_get_template_part( 'activity/comment' );
 424      $response = array( 'contents' => ob_get_contents() );
 425      ob_end_clean();
 426  
 427      unset( $activities_template );
 428  
 429      wp_send_json_success( $response );
 430  }
 431  
 432  /**
 433   * Get items to attach the activity to.
 434   *
 435   * This is used within the activity post form autocomplete field.
 436   *
 437   * @since 3.0.0
 438   *
 439   * @return string JSON reply
 440   */
 441  function bp_nouveau_ajax_get_activity_objects() {
 442      $response = array();
 443  
 444      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_activity' ) ) {
 445          wp_send_json_error( $response );
 446      }
 447  
 448      if ( 'group' === $_POST['type'] ) {
 449          $groups = groups_get_groups(
 450              array(
 451                  'user_id'      => bp_loggedin_user_id(),
 452                  'search_terms' => $_POST['search'],
 453                  'show_hidden'  => true,
 454                  'per_page'     => 2,
 455              )
 456          );
 457  
 458          wp_send_json_success( array_map( 'bp_nouveau_prepare_group_for_js', $groups['groups'] ) );
 459      } else {
 460  
 461          /**
 462           * Filters the response for custom activity objects.
 463           *
 464           * @since 3.0.0
 465           *
 466           * @param array $response Array of custom response objects to send to AJAX return.
 467           * @param array $value    Activity object type from $_POST global.
 468           */
 469          $response = apply_filters( 'bp_nouveau_get_activity_custom_objects', $response, $_POST['type'] );
 470      }
 471  
 472      if ( empty( $response ) ) {
 473          wp_send_json_error( array( 'error' => __( 'No activities were found.', 'buddypress' ) ) );
 474      } else {
 475          wp_send_json_success( $response );
 476      }
 477  }
 478  
 479  /**
 480   * Processes Activity updates received via a POST request.
 481   *
 482   * @since 3.0.0
 483   *
 484   * @return string JSON reply
 485   */
 486  function bp_nouveau_ajax_post_update() {
 487      $bp = buddypress();
 488  
 489      if ( ! is_user_logged_in() || empty( $_POST['_wpnonce_post_update'] ) || ! wp_verify_nonce( $_POST['_wpnonce_post_update'], 'post_update' ) ) {
 490          wp_send_json_error();
 491      }
 492  
 493      if ( empty( $_POST['content'] ) ) {
 494          wp_send_json_error(
 495              array(
 496                  'message' => __( 'Please enter some content to post.', 'buddypress' ),
 497              )
 498          );
 499      }
 500  
 501      $activity_id = 0;
 502      $item_id     = 0;
 503      $object      = '';
 504      $is_private  = false;
 505  
 506      // Try to get the item id from posted variables.
 507      if ( ! empty( $_POST['item_id'] ) ) {
 508          $item_id = (int) $_POST['item_id'];
 509      }
 510  
 511      // Try to get the object from posted variables.
 512      if ( ! empty( $_POST['object'] ) ) {
 513          $object = sanitize_key( $_POST['object'] );
 514  
 515      // If the object is not set and we're in a group, set the item id and the object
 516      } elseif ( bp_is_group() ) {
 517          $item_id = bp_get_current_group_id();
 518          $object  = 'group';
 519          $status  = groups_get_current_group()->status;
 520      }
 521  
 522      if ( 'user' === $object && bp_is_active( 'activity' ) ) {
 523          $activity_id = bp_activity_post_update( array( 'content' => $_POST['content'] ) );
 524  
 525      } elseif ( 'group' === $object ) {
 526          if ( $item_id && bp_is_active( 'groups' ) ) {
 527              // This function is setting the current group!
 528              $activity_id = groups_post_update(
 529                  array(
 530                      'content'  => $_POST['content'],
 531                      'group_id' => $item_id,
 532                  )
 533              );
 534  
 535              if ( empty( $status ) ) {
 536                  if ( ! empty( $bp->groups->current_group->status ) ) {
 537                      $status = $bp->groups->current_group->status;
 538                  } else {
 539                      $group  = groups_get_group( array( 'group_id' => $group_id ) );
 540                      $status = $group->status;
 541                  }
 542  
 543                  $is_private = 'public' !== $status;
 544              }
 545          }
 546  
 547      } else {
 548          /** This filter is documented in bp-activity/actions/post.php */
 549          $activity_id = apply_filters( 'bp_activity_custom_update', false, $object, $item_id, $_POST['content'] );
 550      }
 551  
 552      if ( empty( $activity_id ) ) {
 553          wp_send_json_error(
 554              array(
 555                  'message' => __( 'There was a problem posting your update. Please try again.', 'buddypress' ),
 556              )
 557          );
 558      }
 559  
 560      ob_start();
 561      if ( bp_has_activities( array( 'include' => $activity_id, 'show_hidden' => $is_private ) ) ) {
 562          while ( bp_activities() ) {
 563              bp_the_activity();
 564              bp_get_template_part( 'activity/entry' );
 565          }
 566      }
 567      $acivity = ob_get_contents();
 568      ob_end_clean();
 569  
 570      wp_send_json_success( array(
 571          'id'           => $activity_id,
 572          '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' ) ),
 573          'activity'     => $acivity,
 574  
 575          /**
 576           * Filters whether or not an AJAX post update is private.
 577           *
 578           * @since 3.0.0
 579           *
 580           * @param string/bool $is_private Privacy status for the update.
 581           */
 582          'is_private'   => apply_filters( 'bp_nouveau_ajax_post_update_is_private', $is_private ),
 583          'is_directory' => bp_is_activity_directory(),
 584      ) );
 585  }
 586  
 587  /**
 588   * AJAX spam an activity item or comment.
 589   *
 590   * @since 3.0.0
 591   *
 592   * @return string JSON reply.
 593   */
 594  function bp_nouveau_ajax_spam_activity() {
 595      $bp = buddypress();
 596  
 597      $response = array(
 598          'feedback' => sprintf(
 599              '<div class="bp-feedback bp-messages error">%s</div>',
 600              esc_html__( 'There was a problem marking this activity as spam. Please try again.', 'buddypress' )
 601          ),
 602      );
 603  
 604      // Bail if not a POST action.
 605      if ( ! bp_is_post_request() ) {
 606          wp_send_json_error( $response );
 607      }
 608  
 609      if ( ! is_user_logged_in() || ! bp_is_active( 'activity' ) || empty( $bp->activity->akismet ) ) {
 610          wp_send_json_error( $response );
 611      }
 612  
 613      if ( empty( $_POST['id'] ) || ! is_numeric( $_POST['id'] ) ) {
 614          wp_send_json_error( $response );
 615      }
 616  
 617      // Is the current user allowed to spam items?
 618      if ( ! bp_activity_user_can_mark_spam() ) {
 619          wp_send_json_error( $response );
 620      }
 621  
 622      $activity = new BP_Activity_Activity( (int) $_POST['id'] );
 623  
 624      if ( empty( $activity->component ) ) {
 625          wp_send_json_error( $response );
 626      }
 627  
 628      // Nonce check!
 629      if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'bp_activity_akismet_spam_' . $activity->id ) ) {
 630          wp_send_json_error( $response );
 631      }
 632  
 633      /** This action is documented in bp-activity/bp-activity-actions.php */
 634      do_action( 'bp_activity_before_action_spam_activity', $activity->id, $activity );
 635  
 636      // Mark as spam.
 637      bp_activity_mark_as_spam( $activity );
 638      $activity->save();
 639  
 640      /** This action is documented in bp-activity/bp-activity-actions.php */
 641      do_action( 'bp_activity_action_spam_activity', $activity->id, $activity->user_id );
 642  
 643      // Prepare the successfull reply
 644      $response = array( 'spammed' => $activity->id );
 645  
 646      // If on a single activity redirect to user's home.
 647      if ( ! empty( $_POST['is_single'] ) ) {
 648          $response['redirect'] = bp_core_get_user_domain( $activity->user_id );
 649          bp_core_add_message( __( 'This activity has been marked as spam and is no longer visible.', 'buddypress' ) );
 650      }
 651  
 652      // Send the json reply
 653      wp_send_json_success( $response );
 654  }


Generated: Thu Sep 23 01:01:38 2021 Cross-referenced by PHPXref 0.7.1