Skip to:
Content

BuddyPress.org

Ticket #3460: 3460.diff

File 3460.diff, 12.7 KB (added by imath, 11 years ago)
  • bp-blogs/bp-blogs-functions.php

     
    278278add_action( 'transition_post_status', 'bp_blogs_catch_published_post', 10, 3 );
    279279
    280280/**
     281 * Callback function to build the permalink to the post type
     282 *
     283 * @param  integer $post_id the post type id
     284 * @param  integer $blog_id the blog id
     285 * @param  WP_Post  $post    Post object.
     286 * @uses   add_query_arg()   To add custom args to the url
     287 * @uses   get_home_url()
     288 * @return string           the post type permalink
     289 */
     290function bp_blogs_activity_permalink_post_callback( $post_id = 0, $blog_id = 0, $post = null ) {
     291        $post_permalink = add_query_arg(
     292                'p',
     293                $post_id,
     294                trailingslashit( get_home_url( $blog_id ) )
     295        );
     296
     297        return $post_permalink;
     298}
     299
     300/**
     301 * Callback function to build the action for the post type
     302 *
     303 * @param  array  $args the available arguments
     304 * @uses   bp_core_get_userlink() to build the post author profile link
     305 * @uses   get_blog_option() WordPress function to fetch blog meta.
     306 * @return string the activity action
     307 */
     308function bp_blogs_activity_action_post_callback( $args = array() ) {
     309        extract( $args, EXTR_SKIP );
     310
     311        if ( !empty( $blog_id ) )
     312                $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( (int) $post_author ), '<a href="' . $post_permalink . '">' . $post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
     313        else
     314                $activity_action  = sprintf( __( '%1$s wrote a new post, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post_author ), '<a href="' . $post_permalink . '">' . $post_title . '</a>' );
     315
     316        return $activity_action;
     317}
     318
     319/**
    281320 * Record a new blog post in the BuddyPress activity stream.
    282321 *
    283322 * @param int $post_id ID of the post being recorded.
     
    311350        if ( (int) $blog_id == $tags_blog_id && apply_filters( 'bp_blogs_block_sitewide_tags_activity', true ) )
    312351                return false;
    313352
    314         // Don't record this if it's not a post
    315         if ( !in_array( $post->post_type, apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) ) ) )
     353        // Don't record this if the post type does not support bp_tracking
     354        if ( ! post_type_supports( $post->post_type, 'bp_tracking' ) )
    316355                return false;
    317356
    318357        $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );
     
    320359        if ( 'publish' == $post->post_status && empty( $post->post_password ) ) {
    321360                if ( $is_blog_public || !is_multisite() ) {
    322361
    323                         // Record this in activity streams
    324                         $post_permalink = add_query_arg(
    325                                 'p',
    326                                 $post_id,
    327                                 trailingslashit( get_home_url( $blog_id ) )
     362                        $post_type_object = get_post_type_object( $post->post_type );
     363                        extract( $post_type_object->bp_tracking, EXTR_SKIP );
     364
     365                        // Builds the permalink to the post_type
     366                        if ( !empty( $activity_permalink_post_callback ) && is_callable( $activity_permalink_post_callback ) )
     367                                $post_permalink = call_user_func_array( $activity_permalink_post_callback, array( $post_id, $blog_id, $post ) );
     368                        else
     369                                $post_permalink = bp_blogs_activity_permalink_post_callback( $post_id, $blog_id, $post );
     370
     371                        $action_args = array(
     372                                'post_author'    => $post->post_author,
     373                                'post_permalink' => $post_permalink,
     374                                'post_title'     => $post->post_title
    328375                        );
    329376
    330                         if ( is_multisite() )
    331                                 $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>' );
     377                        if( is_multisite() )
     378                                $action_args['blog_id'] = $blog_id;
     379
     380                        // Builds the activity action for the post type
     381                        if( !empty( $activity_action_post_callback ) && is_callable( $activity_action_post_callback ) )
     382                                $activity_action = call_user_func_array( $activity_action_post_callback, array( $action_args ) );
    332383                        else
    333                                 $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>' );
     384                                $activity_action = bp_blogs_activity_action_post_callback( $action_args );
    334385
     386                        $type_post = !empty( $type_post ) ? $type_post : 'new_blog_post';
     387
    335388                        // Make sure there's not an existing entry for this post (prevent bumping)
    336389                        if ( bp_is_active( 'activity' ) ) {
    337390                                $existing = bp_activity_get( array(
    338391                                        'filter' => array(
    339                                                 'action'       => 'new_blog_post',
     392                                                'action'       => $type_post,
    340393                                                'primary_id'   => $blog_id,
    341394                                                'secondary_id' => $post_id,
    342395                                        )
     
    351404
    352405                        bp_blogs_record_activity( array(
    353406                                'user_id'           => (int) $post->post_author,
    354                                 'action'            => apply_filters( 'bp_blogs_activity_new_post_action',       $activity_action,  $post, $post_permalink ),
    355                                 'content'           => apply_filters( 'bp_blogs_activity_new_post_content',      $activity_content, $post, $post_permalink ),
    356                                 'primary_link'      => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink,   $post_id               ),
    357                                 'type'              => 'new_blog_post',
     407                                'action'            => apply_filters( "bp_blogs_activity_new_{$post->post_type}_action",       $activity_action,  $post, $post_permalink ),
     408                                'content'           => apply_filters( "bp_blogs_activity_new_{$post->post_type}_content",      $activity_content, $post, $post_permalink ),
     409                                'primary_link'      => apply_filters( "bp_blogs_activity_new_{$post->post_type}_primary_link", $post_permalink,   $post_id               ),
     410                                'type'              => $type_post,
    358411                                'item_id'           => $blog_id,
    359412                                'secondary_item_id' => $post_id,
    360413                                'recorded_time'     => $post->post_date_gmt,
     
    371424}
    372425
    373426/**
     427 * Callback function to build the action for the comment on the post type
     428 *
     429 * @param  array  $args the available arguments
     430 * @uses   bp_core_get_userlink() to build the post author profile link
     431 * @uses   get_blog_option() WordPress function to fetch blog meta.
     432 * @return string the activity action
     433 */
     434function bp_blogs_activity_action_comment_callback( $args = array() ) {
     435        extract( $args, EXTR_SKIP );
     436
     437        if ( !empty( $blog_id ) )
     438                $activity_action = sprintf( __( '%1$s commented on the post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $post_title ) . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
     439        else
     440                $activity_action = sprintf( __( '%1$s commented on the post, %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $post_title ) . '</a>' );
     441
     442        return $activity_action;
     443}
     444
     445/**
    374446 * Record a new blog comment in the BuddyPress activity stream.
    375447 *
    376448 * Only posts the item if blog is public and post is not password-protected.
     
    422494        if ( !empty( $recorded_comment->post->post_password ) )
    423495                return false;
    424496
    425         // Don't record activity if the comment's associated post isn't a WordPress Post
    426         if ( !in_array( $recorded_comment->post->post_type, apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) ) ) )
     497        // Don't record this if the comment's associated post does not support bp_tracking
     498        if ( ! post_type_supports( $recorded_comment->post->post_type, 'bp_tracking' ) )
    427499                return false;
    428500
    429501        $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );
     
    431503        // If blog is public allow activity to be posted
    432504        if ( $is_blog_public ) {
    433505
    434                 // Get activity related links
    435                 $post_permalink = get_permalink( $recorded_comment->comment_post_ID );
    436                 $comment_link   = get_comment_link( $recorded_comment->comment_ID );
     506                $post_type_object = get_post_type_object( $recorded_comment->post->post_type );
     507                extract( $post_type_object->bp_tracking, EXTR_SKIP );
    437508
    438                 // Prepare to record in activity streams
    439                 if ( is_multisite() )
    440                         $activity_action = sprintf( __( '%1$s commented on the post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
     509                // Builds the permalink to the post_type
     510                if ( !empty( $activity_permalink_post_callback ) && is_callable( $activity_permalink_post_callback ) )
     511                        $post_permalink = call_user_func_array( $activity_permalink_post_callback, array( $recorded_comment->comment_post_ID, $blog_id, $recorded_comment->post ) );
    441512                else
    442                         $activity_action = sprintf( __( '%1$s commented on the post, %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>' );
     513                        $post_permalink = get_permalink( $recorded_comment->comment_post_ID );
    443514
     515                $action_args = array(
     516                        'user_id'        => $user_id,
     517                        'post_permalink' => $post_permalink,
     518                        'post_title'     => $recorded_comment->post->post_title
     519                );
     520
     521                if( is_multisite() )
     522                        $action_args['blog_id'] = $blog_id;
     523
     524                // Builds the activity action for the post type
     525                if( !empty( $activity_action_comment_callback ) && is_callable( $activity_action_comment_callback ) )
     526                        $activity_action = call_user_func_array( $activity_action_comment_callback, array( $action_args ) );
     527                else
     528                        $activity_action = bp_blogs_activity_action_comment_callback( $action_args );
     529
     530                // Get the activity type
     531                $type_comment = !empty( $type_comment ) ? $type_comment : 'new_blog_comment';
     532
     533                // Get comment link
     534                $comment_link   = get_comment_link( $recorded_comment->comment_ID );
     535                       
     536                // Get the comment content
    444537                $activity_content       = $recorded_comment->comment_content;
    445538
     539                $comment_post_type = ( 'post' == $recorded_comment->post->post_type ) ? 'comment' : $recorded_comment->post->post_type . '_comment';
     540
    446541                // Record in activity streams
    447542                bp_blogs_record_activity( array(
    448543                        'user_id'           => $user_id,
    449                         'action'            => apply_filters_ref_array( 'bp_blogs_activity_new_comment_action',       array( $activity_action,  &$recorded_comment, $comment_link ) ),
    450                         'content'           => apply_filters_ref_array( 'bp_blogs_activity_new_comment_content',      array( $activity_content, &$recorded_comment, $comment_link ) ),
    451                         'primary_link'      => apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link,     &$recorded_comment                ) ),
    452                         'type'              => 'new_blog_comment',
     544                        'action'            => apply_filters_ref_array( "bp_blogs_activity_new_{$comment_post_type}_action",       array( $activity_action,  &$recorded_comment, $comment_link ) ),
     545                        'content'           => apply_filters_ref_array( "bp_blogs_activity_new_{$comment_post_type}_content",      array( $activity_content, &$recorded_comment, $comment_link ) ),
     546                        'primary_link'      => apply_filters_ref_array( "bp_blogs_activity_new_{$comment_post_type}_primary_link", array( $comment_link,     &$recorded_comment                ) ),
     547                        'type'              => $type_comment,
    453548                        'item_id'           => $blog_id,
    454549                        'secondary_item_id' => $comment_id,
    455550                        'recorded_time'     => $recorded_comment->comment_date_gmt
  • bp-blogs/bp-blogs-actions.php

     
    3232        }
    3333}
    3434add_action( 'bp_actions', 'bp_blogs_redirect_to_random_blog' );
     35
     36/**
     37 * Early hooks bp_init to extend the post post type arguments and add to it the "bp_tracking" support
     38 *
     39 * @global $wp_post_types the different registered post types
     40 * @uses add_post_type_support() to add the "bp_tracking" support to the post post type
     41 */
     42function bp_blogs_extend_initial_post_types() {
     43        global $wp_post_types;
     44
     45        $wp_post_types['post']->bp_tracking = array(
     46                'type_post'                           => 'new_blog_post',
     47                'activity_permalink_post_callback'    => 'bp_blogs_activity_permalink_post_callback',
     48                'activity_action_post_callback'       => 'bp_blogs_activity_action_post_callback',
     49                'type_comment'                        => 'new_blog_comment',
     50                'activity_action_comment_callback'    => 'bp_blogs_activity_action_comment_callback',
     51        );
     52
     53        add_post_type_support( 'post', 'bp_tracking' );
     54}
     55
     56add_action( 'bp_init', 'bp_blogs_extend_initial_post_types', 5 );