Skip to:
Content

BuddyPress.org


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

Post Type Activity Tracking feature

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

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

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

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

props boonebgorges, DJPaul

Fixes #5669

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/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/**
Note: See TracChangeset for help on using the changeset viewer.