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


Generated: Mon Nov 18 01:01:37 2019 Cross-referenced by PHPXref 0.7.1