Skip to:
Content

BuddyPress.org

Changeset 8747


Ignore:
Timestamp:
08/01/2014 07:58:47 PM (10 years ago)
Author:
boonebgorges
Message:

Ensure that the 'Comments' dropdown filter on the activity directory includes new- and old-style comment activity

The change in BuddyPress 2.0 to storing blog comment activity in
activity_comment items resulted in the 'Comments' dropdown filter breaking: it
continued to return only 'new_blog_comment' items. This changeset introduces
a number of filters to ensure that both the appropriate 'activity_comment'
and 'new_blog_comment' items are included when passing 'action=new_blog_comment'
to bp_has_activities().

See #5608

Location:
trunk
Files:
2 edited

Legend:

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

    r8715 r8747  
    658658
    659659/**
     660 * Filter 'new_blog_comment' bp_has_activities() loop to include new- and old-style blog activity comment items.
     661 *
     662 * In BuddyPress 2.0, the schema for storing activity items related to blog
     663 * posts changed. Instead creating new top-level 'new_blog_comment' activity
     664 * items, blog comments are recorded in the activity stream as comments on the
     665 * 'new_blog_post' activity items corresponding to the parent post. This filter
     666 * ensures that the 'new_blog_comment' filter in bp_has_activities() (which
     667 * powers the 'Comments' filter in the activity directory dropdown) includes
     668 * both old-style and new-style activity comments.
     669 *
     670 * This implementation involves filtering the activity queries directly, and
     671 * should be considered a stopgap. The proper solution would involve enabling
     672 * multiple query condition clauses, connected by an OR, in the bp_has_activities()
     673 * API.
     674 *
     675 * @since BuddyPress (2.1.0)
     676 *
     677 * @param array $args Arguments passed from bp_parse_args() in bp_has_activities().
     678 * @return array $args
     679 */
     680function bp_blogs_new_blog_comment_query_backpat( $args ) {
     681    // Bail if this is not a 'new_blog_comment' query
     682    if ( 'new_blog_comment' !== $args['action'] ) {
     683        return $args;
     684    }
     685
     686    // display_comments=stream is required to show new-style
     687    // 'activity_comment' items inline
     688    $args['display_comments'] = 'stream';
     689
     690    // For the remaining clauses, we filter the SQL query directly
     691    add_filter( 'bp_activity_paged_activities_sql', '_bp_blogs_new_blog_comment_query_backpat_filter' );
     692    add_filter( 'bp_activity_total_activities_sql', '_bp_blogs_new_blog_comment_query_backpat_filter' );
     693
     694    // Return the original arguments
     695    return $args;
     696}
     697add_filter( 'bp_after_has_activities_parse_args', 'bp_blogs_new_blog_comment_query_backpat' );
     698
     699/**
     700 * Filter activity SQL to include new- and old-style 'new_blog_comment' activity items.
     701 *
     702 * @since BuddyPress (2.1.0)
     703 *
     704 * @access private
     705 * @see bp_blogs_new_blog_comment_query_backpat()
     706 *
     707 * @param string $query SQL query as assembled in BP_Activity_Activity::get().
     708 * @return string $query Modified SQL query.
     709 */
     710function _bp_blogs_new_blog_comment_query_backpat_filter( $query ) {
     711    $bp = buddypress();
     712
     713    // The query passed to the filter is for old-style 'new_blog_comment'
     714    // items. We include new-style 'activity_comment' items by running a
     715    // subquery inside of a large OR clause.
     716    $activity_comment_subquery = "SELECT a.id FROM {$bp->activity->table_name} a INNER JOIN {$bp->activity->table_name_meta} am ON (a.id = am.activity_id) WHERE am.meta_key = 'bp_blogs_post_comment_id' AND a.type = 'activity_comment'";
     717
     718    // WHERE ( [original WHERE clauses] OR a.id IN (activity_comment subquery) )
     719    $query = preg_replace( '|WHERE (.*?) ORDER|', 'WHERE ( ( $1 ) OR ( a.id IN ( ' . $activity_comment_subquery . ' ) ) ) ORDER', $query );
     720
     721    // Don't run this on future queries
     722    remove_filter( current_filter(), '_bp_blogs_new_blog_comment_query_backpat_filter' );
     723
     724    return $query;
     725}
     726
     727/**
    660728 * Utility function to set up some variables for use in the activity loop.
    661729 *
  • trunk/tests/phpunit/testcases/activity/template.php

    r8491 r8747  
    310310
    311311    /**
     312     * @group bp_has_activities
     313     */
     314    public function test_bp_has_activities_with_type_new_blog_comments() {
     315        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     316
     317        $now = time();
     318        $a1 = $this->factory->activity->create( array(
     319            'content' => 'Life Rules',
     320            'component' => 'blogs',
     321            'type' => 'new_blog_post',
     322            'recorded_time' => date( 'Y-m-d H:i:s', $now ),
     323        ) );
     324        $a2 = $this->factory->activity->create( array(
     325            'content' => 'Life Drools',
     326            'component' => 'blogs',
     327            'type' => 'new_blog_comment',
     328            'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ),
     329        ) );
     330
     331        // This one will show up in the stream because it's a comment
     332        // on a blog post
     333        $a3 = bp_activity_new_comment( array(
     334            'activity_id' => $a1,
     335            'content' => 'Candy is good',
     336            'recorded_time' => date( 'Y-m-d H:i:s', $now - 200 ),
     337        ) );
     338
     339        $a4 = $this->factory->activity->create( array(
     340            'content' => 'Life Rulez',
     341            'component' => 'activity',
     342            'type' => 'activity_update',
     343            'recorded_time' => date( 'Y-m-d H:i:s', $now - 300 ),
     344        ) );
     345
     346        // This one should not show up in the stream because it's a
     347        // comment on an activity item
     348        $a5 = bp_activity_new_comment( array(
     349            'activity_id' => $a4,
     350            'content' => 'Candy is great',
     351            'recorded_time' => date( 'Y-m-d H:i:s', $now - 400 ),
     352        ) );
     353        global $activities_template;
     354
     355        // prime
     356        bp_has_activities( array(
     357            'component' => 'blogs',
     358            'action' => 'new_blog_comment',
     359        ) );
     360
     361        $this->assertEquals( array( $a3, $a2 ), wp_parse_id_list( wp_list_pluck( $activities_template->activities, 'id' ) ) );
     362
     363        // Clean up
     364        $activities_template = null;
     365        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     366    }
     367
     368    /**
    312369     * @group bp_activity_can_comment_reply
    313370     */
Note: See TracChangeset for help on using the changeset viewer.