Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/27/2014 05:13:29 PM (7 years ago)
Author:
imath
Message:

Post Type Activity Tracking feature

So far the tracking feature was requiring the Blogs component to be active. In 2.2 we are centralizing the majority of the tracking code into the Activity component. A new set of functions and hooks has been created to catch public post types supporting the feature 'buddypress-activity' and to automatically generate an activity when a new item is publicly published.

It's now possible to add this support to any public post type using one unique line of code, eg: add_post_type_support( 'page', 'buddypress-activity' ). In this case BuddyPress will use generic activity attributes.
Each activity attribute of the supported post type can be customized using a specific function (eg: set custom strings to describe the post type activity action).

When registering a post type in WordPress it's also possible to set the 'buddypress-activity' feature using the support parameter of the second argument of the register_post_type() function. Custom activity action strings can be defined within the labels parameter and activity attributes can be set using the new parameter 'bp_activity'.

When the Blogs component is active, the 'post' post type is automatically supporting the 'buddypress-activity' feature. The conditional logic (eg: blog_public option set to 1 ...) that occurs before a new activity is posted and the comments tracking remain unchanged.

props boonebgorges, DJPaul

Fixes #5669

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/activity/functions.php

    r9139 r9194  
    574574
    575575        $this->assertSame( $expected, $a_obj->action );
     576    }
     577
     578    /**
     579     * @group activity_action
     580     * @group bp_activity_format_activity_action_custom_post_type_post
     581     * @group activity_tracking
     582     */
     583    public function test_bp_activity_format_activity_action_custom_post_type_post_nonms() {
     584        if ( is_multisite() ) {
     585            return;
     586        }
     587
     588        $bp = buddypress();
     589
     590        register_post_type( 'foo', array(
     591            'label'   => 'foo',
     592            'public'   => true,
     593            'supports' => array( 'buddypress-activity' ),
     594        ) );
     595
     596        // Build the actions to fetch the tracking args
     597        bp_activity_get_actions();
     598
     599        $u = $this->factory->user->create();
     600        $p = $this->factory->post->create( array(
     601            'post_author' => $u,
     602            'post_type'   => 'foo',
     603        ) );
     604
     605        $a = $this->factory->activity->create( array(
     606            'component'         => 'activity',
     607            'type'              => 'new_foo',
     608            'user_id'           => $u,
     609            'item_id'           => 1,
     610            'secondary_item_id' => $p,
     611        ) );
     612
     613        $a_obj = new BP_Activity_Activity( $a );
     614
     615        $user_link = bp_core_get_userlink( $u );
     616        $blog_url = get_home_url();
     617        $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
     618        $post_link = '<a href="' . $post_url . '">item</a>';
     619
     620        $expected = sprintf( '%s wrote a new %s', $user_link, $post_link );
     621
     622        $this->assertSame( $expected, $a_obj->action );
     623
     624        _unregister_post_type( 'foo' );
     625
     626        // Reset globals
     627        unset( $bp->activity->actions->activity->new_foo );
     628        $bp->activity->track = array();
     629    }
     630
     631    /**
     632     * @group activity_action
     633     * @group bp_activity_format_activity_action_custom_post_type_post_ms
     634     * @group activity_tracking
     635     */
     636    public function test_bp_activity_format_activity_action_custom_post_type_post_ms() {
     637        if ( ! is_multisite() ) {
     638            return;
     639        }
     640
     641        $bp = buddypress();
     642
     643        $b = $this->factory->blog->create();
     644        $u = $this->factory->user->create();
     645
     646        switch_to_blog( $b );
     647
     648        register_post_type( 'foo', array(
     649            'label'   => 'foo',
     650            'public'   => true,
     651            'supports' => array( 'buddypress-activity' ),
     652        ) );
     653
     654        // Build the actions to fetch the tracking args
     655        bp_activity_get_actions();
     656
     657        $p = $this->factory->post->create( array(
     658            'post_author' => $u,
     659            'post_type'   => 'foo',
     660        ) );
     661
     662        $activity_args = array(
     663            'component'         => 'activity',
     664            'type'              => 'new_foo',
     665            'user_id'           => $u,
     666            'item_id'           => $b,
     667            'secondary_item_id' => $p,
     668        );
     669
     670        _unregister_post_type( 'foo' );
     671        bp_activity_get_actions();
     672
     673        restore_current_blog();
     674
     675        $a = $this->factory->activity->create( $activity_args );
     676
     677        $a_obj = new BP_Activity_Activity( $a );
     678
     679        $user_link = bp_core_get_userlink( $u );
     680        $blog_url = get_blog_option( $a_obj->item_id, 'home' );
     681        $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
     682
     683        $post_link = '<a href="' . $post_url . '">item</a>';
     684
     685        $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>' );
     686
     687        $this->assertSame( $expected, $a_obj->action );
     688
     689        // Reset globals
     690        unset( $bp->activity->actions->activity->new_foo );
     691        $bp->activity->track = array();
     692    }
     693
     694    /**
     695     * @group activity_action
     696     * @group bp_activity_get_actions
     697     */
     698    public function test_bp_activity_get_actions_should_sort_by_position() {
     699        $old_actions = bp_activity_get_actions();
     700        buddypress()->activity->actions = new stdClass;
     701
     702        register_post_type( 'foo5', array(
     703            'public'      => true,
     704            'supports'    => array( 'buddypress-activity' ),
     705            'bp_activity' => array(
     706                'component_id' => 'foo',
     707                'action_id' => 'foo_bar_5',
     708                'position' => 5,
     709            ),
     710        ) );
     711
     712        register_post_type( 'foo50', array(
     713            'public'      => true,
     714            'supports'    => array( 'buddypress-activity' ),
     715            'bp_activity' => array(
     716                'component_id' => 'foo',
     717                'action_id' => 'foo_bar_50',
     718                'position' => 50,
     719            ),
     720        ) );
     721
     722        register_post_type( 'foo25', array(
     723            'public'      => true,
     724            'supports'    => array( 'buddypress-activity' ),
     725            'bp_activity' => array(
     726                'component_id' => 'foo',
     727                'action_id' => 'foo_bar_25',
     728                'position' => 25,
     729            ),
     730        ) );
     731
     732        $actions = bp_activity_get_actions();
     733
     734        $expected = array(
     735            'foo_bar_5',
     736            'foo_bar_25',
     737            'foo_bar_50',
     738        );
     739        $foo_actions = (array) $actions->foo;
     740        $this->assertEquals( $expected, array_values( wp_list_pluck( $foo_actions, 'key' ) ) );
     741
     742        buddypress()->activity->actions = $old_actions;
     743    }
     744
     745    /**
     746     * @group activity_action
     747     * @group bp_activity_format_activity_action_custom_post_type_post
     748     */
     749    public function test_bp_activity_format_activity_action_custom_string_post_type_post_nonms() {
     750        if ( is_multisite() ) {
     751            return;
     752        }
     753
     754        $bp = buddypress();
     755
     756        $labels = array(
     757            'name'                 => 'bars',
     758            'singular_name'        => 'bar',
     759            'bp_activity_new_post' => '%1$s shared a new <a href="%2$s">bar</a>',
     760        );
     761
     762        register_post_type( 'foo', array(
     763            'labels'      => $labels,
     764            'public'      => true,
     765            'supports'    => array( 'buddypress-activity' ),
     766            'bp_activity' => array(
     767                'action_id' => 'foo_bar',
     768            ),
     769        ) );
     770
     771        // Build the actions to fetch the tracking args
     772        bp_activity_get_actions();
     773
     774        $u = $this->factory->user->create();
     775        $p = $this->factory->post->create( array(
     776            'post_author' => $u,
     777            'post_type'   => 'foo',
     778        ) );
     779
     780        $a = $this->factory->activity->create( array(
     781            'component'         => 'activity',
     782            'type'              => 'foo_bar',
     783            'user_id'           => $u,
     784            'item_id'           => 1,
     785            'secondary_item_id' => $p,
     786        ) );
     787
     788        $a_obj = new BP_Activity_Activity( $a );
     789
     790        $user_link = bp_core_get_userlink( $u );
     791        $blog_url = get_home_url();
     792        $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
     793
     794        $expected = sprintf( '%1$s shared a new <a href="%2$s">bar</a>', $user_link, $post_url );
     795
     796        $this->assertSame( $expected, $a_obj->action );
     797
     798        _unregister_post_type( 'foo' );
     799
     800        // Reset globals
     801        unset( $bp->activity->actions->activity->foo_bar );
     802        $bp->activity->track = array();
     803    }
     804
     805    /**
     806     * @group activity_action
     807     * @group bp_activity_format_activity_action_custom_post_type_post_ms
     808     * @group activity_tracking
     809     */
     810    public function test_bp_activity_format_activity_action_custom_string_post_type_post_ms() {
     811        if ( ! is_multisite() ) {
     812            return;
     813        }
     814
     815        $bp = buddypress();
     816        $reset = $bp->activity->actions;
     817
     818        $b = $this->factory->blog->create();
     819        $u = $this->factory->user->create();
     820
     821        switch_to_blog( $b );
     822
     823        $labels = array(
     824            'name'                    => 'bars',
     825            'singular_name'           => 'bar',
     826            'bp_activity_new_post_ms' => '%1$s shared a new <a href="%2$s">bar</a>, on the site %3$s',
     827        );
     828
     829        register_post_type( 'foo', array(
     830            'labels'   => $labels,
     831            'public'   => true,
     832            'supports' => array( 'buddypress-activity' ),
     833        ) );
     834
     835        // Build the actions to fetch the tracking args
     836        bp_activity_get_actions();
     837
     838        $p = $this->factory->post->create( array(
     839            'post_author' => $u,
     840            'post_type'   => 'foo',
     841        ) );
     842
     843        $activity_args = array(
     844            'component'         => 'activity',
     845            'type'              => 'new_foo',
     846            'user_id'           => $u,
     847            'item_id'           => $b,
     848            'secondary_item_id' => $p,
     849        );
     850
     851        _unregister_post_type( 'foo' );
     852
     853        restore_current_blog();
     854
     855        $a = $this->factory->activity->create( $activity_args );
     856
     857        $a_obj = new BP_Activity_Activity( $a );
     858
     859        $user_link = bp_core_get_userlink( $u );
     860        $blog_url = get_blog_option( $a_obj->item_id, 'home' );
     861        $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
     862
     863        $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>' );
     864
     865        $this->assertSame( $expected, $a_obj->action );
     866
     867        // Reset globals
     868        unset( $bp->activity->actions->activity->new_foo );
     869        $bp->activity->track = array();
     870    }
     871
     872    /**
     873     * @group bp_activity_set_post_type_tracking_args
     874     * @group activity_tracking
     875     */
     876    public function test_bp_activity_set_post_type_tracking_args() {
     877        $bp = buddypress();
     878
     879        add_post_type_support( 'page', 'buddypress-activity' );
     880
     881        bp_activity_set_post_type_tracking_args( 'page', array(
     882            'component_id' => $bp->blogs->id,
     883            'dummy'        => 'dummy value',
     884        ) );
     885
     886        // Build the actions to fetch the tracking args
     887        bp_activity_get_actions();
     888
     889        $u = $this->factory->user->create();
     890
     891        $post_id = $this->factory->post->create( array(
     892            'post_author' => $u,
     893            'post_status' => 'publish',
     894            'post_type'   => 'page',
     895        ) );
     896
     897        $a = bp_activity_get( array(
     898            'action'            => 'new_page',
     899            'item_id'           => get_current_blog_id(),
     900            'secondary_item_id' => $post_id,
     901        ) );
     902
     903        $this->assertSame( $bp->blogs->id, $a['activities'][0]->component );
     904
     905        remove_post_type_support( 'page', 'buddypress-activity' );
     906
     907        // Reset globals
     908        unset( $bp->activity->actions->blogs->new_page );
     909        $bp->activity->track = array();
    576910    }
    577911
Note: See TracChangeset for help on using the changeset viewer.