[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-blogs/ -> bp-blogs-activity.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Blogs Activity.
   4   *
   5   * @package BuddyPress
   6   * @subpackage BlogsActivity
   7   * @since 1.5.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Register activity actions for the blogs component.
  15   *
  16   * @since 1.0.0
  17   *
  18   * @return bool|null Returns false if activity component is not active.
  19   */
  20  function bp_blogs_register_activity_actions() {
  21      if ( is_multisite() ) {
  22          bp_activity_set_action(
  23              buddypress()->blogs->id,
  24              'new_blog',
  25              __( 'New site created', 'buddypress' ),
  26              'bp_blogs_format_activity_action_new_blog',
  27              __( 'New Sites', 'buddypress' ),
  28              array( 'activity', 'member' ),
  29              0
  30          );
  31      }
  32  
  33      /**
  34       * Fires after the registry of the default blog component activity actions.
  35       *
  36       * @since 1.1.0
  37       */
  38      do_action( 'bp_blogs_register_activity_actions' );
  39  }
  40  add_action( 'bp_register_activity_actions', 'bp_blogs_register_activity_actions' );
  41  
  42  /**
  43   * Set up the tracking arguments for the 'post' post type.
  44   *
  45   * @since 2.5.0 This was moved out of the BP_Blogs_Component class.
  46   *
  47   * @see bp_activity_get_post_type_tracking_args() for information on parameters.
  48   *
  49   * @param object|null $params    Tracking arguments.
  50   * @param string|int  $post_type Post type to track.
  51   * @return object|null
  52   */
  53  function bp_blogs_register_post_tracking_args( $params = null, $post_type = 0 ) {
  54  
  55      /**
  56       * Filters the post types to track for the Blogs component.
  57       *
  58       * @since 1.5.0
  59       * @deprecated 2.3.0
  60       *
  61       * Make sure plugins still using 'bp_blogs_record_post_post_types'
  62       * to track their post types will generate new_blog_post activities
  63       * See https://buddypress.trac.wordpress.org/ticket/6306
  64       *
  65       * @param array $value Array of post types to track.
  66       */
  67      $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) );
  68      $post_types_array = array_flip( $post_types );
  69  
  70      if ( ! isset( $post_types_array[ $post_type ] ) ) {
  71          return $params;
  72      }
  73  
  74      // Set specific params for the 'post' post type.
  75      $params->component_id    = buddypress()->blogs->id;
  76      $params->action_id       = 'new_blog_post';
  77      $params->admin_filter    = __( 'New post published', 'buddypress' );
  78      $params->format_callback = 'bp_blogs_format_activity_action_new_blog_post';
  79      $params->front_filter    = __( 'Posts', 'buddypress' );
  80      $params->contexts        = array( 'activity', 'member' );
  81      $params->position        = 5;
  82  
  83      if ( post_type_supports( $post_type, 'comments' ) ) {
  84          $params->comment_action_id = 'new_blog_comment';
  85  
  86          /**
  87           * Filters the post types to track for the Blogs component.
  88           *
  89           * @since 1.5.0
  90           * @deprecated 2.5.0
  91           *
  92           * Make sure plugins still using 'bp_blogs_record_comment_post_types'
  93           * to track comment about their post types will generate new_blog_comment activities
  94           * See https://buddypress.trac.wordpress.org/ticket/6306
  95           *
  96           * @param array $value Array of post types to track.
  97           */
  98          $comment_post_types = apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) );
  99          $comment_post_types_array = array_flip( $comment_post_types );
 100  
 101          if ( isset( $comment_post_types_array[ $post_type ] ) ) {
 102              $params->comments_tracking = new stdClass();
 103              $params->comments_tracking->component_id    = buddypress()->blogs->id;
 104              $params->comments_tracking->action_id       = 'new_blog_comment';
 105              $params->comments_tracking->admin_filter    = __( 'New post comment posted', 'buddypress' );
 106              $params->comments_tracking->format_callback = 'bp_blogs_format_activity_action_new_blog_comment';
 107              $params->comments_tracking->front_filter    = __( 'Comments', 'buddypress' );
 108              $params->comments_tracking->contexts        = array( 'activity', 'member' );
 109              $params->comments_tracking->position        = 10;
 110          }
 111      }
 112  
 113      return $params;
 114  }
 115  add_filter( 'bp_activity_get_post_type_tracking_args', 'bp_blogs_register_post_tracking_args', 10, 2 );
 116  
 117  /**
 118   * Format 'new_blog' activity actions.
 119   *
 120   * @since 2.0.0
 121   *
 122   * @param string $action   Static activity action.
 123   * @param object $activity Activity data object.
 124   * @return string
 125   */
 126  function bp_blogs_format_activity_action_new_blog( $action, $activity ) {
 127      $blog_url  = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
 128      $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
 129  
 130      $action = sprintf( esc_html__( '%s created the site %s', 'buddypress' ), bp_core_get_userlink( $activity->user_id ), '<a href="' . esc_url( $blog_url ) . '">' . esc_html( $blog_name ) . '</a>' );
 131  
 132      // Legacy filter - requires the BP_Blogs_Blog object.
 133      if ( has_filter( 'bp_blogs_activity_created_blog_action' ) ) {
 134          $user_blog = BP_Blogs_Blog::get_user_blog( $activity->user_id, $activity->item_id );
 135          if ( $user_blog ) {
 136              $recorded_blog = new BP_Blogs_Blog( $user_blog );
 137          }
 138  
 139          if ( isset( $recorded_blog ) ) {
 140              $action = apply_filters( 'bp_blogs_activity_created_blog_action', $action, $recorded_blog, $blog_name, bp_blogs_get_blogmeta( $activity->item_id, 'description' ) );
 141          }
 142      }
 143  
 144      /**
 145       * Filters the new blog activity action for the new blog.
 146       *
 147       * @since 2.0.0
 148       *
 149       * @param string $action   Constructed activity action.
 150       * @param object $activity Activity data object.
 151       */
 152      return apply_filters( 'bp_blogs_format_activity_action_new_blog', $action, $activity );
 153  }
 154  
 155  /**
 156   * Format 'new_blog_post' activity actions.
 157   *
 158   * @since 2.0.0
 159   *
 160   * @param string $action   Static activity action.
 161   * @param object $activity Activity data object.
 162   * @return string Constructed activity action.
 163   */
 164  function bp_blogs_format_activity_action_new_blog_post( $action, $activity ) {
 165      $blog_url  = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
 166      $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
 167  
 168      if ( empty( $blog_url ) || empty( $blog_name ) ) {
 169          $blog_url  = get_home_url( $activity->item_id );
 170          $blog_name = get_blog_option( $activity->item_id, 'blogname' );
 171  
 172          bp_blogs_update_blogmeta( $activity->item_id, 'url', $blog_url );
 173          bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name );
 174      }
 175  
 176      /**
 177       * When the post is published we are faking an activity object
 178       * to which we add 2 properties :
 179       * - the post url
 180       * - the post title
 181       * This is done to build the 'post link' part of the activity
 182       * action string.
 183       * NB: in this case the activity has not yet been created.
 184       */
 185      if ( isset( $activity->post_url ) ) {
 186          $post_url = $activity->post_url;
 187  
 188      /**
 189       * The post_url property is not set, we need to build the url
 190       * thanks to the post id which is also saved as the secondary
 191       * item id property of the activity object.
 192       */
 193      } else {
 194          $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) );
 195      }
 196  
 197      // Should be the case when the post has just been published.
 198      if ( isset( $activity->post_title ) ) {
 199          $post_title = $activity->post_title;
 200  
 201      // If activity already exists try to get the post title from activity meta.
 202      } else if ( ! empty( $activity->id ) ) {
 203          $post_title = bp_activity_get_meta( $activity->id, 'post_title' );
 204      }
 205  
 206      /**
 207       * In case the post was published without a title
 208       * or the activity meta was not found.
 209       */
 210      if ( empty( $post_title ) ) {
 211          // Defaults to no title.
 212          $post_title = __( '(no title)', 'buddypress' );
 213  
 214          switch_to_blog( $activity->item_id );
 215  
 216          $post = get_post( $activity->secondary_item_id );
 217          if ( is_a( $post, 'WP_Post' ) ) {
 218              // Does the post have a title ?
 219              if ( ! empty( $post->post_title ) ) {
 220                  $post_title = $post->post_title;
 221              }
 222  
 223              // Make sure the activity exists before saving the post title in activity meta.
 224              if ( ! empty( $activity->id ) ) {
 225                  bp_activity_update_meta( $activity->id, 'post_title', $post_title );
 226              }
 227          }
 228  
 229          restore_current_blog();
 230      }
 231  
 232      // Build the 'post link' part of the activity action string.
 233      $post_link  = '<a href="' . esc_url( $post_url ) . '">' . esc_html( $post_title ) . '</a>';
 234  
 235      $user_link = bp_core_get_userlink( $activity->user_id );
 236  
 237      // Build the complete activity action string.
 238      if ( is_multisite() ) {
 239          $action  = sprintf( esc_html__( '%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress' ), $user_link, $post_link, '<a href="' . esc_url( $blog_url ) . '">' . esc_html( $blog_name ) . '</a>' );
 240      } else {
 241          $action  = sprintf( esc_html__( '%1$s wrote a new post, %2$s', 'buddypress' ), $user_link, $post_link );
 242      }
 243  
 244      // Legacy filter - requires the post object.
 245      if ( has_filter( 'bp_blogs_activity_new_post_action' ) ) {
 246          switch_to_blog( $activity->item_id );
 247          $post = get_post( $activity->secondary_item_id );
 248          restore_current_blog();
 249  
 250          if ( ! empty( $post ) && ! is_wp_error( $post ) ) {
 251              $action = apply_filters( 'bp_blogs_activity_new_post_action', $action, $post, $post_url );
 252          }
 253      }
 254  
 255      /**
 256       * Filters the new blog post action for the new blog.
 257       *
 258       * @since 2.0.0
 259       *
 260       * @param string $action   Constructed activity action.
 261       * @param object $activity Activity data object.
 262       */
 263      return apply_filters( 'bp_blogs_format_activity_action_new_blog_post', $action, $activity );
 264  }
 265  
 266  /**
 267   * Format 'new_blog_comment' activity actions.
 268   *
 269   * @since 2.0.0
 270   *
 271   * @param string $action   Static activity action.
 272   * @param object $activity Activity data object.
 273   * @return string Constructed activity action.
 274   */
 275  function bp_blogs_format_activity_action_new_blog_comment( $action, $activity ) {
 276      /**
 277       * When the comment is published we are faking an activity object
 278       * to which we add 4 properties :
 279       * - the post url
 280       * - the post title
 281       * - the blog url
 282       * - the blog name
 283       * This is done to build the 'post link' part of the activity
 284       * action string.
 285       * NB: in this case the activity has not yet been created.
 286       */
 287  
 288      $blog_url = false;
 289  
 290      // Try to get the blog url from the activity object
 291      if ( isset( $activity->blog_url ) ) {
 292          $blog_url = $activity->blog_url;
 293      } else {
 294          $blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
 295      }
 296  
 297      $blog_name = false;
 298  
 299      // Try to get the blog name from the activity object
 300      if ( isset( $activity->blog_name ) ) {
 301          $blog_name = $activity->blog_name;
 302      } else {
 303          $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
 304      }
 305  
 306      if ( empty( $blog_url ) || empty( $blog_name ) ) {
 307          $blog_url  = get_home_url( $activity->item_id );
 308          $blog_name = get_blog_option( $activity->item_id, 'blogname' );
 309  
 310          bp_blogs_update_blogmeta( $activity->item_id, 'url', $blog_url );
 311          bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name );
 312      }
 313  
 314      $post_url = false;
 315  
 316      // Try to get the post url from the activity object
 317      if ( isset( $activity->post_url ) ) {
 318          $post_url = $activity->post_url;
 319  
 320      /**
 321       * The post_url property is not set, we need to build the url
 322       * thanks to the post id which is also saved as the secondary
 323       * item id property of the activity object.
 324       */
 325      } elseif ( ! empty( $activity->id ) ) {
 326          $post_url = bp_activity_get_meta( $activity->id, 'post_url' );
 327      }
 328  
 329      $post_title = false;
 330  
 331      // Should be the case when the comment has just been published
 332      if ( isset( $activity->post_title ) ) {
 333          $post_title = $activity->post_title;
 334  
 335      // If activity already exists try to get the post title from activity meta
 336      } elseif ( ! empty( $activity->id ) ) {
 337          $post_title = bp_activity_get_meta( $activity->id, 'post_title' );
 338      }
 339  
 340      // Should only be empty at the time of post creation.
 341      if ( empty( $post_url ) || empty( $post_title ) ) {
 342          switch_to_blog( $activity->item_id );
 343  
 344          $comment = get_comment( $activity->secondary_item_id );
 345  
 346          if ( ! empty( $comment->comment_post_ID ) ) {
 347              $post_url = add_query_arg( 'p', $comment->comment_post_ID, trailingslashit( $blog_url ) );
 348              bp_activity_update_meta( $activity->id, 'post_url', $post_url );
 349  
 350              $post = get_post( $comment->comment_post_ID );
 351  
 352              if ( is_a( $post, 'WP_Post' ) ) {
 353                  $post_title = $post->post_title;
 354                  bp_activity_update_meta( $activity->id, 'post_title', $post_title );
 355              }
 356          }
 357  
 358          restore_current_blog();
 359      }
 360  
 361      $post_link = '<a href="' . esc_url( $post_url ) . '">' . esc_html( $post_title ) . '</a>';
 362      $user_link = bp_core_get_userlink( $activity->user_id );
 363  
 364      if ( is_multisite() ) {
 365          $action  = sprintf( esc_html__( '%1$s commented on the post, %2$s, on the site %3$s', 'buddypress' ), $user_link, $post_link, '<a href="' . esc_url( $blog_url ) . '">' . esc_html( $blog_name ) . '</a>' );
 366      } else {
 367          $action  = sprintf( esc_html__( '%1$s commented on the post, %2$s', 'buddypress' ), $user_link, $post_link );
 368      }
 369  
 370      // Legacy filter - requires the comment object.
 371      if ( has_filter( 'bp_blogs_activity_new_comment_action' ) ) {
 372          switch_to_blog( $activity->item_id );
 373          $comment = get_comment( $activity->secondary_item_id );
 374          restore_current_blog();
 375  
 376          if ( ! empty( $comment ) && ! is_wp_error( $comment ) ) {
 377              $action = apply_filters( 'bp_blogs_activity_new_comment_action', $action, $comment, $post_url . '#' . $activity->secondary_item_id );
 378          }
 379      }
 380  
 381      /**
 382       * Filters the new blog comment action for the new blog.
 383       *
 384       * @since 2.0.0
 385       *
 386       * @param string $action   Constructed activity action.
 387       * @param object $activity Activity data object.
 388       */
 389      return apply_filters( 'bp_blogs_format_activity_action_new_blog_comment', $action, $activity );
 390  }
 391  
 392  /**
 393   * Fetch data related to blogs at the beginning of an activity loop.
 394   *
 395   * This reduces database overhead during the activity loop.
 396   *
 397   * @since 2.0.0
 398   *
 399   * @param array $activities Array of activity items.
 400   * @return array
 401   */
 402  function bp_blogs_prefetch_activity_object_data( $activities ) {
 403      if ( empty( $activities ) ) {
 404          return $activities;
 405      }
 406  
 407      $blog_ids = array();
 408  
 409      foreach ( $activities as $activity ) {
 410          if ( buddypress()->blogs->id !== $activity->component ) {
 411              continue;
 412          }
 413  
 414          $blog_ids[] = $activity->item_id;
 415      }
 416  
 417      if ( ! empty( $blog_ids ) ) {
 418          bp_blogs_update_meta_cache( $blog_ids );
 419      }
 420  
 421      return $activities;
 422  }
 423  add_filter( 'bp_activity_prefetch_object_data', 'bp_blogs_prefetch_activity_object_data' );
 424  
 425  /**
 426   * Record blog-related activity to the activity stream.
 427   *
 428   * @since 1.0.0
 429   *
 430   * @see bp_activity_add() for description of parameters.
 431   *
 432   * @param array|string $args {
 433   *     See {@link bp_activity_add()} for complete description of arguments.
 434   *     The arguments listed here have different default values from
 435   *     bp_activity_add().
 436   *     @type string $component Default: 'blogs'.
 437   * }
 438   * @return WP_Error|bool|int On success, returns the activity ID. False on failure.
 439   */
 440  function bp_blogs_record_activity( $args = '' ) {
 441      $defaults = array(
 442          'user_id'           => bp_loggedin_user_id(),
 443          'action'            => '',
 444          'content'           => '',
 445          'primary_link'      => '',
 446          'component'         => buddypress()->blogs->id,
 447          'type'              => false,
 448          'item_id'           => false,
 449          'secondary_item_id' => false,
 450          'recorded_time'     => bp_core_current_time(),
 451          'hide_sitewide'     => false
 452      );
 453  
 454      $r = wp_parse_args( $args, $defaults );
 455  
 456      if ( ! empty( $r['action'] ) ) {
 457  
 458          /**
 459           * Filters the action associated with activity for activity stream.
 460           *
 461           * @since 1.2.0
 462           *
 463           * @param string $value Action for the activity stream.
 464           */
 465          $r['action'] = apply_filters( 'bp_blogs_record_activity_action', $r['action'] );
 466      }
 467  
 468      if ( ! empty( $r['content'] ) ) {
 469  
 470          /**
 471           * Filters the content associated with activity for activity stream.
 472           *
 473           * @since 1.2.0
 474           *
 475           * @param string $value Generated summary from content for the activity stream.
 476           * @param string $value Content for the activity stream.
 477           * @param array  $r     Array of arguments used for the activity stream item.
 478           */
 479          $r['content'] = apply_filters( 'bp_blogs_record_activity_content', bp_activity_create_summary( $r['content'], $r ), $r['content'], $r );
 480      }
 481  
 482      // Check for an existing entry and update if one exists.
 483      $id = bp_activity_get_activity_id( array(
 484          'user_id'           => $r['user_id'],
 485          'component'         => $r['component'],
 486          'type'              => $r['type'],
 487          'item_id'           => $r['item_id'],
 488          'secondary_item_id' => $r['secondary_item_id'],
 489      ) );
 490  
 491      return bp_activity_add( array( 'id' => $id, 'user_id' => $r['user_id'], 'action' => $r['action'], 'content' => $r['content'], 'primary_link' => $r['primary_link'], 'component' => $r['component'], 'type' => $r['type'], 'item_id' => $r['item_id'], 'secondary_item_id' => $r['secondary_item_id'], 'recorded_time' => $r['recorded_time'], 'hide_sitewide' => $r['hide_sitewide'] ) );
 492  }
 493  
 494  /**
 495   * Delete a blog-related activity stream item.
 496   *
 497   * @since 1.0.0
 498   *
 499   * @see bp_activity_delete() for description of parameters.
 500   *
 501   * @param array|string $args {
 502   *     See {@link bp_activity_delete()} for complete description of arguments.
 503   *     The arguments listed here have different default values from
 504   *     bp_activity_add().
 505   *     @type string $component Default: 'blogs'.
 506   * }
 507   * @return bool True on success, false on failure.
 508   */
 509  function bp_blogs_delete_activity( $args = '' ) {
 510      $r = bp_parse_args( $args, array(
 511          'item_id'           => false,
 512          'component'         => buddypress()->blogs->id,
 513          'type'              => false,
 514          'user_id'           => false,
 515          'secondary_item_id' => false
 516      ) );
 517  
 518      bp_activity_delete_by_item_id( $r );
 519  }
 520  
 521  /**
 522   * Check if a blog post's activity item should be closed from commenting.
 523   *
 524   * This mirrors the {@link comments_open()} and {@link _close_comments_for_old_post()}
 525   * functions, but for use with the BuddyPress activity stream to be as
 526   * lightweight as possible.
 527   *
 528   * By lightweight, we actually mirror a few of the blog's commenting settings
 529   * to blogmeta and checks the values in blogmeta instead.  This is to prevent
 530   * multiple {@link switch_to_blog()} calls in the activity stream.
 531   *
 532   * @since 2.0.0
 533   *
 534   * @param object $activity The BP_Activity_Activity object.
 535   * @return bool
 536   */
 537  function bp_blogs_comments_open( $activity ) {
 538      $open = true;
 539  
 540      $blog_id = $activity->item_id;
 541  
 542      // See if we've mirrored the close comments option before.
 543      $days_old = bp_blogs_get_blogmeta( $blog_id, 'close_comments_days_old' );
 544  
 545      // We've never cached these items before, so do it now.
 546      if ( '' === $days_old ) {
 547          switch_to_blog( $blog_id );
 548  
 549          // Use comments_open().
 550          remove_filter( 'comments_open', 'bp_comments_open', 10 );
 551          $open = comments_open( $activity->secondary_item_id );
 552          add_filter( 'comments_open', 'bp_comments_open', 10, 2 );
 553  
 554          // Might as well mirror values to blogmeta since we're here!
 555          $thread_depth = get_option( 'thread_comments' );
 556          if ( ! empty( $thread_depth ) ) {
 557              $thread_depth = get_option( 'thread_comments_depth' );
 558          } else {
 559              // Perhaps filter this?
 560              $thread_depth = 1;
 561          }
 562  
 563          bp_blogs_update_blogmeta( $blog_id, 'close_comments_for_old_posts', get_option( 'close_comments_for_old_posts' ) );
 564          bp_blogs_update_blogmeta( $blog_id, 'close_comments_days_old',      get_option( 'close_comments_days_old' ) );
 565          bp_blogs_update_blogmeta( $blog_id, 'thread_comments_depth',        $thread_depth );
 566  
 567          restore_current_blog();
 568  
 569      // Check blogmeta and manually check activity item.
 570      // Basically a copy of _close_comments_for_old_post().
 571      } else {
 572  
 573          // Comments are closed.
 574          if ( 'closed' == bp_activity_get_meta( $activity->id, 'post_comment_status' ) ) {
 575              return false;
 576          }
 577  
 578          if ( ! bp_blogs_get_blogmeta( $blog_id, 'close_comments_for_old_posts' ) ) {
 579              return $open;
 580          }
 581  
 582          $days_old = (int) $days_old;
 583          if ( ! $days_old ) {
 584              return $open;
 585          }
 586  
 587          /*
 588             Commenting out for now - needs some more thought...
 589             should we add the post type to activity meta?
 590  
 591          $post = get_post($post_id);
 592  
 593          // This filter is documented in wp-includes/comment.php
 594          $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
 595          if ( ! in_array( $post->post_type, $post_types ) )
 596              return $open;
 597          */
 598  
 599          if ( time() - strtotime( $activity->date_recorded ) > ( $days_old * DAY_IN_SECONDS ) ) {
 600              return false;
 601          }
 602  
 603          return $open;
 604      }
 605  
 606      return $open;
 607  }
 608  
 609  /** SITE TRACKING *******************************************************/
 610  
 611  /**
 612   * Add an activity entry for a newly-created site.
 613   *
 614   * Hooked to the 'bp_blogs_new_blog' action.
 615   *
 616   * @since 2.6.0
 617   *
 618   * @param BP_Blogs_Blog $recorded_blog Current site being recorded. Passed by reference.
 619   * @param bool          $is_private    Whether the current site being recorded is private.
 620   * @param bool          $is_recorded   Whether the current site was recorded.
 621   */
 622  function bp_blogs_record_activity_on_site_creation( $recorded_blog, $is_private, $is_recorded, $no_activity ) {
 623      // Only record this activity if the blog is public.
 624      if ( ! $is_private && ! $no_activity && bp_blogs_is_blog_trackable( $recorded_blog->blog_id, $recorded_blog->user_id ) ) {
 625          bp_blogs_record_activity( array(
 626              'user_id'      => $recorded_blog->user_id,
 627  
 628              /**
 629               * Filters the activity created blog primary link.
 630               *
 631               * @since 1.1.0
 632               *
 633               * @param string $value Blog primary link.
 634               * @param int    $value Blog ID.
 635               */
 636              'primary_link' => apply_filters( 'bp_blogs_activity_created_blog_primary_link', bp_blogs_get_blogmeta( $recorded_blog->blog_id, 'url' ), $recorded_blog->blog_id ),
 637              'type'         => 'new_blog',
 638              'item_id'      => $recorded_blog->blog_id
 639          ) );
 640      }
 641  }
 642  add_action( 'bp_blogs_new_blog', 'bp_blogs_record_activity_on_site_creation', 10, 4 );
 643  
 644  /**
 645   * Deletes the 'new_blog' activity entry when a site is deleted.
 646   *
 647   * @since 2.6.0
 648   *
 649   * @param int $blog_id Site ID.
 650   */
 651  function bp_blogs_delete_new_blog_activity_for_site( $blog_id, $user_id = 0 ) {
 652      $args = array(
 653          'item_id'   => $blog_id,
 654          'component' => buddypress()->blogs->id,
 655          'type'      => 'new_blog'
 656      );
 657  
 658      /**
 659       * In the case a user is removed, make sure he is the author of the 'new_blog' activity
 660       * when trying to delete it.
 661       */
 662      if ( ! empty( $user_id ) ) {
 663          $args['user_id'] = $user_id;
 664      }
 665  
 666      bp_blogs_delete_activity( $args );
 667  }
 668  add_action( 'bp_blogs_remove_blog',          'bp_blogs_delete_new_blog_activity_for_site', 10, 1 );
 669  add_action( 'bp_blogs_remove_blog_for_user', 'bp_blogs_delete_new_blog_activity_for_site', 10, 2 );
 670  
 671  /**
 672   * Delete all 'blogs' activity items for a site when the site is deleted.
 673   *
 674   * @since 2.6.0
 675   *
 676   * @param int $blog_id Site ID.
 677   */
 678  function bp_blogs_delete_activity_for_site( $blog_id ) {
 679      bp_blogs_delete_activity( array(
 680          'item_id'   => $blog_id,
 681          'component' => buddypress()->blogs->id,
 682          'type'      => false
 683      ) );
 684  }
 685  add_action( 'bp_blogs_remove_data_for_blog', 'bp_blogs_delete_activity_for_site' );
 686  
 687  /**
 688   * Remove a blog post activity item from the activity stream.
 689   *
 690   * @since 1.0.0
 691   *
 692   * @param int $post_id ID of the post to be removed.
 693   * @param int $blog_id Optional. Defaults to current blog ID.
 694   * @param int $user_id Optional. Defaults to the logged-in user ID. This param
 695   *                     is currently unused in the function (but is passed to hooks).
 696   * @return bool
 697   */
 698  function bp_blogs_remove_post( $post_id, $blog_id = 0, $user_id = 0 ) {
 699      global $wpdb;
 700  
 701      if ( empty( $wpdb->blogid ) ) {
 702          return false;
 703      }
 704  
 705      $post_id = (int) $post_id;
 706  
 707      if ( ! $blog_id ) {
 708          $blog_id = (int) $wpdb->blogid;
 709      }
 710  
 711      if ( ! $user_id ) {
 712          $user_id = bp_loggedin_user_id();
 713      }
 714  
 715      /**
 716       * Fires before removal of a blog post activity item from the activity stream.
 717       *
 718       * @since 1.5.0
 719       *
 720       * @param int $blog_id ID of the blog associated with the post that was removed.
 721       * @param int $post_id ID of the post that was removed.
 722       * @param int $user_id ID of the user having the blog removed for.
 723       */
 724      do_action( 'bp_blogs_before_remove_post', $blog_id, $post_id, $user_id );
 725  
 726      bp_blogs_delete_activity( array(
 727          'item_id'           => $blog_id,
 728          'secondary_item_id' => $post_id,
 729          'component'         => buddypress()->blogs->id,
 730          'type'              => 'new_blog_post'
 731      ) );
 732  
 733      /**
 734       * Fires after removal of a blog post activity item from the activity stream.
 735       *
 736       * @since 1.0.0
 737       *
 738       * @param int $blog_id ID of the blog associated with the post that was removed.
 739       * @param int $post_id ID of the post that was removed.
 740       * @param int $user_id ID of the user having the blog removed for.
 741       */
 742      do_action( 'bp_blogs_remove_post', $blog_id, $post_id, $user_id );
 743  }
 744  add_action( 'delete_post', 'bp_blogs_remove_post' );
 745  
 746  /** POST COMMENT SYNCHRONIZATION ****************************************/
 747  
 748  /**
 749   * Syncs activity comments and posts them back as blog comments.
 750   *
 751   * Note: This is only a one-way sync - activity comments -> blog comment.
 752   *
 753   * For blog post -> activity comment, see {@link bp_activity_post_type_comment()}.
 754   *
 755   * @since 2.0.0
 756   * @since 2.5.0 Allow custom post types to sync their comments with activity ones
 757   *
 758   * @param int    $comment_id      The activity ID for the posted activity comment.
 759   * @param array  $params          Parameters for the activity comment.
 760   * @param object $parent_activity Parameters of the parent activity item (in this case, the blog post).
 761   */
 762  function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_activity ) {
 763      // if parent activity isn't a post type having the buddypress-activity support, stop now!
 764      if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
 765          return;
 766      }
 767  
 768      // If activity comments are disabled for blog posts, stop now!
 769      if ( bp_disable_blogforum_comments() ) {
 770          return;
 771      }
 772  
 773      // Do not sync if the activity comment was marked as spam.
 774      $activity = new BP_Activity_Activity( $comment_id );
 775      if ( $activity->is_spam ) {
 776          return;
 777      }
 778  
 779      // Check if comments are still open for parent item.
 780      $comments_open = bp_blogs_comments_open( $parent_activity );
 781      if ( ! $comments_open ) {
 782          return;
 783      }
 784  
 785      // Get userdata.
 786      if ( $params['user_id'] == bp_loggedin_user_id() ) {
 787          $user = buddypress()->loggedin_user->userdata;
 788      } else {
 789          $user = bp_core_get_core_userdata( $params['user_id'] );
 790      }
 791  
 792      // Get associated post type and set default comment parent
 793      $post_type      = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' );
 794      $comment_parent = 0;
 795  
 796      // See if a parent WP comment ID exists.
 797      if ( ! empty( $params['parent_id'] ) && ! empty( $post_type ) ) {
 798          $comment_parent = bp_activity_get_meta( $params['parent_id'], "bp_blogs_{$post_type}_comment_id" );
 799      }
 800  
 801      // Comment args.
 802      $args = array(
 803          'comment_post_ID'      => $parent_activity->secondary_item_id,
 804          'comment_author'       => bp_core_get_user_displayname( $params['user_id'] ),
 805          'comment_author_email' => $user->user_email,
 806          'comment_author_url'   => bp_core_get_user_domain( $params['user_id'], $user->user_nicename, $user->user_login ),
 807          'comment_content'      => $params['content'],
 808          'comment_type'         => '', // Could be interesting to add 'buddypress' here...
 809          'comment_parent'       => (int) $comment_parent,
 810          'user_id'              => $params['user_id'],
 811          'comment_approved'     => 1
 812      );
 813  
 814      // Prevent separate activity entry being made.
 815      remove_action( 'comment_post', 'bp_activity_post_type_comment', 10 );
 816  
 817      // Handle multisite.
 818      switch_to_blog( $parent_activity->item_id );
 819  
 820      // Handle timestamps for the WP comment after we've switched to the blog.
 821      $args['comment_date']     = current_time( 'mysql' );
 822      $args['comment_date_gmt'] = current_time( 'mysql', 1 );
 823  
 824      // Post the comment.
 825      $post_comment_id = wp_insert_comment( $args );
 826  
 827      // Add meta to comment.
 828      add_comment_meta( $post_comment_id, 'bp_activity_comment_id', $comment_id );
 829  
 830      // Add meta to activity comment.
 831      if ( ! empty( $post_type ) ) {
 832          bp_activity_update_meta( $comment_id, "bp_blogs_{$post_type}_comment_id", $post_comment_id );
 833      }
 834  
 835      // Resave activity comment with WP comment permalink.
 836      //
 837      // in bp_blogs_activity_comment_permalink(), we change activity comment
 838      // permalinks to use the post comment link
 839      //
 840      // @todo since this is done after AJAX posting, the activity comment permalink
 841      // doesn't change on the front end until the next page refresh.
 842      $resave_activity = new BP_Activity_Activity( $comment_id );
 843      $resave_activity->primary_link = get_comment_link( $post_comment_id );
 844  
 845      /**
 846       * Now that the activity id exists and the post comment was created, we don't need to update
 847       * the content of the comment as there are no chances it has evolved.
 848       */
 849      remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
 850  
 851      $resave_activity->save();
 852  
 853      // Add the edit activity comment hook back.
 854      add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
 855  
 856      // Multisite again!
 857      restore_current_blog();
 858  
 859      // Add the comment hook back.
 860      add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
 861  
 862      /**
 863       * Fires after activity comments have been synced and posted as blog comments.
 864       *
 865       * @since 2.0.0
 866       *
 867       * @param int    $comment_id      The activity ID for the posted activity comment.
 868       * @param array  $args            Array of args used for the comment syncing.
 869       * @param object $parent_activity Parameters of the blog post parent activity item.
 870       * @param object $user            User data object for the blog comment.
 871       */
 872      do_action( 'bp_blogs_sync_add_from_activity_comment', $comment_id, $args, $parent_activity, $user );
 873  }
 874  add_action( 'bp_activity_comment_posted', 'bp_blogs_sync_add_from_activity_comment', 10, 3 );
 875  
 876  /**
 877   * Deletes the blog comment when the associated activity comment is deleted.
 878   *
 879   * Note: This is hooked on the 'bp_activity_delete_comment_pre' filter instead
 880   * of the 'bp_activity_delete_comment' action because we need to fetch the
 881   * activity comment children before they are deleted.
 882   *
 883   * @since 2.0.0
 884   * @since 2.5.0 Add the $delected parameter
 885   *
 886   * @param bool $retval             Whether BuddyPress should continue or not.
 887   * @param int  $parent_activity_id The parent activity ID for the activity comment.
 888   * @param int  $activity_id        The activity ID for the pending deleted activity comment.
 889   * @param bool $deleted            Whether the comment was deleted or not.
 890   * @return bool
 891   */
 892  function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id, &$deleted ) {
 893      // Check if parent activity is a blog post.
 894      $parent_activity = new BP_Activity_Activity( $parent_activity_id );
 895  
 896      // if parent activity isn't a post type having the buddypress-activity support, stop now!
 897      if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
 898          return $retval;
 899      }
 900  
 901      // Fetch the activity comments for the activity item.
 902      $activity = bp_activity_get( array(
 903          'in'               => $activity_id,
 904          'display_comments' => 'stream',
 905          'spam'             => 'all',
 906      ) );
 907  
 908      // Get all activity comment IDs for the pending deleted item.
 909      $activity_ids   = bp_activity_recurse_comments_activity_ids( $activity );
 910      $activity_ids[] = $activity_id;
 911  
 912      // Handle multisite
 913      // switch to the blog where the comment was made.
 914      switch_to_blog( $parent_activity->item_id );
 915  
 916      // Remove associated blog comments.
 917      bp_blogs_remove_associated_blog_comments( $activity_ids, current_user_can( 'moderate_comments' ) );
 918  
 919      // Multisite again!
 920      restore_current_blog();
 921  
 922      // Rebuild activity comment tree
 923      // emulate bp_activity_delete_comment().
 924      BP_Activity_Activity::rebuild_activity_comment_tree( $parent_activity_id );
 925  
 926      // Avoid the error message although the comments were successfully deleted
 927      $deleted = true;
 928  
 929      // We're overriding the default bp_activity_delete_comment() functionality
 930      // so we need to return false.
 931      return false;
 932  }
 933  add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activity_comment', 10, 4 );
 934  
 935  /**
 936   * Updates the blog comment when the associated activity comment is edited.
 937   *
 938   * @since 2.0.0
 939   *
 940   * @param BP_Activity_Activity $activity The activity object.
 941   */
 942  function bp_blogs_sync_activity_edit_to_post_comment( BP_Activity_Activity $activity ) {
 943      // This is a new entry, so stop!
 944      // We only want edits!
 945      if ( empty( $activity->id ) || bp_disable_blogforum_comments() ) {
 946          return;
 947      }
 948  
 949      // fetch parent activity item
 950      $parent_activity = new BP_Activity_Activity( $activity->item_id );
 951  
 952      // if parent activity isn't a post type having the buddypress-activity support for comments, stop now!
 953      if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
 954          return;
 955      }
 956  
 957      $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' );
 958  
 959      // No associated post type for this activity comment, stop.
 960      if ( ! $post_type ) {
 961          return;
 962      }
 963  
 964      // Try to see if a corresponding blog comment exists.
 965      $post_comment_id = bp_activity_get_meta( $activity->id, "bp_blogs_{$post_type}_comment_id" );
 966  
 967      if ( empty( $post_comment_id ) ) {
 968          return;
 969      }
 970  
 971      // Handle multisite.
 972      switch_to_blog( $parent_activity->item_id );
 973  
 974      // Get the comment status
 975      $post_comment_status = wp_get_comment_status( $post_comment_id );
 976      $old_comment_status  = $post_comment_status;
 977  
 978      // No need to edit the activity, as it's the activity who's updating the comment
 979      remove_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10 );
 980      remove_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10 );
 981  
 982      if ( 1 === $activity->is_spam && 'spam' !== $post_comment_status ) {
 983          wp_spam_comment( $post_comment_id );
 984      } elseif ( ! $activity->is_spam ) {
 985          if ( 'spam' === $post_comment_status  ) {
 986              wp_unspam_comment( $post_comment_id );
 987          } elseif ( 'trash' === $post_comment_status ) {
 988              wp_untrash_comment( $post_comment_id );
 989          } else {
 990              // Update the blog post comment.
 991              wp_update_comment( array(
 992                  'comment_ID'       => $post_comment_id,
 993                  'comment_content'  => $activity->content,
 994              ) );
 995          }
 996      }
 997  
 998      // Restore actions
 999      add_action( 'transition_comment_status',     'bp_activity_transition_post_type_comment_status', 10, 3 );
