Skip to:
Content

BuddyPress.org

Changeset 10544


Ignore:
Timestamp:
02/07/2016 04:47:35 PM (9 years ago)
Author:
imath
Message:

Post Type Activities: generalize the Blogs component "post" comment synchronization feature to any post type.

As soon as the Blogs component is active and activity comments about Post Type activities are not disallowed, a new comment added to a post type will generate an activity comment about the corresponding Post Type Activity and vice versa.

Deprecate the Blogs component functions previously used to track the "post" post type in favor of the Activity component functions introduced in r10543.

Only load the Blogs component activity functions if the Activity component is active.

Make sure a synchronized activity comment marked as spam is also marking as spam the corresponding post type comment.

NB: some unit tests will temporarly fail after this commit. Next commit will fix the failing tests.

Props shanebp, r-a-y.

See #6482
Fixes #6793

Location:
trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-actions.php

    r10543 r10544  
    952952    remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
    953953}
     954add_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 );
  • trunk/src/bp-activity/bp-activity-functions.php

    r10543 r10544  
    24832483    return $activity_id;
    24842484}
     2485add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
     2486add_action( 'edit_comment', 'bp_activity_post_type_comment', 10    );
    24852487
    24862488/**
     
    25482550    return $deleted;
    25492551}
     2552add_action( 'delete_comment', 'bp_activity_post_type_remove_comment', 10, 1 );
    25502553
    25512554/**
  • trunk/src/bp-blogs/bp-blogs-activity.php

    r10417 r10544  
    1919 */
    2020function bp_blogs_register_activity_actions() {
    21     $bp = buddypress();
    22 
    23     // Bail if activity is not active.
    24     if ( ! bp_is_active( 'activity' ) ) {
    25         return false;
    26     }
    27 
    2821    if ( is_multisite() ) {
    2922        bp_activity_set_action(
    30             $bp->blogs->id,
     23            buddypress()->blogs->id,
    3124            'new_blog',
    3225            __( 'New site created', 'buddypress' ),
     
    3831    }
    3932
    40     // Only add the comment type if the 'post' post type is trackable.
    41     if ( post_type_supports( 'post', 'buddypress-activity' ) ) {
    42         bp_activity_set_action(
    43             $bp->blogs->id,
    44             'new_blog_comment',
    45             __( 'New post comment posted', 'buddypress' ),
    46             'bp_blogs_format_activity_action_new_blog_comment',
    47             __( 'Comments', 'buddypress' ),
    48             array( 'activity', 'member' ),
    49             10
    50         );
    51     }
    52 
    5333    /**
    5434     * Fires after the registry of the default blog component activity actions.
     
    5939}
    6040add_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
     52 */
     53function 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}
     115add_filter( 'bp_activity_get_post_type_tracking_args', 'bp_blogs_register_post_tracking_args', 10, 2 );
    61116
    62117/**
     
    219274 */
    220275function bp_blogs_format_activity_action_new_blog_comment( $action, $activity ) {
    221     $blog_url  = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
    222     $blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
     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    }
    223305
    224306    if ( empty( $blog_url ) || empty( $blog_name ) ) {
     
    230312    }
    231313
    232     $post_url   = bp_activity_get_meta( $activity->id, 'post_url' );
    233     $post_title = bp_activity_get_meta( $activity->id, 'post_title' );
     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    }
    234339
    235340    // Should only be empty at the time of post creation.
     
    334439 */
    335440function bp_blogs_record_activity( $args = '' ) {
    336 
    337     // Bail if activity is not active.
    338     if ( ! bp_is_active( 'activity' ) ) {
    339         return false;
    340     }
    341 
    342     $bp = buddypress();
    343 
    344441    $defaults = array(
    345442        'user_id'           => bp_loggedin_user_id(),
     
    347444        'content'           => '',
    348445        'primary_link'      => '',
    349         'component'         => $bp->blogs->id,
     446        'component'         => buddypress()->blogs->id,
    350447        'type'              => false,
    351448        'item_id'           => false,
     
    411508 */
    412509function bp_blogs_delete_activity( $args = '' ) {
    413 
    414     // Bail if activity is not active.
    415     if ( ! bp_is_active( 'activity' ) ) {
    416         return false;
    417     }
    418 
    419510    $r = bp_parse_args( $args, array(
    420511        'item_id'           => false,
     
    523614 * Note: This is only a one-way sync - activity comments -> blog comment.
    524615 *
    525  * For blog post -> activity comment, see {@link bp_blogs_record_comment()}.
     616 * For blog post -> activity comment, see {@link bp_activity_post_type_comment()}.
    526617 *
    527618 * @since 2.0.0
     619 * @since 2.5.0 Allow custom post types to sync their comments with activity ones
    528620 *
    529621 * @param int    $comment_id      The activity ID for the posted activity comment.
     
    532624 */
    533625function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_activity ) {
    534     // If parent activity isn't a blog post, stop now!
    535     if ( $parent_activity->type != 'new_blog_post' ) {
     626    // if parent activity isn't a post type having the buddypress-activity support, stop now!
     627    if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
    536628        return;
    537629    }
     
    549641    }
    550642
     643    // Get associated post type and set default comment parent
     644    $post_type      = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' );
     645    $comment_parent = 0;
     646
    551647    // See if a parent WP comment ID exists.
    552     if ( ! empty( $params['parent_id'] ) ) {
    553         $comment_parent = bp_activity_get_meta( $params['parent_id'], 'bp_blogs_post_comment_id' );
    554     } else {
    555         $comment_parent = 0;
     648    if ( ! empty( $params['parent_id'] ) && ! empty( $post_type ) ) {
     649        $comment_parent = bp_activity_get_meta( $params['parent_id'], "bp_blogs_{$post_type}_comment_id" );
    556650    }
    557651
     
    566660        'comment_parent'       => (int) $comment_parent,
    567661        'user_id'              => $params['user_id'],
    568 
    569         // Commenting these out for now
    570         // 'comment_author_IP'    => '127.0.0.1',
    571         // 'comment_agent'        => '', .
    572662        'comment_approved'     => 1
    573663    );
    574664
    575665    // Prevent separate activity entry being made.
    576     remove_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     666    remove_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
    577667
    578668    // Handle multisite.
     
    590680
    591681    // Add meta to activity comment.
    592     bp_activity_update_meta( $comment_id, 'bp_blogs_post_comment_id', $post_comment_id );
     682    if ( ! empty( $post_type ) ) {
     683        bp_activity_update_meta( $comment_id, "bp_blogs_{$post_type}_comment_id", $post_comment_id );
     684    }
    593685
    594686    // Resave activity comment with WP comment permalink.
     
    617709
    618710    // Add the comment hook back.
    619     add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     711    add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
    620712
    621713    /**
     
    641733 *
    642734 * @since 2.0.0
     735 * @since 2.5.0 Add the $delected parameter
    643736 *
    644737 * @param bool $retval             Whether BuddyPress should continue or not.
    645738 * @param int  $parent_activity_id The parent activity ID for the activity comment.
    646739 * @param int  $activity_id        The activity ID for the pending deleted activity comment.
     740 * @param bool $deleted            Whether the comment was deleted or not.
    647741 * @return bool
    648742 */
    649 function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id ) {
     743function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id, &$deleted ) {
    650744    // Check if parent activity is a blog post.
    651745    $parent_activity = new BP_Activity_Activity( $parent_activity_id );
    652     if ( 'new_blog_post' != $parent_activity->type ) {
     746
     747    // if parent activity isn't a post type having the buddypress-activity support, stop now!
     748    if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
    653749        return $retval;
    654750    }
     
    658754        'in'               => $activity_id,
    659755        'display_comments' => 'stream',
     756        'spam'             => 'all',
    660757    ) );
    661758
     
    678775    BP_Activity_Activity::rebuild_activity_comment_tree( $parent_activity_id );
    679776
     777    // Avoid the error message although the comments were successfully deleted
     778    $deleted = true;
     779
    680780    // We're overriding the default bp_activity_delete_comment() functionality
    681781    // so we need to return false.
    682782    return false;
    683783}
    684 add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activity_comment', 10, 3 );
     784add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activity_comment', 10, 4 );
    685785
    686786/**
     
    692792 */
    693793function bp_blogs_sync_activity_edit_to_post_comment( BP_Activity_Activity $activity ) {
    694     // Not an activity comment? stop now!
    695     if ( 'activity_comment' !== $activity->type ) {
    696         return;
    697     }
    698 
    699794    // This is a new entry, so stop!
    700795    // We only want edits!
    701     if ( empty( $activity->id ) ) {
     796    if ( empty( $activity->id ) || bp_disable_blogforum_comments() ) {
    702797        return;
    703798    }
    704799
    705     // Prevent recursion.
    706     remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     800    // fetch parent activity item
     801    $parent_activity = new BP_Activity_Activity( $activity->item_id );
     802
     803    // if parent activity isn't a post type having the buddypress-activity support for comments, stop now!
     804    if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
     805        return;
     806    }
     807
     808    $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' );
     809
     810    // No associated post type for this activity comment, stop.
     811    if ( ! $post_type ) {
     812        return;
     813    }
    707814
    708815    // Try to see if a corresponding blog comment exists.
    709     $post_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
     816    $post_comment_id = bp_activity_get_meta( $activity->id, "bp_blogs_{$post_type}_comment_id" );
    710817
    711818    if ( empty( $post_comment_id ) ) {
     
    713820    }
    714821
    715     // Fetch parent activity item.
    716     $parent_activity = new BP_Activity_Activity( $activity->item_id );
    717 
    718     // Sanity check.
    719     if ( 'new_blog_post' !== $parent_activity->type ) {
    720         return;
    721     }
    722 
    723822    // Handle multisite.
    724823    switch_to_blog( $parent_activity->item_id );
    725824
    726     // Update the blog post comment.
    727     wp_update_comment( array(
    728         'comment_ID'      => $post_comment_id,
    729         'comment_content' => $activity->content
    730     ) );
     825    // Get the comment status
     826    $post_comment_status = wp_get_comment_status( $post_comment_id );
     827    $old_comment_status  = $post_comment_status;
     828
     829    // No need to edit the activity, as it's the activity who's updating the comment
     830    remove_action( 'transition_comment_status',     'bp_activity_transition_post_type_comment_status', 10, 3 );
     831    remove_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment',          10, 4 );
     832
     833    if ( 1 === (int) $activity->is_spam && 'spam' !== $post_comment_status ) {
     834        wp_spam_comment( $post_comment_id );
     835    } elseif ( ! $activity->is_spam ) {
     836        if ( 'spam' === $post_comment_status  ) {
     837            wp_unspam_comment( $post_comment_id );
     838        } elseif ( 'trash' === $post_comment_status ) {
     839            wp_untrash_comment( $post_comment_id );
     840        } else {
     841            // Update the blog post comment.
     842            wp_update_comment( array(
     843                'comment_ID'       => $post_comment_id,
     844                'comment_content'  => $activity->content,
     845            ) );
     846        }
     847    }
     848
     849    // Restore actions
     850    add_action( 'transition_comment_status',     'bp_activity_transition_post_type_comment_status', 10, 3 );
     851    add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment',          10, 4 );
    731852
    732853    restore_current_blog();
     
    770891 * both old-style and new-style activity comments.
    771892 *
    772  * This implementation involves filtering the activity queries directly, and
    773  * should be considered a stopgap. The proper solution would involve enabling
    774  * multiple query condition clauses, connected by an OR, in the bp_has_activities()
    775  * API.
    776  *
    777893 * @since 2.1.0
     894 * @since 2.5.0 Used for any synced Post type comments, in wp-admin or front-end contexts.
    778895 *
    779896 * @param array $args Arguments passed from bp_parse_args() in bp_has_activities().
     
    784901    $bp = buddypress();
    785902
    786     // Bail if this is not a 'new_blog_comment' query.
    787     if ( 'new_blog_comment' !== $args['action'] ) {
     903    // If activity comments are disabled for blog posts, stop now!
     904    if ( bp_disable_blogforum_comments() ) {
    788905        return $args;
    789906    }
    790907
     908    // Get the associated post type
     909    $post_type = bp_activity_post_type_get_tracking_arg( $args['action'], 'post_type' );
     910
     911    // Bail if this is not an activity associated with a post type
     912    if ( empty( $post_type ) ) {
     913        return $args;
     914    }
     915
     916    // Bail if this is an activity about posts and not comments
     917    if ( bp_activity_post_type_get_tracking_arg( $args['action'], 'comment_action_id' ) ) {
     918        return $args;
     919    }
     920
    791921    // Comment synced ?
    792     $activity_ids = $wpdb->get_col( $wpdb->prepare( "SELECT activity_id FROM {$bp->activity->table_name_meta} WHERE meta_key = %s", 'bp_blogs_post_comment_id' ) );
     922    $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" ) );
    793923
    794924    if ( empty( $activity_ids ) ) {
     
    799929    $filter_query = array();
    800930
    801     if ( 'null' === $args['scope'] ) {
     931    if ( ! isset( $args['scope'] ) || 'null' === $args['scope'] ) {
    802932        $args['scope'] = '';
    803933    } elseif ( 'just-me' === $args['scope'] ) {
     
    832962    // Finally reset the action.
    833963    $args['action'] = '';
     964    $args['type']   = '';
    834965
    835966    // Return the original arguments.
    836967    return $args;
    837968}
    838 add_filter( 'bp_after_has_activities_parse_args', 'bp_blogs_new_blog_comment_query_backpat' );
     969add_filter( 'bp_after_has_activities_parse_args',                'bp_blogs_new_blog_comment_query_backpat' );
     970add_filter( 'bp_activity_list_table_filter_activity_type_items', 'bp_blogs_new_blog_comment_query_backpat' );
    839971
    840972/**
     
    858990    }
    859991
    860     // Parent not a blog post? stop now!
    861     if ( 'new_blog_post' !== $activity->type ) {
     992    // The activity type does not support comments or replies ? stop now!
     993    if ( ! bp_activity_type_supports( $activity->type, 'post-type-comment-reply' ) ) {
    862994        return;
    863995    }
     
    9361068 */
    9371069function bp_blogs_disable_activity_commenting( $retval ) {
     1070    global $activities_template;
     1071
    9381072    // If activity commenting is disabled, return current value.
     1073    if ( bp_disable_blogforum_comments() || ! isset( $activities_template->in_the_loop ) ) {
     1074        return $retval;
     1075    }
     1076
     1077    $type = bp_get_activity_type();
     1078
     1079    // It's a post type supporting comment tracking.
     1080    if ( bp_activity_type_supports( $type, 'post-type-comment-tracking' ) ) {
     1081        // The activity type is supporting comments or replies
     1082        if ( bp_activity_type_supports( $type, 'post-type-comment-reply' ) ) {
     1083            // Setup some globals we'll need to reference later.
     1084            bp_blogs_setup_activity_loop_globals( $activities_template->activity );
     1085
     1086            // If comments are closed for the WP blog post, we should disable
     1087            // activity comments for this activity entry.
     1088            if ( empty( buddypress()->blogs->allow_comments[ bp_get_activity_id() ] ) ) {
     1089                $retval = false;
     1090            }
     1091        // The activity type does not support comments or replies
     1092        } else {
     1093            $retval = false;
     1094        }
     1095    }
     1096
     1097    return $retval;
     1098}
     1099add_filter( 'bp_activity_can_comment', 'bp_blogs_disable_activity_commenting' );
     1100
     1101/**
     1102 * Limit the display of post type synced comments.
     1103 *
     1104 * @since  2.5.0
     1105 *
     1106 * When viewing the synced comments in stream mode, this prevents comments to
     1107 * be displayed twice, and avoids a Javascript error as the form to add replies
     1108 * is not available.
     1109 *
     1110 * @param  int $retval  The comment count for the activity.
     1111 * @return int          The comment count, or 0 to hide activity comment replies.
     1112 */
     1113function bp_blogs_post_type_comments_avoid_duplicates( $retval ) {
     1114    /**
     1115     * Only limit the display when Post type comments are synced with
     1116     * activity comments.
     1117     */
    9391118    if ( bp_disable_blogforum_comments() ) {
    9401119        return $retval;
    9411120    }
    9421121
    943     // Activity commenting is enabled for blog posts.
    944     switch ( bp_get_activity_action_name() ) {
    945 
    946         // We still have to disable activity commenting for 'new_blog_comment' items
    947         // commenting should only be done on the parent 'new_blog_post' item.
    948         case 'new_blog_comment' :
    949             $retval = false;
    950 
    951             break;
    952 
    953         // Check if commenting is disabled for the WP blog post
    954         // we should extrapolate this and automate this for plugins... or not.
    955         case 'new_blog_post' :
    956             global $activities_template;
    957 
    958             // Setup some globals we'll need to reference later.
    959             bp_blogs_setup_activity_loop_globals( $activities_template->activity );
    960 
    961             // If comments are closed for the WP blog post, we should disable
    962             // activity comments for this activity entry.
    963             if ( empty( buddypress()->blogs->allow_comments[bp_get_activity_id()] ) ) {
    964                 $retval = false;
    965             }
    966 
    967             break;
     1122    if ( 'activity_comment' !== bp_get_activity_type() ) {
     1123        return $retval;
     1124    }
     1125
     1126    // Check the parent activity
     1127    $parent_activity = new BP_Activity_Activity( bp_get_activity_item_id() );
     1128
     1129    if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) {
     1130        $retval = 0;
    9681131    }
    9691132
    9701133    return $retval;
    9711134}
    972 add_filter( 'bp_activity_can_comment', 'bp_blogs_disable_activity_commenting' );
     1135add_filter( 'bp_activity_get_comment_count', 'bp_blogs_post_type_comments_avoid_duplicates' );
    9731136
    9741137/**
     
    10581221    }
    10591222
    1060     $blog_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
    1061 
    1062     if ( ! empty( $blog_comment_id ) ) {
     1223    if ( bp_disable_blogforum_comments() ) {
     1224        return $retval;
     1225    }
     1226
     1227    $parent_activity = new BP_Activity_Activity( $activity->item_id );
     1228
     1229    if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) {
    10631230        $retval = $activity->primary_link;
    10641231    }
     
    10841251    }
    10851252
    1086     $blog_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
     1253    if ( bp_disable_blogforum_comments() ) {
     1254        return $retval;
     1255    }
     1256
     1257    $parent_activity = new BP_Activity_Activity( $activity->item_id );
     1258
     1259    if ( ! isset( $parent_activity->type ) ) {
     1260        return $retval;
     1261    }
     1262
     1263    $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' );
     1264
     1265    if ( ! $post_type ) {
     1266        return $retval;
     1267    }
     1268
     1269    $blog_comment_id = bp_activity_get_meta( $activity->id, "bp_blogs_{$post_type}_comment_id" );
    10871270
    10881271    if ( ! empty( $blog_comment_id ) ) {
    1089         // Fetch the parent blog post activity item.
    1090         $parent_blog_post_activity = new BP_Activity_Activity( $activity->item_id );
    1091 
    1092         // Fake a 'new_blog_comment' activity object.
    1093         $object = $activity;
    1094 
    1095         // Override 'item_id' to use blog ID.
    1096         $object->item_id = $parent_blog_post_activity->item_id;
    1097 
    1098         // Override 'secondary_item_id' to use comment ID.
    1099         $object->secondary_item_id = $blog_comment_id;
    1100 
    1101         // Now format the activity action using the 'new_blog_comment' action callback.
    1102         $retval = bp_blogs_format_activity_action_new_blog_comment( '', $object );
     1272        $bp = buddypress();
     1273
     1274        // Check if a comment action id is set for the parent activity
     1275        $comment_action_id = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'comment_action_id' );
     1276
     1277        // Use the action string callback for the activity type
     1278        if ( ! empty( $comment_action_id ) ) {
     1279            // Fake a 'new_{post_type}_comment' by cloning the activity object.
     1280            $object = clone $activity;
     1281
     1282            // Set the type of the activity to be a comment about a post type
     1283            $object->type = $comment_action_id;
     1284
     1285            // Use the blog ID as the item_id.
     1286            $object->item_id = $parent_activity->item_id;
     1287
     1288            // Use comment ID as the secondary_item_id.
     1289            $object->secondary_item_id = $blog_comment_id;
     1290
     1291            // Get the format callback for this activity comment
     1292            $format_callback = bp_activity_post_type_get_tracking_arg( $comment_action_id, 'format_callback' );
     1293
     1294            // now format the activity action using the 'new_{post_type}_comment' action callback
     1295            if ( is_callable( $format_callback ) ) {
     1296                $retval = call_user_func_array( $format_callback, array( '', $object ) );
     1297            }
     1298        }
    11031299    }
    11041300
  • trunk/src/bp-blogs/bp-blogs-filters.php

    r10374 r10544  
    6262
    6363/**
    64  * Check whether the current post can be published.
     64 * Check whether the current activity about a post or a comment can be published.
    6565 *
    6666 * Abstracted from the deprecated `bp_blogs_record_post()`.
     
    122122}
    123123add_filter( 'bp_activity_post_pre_publish', 'bp_blogs_post_pre_publish', 10, 4 );
     124add_filter( 'bp_activity_post_pre_comment', 'bp_blogs_post_pre_publish', 10, 4 );
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r10426 r10544  
    478478 *
    479479 * @since 2.2.0
    480  *
    481  * @param WP_Post              $post     Post object.
    482  * @param BP_Activity_Activity $activity Activity object.
    483  */
    484 function bp_blogs_update_post_activity_meta( $post, $activity ) {
    485     if ( empty( $activity->id ) || 'post' != $post->post_type ) {
     480 * @since 2.5.0 Add the post type tracking args object parameter
     481 *
     482 * @param WP_Post              $post                 Post object.
     483 * @param BP_Activity_Activity $activity             Activity object.
     484 * @param object               $activity_post_object The post type tracking args object.
     485 */
     486function bp_blogs_update_post_activity_meta( $post, $activity, $activity_post_object ) {
     487    if ( empty( $activity->id ) || empty( $activity_post_object->action_id ) ) {
    486488        return;
    487489    }
     
    492494        bp_activity_update_meta( $activity->id, 'post_title', $post->post_title );
    493495
    494         // Now update activity meta for post comments... sigh.
    495         add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
    496         $comments = get_comments( array( 'post_id' => $post->ID ) );
    497         remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
    498 
    499         if ( ! empty( $comments ) ) {
    500             $activity_ids = array();
    501             $comment_ids  = wp_list_pluck( $comments, 'comment_ID' );
    502 
    503             // Set up activity args.
    504             $args = array(
    505                 'update_meta_cache' => false,
    506                 'show_hidden'       => true,
    507                 'per_page'          => 99999,
    508             );
    509 
    510             // Query for old-style "new_blog_comment" activity items.
    511             $args['filter'] = array(
    512                 'object'       => buddypress()->blogs->id,
    513                 'action'       => 'new_blog_comment',
    514                 'secondary_id' => implode( ',', $comment_ids ),
    515             );
    516 
    517             $activities = bp_activity_get( $args );
    518             if ( ! empty( $activities['activities'] ) ) {
    519                 $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' );
     496        if ( ! empty( $activity_post_object->comments_tracking->action_id ) ) {
     497            // Now update activity meta for post comments... sigh.
     498            add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
     499            $comments = get_comments( array( 'post_id' => $post->ID ) );
     500            remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
     501
     502            if ( ! empty( $comments ) ) {
     503                $activity_ids = array();
     504                $comment_ids  = wp_list_pluck( $comments, 'comment_ID' );
     505
     506                // Set up activity args.
     507                $args = array(
     508                    'update_meta_cache' => false,
     509                    'show_hidden'       => true,
     510                    'per_page'          => 99999,
     511                );
     512
     513                // Query for old-style "new_blog_comment" activity items.
     514                $args['filter'] = array(
     515                    'object'       => $activity_post_object->comments_tracking->component_id,
     516                    'action'       => $activity_post_object->comments_tracking->action_id,
     517                    'secondary_id' => implode( ',', $comment_ids ),
     518                );
     519
     520                $activities = bp_activity_get( $args );
     521                if ( ! empty( $activities['activities'] ) ) {
     522                    $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' );
     523                }
     524
     525                // Query for activity comments connected to a blog post.
     526                unset( $args['filter'] );
     527                $args['meta_query'] = array( array(
     528                    'key'     => 'bp_blogs_' . $post->post_type . '_comment_id',
     529                    'value'   => $comment_ids,
     530                    'compare' => 'IN',
     531                ) );
     532                $args['type'] = 'activity_comment';
     533                $args['display_comments'] = 'stream';
     534
     535                $activities = bp_activity_get( $args );
     536                if ( ! empty( $activities['activities'] ) ) {
     537                    $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) );
     538                }
     539
     540                // Update activity meta for all found activity items.
     541                if ( ! empty( $activity_ids ) ) {
     542                    foreach ( $activity_ids as $aid ) {
     543                        bp_activity_update_meta( $aid, 'post_title', $post->post_title );
     544                    }
     545                }
     546
     547                unset( $activities, $activity_ids, $comment_ids, $comments );
    520548            }
    521 
    522             // Query for activity comments connected to a blog post.
    523             unset( $args['filter'] );
    524             $args['meta_query'] = array( array(
    525                 'key'     => 'bp_blogs_post_comment_id',
    526                 'value'   => $comment_ids,
    527                 'compare' => 'IN',
    528             ) );
    529             $args['type'] = 'activity_comment';
    530             $args['display_comments'] = 'stream';
    531 
    532             $activities = bp_activity_get( $args );
    533             if ( ! empty( $activities['activities'] ) ) {
    534                 $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) );
    535             }
    536 
    537             // Update activity meta for all found activity items.
    538             if ( ! empty( $activity_ids ) ) {
    539                 foreach ( $activity_ids as $aid ) {
    540                     bp_activity_update_meta( $aid, 'post_title', $post->post_title );
    541                 }
    542             }
    543 
    544             unset( $activities, $activity_ids, $comment_ids, $comments );
    545549        }
    546550    }
     
    553557    }
    554558}
    555 add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 2 );
    556 
    557 /**
    558  * Record a new blog comment in the BuddyPress activity stream.
    559  *
    560  * Only posts the item if blog is public and post is not password-protected.
    561  *
    562  * @param int         $comment_id  ID of the comment being recorded.
    563  * @param bool|string $is_approved Optional. The $is_approved value passed to
    564  *                                 the 'comment_post' action. Default: true.
    565  * @return bool|object Returns false on failure, the comment object on success.
    566  */
    567 function bp_blogs_record_comment( $comment_id, $is_approved = true ) {
    568     // Bail if activity component is not active.
    569     if ( ! bp_is_active( 'activity' ) ) {
    570         return;
    571     }
    572 
    573     // Get the users comment.
    574     $recorded_comment = get_comment( $comment_id );
    575 
    576     // Don't record activity if the comment hasn't been approved.
    577     if ( empty( $is_approved ) )
     559add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 3 );
     560
     561/**
     562 * Update Activity and blogs meta and eventually sync comment with activity comment
     563 *
     564 * @since  2.5.0
     565 *
     566 * @param  int|bool   $activity_id          ID of recorded activity, or false if sync is active.
     567 * @param  WP_Comment $comment              The comment object.
     568 * @param  array      $activity_args        Array of activity arguments.
     569 * @param  object     $activity_post_object The post type tracking args object.
     570 * @return int|bool   Returns false if no activity, the activity id otherwise.
     571 */
     572function bp_blogs_comment_sync_activity_comment( &$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null ) {
     573    if ( empty( $activity_args ) || empty( $comment->post->ID ) || empty( $activity_post_object->comment_action_id ) ) {
    578574        return false;
    579 
    580     // Don't record activity if no email address has been included.
    581     if ( empty( $recorded_comment->comment_author_email ) )
    582         return false;
    583 
    584     // Don't record activity if the comment has already been marked as spam.
    585     if ( 'spam' === $is_approved )
    586         return false;
    587 
    588     // Get the user by the comment author email.
    589     $user = get_user_by( 'email', $recorded_comment->comment_author_email );
    590 
    591     // If user isn't registered, don't record activity.
    592     if ( empty( $user ) )
    593         return false;
    594 
    595     // Get the user_id.
    596     $user_id = (int) $user->ID;
    597 
    598     // Get blog and post data.
     575    }
     576
     577    // Set the current blog id.
    599578    $blog_id = get_current_blog_id();
    600579
    601     // If blog is not trackable, do not record the activity.
    602     if ( ! bp_blogs_is_blog_trackable( $blog_id, $user_id ) )
    603         return false;
    604 
    605     $recorded_comment->post = get_post( $recorded_comment->comment_post_ID );
    606 
    607     if ( empty( $recorded_comment->post ) || is_wp_error( $recorded_comment->post ) )
    608         return false;
    609 
    610     // If this is a password protected post, don't record the comment.
    611     if ( !empty( $recorded_comment->post->post_password ) )
    612         return false;
    613 
    614     // Don't record activity if the comment's associated post isn't a WordPress Post.
    615     if ( !in_array( $recorded_comment->post->post_type, apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) ) ) )
    616         return false;
    617 
    618     $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );
    619 
    620     // If blog is public allow activity to be posted.
    621     if ( $is_blog_public ) {
    622 
    623         // Get activity related links.
    624         $post_permalink = get_permalink( $recorded_comment->comment_post_ID );
    625         $comment_link   = get_comment_link( $recorded_comment->comment_ID );
    626 
    627         // Setup activity args.
    628         $args = array();
    629 
    630         $args['user_id']       = $user_id;
    631         $args['content']       = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content', array( $recorded_comment->comment_content, &$recorded_comment, $comment_link ) );
    632         $args['primary_link']  = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link,     &$recorded_comment ) );
    633         $args['recorded_time'] = $recorded_comment->comment_date_gmt;
    634 
    635         // Setup some different activity args depending if activity commenting is
    636         // enabled or not.
    637         // if cannot comment, record separate activity entry
    638         // this is the old way of doing things.
    639         if ( bp_disable_blogforum_comments() ) {
    640             $args['type']              = 'new_blog_comment';
    641             $args['item_id']           = $blog_id;
    642             $args['secondary_item_id'] = $comment_id;
    643 
    644             // Record the activity entry.
    645             $activity_id = bp_blogs_record_activity( $args );
    646 
    647             // Add some post info in activity meta.
    648             bp_activity_update_meta( $activity_id, 'post_title', $recorded_comment->post->post_title );
    649             bp_activity_update_meta( $activity_id, 'post_url',   add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) );
    650 
    651         // Record comment as BP activity comment under the parent 'new_blog_post'
    652         // activity item.
    653         } else {
    654             // This is a comment edit
    655             // check to see if corresponding activity entry already exists.
    656             if ( ! empty( $_REQUEST['action'] ) ) {
    657                 $existing_activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
    658 
    659                 if ( ! empty( $existing_activity_id ) ) {
    660                     $args['id'] = $existing_activity_id;
    661                 }
     580    // These activity metadatas are used to build the new_blog_comment action string
     581    if ( ! empty( $activity_id ) && ! empty( $activity_args['item_id'] ) && 'new_blog_comment' === $activity_post_object->comment_action_id ) {
     582        // add some post info in activity meta
     583        bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title );
     584        bp_activity_update_meta( $activity_id, 'post_url',   esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) );
     585    }
     586
     587    // Sync comment - activity comment
     588    if ( ! bp_disable_blogforum_comments() ) {
     589
     590        if ( ! empty( $_REQUEST['action'] ) ) {
     591            $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
     592
     593            if ( ! empty( $existing_activity_id ) ) {
     594                $activity_args['id'] = $existing_activity_id;
    662595            }
    663 
    664             // Find the parent 'new_blog_post' activity entry.
     596        }
     597
     598        if ( empty( $activity_post_object ) ) {
     599            $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type );
     600        }
     601
     602        if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) {
     603            // find the parent 'new_post_type' activity entry
    665604            $parent_activity_id = bp_activity_get_activity_id( array(
    666                 'component'         => 'blogs',
    667                 'type'              => 'new_blog_post',
     605                'component'         => $activity_post_object->component_id,
     606                'type'              => $activity_post_object->action_id,
    668607                'item_id'           => $blog_id,
    669                 'secondary_item_id' => $recorded_comment->comment_post_ID
     608                'secondary_item_id' => $comment->comment_post_ID
    670609            ) );
    671610
    672611            // Try to create a new activity item for the parent blog post.
    673612            if ( empty( $parent_activity_id ) ) {
    674                 $parent_activity_id = bp_activity_post_type_publish( $recorded_comment->comment_post_ID, $recorded_comment->post );
     613                $parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post );
    675614            }
    676 
    677             // We found the parent activity entry
    678             // so let's go ahead and reconfigure some activity args.
    679             if ( ! empty( $parent_activity_id ) ) {
    680                 // Set the 'item_id' with the parent activity entry ID.
    681                 $args['item_id'] = $parent_activity_id;
    682 
    683                 // Now see if the WP parent comment has a BP activity ID.
    684                 $comment_parent = 0;
    685                 if ( ! empty( $recorded_comment->comment_parent ) ) {
    686                     $comment_parent = get_comment_meta( $recorded_comment->comment_parent, 'bp_activity_comment_id', true );
    687                 }
    688 
    689                 // WP parent comment does not have a BP activity ID
    690                 // so set to 'new_blog_post' activity ID.
    691                 if ( empty( $comment_parent ) ) {
    692                     $comment_parent = $parent_activity_id;
    693                 }
    694 
    695                 $args['secondary_item_id'] = $comment_parent;
    696                 $args['component']         = 'activity';
    697                 $args['type']              = 'activity_comment';
    698 
    699             // Could not find corresponding parent activity entry
    700             // so wipe out $args array.
    701             } else {
    702                 $args = array();
     615        }
     616
     617        // we found the parent activity entry
     618        // so let's go ahead and reconfigure some activity args
     619        if ( ! empty( $parent_activity_id ) ) {
     620            // set the parent activity entry ID
     621            $activity_args['activity_id'] = $parent_activity_id;
     622
     623            // now see if the WP parent comment has a BP activity ID
     624            $comment_parent = 0;
     625            if ( ! empty( $comment->comment_parent ) ) {
     626                $comment_parent = get_comment_meta( $comment->comment_parent, 'bp_activity_comment_id', true );
    703627            }
    704628
    705             // Record in activity streams.
    706             if ( ! empty( $args ) ) {
    707                 // @todo should we use bp_activity_new_comment()? that function will also send
    708                 // an email to people in the activity comment thread.
    709                 //
    710                 // What if a site already has some comment email notification plugin setup?
    711                 // this is why I decided to go with bp_activity_add() to avoid any conflict
    712                 // with existing comment email notification plugins.
    713                 $comment_activity_id = bp_activity_add( $args );
    714 
    715                 if ( empty( $args['id'] ) ) {
    716                     // Add meta to activity comment.
    717                     bp_activity_update_meta( $comment_activity_id, 'bp_blogs_post_comment_id', $comment_id );
    718                     bp_activity_update_meta( $comment_activity_id, 'post_title', $recorded_comment->post->post_title );
    719                     bp_activity_update_meta( $comment_activity_id, 'post_url', add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) );
    720 
    721                     // Add meta to comment.
    722                     add_comment_meta( $comment_id, 'bp_activity_comment_id', $comment_activity_id );
     629            // WP parent comment does not have a BP activity ID
     630            // so set to 'new_' . post_type activity ID
     631            if ( empty( $comment_parent ) ) {
     632                $comment_parent = $parent_activity_id;
     633            }
     634
     635            $activity_args['parent_id']         = $comment_parent;
     636            $activity_args['skip_notification'] = true;
     637
     638        // could not find corresponding parent activity entry
     639        // so wipe out $args array
     640        } else {
     641            $activity_args = array();
     642        }
     643
     644        // Record in activity streams
     645        if ( ! empty( $activity_args ) ) {
     646            $activity_id = bp_activity_new_comment( $activity_args );
     647
     648            if ( empty( $activity_args['id'] ) ) {
     649                // The activity metadata to inform about the corresponding comment ID
     650                bp_activity_update_meta( $activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID );
     651
     652                // The comment metadata to inform about the corresponding activity ID
     653                add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id );
     654
     655                // These activity metadatas are used to build the new_blog_comment action string
     656                if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
     657                    bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title );
     658                    bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) );
    723659                }
    724660            }
    725661        }
    726 
    727         // Update the blogs last active date.
    728         bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    729     }
    730 
    731     return $recorded_comment;
    732 }
    733 add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    734 add_action( 'edit_comment', 'bp_blogs_record_comment', 10    );
     662    }
     663
     664    // Update the blogs last active date
     665    bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
     666
     667    if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
     668        /**
     669         * Fires after BuddyPress has recorded metadata about a published blog post comment.
     670         *
     671         * @since 2.5.0
     672         *
     673         * @param int     $value    Comment ID of the blog post comment being recorded.
     674         * @param WP_Post $post  WP_Comment object for the current blog post.
     675         * @param string  $value ID of the user associated with the current blog post comment.
     676         */
     677        do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() );
     678    }
     679
     680    return $activity_id;
     681}
     682add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 );
    735683
    736684/**
     
    985933
    986934/**
    987  * Remove a blog comment activity item from the activity stream.
    988  *
    989  * @param int $comment_id ID of the comment to be removed.
    990  */
    991 function bp_blogs_remove_comment( $comment_id ) {
    992     global $wpdb;
    993 
    994     // Activity comments are disabled for blog posts
    995     // which means that individual activity items exist for blog comments.
    996     if ( bp_disable_blogforum_comments() ) {
    997         // Delete the individual activity stream item.
    998         bp_blogs_delete_activity( array(
    999             'item_id'           => $wpdb->blogid,
    1000             'secondary_item_id' => $comment_id,
    1001             'type'              => 'new_blog_comment'
    1002         ) );
    1003 
    1004     // Activity comments are enabled for blog posts
    1005     // remove the associated activity item.
    1006     } else {
    1007         // Get associated activity ID from comment meta.
     935 * Remove a synced activity comment from the activity stream.
     936 *
     937 * @since 2.5.0
     938 *
     939 * @param bool   $deleted              True when a comment post type activity was successfully removed.
     940 * @param int    $comment_id           ID of the comment to be removed.
     941 * @param object $activity_post_object The post type tracking args object.
     942 * @param string $activity_type        The post type comment activity type.
     943 *
     944 * @return bool True on success. False on error.
     945 */
     946function bp_blogs_post_type_remove_comment( $deleted, $comment_id, $activity_post_object, $activity_type = '' ) {
     947    // Remove synced activity comments, if needed.
     948    if ( ! bp_disable_blogforum_comments() ) {
     949        // Get associated activity ID from comment meta
    1008950        $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
    1009951
    1010         // Delete the associated activity comment.
    1011         //
    1012         // Also removes child post comments and associated activity comments.
    1013         if ( ! empty( $activity_id ) && bp_is_active( 'activity' ) ) {
    1014             // Fetch the activity comments for the activity item.
     952        /**
     953         * Delete the associated activity comment & also remove
     954         * child post comments and associated activity comments.
     955         */
     956        if ( ! empty( $activity_id ) ) {
     957            // fetch the activity comments for the activity item
    1015958            $activity = bp_activity_get( array(
    1016959                'in'               => $activity_id,
     
    1019962            ) );
    1020963
    1021             // Get all activity comment IDs for the pending deleted item.
     964            // get all activity comment IDs for the pending deleted item
    1022965            if ( ! empty( $activity['activities'] ) ) {
    1023966                $activity_ids   = bp_activity_recurse_comments_activity_ids( $activity );
    1024967                $activity_ids[] = $activity_id;
    1025968
    1026                 // Delete activity items.
     969                // delete activity items
    1027970                foreach ( $activity_ids as $activity_id ) {
    1028971                    bp_activity_delete( array(
     
    1031974                }
    1032975
    1033                 // Remove associated blog comments.
     976                // remove associated blog comments
    1034977                bp_blogs_remove_associated_blog_comments( $activity_ids );
    1035978
    1036                 // Rebuild activity comment tree.
     979                // rebuild activity comment tree
    1037980                BP_Activity_Activity::rebuild_activity_comment_tree( $activity['activities'][0]->item_id );
     981
     982                // Set the result
     983                $deleted = true;
    1038984            }
    1039985        }
    1040986    }
    1041987
    1042     /**
    1043      * Fires after a blog comment activity item was removed from activity stream.
    1044      *
    1045      * @since 1.0.0
    1046      *
    1047      * @param int $blogid     Item ID for the blog associated with the removed comment.
    1048      * @param int $comment_id ID of the comment being removed.
    1049      * @param int $value      ID of the current logged in user.
    1050      */
    1051     do_action( 'bp_blogs_remove_comment', $wpdb->blogid, $comment_id, bp_loggedin_user_id() );
    1052 }
    1053 add_action( 'delete_comment', 'bp_blogs_remove_comment' );
     988    // Backcompat for comments about the 'post' post type.
     989    if ( 'new_blog_comment' === $activity_type ) {
     990        /**
     991         * Fires after a blog comment activity item was removed from activity stream.
     992         *
     993         * @since 1.0.0
     994         *
     995         * @param int $value      ID for the blog associated with the removed comment.
     996         * @param int $comment_id ID of the comment being removed.
     997         * @param int $value      ID of the current logged in user.
     998         */
     999        do_action( 'bp_blogs_remove_comment', get_current_blog_id(), $comment_id, bp_loggedin_user_id() );
     1000    }
     1001
     1002    return $deleted;
     1003}
     1004add_action( 'bp_activity_post_type_remove_comment', 'bp_blogs_post_type_remove_comment', 10, 4 );
    10541005
    10551006/**
     
    10581009 * @since 2.0.0
    10591010 *
    1060  * @see bp_blogs_remove_comment()
     1011 * @see bp_blogs_remove_synced_comment()
    10611012 * @see bp_blogs_sync_delete_from_activity_comment()
    10621013 *
     
    10951046
    10961047/**
    1097  * When a blog comment status transition occurs, update the relevant activity's status.
    1098  *
    1099  * @since 1.6.0
    1100  *
    1101  * @param string $new_status New comment status.
    1102  * @param string $old_status Previous comment status.
    1103  * @param object $comment    Comment data.
    1104  */
    1105 function bp_blogs_transition_activity_status( $new_status, $old_status, $comment ) {
    1106 
    1107     // Check the Activity component is active.
    1108     if ( ! bp_is_active( 'activity' ) )
    1109         return;
    1110 
    1111     /**
    1112      * Activity currently doesn't have any concept of a trash, or an unapproved/approved state.
    1113      *
    1114      * If a blog comment transitions to a "delete" or "hold" status, delete the activity item.
    1115      * If a blog comment transitions to trashed, or spammed, mark the activity as spam.
    1116      * If a blog comment transitions to approved (and the activity exists), mark the activity as ham.
    1117      * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam.
    1118      * Otherwise, record the comment into the activity stream.
    1119      */
    1120 
    1121     // This clause was moved in from bp_blogs_remove_comment() in BuddyPress 1.6. It handles delete/hold.
    1122     if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {
    1123         return bp_blogs_remove_comment( $comment->comment_ID );
    1124 
    1125     // These clauses handle trash, spam, and un-spams.
    1126     } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) {
    1127         $action = 'spam_activity';
    1128     } elseif ( 'approved' == $new_status ) {
    1129         $action = 'ham_activity';
    1130     }
    1131 
    1132     // Get the activity.
    1133     if ( bp_disable_blogforum_comments() ) {
    1134         $activity_id = bp_activity_get_activity_id( array(
    1135             'component'         => buddypress()->blogs->id,
    1136             'item_id'           => get_current_blog_id(),
    1137             'secondary_item_id' => $comment->comment_ID,
    1138             'type'              => 'new_blog_comment'
    1139         ) );
    1140     } else {
    1141         $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
    1142     }
    1143 
    1144     // Check activity item exists.
    1145     if ( empty( $activity_id ) ) {
    1146         // If no activity exists, but the comment has been approved, record it into the activity table.
    1147         if ( 'approved' == $new_status ) {
    1148             return bp_blogs_record_comment( $comment->comment_ID, true );
    1149         }
    1150 
    1151         return;
    1152     }
    1153 
    1154     // Create an activity object.
    1155     $activity = new BP_Activity_Activity( $activity_id );
    1156     if ( empty( $activity->component ) )
    1157         return;
    1158 
    1159     // Spam/ham the activity if it's not already in that state.
    1160     if ( 'spam_activity' == $action && ! $activity->is_spam ) {
    1161         bp_activity_mark_as_spam( $activity );
    1162     } elseif ( 'ham_activity' == $action) {
    1163         bp_activity_mark_as_ham( $activity );
    1164     }
    1165 
    1166     // Add "new_blog_comment" to the whitelisted activity types, so that the activity's Akismet history is generated.
    1167     $comment_akismet_history = create_function( '$t', '$t[] = "new_blog_comment"; return $t;' );
    1168     add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
    1169 
    1170     // Save the updated activity.
    1171     $activity->save();
    1172 
    1173     // Remove the "new_blog_comment" activity type whitelist so we don't break anything.
    1174     remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
    1175 }
    1176 add_action( 'transition_comment_status', 'bp_blogs_transition_activity_status', 10, 3 );
    1177 
    1178 /**
    11791048 * Get the total number of blogs being tracked by BuddyPress.
    11801049 *
  • trunk/src/bp-blogs/classes/class-bp-blogs-component.php

    r10517 r10544  
    107107            }
    108108        }
    109 
    110         // Filter the generic track parameters for the 'post' post type.
    111         add_filter( 'bp_activity_get_post_type_tracking_args', array( $this, 'post_tracking_args' ), 10, 2 );
    112109    }
    113110
     
    129126            'template',
    130127            'filters',
    131             'activity',
    132128            'functions',
    133129        );
     130
     131        if ( bp_is_active( 'activity' ) ) {
     132            $includes[] = 'activity';
     133        }
    134134
    135135        if ( is_multisite() ) {
     
    301301        parent::setup_cache_groups();
    302302    }
    303 
    304     /**
    305      * Set up the tracking arguments for the 'post' post type.
    306      *
    307      * @since 2.2.0
    308      *
    309      * @see bp_activity_get_post_type_tracking_args() for information on parameters.
    310      *
    311      * @param object|null $params    Tracking arguments.
    312      * @param string|int  $post_type Post type to track.
    313      * @return object
    314      */
    315     public function post_tracking_args( $params = null, $post_type = 0 ) {
    316 
    317         /**
    318          * Filters the post types to track for the Blogs component.
    319          *
    320          * @since 1.5.0
    321          * @deprecated 2.3.0
    322          *
    323          * Make sure plugins still using 'bp_blogs_record_post_post_types'
    324          * to track their post types will generate new_blog_post activities
    325          * See https://buddypress.trac.wordpress.org/ticket/6306
    326          *
    327          * @param array $value Array of post types to track.
    328          */
    329         $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) );
    330         $post_types_array = array_flip( $post_types );
    331 
    332         if ( ! isset( $post_types_array[ $post_type ] ) ) {
    333             return $params;
    334         }
    335 
    336         // Set specific params for the 'post' post type.
    337         $params->component_id    = $this->id;
    338         $params->action_id       = 'new_blog_post';
    339         $params->admin_filter    = __( 'New post published', 'buddypress' );
    340         $params->format_callback = 'bp_blogs_format_activity_action_new_blog_post';
    341         $params->front_filter    = __( 'Posts', 'buddypress' );
    342         $params->contexts        = array( 'activity', 'member' );
    343         $params->position        = 5;
    344 
    345         return $params;
    346     }
    347303}
  • trunk/src/bp-core/deprecated/1.6.php

    r10108 r10544  
    8585/**
    8686 * @deprecated 1.6.0
    87  * @deprecated No longer used; see bp_blogs_transition_activity_status()
     87 * @deprecated No longer used; see bp_activity_transition_post_type_comment_status()
    8888 */
    8989function bp_blogs_manage_comment( $comment_id, $comment_status ) {
  • trunk/src/bp-core/deprecated/2.5.php

    r10540 r10544  
    874874}
    875875add_action( 'bp_send_email_success', 'bp_core_deprecated_email_actions', 20, 2 );
     876
     877/**
     878 * When a blog comment status transition occurs, update the relevant activity's status.
     879 *
     880 * @since 1.6.0
     881 * @deprecated 2.5.0
     882 *
     883 * @param string $new_status New comment status.
     884 * @param string $old_status Previous comment status.
     885 * @param object $comment Comment data.
     886 */
     887function bp_blogs_transition_activity_status( $new_status, $old_status, $comment ) {
     888    _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_transition_post_type_comment_status()' );
     889    bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment );
     890}
     891
     892/**
     893 * Record a new blog comment in the BuddyPress activity stream.
     894 *
     895 * Only posts the item if blog is public and post is not password-protected.
     896 *
     897 * @deprecated 2.5.0
     898 *
     899 * @param int $comment_id ID of the comment being recorded.
     900 * @param bool|string $is_approved Optional. The $is_approved value passed to
     901 *        the 'comment_post' action. Default: true.
     902 * @return bool|object Returns false on failure, the comment object on success.
     903 */
     904function bp_blogs_record_comment( $comment_id, $is_approved = true ) {
     905    _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_comment()' );
     906    bp_activity_post_type_comment( $comment_id, $is_approved );
     907}
     908
     909/**
     910 * Remove a blog comment activity item from the activity stream.
     911 *
     912 * @deprecated 2.5.0
     913 *
     914 * @param int $comment_id ID of the comment to be removed.
     915 */
     916function bp_blogs_remove_comment( $comment_id ) {
     917    _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_remove_comment()' );
     918    bp_activity_post_type_remove_comment( $comment_id );
     919}
Note: See TracChangeset for help on using the changeset viewer.