Skip to:
Content

BuddyPress.org

Changeset 10542


Ignore:
Timestamp:
02/07/2016 04:29:52 PM (4 years ago)
Author:
imath
Message:

Post Type Activities: allow post types to register comments tracking

When registering a post type, it is now possible to inform BuddyPress the comments about this post type need to be tracked into the activity stream. This can be achieved by making sure the post type supports "comments" and "buddypress-activity" and by adding extra parameters to the "bp_activity" argument of the register_post_type() function one or by using the bp_activity_set_post_type_tracking_args() function.

We are also introducing a new function to inform if an activity type is supporting a specific feature: bp_activity_type_supports(). For now this function is helping to check if:

  • a activity supports a an activity comment
  • an activity comment supports synchronization with a post type comment

Props shanebp, r-a-y.

See #6482

Location:
trunk/src/bp-activity
Files:
2 edited

Legend:

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

    r10515 r10542  
    439439    }
    440440
     441    $activity_labels = array(
     442        /* Post labels */
     443        'bp_activity_admin_filter',
     444        'bp_activity_front_filter',
     445        'bp_activity_new_post',
     446        'bp_activity_new_post_ms',
     447        /* Comment labels */
     448        'bp_activity_comments_admin_filter',
     449        'bp_activity_comments_front_filter',
     450        'bp_activity_new_comment',
     451        'bp_activity_new_comment_ms'
     452    );
     453
    441454    // Labels are loaded into the post type object.
    442     foreach ( array( 'bp_activity_admin_filter', 'bp_activity_front_filter', 'bp_activity_new_post', 'bp_activity_new_post_ms' ) as $label_type ) {
     455    foreach ( $activity_labels as $label_type ) {
    443456        if ( ! empty( $args[ $label_type ] ) ) {
    444457            $wp_post_types[ $post_type ]->labels->{$label_type} = $args[ $label_type ];
     
    457470 *
    458471 * @since 2.2.0
     472 * @since 2.5.0 Add post type comments tracking args
    459473 *
    460474 * @param  string $post_type Name of the post type.
     
    466480    }
    467481
    468     $post_type_object = get_post_type_object( $post_type );
     482    $post_type_object           = get_post_type_object( $post_type );
     483    $post_type_support_comments = post_type_supports( $post_type, 'comments' );
    469484
    470485    $post_type_activity = array(
    471         'component_id'      => buddypress()->activity->id,
    472         'action_id'         => 'new_' . $post_type,
    473         'format_callback'   => 'bp_activity_format_activity_action_custom_post_type_post',
    474         'front_filter'      => $post_type_object->labels->name,
    475         'contexts'          => array( 'activity' ),
    476         'position'          => 0,
    477         'singular'          => strtolower( $post_type_object->labels->singular_name ),
    478         'activity_comment' => ! post_type_supports( $post_type, 'comments' ),
     486        'component_id'            => buddypress()->activity->id,
     487        'action_id'               => 'new_' . $post_type,
     488        'format_callback'         => 'bp_activity_format_activity_action_custom_post_type_post',
     489        'front_filter'            => $post_type_object->labels->name,
     490        'contexts'                => array( 'activity' ),
     491        'position'                => 0,
     492        'singular'                => strtolower( $post_type_object->labels->singular_name ),
     493        'activity_comment'        => ! $post_type_support_comments,
     494        'comment_action_id'       => false,
     495        'comment_format_callback' => 'bp_activity_format_activity_action_custom_post_type_comment',
    479496    );
    480497
     
    509526    }
    510527
     528    // If the post type supports comments and has a comment action id, build the comments tracking args
     529    if ( $post_type_support_comments && ! empty( $post_type_activity->comment_action_id ) ) {
     530        // Init a new container for the activity type for comments
     531        $post_type_activity->comments_tracking = new stdClass();
     532
     533        // Build the activity type for comments
     534        $post_type_activity->comments_tracking->component_id = $post_type_activity->component_id;
     535        $post_type_activity->comments_tracking->action_id    = $post_type_activity->comment_action_id;
     536
     537        // Try to get the comments admin filter from the post type labels.
     538        if ( ! empty( $post_type_object->labels->bp_activity_comments_admin_filter ) ) {
     539            $post_type_activity->comments_tracking->admin_filter = $post_type_object->labels->bp_activity_comments_admin_filter;
     540
     541        // Fall back to a generic name.
     542        } else {
     543            $post_type_activity->comments_tracking->admin_filter = _x( 'New item comment posted', 'Post Type generic comments activity admin filter', 'buddypress' );
     544        }
     545
     546        $post_type_activity->comments_tracking->format_callback = $post_type_activity->comment_format_callback;
     547
     548        // Check for the comments front filter in the post type labels.
     549        if ( ! empty( $post_type_object->labels->bp_activity_comments_front_filter ) ) {
     550            $post_type_activity->comments_tracking->front_filter = $post_type_object->labels->bp_activity_comments_front_filter;
     551
     552        // Fall back to a generic name.
     553        } else {
     554            $post_type_activity->comments_tracking->front_filter = sprintf( __( '%s comments', 'buddypress' ), $post_type_object->labels->singular_name );
     555        }
     556
     557        $post_type_activity->comments_tracking->contexts = $post_type_activity->contexts;
     558        $post_type_activity->comments_tracking->position = (int) $post_type_activity->position + 1;
     559
     560        // Try to get the action for new post type comment action on non-multisite installations.
     561        if ( ! empty( $post_type_object->labels->bp_activity_new_comment ) ) {
     562            $post_type_activity->comments_tracking->new_post_type_comment_action = $post_type_object->labels->bp_activity_new_comment;
     563        }
     564
     565        // Try to get the action for new post type comment action on multisite installations.
     566        if ( ! empty( $post_type_object->labels->bp_activity_new_comment_ms ) ) {
     567            $post_type_activity->comments_tracking->new_post_type_comment_action_ms = $post_type_object->labels->bp_activity_new_comment_ms;
     568        }
     569    }
     570
     571    // Finally make sure we'll be able to find the post type this activity type is associated to.
     572    $post_type_activity->post_type = $post_type;
     573
    511574    /**
    512575     * Filters tracking arguments for a specific post type.
     
    524587 *
    525588 * @since 2.2.0
     589 * @since 2.5.0 Include post type comments tracking args if needed
    526590 *
    527591 * @return array List of post types with their tracking arguments.
     
    537601
    538602        if ( ! empty( $track_post_type ) ) {
     603            // Set the post type comments tracking args
     604            if ( ! empty( $track_post_type->comments_tracking->action_id ) ) {
     605                // Used to check support for comment tracking by activity type (new_post_type_comment)
     606                $track_post_type->comments_tracking->comments_tracking = true;
     607
     608                // Used to be able to find the post type this activity type is associated to.
     609                $track_post_type->comments_tracking->post_type = $post_type;
     610
     611                $post_types_tracking_args[ $track_post_type->comments_tracking->action_id ] = $track_post_type->comments_tracking;
     612
     613                // Used to check support for comment tracking by activity type (new_post_type)
     614                $track_post_type->comments_tracking = true;
     615            }
     616
    539617            $post_types_tracking_args[ $track_post_type->action_id ] = $track_post_type;
    540618        }
     
    551629     */
    552630    return apply_filters( 'bp_activity_get_post_types_tracking_args', $post_types_tracking_args );
     631}
     632
     633/**
     634 * Check if the *Post Type* activity supports a specific feature.
     635 *
     636 * @since 2.5.0
     637 *
     638 * @param  string $activity_type The activity type to check.
     639 * @param  string $feature       The feature to check. Currently supports:
     640 *                               'post-type-comment-tracking', 'post-type-comment-reply' & 'comment-reply'.
     641 *                               See inline doc for more info.
     642 * @return bool
     643 */
     644function bp_activity_type_supports( $activity_type = '', $feature = '' ) {
     645    $retval = false;
     646
     647    $bp = buddypress();
     648
     649    switch ( $feature ) {
     650        /**
     651         * Does this activity type support comment tracking?
     652         *
     653         * eg. 'new_blog_post' and 'new_blog_comment' will both return true.
     654         */
     655        case 'post-type-comment-tracking' :
     656            // Set the activity track global if not set yet
     657            if ( empty( $bp->activity->track ) ) {
     658                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     659            }
     660
     661            if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
     662                $retval = true;
     663            }
     664            break;
     665
     666        /**
     667         * Is this a parent activity type that support post comments?
     668         *
     669         * eg. 'new_blog_post' will return true; 'new_blog_comment' will return false.
     670         */
     671        case 'post-type-comment-reply' :
     672            // Set the activity track global if not set yet.
     673            if ( empty( $bp->activity->track ) ) {
     674                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     675            }
     676
     677            if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) && ! empty( $bp->activity->track[ $activity_type ]->comment_action_id ) ) {
     678                $retval = true;
     679            }
     680            break;
     681
     682        /**
     683         * Does this activity type support comment & reply?
     684         */
     685        case 'comment-reply' :
     686            // Set the activity track global if not set yet.
     687            if ( empty( $bp->activity->track ) ) {
     688                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     689            }
     690
     691            // Post Type activities
     692            if ( ! empty( $bp->activity->track[ $activity_type ] ) ) {
     693                if ( isset( $bp->activity->track[ $activity_type ]->activity_comment ) ) {
     694                    $retval = $bp->activity->track[ $activity_type ]->activity_comment;
     695                }
     696
     697                // Eventually override with comment synchronization feature.
     698                if ( isset( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
     699                    $retval = $bp->activity->track[ $activity_type ]->comments_tracking && ! bp_disable_blogforum_comments();
     700                }
     701
     702            // Retired Forums component
     703            } elseif ( 'new_forum_topic' === $activity_type || 'new_forum_post' === $activity_type ) {
     704                $retval = ! bp_disable_blogforum_comments();
     705
     706            // By Default, all other activity types are supporting comments.
     707            } else {
     708                $retval = true;
     709            }
     710            break;
     711    }
     712
     713    return $retval;
     714}
     715
     716/**
     717 * Get a specific tracking argument for a given activity type
     718 *
     719 * @since 2.5.0
     720 *
     721 * @param  string       $activity_type the activity type.
     722 * @param  string       $arg           the key of the tracking argument.
     723 * @return mixed        the value of the tracking arg, false if not found.
     724 */
     725function bp_activity_post_type_get_tracking_arg( $activity_type, $arg = '' ) {
     726    if ( empty( $activity_type ) || empty( $arg ) ) {
     727        return false;
     728    }
     729
     730    $bp = buddypress();
     731
     732    // Set the activity track global if not set yet
     733    if ( empty( $bp->activity->track ) ) {
     734        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     735    }
     736
     737    if ( isset( $bp->activity->track[ $activity_type ]->{$arg} ) ) {
     738        return $bp->activity->track[ $activity_type ]->{$arg};
     739    } else {
     740        return false;
     741    }
    553742}
    554743
     
    14021591     */
    14031592    return apply_filters( 'bp_activity_custom_post_type_post_action', $action, $activity );
     1593}
     1594
     1595/**
     1596 * Format activity action strings for custom post types comments.
     1597 *
     1598 * @since 2.5.0
     1599 *
     1600 * @param string $action   Static activity action.
     1601 * @param object $activity Activity data object.
     1602 *
     1603 * @return string
     1604 */
     1605function bp_activity_format_activity_action_custom_post_type_comment( $action, $activity ) {
     1606    $bp = buddypress();
     1607
     1608    // Fetch all the tracked post types once.
     1609    if ( empty( $bp->activity->track ) ) {
     1610        $bp->activity->track = bp_activity_get_post_types_tracking_args();
     1611    }
     1612
     1613    if ( empty( $activity->type ) || empty( $bp->activity->track[ $activity->type ] ) ) {
     1614        return $action;
     1615    }
     1616
     1617    $user_link = bp_core_get_userlink( $activity->user_id );
     1618
     1619    if ( is_multisite() ) {
     1620        $blog_link = '<a href="' . esc_url( get_home_url( $activity->item_id ) ) . '">' . get_blog_option( $activity->item_id, 'blogname' ) . '</a>';
     1621
     1622        if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms ) ) {
     1623            $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action_ms, $user_link, $activity->primary_link, $blog_link );
     1624        } else {
     1625            $action = sprintf( _x( '%1$s commented on the <a href="%2$s">item</a>, on the site %3$s', 'Activity Custom Post Type comment action', 'buddypress' ), $user_link, $activity->primary_link, $blog_link );
     1626        }
     1627    } else {
     1628        if ( ! empty( $bp->activity->track[ $activity->type ]->new_post_type_comment_action ) ) {
     1629            $action = sprintf( $bp->activity->track[ $activity->type ]->new_post_type_comment_action, $user_link, $activity->primary_link );
     1630        } else {
     1631            $action = sprintf( _x( '%1$s commented on the <a href="%2$s">item</a>', 'Activity Custom Post Type post comment action', 'buddypress' ), $user_link, $activity->primary_link );
     1632        }
     1633    }
     1634
     1635    /**
     1636     * Filters the formatted custom post type activity comment action string.
     1637     *
     1638     * @since 2.5.0
     1639     *
     1640     * @param string               $action   Activity action string value.
     1641     * @param BP_Activity_Activity $activity Activity item object.
     1642     */
     1643    return apply_filters( 'bp_activity_custom_post_type_comment_action', $action, $activity );
    14041644}
    14051645
  • trunk/src/bp-activity/bp-activity-template.php

    r10516 r10542  
    30303030    $bp = buddypress();
    30313031
    3032     // Assume activity can be commented on.
    3033     $can_comment = true;
    3034 
    3035     // Determine ability to comment based on activity action name.
    3036     $activity_action = bp_get_activity_action_name();
    3037 
    3038     $turn_off = 0;
    3039     if ( ! empty( $activities_template->disable_blogforum_replies ) ) {
    3040         $turn_off = 1;
    3041     }
    3042 
    3043     $maybe_turn_off = array_fill_keys( array(
    3044         'new_blog_post',
    3045         'new_blog_comment',
    3046         'new_forum_topic',
    3047         'new_forum_post',
    3048     ), $turn_off );
    3049 
    3050     $maybe_turn_off['activity_comment'] = 1;
    3051 
    3052     // Fetch all the tracked post types once.
    3053     if ( empty( $bp->activity->track ) ) {
    3054         $bp->activity->track = bp_activity_get_post_types_tracking_args();
    3055     }
    3056 
    3057     foreach ( $bp->activity->track as $action => $tracking_args ) {
    3058         if ( empty( $tracking_args->activity_comment ) ) {
    3059             $maybe_turn_off[ $action ] = $turn_off;
    3060         }
    3061     }
    3062 
    3063     $can_comment = empty( $maybe_turn_off[ $activity_action ] );
     3032    // Determine ability to comment based on activity type name.
     3033    $activity_type = bp_get_activity_type();
     3034
     3035    // Get the 'comment-reply' support for the current activity type.
     3036    $can_comment = bp_activity_type_supports( $activity_type, 'comment-reply' );
     3037
     3038    // Neutralize activity_comment.
     3039    if ( 'activity_comment' === $activity_type ) {
     3040        $can_comment = false;
     3041    }
    30643042
    30653043    /**
     
    30673045     *
    30683046     * @since 1.5.0
     3047     * @since 2.5.0 Use $activity_type instead of $activity_name for the second parameter.
    30693048     *
    30703049     * @param bool   $can_comment     Status on if activity can be commented on.
    3071      * @param string $activity_action Current activity action being checked on.
    3072      */
    3073     return apply_filters( 'bp_activity_can_comment', $can_comment, $activity_action );
     3050     * @param string $activity_type   Current activity type being checked on.
     3051     */
     3052    return apply_filters( 'bp_activity_can_comment', $can_comment, $activity_type );
    30743053}
    30753054
Note: See TracChangeset for help on using the changeset viewer.