Skip to:
Content

BuddyPress.org

Ticket #6482: 6482.05.patch

File 6482.05.patch, 133.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 21fda74..3fc2862 100644
    class BP_Activity_List_Table extends WP_List_Table { 
    11031103
    11041104                // Option defaults.
    11051105                $filter           = array();
     1106                $filter_query     = false;
    11061107                $include_id       = false;
    11071108                $search_terms     = false;
    11081109                $sort             = 'DESC';
    class BP_Activity_List_Table extends WP_List_Table { 
    11291130                }*/
    11301131
    11311132                // Filter.
    1132                 if ( !empty( $_REQUEST['activity_type'] ) )
     1133                if ( ! empty( $_REQUEST['activity_type'] ) ) {
    11331134                        $filter = array( 'action' => $_REQUEST['activity_type'] );
    11341135
     1136                        /**
     1137                         * Filter here to override the filter with a filter query
     1138                         *
     1139                         * @since  2.5.0
     1140                         *
     1141                         * @param array $filter
     1142                         */
     1143                        $has_filter_query = apply_filters( 'bp_activity_list_table_filter_activity_type_items', $filter );
     1144
     1145                        if ( ! empty( $has_filter_query['filter_query'] ) ) {
     1146                                // Reset the filter
     1147                                $filter       = array();
     1148
     1149                                // And use the filter query instead
     1150                                $filter_query = $has_filter_query['filter_query'];
     1151                        }
     1152                }
     1153
    11351154                // Are we doing a search?
    11361155                if ( !empty( $_REQUEST['s'] ) )
    11371156                        $search_terms = $_REQUEST['s'];
    class BP_Activity_List_Table extends WP_List_Table { 
    11581177                        'page'             => $page,
    11591178                        'per_page'         => $per_page,
    11601179                        'search_terms'     => $search_terms,
     1180                        'filter_query'     => $filter_query,
    11611181                        'show_hidden'      => true,
    11621182                        // 'sort'             => $sort,
    11631183                        'spam'             => $spam,
    class BP_Activity_List_Table extends WP_List_Table { 
    17571777         * functions from working as intended.
    17581778         *
    17591779         * @since 2.0.0
     1780         * @since 2.5.0 Include Post type activities types
    17601781         *
    17611782         * @param array $item An array version of the BP_Activity_Activity object.
    17621783         * @return bool $can_comment
    17631784         */
    1764         protected function can_comment( $item  ) {
    1765                 $can_comment = true;
    1766 
    1767                 if ( $this->disable_blogforum_comments ) {
    1768                         switch ( $item['type'] ) {
    1769                                 case 'new_blog_post' :
    1770                                 case 'new_blog_comment' :
    1771                                 case 'new_forum_topic' :
    1772                                 case 'new_forum_post' :
    1773                                         $can_comment = false;
    1774                                         break;
    1775                         }
     1785        protected function can_comment( $item ) {
     1786                $can_comment = bp_activity_type_supports( $item['type'], 'comment-reply' );
    17761787
    1777                 // Activity comments supported.
    1778                 } else {
    1779                         // Activity comment.
    1780                         if ( 'activity_comment' == $item['type'] ) {
    1781                                 // Blogs.
    1782                                 if ( bp_is_active( 'blogs' ) ) {
    1783                                         // Grab the parent activity entry.
    1784                                         $parent_activity = new BP_Activity_Activity( $item['item_id'] );
    1785 
    1786                                         // Fetch blog post comment depth and if the blog post's comments are open.
    1787                                         bp_blogs_setup_activity_loop_globals( $parent_activity );
    1788 
    1789                                         // Check if the activity item can be replied to.
    1790                                         if ( false === bp_blogs_can_comment_reply( true, $item ) ) {
    1791                                                 $can_comment = false;
    1792                                         }
    1793                                 }
     1788                if ( ! $this->disable_blogforum_comments && bp_is_active( 'blogs' ) ) {
     1789                        $parent_activity = false;
     1790
     1791                        if ( bp_activity_type_supports( $item['type'], 'post-type-comment-tracking' ) ) {
     1792                                $parent_activity = (object) $item;
     1793                        } elseif ( 'activity_comment' === $item['type'] && bp_activity_get_meta( $item['id'], 'bp_blogs_post_comment_id' ) ) {
     1794                                $parent_activity =  new BP_Activity_Activity( $item['item_id'] );
     1795                        }
    17941796
    1795                         // Blog post.
    1796                         } elseif ( 'new_blog_post' == $item['type'] ) {
    1797                                 if ( bp_is_active( 'blogs' ) ) {
    1798                                         bp_blogs_setup_activity_loop_globals( (object) $item );
     1797                        if ( ! empty( $parent_activity ) ) {
     1798                                // Fetch blog post comment depth and if the blog post's comments are open.
     1799                                bp_blogs_setup_activity_loop_globals( $parent_activity );
    17991800
    1800                                         if ( empty( buddypress()->blogs->allow_comments[$item['id']] ) ) {
    1801                                                 $can_comment = false;
    1802                                         }
    1803                                 }
     1801                                $can_comment = bp_blogs_can_comment_reply( true, $item );
    18041802                        }
    18051803                }
    18061804
    class BP_Activity_List_Table extends WP_List_Table { 
    18081806                 * Filters if an activity item can be commented on or not.
    18091807                 *
    18101808                 * @since 2.0.0
     1809                 * @since 2.5.0 Add a second parameter to include the activity item into the filter.
    18111810                 *
    1812                  * @param bool $can_comment Whether an activity item can be commented on or not.
     1811                 * @param bool   $can_comment Whether an activity item can be commented on or not.
     1812                 * @param array  $item        An array version of the BP_Activity_Activity object.
    18131813                 */
    1814                 return apply_filters( 'bp_activity_list_table_can_comment', $can_comment );
     1814                return apply_filters( 'bp_activity_list_table_can_comment', $can_comment, $item );
    18151815        }
    18161816
    18171817        /**
  • 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..43216d5 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
    501561        /**
    502562         * Filters tracking arguments for a specific post type.
    503563         *
    function bp_activity_get_post_type_tracking_args( $post_type ) { 
    513573 * Get tracking arguments for all post types.
    514574 *
    515575 * @since 2.2.0
     576 * @since 2.5.0 Include post type comments tracking args if needed
    516577 *
    517578 * @return array List of post types with their tracking arguments.
    518579 */
    function bp_activity_get_post_types_tracking_args() { 
    526587                $track_post_type = bp_activity_get_post_type_tracking_args( $post_type );
    527588
    528589                if ( ! empty( $track_post_type ) ) {
     590                        // Set the post type comments tracking args
     591                        if ( ! empty( $track_post_type->comments_tracking->action_id ) ) {
     592                                // Used to check support for comment tracking by activity type (new_post_type_comment)
     593                                $track_post_type->comments_tracking->comments_tracking = true;
     594
     595                                $post_types_tracking_args[ $track_post_type->comments_tracking->action_id ] = $track_post_type->comments_tracking;
     596
     597                                // Used to check support for comment tracking by activity type (new_post_type)
     598                                $track_post_type->comments_tracking = true;
     599                        }
     600
    529601                        $post_types_tracking_args[ $track_post_type->action_id ] = $track_post_type;
    530602                }
    531603
    function bp_activity_get_post_types_tracking_args() { 
    543615}
    544616
    545617/**
     618 * Check if the *Post Type* activity supports a specific feature.
     619 *
     620 * @since 2.5.0
     621 *
     622 * @param  string $activity_type The activity type to check.
     623 * @param  string $feature       The feature to check. Currently supports:
     624 *                               'post-type-comment-tracking', 'post-type-comment-reply' & 'comment-reply'.
     625 *                               See inline doc for more info.
     626 * @return bool
     627 */
     628function bp_activity_type_supports( $activity_type = '', $feature = '' ) {
     629        $retval = false;
     630
     631        $bp = buddypress();
     632
     633        switch ( $feature ) {
     634                /**
     635                 * Does this activity type support comment tracking?
     636                 *
     637                 * eg. 'new_blog_post' and 'new_blog_comment' will both return true.
     638                 */
     639                case 'post-type-comment-tracking' :
     640                        // Set the activity track global if not set yet
     641                        if ( empty( $bp->activity->track ) ) {
     642                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     643                        }
     644
     645                        if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
     646                                $retval = true;
     647                        }
     648                        break;
     649
     650                /**
     651                 * Is this a parent activity type that support post comments?
     652                 *
     653                 * eg. 'new_blog_post' will return true; 'new_blog_comment' will return false.
     654                 */
     655                case 'post-type-comment-reply' :
     656                        // Set the activity track global if not set yet.
     657                        if ( empty( $bp->activity->track ) ) {
     658                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     659                        }
     660
     661                        if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) && ! empty( $bp->activity->track[ $activity_type ]->comment_action_id ) ) {
     662                                $retval = true;
     663                        }
     664                        break;
     665
     666                /**
     667                 * Does this activity type support comment & reply?
     668                 */
     669                case 'comment-reply' :
     670                        // Set the activity track global if not set yet.
     671                        if ( empty( $bp->activity->track ) ) {
     672                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     673                        }
     674
     675                        // Post Type activities
     676                        if ( ! empty( $bp->activity->track[ $activity_type ] ) ) {
     677                                if ( isset( $bp->activity->track[ $activity_type ]->activity_comment ) ) {
     678                                        $retval = $bp->activity->track[ $activity_type ]->activity_comment;
     679                                }
     680
     681                                // Eventually override with comment synchronization feature.
     682                                if ( isset( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
     683                                        $retval = $bp->activity->track[ $activity_type ]->comments_tracking && ! bp_disable_blogforum_comments();
     684                                }
     685
     686                        // Retired Forums component
     687                        } elseif ( 'new_forum_topic' === $activity_type || 'new_forum_post' === $activity_type ) {
     688                                $retval = ! bp_disable_blogforum_comments();
     689
     690                        // By Default, all other activity types are supporting comments.
     691                        } else {
     692                                $retval = true;
     693                        }
     694                        break;
     695        }
     696
     697        return $retval;
     698}
     699
     700/**
     701 * Helper function to get the parent type out of an activity comment generated by a post type comment
     702 *
     703 * eg: new_blog_comment > new_blog_post
     704 *
     705 * @since 2.5.0
     706 *
     707 * @param  string       $type the activity type to check
     708 * @return array|object $parent_type the parent type registered into the post type tracking args
     709 *                      empty array if not found
     710 */
     711function bp_activity_post_type_get_parent_type( $type ) {
     712        if ( empty( $type ) ) {
     713                return false;
     714        }
     715
     716        $bp = buddypress();
     717
     718        // Set the activity track global if not set yet
     719        if ( empty( $bp->activity->track ) ) {
     720                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     721        }
     722
     723        $parent_type = wp_list_filter( $bp->activity->track, array( 'comment_action_id' => $type ) );
     724
     725        return reset( $parent_type );
     726}
     727
     728/**
    546729 * Get all components' activity actions, sorted by their position attribute.
    547730 *
    548731 * @since 2.2.0
    function bp_activity_format_activity_action_custom_post_type_post( $action, $act 
    14051588        return apply_filters( 'bp_activity_custom_post_type_post_action', $action, $activity );
    14061589}
    14071590
     1591/**
     1592 * Format activity action strings for custom post types comments.
     1593 *
     1594 * @since 2.5.0
     1595 *
     1596 * @param string $action   Static activity action.
     1597 * @param object $activity Activity data object.
     1598 *
     1599 * @return string
     1600 */
     1601function bp_activity_format_activity_action_custom_post_type_comment( $action, $activity ) {
     1602        $bp = buddypress();
     1603
     1604        // Fetch all the tracked post types once.
     1605        if ( empty( $bp->activity->track ) ) {
     1606                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1607        }
     1608
     1609        if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) {
     1610                return $action;
     1611        }
     1612
     1613        $user_link = bp_core_get_userlink( $activity->user_id );
     1614
     1615        if ( is_multisite() ) {
     1616                $blog_link = '<a href="' . esc_url( get_home_url( $activity->item_id ) ) . '">' . get_blog_option( $activity->item_id, 'blogname' ) . '</a>';
     1617
     1618                if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms ) ) {
     1619                        $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms, $user_link, $activity->primary_link, $blog_link );
     1620                } else {
     1621                        $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 );
     1622                }
     1623        } else {
     1624                if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action ) ) {
     1625                        $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action, $user_link, $activity->primary_link );
     1626                } else {
     1627                        $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 );
     1628                }
     1629        }
     1630
     1631        /**
     1632         * Filters the formatted custom post type activity comment action string.
     1633         *
     1634         * @since 2.5.0
     1635         *
     1636         * @param string               $action   Activity action string value.
     1637         * @param BP_Activity_Activity $activity Activity item object.
     1638         */
     1639        return apply_filters( 'bp_activity_custom_post_type_comment_action', $action, $activity );
     1640}
     1641
    14081642/*
    14091643 * Business functions are where all the magic happens in BuddyPress. They will
    14101644 * handle the actual saving or manipulation of information. Usually they will
    function bp_activity_post_type_update( $post = null ) { 
    19932227         * Fires after the updating of an activity item for a custom post type entry.
    19942228         *
    19952229         * @since 2.2.0
     2230         * @since 2.5.0 Add the post type tracking args parameter
    19962231         *
    1997          * @param WP_Post              $post     Post object.
    1998          * @param BP_Activity_Activity $activity Activity object.
     2232         * @param WP_Post              $post                 Post object.
     2233         * @param BP_Activity_Activity $activity             Activity object.
     2234         * @param object               $activity_post_object The post type tracking args object.
    19992235         */
    2000         do_action( 'bp_activity_post_type_updated', $post, $activity );
     2236        do_action( 'bp_activity_post_type_updated', $post, $activity, $activity_post_object );
    20012237
    20022238        return $updated;
    20032239}
    function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) { 
    20532289}
    20542290
    20552291/**
     2292 * Create an activity item for a newly posted post type comment.
     2293 *
     2294 * @since 2.5.0
     2295 *
     2296 * @param  int  $comment_id  ID of the comment.
     2297 * @param  bool $is_approved Whether the comment is approved or not.
     2298 * @param  object $activity_post_object the post type tracking args object.
     2299 *
     2300 * @return int|bool The ID of the activity on success. False on error.
     2301 */
     2302function bp_activity_post_type_comment( $comment_id = 0, $is_approved = true, $activity_post_object = null ) {
     2303        // Get the users comment
     2304        $post_type_comment = get_comment( $comment_id );
     2305
     2306        // Don't record activity if the comment hasn't been approved
     2307        if ( empty( $is_approved ) ) {
     2308                return false;
     2309        }
     2310
     2311        // Don't record activity if no email address has been included
     2312        if ( empty( $post_type_comment->comment_author_email ) ) {
     2313                return false;
     2314        }
     2315
     2316        // Don't record activity if the comment has already been marked as spam
     2317        if ( 'spam' === $is_approved ) {
     2318                return false;
     2319        }
     2320
     2321        // Get the user by the comment author email.
     2322        $user = get_user_by( 'email', $post_type_comment->comment_author_email );
     2323
     2324        // If user isn't registered, don't record activity
     2325        if ( empty( $user ) ) {
     2326                return false;
     2327        }
     2328
     2329        // Get the user_id
     2330        $user_id = (int) $user->ID;
     2331
     2332        // Get blog and post data
     2333        $blog_id = get_current_blog_id();
     2334
     2335        // Get the post
     2336        $post_type_comment->post = get_post( $post_type_comment->comment_post_ID );
     2337
     2338        if ( ! is_a( $post_type_comment->post, 'WP_Post' ) ) {
     2339                return false;
     2340        }
     2341
     2342        /**
     2343         * Filters whether to publish activities about the comment regarding the post status
     2344         *
     2345         * @since 2.5.0
     2346         *
     2347         * @param bool true to bail, false otherwise.
     2348         */
     2349        $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 ) );
     2350
     2351        // If this is a password protected post, or not a public post don't record the comment
     2352        if ( $is_post_status_not_allowed ) {
     2353                return false;
     2354        }
     2355
     2356        // Set post type
     2357        $post_type = $post_type_comment->post->post_type;
     2358
     2359        if ( empty( $activity_post_object ) ) {
     2360                // Get the post type tracking args.
     2361                $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     2362
     2363                // Bail if the activity type does not exist
     2364                if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     2365                        return false;
     2366                }
     2367        }
     2368
     2369        // Set the $activity_comment_object
     2370        $activity_comment_object = $activity_post_object->comments_tracking;
     2371
     2372        /**
     2373         * Filters whether or not to post the activity about the comment.
     2374         *
     2375         * This is a variable filter, dependent on the post type,
     2376         * that lets components or plugins bail early if needed.
     2377         *
     2378         * @since 2.5.0
     2379         *
     2380         * @param bool $value      Whether or not to continue.
     2381         * @param int  $blog_id    ID of the current site.
     2382         * @param int  $post_id    ID of the current post being commented.
     2383         * @param int  $user_id    ID of the current user.
     2384         * @param int  $comment_id ID of the current comment being posted.
     2385         */
     2386        if ( false === apply_filters( "bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id ) ) {
     2387                return false;
     2388        }
     2389
     2390        // Is this an update ?
     2391        $activity_id = bp_activity_get_activity_id( array(
     2392                'user_id'           => $user_id,
     2393                'component'         => $activity_comment_object->component_id,
     2394                'type'              => $activity_comment_object->action_id,
     2395                'item_id'           => $blog_id,
     2396                'secondary_item_id' => $comment_id,
     2397        ) );
     2398
     2399        // Record this in activity streams.
     2400        $comment_link = get_comment_link( $post_type_comment->comment_ID );
     2401
     2402        // Backward compatibility filters for the 'blogs' component.
     2403        if ( 'blogs' == $activity_comment_object->component_id )  {
     2404                $activity_content      = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content',      array( $post_type_comment->comment_content, &$post_type_comment, $comment_link ) );
     2405                $activity_primary_link = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$post_type_comment ) );
     2406        } else {
     2407                $activity_content      = $post_type_comment->comment_content;
     2408                $activity_primary_link = $comment_link;
     2409        }
     2410
     2411        $activity_args = array(
     2412                'id'            => $activity_id,
     2413                'user_id'       => $user_id,
     2414                'content'       => $activity_content,
     2415                'primary_link'  => $activity_primary_link,
     2416                'component'     => $activity_comment_object->component_id,
     2417                'recorded_time' => $post_type_comment->comment_date_gmt,
     2418        );
     2419
     2420        if ( bp_disable_blogforum_comments() ) {
     2421                $blog_url = get_home_url( $blog_id );
     2422                $post_url = add_query_arg(
     2423                        'p',
     2424                        $post_type_comment->post->ID,
     2425                        trailingslashit( $blog_url )
     2426                );
     2427
     2428                $activity_args['type']              = $activity_comment_object->action_id;
     2429                $activity_args['item_id']           = $blog_id;
     2430                $activity_args['secondary_item_id'] = $post_type_comment->comment_ID;
     2431
     2432                if ( ! empty( $activity_args['content'] ) ) {
     2433                        // Create the excerpt.
     2434                        $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args );
     2435
     2436                        // Backward compatibility filter for blog comments.
     2437                        if ( 'blogs' == $activity_post_object->component_id )  {
     2438                                $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type );
     2439                        } else {
     2440                                $activity_args['content'] = $activity_summary;
     2441                        }
     2442                }
     2443
     2444                // Set up the action by using the format functions.
     2445                $action_args = array_merge( $activity_args, array(
     2446                        'post_title' => $post_type_comment->post->post_title,
     2447                        'post_url'   => $post_url,
     2448                        'blog_url'   => $blog_url,
     2449                        'blog_name'  => get_blog_option( $blog_id, 'blogname' ),
     2450                ) );
     2451
     2452                $activity_args['action'] = call_user_func_array( $activity_comment_object->format_callback, array( '', (object) $action_args ) );
     2453
     2454                // Make sure the action is set.
     2455                if ( empty( $activity_args['action'] ) ) {
     2456                        return;
     2457                } else {
     2458                        // Backward compatibility filter for the blogs component.
     2459                        if ( 'blogs' === $activity_post_object->component_id )  {
     2460                                $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
     2461                        }
     2462                }
     2463
     2464                $activity_id = bp_activity_add( $activity_args );
     2465        }
     2466
     2467        /**
     2468         * Fires after the publishing of an activity item for a newly published post type post.
     2469         *
     2470         * @since 2.5.0
     2471         *
     2472         * @param int        $activity_id          ID of the newly published activity item.
     2473         * @param WP_Comment $post_type_comment    Comment object.
     2474         * @param array      $activity_args        Array of activity arguments.
     2475         * @param object     $activity_post_object the post type tracking args object.
     2476         */
     2477        do_action_ref_array( 'bp_activity_post_type_comment', array( &$activity_id, $post_type_comment, $activity_args, $activity_post_object ) );
     2478
     2479        return $activity_id;
     2480}
     2481add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
     2482add_action( 'edit_comment', 'bp_activity_post_type_comment', 10    );
     2483
     2484/**
     2485 * Remove an activity item when a comment about a post type is deleted.
     2486 *
     2487 * @since 2.5.0
     2488 *
     2489 * @param  int    $comment_id           ID of the comment.
     2490 * @param  object $activity_post_object The post type tracking args object.
     2491 *
     2492 * @return bool True on success. False on error.
     2493 */
     2494function bp_activity_post_type_remove_comment( $comment_id = 0, $activity_post_object = null ) {
     2495        if ( empty( $activity_post_object ) ) {
     2496                $comment = get_comment( $comment_id );
     2497                if ( ! $comment ) {
     2498                        return;
     2499                }
     2500
     2501                $post_type = get_post_type( $comment->comment_post_ID );
     2502                if ( ! $post_type ) {
     2503                        return;
     2504                }
     2505
     2506                // Get the post type tracking args.
     2507                $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     2508
     2509                // Bail if the activity type does not exist
     2510                if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     2511                        return false;
     2512                }
     2513        }
     2514
     2515        // Set the $activity_comment_object
     2516        $activity_comment_object = $activity_post_object->comments_tracking;
     2517
     2518        if ( empty( $activity_comment_object->action_id ) ) {
     2519                return false;
     2520        }
     2521
     2522        $deleted = false;
     2523
     2524        if ( bp_disable_blogforum_comments() ) {
     2525                $deleted = bp_activity_delete_by_item_id( array(
     2526                        'item_id'           => get_current_blog_id(),
     2527                        'secondary_item_id' => $comment_id,
     2528                        'component'         => $activity_comment_object->component_id,
     2529                        'type'              => $activity_comment_object->action_id,
     2530                        'user_id'           => false,
     2531                ) );
     2532        }
     2533
     2534        /**
     2535         * Fires after the custom post type comment activity was removed.
     2536         *
     2537         * @since 2.5.0
     2538         *
     2539         * @param bool       $deleted              True if the activity was deleted false otherwise
     2540         * @param WP_Comment $comment              Comment object.
     2541         * @param object     $activity_post_object The post type tracking args object.
     2542         * @param string     $value                The post type comment activity type.
     2543         */
     2544        do_action( 'bp_activity_post_type_remove_comment', $deleted, $comment_id, $activity_post_object, $activity_comment_object->action_id );
     2545
     2546        return $deleted;
     2547}
     2548add_action( 'delete_comment', 'bp_activity_post_type_remove_comment', 10, 1 );
     2549
     2550/**
     2551 * Get all activity comments corresponding to Post type synced comments
     2552 * and reset the bp_parent_type activity meta.
     2553 *
     2554 * Used in the 2.5.0 upgrade routine as we need to make sure filtering the activities
     2555 * for new_blog_comment is getting activity_comment types having a new_blog_post activity
     2556 * type as their parent activity. It can also be used as a repair tool too, if needed.
     2557 *
     2558 * @since 2.5.0
     2559 */
     2560function bp_activity_post_type_comment_reset_parent_type_meta() {
     2561        /**
     2562         * First get all activities associated to a Post type comment ID
     2563         * without a bp_parent_type meta set.
     2564         */
     2565        $synced_post_comments = bp_activity_get( array(
     2566                'type'              => 'activity_comment',
     2567                'display_comments'  => 'stream',
     2568                'per_page'          => false,
     2569                'meta_query'        => array(
     2570                        'relation' => 'AND',
     2571                        array(
     2572                                'key'       => 'bp_blogs_post_comment_id',
     2573                                'compare'   => 'EXISTS',
     2574                        ),
     2575                        array(
     2576                                'key'       => 'bp_parent_type',
     2577                                'compare'   => 'NOT EXISTS',
     2578                        ),
     2579                )
     2580        ) );
     2581
     2582        if ( empty( $synced_post_comments['activities'] ) ) {
     2583                return;
     2584        }
     2585
     2586        // Prepare the 'in' parameter to get the parent activity types
     2587        $parent_activities = array_unique( wp_list_pluck( $synced_post_comments['activities'], 'item_id' ) );
     2588
     2589        $parent_activity_types = bp_activity_get( array(
     2590                'per_page' => false,
     2591                'in'       => $parent_activities,
     2592        ) );
     2593
     2594        if ( empty( $parent_activity_types['activities'] ) ) {
     2595                return;
     2596        }
     2597
     2598        // Build the list of parent activity types
     2599        $parent_activity_types = wp_list_pluck( $parent_activity_types['activities'], 'type', 'id' );
     2600
     2601        // Loop through each activity and set the 'bp_parent_type' if needed
     2602        foreach ( $synced_post_comments['activities'] as $synced_post_comment ) {
     2603                if ( isset( $parent_activity_types[ $synced_post_comment->item_id ] ) ) {
     2604                        bp_activity_update_meta( $synced_post_comment->id, 'bp_parent_type', $parent_activity_types[ $synced_post_comment->item_id ] );
     2605                }
     2606        }
     2607}
     2608
     2609/**
    20562610 * Add an activity comment.
    20572611 *
    20582612 * @since 1.2.0
     2613 * @since 2.5.0 Add a new possible parameter $skip_notification for the array of arguments.
     2614 *              Add the $primary_link parameter for the array of arguments.
    20592615 *
    20602616 * @uses wp_parse_args()
    20612617 * @uses bp_activity_add()
    function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) { 
    20652621 * @uses do_action() To call the 'bp_activity_comment_posted' hook.
    20662622 *
    20672623 * @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.
     2624 *     @type int    $id                Optional. Pass an ID to update an existing comment.
     2625 *     @type string $content           The content of the comment.
     2626 *     @type int    $user_id           Optional. The ID of the user making the comment.
     2627 *                                     Defaults to the ID of the logged-in user.
     2628 *     @type int    $activity_id       The ID of the "root" activity item, ie the oldest
     2629 *                                     ancestor of the comment.
     2630 *     @type int    $parent_id         Optional. The ID of the parent activity item, ie the item to
     2631 *                                     which the comment is an immediate reply. If not provided,
     2632 *                                     this value defaults to the $activity_id.
     2633 *     @type string $primary_link      Optional. the primary link for the comment.
     2634 *                                     Defaults to an empty string.
     2635 *     @type bool   $skip_notification Optional. false to send a comment notification, false otherwise.
     2636 *                                     Defaults to false.
    20772637 * }
    20782638 * @return int|bool The ID of the comment on success, otherwise false.
    20792639 */
    function bp_activity_new_comment( $args = '' ) { 
    20872647        }
    20882648
    20892649        $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).
     2650                'id'                => false,
     2651                'content'           => false,
     2652                'user_id'           => bp_loggedin_user_id(),
     2653                'activity_id'       => false, // ID of the root activity item.
     2654                'parent_id'         => false, // ID of a parent comment (optional).
     2655                'primary_link'      => '',
     2656                'skip_notification' => false,
    20952657        ) );
    20962658
    20972659        // Bail if missing necessary data.
    function bp_activity_new_comment( $args = '' ) { 
    21382700                'content'           => $comment_content,
    21392701                'component'         => buddypress()->activity->id,
    21402702                'type'              => 'activity_comment',
     2703                'primary_link'      => $r['primary_link'],
    21412704                'user_id'           => $r['user_id'],
    21422705                'item_id'           => $activity_id,
    21432706                'secondary_item_id' => $r['parent_id'],
    function bp_activity_new_comment( $args = '' ) { 
    21562719        }
    21572720        wp_cache_delete( $activity_id, 'bp_activity' );
    21582721
    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 );
     2722        if ( empty( $r[ 'skip_notification' ] ) ) {
     2723                /**
     2724                 * Fires near the end of an activity comment posting, before the returning of the comment ID.
     2725                 * Sends a notification to the user @see bp_activity_new_comment_notification_helper().
     2726                 *
     2727                 * @since 1.2.0
     2728                 *
     2729                 * @param int   $comment_id ID of the newly posted activity comment.
     2730                 * @param array $r          Array of parsed comment arguments.
     2731                 * @param int   $activity   ID of the activity item being commented on.
     2732                 */
     2733                do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
     2734        } else {
     2735                /**
     2736                 * Fires near the end of an activity comment posting, before the returning of the comment ID.
     2737                 * without sending a notification to the user
     2738                 *
     2739                 * @since 2.5.0
     2740                 *
     2741                 * @param int   $comment_id ID of the newly posted activity comment.
     2742                 * @param array $r          Array of parsed comment arguments.
     2743                 * @param int   $activity   ID of the activity item being commented on.
     2744                 */
     2745                do_action( 'bp_activity_comment_posted_notification_skipped', $comment_id, $r, $activity );
     2746        }
    21692747
    21702748        if ( empty( $comment_id ) ) {
    21712749                $errors->add( 'comment_failed', $feedback );
    function bp_activity_delete( $args = '' ) { 
    24343012 * @return bool True on success, false on failure.
    24353013 */
    24363014function bp_activity_delete_comment( $activity_id, $comment_id ) {
     3015        $deleted = false;
    24373016
    24383017        /**
    24393018         * Filters whether BuddyPress should delete an activity comment or not.
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    24423021         * handle the deletion of child comments differently. Make sure you return false.
    24433022         *
    24443023         * @since 1.2.0
     3024         * @since 2.5.0 Add the deleted parameter (passed by reference)
    24453025         *
    24463026         * @param bool $value       Whether BuddyPress should continue or not.
    24473027         * @param int  $activity_id ID of the root activity item being deleted.
    24483028         * @param int  $comment_id  ID of the comment being deleted.
    24493029         */
    2450         if ( ! apply_filters( 'bp_activity_delete_comment_pre', true, $activity_id, $comment_id ) ) {
    2451                 return false;
     3030        if ( ! apply_filters_ref_array( 'bp_activity_delete_comment_pre', array( true, $activity_id, $comment_id, &$deleted ) ) ) {
     3031                return $deleted;
    24523032        }
    24533033
    24543034        // Delete any children of this comment.
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    24573037        // Delete the actual comment.
    24583038        if ( ! bp_activity_delete( array( 'id' => $comment_id, 'type' => 'activity_comment' ) ) ) {
    24593039                return false;
     3040        } else {
     3041                $deleted = true;
    24603042        }
    24613043
    24623044        // Purge comment cache for the root activity update.
    function bp_activity_delete_comment( $activity_id, $comment_id ) { 
    24753057         */
    24763058        do_action( 'bp_activity_delete_comment', $activity_id, $comment_id );
    24773059
    2478         return true;
     3060        return $deleted;
    24793061}
    24803062
    24813063        /**
  • 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..416ba35 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_ 
    565657                'comment_type'         => '', // Could be interesting to add 'buddypress' here...
    566658                'comment_parent'       => (int) $comment_parent,
    567659                'user_id'              => $params['user_id'],
    568 
    569                 // Commenting these out for now
    570                 // 'comment_author_IP'    => '127.0.0.1',
    571                 // 'comment_agent'        => '', .
    572660                'comment_approved'     => 1
    573661        );
    574662
    575663        // Prevent separate activity entry being made.
    576         remove_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     664        remove_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
    577665
    578666        // Handle multisite.
    579667        switch_to_blog( $parent_activity->item_id );
    function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 
    590678
    591679        // Add meta to activity comment.
    592680        bp_activity_update_meta( $comment_id, 'bp_blogs_post_comment_id', $post_comment_id );
     681        bp_activity_update_meta( $comment_id, 'bp_parent_type', $parent_activity->type );
    593682
    594683        // Resave activity comment with WP comment permalink.
    595684        //
    function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_ 
    616705        restore_current_blog();
    617706
    618707        // Add the comment hook back.
    619         add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     708        add_action( 'comment_post', 'bp_activity_post_type_comment', 10, 2 );
    620709
    621710        /**
    622711         * 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 
    640729 * activity comment children before they are deleted.
    641730 *
    642731 * @since 2.0.0
     732 * @since 2.5.0 Add the $delected parameter
    643733 *
    644734 * @param bool $retval             Whether BuddyPress should continue or not.
    645735 * @param int  $parent_activity_id The parent activity ID for the activity comment.
    646736 * @param int  $activity_id        The activity ID for the pending deleted activity comment.
     737 * @param bool $deleted            Whether the comment was deleted or not.
    647738 * @return bool
    648739 */
    649 function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id ) {
     740function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id, &$deleted ) {
    650741        // Check if parent activity is a blog post.
    651742        $parent_activity = new BP_Activity_Activity( $parent_activity_id );
    652         if ( 'new_blog_post' != $parent_activity->type ) {
     743
     744        // if parent activity isn't a post type having the buddypress-activity support, stop now!
     745        if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
    653746                return $retval;
    654747        }
    655748
    function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_i 
    657750        $activity = bp_activity_get( array(
    658751                'in'               => $activity_id,
    659752                'display_comments' => 'stream',
     753                'spam'             => 'all',
    660754        ) );
    661755
    662756        // Get all activity comment IDs for the pending deleted item.
    function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_i 
    677771        // emulate bp_activity_delete_comment().
    678772        BP_Activity_Activity::rebuild_activity_comment_tree( $parent_activity_id );
    679773
     774        // Avoid the error message although the comments were successfully deleted
     775        $deleted = true;
     776
    680777        // We're overriding the default bp_activity_delete_comment() functionality
    681778        // so we need to return false.
    682779        return false;
    683780}
    684 add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activity_comment', 10, 3 );
     781add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activity_comment', 10, 4 );
    685782
    686783/**
    687784 * Updates the blog comment when the associated activity comment is edited.
    add_filter( 'bp_activity_delete_comment_pre', 'bp_blogs_sync_delete_from_activit 
    691788 * @param BP_Activity_Activity $activity The activity object.
    692789 */
    693790function 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 
    699791        // This is a new entry, so stop!
    700792        // We only want edits!
    701         if ( empty( $activity->id ) ) {
     793        if ( empty( $activity->id ) || bp_disable_blogforum_comments() ) {
    702794                return;
    703795        }
    704796
    705         // Prevent recursion.
    706         remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     797        // fetch parent activity item
     798        $parent_activity = new BP_Activity_Activity( $activity->item_id );
     799
     800        // if parent activity isn't a post type having the buddypress-activity support for comments, stop now!
     801        if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) {
     802                return;
     803        }
    707804
    708805        // Try to see if a corresponding blog comment exists.
    709806        $post_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
    function bp_blogs_sync_activity_edit_to_post_comment( BP_Activity_Activity $acti 
    712809                return;
    713810        }
    714811
    715         // Fetch parent activity item.
    716         $parent_activity = new BP_Activity_Activity( $activity->item_id );
     812        // Get the parent type
     813        $parent_type = bp_activity_get_meta( $activity->id, 'bp_parent_type' );
    717814
    718815        // Sanity check.
    719         if ( 'new_blog_post' !== $parent_activity->type ) {
     816        if ( $parent_type !== $parent_activity->type ) {
    720817                return;
    721818        }
    722819
    723820        // Handle multisite.
    724821        switch_to_blog( $parent_activity->item_id );
    725822
    726         // Update the blog post comment.
    727         wp_update_comment( array(
    728                 'comment_ID'      => $post_comment_id,
    729                 'comment_content' => $activity->content
    730         ) );
     823        // Get the comment status
     824        $post_comment_status = wp_get_comment_status( $post_comment_id );
     825        $old_comment_status  = $post_comment_status;
     826
     827        // No need to edit the activity, as it's the activity who's updating the comment
     828        remove_action( 'transition_comment_status',     'bp_activity_transition_post_type_comment_status', 10, 3 );
     829        remove_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment',          10, 4 );
     830
     831        if ( 1 === (int) $activity->is_spam && 'spam' !== $post_comment_status ) {
     832                wp_spam_comment( $post_comment_id );
     833        } elseif ( ! $activity->is_spam ) {
     834                if ( 'spam' === $post_comment_status  ) {
     835                        wp_unspam_comment( $post_comment_id );
     836                } elseif ( 'trash' === $post_comment_status ) {
     837                        wp_untrash_comment( $post_comment_id );
     838                } else {
     839                        // Update the blog post comment.
     840                        wp_update_comment( array(
     841                                'comment_ID'       => $post_comment_id,
     842                                'comment_content'  => $activity->content,
     843                        ) );
     844                }
     845        }
     846
     847        // Restore actions
     848        add_action( 'transition_comment_status',     'bp_activity_transition_post_type_comment_status', 10, 3 );
     849        add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment',          10, 4 );
    731850
    732851        restore_current_blog();
    733852}
    add_action( 'trashed_post_comments', 'bp_blogs_remove_activity_meta_for_trashed_ 
    769888 * powers the 'Comments' filter in the activity directory dropdown) includes
    770889 * both old-style and new-style activity comments.
    771890 *
    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  *
    777891 * @since 2.1.0
     892 * @since 2.5.0 Used for any synced Post type comments, in wp-admin or front-end contexts.
    778893 *
    779894 * @param array $args Arguments passed from bp_parse_args() in bp_has_activities().
    780895 * @return array $args
    function bp_blogs_new_blog_comment_query_backpat( $args ) { 
    783898        global $wpdb;
    784899        $bp = buddypress();
    785900
    786         // Bail if this is not a 'new_blog_comment' query.
    787         if ( 'new_blog_comment' !== $args['action'] ) {
     901        // If activity comments are disabled for blog posts, stop now!
     902        if ( bp_disable_blogforum_comments() ) {
     903                return $args;
     904        }
     905
     906        // Get the parent type
     907        $parent_type = bp_activity_post_type_get_parent_type( $args['action'] );
     908
     909        // Bail if this is not a 'new_blog_comment' query
     910        if ( empty( $parent_type->action_id ) ) {
    788911                return $args;
    789912        }
    790913
    791914        // 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' ) );
     915        $activity_ids = $wpdb->get_col( $wpdb->prepare( "SELECT activity_id FROM {$bp->activity->table_name_meta} WHERE meta_key = 'bp_parent_type' AND meta_value = %s", $parent_type->action_id ) );
    793916
    794917        if ( empty( $activity_ids ) ) {
    795918                return $args;
    function bp_blogs_new_blog_comment_query_backpat( $args ) { 
    798921        // Init the filter query.
    799922        $filter_query = array();
    800923
    801         if ( 'null' === $args['scope'] ) {
     924        if ( ! isset( $args['scope'] ) || 'null' === $args['scope'] ) {
    802925                $args['scope'] = '';
    803926        } elseif ( 'just-me' === $args['scope'] ) {
    804927                $filter_query = array(
    function bp_blogs_new_blog_comment_query_backpat( $args ) { 
    831954
    832955        // Finally reset the action.
    833956        $args['action'] = '';
     957        $args['type']   = '';
    834958
    835959        // Return the original arguments.
    836960        return $args;
    837961}
    838 add_filter( 'bp_after_has_activities_parse_args', 'bp_blogs_new_blog_comment_query_backpat' );
     962add_filter( 'bp_after_has_activities_parse_args',                'bp_blogs_new_blog_comment_query_backpat' );
     963add_filter( 'bp_activity_list_table_filter_activity_type_items', 'bp_blogs_new_blog_comment_query_backpat' );
    839964
    840965/**
    841966 * Utility function to set up some variables for use in the activity loop.
    function bp_blogs_setup_activity_loop_globals( $activity ) { 
    857982                return;
    858983        }
    859984
    860         // Parent not a blog post? stop now!
    861         if ( 'new_blog_post' !== $activity->type ) {
     985        // The activity type does not support comments or replies ? stop now!
     986        if ( ! bp_activity_type_supports( $activity->type, 'post-type-comment-reply' ) ) {
    862987                return;
    863988        }
    864989
    add_action( 'bp_before_activity_comment', 'bp_blogs_setup_comment_loop_globals_o 
    9351060 * @return bool
    9361061 */
    9371062function bp_blogs_disable_activity_commenting( $retval ) {
     1063        global $activities_template;
     1064
    9381065        // If activity commenting is disabled, return current value.
    939         if ( bp_disable_blogforum_comments() ) {
     1066        if ( bp_disable_blogforum_comments() || ! isset( $activities_template->in_the_loop ) ) {
    9401067                return $retval;
    9411068        }
    9421069
    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;
     1070        $type = bp_get_activity_type();
    9571071
     1072        // It's a post type supporting comment tracking.
     1073        if ( bp_activity_type_supports( $type, 'post-type-comment-tracking' ) ) {
     1074                // The activity type is supporting comments or replies
     1075                if ( bp_activity_type_supports( $type, 'post-type-comment-reply' ) ) {
    9581076                        // Setup some globals we'll need to reference later.
    9591077                        bp_blogs_setup_activity_loop_globals( $activities_template->activity );
    9601078
    9611079                        // If comments are closed for the WP blog post, we should disable
    9621080                        // activity comments for this activity entry.
    963                         if ( empty( buddypress()->blogs->allow_comments[bp_get_activity_id()] ) ) {
     1081                        if ( empty( buddypress()->blogs->allow_comments[ bp_get_activity_id() ] ) ) {
    9641082                                $retval = false;
    9651083                        }
    966 
    967                         break;
     1084                // The activity type does not support comments or replies
     1085                } else {
     1086                        $retval = false;
     1087                }
    9681088        }
    9691089
    9701090        return $retval;
    function bp_blogs_disable_activity_commenting( $retval ) { 
    9721092add_filter( 'bp_activity_can_comment', 'bp_blogs_disable_activity_commenting' );
    9731093
    9741094/**
     1095 * Limit the display of post type synced comments.
     1096 *
     1097 * @since  2.5.0
     1098 *
     1099 * When viewing the synced comments in stream mode, this prevents comments to
     1100 * be displayed twice, and avoids a Javascript error as the form to add replies
     1101 * is not available.
     1102 *
     1103 * @param  int $retval  The comment count for the activity.
     1104 * @return int          The comment count, or 0 to hide activity comment replies.
     1105 */
     1106function bp_blogs_post_type_comments_avoid_duplicates( $retval ) {
     1107        /**
     1108         * Only limit the display when Post type comments are synced with
     1109         * activity comments.
     1110         */
     1111        if ( bp_disable_blogforum_comments() ) {
     1112                return $retval;
     1113        }
     1114
     1115        if ( 'activity_comment' !== bp_get_activity_type() ) {
     1116                return $retval;
     1117        }
     1118
     1119        if ( true === (bool) bp_activity_get_meta( bp_get_activity_id(), 'bp_blogs_post_comment_id' ) ) {
     1120                $retval = 0;
     1121        }
     1122
     1123        return $retval;
     1124}
     1125add_filter( 'bp_activity_get_comment_count', 'bp_blogs_post_type_comments_avoid_duplicates' );
     1126
     1127/**
    9751128 * Check if an activity comment associated with a blog post can be replied to.
    9761129 *
    9771130 * By default, disables replying to activity comments if the corresponding WP
    function bp_blogs_activity_comment_single_action( $retval, $activity ) { 
    10861239        $blog_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
    10871240
    10881241        if ( ! empty( $blog_comment_id ) ) {
     1242                $bp = buddypress();
     1243
    10891244                // Fetch the parent blog post activity item.
    10901245                $parent_blog_post_activity = new BP_Activity_Activity( $activity->item_id );
    10911246
    function bp_blogs_activity_comment_single_action( $retval, $activity ) { 
    10981253                // Override 'secondary_item_id' to use comment ID.
    10991254                $object->secondary_item_id = $blog_comment_id;
    11001255
    1101                 // Now format the activity action using the 'new_blog_comment' action callback.
    1102                 $retval = bp_blogs_format_activity_action_new_blog_comment( '', $object );
     1256                // Set the activity track global if not set yet
     1257                if ( empty( $bp->activity->track ) ) {
     1258                        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1259                }
     1260
     1261                // Use the action string callback for the activity type
     1262                if ( ! empty( $bp->activity->track[ $parent_blog_post_activity->type ]->comment_action_id ) ) {
     1263                        $object->type = $bp->activity->track[ $parent_blog_post_activity->type ]->comment_action_id;
     1264
     1265                        // now format the activity action using the 'new_blog_comment' action callback
     1266                        $retval = call_user_func_array( $bp->activity->track[ $object->type ]->format_callback, array( '', $object ) );
     1267
     1268                        /**
     1269                         * Reset the activity type to 'activity_comment'
     1270                         * so that the "View Conversation" button appears
     1271                         */
     1272                        $object->type = 'activity_comment';
     1273                }
    11031274        }
    11041275
    11051276        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..cdfbf03 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_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;
     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        }
    643586
    644                         // Record the activity entry.
    645                         $activity_id = bp_blogs_record_activity( $args );
     587        // Sync comment - activity comment
     588        if ( ! bp_disable_blogforum_comments() ) {
    646589
    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( '/' ) ) );
     590                if ( ! empty( $_REQUEST['action'] ) ) {
     591                        $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
    650592
    651                 // Record comment as BP activity comment under the parent 'new_blog_post'
    652                 // activity item.
    653                 } else {
    654                         // This is a comment edit
    655                         // check to see if corresponding activity entry already exists.
    656                         if ( ! empty( $_REQUEST['action'] ) ) {
    657                                 $existing_activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
    658 
    659                                 if ( ! empty( $existing_activity_id ) ) {
    660                                         $args['id'] = $existing_activity_id;
    661                                 }
     593                        if ( ! empty( $existing_activity_id ) ) {
     594                                $activity_args['id'] = $existing_activity_id;
    662595                        }
     596                }
    663597
    664                         // Find the parent 'new_blog_post' activity entry.
     598                if ( empty( $activity_post_object ) ) {
     599                        $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type );
     600                }
     601
     602                if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) {
     603                        // find the parent 'new_post_type' activity entry
    665604                        $parent_activity_id = bp_activity_get_activity_id( array(
    666                                 'component'         => 'blogs',
    667                                 'type'              => 'new_blog_post',
     605                                'component'         => $activity_post_object->component_id,
     606                                'type'              => $activity_post_object->action_id,
    668607                                'item_id'           => $blog_id,
    669                                 'secondary_item_id' => $recorded_comment->comment_post_ID
     608                                'secondary_item_id' => $comment->comment_post_ID
    670609                        ) );
    671610
    672611                        // Try to create a new activity item for the parent blog post.
    673612                        if ( empty( $parent_activity_id ) ) {
    674                                 $parent_activity_id = bp_activity_post_type_publish( $recorded_comment->comment_post_ID, $recorded_comment->post );
     613                                $parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post );
    675614                        }
     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_post_comment_id', $comment->comment_ID );
     651
     652                                /**
     653                                 * The activity metadata to inform about the "parent" type
     654                                 * eg: the "parent" type for new_blog_comment is new_blog_post
     655                                 *
     656                                 * This is used to make sure filtering the activities using the new_{post_type}_comment type
     657                                 * will fetch all new_{post_type}_comment activities & activity_comment activities belonging to
     658                                 * the new_{post_type} parent activity
     659                                 */
     660                                bp_activity_update_meta( $activity_id, 'bp_parent_type', $activity_post_object->action_id );
     661
     662                                // The comment metadata to inform about the corresponding activity ID
     663                                add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id );
     664
     665                                // These activity metadatas are used to build the new_blog_comment action string
     666                                if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
     667                                        bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title );
     668                                        bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) );
    723669                                }
    724670                        }
    725671                }
     672        }
    726673
    727                 // Update the blogs last active date.
    728                 bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
     674        // Update the blogs last active date
     675        bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
     676
     677        if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
     678                /**
     679                 * Fires after BuddyPress has recorded metadata about a published blog post comment.
     680                 *
     681                 * @since 2.5.0
     682                 *
     683                 * @param int     $value    Comment ID of the blog post comment being recorded.
     684                 * @param WP_Post $post  WP_Comment object for the current blog post.
     685                 * @param string  $value ID of the user associated with the current blog post comment.
     686                 */
     687                do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() );
    729688        }
    730689
    731         return $recorded_comment;
     690        return $activity_id;
    732691}
    733 add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    734 add_action( 'edit_comment', 'bp_blogs_record_comment', 10    );
     692add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 );
    735693
    736694/**
    737695 * Record a user's association with a blog.
    function bp_blogs_remove_post( $post_id, $blog_id = 0, $user_id = 0 ) { 
    984942add_action( 'delete_post', 'bp_blogs_remove_post' );
    985943
    986944/**
    987  * Remove a blog comment activity item from the activity stream.
     945 * Remove a synced activity comment from the activity stream.
     946 *
     947 * @since 2.5.0
     948 *
     949 * @param bool   $deleted              True when a comment post type activity was successfully removed.
     950 * @param int    $comment_id           ID of the comment to be removed.
     951 * @param object $activity_post_object The post type tracking args object.
     952 * @param string $activity_type        The post type comment activity type.
    988953 *
    989  * @param int $comment_id ID of the comment to be removed.
     954 * @return bool True on success. False on error.
    990955 */
    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.
     956function bp_blogs_post_type_remove_comment( $deleted, $comment_id, $activity_post_object, $activity_type = '' ) {
     957        // Remove synced activity comments, if needed.
     958        if ( ! bp_disable_blogforum_comments() ) {
     959                // Get associated activity ID from comment meta
    1008960                $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
    1009961
    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.
     962                /**
     963                 * Delete the associated activity comment & also remove
     964                 * child post comments and associated activity comments.
     965                 */
     966                if ( ! empty( $activity_id ) ) {
     967                        // fetch the activity comments for the activity item
    1015968                        $activity = bp_activity_get( array(
    1016969                                'in'               => $activity_id,
    1017970                                'display_comments' => 'stream',
    1018971                                'spam'             => 'all',
    1019972                        ) );
    1020973
    1021                         // Get all activity comment IDs for the pending deleted item.
     974                        // get all activity comment IDs for the pending deleted item
    1022975                        if ( ! empty( $activity['activities'] ) ) {
    1023976                                $activity_ids   = bp_activity_recurse_comments_activity_ids( $activity );
    1024977                                $activity_ids[] = $activity_id;
    1025978
    1026                                 // Delete activity items.
     979                                // delete activity items
    1027980                                foreach ( $activity_ids as $activity_id ) {
    1028981                                        bp_activity_delete( array(
    1029982                                                'id' => $activity_id
    1030983                                        ) );
    1031984                                }
    1032985
    1033                                 // Remove associated blog comments.
     986                                // remove associated blog comments
    1034987                                bp_blogs_remove_associated_blog_comments( $activity_ids );
    1035988
    1036                                 // Rebuild activity comment tree.
     989                                // rebuild activity comment tree
    1037990                                BP_Activity_Activity::rebuild_activity_comment_tree( $activity['activities'][0]->item_id );
     991
     992                                // Set the result
     993                                $deleted = true;
    1038994                        }
    1039995                }
    1040996        }
    1041997
    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() );
     998        // Backcompat for comments about the 'post' post type.
     999        if ( 'new_blog_comment' === $activity_type ) {
     1000                /**
     1001                 * Fires after a blog comment activity item was removed from activity stream.
     1002                 *
     1003                 * @since 1.0.0
     1004                 *
     1005                 * @param int $value      ID for the blog associated with the removed comment.
     1006                 * @param int $comment_id ID of the comment being removed.
     1007                 * @param int $value      ID of the current logged in user.
     1008                 */
     1009                do_action( 'bp_blogs_remove_comment', get_current_blog_id(), $comment_id, bp_loggedin_user_id() );
     1010        }
     1011
     1012        return $deleted;
    10521013}
    1053 add_action( 'delete_comment', 'bp_blogs_remove_comment' );
     1014add_action( 'bp_activity_post_type_remove_comment', 'bp_blogs_post_type_remove_comment', 10, 4 );
    10541015
    10551016/**
    10561017 * Removes blog comments that are associated with activity comments.
    10571018 *
    10581019 * @since 2.0.0
    10591020 *
    1060  * @see bp_blogs_remove_comment()
     1021 * @see bp_blogs_remove_synced_comment()
    10611022 * @see bp_blogs_sync_delete_from_activity_comment()
    10621023 *
    10631024 * @param array $activity_ids The activity IDs to check association with blog
    function bp_blogs_remove_associated_blog_comments( $activity_ids = array(), $for 
    10941055}
    10951056
    10961057/**
    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 /**
    11791058 * Get the total number of blogs being tracked by BuddyPress.
    11801059 *
    11811060 * @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/bp-core-update.php

    diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php
    index a85c41e..c14086f 100644
    function bp_version_updater() { 
    260260                if ( $raw_db_version < 9615 ) {
    261261                        bp_update_to_2_3();
    262262                }
     263
     264                // 2.5.0
     265                if ( $raw_db_version < 10457 ) {
     266                        bp_update_to_2_5();
     267                }
    263268        }
    264269
    265270        /** All done! *************************************************************/
    function bp_update_to_2_3() { 
    484489}
    485490
    486491/**
     492 * 2.5.0 update routine.
     493 *
     494 * - Add a new activity meta for Post comments synced with activity comments
     495 *
     496 * @since 2.5.0
     497 */
     498function bp_update_to_2_5() {
     499        if ( bp_is_active( 'activity' ) ) {
     500                bp_activity_post_type_comment_reset_parent_type_meta();
     501        }
     502}
     503
     504/**
    487505 * Updates the component field for new_members type.
    488506 *
    489507 * @since 2.2.0
  • 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 e69de29..de1a8ba 100644
     
     1<?php
     2/**
     3 * Deprecated functions.
     4 *
     5 * @deprecated 2.5.0
     6 */
     7
     8// Exit if accessed directly.
     9defined( 'ABSPATH' ) || exit;
     10
     11/**
     12 * When a blog comment status transition occurs, update the relevant activity's status.
     13 *
     14 * @since 1.6.0
     15 * @deprecated 2.5.0
     16 *
     17 * @param string $new_status New comment status.
     18 * @param string $old_status Previous comment status.
     19 * @param object $comment Comment data.
     20 */
     21function bp_blogs_transition_activity_status( $new_status, $old_status, $comment ) {
     22        _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_transition_post_type_comment_status()' );
     23        bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment );
     24}
     25
     26/**
     27 * Record a new blog comment in the BuddyPress activity stream.
     28 *
     29 * Only posts the item if blog is public and post is not password-protected.
     30 *
     31 * @deprecated 2.5.0
     32 *
     33 * @param int $comment_id ID of the comment being recorded.
     34 * @param bool|string $is_approved Optional. The $is_approved value passed to
     35 *        the 'comment_post' action. Default: true.
     36 * @return bool|object Returns false on failure, the comment object on success.
     37 */
     38function bp_blogs_record_comment( $comment_id, $is_approved = true ) {
     39        _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_comment()' );
     40        bp_activity_post_type_comment( $comment_id, $is_approved );
     41}
     42
     43/**
     44 * Remove a blog comment activity item from the activity stream.
     45 *
     46 * @deprecated 2.5.0
     47 *
     48 * @param int $comment_id ID of the comment to be removed.
     49 */
     50function bp_blogs_remove_comment( $comment_id ) {
     51        _deprecated_function( __FUNCTION__, '2.5.0', 'bp_activity_post_type_remove_comment()' );
     52        bp_activity_post_type_remove_comment( $comment_id );
     53}
  • src/bp-loader.php

    diff --git src/bp-loader.php src/bp-loader.php
    index 77d5408..0420c3a 100644
    class BuddyPress { 
    328328                /** Versions **********************************************************/
    329329
    330330                $this->version    = '2.5.0-alpha';
    331                 $this->db_version = 10071;
     331                $this->db_version = 10457;
    332332
    333333                /** Loading ***********************************************************/
    334334
    class BuddyPress { 
    501501                        require( $this->plugin_dir . 'bp-core/deprecated/2.2.php' );
    502502                        require( $this->plugin_dir . 'bp-core/deprecated/2.3.php' );
    503503                        require( $this->plugin_dir . 'bp-core/deprecated/2.4.php' );
     504                        require( $this->plugin_dir . 'bp-core/deprecated/2.5.php' );
    504505                }
    505506        }
    506507
  • 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..3398afa 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         */
    Bar!'; 
    14321475                $this->assertSame( array(), $found['activities'] );
    14331476        }
    14341477
     1478        /**
     1479         * @group bp_activity_post_type_comment_reset_parent_type_meta
     1480         * @group bp_blogs_new_blog_comment_query_backpat
     1481         * @group post_type_comment_activities
     1482         */
     1483        public function test_bp_activity_post_type_comment_reset_parent_type_meta() {
     1484                $blogs = array();
     1485                if ( is_multisite() ) {
     1486                        $blogs = array(
     1487                                $this->factory->blog->create(),
     1488                                $this->factory->blog->create(),
     1489                        );
     1490                }
     1491
     1492                $blogs = array_merge( array( get_current_blog_id() ), $blogs );
     1493                $synced_activity_comments = array();
     1494
     1495                $u = $this->factory->user->create();
     1496                $i = 1;
     1497
     1498                foreach ( $blogs as $blog ) {
     1499
     1500                        $a = $this->factory->activity->create( array(
     1501                                'type'    => 'new_blog_post',
     1502                                'user_id' => $u,
     1503                                'item_id' => $blog,
     1504                        ) );
     1505
     1506                        $ac = $this->factory->activity->create( array(
     1507                                'type'    => 'activity_comment',
     1508                                'user_id' => $u,
     1509                                'item_id' => $a,
     1510                        ) );
     1511
     1512                        bp_activity_update_meta( $ac, 'bp_blogs_post_comment_id', $i );
     1513
     1514                        $synced_activity_comments[ $ac ] = $i;
     1515
     1516                        $i += 1;
     1517                }
     1518
     1519                bp_activity_post_type_comment_reset_parent_type_meta();
     1520
     1521                add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     1522
     1523                global $activities_template;
     1524                $reset_activities_template = $activities_template;
     1525                bp_has_activities( array( 'action' => 'new_blog_comment' ) );
     1526
     1527                $check_activities = wp_list_pluck( $activities_template->activities, 'id' );
     1528
     1529                foreach ( $check_activities as $cid ) {
     1530                        $this->assertTrue( (int) $synced_activity_comments[ $cid ] === (int) bp_activity_get_meta( $cid, 'bp_blogs_post_comment_id' ) );
     1531                        unset( $synced_activity_comments[ $cid ] );
     1532                }
     1533
     1534                $this->assertEmpty( $synced_activity_comments );
     1535
     1536                remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     1537                $activities_template = $reset_activities_template;
     1538        }
     1539
    14351540        public function check_activity_caches() {
    14361541                foreach ( $this->acaches as $k => $v ) {
    14371542                        $this->acaches[ $k ] = wp_cache_get( $k, 'bp_activity' );
  • 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..9d957d4 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_post_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_post_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        }