Skip to:
Content

BuddyPress.org

Changeset 9194


Ignore:
Timestamp:
11/27/2014 05:13:29 PM (5 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

Location:
trunk
Files:
2 added
11 edited

Legend:

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

    r9190 r9194  
    767767}
    768768add_action( 'wp_ajax_bp_get_suggestions', 'bp_ajax_get_suggestions' );
     769
     770/**
     771 * Detect a change in post type status, and initiate an activity update if necessary.
     772 *
     773 * @since BuddyPress (2.2.0)
     774 *
     775 * @todo Support untrashing better.
     776 *
     777 * @param string $new_status New status for the post.
     778 * @param string $old_status Old status for the post.
     779 * @param object $post       Post data.
     780 */
     781function bp_activity_catch_transition_post_type_status( $new_status, $old_status, $post ) {
     782    if ( ! post_type_supports( $post->post_type, 'buddypress-activity' ) ) {
     783        return;
     784    }
     785
     786    // This is an edit.
     787    if ( $new_status === $old_status ) {
     788        // An edit of an existing post should update the existing activity item.
     789        if ( $new_status == 'publish' ) {
     790            bp_activity_post_type_update( $post );
     791        }
     792
     793        return;
     794    }
     795
     796    // Publishing a previously unpublished post.
     797    if ( 'publish' === $new_status ) {
     798        // Untrashing the post type - nothing here yet.
     799        if ( 'trash' == $old_status ) {
     800            do_action( 'bp_activity_post_type_untrash_' . $post->post_type, $post );
     801        } else {
     802            // Record the post.
     803            bp_activity_post_type_publish( $post->ID, $post );
     804        }
     805
     806    // Unpublishing a previously published post.
     807    } else if ( 'publish' === $old_status ) {
     808        // Some form of pending status - only remove the activity entry
     809        bp_activity_post_type_unpublish( $post->ID, $post );
     810    }
     811}
     812add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
  • trunk/src/bp-activity/bp-activity-admin.php

    r9078 r9194  
    799799
    800800    // Walk through the registered actions, and build an array of actions/values.
    801     foreach ( buddypress()->activity->actions as $action ) {
     801    foreach ( bp_activity_get_actions() as $action ) {
    802802        $action = array_values( (array) $action );
    803803
     
    832832
    833833    // Walk through the registered actions, and build an array of actions/values.
    834     foreach ( $bp->activity->actions as $action ) {
     834    foreach ( bp_activity_get_actions() as $action ) {
    835835        $action = array_values( (array) $action );
    836836
     
    13661366
    13671367        // Get the actions
    1368         $activity_actions = buddypress()->activity->actions; ?>
     1368        $activity_actions = bp_activity_get_actions(); ?>
    13691369
    13701370        <div class="alignleft actions">
  • trunk/src/bp-activity/bp-activity-functions.php

    r9155 r9194  
    317317 * @since BuddyPress (1.1.0)
    318318 *
    319  * @param string $component_id The unique string ID of the component.
    320  * @param string $type The action type.
    321  * @param string $description The action description.
    322  * @param callable $format_callback Callback for formatting the action string.
    323  * @param string $label String to describe this action in the activity stream
    324  *        filter dropdown.
    325  * @param array $context Activity stream contexts where the filter should appear.
    326  *        'activity', 'member', 'member_groups', 'group'
     319 * @param  string   $component_id    The unique string ID of the component.
     320 * @param  string   $type            The action type.
     321 * @param  string   $description    The action description.
     322 * @param  callable $format_callback Callback for formatting the action string.
     323 * @param  string   $label           String to describe this action in the activity stream filter dropdown.
     324 * @param  array    $context         Optional. Activity stream contexts where the filter should appear.
     325 *                                   Values: 'activity', 'member', 'member_groups', 'group'
     326 * @param  int      $position        Optional. The position of the action when listed in dropdowns.
    327327 * @return bool False if any param is empty, otherwise true.
    328328 */
    329 function bp_activity_set_action( $component_id, $type, $description, $format_callback = false, $label = false, $context = array() ) {
     329function bp_activity_set_action( $component_id, $type, $description, $format_callback = false, $label = false, $context = array(), $position = 0 ) {
    330330    $bp = buddypress();
    331331
     
    369369        'label'           => $label,
    370370        'context'         => $context,
     371        'position'        => $position,
    371372    ), $component_id, $type, $description, $format_callback, $label, $context );
    372373
    373374    return true;
     375}
     376
     377/**
     378 * Set tracking arguments for a given post type.
     379 *
     380 * @since BuddyPress (2.2.0)
     381 *
     382 * @global $wp_post_types
     383 *
     384 * @param string $post_type The name of the post type, as registered with WordPress. Eg 'post' or 'page'.
     385 * @param array  $args {
     386 *     An associative array of tracking parameters. All items are optional.
     387 *
     388 *     @type string   $bp_activity_admin_filter String to use in the Dashboard > Activity dropdown.
     389 *     @type string   $bp_activity_front_filter String to use in frontend dropdown.
     390 *     @type string   $bp_activity_new_post     String format to use for generating the activity action. Should be a
     391 *                                              translatable string where %1$s is replaced by a user link and %2$s is
     392 *                                              the URL of the newly created post.
     393 *     @type string   $bp_activity_new_post_ms  String format to use for generating the activity action on Multisite.
     394 *                                              Should be a translatable string where %1$s is replaced by a user link,
     395 *                                              %2$s is the URL of the newly created post, and %3$s is a link to
     396 *                                              the site.
     397 *     @type string   $component_id             ID of the BuddyPress component to associate the activity item.
     398 *     @type string   $action_id                Value for the 'type' param of the new activity item.
     399 *     @type callable $format_callback          Callback for formatting the activity action string.
     400 *                                              Default: 'bp_activity_format_activity_action_custom_post_type_post'.
     401 *     @type array    $contexts                 The directory contexts in which the filter will show.
     402 *                                              Default: array( 'activity' ),
     403 *     @type array    $position                 Position of the item in filter dropdowns.
     404 *     @type string   $singular                 Singular, translatable name of the post type item. If no value is
     405 *                                              provided, it's pulled from the 'singular_name' of the post type.
     406 *     @type bool     $activity_comment         Whether to allow comments on the activity items. Defaults to true if
     407 *                                              the post type does not natively support comments, otherwise false.
     408 * }
     409 */
     410function bp_activity_set_post_type_tracking_args( $post_type = '', $args = array() ) {
     411    global $wp_post_types;
     412    $bp = buddypress();
     413
     414    if ( empty( $wp_post_types[ $post_type ] ) || ! post_type_supports( $post_type, 'buddypress-activity' ) || ! is_array( $args ) ) {
     415        return false;
     416    }
     417
     418    // Labels are loaded into the post type object.
     419    foreach ( array( 'bp_activity_admin_filter', 'bp_activity_front_filter', 'bp_activity_new_post', 'bp_activity_new_post_ms' ) as $label_type ) {
     420        if ( ! empty( $args[ $label_type ] ) ) {
     421            $wp_post_types[ $post_type ]->labels->{$label_type} = $args[ $label_type ];
     422            unset( $args[ $post_type ] );
     423        }
     424    }
     425
     426    // If there are any additional args, put them in the bp_activity attribute of the post type.
     427    if ( ! empty( $args ) ) {
     428        $wp_post_types[ $post_type ]->bp_activity = $args;
     429    }
     430}
     431
     432/**
     433 * Get tracking arguments for a specific post type.
     434 *
     435 * @since BuddyPress (2.2.0)
     436 *
     437 * @param  string $post_type Name of the post type
     438 * @return object The tracking arguments of the post type.
     439 */
     440function bp_activity_get_post_type_tracking_args( $post_type ) {
     441    if ( ! post_type_supports( $post_type, 'buddypress-activity' ) ) {
     442        return false;
     443    }
     444
     445    $post_type_object = get_post_type_object( $post_type );
     446
     447    $post_type_activity = array(
     448        'component_id'      => buddypress()->activity->id,
     449        'action_id'         => 'new_' . $post_type,
     450        'format_callback'   => 'bp_activity_format_activity_action_custom_post_type_post',
     451        'front_filter'      => $post_type_object->labels->name,
     452        'contexts'          => array( 'activity' ),
     453        'position'          => 0,
     454        'singular'          => strtolower( $post_type_object->labels->singular_name ),
     455        'activity_comment' => ! post_type_supports( $post_type, 'comments' ),
     456    );
     457
     458    if ( ! empty( $post_type_object->bp_activity ) ) {
     459        $post_type_activity = bp_parse_args( (array) $post_type_object->bp_activity, $post_type_activity, $post_type . '_tracking_args' );
     460    }
     461
     462    $post_type_activity = (object) $post_type_activity;
     463
     464    // Try to get the admin filter from the post type labels.
     465    if ( ! empty( $post_type_object->labels->bp_activity_admin_filter ) ) {
     466        $post_type_activity->admin_filter = $post_type_object->labels->bp_activity_admin_filter;
     467
     468    // Fall back to a generic name.
     469    } else {
     470        $post_type_activity->admin_filter = _x( 'New item published', 'Post Type generic activity post admin filter', 'buddypress' );
     471    }
     472
     473    // Check for the front filter in the post type labels.
     474    if ( ! empty( $post_type_object->labels->bp_activity_front_filter ) ) {
     475        $post_type_activity->front_filter = $post_type_object->labels->bp_activity_front_filter;
     476    }
     477
     478    // Try to get the action for new post type action on non-multisite installations.
     479    if ( ! empty( $post_type_object->labels->bp_activity_new_post ) ) {
     480        $post_type_activity->new_post_type_action = $post_type_object->labels->bp_activity_new_post;
     481    }
     482
     483    // Try to get the action for new post type action on multisite installations.
     484    if ( ! empty( $post_type_object->labels->bp_activity_new_post_ms ) ) {
     485        $post_type_activity->new_post_type_action_ms = $post_type_object->labels->bp_activity_new_post_ms;
     486    }
     487
     488    return apply_filters( 'bp_activity_get_post_type_tracking_args', $post_type_activity, $post_type );
     489}
     490
     491/**
     492 * Get tracking arguments for all post types.
     493 *
     494 * @since BuddyPress (2.2.0)
     495 *
     496 * @return array List of post types with their tracking arguments.
     497 */
     498function bp_activity_get_post_types_tracking_args() {
     499    // Fetch all public post types
     500    $post_types = get_post_types( array( 'public' => true ), 'names' );
     501
     502    $post_types_tracking_args = array();
     503
     504    foreach ( $post_types as $post_type ) {
     505        $track_post_type = bp_activity_get_post_type_tracking_args( $post_type );
     506
     507        if ( ! empty( $track_post_type ) ) {
     508            $post_types_tracking_args[ $track_post_type->action_id ] = $track_post_type;
     509        }
     510
     511    }
     512
     513    return apply_filters( 'bp_activity_get_post_types_tracking_args', $post_types_tracking_args );
     514}
     515
     516/**
     517 * Get all components' activity actions, sorted by their position attribute.
     518 *
     519 * @since BuddyPress (2.2.0)
     520 *
     521 * @return object actions ordered by their position
     522 */
     523function bp_activity_get_actions() {
     524    $bp = buddypress();
     525
     526    $post_types = bp_activity_get_post_types_tracking_args();
     527
     528    // Create the actions for the post types, if they haven't already been created.
     529    if ( ! empty( $post_types ) ) {
     530        foreach ( $post_types as $post_type ) {
     531            if ( isset( $bp->activity->actions->{$post_type->component_id}->{$post_type->action_id} ) ) {
     532                continue;
     533            }
     534
     535            bp_activity_set_action(
     536                $post_type->component_id,
     537                $post_type->action_id,
     538                $post_type->admin_filter,
     539                $post_type->format_callback,
     540                $post_type->front_filter,
     541                $post_type->contexts,
     542                $post_type->position
     543            );
     544        }
     545    }
     546
     547    // Sort the actions by their position within each component.
     548    foreach ( $bp->activity->actions as $component => $actions ) {
     549        $actions = (array) $actions;
     550        $temp = bp_sort_by_key( $actions, 'position', 'num' );
     551
     552        // Restore keys.
     553        $bp->activity->actions->{$component} = new stdClass;
     554        foreach ( $temp as $key_ordered ) {
     555            $bp->activity->actions->{$component}->{$key_ordered['key']} = $key_ordered;
     556        }
     557    }
     558
     559    return $bp->activity->actions;
    374560}
    375561
     
    392578    }
    393579
    394     $bp     = buddypress();
    395     $retval = isset( $bp->activity->actions->{$component_id}->{$key} )
    396         ? $bp->activity->actions->{$component_id}->{$key}
    397         : false;
     580    $bp      = buddypress();
     581    $actions = bp_activity_get_actions();
     582
     583    $retval = false;
     584    if ( isset( $actions->{$component_id}->{$key} ) ) {
     585        $retval = $actions->{$component_id}->{$key};
     586    }
    398587
    399588    /**
     
    402591     * @since BuddyPress (1.1.0)
    403592     *
    404      * @param string|bool $retval The action key.
     593     * @param string|bool $retval       The action key.
    405594     * @param string      $component_id The unique string ID of the component.
    406      * @param string      $key The action key.
     595     * @param string      $key          The action key.
    407596     */
    408597    return apply_filters( 'bp_activity_get_action', $retval, $component_id, $key );
     
    420609
    421610    // Walk through the registered actions, and build an array of actions/values.
    422     foreach ( buddypress()->activity->actions as $action ) {
     611    foreach ( bp_activity_get_actions() as $action ) {
    423612        $action = array_values( (array) $action );
    424613
     
    11291318     */
    11301319    return apply_filters( 'bp_activity_comment_action', $action, $activity );
     1320}
     1321
     1322/**
     1323 * Format activity action strings for custom post types.
     1324 *
     1325 * @since BuddyPress (2.2.0)
     1326 *
     1327 * @param string $action   Static activity action.
     1328 * @param object $activity Activity data object.
     1329 * @return string
     1330 */
     1331function bp_activity_format_activity_action_custom_post_type_post( $action, $activity ) {
     1332    $bp = buddypress();
     1333
     1334    // Fetch all the tracked post types once.
     1335    if ( empty( $bp->activity->track ) ) {
     1336        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1337    }
     1338
     1339    if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) {
     1340        return $action;
     1341    }
     1342
     1343    $user_link = bp_core_get_userlink( $activity->user_id );
     1344    $blog_url  = get_home_url( $activity->item_id );
     1345
     1346    if ( empty( $activity->post_url ) ) {
     1347        $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) );
     1348    } else {
     1349        $post_url = $activity->post_url;
     1350    }
     1351
     1352    if ( is_multisite() ) {
     1353        $blog_link = '<a href="' . $blog_url . '">' . get_blog_option( $activity->item_id, 'blogname' ) . '</a>';
     1354
     1355        if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_action_ms ) ) {
     1356            $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_action_ms, $user_link, $post_url, $blog_link );
     1357        } else {
     1358            $action = sprintf( _x( '%1$s wrote a new <a href="%2$s">item</a>, on the site %3$s', 'Activity Custom Post Type post action', 'buddypress' ), $user_link, $post_url, $blog_link );
     1359        }
     1360    } else {
     1361        if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_action ) ) {
     1362            $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_action, $user_link, $post_url );
     1363        } else {
     1364            $action = sprintf( _x( '%1$s wrote a new <a href="%2$s">item</a>', 'Activity Custom Post Type post action', 'buddypress' ), $user_link, $post_url );
     1365        }
     1366    }
     1367
     1368    /**
     1369     * Filters the formatted custom post type activity post action string.
     1370     *
     1371     * @since BuddyPress (2.2.0)
     1372     *
     1373     * @param string               $action Activity action string value.
     1374     * @param BP_Activity_Activity $activity Activity item object.
     1375     */
     1376    return apply_filters( 'bp_activity_custom_post_type_post_action', $action, $activity );
    11311377}
    11321378
     
    15141760
    15151761/**
     1762 * Create an activity item for a newly published post type post.
     1763 *
     1764 * @since BuddyPress (2.2.0)
     1765 *
     1766 * @param  int      $post_id ID of the new post.
     1767 * @param  WP_Post  $post    Post object.
     1768 * @param  int      $user_id ID of the post author.
     1769 * @return int|bool The ID of the activity on success. False on error.
     1770 */
     1771function bp_activity_post_type_publish( $post_id = 0, $post = null, $user_id = 0 ) {
     1772    $bp = buddypress();
     1773
     1774    if ( ! is_a( $post, 'WP_Post' ) ) {
     1775        return;
     1776    }
     1777
     1778    // Get the post type tracking args.
     1779    $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type );
     1780
     1781    if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $activity_post_object->action_id ) ) {
     1782        return;
     1783    }
     1784
     1785    if ( empty( $post_id ) ) {
     1786        $post_id = $post->ID;
     1787    }
     1788
     1789    $blog_id = get_current_blog_id();
     1790
     1791    if ( empty( $user_id ) ) {
     1792        $user_id = (int) $post->post_author;
     1793    }
     1794
     1795    // Bail if an activity item already exists for this post.
     1796    $existing = bp_activity_get( array(
     1797        'filter' => array(
     1798            'action'       => $activity_post_object->action_id,
     1799            'primary_id'   => $blog_id,
     1800            'secondary_id' => $post_id,
     1801        )
     1802    ) );
     1803
     1804    if ( ! empty( $existing['activities'] ) ) {
     1805        return;
     1806    }
     1807
     1808    // Let components/plugins bail before the activity is posted.
     1809    if ( false === apply_filters( "bp_activity_{$post->post_type}_pre_publish", true, $blog_id, $post_id, $user_id ) ) {
     1810        return;
     1811    }
     1812
     1813    // Record this in activity streams.
     1814    $blog_url = get_home_url( $blog_id );
     1815    $post_url = add_query_arg(
     1816        'p',
     1817        $post_id,
     1818        trailingslashit( $blog_url )
     1819    );
     1820
     1821    // Backward compatibility filters for the 'blogs' component.
     1822    if ( 'blogs' == $activity_post_object->component_id )  {
     1823        $activity_content      = apply_filters( 'bp_blogs_activity_new_post_content', $post->post_content, $post, $post_url, $post->post_type );
     1824        $activity_primary_link = apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_url, $post_id, $post->post_type );
     1825    } else {
     1826        $activity_content      = $post->post_content;
     1827        $activity_primary_link = $post_url;
     1828    }
     1829
     1830    $activity_args = array(
     1831        'user_id'           => $user_id,
     1832        'content'           => $activity_content,
     1833        'primary_link'      => $activity_primary_link,
     1834        'component'         => $activity_post_object->component_id,
     1835        'type'              => $activity_post_object->action_id,
     1836        'item_id'           => $blog_id,
     1837        'secondary_item_id' => $post_id,
     1838        'recorded_time'     => $post->post_date_gmt,
     1839    );
     1840
     1841    // Remove large images and replace them with just one image thumbnail.
     1842    if ( ! empty( $activity_args['content'] ) ) {
     1843        $activity_args['content'] = bp_activity_thumbnail_content_images( $activity_args['content'], $activity_args['primary_link'], $activity_args );
     1844    }
     1845
     1846    if ( ! empty( $activity_args['content'] ) ) {
     1847        // Create the excerpt.
     1848        $activity_excerpt = bp_create_excerpt( $activity_args['content'] );
     1849
     1850        // Backward compatibility filter for blog posts.
     1851        if ( 'blogs' == $activity_post_object->component_id )  {
     1852            $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_excerpt, $activity_args['content'], $activity_args, $post->post_type );
     1853        } else {
     1854            $activity_args['content'] = $activity_excerpt;
     1855        }
     1856    }
     1857
     1858    // Set up the action by using the format functions.
     1859    $action_args = array_merge( $activity_args, array(
     1860        'post_title' => $post->post_title,
     1861        'post_url'   => $post_url,
     1862    ) );
     1863
     1864    $activity_args['action'] = call_user_func_array( $activity_post_object->format_callback, array( '', (object) $action_args ) );
     1865
     1866    // Make sure the action is set.
     1867    if ( empty( $activity_args['action'] ) ) {
     1868        return;
     1869    } else {
     1870        // Backward compatibility filter for the blogs component.
     1871        if ( 'blogs' == $activity_post_object->component_id )  {
     1872            $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] );
     1873        }
     1874    }
     1875
     1876    $activity_id = bp_activity_add( $activity_args );
     1877
     1878    do_action( 'bp_activity_post_type_published', $activity_id, $post, $activity_args );
     1879
     1880    return $activity_id;
     1881}
     1882
     1883/**
     1884 * Update the activity item for a custom post type entry.
     1885 *
     1886 * @since BuddyPress (2.2.0)
     1887 *
     1888 * @param  WP_Post $post Post item.
     1889 * @return bool    True on success, false on failure.
     1890 */
     1891function bp_activity_post_type_update( $post = null ) {
     1892    $bp = buddypress();
     1893
     1894    if ( ! is_a( $post, 'WP_Post' ) ) {
     1895        return;
     1896    }
     1897
     1898    // Get the post type tracking args.
     1899    $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type );
     1900
     1901    if ( empty( $activity_post_object->action_id ) ) {
     1902        return;
     1903    }
     1904
     1905    $activity_id = bp_activity_get_activity_id( array(
     1906        'component'         => $activity_post_object->component_id,
     1907        'item_id'           => get_current_blog_id(),
     1908        'secondary_item_id' => $post->ID,
     1909        'type'              => $activity_post_object->action_id,
     1910    ) );
     1911
     1912    // Activity ID doesn't exist, so stop!
     1913    if ( empty( $activity_id ) ) {
     1914        return;
     1915    }
     1916
     1917    // Delete the activity if the post was updated with a password.
     1918    if ( ! empty( $post->post_password ) ) {
     1919        bp_activity_delete( array( 'id' => $activity_id ) );
     1920    }
     1921
     1922    // Update the activity entry.
     1923    $activity = new BP_Activity_Activity( $activity_id );
     1924
     1925    if ( ! empty( $post->post_content ) ) {
     1926        // Make sure to update the thumbnail image.
     1927        $post_content = bp_activity_thumbnail_content_images( $post->post_content, $activity->primary_link, (array) $activity );
     1928
     1929        // Generate an excerpt.
     1930        $activity_excerpt = bp_create_excerpt( $post_content );
     1931
     1932        // Backward compatibility filter for the blogs component.
     1933        if ( 'blogs' == $activity_post_object->component_id ) {
     1934            $activity->content = apply_filters( 'bp_blogs_record_activity_content', $activity_excerpt, $post_content, (array) $activity, $post->post_type );
     1935        } else {
     1936            $activity->content = $activity_excerpt;
     1937        }
     1938    }
     1939
     1940    // Save the updated activity.
     1941    $updated = $activity->save();
     1942
     1943    do_action( 'bp_activity_post_type_updated', $post, $activity );
     1944
     1945    return $updated;
     1946}
     1947
     1948/**
     1949 * Unpublish an activity for the custom post type.
     1950 *
     1951 * @since BuddyPress (2.2.0)
     1952 *
     1953 * @param  int     $post_id ID of the post being unpublished.
     1954 * @param  WP_Post $post    Post object.
     1955 * @return bool    True on success, false on failure.
     1956 */
     1957function bp_activity_post_type_unpublish( $post_id = 0, $post = null ) {
     1958    $bp = buddypress();
     1959
     1960    if ( ! is_a( $post, 'WP_Post' ) ) {
     1961        return;
     1962    }
     1963
     1964    // Get the post type tracking args
     1965    $activity_post_object = bp_activity_get_post_type_tracking_args( $post->post_type );
     1966
     1967    if ( empty( $activity_post_object->action_id ) ) {
     1968        return;
     1969    }
     1970
     1971    if ( empty( $post_id ) ) {
     1972        $post_id = $post->ID;
     1973    }
     1974
     1975    $delete_activity_args = array(
     1976        'item_id'           => get_current_blog_id(),
     1977        'secondary_item_id' => $post_id,
     1978        'component'         => $activity_post_object->component_id,
     1979        'type'              => $activity_post_object->action_id,
     1980        'user_id'           => false,
     1981    );
     1982
     1983    $deleted = bp_activity_delete_by_item_id( $delete_activity_args );
     1984
     1985    do_action( 'bp_activity_post_type_unpublished', $delete_activity_args, $post, $deleted );
     1986
     1987    return $deleted;
     1988}
     1989
     1990/**
    15161991 * Add an activity comment.
    15171992 *
     
    19892464 */
    19902465function bp_activity_get_permalink( $activity_id, $activity_obj = false ) {
     2466    $bp = buddypress();
    19912467
    19922468    if ( empty( $activity_obj ) ) {
     
    19982474    }
    19992475
    2000     if ( 'new_blog_post' == $activity_obj->type || 'new_blog_comment' == $activity_obj->type || 'new_forum_topic' == $activity_obj->type || 'new_forum_post' == $activity_obj->type ) {
     2476    $use_primary_links = array(
     2477        'new_blog_post',
     2478        'new_blog_comment',
     2479        'new_forum_topic',
     2480        'new_forum_post',
     2481    );
     2482
     2483    if ( ! empty( $bp->activity->track ) ) {
     2484        $use_primary_links = array_merge( $use_primary_links, array_keys( $bp->activity->track ) );
     2485    }
     2486
     2487    if ( false !== array_search( $activity_obj->type, $use_primary_links ) ) {
    20012488        $link = $activity_obj->primary_link;
    20022489    } else {
  • trunk/src/bp-activity/bp-activity-template.php

    r9169 r9194  
    33163316function bp_activity_can_comment() {
    33173317    global $activities_template;
     3318    $bp = buddypress();
    33183319
    33193320    // Assume activity can be commented on
     
    33223323    // Determine ability to comment based on activity action name
    33233324    $activity_action = bp_get_activity_action_name();
    3324     switch ( $activity_action ) {
    3325 
    3326         // Maybe turn off for blog and forum updates
    3327         case 'new_blog_post'    :
    3328         case 'new_blog_comment' :
    3329         case 'new_forum_topic'  :
    3330         case 'new_forum_post'   :
    3331             if ( ! empty( $activities_template->disable_blogforum_replies ) ) {
    3332                 $can_comment = false;
    3333             }
    3334             break;
    3335 
    3336         // Turn off for activity comments
    3337         case 'activity_comment' :
    3338             $can_comment = false;
    3339             break;
    3340     }
     3325
     3326    $turn_off = 0;
     3327    if ( ! empty( $activities_template->disable_blogforum_replies ) ) {
     3328        $turn_off = 1;
     3329    }
     3330
     3331    $maybe_turn_off = array_fill_keys( array(
     3332        'new_blog_post',
     3333        'new_blog_comment',
     3334        'new_forum_topic',
     3335        'new_forum_post',
     3336    ), $turn_off );
     3337
     3338    $maybe_turn_off['activity_comment'] = 1;
     3339
     3340    // Fetch all the tracked post types once.
     3341    if ( empty( $bp->activity->track ) ) {
     3342        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     3343    }
     3344
     3345    foreach ( $bp->activity->track as $action => $tracking_args ) {
     3346        if ( empty( $tracking_args->activity_comment ) ) {
     3347            $maybe_turn_off[ $action ] = $turn_off;
     3348        }
     3349    }
     3350
     3351    $can_comment = empty( $maybe_turn_off[ $activity_action ] );
    33413352
    33423353    /**
     
    43684379        // Walk through the registered actions, and prepare an the
    43694380        // select box options.
    4370         foreach ( buddypress()->activity->actions as $actions ) {
     4381        foreach ( bp_activity_get_actions() as $actions ) {
    43714382            foreach ( $actions as $action ) {
    43724383                if ( ! in_array( $context, (array) $action['context'] ) ) {
  • trunk/src/bp-blogs/bp-blogs-activity.php

    r8747 r9194  
    3434            __( 'New site created', 'buddypress' ),
    3535            'bp_blogs_format_activity_action_new_blog',
    36             __( 'New Sites', 'buddypress' )
     36            __( 'New Sites', 'buddypress' ),
     37            0
    3738        );
    3839    }
    3940
    40     bp_activity_set_action(
    41         $bp->blogs->id,
    42         'new_blog_post',
    43         __( 'New post published', 'buddypress' ),
    44         'bp_blogs_format_activity_action_new_blog_post',
    45         __( 'Posts', 'buddypress' ),
    46         array( 'activity', 'member' )
    47     );
    48 
    49     bp_activity_set_action(
    50         $bp->blogs->id,
    51         'new_blog_comment',
    52         __( 'New post comment posted', 'buddypress' ),
    53         'bp_blogs_format_activity_action_new_blog_comment',
    54         __( 'Comments', 'buddypress' ),
    55         array( 'activity', 'member' )
    56     );
     41    // Only add the comment type if the 'post' post type is trackable
     42    if ( post_type_supports( 'post', 'buddypress-activity' ) ) {
     43        bp_activity_set_action(
     44            $bp->blogs->id,
     45            'new_blog_comment',
     46            __( 'New post comment posted', 'buddypress' ),
     47            'bp_blogs_format_activity_action_new_blog_comment',
     48            __( 'Comments', 'buddypress' ),
     49            array( 'activity', 'member' ),
     50            10
     51        );
     52    }
    5753
    5854    do_action( 'bp_blogs_register_activity_actions' );
     
    109105    }
    110106
    111     $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) );
    112 
    113     $post_title = bp_activity_get_meta( $activity->id, 'post_title' );
     107    if ( empty( $activity->post_url ) ) {
     108        $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) );
     109    } else {
     110        $post_url = $activity->post_url;
     111    }
     112
     113    if ( empty( $activity->post_title ) ) {
     114        $post_title = bp_activity_get_meta( $activity->id, 'post_title' );
     115    } else {
     116        $post_title = $activity->post_title;
     117    }
    114118
    115119    // Should only be empty at the time of post creation
  • trunk/src/bp-blogs/bp-blogs-filters.php

    r8945 r9194  
    4343 * @since BuddyPress (2.1.0)
    4444 *
    45  * @see bp_blogs_update_post()
     45 * @see bp_blogs_update_post_activity_meta()
    4646 *
    4747 * @param array Current SQL clauses in array format
     
    5050function bp_blogs_comments_clauses_select_by_id( $retval ) {
    5151    $retval['fields'] = 'comment_ID';
    52    
     52
    5353    return $retval;
    5454}
     55
     56/**
     57 * Check whether the current post can be published.
     58 *
     59 * Abstracted from the deprecated `bp_blogs_record_post()`.
     60 *
     61 * @since BuddyPress (2.2.0)
     62 *
     63 * @param  bool $return  Whether the post should be published.
     64 * @param  int  $blog_id ID of the blog.
     65 * @param  int  $post_id ID of the post.
     66 * @param  int  $user_id ID of the post author.
     67 * @return bool True to authorize the post to be published, otherwise false.
     68 */
     69function bp_blogs_post_pre_publish( $return = true, $blog_id = 0, $post_id = 0, $user_id = 0 ) {
     70    $bp = buddypress();
     71
     72    // If blog is not trackable, do not record the activity.
     73    if ( ! bp_blogs_is_blog_trackable( $blog_id, $user_id ) ) {
     74        return false;
     75    }
     76
     77    /*
     78     * Stop infinite loops with WordPress MU Sitewide Tags.
     79     * That plugin changed the way its settings were stored at some point. Thus the dual check.
     80     */
     81    if ( ! empty( $bp->site_options['sitewide_tags_blog'] ) ) {
     82        $st_options = maybe_unserialize( $bp->site_options['sitewide_tags_blog'] );
     83        $tags_blog_id = isset( $st_options['tags_blog_id'] ) ? $st_options['tags_blog_id'] : 0;
     84    } else {
     85        $tags_blog_id = isset( $bp->site_options['tags_blog_id'] ) ? $bp->site_options['tags_blog_id'] : 0;
     86    }
     87
     88    if ( (int) $blog_id == $tags_blog_id && apply_filters( 'bp_blogs_block_sitewide_tags_activity', true ) ) {
     89        return false;
     90    }
     91
     92    $is_blog_public = apply_filters( 'bp_is_blog_public', (int) get_blog_option( $blog_id, 'blog_public' ) );
     93
     94    if ( 0 === $is_blog_public && is_multisite() ) {
     95        return false;
     96    }
     97
     98    return $return;
     99}
     100add_filter( 'bp_activity_post_pre_publish', 'bp_blogs_post_pre_publish', 10, 4 );
  • trunk/src/bp-blogs/bp-blogs-functions.php

    r9039 r9194  
    403403
    404404/**
    405  * Detect a change in post status, and initiate an activity update if necessary.
    406  *
    407  * Posts get new activity updates when (a) they are being published, and (b)
    408  * they have not already been published. This enables proper posting for
    409  * regular posts as well as scheduled posts, while preventing post bumping.
    410  *
    411  * See #4090, #3746, #2546 for background.
    412  *
    413  * @since BuddyPress (2.0.0)
    414  *
    415  * @todo Support untrashing better
    416  *
    417  * @param string $new_status New status for the post.
    418  * @param string $old_status Old status for the post.
    419  * @param object $post Post data.
    420  */
    421 function bp_blogs_catch_transition_post_status( $new_status, $old_status, $post ) {
    422 
    423     // This is an edit
    424     if ( $new_status === $old_status ) {
    425         if ( $new_status == 'publish' ) {
    426             bp_blogs_update_post( $post );
    427             return;
    428         }
    429     }
    430 
    431     // Publishing a previously unpublished post
    432     if ( 'publish' === $new_status ) {
    433         // Untrashing the post
    434         // Nothing here yet
    435         if ( 'trash' == $old_status ) {}
    436 
    437         // Record the post
    438         bp_blogs_record_post( $post->ID, $post );
    439 
    440     // Unpublishing a previously published post
    441     } else if ( 'publish' === $old_status ) {
    442         // Some form of pending status
    443         // Only remove the activity entry
    444         bp_blogs_delete_activity( array(
    445             'item_id'           => get_current_blog_id(),
    446             'secondary_item_id' => $post->ID,
    447             'component'         => buddypress()->blogs->id,
    448             'type'              => 'new_blog_post'
    449         ) );
    450     }
    451 }
    452 add_action( 'transition_post_status', 'bp_blogs_catch_transition_post_status', 10, 3 );
    453 
    454 /**
    455  * Record a new blog post in the BuddyPress activity stream.
    456  *
    457  * @param int $post_id ID of the post being recorded.
    458  * @param object $post The WP post object passed to the 'save_post' action.
    459  * @param int $user_id Optional. The user to whom the activity item will be
    460  *        associated. Defaults to the post_author.
    461  * @return bool|null Returns false on failure.
    462  */
    463 function bp_blogs_record_post( $post_id, $post, $user_id = 0 ) {
    464     global $bp, $wpdb;
    465 
    466     $post_id = (int) $post_id;
    467     $blog_id = (int) $wpdb->blogid;
    468 
    469     // If blog is not trackable, do not record the activity.
    470     if ( ! bp_blogs_is_blog_trackable( $blog_id, $user_id ) )
    471         return false;
    472 
    473     if ( !$user_id )
    474         $user_id = (int) $post->post_author;
    475 
    476     // Stop infinite loops with WordPress MU Sitewide Tags.
    477     // That plugin changed the way its settings were stored at some point. Thus the dual check.
    478     if ( !empty( $bp->site_options['sitewide_tags_blog'] ) ) {
    479         $st_options = maybe_unserialize( $bp->site_options['sitewide_tags_blog'] );
    480         $tags_blog_id = isset( $st_options['tags_blog_id'] ) ? $st_options['tags_blog_id'] : 0;
     405 * Record activity metadata about a published blog post.
     406 *
     407 * @since BuddyPress (2.2.0)
     408 *
     409 * @param  int     $activity_id ID of the acitvity item.
     410 * @param  WP_Post $post        Post object.
     411 */
     412function bp_blogs_publish_post_activity_meta( $activity_id, $post, $args ) {
     413    if ( empty( $activity_id ) || 'post' != $post->post_type ) {
     414        return;
     415    }
     416
     417    bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
     418
     419    if ( ! empty( $args['post_url'] ) ) {
     420        $post_permalink = $args['post_url'];
    481421    } else {
    482         $tags_blog_id = isset( $bp->site_options['tags_blog_id'] ) ? $bp->site_options['tags_blog_id'] : 0;
    483     }
    484 
    485     if ( (int) $blog_id == $tags_blog_id && apply_filters( 'bp_blogs_block_sitewide_tags_activity', true ) )
    486         return false;
    487 
    488     // Don't record this if it's not a post
    489     if ( !in_array( $post->post_type, apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) ) ) )
    490         return false;
    491 
    492     $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );
    493 
    494     if ( 'publish' == $post->post_status && empty( $post->post_password ) ) {
    495         if ( $is_blog_public || !is_multisite() ) {
    496 
    497             // Record this in activity streams
    498             $post_permalink = add_query_arg(
    499                 'p',
    500                 $post_id,
    501                 trailingslashit( get_home_url( $blog_id ) )
    502             );
    503 
    504             if ( is_multisite() )
    505                 $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
    506             else
    507                 $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
    508 
    509             // Make sure there's not an existing entry for this post (prevent bumping)
    510             if ( bp_is_active( 'activity' ) ) {
    511                 $existing = bp_activity_get( array(
    512                     'filter' => array(
    513                         'action'       => 'new_blog_post',
    514                         'primary_id'   => $blog_id,
    515                         'secondary_id' => $post_id,
    516                     )
    517                 ) );
    518 
    519                 if ( !empty( $existing['activities'] ) ) {
    520                     return;
    521                 }
    522             }
    523 
    524             $activity_content = $post->post_content;
    525 
    526             $activity_id = bp_blogs_record_activity( array(
    527                 'user_id'           => (int) $post->post_author,
    528                 'content'           => apply_filters( 'bp_blogs_activity_new_post_content',      $activity_content, $post, $post_permalink ),
    529                 'primary_link'      => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink,   $post_id               ),
    530                 'type'              => 'new_blog_post',
    531                 'item_id'           => $blog_id,
    532                 'secondary_item_id' => $post_id,
    533                 'recorded_time'     => $post->post_date_gmt,
    534             ) );
    535 
    536             // save post title in activity meta
    537             if ( bp_is_active( 'activity' ) ) {
    538                 bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
    539                 bp_activity_update_meta( $activity_id, 'post_url',   $post_permalink );
    540             }
    541         }
    542 
    543         // Update the blogs last activity
    544         bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    545     } else {
    546         bp_blogs_remove_post( $post_id, $blog_id, $user_id );
    547     }
    548 
    549     do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
    550 }
    551 
    552 /**
    553  * Updates a blog post's corresponding activity entry during a post edit.
    554  *
    555  * @since BuddyPress (2.0.0)
    556  *
    557  * @see bp_blogs_catch_transition_post_status()
    558  *
    559  * @param WP_Post $post
    560  */
    561 function bp_blogs_update_post( $post ) {
    562     if ( ! bp_is_active( 'activity' ) ) {
     422        $post_permalink = $post->guid;
     423    }
     424
     425    bp_activity_update_meta( $activity_id, 'post_url',   $post_permalink );
     426
     427    // Update the blog's last activity.
     428    bp_blogs_update_blogmeta( $args['item_id'], 'last_activity', bp_core_current_time() );
     429
     430    do_action( 'bp_blogs_new_blog_post', $post->ID, $post, $args['user_id'] );
     431}
     432add_action( 'bp_activity_post_type_published', 'bp_blogs_publish_post_activity_meta', 10, 3 );
     433
     434/**
     435 * Updates a blog post's activity meta entry during a post edit.
     436 *
     437 * @since BuddyPress (2.2.0)
     438 *
     439 * @param WP_Post              $post     Post object.
     440 * @param BP_Actitivy_Activity $activity Activity object.
     441 */
     442function bp_blogs_update_post_activity_meta( $post, $activity ) {
     443    if ( empty( $activity->id ) || 'post' != $post->post_type ) {
    563444        return;
    564445    }
    565446
    566     $activity_id = bp_activity_get_activity_id( array(
    567         'component'         => buddypress()->blogs->id,
    568         'item_id'           => get_current_blog_id(),
    569         'secondary_item_id' => $post->ID,
    570         'type'              => 'new_blog_post',
    571      ) );
    572 
    573     // activity ID doesn't exist, so stop!
    574     if ( empty( $activity_id ) ) {
    575         return;
    576     }
    577 
    578     // update the activity entry
    579     $activity = new BP_Activity_Activity( $activity_id );
    580 
    581     if ( ! empty( $post->post_content ) ) {
    582         // Make sure to update the thumbnail image
    583         $post_content = bp_activity_thumbnail_content_images( $post->post_content, $activity->primary_link, (array) $activity );
    584 
    585         // Make sure to apply the blop post excerpt
    586         $activity->content = apply_filters( 'bp_blogs_record_activity_content', bp_create_excerpt( $post_content ), $post_content, (array) $activity );
    587     }
    588 
    589     // Save the updated activity
    590     $activity->save();
    591 
    592     // update post title in activity meta
    593     $existing_title = bp_activity_get_meta( $activity_id, 'post_title' );
     447    // Update post title in activity meta.
     448    $existing_title = bp_activity_get_meta( $activity->id, 'post_title' );
    594449    if ( $post->post_title !== $existing_title ) {
    595         bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
    596 
    597         // now update activity meta for post comments... sigh
     450        bp_activity_update_meta( $activity->id, 'post_title', $post->post_title );
     451
     452        // Now update activity meta for post comments... sigh.
    598453        add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
    599454        $comments = get_comments( array( 'post_id' => $post->ID ) );
     
    604459            $comment_ids  = wp_list_pluck( $comments, 'comment_ID' );
    605460
    606             // setup activity args
     461            // Set up activity args.
    607462            $args = array(
    608463                'update_meta_cache' => false,
     
    611466            );
    612467
    613             // query for old-style "new_blog_comment" activity items
     468            // Query for old-style "new_blog_comment" activity items.
    614469            $args['filter'] = array(
    615470                'object'       => buddypress()->blogs->id,
     
    623478            }
    624479
    625             // query for activity comments connected to a blog post
     480            // Query for activity comments connected to a blog post.
    626481            unset( $args['filter'] );
    627482            $args['meta_query'] = array( array(
     
    638493            }
    639494
    640             // update activity meta for all found activity items
     495            // Update activity meta for all found activity items.
    641496            if ( ! empty( $activity_ids ) ) {
    642497                foreach ( $activity_ids as $aid ) {
     
    649504    }
    650505
    651     // add post comment status to activity meta if closed
     506    // Add post comment status to activity meta if closed.
    652507    if( 'closed' == $post->comment_status ) {
    653         bp_activity_update_meta( $activity_id, 'post_comment_status', $post->comment_status );
     508        bp_activity_update_meta( $activity->id, 'post_comment_status', $post->comment_status );
    654509    } else {
    655         bp_activity_delete_meta( $activity_id, 'post_comment_status' );
    656     }
    657 }
     510        bp_activity_delete_meta( $activity->id, 'post_comment_status' );
     511    }
     512}
     513add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 2 );
    658514
    659515/**
  • trunk/src/bp-blogs/bp-blogs-loader.php

    r8662 r9194  
    7676        // Setup the globals
    7777        parent::setup_globals( $args );
     78
     79        /*
     80         * Set up the post post type to track.
     81         *
     82         * In case the config is not multisite, the blog_public option is ignored.
     83         */
     84        if ( 0 !== (int) get_option( 'blog_public' ) || ! is_multisite() ) {
     85            // Get all posts to track.
     86            $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) );
     87
     88            foreach ( $post_types as $post_type ) {
     89                add_post_type_support( $post_type, 'buddypress-activity' );
     90            }
     91        }
     92
     93        // Filter the generic track parameters for the 'post' post type.
     94        add_filter( 'bp_activity_get_post_type_tracking_args', array( $this, 'post_tracking_args' ), 10, 2 );
    7895    }
    7996
     
    247264        parent::setup_title();
    248265    }
     266
     267    /**
     268     * Set up the tracking arguments for the 'post' post type.
     269     *
     270     * @since BuddyPress (2.2.0)
     271     *
     272     * @see bp_activity_get_post_type_tracking_args() for information on parameters.
     273     */
     274    public function post_tracking_args( $params = array(), $post_type = 0 ) {
     275        if ( 'post' != $post_type ) {
     276            return $params;
     277        }
     278
     279        // Set specific params for the 'post' post type.
     280        $params->component_id    = $this->id;
     281        $params->action_id       = 'new_blog_post';
     282        $params->admin_filter    = __( 'New post published', 'buddypress' );
     283        $params->format_callback = 'bp_blogs_format_activity_action_new_blog_post';
     284        $params->front_filter    = __( 'Posts', 'buddypress' );
     285        $params->contexts        = array( 'activity', 'member' );
     286        $params->position        = 5;
     287
     288        return $params;
     289    }
    249290}
    250291
  • 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
  • trunk/tests/phpunit/testcases/blogs/activity.php

    r9139 r9194  
    22
    33class BP_Tests_Blogs_Activity extends BP_UnitTestCase {
     4    /**
     5     * @group bp_blogs_register_activity_actions
     6     * @group activity_tracking
     7     */
     8    public function test_bp_blogs_loader_post_tracking_args_filter() {
     9        $bp = buddypress();
     10
     11        $expected = array( 'new_blog_post', 'new_blog_comment' );
     12
     13        if ( is_multisite() ) {
     14            $expected = array_merge( array( 'new_blog' ), $expected );
     15        }
     16
     17        $actions = bp_activity_get_actions();
     18        $actions = array_keys( (array) $actions->blogs );
     19
     20        $this->assertEquals( $expected, $actions );
     21    }
     22
    423    /**
    524     * @group activity_action
  • trunk/tests/phpunit/testcases/blogs/functions.php

    r9139 r9194  
    299299
    300300        $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity (no change)' );
     301    }
     302
     303    /**
     304     * @group bp_blogs_catch_transition_post_status
     305     */
     306    public function test_transition_post_status_password_publish() {
     307        $post_id = $this->factory->post->create( array(
     308            'post_status'   => 'publish',
     309            'post_type'     => 'post',
     310            'post_password' => 'pass',
     311        ) );
     312        $post = get_post( $post_id );
     313
     314        // 'new' => 'publish with password'
     315        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Published with password post should not have activity' );
     316    }
     317
     318    /**
     319     * @group bp_blogs_catch_transition_post_status
     320     */
     321    public function test_transition_post_status_publish_update_password() {
     322        $post_id = $this->factory->post->create( array(
     323            'post_status'   => 'publish',
     324            'post_type'     => 'post',
     325        ) );
     326        $post = get_post( $post_id );
     327
     328        // 'publish' => 'publish'
     329        $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     330
     331        $post->post_content .= ' foo';
     332        $post->post_password = 'pass';
     333
     334        wp_update_post( $post );
     335
     336        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Updated with password post should not have activity' );
     337    }
     338
     339    /**
     340     * @group bp_blogs_catch_transition_post_status
     341     */
     342    public function test_transition_post_status_private_publish() {
     343        $post_id = $this->factory->post->create( array(
     344            'post_status'   => 'private',
     345            'post_type'     => 'post',
     346        ) );
     347        $post = get_post( $post_id );
     348
     349        // 'new' => 'private'
     350        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Private post should not have activity' );
     351
     352        $post->post_status = 'publish';
     353
     354        wp_update_post( $post );
     355
     356        // 'private' => 'publish'
     357        $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     358    }
     359
     360    /**
     361     * @group bp_blogs_catch_transition_post_status
     362     */
     363    public function test_transition_post_status_publish_private() {
     364        $post_id = $this->factory->post->create( array(
     365            'post_status'   => 'publish',
     366            'post_type'     => 'post',
     367        ) );
     368        $post = get_post( $post_id );
     369
     370        // 'new' => 'publish'
     371        $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     372
     373        $post->post_status = 'private';
     374
     375        wp_update_post( $post );
     376
     377        // 'publish' => 'private'
     378        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Private post should not have activity' );
    301379    }
    302380
     
    478556    }
    479557
     558    /**
     559     * @group bp_blogs_catch_transition_post_status
     560     */
     561    public function test_bp_blogs_is_blog_trackable_false_publish_post() {
     562        add_filter( 'bp_blogs_is_blog_trackable', '__return_false' );
     563
     564        $post_id = $this->factory->post->create( array(
     565            'post_status'   => 'publish',
     566            'post_type'     => 'post',
     567        ) );
     568        $post = get_post( $post_id );
     569
     570        // 'new' => 'publish'
     571        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not trackable blog post should not have activity' );
     572
     573        $post->post_content .= ' foo';
     574
     575        wp_update_post( $post );
     576
     577        // 'publish' => 'publish'
     578        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not trackable blog post should not have activity' );
     579
     580        remove_filter( 'bp_blogs_is_blog_trackable', '__return_false' );
     581    }
     582
     583    /**
     584     * @group bp_blogs_catch_transition_post_status
     585     */
     586    public function test_bp_is_blog_public_zero_publish_post() {
     587        if ( ! is_multisite() ) {
     588            return;
     589        }
     590
     591        add_filter( 'bp_is_blog_public', '__return_zero' );
     592
     593        $post_id = $this->factory->post->create( array(
     594            'post_status'   => 'publish',
     595            'post_type'     => 'post',
     596        ) );
     597        $post = get_post( $post_id );
     598
     599        // 'new' => 'publish'
     600        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not public blog post should not have activity' );
     601
     602        $post->post_content .= ' foo';
     603
     604        wp_update_post( $post );
     605
     606        // 'publish' => 'publish'
     607        $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Not public blog post should not have activity' );
     608
     609        remove_filter( 'bp_is_blog_public', '__return_zero' );
     610    }
     611
    480612    protected function activity_exists_for_post( $post_id ) {
    481613        $a = bp_activity_get( array(
Note: See TracChangeset for help on using the changeset viewer.