bp_activity_post_type_comment( int $comment_id, bool $is_approved = true, object|null $activity_post_object = null )

Create an activity item for a newly posted post type comment.

Description

Parameters

$comment_id

(Required) ID of the comment.

$is_approved

(Optional) Whether the comment is approved or not.

Default value: true

$activity_post_object

(Optional) The post type tracking args object.

Default value: null

Return

(null|WP_Error|bool|int) The ID of the activity on success. False on error.

Source

File: bp-activity/bp-activity-functions.php

2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
function bp_activity_post_type_comment( $comment_id = 0, $is_approved = true, $activity_post_object = null ) {
    // Get the users comment
    $post_type_comment = get_comment( $comment_id );
 
    // Don't record activity if the comment hasn't been approved
    if ( empty( $is_approved ) ) {
        return false;
    }
 
    // Don't record activity if no email address has been included
    if ( empty( $post_type_comment->comment_author_email ) ) {
        return false;
    }
 
    // Don't record activity if the comment has already been marked as spam
    if ( 'spam' === $is_approved ) {
        return false;
    }
 
    // Get the user by the comment author email.
    $user = get_user_by( 'email', $post_type_comment->comment_author_email );
 
    // If user isn't registered, don't record activity
    if ( empty( $user ) ) {
        return false;
    }
 
    // Get the user_id
    $user_id = (int) $user->ID;
 
    // Get blog and post data
    $blog_id = get_current_blog_id();
 
    // Get the post
    $post_type_comment->post = get_post( $post_type_comment->comment_post_ID );
 
    if ( ! is_a( $post_type_comment->post, 'WP_Post' ) ) {
        return false;
    }
 
    /**
     * Filters whether to publish activities about the comment regarding the post status
     *
     * @since BuddyPress 2.5.0
     *
     * @param bool true to bail, false otherwise.
     */
    $is_post_status_not_allowed = (bool) apply_filters( 'bp_activity_post_type_is_post_status_allowed', 'publish' !== $post_type_comment->post->post_status || ! empty( $post_type_comment->post->post_password ) );
 
    // If this is a password protected post, or not a public post don't record the comment
    if ( $is_post_status_not_allowed ) {
        return false;
    }
 
    // Set post type
    $post_type = $post_type_comment->post->post_type;
 
    if ( empty( $activity_post_object ) ) {
        // Get the post type tracking args.
        $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
 
        // Bail if the activity type does not exist
        if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
            return false;
        }
    }
 
    // Set the $activity_comment_object
    $activity_comment_object = $activity_post_object->comments_tracking;
 
    /**
     * Filters whether or not to post the activity about the comment.
     *
     * This is a variable filter, dependent on the post type,
     * that lets components or plugins bail early if needed.
     *
     * @since BuddyPress 2.5.0
     *
     * @param bool $value      Whether or not to continue.
     * @param int  $blog_id    ID of the current site.
     * @param int  $post_id    ID of the current post being commented.
     * @param int  $user_id    ID of the current user.
     * @param int  $comment_id ID of the current comment being posted.
     */
    if ( false === apply_filters( "bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id ) ) {
        return false;
    }
 
    // Is this an update ?
    $activity_id = bp_activity_get_activity_id( array(
        'user_id'           => $user_id,
        'component'         => $activity_comment_object->component_id,
        'type'              => $activity_comment_object->action_id,
        'item_id'           => $blog_id,
        'secondary_item_id' => $comment_id,
    ) );
 
    // Record this in activity feeds.
    $comment_link = get_comment_link( $post_type_comment->comment_ID );
 
    // Backward compatibility filters for the 'blogs' component.
    if ( 'blogs' == $activity_comment_object->component_id )  {
        $activity_content      = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content',      array( $post_type_comment->comment_content, &$post_type_comment, $comment_link ) );
        $activity_primary_link = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$post_type_comment ) );
    } else {
        $activity_content      = $post_type_comment->comment_content;
        $activity_primary_link = $comment_link;
    }
 
    $activity_args = array(
        'id'            => $activity_id,
        'user_id'       => $user_id,
        'content'       => $activity_content,
        'primary_link'  => $activity_primary_link,
        'component'     => $activity_comment_object->component_id,
        'recorded_time' => $post_type_comment->comment_date_gmt,
    );
 
    if ( bp_disable_blogforum_comments() ) {
        $blog_url = get_home_url( $blog_id );
        $post_url = add_query_arg(
            'p',
            $post_type_comment->post->ID,
            trailingslashit( $blog_url )
        );
 
        $activity_args['type']              = $activity_comment_object->action_id;
        $activity_args['item_id']           = $blog_id;
        $activity_args['secondary_item_id'] = $post_type_comment->comment_ID;
 
        if ( ! empty( $activity_args['content'] ) ) {
            // Create the excerpt.
            $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
 
            // Backward compatibility filter for blog comments.
            if ( 'blogs' == $activity_post_object->component_id )  {
                $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type );
            } else {
                $activity_args['content'] = $activity_summary;
            }
        }
 
        // Set up the action by using the format functions.
        $action_args = array_merge( $activity_args, array(
            'post_title' => $post_type_comment->post->post_title,
            'post_url'   => $post_url,
            'blog_url'   => $blog_url,
            'blog_name'  => get_blog_option( $blog_id, 'blogname' ),
        ) );
 
        $activity_args['action'] = call_user_func_array( $activity_comment_object->format_callback, array( '', (object) $action_args ) );
 
        // Make sure the action is set.
        if ( empty( $activity_args['action'] ) ) {
            return;
        } else {
            // Backward compatibility filter for the blogs component.
            if ( 'blogs' === $activity_post_object->component_id )  {
                $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
            }
        }
 
        $activity_id = bp_activity_add( $activity_args );
    }
 
    /**
     * Fires after the publishing of an activity item for a newly published post type post.
     *
     * @since BuddyPress 2.5.0
     *
     * @param int        $activity_id          ID of the newly published activity item.
     * @param WP_Comment $post_type_comment    Comment object.
     * @param array      $activity_args        Array of activity arguments.
     * @param object     $activity_post_object the post type tracking args object.
     */
    do_action_ref_array( 'bp_activity_post_type_comment', array( &$activity_id, $post_type_comment, $activity_args, $activity_post_object ) );
 
    return $activity_id;
}

Changelog

Changelog
Version Description
BuddyPress 2.5.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.