Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/07/2016 04:36:40 PM (9 years ago)
Author:
imath
Message:

Post Type Activities: introduce new functions to track the post type comments

These functions are generalizing to any post type what was only performed for the "post" post type so far.

We are also editing the bp_activity_new_comment() so that it is possible to post an activity comment without sending notifications.

Props shanebp, r-a-y.

See #6482

File:
1 edited

Legend:

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

    r10487 r10543  
    832832}
    833833add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
     834
     835/**
     836 * When a post type comment status transition occurs, update the relevant activity's status.
     837 *
     838 * @since 2.5.0
     839 *
     840 * @param string     $new_status New comment status.
     841 * @param string     $old_status Previous comment status.
     842 * @param WP_Comment $comment Comment data.
     843 */
     844function bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ) {
     845    $post_type = get_post_type( $comment->comment_post_ID );
     846    if ( ! $post_type ) {
     847        return;
     848    }
     849
     850    // Get the post type tracking args.
     851    $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     852
     853    // Bail if the activity type does not exist
     854    if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     855        return false;
     856
     857    // Set the $activity_comment_object
     858    } else {
     859        $activity_comment_object = $activity_post_object->comments_tracking;
     860    }
     861
     862    // Init an empty activity ID
     863    $activity_id = 0;
     864
     865    /**
     866     * Activity currently doesn't have any concept of a trash, or an unapproved/approved state.
     867     *
     868     * If a blog comment transitions to a "delete" or "hold" status, delete the activity item.
     869     * If a blog comment transitions to trashed, or spammed, mark the activity as spam.
     870     * If a blog comment transitions to approved (and the activity exists), mark the activity as ham.
     871     * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam.
     872     * Otherwise, record the comment into the activity stream.
     873     */
     874
     875    // This clause handles delete/hold.
     876    if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {
     877        return bp_activity_post_type_remove_comment( $comment->comment_ID, $activity_post_object );
     878
     879    // These clauses handle trash, spam, and un-spams.
     880    } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) {
     881        $action = 'spam_activity';
     882    } elseif ( 'approved' == $new_status ) {
     883        $action = 'ham_activity';
     884    }
     885
     886    // Get the activity
     887    if ( bp_disable_blogforum_comments() ) {
     888        $activity_id = bp_activity_get_activity_id( array(
     889            'component'         => $activity_comment_object->component_id,
     890            'item_id'           => get_current_blog_id(),
     891            'secondary_item_id' => $comment->comment_ID,
     892            'type'              => $activity_comment_object->action_id,
     893        ) );
     894    } else {
     895        $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
     896    }
     897
     898    /**
     899     * Leave a chance to plugins to manage activity comments differently.
     900     *
     901     * @since  2.5.0
     902     *
     903     * @param bool        $value       True to override BuddyPress management.
     904     * @param string      $post_type   The post type name.
     905     * @param int         $activity_id The post type activity (0 if not found).
     906     * @param string      $new_status  The new status of the post type comment.
     907     * @param string      $old_status  The old status of the post type comment.
     908     * @param WP_Comment  $comment Comment data.
     909     */
     910    if ( true === apply_filters( 'bp_activity_pre_transition_post_type_comment_status', false, $post_type, $activity_id, $new_status, $old_status, $comment ) ) {
     911        return false;
     912    }
     913
     914    // Check activity item exists
     915    if ( empty( $activity_id ) ) {
     916        // If no activity exists, but the comment has been approved, record it into the activity table.
     917        if ( 'approved' == $new_status ) {
     918            return bp_activity_post_type_comment( $comment->comment_ID, true, $activity_post_object );
     919        }
     920
     921        return;
     922    }
     923
     924    // Create an activity object
     925    $activity = new BP_Activity_Activity( $activity_id );
     926    if ( empty( $activity->component ) ) {
     927        return;
     928    }
     929
     930    // Spam/ham the activity if it's not already in that state
     931    if ( 'spam_activity' === $action && ! $activity->is_spam ) {
     932        bp_activity_mark_as_spam( $activity );
     933    } elseif ( 'ham_activity' == $action) {
     934        bp_activity_mark_as_ham( $activity );
     935    }
     936
     937    // Add "new_post_type_comment" to the whitelisted activity types, so that the activity's Akismet history is generated
     938    $post_type_comment_action = $activity_comment_object->action_id;
     939    $comment_akismet_history = create_function( '$t', '$t[] = $post_type_comment_action; return $t;' );
     940    add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
     941
     942    // Make sure the activity change won't edit the comment if sync is on
     943    remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     944
     945    // Save the updated activity
     946    $activity->save();
     947
     948    // Restore the action
     949    add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     950
     951    // Remove the "new_blog_comment" activity type whitelist so we don't break anything
     952    remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
     953}
Note: See TracChangeset for help on using the changeset viewer.