1000      add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment',          10, 4 );
1001  
1002      restore_current_blog();
1003  }
1004  add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
1005  
1006  /**
1007   * When a post is trashed, remove each comment's associated activity meta.
1008   *
1009   * When a post is trashed and later untrashed, we currently don't reinstate
1010   * activity items for these comments since their activity entries are already
1011   * deleted when initially trashed.
1012   *
1013   * Since these activity entries are deleted, we need to remove the deleted
1014   * activity comment IDs from each comment's meta when a post is trashed.
1015   *
1016   * @since 2.0.0
1017   *
1018   * @param int   $post_id  The post ID.
1019   * @param array $comments Array of comment statuses. The key is comment ID, the
1020   *                        value is the $comment->comment_approved value.
1021   */
1022  function bp_blogs_remove_activity_meta_for_trashed_comments( $post_id = 0, $comments = array() ) {
1023      if ( ! empty( $comments ) ) {
1024          foreach ( array_keys( $comments ) as $comment_id ) {
1025              delete_comment_meta( $comment_id, 'bp_activity_comment_id' );
1026          }
1027      }
1028  }
1029  add_action( 'trashed_post_comments', 'bp_blogs_remove_activity_meta_for_trashed_comments', 10, 2 );
1030  
1031  /**
1032   * Filter 'new_blog_comment' bp_has_activities() loop to include new- and old-style blog activity comment items.
1033   *
1034   * In BuddyPress 2.0, the schema for storing activity items related to blog
1035   * posts changed. Instead creating new top-level 'new_blog_comment' activity
1036   * items, blog comments are recorded in the activity stream as comments on the
1037   * 'new_blog_post' activity items corresponding to the parent post. This filter
1038   * ensures that the 'new_blog_comment' filter in bp_has_activities() (which
1039   * powers the 'Comments' filter in the activity directory dropdown) includes
1040   * both old-style and new-style activity comments.
1041   *
1042   * @since 2.1.0
1043   * @since 2.5.0 Used for any synced Post type comments, in wp-admin or front-end contexts.
1044   *
1045   * @param array $args Arguments passed from bp_parse_args() in bp_has_activities().
1046   * @return array $args
1047   */
1048  function bp_blogs_new_blog_comment_query_backpat( $args ) {
1049      global $wpdb;
1050      $bp = buddypress();
1051  
1052      // If activity comments are disabled for blog posts, stop now!
1053      if ( bp_disable_blogforum_comments() ) {
1054          return $args;
1055      }
1056  
1057      // Get the associated post type
1058      $post_type = bp_activity_post_type_get_tracking_arg( $args['action'], 'post_type' );
1059  
1060      // Bail if this is not an activity associated with a post type
1061      if ( empty( $post_type ) ) {
1062          return $args;
1063      }
1064  
1065      // Bail if this is an activity about posts and not comments
1066      if ( bp_activity_post_type_get_tracking_arg( $args['action'], 'comment_action_id' ) ) {
1067          return $args;
1068      }
1069  
1070      // Comment synced ?
1071      $activity_ids = $wpdb->get_col( $wpdb->prepare( "SELECT activity_id FROM {$bp->activity->table_name_meta} WHERE meta_key = %s", "bp_blogs_{$post_type}_comment_id" ) );
1072  
1073      if ( empty( $activity_ids ) ) {
1074          return $args;
1075      }
1076  
1077      // Init the filter query.
1078      $filter_query = array();
1079  
1080      if ( ! isset( $args['scope'] ) || 'null' === $args['scope'] ) {
1081          $args['scope'] = '';
1082      } elseif ( 'just-me' === $args['scope'] ) {
1083          $filter_query = array(
1084              'relation' => 'AND',
1085              array(
1086                  'column' => 'user_id',
1087                  'value'  => bp_displayed_user_id(),
1088              ),
1089          );
1090          $args['scope'] = '';
1091      }
1092  
1093      $filter_query[] = array(
1094          'relation' => 'OR',
1095          array(
1096              'column' => 'type',
1097              'value'  => $args['action'],
1098          ),
1099          array(
1100              'column'  => 'id',
1101              'value'   =>  $activity_ids,
1102              'compare' => 'IN'
1103          ),
1104      );
1105  
1106      $args['filter_query'] = $filter_query;
1107  
1108      // Make sure to have comment in stream mode && avoid duplicate content.
1109      $args['display_comments'] = 'stream';
1110  
1111      // Finally reset the action.
1112      $args['action'] = '';
1113      $args['type']   = '';
1114  
1115      // Return the original arguments.
1116      return $args;
1117  }
1118  add_filter( 'bp_after_has_activities_parse_args',                'bp_blogs_new_blog_comment_query_backpat' );
1119  add_filter( 'bp_activity_list_table_filter_activity_type_items', 'bp_blogs_new_blog_comment_query_backpat' );
1120  
1121  /**
1122   * Utility function to set up some variables for use in the activity loop.
1123   *
1124   * Grabs the blog's comment depth and the post's open comment status options
1125   * for later use in the activity and activity comment loops.
1126   *
1127   * This is to prevent having to requery these items later on.
1128   *
1129   * @since 2.0.0
1130   *
1131   * @see bp_blogs_disable_activity_commenting()
1132   * @see bp_blogs_setup_comment_loop_globals_on_ajax()
1133   *
1134   * @param object $activity The BP_Activity_Activity object.
1135   */
1136  function bp_blogs_setup_activity_loop_globals( $activity ) {
1137      if ( ! is_object( $activity ) ) {
1138          return;
1139      }
1140  
1141      // The activity type does not support comments or replies ? stop now!
1142      if ( ! bp_activity_type_supports( $activity->type, 'post-type-comment-reply' ) ) {
1143          return;
1144      }
1145  
1146      if ( empty( $activity->id ) ) {
1147          return;
1148      }
1149  
1150      // If we've already done this before, stop now!
1151      if ( isset( buddypress()->blogs->allow_comments[ $activity->id ] ) ) {
1152          return;
1153      }
1154  
1155      $allow_comments = bp_blogs_comments_open( $activity );
1156      $thread_depth   = bp_blogs_get_blogmeta( $activity->item_id, 'thread_comments_depth' );
1157      $moderation     = bp_blogs_get_blogmeta( $activity->item_id, 'comment_moderation' );
1158  
1159      // Initialize a local object so we won't have to query this again in the
1160      // comment loop.
1161      if ( ! isset( buddypress()->blogs->allow_comments ) ) {
1162          buddypress()->blogs->allow_comments = array();
1163      }
1164      if ( ! isset( buddypress()->blogs->thread_depth ) ) {
1165          buddypress()->blogs->thread_depth   = array();
1166      }
1167      if ( ! isset( buddypress()->blogs->comment_moderation ) ) {
1168          buddypress()->blogs->comment_moderation = array();
1169      }
1170  
1171      /*
1172       * Cache comment settings in the buddypress() singleton for later reference.
1173       *
1174       * See bp_blogs_disable_activity_commenting() / bp_blogs_can_comment_reply().
1175       *
1176       * thread_depth is keyed by activity ID instead of blog ID because when we're
1177       * in an actvity comment loop, we don't have access to the blog ID...
1178       *
1179       * Should probably object cache these values instead...
1180       */
1181      buddypress()->blogs->allow_comments[ $activity->id ]     = $allow_comments;
1182      buddypress()->blogs->thread_depth[ $activity->id ]       = $thread_depth;
1183      buddypress()->blogs->comment_moderation[ $activity->id ] = $moderation;
1184  }
1185  
1186  /**
1187   * Set up some globals used in the activity comment loop when AJAX is used.
1188   *
1189   * @since 2.0.0
1190   *
1191   * @see bp_blogs_setup_activity_loop_globals()
1192   */
1193  function bp_blogs_setup_comment_loop_globals_on_ajax() {
1194      // Not AJAX? stop now!
1195      if ( ! defined( 'DOING_AJAX' ) ) {
1196          return;
1197      }
1198      if ( false === (bool) constant( 'DOING_AJAX' ) ) {
1199          return;
1200      }
1201  
1202      // Get the parent activity item.
1203      $comment         = bp_activity_current_comment();
1204      $parent_activity = new BP_Activity_Activity( $comment->item_id );
1205  
1206      // Setup the globals.
1207      bp_blogs_setup_activity_loop_globals( $parent_activity );
1208  }
1209  add_action( 'bp_before_activity_comment', 'bp_blogs_setup_comment_loop_globals_on_ajax' );
1210  
1211  /**
1212   * Disable activity commenting for blog posts based on certain criteria.
1213   *
1214   * If activity commenting is enabled for blog posts, we still need to disable
1215   * commenting if:
1216   *  - comments are disabled for the WP blog post from the admin dashboard
1217   *  - the WP blog post is supposed to be automatically closed from comments
1218   *    based on a certain age
1219   *  - the activity entry is a 'new_blog_comment' type
1220   *
1221   * @since 2.0.0
1222   *
1223   * @param bool $retval Is activity commenting enabled for this activity entry.
1224   * @return bool
1225   */
1226  function bp_blogs_disable_activity_commenting( $retval ) {
1227      global $activities_template;
1228  
1229      // If activity commenting is disabled, return current value.
1230      if ( bp_disable_blogforum_comments() || ! isset( $activities_template->in_the_loop ) ) {
1231          return $retval;
1232      }
1233  
1234      $type = bp_get_activity_type();
1235  
1236      // It's a post type supporting comment tracking.
1237      if ( bp_activity_type_supports( $type, 'post-type-comment-tracking' ) ) {
1238          // The activity type is supporting comments or replies
1239          if ( bp_activity_type_supports( $type, 'post-type-comment-reply' ) ) {
1240              // Setup some globals we'll need to reference later.
1241              bp_blogs_setup_activity_loop_globals( $activities_template->activity );
1242  
1243              // If comments are closed for the WP blog post, we should disable
1244              // activity comments for this activity entry.
1245              if ( ! isset( buddypress()->blogs->allow_comments[ bp_get_activity_id() ] ) || ! buddypress()->blogs->allow_comments[ bp_get_activity_id() ] ) {
1246                  $retval = false;
1247              }
1248  
1249              // If comments need moderation, disable activity commenting.
1250              if ( isset( buddypress()->blogs->comment_moderation[ bp_get_activity_id() ] ) && buddypress()->blogs->comment_moderation[ bp_get_activity_id() ] ) {
1251                  $retval = false;
1252              }
1253          // The activity type does not support comments or replies
1254          } else {
1255              $retval = false;
1256          }
1257      }
1258  
1259      return $retval;
1260  }
1261  add_filter( 'bp_activity_can_comment', 'bp_blogs_disable_activity_commenting' );
1262  
1263  /**
1264   * Limit the display of post type synced comments.
1265   *
1266   * @since  2.5.0
1267   *
1268   * When viewing the synced comments in stream mode, this prevents comments to
1269   * be displayed twice, and avoids a Javascript error as the form to add replies
1270   * is not available.
1271   *
1272   * @param  int $retval  The comment count for the activity.
1273   * @return int          The comment count, or 0 to hide activity comment replies.
1274   */
1275  function bp_blogs_post_type_comments_avoid_duplicates( $retval ) {
1276      /**
1277       * Only limit the display when Post type comments are synced with
1278       * activity comments.
1279       */
1280      if ( bp_disable_blogforum_comments() ) {
1281          return $retval;
1282      }
1283  
1284      if ( 'activity_comment' !== bp_get_activity_type() ) {
1285          return $retval;
1286      }
1287  
1288      // Check the parent activity
1289      $parent_activity = new BP_Activity_Activity( bp_get_activity_item_id() );
1290  
1291      if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) {
1292          $retval = 0;
1293      }
1294  
1295      return $retval;
1296  }
1297  add_filter( 'bp_activity_get_comment_count', 'bp_blogs_post_type_comments_avoid_duplicates' );
1298  
1299  /**
1300   * Check if an activity comment associated with a blog post can be replied to.
1301   *
1302   * By default, disables replying to activity comments if the corresponding WP
1303   * blog post no longer accepts comments.
1304   *
1305   * This check uses a locally-cached value set in {@link bp_blogs_disable_activity_commenting()}
1306   * via {@link bp_blogs_setup_activity_loop_globals()}.
1307   *
1308   * @since 2.0.0
1309   *
1310   * @param bool         $retval  Are replies allowed for this activity reply.
1311   * @param object|array $comment The activity comment object.
1312   *
1313   * @return bool
1314   */
1315  function bp_blogs_can_comment_reply( $retval, $comment ) {
1316      if ( is_array( $comment ) ) {
1317          $comment = (object) $comment;
1318      }
1319  
1320      // Check comment depth and disable if depth is too large.
1321      if ( isset( buddypress()->blogs->thread_depth[$comment->item_id] ) ){
1322          if ( bp_activity_get_comment_depth( $comment ) >= buddypress()->blogs->thread_depth[$comment->item_id] ) {
1323              $retval = false;
1324          }
1325      }
1326  
1327      // Check if we should disable activity replies based on the parent activity.
1328      if ( isset( buddypress()->blogs->allow_comments[$comment->item_id] ) ){
1329          // The blog post has closed off commenting, so we should disable all activity
1330          // comments under the parent 'new_blog_post' activity entry.
1331          if ( ! buddypress()->blogs->allow_comments[ $comment->item_id ] ) {
1332              $retval = false;
1333          }
1334      }
1335  
1336      // If comments need moderation, disable activity commenting.
1337      if ( isset( buddypress()->blogs->comment_moderation[ $comment->item_id ] ) && buddypress()->blogs->comment_moderation[ $comment->item_id ] ) {
1338          $retval = false;
1339      }
1340  
1341      return $retval;
1342  }
1343  add_filter( 'bp_activity_can_comment_reply', 'bp_blogs_can_comment_reply', 10, 2 );
1344  
1345  /**
1346   * Changes activity comment permalinks to use the blog comment permalink
1347   * instead of the activity permalink.
1348   *
1349   * This is only done if activity commenting is allowed and whether the parent
1350   * activity item is a 'new_blog_post' entry.
1351   *
1352   * @since 2.0.0
1353   *
1354   * @param string $retval The activity comment permalink.
1355   * @return string
1356   */
1357  function bp_blogs_activity_comment_permalink( $retval = '' ) {
1358      global $activities_template;
1359  
1360      // Get the current comment ID.
1361      $item_id = isset( $activities_template->activity->current_comment->item_id )
1362          ? $activities_template->activity->current_comment->item_id
1363          : false;
1364  
1365      // Maybe adjust the link if item ID exists.
1366      if ( ( false !== $item_id ) && isset( buddypress()->blogs->allow_comments[ $item_id ] ) ) {
1367          $retval = $activities_template->activity->current_comment->primary_link;
1368      }
1369  
1370      return $retval;
1371  }
1372  add_filter( 'bp_get_activity_comment_permalink', 'bp_blogs_activity_comment_permalink' );
1373  
1374  /**
1375   * Changes single activity comment entries to use the blog comment permalink.
1376   *
1377   * This is only done if the activity comment is associated with a blog comment.
1378   *
1379   * @since 2.0.1
1380   *
1381   * @param string               $retval   The activity permalink.
1382   * @param BP_Activity_Activity $activity Activity object.
1383   * @return string
1384   */
1385  function bp_blogs_activity_comment_single_permalink( $retval, $activity ) {
1386      if ( 'activity_comment' !== $activity->type ) {
1387          return $retval;
1388      }
1389  
1390      if ( bp_disable_blogforum_comments() ) {
1391          return $retval;
1392      }
1393  
1394      $parent_activity = new BP_Activity_Activity( $activity->item_id );
1395  
1396      if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) {
1397          $retval = $activity->primary_link;
1398      }
1399  
1400      return $retval;
1401  }
1402  add_filter( 'bp_activity_get_permalink', 'bp_blogs_activity_comment_single_permalink', 10, 2 );
1403  
1404  /**
1405   * Formats single activity comment entries to use the blog comment action.
1406   *
1407   * This is only done if the activity comment is associated with a blog comment.
1408   *
1409   * @since 2.0.1
1410   *
1411   * @param string               $retval   The activity action.
1412   * @param BP_Activity_Activity $activity Activity object.
1413   * @return string
1414   */
1415  function bp_blogs_activity_comment_single_action( $retval, $activity ) {
1416      if ( 'activity_comment' !== $activity->type ) {
1417          return $retval;
1418      }
1419  
1420      if ( bp_disable_blogforum_comments() ) {
1421          return $retval;
1422      }
1423  
1424      $parent_activity = new BP_Activity_Activity( $activity->item_id );
1425  
1426      if ( ! isset( $parent_activity->type ) ) {
1427          return $retval;
1428      }
1429  
1430      $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' );
1431  
1432      if ( ! $post_type ) {
1433          return $retval;
1434      }
1435  
1436      $blog_comment_id = bp_activity_get_meta( $activity->id, "bp_blogs_{$post_type}_comment_id" );
1437  
1438      if ( ! empty( $blog_comment_id ) ) {
1439          $bp = buddypress();
1440  
1441          // Check if a comment action id is set for the parent activity
1442          $comment_action_id = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'comment_action_id' );
1443  
1444          // Use the action string callback for the activity type
1445          if ( ! empty( $comment_action_id ) ) {
1446              // Fake a 'new_{post_type}_comment' by cloning the activity object.
1447              $object = clone $activity;
1448  
1449              // Set the type of the activity to be a comment about a post type
1450              $object->type = $comment_action_id;
1451  
1452              // Use the blog ID as the item_id.
1453              $object->item_id = $parent_activity->item_id;
1454  
1455              // Use comment ID as the secondary_item_id.
1456              $object->secondary_item_id = $blog_comment_id;
1457  
1458              // Get the format callback for this activity comment
1459              $format_callback = bp_activity_post_type_get_tracking_arg( $comment_action_id, 'format_callback' );
1460  
1461              // now format the activity action using the 'new_{post_type}_comment' action callback
1462              if ( is_callable( $format_callback ) ) {
1463                  $retval = call_user_func_array( $format_callback, array( '', $object ) );
1464              }
1465          }
1466      }
1467  
1468      return $retval;
1469  }
1470  add_filter( 'bp_get_activity_action_pre_meta', 'bp_blogs_activity_comment_single_action', 10, 2 );


Generated: Tue Nov 19 01:01:36 2019 Cross-referenced by PHPXref 0.7.1