Skip to:
Content

BuddyPress.org

Ticket #6482: 6482.06.patch

File 6482.06.patch, 130.7 KB (added by imath, 4 years ago)
  • src/bp-activity/bp-activity-actions.php

    diff --git src/bp-activity/bp-activity-actions.php src/bp-activity/bp-activity-actions.php
    index 7d17133..8109ae7 100644
    function bp_activity_catch_transition_post_type_status( $new_status, $old_status 
    831831        }
    832832}
    833833add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
     834
     835/**
     836 * When a post type comment status transition occurs, update the relevant activity's status.
     837 *
     838 * @since 2.5.0
     839 *
     840 * @param string     $new_status New comment status.
     841 * @param string     $old_status Previous comment status.
     842 * @param WP_Comment $comment Comment data.
     843 */
     844function bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ) {
     845        $post_type = get_post_type( $comment->comment_post_ID );
     846        if ( ! $post_type ) {
     847                return;
     848        }
     849
     850        // Get the post type tracking args.
     851        $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     852
     853        // Bail if the activity type does not exist
     854        if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     855                return false;
     856
     857        // Set the $activity_comment_object
     858        } else {
     859                $activity_comment_object = $activity_post_object->comments_tracking;
     860        }
     861
     862        // Init an empty activity ID
     863        $activity_id = 0;
     864
     865        /**
     866         * Activity currently doesn't have any concept of a trash, or an unapproved/approved state.
     867         *
     868         * If a blog comment transitions to a "delete" or "hold" status, delete the activity item.
     869         * If a blog comment transitions to trashed, or spammed, mark the activity as spam.
     870         * If a blog comment transitions to approved (and the activity exists), mark the activity as ham.
     871         * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam.
     872         * Otherwise, record the comment into the activity stream.
     873         */
     874
     875        // This clause handles delete/hold.
     876        if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {
     877                return bp_activity_post_type_remove_comment( $comment->comment_ID, $activity_post_object );
     878
     879        // These clauses handle trash, spam, and un-spams.
     880        } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) {
     881                $action = 'spam_activity';
     882        } elseif ( 'approved' == $new_status ) {
     883                $action = 'ham_activity';
     884        }
     885
     886        // Get the activity
     887        if ( bp_disable_blogforum_comments() ) {
     888                $activity_id = bp_activity_get_activity_id( array(
     889                        'component'         => $activity_comment_object->component_id,
     890                        'item_id'           => get_current_blog_id(),
     891                        'secondary_item_id' => $comment->comment_ID,
     892                        'type'              => $activity_comment_object->action_id,
     893                ) );
     894        } else {
     895                $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
     896        }
     897
     898        /**
     899         * Leave a chance to plugins to manage activity comments differently.
     900         *
     901         * @since  2.5.0
     902         *
     903         * @param bool        $value       True to override BuddyPress management.
     904         * @param string      $post_type   The post type name.
     905         * @param int         $activity_id The post type activity (0 if not found).
     906         * @param string      $new_status  The new status of the post type comment.
     907         * @param string      $old_status  The old status of the post type comment.
     908         * @param WP_Comment  $comment Comment data.
     909         */
     910        if ( true === apply_filters( 'bp_activity_pre_transition_post_type_comment_status', false, $post_type, $activity_id, $new_status, $old_status, $comment ) ) {
     911                return false;
     912        }
     913
     914        // Check activity item exists
     915        if ( empty( $activity_id ) ) {
     916                // If no activity exists, but the comment has been approved, record it into the activity table.
     917                if ( 'approved' == $new_status ) {
     918                        return bp_activity_post_type_comment( $comment->comment_ID, true, $activity_post_object );
     919                }
     920
     921                return;
     922        }
     923
     924        // Create an activity object
     925        $activity = new BP_Activity_Activity( $activity_id );
     926        if ( empty( $activity->component ) ) {
     927                return;
     928        }
     929
     930        // Spam/ham the activity if it's not already in that state
     931        if ( 'spam_activity' === $action && ! $activity->is_spam ) {
     932                bp_activity_mark_as_spam( $activity );
     933        } elseif ( 'ham_activity' == $action) {
     934                bp_activity_mark_as_ham( $activity );
     935        }
     936
     937        // Add "new_post_type_comment" to the whitelisted activity types, so that the activity's Akismet history is generated
     938        $post_type_comment_action = $activity_comment_object->action_id;
     939        $comment_akismet_history = create_function( '$t', '$t[] = $post_type_comment_action; return $t;' );
     940        add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
     941
     942        // Make sure the activity change won't edit the comment if sync is on
     943        remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     944
     945        // Save the updated activity
     946        $activity->save();
     947
     948        // Restore the action
     949        add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     950
     951        // Remove the "new_blog_comment" activity type whitelist so we don't break anything
     952        remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
     953}
     954add_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 );
  • src/bp-activity/bp-activity-admin.php

    diff --git src/bp-activity/bp-activity-admin.php src/bp-activity/bp-activity-admin.php
    index 49c9cad..9cf7e07 100644
    class BP_Activity_List_Table extends WP_List_Table { 
    11101110
    11111111                // Option defaults.
    11121112                $filter           = array();
     1113                $filter_query     = false;
    11131114                $include_id       = false;
    11141115                $search_terms     = false;
    11151116                $sort             = 'DESC';
    class BP_Activity_List_Table extends WP_List_Table { 
    11361137                }*/
    11371138
    11381139                // Filter.
    1139                 if ( !empty( $_REQUEST['activity_type'] ) )
     1140                if ( ! empty( $_REQUEST['activity_type'] ) ) {
    11401141                        $filter = array( 'action' => $_REQUEST['activity_type'] );
    11411142
     1143                        /**
     1144                         * Filter here to override the filter with a filter query
     1145                         *
     1146                         * @since  2.5.0
     1147                         *
     1148                         * @param array $filter
     1149                         */
     1150                        $has_filter_query = apply_filters( 'bp_activity_list_table_filter_activity_type_items', $filter );
     1151
     1152                        if ( ! empty( $has_filter_query['filter_query'] ) ) {
     1153                                // Reset the filter
     1154                                $filter       = array();
     1155
     1156                                // And use the filter query instead
     1157                                $filter_query = $has_filter_query['filter_query'];
     1158                        }
     1159                }
     1160
    11421161                // Are we doing a search?
    11431162                if ( !empty( $_REQUEST['s'] ) )
    11441163                        $search_terms = $_REQUEST['s'];
    class BP_Activity_List_Table extends WP_List_Table { 
    11651184                        'page'             => $page,
    11661185                        'per_page'         => $per_page,
    11671186                        'search_terms'     => $search_terms,
     1187                        'filter_query'     => $filter_query,
    11681188                        'show_hidden'      => true,
    11691189                        // 'sort'             => $sort,
    11701190                        'spam'             => $spam,
    class BP_Activity_List_Table extends WP_List_Table { 
    17681788         * functions from working as intended.
    17691789         *
    17701790         * @since 2.0.0
     1791         * @since 2.5.0 Include Post type activities types
    17711792         *
    17721793         * @param array $item An array version of the BP_Activity_Activity object.
    17731794         * @return bool $can_comment
    17741795         */
    1775         protected function can_comment( $item  ) {
    1776                 $can_comment = true;
    1777 
    1778                 if ( $this->disable_blogforum_comments ) {
    1779                         switch ( $item['type'] ) {
    1780                                 case 'new_blog_post' :
    1781                                 case 'new_blog_comment' :
    1782                                 case 'new_forum_topic' :
    1783                                 case 'new_forum_post' :
    1784                                         $can_comment = false;
    1785                                         break;
    1786                         }
     1796        protected function can_comment( $item ) {
     1797                $can_comment = bp_activity_type_supports( $item['type'], 'comment-reply' );
    17871798
    1788                 // Activity comments supported.
    1789                 } else {
    1790                         // Activity comment.
    1791                         if ( 'activity_comment' == $item['type'] ) {
    1792                                 // Blogs.
    1793                                 if ( bp_is_active( 'blogs' ) ) {
    1794                                         // Grab the parent activity entry.
    1795                                         $parent_activity = new BP_Activity_Activity( $item['item_id'] );
    1796 
    1797                                         // Fetch blog post comment depth and if the blog post's comments are open.
    1798                                         bp_blogs_setup_activity_loop_globals( $parent_activity );
    1799 
    1800                                         // Check if the activity item can be replied to.
    1801                                         if ( false === bp_blogs_can_comment_reply( true, $item ) ) {
    1802                                                 $can_comment = false;
    1803                                         }
    1804                                 }
     1799                if ( ! $this->disable_blogforum_comments && bp_is_active( 'blogs' ) ) {
     1800                        $parent_activity = false;
     1801
     1802                        if ( bp_activity_type_supports( $item['type'], 'post-type-comment-tracking' ) ) {
     1803                                $parent_activity = (object) $item;
     1804                        } elseif ( 'activity_comment' === $item['type'] ) {
     1805                                $parent_activity = new BP_Activity_Activity( $item['item_id'] );
     1806                        }
    18051807
    1806                         // Blog post.
    1807                         } elseif ( 'new_blog_post' == $item['type'] ) {
    1808                                 if ( bp_is_active( 'blogs' ) ) {
    1809                                         bp_blogs_setup_activity_loop_globals( (object) $item );
     1808                        if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) {
     1809                                // Fetch blog post comment depth and if the blog post's comments are open.
     1810                                bp_blogs_setup_activity_loop_globals( $parent_activity );
    18101811
    1811                                         if ( empty( buddypress()->blogs->allow_comments[$item['id']] ) ) {
    1812                                                 $can_comment = false;
    1813                                         }
    1814                                 }
     1812                                $can_comment = bp_blogs_can_comment_reply( true, $item );
    18151813                        }
    18161814                }
    18171815
    class BP_Activity_List_Table extends WP_List_Table { 
    18191817                 * Filters if an activity item can be commented on or not.
    18201818                 *
    18211819                 * @since 2.0.0
     1820                 * @since 2.5.0 Add a second parameter to include the activity item into the filter.
    18221821                 *
    1823                  * @param bool $can_comment Whether an activity item can be commented on or not.
     1822                 * @param bool   $can_comment Whether an activity item can be commented on or not.
     1823                 * @param array  $item        An array version of the BP_Activity_Activity object.
    18241824                 */
    1825                 return apply_filters( 'bp_activity_list_table_can_comment', $can_comment );
     1825                return apply_filters( 'bp_activity_list_table_can_comment', $can_comment, $item );
    18261826        }
    18271827
    18281828        /**
  • src/bp-activity/bp-activity-functions.php

    diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php
    index b5e8a93..75464fb 100644
    function bp_activity_set_post_type_tracking_args( $post_type = '', $args = array 
    428428                return false;
    429429        }
    430430
     431        $activity_labels = array(
     432                /* Post labels */
     433                'bp_activity_admin_filter',
     434                'bp_activity_front_filter',
     435                'bp_activity_new_post',
     436                'bp_activity_new_post_ms',
     437                /* Comment labels */
     438                'bp_activity_comments_admin_filter',
     439                'bp_activity_comments_front_filter',
     440                'bp_activity_new_comment',
     441                'bp_activity_new_comment_ms'
     442        );
     443
    431444        // Labels are loaded into the post type object.
    432         foreach ( array( 'bp_activity_admin_filter', 'bp_activity_front_filter', 'bp_activity_new_post', 'bp_activity_new_post_ms' ) as $label_type ) {
     445        foreach ( $activity_labels as $label_type ) {
    433446                if ( ! empty( $args[ $label_type ] ) ) {
    434447                        $wp_post_types[ $post_type ]->labels->{$label_type} = $args[ $label_type ];
    435448                        unset( $args[ $label_type ] );
    function bp_activity_set_post_type_tracking_args( $post_type = '', $args = array 
    446459 * Get tracking arguments for a specific post type.
    447460 *
    448461 * @since 2.2.0
     462 * @since 2.5.0 Add post type comments tracking args
    449463 *
    450464 * @param  string $post_type Name of the post type.
    451465 * @return object The tracking arguments of the post type.
    function bp_activity_get_post_type_tracking_args( $post_type ) { 
    455469                return false;
    456470        }
    457471
    458         $post_type_object = get_post_type_object( $post_type );
     472        $post_type_object           = get_post_type_object( $post_type );
     473        $post_type_support_comments = post_type_supports( $post_type, 'comments' );
    459474
    460475        $post_type_activity = array(
    461                 'component_id'      => buddypress()->activity->id,
    462                 'action_id'         => 'new_' . $post_type,
    463                 'format_callback'   => 'bp_activity_format_activity_action_custom_post_type_post',
    464                 'front_filter'      => $post_type_object->labels->name,
    465                 'contexts'          => array( 'activity' ),
    466                 'position'          => 0,
    467                 'singular'          => strtolower( $post_type_object->labels->singular_name ),
    468                 'activity_comment' => ! post_type_supports( $post_type, 'comments' ),
     476                'component_id'            => buddypress()->activity->id,
     477                'action_id'               => 'new_' . $post_type,
     478                'format_callback'         => 'bp_activity_format_activity_action_custom_post_type_post',
     479                'front_filter'            => $post_type_object->labels->name,
     480                'contexts'                => array( 'activity' ),
     481                'position'                => 0,
     482                'singular'                => strtolower( $post_type_object->labels->singular_name ),
     483                'activity_comment'        => ! $post_type_support_comments,
     484                'comment_action_id'       => false,
     485                'comment_format_callback' => 'bp_activity_format_activity_action_custom_post_type_comment',
    469486        );
    470487
    471488        if ( ! empty( $post_type_object->bp_activity ) ) {
    function bp_activity_get_post_type_tracking_args( $post_type ) { 
    498515                $post_type_activity->new_post_type_action_ms = $post_type_object->labels->bp_activity_new_post_ms;
    499516        }
    500517
     518        // If the post type supports comments and has a comment action id, build the comments tracking args
     519        if ( $post_type_support_comments && ! empty( $post_type_activity->comment_action_id ) ) {
     520                // Init a new container for the activity type for comments
     521                $post_type_activity->comments_tracking = new stdClass();
     522
     523                // Build the activity type for comments
     524                $post_type_activity->comments_tracking->component_id = $post_type_activity->component_id;
     525                $post_type_activity->comments_tracking->action_id    = $post_type_activity->comment_action_id;
     526
     527                // Try to get the comments admin filter from the post type labels.
     528                if ( ! empty( $post_type_object->labels->bp_activity_comments_admin_filter ) ) {
     529                        $post_type_activity->comments_tracking->admin_filter = $post_type_object->labels->bp_activity_comments_admin_filter;
     530
     531                // Fall back to a generic name.
     532                } else {
     533                        $post_type_activity->comments_tracking->admin_filter = _x( 'New item comment posted', 'Post Type generic comments activity admin filter', 'buddypress' );
     534                }
     535
     536                $post_type_activity->comments_tracking->format_callback = $post_type_activity->comment_format_callback;
     537
     538                // Check for the comments front filter in the post type labels.
     539                if ( ! empty( $post_type_object->labels->bp_activity_comments_front_filter ) ) {
     540                        $post_type_activity->comments_tracking->front_filter = $post_type_object->labels->bp_activity_comments_front_filter;
     541
     542                // Fall back to a generic name.
     543                } else {
     544                        $post_type_activity->comments_tracking->front_filter = sprintf( __( '%s comments', 'buddypress' ), $post_type_object->labels->singular_name );
     545                }
     546
     547                $post_type_activity->comments_tracking->contexts = $post_type_activity->contexts;
     548                $post_type_activity->comments_tracking->position = (int) $post_type_activity->position + 1;
     549
     550                // Try to get the action for new post type comment action on non-multisite installations.
     551                if ( ! empty( $post_type_object->labels->bp_activity_new_comment ) ) {
     552                        $post_type_activity->comments_tracking->new_post_type_comment_action = $post_type_object->labels->bp_activity_new_comment;
     553                }
     554
     555                // Try to get the action for new post type comment action on multisite installations.
     556                if ( ! empty( $post_type_object->labels->bp_activity_new_comment_ms ) ) {
     557                        $post_type_activity->comments_tracking->new_post_type_comment_action_ms = $post_type_object->labels->bp_activity_new_comment_ms;
     558                }
     559        }
     560
     561        // Finally make sure we'll be able to find the post type this activity type is associated to.
     562        $post_type_activity->post_type = $post_type;
     563
    501564        /**
    502565         * Filters tracking arguments for a specific post type.
    503566         *
    function bp_activity_get_post_type_tracking_args( $post_type ) { 
    513576 * Get tracking arguments for all post types.
    514577 *
    515578 * @since 2.2.0
     579 * @since 2.5.0 Include post type comments tracking args if needed
    516580 *
    517581 * @return array List of post types with their tracking arguments.
    518582 */
    function bp_activity_get_post_types_tracking_args() { 
    526590                $track_post_type = bp_activity_get_post_type_tracking_args( $post_type );
    527591
    528592                if ( ! empty( $track_post_type ) ) {
     593                        // Set the post type comments tracking args
     594                        if ( ! empty( $track_post_type->comments_tracking->action_id ) ) {
     595                                // Used to check support for comment tracking by activity type (new_post_type_comment)
     596                                $track_post_type->comments_tracking->comments_tracking = true;
     597
     598                                // Used to be able to find the post type this activity type is associated to.
     599                                $track_post_type->comments_tracking->post_type = $post_type;
     600
     601                                $post_types_tracking_args[ $track_post_type->comments_tracking->action_id ] = $track_post_type->comments_tracking;
     602
     603                                // Used to check support for comment tracking by activity type (new_post_type)
     604                                $track_post_type->comments_tracking = true;
     605                        }
     606
    529607                        $post_types_tracking_args[ $track_post_type->action_id ] = $track_post_type;
    530608                }
    531609
    function bp_activity_get_post_types_tracking_args() { 
    543621}
    544622
    545623/**
     624 * Check if the *Post Type* activity supports a specific feature.
     625 *
     626 * @since 2.5.0
     627 *
     628 * @param  string $activity_type The activity type to check.
     629 * @param  string $feature       The feature to check. Currently supports:
     630 *                               'post-type-comment-tracking', 'post-type-comment-reply' & 'comment-reply'.
     631 *                               See inline doc for more info.
     632 * @return bool
     633 */
     634function bp_activity_type_supports( $activity_type = '', $feature = '' ) {
     635        $retval = false;
     636
     637        $bp = buddypress();
     638
     639        switch ( $feature ) {
     640                /**
     641                 * Does this activity type support comment tracking?
     642                 *
     643                 * eg. 'new_blog_post' and 'new_blog_comment' will both return true.
     644                 */
     645                case 'post-type-comment-tracking' :
     646                        // Set the activity track global if not set yet
     647                        if ( empty( $bp->activity->track ) ) {
     648                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     649                        }
     650
     651                        if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
     652                                $retval = true;
     653                        }
     654                        break;
     655
     656                /**
     657                 * Is this a parent activity type that support post comments?
     658                 *
     659                 * eg. 'new_blog_post' will return true; 'new_blog_comment' will return false.
     660                 */
     661                case 'post-type-comment-reply' :
     662                        // Set the activity track global if not set yet.
     663                        if ( empty( $bp->activity->track ) ) {
     664                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     665                        }
     666
     667                        if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) && ! empty( $bp->activity->track[ $activity_type ]->comment_action_id ) ) {
     668                                $retval = true;
     669                        }
     670                        break;
     671
     672                /**
     673                 * Does this activity type support comment & reply?
     674                 */
     675                case 'comment-reply' :
     676                        // Set the activity track global if not set yet.
     677                        if ( empty( $bp->activity->track ) ) {
     678                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     679                        }
     680
     681                        // Post Type activities
     682                        if ( ! empty( $bp->activity->track[ $activity_type ] ) ) {
     683                                if ( isset( $bp->activity->track[ $activity_type ]->activity_comment ) ) {
     684                                        $retval = $bp->activity->track[ $activity_type ]->activity_comment;
     685                                }
     686
     687                                // Eventually override with comment synchronization feature.
     688                                if ( isset( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
     689                                        $retval = $bp->activity->track[ $activity_type ]->comments_tracking && ! bp_disable_blogforum_comments();
     690                                }
     691
     692                        // Retired Forums component
     693                        } elseif ( 'new_forum_topic' === $activity_type || 'new_forum_post' === $activity_type ) {
     694                                $retval = ! bp_disable_blogforum_comments();
     695
     696                        // By Default, all other activity types are supporting comments.
     697                        } else {
     698                                $retval = true;
     699                        }
     700                        break;
     701        }
     702
     703        return $retval;
     704}
     705
     706/**
     707 * Get a specific tracking argument for a given activity type
     708 *
     709 * @since 2.5.0
     710 *
     711 * @param  string       $activity_type the activity type.
     712 * @param  string       $arg           the key of the tracking argument.
     713 * @return mixed        the value of the tracking arg, false if not found.
     714 */
     715function bp_activity_post_type_get_tracking_arg( $activity_type, $arg = '' ) {
     716        if ( empty( $activity_type ) || empty( $arg ) ) {
     717                return false;
     718        }
     719
     720        $bp = buddypress();
     721
     722        // Set the activity track global if not set yet
     723        if ( empty( $bp->activity->track ) ) {
     724                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     725        }
     726
     727        if ( isset( $bp->activity->track[ $activity_type ]->{$arg} ) ) {
     728                return $bp->activity->track[ $activity_type ]->{$arg};
     729        } else {
     730                return false;
     731        }
     732}
     733
     734/**
    546735 * Get all components' activity actions, sorted by their position attribute.
    547736 *
    548737 * @since 2.2.0
    function bp_activity_format_activity_action_custom_post_type_post( $action, $act 
    14051594        return apply_filters( 'bp_activity_custom_post_type_post_action', $action, $activity );
    14061595}
    14071596
     1597/**
     1598 * Format activity action strings for custom post types comments.
     1599 *
     1600 * @since 2.5.0
     1601 *
     1602 * @param string $action   Static activity action.
     1603 * @param object $activity Activity data object.
     1604 *
     1605 * @return string
     1606 */
     1607function bp_activity_format_activity_action_custom_post_type_comment( $action, $activity ) {
     1608        $bp = buddypress();
     1609
     1610        // Fetch all the tracked post types once.
     1611        if ( empty( $bp->activity->track ) ) {
     1612                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1613        }
     1614
     1615        if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) {
     1616                return $action;
     1617        }
     1618
     1619        $user_link = bp_core_get_userlink( $activity->user_id );
     1620
     1621        if ( is_multisite() ) {
     1622                $blog_link = '<a href="' . esc_url( get_home_url( $activity->item_id ) ) . '">' . get_blog_option( $activity->item_id, 'blogname' ) . '</a>';
     1623
     1624                if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms ) ) {
     1625                        $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms, $user_link, $activity->primary_link, $blog_link );
     1626                } else {
     1627                        $action = sprintf( _x( '%1$s commented on the <a href="%2$s">item</a>, on the site %3$s', 'Activity Custom Post Type comment action', 'buddypress' ), $user_link, $activity->primary_link, $blog_link );
     1628                }
     1629        } else {
     1630                if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action ) ) {
     1631                        $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action, $user_link, $activity->primary_link );
     1632                } else {
     1633                        $action = sprintf( _x( '%1$s commented on the <a href="%2$s">item</a>', 'Activity Custom Post Type post comment action', 'buddypress' ), $user_link, $activity->primary_link );
     1634                }
     1635        }
     1636
     1637        /**
     1638         * Filters the formatted custom post type activity comment action string.
     1639         *
     1640         * @since 2.5.0
     1641         *
     1642         * @param string               $action   Activity action string value.
     1643         * @param BP_Activity_Activity $activity Activity item object.
     1644         */
     1645        return apply_filters( 'bp_activity_custom_post_type_comment_action', $action, $activity );
     1646}
     1647
    14081648/*
    14091649 * Business functions are where all the magic happens in BuddyPress. They will
    14101650 * handle the actual saving or manipulation of information. Usually they will
    function bp_activity_post_type_update( $post = null ) { 
    19932233         * Fires after the updating of an activity item for a custom post type entry.
    19942234         *
    19952235         * @since 2.2.0
     2236         * @since 2.5.0 Add the post type tracking args parameter
    19962237         *
    1997          * @param WP_Post              $post     Post object.
    1998          * @param BP_Activity_Activity $activity Activity object.
     2238         * @param WP_Post              $post                 Post object.
     2239         * @param BP_Activity_Activity $activity             Activity object.
     2240         * @param object               $activity_post_object The post type tracking args object.
    19992241         */
    2000         do_action( 'bp_activity_post_type_updated', $post, $activity );
     2242        do_action( 'bp_activity_post_type_updated', $post, $activity, $activity_post_object );
    20012243
    20022244        return $updated;
    20032245}
    function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) { 
    20532295}
    20542296
    20552297/**
     2298 * Create an activity item for a newly posted post type comment.
     2299 *
     2300 * @since 2.5.0
     2301 *
     2302 * @param  int  $comment_id  ID of the comment.
     2303 * @param  bool $is_approved Whether the comment is approved or not.
     2304 * @param  object $activity_post_object the post type tracking args object.
     2305 *
     2306 * @return int|bool The ID of the activity on success. False on error.
     2307 */
     2308function bp_activity_post_type_comment( $comment_id = 0, $is_approved = true, $activity_post_object = null ) {
     2309        // Get the users comment
     2310        $post_type_comment = get_comment( $comment_id );
     2311
     2312        // Don't record activity if the comment hasn't been approved
     2313        if ( empty( $is_approved ) ) {
     2314                return false;
     2315        }
     2316
     2317        // Don't record activity if no email address has been included
     2318        if ( empty( $post_type_comment->comment_author_email ) ) {
     2319                return false;
     2320        }
     2321
     2322        // Don't record activity if the comment has already been marked as spam
     2323        if ( 'spam' === $is_approved ) {
     2324                return false;
     2325        }
     2326
     2327        // Get the user by the comment author email.
     2328        $user = get_user_by( 'email', $post_type_comment->comment_author_email );
     2329
     2330        // If user isn't registered, don't record activity
     2331        if ( empty( $user ) ) {
     2332                return false;
     2333        }
     2334
     2335        // Get the user_id
     2336        $user_id = (int) $user->ID;
     2337
     2338        // Get blog and post data
     2339        $blog_id = get_current_blog_id();
     2340
     2341        // Get the post
     2342        $post_type_comment->post = get_post( $post_type_comment->comment_post_ID );
     2343
     2344        if ( ! is_a( $post_type_comment->post, 'WP_Post' ) ) {
     2345                return false;
     2346        }
     2347
     2348        /**
     2349         * Filters whether to publish activities about the comment regarding the post status
     2350         *
     2351         * @since 2.5.0
     2352         *
     2353         * @param bool true to bail, false otherwise.
     2354         */
     2355        $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 ) );
     2356
     2357        // If this is a password protected post, or not a public post don't record the comment
     2358        if ( $is_post_status_not_allowed ) {
     2359                return false;
     2360        }
     2361
     2362        // Set post type
     2363        $post_type = $post_type_comment->post->post_type;
     2364
     2365        if ( empty( $activity_post_object ) ) {
     2366                // Get the post type tracking args.
     2367                $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     2368
     2369                // Bail if the activity type does not exist
     2370                if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     2371                        return false;
     2372                }
     2373        }
     2374
     2375        // Set the $activity_comment_object
     2376        $activity_comment_object = $activity_post_object->comments_tracking;
     2377
     2378        /**
     2379         * Filters whether or not to post the activity about the comment.
     2380         *
     2381         * This is a variable filter, dependent on the post type,
     2382         * that lets components or plugins bail early if needed.
     2383         *
     2384         * @since 2.5.0
     2385         *
     2386         * @param bool $value      Whether or not to continue.
     2387         * @param int  $blog_id    ID of the current site.
     2388         * @param int  $post_id    ID of the current post being commented.
     2389         * @param int  $user_id    ID of the current user.
     2390         * @param int  $comment_id ID of the current comment being posted.
     2391         */
     2392        if ( false === apply_filters( "bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id ) ) {
     2393                return false;
     2394        }
     2395
     2396        // Is this an update ?
     2397        $activity_id = bp_activity_get_activity_id( array(
     2398                'user_id'           => $user_id,
     2399                'component'         => $activity_comment_object->component_id,
     2400                'type'              => $activity_comment_object->action_id,
     2401                'item_id'           => $blog_id,
     2402                'secondary_item_id' => $comment_id,
     2403        ) );
     2404
     2405        // Record this in activity streams.
     2406        $comment_link = get_comment_link( $post_type_comment->comment_ID );
     2407
     2408        // Backward compatibility filters for the 'blogs' component.
     2409        if ( 'blogs' == $activity_comment_object->component_id )  {
     2410                $activity_content      = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content',      array( $post_type_comment->comment_content, &$post_type_comment, $comment_link ) );
     2411                $activity_primary_link = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$post_type_comment ) );
     2412        } else {
     2413                $activity_content      = $post_type_comment->comment_content;
     2414                $activity_primary_link = $comment_link;
     2415        }
     2416
     2417        $activity_args = array(
     2418                'id'            => $activity_id,
     2419                'user_id'       => $user_id,
     2420                'content'       => $activity_content,
     2421                'primary_link'  => $activity_primary_link,
     2422                'component'     => $activity_comment_object->component_id,
     2423                'recorded_time' => $post_type_comment->comment_date_gmt,
     2424        );
     2425
     2426        if ( bp_disable_blogforum_comments() ) {
     2427                $blog_url = get_home_url( $blog_id );
     2428                $post_url = add_query_arg(
     2429                        'p',
     2430                        $post_type_comment->post->ID,
     2431                        trailingslashit( $blog_url )
     2432                );
     2433
     2434                $activity_args['type']              = $activity_comment_object->action_id;
     2435                $activity_args['item_id']           = $blog_id;
     2436                $activity_args['secondary_item_id'] = $post_type_comment->comment_ID;
     2437
     2438                if ( ! empty( $activity_args['content'] ) ) {
     2439                        // Create the excerpt.
     2440                        $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
     2441
     2442                        // Backward compatibility filter for blog comments.
     2443                        if ( 'blogs' == $activity_post_object->component_id )  {
     2444                                $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type );
     2445                        } else {
     2446                                $activity_args['content'] = $activity_summary;
     2447                        }
     2448                }
     2449
     2450                // Set up the action by using the format functions.
     2451                $action_args = array_merge( $activity_args, array(
     2452                        'post_title' => $post_type_comment->post->post_title,
     2453                        'post_url'   => $post_url,
     2454                        'blog_url'   => $blog_url,
     2455                        'blog_name'  => get_blog_option( $blog_id, 'blogname' ),
     2456                ) );
     2457
     2458                $activity_args['action'] = call_user_func_array( $activity_comment_object->format_callback, array( '', (object) $action_args ) );
     2459
     2460                // Make sure the action is set.
     2461                if ( empty( $activity_args['action'] ) ) {
     2462                        return;
     2463                } else {
     2464                        // Backward compatibility filter for the blogs component.
     2465                        if ( 'blogs' === $activity_post_object->component_id )  {
     2466                                $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
     2467                        }
     2468                }
     2469
     2470                $activity_id = bp_activity_add( $activity_args );
     2471        }
     2472
     2473        /**
     2474         * Fires after the publishing of an activity item for a newly published post type post.
     2475         *
     2476         * @since 2.5.0
     2477         *
     2478         * @param int        $activity_id          ID of the newly published activity item.
     2479         * @param WP_Comment $post_type_comment    Comment object.
     2480         * @param array      $activity_args        Array of activity arguments.
     2481         * @param object     $activity_post_object the post type tracking args object.
     2482         */
     2483        do_action_ref_array( 'bp_activity_post_type_comment', array( &$activity_id, $post_type_comment, $activity_args, $activity_post_object ) );
     2484
     2485        return $activity_id;
     2486}
     2487add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
     2488add_action( 'edit_comment', 'bp_activity_post_type_comment', 10    );
     2489
     2490/**
     2491 * Remove an activity item when a comment about a post type is deleted.
     2492 *
     2493 * @since 2.5.0
     2494 *
     2495 * @param  int    $comment_id           ID of the comment.
     2496 * @param  object $activity_post_object The post type tracking args object.
     2497 *
     2498 * @return bool True on success. False on error.
     2499 */
     2500function bp_activity_post_type_remove_comment( $comment_id = 0, $activity_post_object = null ) {
     2501        if ( empty( $activity_post_object ) ) {
     2502                $comment = get_comment( $comment_id );
     2503                if ( ! $comment ) {
     2504                        return;
     2505                }
     2506
     2507                $post_type = get_post_type( $comment->comment_post_ID );
     2508                if ( ! $post_type ) {
     2509                        return;
     2510                }
     2511
     2512                // Get the post type tracking args.
     2513                $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     2514
     2515                // Bail if the activity type does not exist
     2516                if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     2517                        return false;
     2518                }
     2519        }
     2520
     2521        // Set the $activity_comment_object
     2522        $activity_comment_object = $activity_post_object->comments_tracking;
     2523
     2524        if ( empty( $activity_comment_object->action_id ) ) {
     2525                return false;
     2526        }
     2527
     2528        $deleted = false;
     2529
     2530        if ( bp_disable_blogforum_comments() ) {
     2531                $deleted = bp_activity_delete_by_item_id( array(
     2532                        'item_id'           => get_current_blog_id(),
     2533                        'secondary_item_id' => $comment_id,
     2534                        'component'         => $activity_comment_object->component_id,
     2535                        'type'              => $activity_comment_object->action_id,
     2536                        'user_id'           => false,
     2537                ) );
     2538        }
     2539
     2540        /**
     2541         * Fires after the custom post type comment activity was removed.
     2542         *
     2543         * @since 2.5.0
     2544         *
     2545         * @param bool       $deleted              True if the activity was deleted false otherwise
     2546         * @param WP_Comment $comment              Comment object.
     2547         * @param object     $activity_post_object The post type tracking args object.
     2548         * @param string     $value                The post type comment activity type.
     2549         */
     2550        do_action( 'bp_activity_post_type_remove_comment', $deleted, $comment_id, $activity_post_object, $activity_comment_object->action_id );
     2551
     2552        return $deleted;
     2553}
     2554add_action( 'delete_comment', 'bp_activity_post_type_remove_comment', 10, 1 );
     2555
     2556/**
    20562557 * Add an activity comment.
    20572558 *
    20582559 * @since 1.2.0
     2560 * @since 2.5.0 Add a new possible parameter $skip_notification for the array of arguments.
     2561 *              Add the $primary_link parameter for the array of arguments.
    20592562 *
    20602563 * @uses wp_parse_args()
    20612564 * @uses bp_activity_add()
    function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) { 
    20652568 * @uses do_action() To call the 'bp_activity_comment_posted' hook.
    20662569 *
    20672570 * @param array|string $args {
    2068  *     @type int    $id          Optional. Pass an ID to update an existing comment.
    2069  *     @type string $content     The content of the comment.
    2070  *     @type int    $user_id     Optional. The ID of the user making the comment.
    2071  *                               Defaults to the ID of the logged-in user.
    2072  *     @type int    $activity_id The ID of the "root" activity item, ie the oldest
    2073  *                               ancestor of the comment.
    2074  *     @type int    $parent_id   Optional. The ID of the parent activity item, ie the item to
    2075  *                               which the comment is an immediate reply. If not provided,
    2076  *                               this value defaults to the $activity_id.
     2571 *     @type int    $id                Optional. Pass an ID to update an existing comment.
     2572 *     @type string $content           The content of the comment.
     2573 *     @type int    $user_id           Optional. The ID of the user making the comment.
     2574 *                                     Defaults to the ID of the logged-in user.
     2575 *     @type int    $activity_id       The ID of the "root" activity item, ie the oldest
     2576 *                                     ancestor of the comment.
     2577 *     @type int    $parent_id         Optional. The ID of the parent activity item, ie the item to
     2578 *                                     which the comment is an immediate reply. If not provided,
     2579 *                                     this value defaults to the $activity_id.
     2580 *     @type string $primary_link      Optional. the primary link for the comment.
     2581 *                                     Defaults to an empty string.
     2582 *     @type bool   $skip_notification Optional. false to send a comment notification, false otherwise.
     2583 *                                     Defaults to false.
    20772584 * }
    20782585 * @return int|bool The ID of the comment on success, otherwise false.
    20792586 */
    function bp_activity_new_comment( $args = '' ) { 
    20872594        }
    20882595
    20892596        $r = wp_parse_args( $args, array(
    2090                 'id'          => false,
    2091                 'content'     => false,
    2092                 'user_id'     => bp_loggedin_user_id(),
    2093                 'activity_id' => false, // ID of the root activity item.
    2094                 'parent_id'   => false  // ID of a parent comment (optional).
     2597                'id'                => false,
     2598                'content'           => false,
     2599                'user_id'           => bp_loggedin_user_id(),
     2600                'activity_id'       => false, // ID of the root activity item.
     2601                'parent_id'         => false, // ID of a parent comment (optional).
     2602                'primary_link'      => '',
     2603                'skip_notification' => false,
    20952604        ) );
    20962605
    20972606        // Bail if missing necessary data.
    function bp_activity_new_comment( $args = '' ) { 
    21382647                'content'           => $comment_content,
    21392648                'component'         => buddypress()->activity->id,
    21402649                'type'              => 'activity_comment',
     2650                'primary_link'      => $r['primary_link'],
    21412651                'user_id'           => $r['user_id'],
    21422652                'item_id'           => $activity_id,
    21432653                'secondary_item_id' => $r['parent_id'],
    function bp_activity_new_comment( $args = '' ) { 
    21562666        }
    21572667        wp_cache_delete( $activity_id, 'bp_activity' );
    21582668
    2159         /**
    2160          * Fires near the end of an activity comment posting, before the returning of the comment ID.
    2161          *
    2162          * @since 1.2.0
    2163          *
    2164          * @param int                  $comment_id ID of the newly posted activity comment.
    2165          * @param array                $r          Array of parsed comment arguments.
    2166          * @param BP_Activity_Activity $activity   Activity item being commented on.
    2167          */
    2168         do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
     2669        if ( empty( $r[ 'skip_notification' ] ) ) {
     2670                /**
     2671                 * Fires near the end of an activity comment posting, before the returning of the comment ID.
     2672                 * Sends a notification to the user @see bp_activity_new_comment_notification_helper().
     2673                 *
     2674                 * @since 1.2.0
     2675                 *
     2676                 * @param int   $comment_id ID of the newly posted activity comment.
     2677                 * @param array $r          Array of parsed comment arguments.
     2678                 * @param int   $activity   ID of the activity item being commented on.
     2679                 */
     2680                do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
     2681        } else {
     2682                /**
     2683                 * Fires near the end of an activity comment posting, before the returning of the comment ID.
     2684                 * without sending a notification to the user
     2685                 *
     2686                 * @since 2.5.0
     2687                 *
     2688                 * @param int   $comment_id ID of the newly posted activity comment.
     2689                 * @param array $r          Array of parsed comment arguments.
     2690                 * @param int   $activity   ID of the activity item being commented on.
     2691                 */
     2692                do_action( 'bp_activity_comment_posted_notification_skipped', $comment_id, $r, $activity );
     2693        }
    21692694
    21702695        if ( empty( $comment_id ) ) {
    21712696                $errors->add( 'comment_failed', $feedback );
    function bp_activity_delete( $args = '' ) { 
    24342959 * @return bool True on success, false on failure.
    24352960 */
    24362961function bp_activity_delete_comment( $activity_id, $comment_id ) {
     2962        $deleted = false;
    24372963
    24382964        /**
    24392965         * Filters whether BuddyPress should delete an activity comment or not.
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    24422968         * handle the deletion of child comments differently. Make sure you return false.
    24432969         *
    24442970         * @since 1.2.0
     2971         * @since 2.5.0 Add the deleted parameter (passed by reference)
    24452972         *
    24462973         * @param bool $value       Whether BuddyPress should continue or not.
    24472974         * @param int  $activity_id ID of the root activity item being deleted.
    24482975         * @param int  $comment_id  ID of the comment being deleted.
    24492976         */
    2450         if ( ! apply_filters( 'bp_activity_delete_comment_pre', true, $activity_id, $comment_id ) ) {
    2451                 return false;
     2977        if ( ! apply_filters_ref_array( 'bp_activity_delete_comment_pre', array( true, $activity_id, $comment_id, &$deleted ) ) ) {
     2978                return $deleted;
    24522979        }
    24532980
    24542981        // Delete any children of this comment.
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    24572984        // Delete the actual comment.
    24582985        if ( ! bp_activity_delete( array( 'id' => $comment_id, 'type' => 'activity_comment' ) ) ) {
    24592986                return false;
     2987        } else {
     2988                $deleted = true;
    24602989        }
    24612990
    24622991        // Purge comment cache for the root activity update.
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    24753004         */
    24763005        do_action( 'bp_activity_delete_comment', $activity_id, $comment_id );
    24773006
    2478         return true;
     3007        return $deleted;
    24793008}
    24803009
    24813010        /**
  • src/bp-activity/bp-activity-template.php

    diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php
    index 113d480..f3ab841 100644
    function bp_activity_can_comment() { 
    34253425        global $activities_template;
    34263426        $bp = buddypress();
    34273427
    3428         // Assume activity can be commented on.
    3429         $can_comment = true;
    3430 
    3431         // Determine ability to comment based on activity action name.
    3432         $activity_action = bp_get_activity_action_name();
    3433 
    3434         $turn_off = 0;
    3435         if ( ! empty( $activities_template->disable_blogforum_replies ) ) {
    3436                 $turn_off = 1;
    3437         }
     3428        // Determine ability to comment based on activity type name.
     3429        $activity_type = bp_get_activity_type();
    34383430
    3439         $maybe_turn_off = array_fill_keys( array(
    3440                 'new_blog_post',
    3441                 'new_blog_comment',
    3442                 'new_forum_topic',
    3443                 'new_forum_post',
    3444         ), $turn_off );
     3431        // Get the 'comment-reply' support for the current activity type.
     3432        $can_comment = bp_activity_type_supports( $activity_type, 'comment-reply' );
    34453433
    3446         $maybe_turn_off['activity_comment'] = 1;
    3447 
    3448         // Fetch all the tracked post types once.
    3449         if ( empty( $bp->activity->track ) ) {
    3450                 $bp->activity->track = bp_activity_get_post_types_tracking_args();
     3434        // Neutralize activity_comment.
     3435        if ( 'activity_comment' === $activity_type ) {
     3436                $can_comment = false;
    34513437        }
    34523438
    3453         foreach ( $bp->activity->track as $action => $tracking_args ) {
    3454                 if ( empty( $tracking_args->activity_comment ) ) {
    3455                         $maybe_turn_off[ $action ] = $turn_off;
    3456                 }
    3457         }
    3458 
    3459         $can_comment = empty( $maybe_turn_off[ $activity_action ] );
    3460 
    34613439        /**
    34623440         * Filters whether a comment can be made on an activity item.
    34633441         *
    34643442         * @since 1.5.0
     3443         * @since 2.5.0 Use $activity_type instead of $activity_name for the second parameter.
    34653444         *
    34663445         * @param bool   $can_comment     Status on if activity can be commented on.
    3467          * @param string $activity_action Current activity action being checked on.
     3446         * @param string $activity_type   Current activity type being checked on.
    34683447         */
    3469         return apply_filters( 'bp_activity_can_comment', $can_comment, $activity_action );
     3448        return apply_filters( 'bp_activity_can_comment', $can_comment, $activity_type );
    34703449}
    34713450
    34723451/**
  • src/bp-blogs/bp-blogs-activity.php

    diff --git src/bp-blogs/bp-blogs-activity.php src/bp-blogs/bp-blogs-activity.php
    index af93731..b822f50 100644
    defined( 'ABSPATH' ) || exit; 
    1818 * @return bool|null Returns false if activity component is not active.
    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' ),
    3326                        'bp_blogs_format_activity_action_new_blog',
    function bp_blogs_register_activity_actions() { 
    3730                );
    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.
    5535         *
    function bp_blogs_register_activity_actions() { 
    6040add_action( 'bp_register_activity_actions', 'bp_blogs_register_activity_actions' );
    6141
    6242/**
     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 );
     116
     117/**
    63118 * Format 'new_blog' activity actions.
    64119 *
    65120 * @since 2.0.0
    function bp_blogs_format_activity_action_new_blog_post( $action, $activity ) { 
    218273 * @return string Constructed activity action.
    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 ) ) {
    225307                $blog_url  = get_home_url( $activity->item_id );
    function bp_blogs_format_activity_action_new_blog_comment( $action, $activity ) 
    229311                bp_blogs_update_blogmeta( $activity->item_id, 'name', $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.
    236341        if ( empty( $post_url ) || empty( $post_title ) ) {
    add_filter( 'bp_activity_prefetch_object_data', 'bp_blogs_prefetch_activity_obje 
    333438 * @return int|bool On success, returns the activity ID. False on failure.
    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(),
    346443                'action'            => '',
    347444                'content'           => '',
    348445                'primary_link'      => '',
    349                 'component'         => $bp->blogs->id,
     446                'component'         => buddypress()->blogs->id,
    350447                'type'              => false,
    351448                'item_id'           => false,
    352449                'secondary_item_id' => false,
    function bp_blogs_record_activity( $args = '' ) { 
    410507 * @return bool True on success, false on failure.
    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,
    421512                'component'         => buddypress()->blogs->id,
    function bp_blogs_comments_open( $activity ) { 
    522613 *
    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.
    530622 * @param array  $params          Parameters for the activity comment.
    531623 * @param object $parent_activity Parameters of the parent activity item (in this case, the blog post).
    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        }
    538630
    function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 
    548640                $user = bp_core_get_core_userdata( $params['user_id'] );
    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
    558652        // Comment args.
    function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 
    565659                'comment_type'         => '', // Could be interesting to add 'buddypress' here...
    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.
    579669        switch_to_blog( $parent_activity->item_id );
    function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 
    589679        add_comment_meta( $post_comment_id, 'bp_activity_comment_id', $comment_id );
    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.
    595687        //
    function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 
    616708        restore_current_blog();
    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        /**
    622714         * Fires after activity comments have been synced and posted as blog comments.
    add_action( 'bp_activity_comment_posted', 'bp_blogs_sync_add_from_activity_comme 
    640732 * activity comment children before they are deleted.
    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        }
    655751
    function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_i 
    657753        $activity = bp_activity_get( array(
    658754                'in'               => $activity_id,
    659755                'display_comments' => 'stream',
     756                'spam'             => 'all',
    660757        ) );
    661758
    662759        // Get all activity comment IDs for the pending deleted item.
    function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_i 
    677774        // emulate bp_activity_delete_comment().
    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/**
    687787 * Updates the blog comment when the associated activity comment is edited.
    add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activit 
    691791 * @param BP_Activity_Activity $activity The activity object.
    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 );
    707802
    708         // Try to see if a corresponding blog comment exists.
    709         $post_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
     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        }
    710807
    711         if ( empty( $post_comment_id ) ) {
     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 ) {
    712812                return;
    713813        }
    714814
    715         // Fetch parent activity item.
    716         $parent_activity = new BP_Activity_Activity( $activity->item_id );
     815        // Try to see if a corresponding blog comment exists.
     816        $post_comment_id = bp_activity_get_meta( $activity->id, "bp_blogs_{$post_type}_comment_id" );
    717817
    718         // Sanity check.
    719         if ( 'new_blog_post' !== $parent_activity->type ) {
     818        if ( empty( $post_comment_id ) ) {
    720819                return;
    721820        }
    722821
    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();
    733854}
    add_action( 'trashed_post_comments', 'bp_blogs_remove_activity_meta_for_trashed_ 
    769890 * powers the 'Comments' filter in the activity directory dropdown) includes
    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().
    780897 * @return array $args
    function bp_blogs_new_blog_comment_query_backpat( $args ) { 
    783900        global $wpdb;
    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() ) {
     905                return $args;
     906        }
     907
     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' ) ) {
    788918                return $args;
    789919        }
    790920
    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 ) ) {
    795925                return $args;
    function bp_blogs_new_blog_comment_query_backpat( $args ) { 
    798928        // Init the filter query.
    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'] ) {
    804934                $filter_query = array(
    function bp_blogs_new_blog_comment_query_backpat( $args ) { 
    831961
    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/**
    841973 * Utility function to set up some variables for use in the activity loop.
    function bp_blogs_setup_activity_loop_globals( $activity ) { 
    857989                return;
    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        }
    864996
    add_action( 'bp_before_activity_comment', 'bp_blogs_setup_comment_loop_globals_o 
    9351067 * @return bool
    9361068 */
    9371069function bp_blogs_disable_activity_commenting( $retval ) {
     1070        global $activities_template;
     1071
    9381072        // If activity commenting is disabled, return current value.
    939         if ( bp_disable_blogforum_comments() ) {
     1073        if ( bp_disable_blogforum_comments() || ! isset( $activities_template->in_the_loop ) ) {
    9401074                return $retval;
    9411075        }
    9421076
    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;
     1077        $type = bp_get_activity_type();
    9571078
     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' ) ) {
    9581083                        // Setup some globals we'll need to reference later.
    9591084                        bp_blogs_setup_activity_loop_globals( $activities_template->activity );
    9601085
    9611086                        // If comments are closed for the WP blog post, we should disable
    9621087                        // activity comments for this activity entry.
    963                         if ( empty( buddypress()->blogs->allow_comments[bp_get_activity_id()] ) ) {
     1088                        if ( empty( buddypress()->blogs->allow_comments[ bp_get_activity_id() ] ) ) {
    9641089                                $retval = false;
    9651090                        }
    966 
    967                         break;
     1091                // The activity type does not support comments or replies
     1092                } else {
     1093                        $retval = false;
     1094                }
    9681095        }
    9691096
    9701097        return $retval;
    function bp_blogs_disable_activity_commenting( $retval ) { 
    9721099add_filter( 'bp_activity_can_comment', 'bp_blogs_disable_activity_commenting' );
    9731100
    9741101/**
     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         */
     1118        if ( bp_disable_blogforum_comments() ) {
     1119                return $retval;
     1120        }
     1121
     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;
     1131        }
     1132
     1133        return $retval;
     1134}
     1135add_filter( 'bp_activity_get_comment_count', 'bp_blogs_post_type_comments_avoid_duplicates' );
     1136
     1137/**
    9751138 * Check if an activity comment associated with a blog post can be replied to.
    9761139 *
    9771140 * By default, disables replying to activity comments if the corresponding WP
    function bp_blogs_activity_comment_single_permalink( $retval, $activity ) { 
    10571220                return $retval;
    10581221        }
    10591222
    1060         $blog_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
     1223        if ( bp_disable_blogforum_comments() ) {
     1224                return $retval;
     1225        }
     1226
     1227        $parent_activity = new BP_Activity_Activity( $activity->item_id );
    10611228
    1062         if ( ! empty( $blog_comment_id ) ) {
     1229        if ( isset( $parent_activity->type ) && bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ) ) {
    10631230                $retval = $activity->primary_link;
    10641231        }
    10651232
    function bp_blogs_activity_comment_single_action( $retval, $activity ) { 
    10831250                return $retval;
    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 );
     1272                $bp = buddypress();
    10911273
    1092                 // Fake a 'new_blog_comment' activity object.
    1093                 $object = $activity;
     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' );
    10941276
    1095                 // Override 'item_id' to use blog ID.
    1096                 $object->item_id = $parent_blog_post_activity->item_id;
     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;
    10971281
    1098                 // Override 'secondary_item_id' to use comment ID.
    1099                 $object->secondary_item_id = $blog_comment_id;
     1282                        // Set the type of the activity to be a comment about a post type
     1283                        $object->type = $comment_action_id;
    11001284
    1101                 // Now format the activity action using the 'new_blog_comment' action callback.
    1102                 $retval = bp_blogs_format_activity_action_new_blog_comment( '', $object );
     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
    11051301        return $retval;
  • src/bp-blogs/bp-blogs-filters.php

    diff --git src/bp-blogs/bp-blogs-filters.php src/bp-blogs/bp-blogs-filters.php
    index 5cb6a2f..bfd96f0 100644
    function bp_blogs_comments_clauses_select_by_id( $retval ) { 
    6161}
    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()`.
    6767 *
    function bp_blogs_post_pre_publish( $return = true, $blog_id = 0, $post_id = 0, 
    121121        return $return;
    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 );
  • src/bp-blogs/bp-blogs-functions.php

    diff --git src/bp-blogs/bp-blogs-functions.php src/bp-blogs/bp-blogs-functions.php
    index 02e3477..a608107 100644
    add_action( 'bp_activity_post_type_published', 'bp_blogs_publish_post_activity_m 
    477477 * Updates a blog post's activity meta entry during a post edit.
    478478 *
    479479 * @since 2.2.0
     480 * @since 2.5.0 Add the post type tracking args object parameter
    480481 *
    481  * @param WP_Post              $post     Post object.
    482  * @param BP_Activity_Activity $activity Activity object.
     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.
    483485 */
    484 function bp_blogs_update_post_activity_meta( $post, $activity ) {
    485         if ( empty( $activity->id ) || 'post' != $post->post_type ) {
     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        }
    488490
    function bp_blogs_update_post_activity_meta( $post, $activity ) { 
    491493        if ( $post->post_title !== $existing_title ) {
    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' );
    520                         }
    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';
     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                                }
    531524
    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                         }
     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                                }
    536539
    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 );
     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                                        }
    541545                                }
    542                         }
    543546
    544                         unset( $activities, $activity_ids, $comment_ids, $comments );
     547                                unset( $activities, $activity_ids, $comment_ids, $comments );
     548                        }
    545549                }
    546550        }
    547551
    function bp_blogs_update_post_activity_meta( $post, $activity ) { 
    552556                bp_activity_delete_meta( $activity->id, 'post_comment_status' );
    553557        }
    554558}
    555 add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 2 );
     559add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 3 );
    556560
    557561/**
    558  * Record a new blog comment in the BuddyPress activity stream.
     562 * Update Activity and blogs meta and eventually sync comment with activity comment
    559563 *
    560  * Only posts the item if blog is public and post is not password-protected.
     564 * @since  2.5.0
    561565 *
    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 * @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.
    566571 */
    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 ) )
     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;
     575        }
    579576
    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.
     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 );
     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        }
    646586
    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( '/' ) ) );
     587        // Sync comment - activity comment
     588        if ( ! bp_disable_blogforum_comments() ) {
    650589
    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 );
     590                if ( ! empty( $_REQUEST['action'] ) ) {
     591                        $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
    658592
    659                                 if ( ! empty( $existing_activity_id ) ) {
    660                                         $args['id'] = $existing_activity_id;
    661                                 }
     593                        if ( ! empty( $existing_activity_id ) ) {
     594                                $activity_args['id'] = $existing_activity_id;
    662595                        }
     596                }
     597
     598                if ( empty( $activity_post_object ) ) {
     599                        $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type );
     600                }
    663601
    664                         // Find the parent 'new_blog_post' activity entry.
     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                        }
     615                }
    676616
    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;
     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;
    682622
    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                                 }
     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 );
     627                        }
    688628
    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                                 }
     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                        }
    694634
    695                                 $args['secondary_item_id'] = $comment_parent;
    696                                 $args['component']         = 'activity';
    697                                 $args['type']              = 'activity_comment';
     635                        $activity_args['parent_id']         = $comment_parent;
     636                        $activity_args['skip_notification'] = true;
    698637
    699                         // Could not find corresponding parent activity entry
    700                         // so wipe out $args array.
    701                         } else {
    702                                 $args = array();
    703                         }
     638                // could not find corresponding parent activity entry
     639                // so wipe out $args array
     640                } else {
     641                        $activity_args = array();
     642                }
    704643
    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 );
     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                }
     662        }
    726663
    727                 // Update the blogs last active date.
    728                 bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
     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() );
    729678        }
    730679
    731         return $recorded_comment;
     680        return $activity_id;
    732681}
    733 add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    734 add_action( 'edit_comment', 'bp_blogs_record_comment', 10    );
     682add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 );
    735683
    736684/**
    737685 * Record a user's association with a blog.
    function bp_blogs_remove_post( $post_id, $blog_id = 0, $user_id = 0 ) { 
    984932add_action( 'delete_post', 'bp_blogs_remove_post' );
    985933
    986934/**
    987  * Remove a blog comment activity item from the activity stream.
     935 * Remove a synced activity comment from the activity stream.
    988936 *
    989  * @param int $comment_id ID of the comment to be removed.
     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.
    990945 */
    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.
     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,
    1017960                                'display_comments' => 'stream',
    1018961                                'spam'             => 'all',
    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(
    1029972                                                'id' => $activity_id
    1030973                                        ) );
    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() );
     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;
    10521003}
    1053 add_action( 'delete_comment', 'bp_blogs_remove_comment' );
     1004add_action( 'bp_activity_post_type_remove_comment', 'bp_blogs_post_type_remove_comment', 10, 4 );
    10541005
    10551006/**
    10561007 * Removes blog comments that are associated with activity comments.
    10571008 *
    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 *
    10631014 * @param array $activity_ids The activity IDs to check association with blog
    function bp_blogs_remove_associated_blog_comments( $activity_ids = array(), $for 
    10941045}
    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 *
    11811050 * @return int $count Total blog count.
  • src/bp-blogs/bp-blogs-loader.php

    diff --git src/bp-blogs/bp-blogs-loader.php src/bp-blogs/bp-blogs-loader.php
    index 67d84db..274bc5f 100644
    class BP_Blogs_Component extends BP_Component { 
    106106                                add_post_type_support( $post_type, 'buddypress-activity' );
    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
    114111        /**
    class BP_Blogs_Component extends BP_Component { 
    128125                        'classes',
    129126                        'template',
    130127                        'filters',
    131                         'activity',
    132128                        'functions',
    133129                );
    134130
     131                if ( bp_is_active( 'activity' ) ) {
     132                        $includes[] = 'activity';
     133                }
     134
    135135                if ( is_multisite() ) {
    136136                        $includes[] = 'widgets';
    137137                }
    class BP_Blogs_Component extends BP_Component { 
    300300
    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}
    348304
    349305/**
  • src/bp-core/deprecated/1.6.php

    diff --git src/bp-core/deprecated/1.6.php src/bp-core/deprecated/1.6.php
    index 626d31b..c5550b6 100644
    function bp_core_is_user_spammer( $user_id = 0 ) { 
    8484
    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 ) {
    9090        _deprecated_function( __FUNCTION__, '1.6', 'No longer used' );
  • src/bp-core/deprecated/2.5.php

    diff --git src/bp-core/deprecated/2.5.php src/bp-core/deprecated/2.5.php
    index f441def..c3246df 100644
    function bp_core_deprecated_email_actions( $email, $delivery_status ) { 
    883883        }
    884884}
    885885add_action( 'bp_sent_email', 'bp_core_deprecated_email_actions', 20, 2 );
     886
     887/**
     888 * When a blog comment status transition occurs, update the relevant activity's status.
     889 *
     890 * @since 1.6.0
     891 * @deprecated 2.5.0
     892 *
     893 * @param string $new_status New comment status.
     894 * @param string $old_status Previous comment status.
     895 * @param object $comment Comment data.
     896 */
     897function bp_blogs_transition_activity_status( $new_status, $old_status, $comment ) {
     898        _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_transition_post_type_comment_status()' );
     899        bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment );
     900}
     901
     902/**
     903 * Record a new blog comment in the BuddyPress activity stream.
     904 *
     905 * Only posts the item if blog is public and post is not password-protected.
     906 *
     907 * @deprecated 2.5.0
     908 *
     909 * @param int $comment_id ID of the comment being recorded.
     910 * @param bool|string $is_approved Optional. The $is_approved value passed to
     911 *        the 'comment_post' action. Default: true.
     912 * @return bool|object Returns false on failure, the comment object on success.
     913 */
     914function bp_blogs_record_comment( $comment_id, $is_approved = true ) {
     915        _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_comment()' );
     916        bp_activity_post_type_comment( $comment_id, $is_approved );
     917}
     918
     919/**
     920 * Remove a blog comment activity item from the activity stream.
     921 *
     922 * @deprecated 2.5.0
     923 *
     924 * @param int $comment_id ID of the comment to be removed.
     925 */
     926function bp_blogs_remove_comment( $comment_id ) {
     927        _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_remove_comment()' );
     928        bp_activity_post_type_remove_comment( $comment_id );
     929}
     930
  • tests/phpunit/includes/testcase.php

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index a75009a..1e8bcb4 100644
    class BP_UnitTestCase extends WP_UnitTestCase { 
    4949
    5050                // Clean up after autocommits.
    5151                add_action( 'bp_blogs_recorded_existing_blogs', array( $this, 'set_autocommit_flag' ) );
     52
     53                // Make sure Activity actions are reset before each test
     54                $this->reset_bp_activity_actions();
     55
     56                // Make sure all Post types activities globals are reset before each test
     57                $this->reset_bp_activity_post_types_globals();
    5258        }
    5359
    5460        public function tearDown() {
    class BP_UnitTestCase extends WP_UnitTestCase { 
    97103                parent::clean_up_global_scope();
    98104        }
    99105
     106        protected function reset_bp_activity_actions() {
     107                buddypress()->activity->actions = new stdClass();
     108
     109                /**
     110                 * Populate the global with default activity actions only
     111                 * before each test.
     112                 */
     113                do_action( 'bp_register_activity_actions' );
     114        }
     115
     116        protected function reset_bp_activity_post_types_globals() {
     117                global $wp_post_types;
     118
     119                // Remove all remaining tracking arguments to each post type
     120                foreach ( $wp_post_types as $post_type => $post_type_arg ) {
     121                        if ( post_type_supports( $post_type, 'buddypress-activity' ) ) {
     122                                remove_post_type_support( $post_type, 'buddypress-activity' );
     123                        }
     124
     125                        if ( isset( $post_type_arg->bp_activity ) ) {
     126                                unset( $post_type_arg->bp_activity );
     127                        }
     128                }
     129
     130                buddypress()->activity->track = array();
     131        }
     132
    100133        function assertPreConditions() {
    101134                parent::assertPreConditions();
    102135
  • tests/phpunit/testcases/activity/actions.php

    diff --git tests/phpunit/testcases/activity/actions.php tests/phpunit/testcases/activity/actions.php
    index d1ae6df..5549607 100644
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    99         * @group activity_tracking
    1010         */
    1111        public function test_bp_activity_catch_transition_post_type_status_publish() {
    12                 $bp = buddypress();
    13 
    1412                register_post_type( 'foo', array(
    1513                        'label'   => 'foo',
    1614                        'public'   => true,
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    2826                $this->assertTrue( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Published post type should have activity' );
    2927
    3028                _unregister_post_type( 'foo' );
    31 
    32                 // Reset globals
    33                 unset( $bp->activity->actions->activity->new_foo );
    34                 $bp->activity->track = array();
    3529        }
    3630
    3731        /**
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    3933         * @group activity_tracking
    4034         */
    4135        public function test_bp_activity_catch_transition_post_type_status_publish_to_publish() {
    42                 $bp = buddypress();
    43 
    4436                register_post_type( 'foo', array(
    4537                        'label'   => 'foo',
    4638                        'public'   => true,
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    6860                $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Updating a post type should not create a new activity' );
    6961
    7062                _unregister_post_type( 'foo' );
    71 
    72                 // Reset globals
    73                 unset( $bp->activity->actions->activity->new_foo );
    74                 $bp->activity->track = array();
    7563        }
    7664
    7765        /**
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    7967         * @group activity_tracking
    8068         */
    8169        public function test_bp_activity_catch_transition_post_type_status_publish_password() {
    82                 $bp = buddypress();
    83 
    8470                register_post_type( 'foo', array(
    8571                        'label'   => 'foo',
    8672                        'public'   => true,
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    10692                $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Password protected post type should not have activity' );
    10793
    10894                _unregister_post_type( 'foo' );
    109 
    110                 // Reset globals
    111                 unset( $bp->activity->actions->activity->new_foo );
    112                 $bp->activity->track = array();
    11395        }
    11496
    11597        /**
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    11799         * @group activity_tracking
    118100         */
    119101        public function test_bp_activity_catch_transition_post_type_status_publish_trash() {
    120                 $bp = buddypress();
    121 
    122102                register_post_type( 'foo', array(
    123103                        'label'   => 'foo',
    124104                        'public'   => true,
    class BP_Tests_Activity_Actions extends BP_UnitTestCase { 
    141121                $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Unpublished post type should not have activity' );
    142122
    143123                _unregister_post_type( 'foo' );
    144 
    145                 // Reset globals
    146                 unset( $bp->activity->actions->activity->new_foo );
    147                 $bp->activity->track = array();
    148124        }
    149125
    150126        protected function activity_exists_for_post( $post_id, $action ) {
  • tests/phpunit/testcases/activity/functions.php

    diff --git tests/phpunit/testcases/activity/functions.php tests/phpunit/testcases/activity/functions.php
    index 8a3304f..273d926 100644
    Bar!'; 
    763763                        return;
    764764                }
    765765
    766                 $bp = buddypress();
    767 
    768766                register_post_type( 'foo', array(
    769767                        'label'   => 'foo',
    770768                        'public'   => true,
    Bar!'; 
    800798                $this->assertSame( $expected, $a_obj->action );
    801799
    802800                _unregister_post_type( 'foo' );
    803 
    804                 // Reset globals
    805                 unset( $bp->activity->actions->activity->new_foo );
    806                 $bp->activity->track = array();
    807801        }
    808802
    809803        /**
    Bar!'; 
    816810                        return;
    817811                }
    818812
    819                 $bp = buddypress();
    820 
    821813                $b = $this->factory->blog->create();
    822814                $u = $this->factory->user->create();
    823815
    Bar!'; 
    863855                $expected = sprintf( '%s wrote a new %s, on the site %s', $user_link, $post_link, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
    864856
    865857                $this->assertSame( $expected, $a_obj->action );
    866 
    867                 // Reset globals
    868                 unset( $bp->activity->actions->activity->new_foo );
    869                 $bp->activity->track = array();
    870858        }
    871859
    872860        /**
    Bar!'; 
    874862         * @group bp_activity_get_actions
    875863         */
    876864        public function test_bp_activity_get_actions_should_sort_by_position() {
    877                 $old_actions = bp_activity_get_actions();
    878865                buddypress()->activity->actions = new stdClass;
    879866
    880867                register_post_type( 'foo5', array(
    Bar!'; 
    916903                );
    917904                $foo_actions = (array) $actions->foo;
    918905                $this->assertEquals( $expected, array_values( wp_list_pluck( $foo_actions, 'key' ) ) );
    919 
    920                 buddypress()->activity->actions = $old_actions;
    921906        }
    922907
    923908        /**
    Bar!'; 
    929914                        return;
    930915                }
    931916
    932                 $bp = buddypress();
    933 
    934917                $labels = array(
    935918                        'name'                 => 'bars',
    936919                        'singular_name'        => 'bar',
    Bar!'; 
    974957                $this->assertSame( $expected, $a_obj->action );
    975958
    976959                _unregister_post_type( 'foo' );
    977 
    978                 // Reset globals
    979                 unset( $bp->activity->actions->activity->foo_bar );
    980                 $bp->activity->track = array();
    981960        }
    982961
    983962        /**
    Bar!'; 
    990969                        return;
    991970                }
    992971
    993                 $bp = buddypress();
    994                 $reset = $bp->activity->actions;
    995 
    996972                $b = $this->factory->blog->create();
    997973                $u = $this->factory->user->create();
    998974
    Bar!'; 
    10411017                $expected = sprintf( '%1$s shared a new <a href="%2$s">bar</a>, on the site %3$s', $user_link, $post_url, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
    10421018
    10431019                $this->assertSame( $expected, $a_obj->action );
    1044 
    1045                 // Reset globals
    1046                 unset( $bp->activity->actions->activity->new_foo );
    1047                 $bp->activity->track = array();
    10481020        }
    10491021
    10501022        /**
    Bar!'; 
    10811053                $this->assertSame( $bp->blogs->id, $a['activities'][0]->component );
    10821054
    10831055                remove_post_type_support( 'page', 'buddypress-activity' );
    1084 
    1085                 // Reset globals
    1086                 unset( $bp->activity->actions->blogs->new_page );
    1087                 $bp->activity->track = array();
    10881056        }
    10891057
    10901058        /**
    Bar!'; 
    11261094        }
    11271095
    11281096        /**
     1097         * @group activity_action
     1098         * @group bp_activity_format_activity_action_custom_post_type_post_ms
     1099         * @group post_type_comment_activities
     1100         */
     1101        public function test_bp_activity_format_activity_action_custom_post_type_comment() {
     1102                if ( is_multisite() ) {
     1103                        $b = $this->factory->blog->create();
     1104                        switch_to_blog( $b );
     1105                        add_filter( 'comment_flood_filter', '__return_false' );
     1106                } else {
     1107                        $b = get_current_blog_id();
     1108                }
     1109
     1110                $u = $this->factory->user->create();
     1111                $userdata = get_userdata( $u );
     1112
     1113                $labels = array(
     1114                        'name'                       => 'bars',
     1115                        'singular_name'              => 'bar',
     1116                        'bp_activity_new_comment'    => __( '%1$s commented on the <a href="%2$s">bar</a>', 'buddypress' ),
     1117                        'bp_activity_new_comment_ms' => __( '%1$s commented on the <a href="%2$s">bar</a>, on the site %3$s', 'buddypress' ),
     1118                );
     1119
     1120                register_post_type( 'foo', array(
     1121                        'labels'   => $labels,
     1122                        'public'   => true,
     1123                        'supports' => array( 'buddypress-activity', 'comments' ),
     1124                        'bp_activity' => array(
     1125                                'action_id'         => 'new_bar',
     1126                                'comment_action_id' => 'new_bar_comment',
     1127                        ),
     1128                ) );
     1129
     1130                // Build the actions to fetch the tracking args
     1131                bp_activity_get_actions();
     1132
     1133                $p = $this->factory->post->create( array(
     1134                        'post_author' => $u,
     1135                        'post_type'   => 'foo',
     1136                ) );
     1137
     1138                $c = wp_new_comment( array(
     1139                        'comment_post_ID'      => $p,
     1140                        'comment_author'       => $userdata->user_nicename,
     1141                        'comment_author_url'   => 'http://buddypress.org',
     1142                        'comment_author_email' => $userdata->user_email,
     1143                        'comment_content'      => 'this is a blog comment',
     1144                        'comment_type'         => '',
     1145                        'comment_parent'       => 0,
     1146                        'user_id'              => $u,
     1147                ) );
     1148
     1149                $a = bp_activity_get_activity_id( array( 'type' => 'new_bar_comment' ) );
     1150
     1151                $a_obj = new BP_Activity_Activity( $a );
     1152
     1153                $user_link    = bp_core_get_userlink( $u );
     1154                $comment_url  = get_comment_link( $c );
     1155
     1156                _unregister_post_type( 'foo' );
     1157
     1158                if ( is_multisite() ) {
     1159                        $blog_url  = get_blog_option( $a_obj->item_id, 'home' );
     1160                        restore_current_blog();
     1161                        remove_filter( 'comment_flood_filter', '__return_false' );
     1162
     1163                        $expected = sprintf( $labels['bp_activity_new_comment_ms'], $user_link, $comment_url, '<a href="' . $blog_url . '">' . get_blog_option( $a_obj->item_id, 'blogname' ) . '</a>' );
     1164                } else {
     1165                        $expected = sprintf( $labels['bp_activity_new_comment'], $user_link, $comment_url );
     1166                }
     1167
     1168                $this->assertSame( $expected, $a_obj->action );
     1169        }
     1170
     1171        /**
    11291172         * @group bp_activity_new_comment
    11301173         * @group cache
    11311174         */
  • tests/phpunit/testcases/activity/template.php

    diff --git tests/phpunit/testcases/activity/template.php tests/phpunit/testcases/activity/template.php
    index db33cda..090701a 100644
    class BP_Tests_Activity_Template extends BP_UnitTestCase { 
    909909        /**
    910910         * @group filter_query
    911911         * @group BP_Activity_Query
     912         * @group post_type_comment_activities
    912913         */
    913914        function test_bp_has_activities_with_filter_query_compare_regex() {
    914915                $u1 = $this->factory->user->create();
    class BP_Tests_Activity_Template extends BP_UnitTestCase { 
    12991300
    13001301        /**
    13011302         * @group bp_has_activities
     1303         * @group post_type_comment_activities
    13021304         */
    13031305        public function test_bp_has_activities_with_type_new_blog_comments() {
    13041306                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
    class BP_Tests_Activity_Template extends BP_UnitTestCase { 
    14301432        }
    14311433
    14321434        /**
     1435         * @group bp_activity_can_comment
     1436         */
     1437        public function test_bp_activity_can_comment() {
     1438                global $activities_template;
     1439                $reset_activities_template = $activities_template;
     1440
     1441                $activities_template = new stdClass;
     1442                $activities_template->disable_blogforum_replies = true;
     1443                $activities_template->activity = (object) array( 'type' => 'activity_comment' );
     1444
     1445                $this->assertFalse( bp_activity_can_comment(), 'bp_activity_can_comment() should return false if the activity type is activity_comment' );
     1446
     1447                $types = array(
     1448                        'new_blog_post',
     1449                        'new_blog_comment',
     1450                        'new_forum_topic',
     1451                        'new_forum_post'
     1452                );
     1453
     1454                foreach ( $types as $type_false ) {
     1455                        $activities_template->activity->type = $type_false;
     1456                        $this->assertFalse( bp_activity_can_comment(), 'Comments about blog or forum posts/replies are disabled' );
     1457                }
     1458
     1459                $activities_template->disable_blogforum_replies = false;
     1460                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     1461
     1462                foreach ( $types as $type_true ) {
     1463                        $activities_template->activity->type = $type_true;
     1464                        $this->assertTrue( bp_activity_can_comment(), 'Comments about blog or forum posts/replies are enabled' );
     1465                }
     1466
     1467                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     1468
     1469                // clean up!
     1470                $activities_template = $reset_activities_template;
     1471        }
     1472
     1473        /**
     1474         * @group bp_activity_can_comment
     1475         */
     1476        public function test_bp_activity_can_comment_post_type_activity() {
     1477                global $activities_template;
     1478                $bp = buddypress();
     1479
     1480                $reset_activities_template = $activities_template;
     1481                $reset_activity_track = $bp->activity->track;
     1482
     1483                $activities_template = new stdClass;
     1484                $activities_template->disable_blogforum_replies = true;
     1485
     1486                register_post_type( 'foo', array(
     1487                        'label'   => 'foo',
     1488                        'public'   => true,
     1489                        'supports' => array( 'buddypress-activity' ),
     1490                ) );
     1491
     1492                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1493
     1494                $activities_template->activity = (object) array( 'type' => 'new_foo' );
     1495
     1496                $this->assertTrue( bp_activity_can_comment(), 'If post type does not support comments, a post type activity can be commented' );
     1497
     1498                add_post_type_support( 'foo', 'comments' );
     1499
     1500                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1501
     1502                $this->assertFalse( bp_activity_can_comment(), 'If post type support comments, a post type activity cannot be commented' );
     1503
     1504                $bp_activity_support = (array) $bp->activity->track['new_foo'];
     1505                $bp_activity_support['activity_comment'] = true;
     1506
     1507                bp_activity_set_post_type_tracking_args( 'foo', $bp_activity_support );
     1508                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1509
     1510                $this->assertTrue( bp_activity_can_comment(), 'If post type supports activity comments, a post type activity can be commented' );
     1511
     1512                // clean up!
     1513                $activities_template = $reset_activities_template;
     1514                $bp->activity->track = $reset_activity_track;
     1515        }
     1516
     1517        /**
    14331518         * @group bp_activity_has_more_items
    14341519         */
    14351520        public function test_bp_activity_has_more_items_no_count_total_false() {
  • tests/phpunit/testcases/blogs/activity.php

    diff --git tests/phpunit/testcases/blogs/activity.php tests/phpunit/testcases/blogs/activity.php
    index 08941e2..d7595ef 100644
    class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 
    156156        /**
    157157         * @group activity_action
    158158         * @group bp_blogs_format_activity_action_new_blog_comment
     159         * @group post_type_comment_activities
    159160         */
    160161        public function test_bp_blogs_format_activity_action_new_blog_comment_ms_nonrootblog() {
    161162                if ( ! is_multisite() ) {
    class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 
    318319                        return;
    319320                }
    320321
    321                 $bp = buddypress();
    322                 $activity_actions = $bp->activity->actions;
    323                 $bp->activity->actions = new stdClass();
     322                buddypress()->activity->actions = new stdClass();
    324323
    325324                $u = $this->factory->user->create();
    326325                $p = wp_insert_post( array(
    class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 
    346345                $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
    347346
    348347                $this->assertSame( $expected, $a_obj['activities'][0]->action );
    349 
    350                 // Reset activity actions
    351                 $bp->activity->actions = $activity_actions;
    352                 $bp->activity->track = array();
    353348        }
    354349
    355350        /**
    class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 
    361356                        return;
    362357                }
    363358
    364                 $bp = buddypress();
    365                 $activity_actions = $bp->activity->actions;
    366                 $bp->activity->actions = new stdClass();
     359                buddypress()->activity->actions = new stdClass();
    367360
    368361                $u = $this->factory->user->create();
    369362                $p = wp_insert_post( array(
    class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 
    407400                ) );
    408401
    409402                $this->assertSame( $expected, $a_obj['activities'][0]->action );
     403        }
     404
     405        /**
     406         * @group bp_blogs_sync_add_from_activity_comment
     407         * @group post_type_comment_activities
     408         */
     409        public function test_bp_blogs_sync_add_from_activity_comment() {
     410                $old_user = get_current_user_id();
     411                $u = $this->factory->user->create();
     412                $this->set_current_user( $u );
     413                $userdata = get_userdata( $u );
     414
     415                // let's use activity comments instead of single "new_blog_comment" activity items
     416                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     417
     418                // create the blog post
     419                $post_id = $this->factory->post->create( array(
     420                        'post_status' => 'publish',
     421                        'post_type'   => 'post',
     422                        'post_title'  => 'Test activity comment to post comment',
     423                ) );
     424
     425                // grab the activity ID for the activity comment
     426                $a1 = bp_activity_get_activity_id( array(
     427                        'type'      => 'new_blog_post',
     428                        'component' => buddypress()->blogs->id,
     429                        'filter'    => array(
     430                                'item_id' => get_current_blog_id(),
     431                                'secondary_item_id' => $post_id
     432                        ),
     433                ) );
     434
     435                $a2 = bp_activity_new_comment( array(
     436                        'content'     => 'this content shoud be in a new post comment',
     437                        'user_id'     => $u,
     438                        'activity_id' => $a1,
     439                ) );
     440
     441                $approved_comments = get_approved_comments( $post_id );
     442                $comment = reset( $approved_comments );
     443
     444                $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
     445                $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
     446
     447                // reset
     448                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     449
     450                $this->set_current_user( $old_user );
     451        }
     452
     453        /**
     454         * @group bp_blogs_sync_delete_from_activity_comment
     455         * @group post_type_comment_activities
     456         */
     457        public function test_bp_blogs_sync_delete_from_activity_comment() {
     458                $old_user = get_current_user_id();
     459                $u = $this->factory->user->create();
     460                $this->set_current_user( $u );
     461                $userdata = get_userdata( $u );
     462
     463                // let's use activity comments instead of single "new_blog_comment" activity items
     464                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     465
     466                // create the blog post
     467                $post_id = $this->factory->post->create( array(
     468                        'post_status' => 'publish',
     469                        'post_type'   => 'post',
     470                        'post_title'  => 'Test activity comment to post comment',
     471                ) );
     472
     473                // grab the activity ID for the activity comment
     474                $a1 = bp_activity_get_activity_id( array(
     475                        'type'      => 'new_blog_post',
     476                        'component' => buddypress()->blogs->id,
     477                        'filter'    => array(
     478                                'item_id' => get_current_blog_id(),
     479                                'secondary_item_id' => $post_id
     480                        ),
     481                ) );
     482
     483                $a2 = bp_activity_new_comment( array(
     484                        'content'     => 'the generated comment should be deleted once the activity comment is removed',
     485                        'user_id'     => $u,
     486                        'activity_id' => $a1,
     487                ) );
     488
     489                bp_activity_delete_comment( $a1, $a2 );
     490
     491                $post_comments = get_comments( array( 'post_id' => $post_id ) );
     492
     493                $this->assertEmpty( $post_comments, 'A post comment should be deleted when the corresponding activity is' );
     494
     495                // reset
     496                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     497
     498                $this->set_current_user( $old_user );
     499        }
     500
     501        /**
     502         * @group bp_blogs_sync_activity_edit_to_post_comment
     503         * @group post_type_comment_activities
     504         */
     505        public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_unspam_activity_comment() {
     506                $old_user = get_current_user_id();
     507                $u = $this->factory->user->create();
     508                $this->set_current_user( $u );
     509                $userdata = get_userdata( $u );
     510
     511                // let's use activity comments instead of single "new_blog_comment" activity items
     512                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     513
     514                // create the blog post
     515                $post_id = $this->factory->post->create( array(
     516                        'post_status' => 'publish',
     517                        'post_type'   => 'post',
     518                        'post_title'  => 'Test activity comment to post comment',
     519                ) );
     520
     521                // grab the activity ID for the activity comment
     522                $a1 = bp_activity_get_activity_id( array(
     523                        'type'      => 'new_blog_post',
     524                        'component' => buddypress()->blogs->id,
     525                        'filter'    => array(
     526                                'item_id' => get_current_blog_id(),
     527                                'secondary_item_id' => $post_id
     528                        ),
     529                ) );
     530
     531                $a2 = bp_activity_new_comment( array(
     532                        'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
     533                        'user_id'     => $u,
     534                        'activity_id' => $a1,
     535                ) );
     536
     537                $activity = new BP_Activity_Activity( $a2 );
     538
     539                bp_activity_mark_as_spam( $activity );
     540                $activity->save();
     541
     542                $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     543
     544                $this->assertEmpty( $post_comments, 'A post comment should be spammed when the corresponding activity is spammed' );
     545
     546                bp_activity_mark_as_ham( $activity );
     547                $activity->save();
     548
     549                $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     550                $comment = reset( $post_comments );
     551
     552                $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
     553                $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
     554
     555                // reset
     556                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     557
     558                $this->set_current_user( $old_user );
     559        }
     560
     561        /**
     562         * @group bp_blogs_sync_activity_edit_to_post_comment
     563         * @group post_type_comment_activities
     564         */
     565        public function test_bp_blogs_sync_activity_edit_to_post_comment_spam_activity_comment_unspam_post_comment() {
     566                $old_user = get_current_user_id();
     567                $u = $this->factory->user->create();
     568                $this->set_current_user( $u );
     569                $userdata = get_userdata( $u );
     570
     571                // let's use activity comments instead of single "new_blog_comment" activity items
     572                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     573
     574                // create the blog post
     575                $post_id = $this->factory->post->create( array(
     576                        'post_status' => 'publish',
     577                        'post_type'   => 'post',
     578                        'post_title'  => 'Test activity comment to post comment',
     579                ) );
     580
     581                // grab the activity ID for the activity comment
     582                $a1 = bp_activity_get_activity_id( array(
     583                        'type'      => 'new_blog_post',
     584                        'component' => buddypress()->blogs->id,
     585                        'filter'    => array(
     586                                'item_id' => get_current_blog_id(),
     587                                'secondary_item_id' => $post_id
     588                        ),
     589                ) );
     590
     591                $a2 = bp_activity_new_comment( array(
     592                        'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
     593                        'user_id'     => $u,
     594                        'activity_id' => $a1,
     595                ) );
     596
     597                $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' );
     598
     599                $activity = new BP_Activity_Activity( $a2 );
     600
     601                bp_activity_mark_as_spam( $activity );
     602                $activity->save();
     603
     604                wp_unspam_comment( $c );
     605
     606                $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     607                $comment = reset( $post_comments );
     608
     609                $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
     610                $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
     611
     612                // reset
     613                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     614
     615                $this->set_current_user( $old_user );
     616        }
     617
     618        /**
     619         * @group bp_blogs_sync_activity_edit_to_post_comment
     620         * @group post_type_comment_activities
     621         * @group imath
     622         */
     623        public function test_bp_blogs_sync_activity_edit_to_post_comment_trash_comment_ham_activity() {
     624                $old_user = get_current_user_id();
     625                $u = $this->factory->user->create();
     626                $this->set_current_user( $u );
     627                $userdata = get_userdata( $u );
     628
     629                // let's use activity comments instead of single "new_blog_comment" activity items
     630                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     631
     632                // create the blog post
     633                $post_id = $this->factory->post->create( array(
     634                        'post_status' => 'publish',
     635                        'post_type'   => 'post',
     636                        'post_title'  => 'Test activity comment to post comment',
     637                ) );
     638
     639                // grab the activity ID for the activity comment
     640                $a1 = bp_activity_get_activity_id( array(
     641                        'type'      => 'new_blog_post',
     642                        'component' => buddypress()->blogs->id,
     643                        'filter'    => array(
     644                                'item_id' => get_current_blog_id(),
     645                                'secondary_item_id' => $post_id
     646                        ),
     647                ) );
     648
     649                $a2 = bp_activity_new_comment( array(
     650                        'content'     => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed',
     651                        'user_id'     => $u,
     652                        'activity_id' => $a1,
     653                ) );
     654
     655                $c = bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' );
     656
     657                wp_trash_comment( $c );
     658
     659                $activity = new BP_Activity_Activity( $a2 );
     660
     661                bp_activity_mark_as_ham( $activity );
     662                $activity->save();
     663
     664                $post_comments = get_comments( array( 'post_id' => $post_id, 'status' => 'approve' ) );
     665                $comment = reset( $post_comments );
     666
     667                $this->assertTrue( (int) $comment->comment_ID === (int) bp_activity_get_meta( $a2, 'bp_blogs_post_comment_id' ), 'The comment ID should be in the activity meta' );
     668                $this->assertTrue( (int) $a2 === (int) get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ), 'The activity ID should be in the comment meta' );
     669
     670                // reset
     671                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
    410672
    411                 // Reset activity actions
    412                 $bp->activity->actions = $activity_actions;
    413                 $bp->activity->track = array();
     673                $this->set_current_user( $old_user );
    414674        }
    415675
    416676        /**
  • tests/phpunit/testcases/blogs/filters.php

    diff --git tests/phpunit/testcases/blogs/filters.php tests/phpunit/testcases/blogs/filters.php
    index a0fc961..3d2af7f 100644
    class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 
    1010        public function setUp() {
    1111                parent::setUp();
    1212
    13                 $bp = buddypress();
    14 
    15                 $this->activity_actions = $bp->activity->actions;
    16                 $bp->activity->actions = new stdClass();
    17 
    1813                $this->custom_post_types = array( 'using_old_filter' );
    1914
    2015                register_post_type( 'using_old_filter', array(
    class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 
    3530                _unregister_post_type( 'using_old_filter' );
    3631                remove_filter( 'bp_blogs_record_post_post_types',    array( $this, 'filter_post_types'), 10, 1 );
    3732                remove_filter( 'bp_blogs_record_comment_post_types', array( $this, 'filter_post_types'), 10, 1 );
    38 
    39                 // Reset activity actions
    40                 $bp->activity->actions = $this->activity_actions;
    41                 $bp->activity->track = array();
    4233        }
    4334
    4435        /**
    class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 
    6455
    6556        /**
    6657         * @goup bp_blogs_record_comment
     58         * @group post_type_comment_activities
    6759         */
    6860        public function test_bp_blogs_record_comment() {
    6961                $u = $this->factory->user->create();
    class BP_Tests_Blogs_Filters extends BP_UnitTestCase { 
    8981
    9082        /**
    9183         * @goup bp_blogs_record_comment_sync_activity_comment
     84         * @group post_type_comment_activities
    9285         */
    9386        public function test_bp_blogs_record_comment_sync_activity_comment() {
    9487                $u = $this->factory->user->create();
  • tests/phpunit/testcases/blogs/functions.php

    diff --git tests/phpunit/testcases/blogs/functions.php tests/phpunit/testcases/blogs/functions.php
    index a058db2..d7fad14 100644
    class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    513513
    514514        /**
    515515         * @group bp_blogs_catch_transition_post_status
     516         * @group post_type_comment_activities
    516517         */
    517518        public function test_update_blog_post_and_new_blog_comment_and_activity_comment_meta() {
    518519                // save the current user and override logged-in user
    class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    596597
    597598        /**
    598599         * @group bp_blogs_transition_activity_status
    599          * @group bp_blogs_remove_comment
     600         * @group bp_blogs_post_type_remove_comment
     601         * @group post_type_comment_activities
    600602         */
    601603        public function test_bp_blogs_remove_comment_should_remove_spammed_activity_comment() {
    602604                // save the current user and override logged-in user
    class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    658660        }
    659661
    660662        /**
     663         * @group bp_blogs_post_type_remove_comment
     664         * @group post_type_comment_activities
     665         */
     666        public function test_bp_blogs_post_type_remove_comment() {
     667                $old_user = get_current_user_id();
     668                $u = $this->factory->user->create();
     669                $this->set_current_user( $u );
     670                $userdata = get_userdata( $u );
     671
     672                // create the blog post
     673                $p = $this->factory->post->create( array(
     674                        'post_status' => 'publish',
     675                        'post_type' => 'post',
     676                        'post_title' => 'First title',
     677                ) );
     678
     679                $c = wp_new_comment( array(
     680                        'comment_post_ID'      => $p,
     681                        'comment_author'       => $userdata->user_nicename,
     682                        'comment_author_url'   => 'http://buddypress.org',
     683                        'comment_author_email' => $userdata->user_email,
     684                        'comment_content'      => 'this comment will be removed',
     685                        'comment_type'         => '',
     686                        'comment_parent'       => 0,
     687                        'user_id'              => $u,
     688                ) );
     689
     690                // An activity should exist
     691                $a = bp_activity_get_activity_id( array(
     692                        'user_id' => $u,
     693                        'type'    => 'new_blog_comment'
     694                ) );
     695
     696                // now permanently delete the comment
     697                wp_delete_comment( $c, true );
     698
     699                // The activity comment should no longer exist
     700                $ac = bp_activity_get( array( 'in' => $a ) );
     701                $this->assertTrue( empty( $ac['activities'] ) );
     702        }
     703
     704        /**
    661705         * @group bp_blogs_catch_transition_post_status
    662706         */
    663707        public function test_bp_blogs_is_blog_trackable_false_publish_post() {
    class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    714758        /**
    715759         * @group bp_blogs_record_comment
    716760         * @group unique
     761         * @group post_type_comment_activities
    717762         */
    718763        public function test_bp_blogs_record_comment_no_duplicate_activity_comments() {
    719764                // save the current user and override logged-in user
    class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    777822
    778823        /**
    779824         * @group bp_blogs_record_comment
     825         * @group post_type_comment_activities
    780826         */
    781827        public function test_bp_blogs_record_comment_should_record_parent_blog_post_activity_if_not_found() {
    782828                // Save the current user and override logged-in user
    class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 
    826872                        ),
    827873                ) );
    828874
     875                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     876
    829877                // Assert that activity item for blog post was created after adding a comment
    830878                $this->assertNotNull( $a1, 'Activity item was not created for existing blog post when recording post comment.' );
    831879
    832880                $this->set_current_user( $old_user );
    833881        }
    834882
     883        /**
     884         * @group bp_blogs_comment_sync_activity_comment
     885         * @group post_type_comment_activities
     886         */
     887        public function test_bp_blogs_comment_sync_activity_comment_for_custom_post_type() {
     888                if ( is_multisite() ) {
     889                        $b = $this->factory->blog->create();
     890                        switch_to_blog( $b );
     891                        add_filter( 'comment_flood_filter', '__return_false' );
     892                } else {
     893                        $b = get_current_blog_id();
     894                }
     895
     896                $u = $this->factory->user->create();
     897                $userdata = get_userdata( $u );
     898
     899                $labels = array(
     900                        'name'                       => 'bars',
     901                        'singular_name'              => 'bar',
     902                );
     903
     904                register_post_type( 'foo', array(
     905                        'labels'   => $labels,
     906                        'public'   => true,
     907                        'supports' => array( 'comments' ),
     908                ) );
     909
     910                add_post_type_support( 'foo', 'buddypress-activity' );
     911
     912                bp_activity_set_post_type_tracking_args( 'foo', array(
     913                        'comment_action_id' => 'new_foo_comment',
     914                ) );
     915
     916                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     917
     918                $p = $this->factory->post->create( array(
     919                        'post_author' => $u,
     920                        'post_type'   => 'foo',
     921                ) );
     922
     923                $a1 = bp_activity_get_activity_id( array(
     924                        'type'      => 'new_foo',
     925                        'filter'    => array(
     926                                'item_id' => $b,
     927                                'secondary_item_id' => $p
     928                        ),
     929                ) );
     930
     931                $c = wp_new_comment( array(
     932                        'comment_post_ID'      => $p,
     933                        'comment_author'       => $userdata->user_nicename,
     934                        'comment_author_url'   => 'http://buddypress.org',
     935                        'comment_author_email' => $userdata->user_email,
     936                        'comment_content'      => 'this is a foo comment',
     937                        'comment_type'         => '',
     938                        'comment_parent'       => 0,
     939                        'user_id'              => $u,
     940                ) );
     941
     942                $a2 = bp_activity_new_comment( array(
     943                        'content'     => 'this should generate a new foo comment',
     944                        'user_id'     => $u,
     945                        'activity_id' => $a1,
     946                ) );
     947
     948                $activity_args = array(
     949                        'type'              => 'activity_comment',
     950                        'display_comments'  => 'stream',
     951                        'meta_query'        => array( array(
     952                                'key'       => 'bp_blogs_foo_comment_id',
     953                                'compare'   => 'exists',
     954                        ) )
     955                );
     956
     957                $a = bp_activity_get( $activity_args );
     958                $aids = wp_list_pluck( $a['activities'], 'id' );
     959                $cids = wp_list_pluck( get_approved_comments( $p ), 'comment_ID' );
     960
     961                foreach ( $aids as $aid ) {
     962                        $this->assertTrue( in_array( bp_activity_get_meta( $aid, 'bp_blogs_foo_comment_id' ), $cids ), 'The comment ID should be in the activity meta' );
     963                }
     964
     965                foreach ( $cids as $cid ) {
     966                        $this->assertTrue( in_array( get_comment_meta( $cid, 'bp_activity_comment_id', true ), $aids ), 'The activity ID should be in the comment meta' );
     967                }
     968
     969                _unregister_post_type( 'foo' );
     970
     971                if ( is_multisite() ) {
     972                        restore_current_blog();
     973                        remove_filter( 'comment_flood_filter', '__return_false' );
     974                }
     975
     976                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     977        }
     978
    835979        public function count_activity_comment_saved() {
    836980                $this->activity_saved_comment_count += 1;
    837981        }