Skip to:
Content

BuddyPress.org

Changeset 11883


Ignore:
Timestamp:
03/04/2018 11:10:35 PM (7 years ago)
Author:
r-a-y
Message:

Activity: Move some functions from bp-activity-actions.php to bp-activity-functions.php

Some of the functions from bp-activity-actions.php are not related to
activity page actions, so these are being moved to the all-purpose
bp-activity-functions.php file (for better or worse!).

The intent is to remove and split up the bp-activity-actions.php and
bp-activity-screens.php files until these functions are actually
needed.

See #7218.

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

Legend:

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

    r11366 r11883  
    1212// Exit if accessed directly.
    1313defined( 'ABSPATH' ) || exit;
    14 
    15 /**
    16  * Allow core components and dependent plugins to register activity actions.
    17  *
    18  * @since 1.2.0
    19  *
    20  */
    21 function bp_register_activity_actions() {
    22 
    23     /**
    24      * Fires on bp_init to allow core components and dependent plugins to register activity actions.
    25      *
    26      * @since 1.2.0
    27      */
    28     do_action( 'bp_register_activity_actions' );
    29 }
    30 add_action( 'bp_init', 'bp_register_activity_actions', 8 );
    3114
    3215/**
     
    604587}
    605588add_action( 'bp_actions', 'bp_activity_action_favorites_feed' );
    606 
    607 /**
    608  * AJAX endpoint for Suggestions API lookups.
    609  *
    610  * @since 2.1.0
    611  */
    612 function bp_ajax_get_suggestions() {
    613     if ( ! bp_is_user_active() || empty( $_GET['term'] ) || empty( $_GET['type'] ) ) {
    614         wp_send_json_error( 'missing_parameter' );
    615         exit;
    616     }
    617 
    618     $args = array(
    619         'term' => sanitize_text_field( $_GET['term'] ),
    620         'type' => sanitize_text_field( $_GET['type'] ),
    621     );
    622 
    623     // Support per-Group suggestions.
    624     if ( ! empty( $_GET['group-id'] ) ) {
    625         $args['group_id'] = absint( $_GET['group-id'] );
    626     }
    627 
    628     $results = bp_core_get_suggestions( $args );
    629 
    630     if ( is_wp_error( $results ) ) {
    631         wp_send_json_error( $results->get_error_message() );
    632         exit;
    633     }
    634 
    635     wp_send_json_success( $results );
    636 }
    637 add_action( 'wp_ajax_bp_get_suggestions', 'bp_ajax_get_suggestions' );
    638 
    639 /**
    640  * Detect a change in post type status, and initiate an activity update if necessary.
    641  *
    642  * @since 2.2.0
    643  *
    644  * @todo Support untrashing better.
    645  *
    646  * @param string $new_status New status for the post.
    647  * @param string $old_status Old status for the post.
    648  * @param object $post       Post data.
    649  */
    650 function bp_activity_catch_transition_post_type_status( $new_status, $old_status, $post ) {
    651     if ( ! post_type_supports( $post->post_type, 'buddypress-activity' ) ) {
    652         return;
    653     }
    654 
    655     // This is an edit.
    656     if ( $new_status === $old_status ) {
    657         // An edit of an existing post should update the existing activity item.
    658         if ( $new_status == 'publish' ) {
    659             $edit = bp_activity_post_type_update( $post );
    660 
    661             // Post was never recorded into activity stream, so record it now!
    662             if ( null === $edit ) {
    663                 bp_activity_post_type_publish( $post->ID, $post );
    664             }
    665 
    666         // Allow plugins to eventually deal with other post statuses.
    667         } else {
    668             /**
    669              * Fires when editing the post and the new status is not 'publish'.
    670              *
    671              * This is a variable filter that is dependent on the post type
    672              * being untrashed.
    673              *
    674              * @since 2.5.0
    675              *
    676              * @param WP_Post $post Post data.
    677              * @param string $new_status New status for the post.
    678              * @param string $old_status Old status for the post.
    679              */
    680             do_action( 'bp_activity_post_type_edit_' . $post->post_type, $post, $new_status, $old_status );
    681         }
    682 
    683         return;
    684     }
    685 
    686     // Publishing a previously unpublished post.
    687     if ( 'publish' === $new_status ) {
    688         // Untrashing the post type - nothing here yet.
    689         if ( 'trash' == $old_status ) {
    690 
    691             /**
    692              * Fires if untrashing post in a post type.
    693              *
    694              * This is a variable filter that is dependent on the post type
    695              * being untrashed.
    696              *
    697              * @since 2.2.0
    698              *
    699              * @param WP_Post $post Post data.
    700              */
    701             do_action( 'bp_activity_post_type_untrash_' . $post->post_type, $post );
    702         } else {
    703             // Record the post.
    704             bp_activity_post_type_publish( $post->ID, $post );
    705         }
    706 
    707     // Unpublishing a previously published post.
    708     } elseif ( 'publish' === $old_status ) {
    709         // Some form of pending status - only remove the activity entry.
    710         bp_activity_post_type_unpublish( $post->ID, $post );
    711 
    712     // For any other cases, allow plugins to eventually deal with it.
    713     } else {
    714         /**
    715          * Fires when the old and the new post status are not 'publish'.
    716          *
    717          * This is a variable filter that is dependent on the post type
    718          * being untrashed.
    719          *
    720          * @since 2.5.0
    721          *
    722          * @param WP_Post $post Post data.
    723          * @param string $new_status New status for the post.
    724          * @param string $old_status Old status for the post.
    725          */
    726         do_action( 'bp_activity_post_type_transition_status_' . $post->post_type, $post, $new_status, $old_status );
    727     }
    728 }
    729 add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
    730 
    731 /**
    732  * When a post type comment status transition occurs, update the relevant activity's status.
    733  *
    734  * @since 2.5.0
    735  *
    736  * @param string     $new_status New comment status.
    737  * @param string     $old_status Previous comment status.
    738  * @param WP_Comment $comment Comment data.
    739  */
    740 function bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ) {
    741     $post_type = get_post_type( $comment->comment_post_ID );
    742     if ( ! $post_type ) {
    743         return;
    744     }
    745 
    746     // Get the post type tracking args.
    747     $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
    748 
    749     // Bail if the activity type does not exist
    750     if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
    751         return false;
    752 
    753     // Set the $activity_comment_object
    754     } else {
    755         $activity_comment_object = $activity_post_object->comments_tracking;
    756     }
    757 
    758     // Init an empty activity ID
    759     $activity_id = 0;
    760 
    761     /**
    762      * Activity currently doesn't have any concept of a trash, or an unapproved/approved state.
    763      *
    764      * If a blog comment transitions to a "delete" or "hold" status, delete the activity item.
    765      * If a blog comment transitions to trashed, or spammed, mark the activity as spam.
    766      * If a blog comment transitions to approved (and the activity exists), mark the activity as ham.
    767      * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam.
    768      * Otherwise, record the comment into the activity stream.
    769      */
    770 
    771     // This clause handles delete/hold.
    772     if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {
    773         return bp_activity_post_type_remove_comment( $comment->comment_ID, $activity_post_object );
    774 
    775     // These clauses handle trash, spam, and un-spams.
    776     } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) {
    777         $action = 'spam_activity';
    778     } elseif ( 'approved' == $new_status ) {
    779         $action = 'ham_activity';
    780     }
    781 
    782     // Get the activity
    783     if ( bp_disable_blogforum_comments() ) {
    784         $activity_id = bp_activity_get_activity_id( array(
    785             'component'         => $activity_comment_object->component_id,
    786             'item_id'           => get_current_blog_id(),
    787             'secondary_item_id' => $comment->comment_ID,
    788             'type'              => $activity_comment_object->action_id,
    789         ) );
    790     } else {
    791         $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
    792     }
    793 
    794     /**
    795      * Leave a chance to plugins to manage activity comments differently.
    796      *
    797      * @since  2.5.0
    798      *
    799      * @param bool        $value       True to override BuddyPress management.
    800      * @param string      $post_type   The post type name.
    801      * @param int         $activity_id The post type activity (0 if not found).
    802      * @param string      $new_status  The new status of the post type comment.
    803      * @param string      $old_status  The old status of the post type comment.
    804      * @param WP_Comment  $comment Comment data.
    805      */
    806     if ( true === apply_filters( 'bp_activity_pre_transition_post_type_comment_status', false, $post_type, $activity_id, $new_status, $old_status, $comment ) ) {
    807         return false;
    808     }
    809 
    810     // Check activity item exists
    811     if ( empty( $activity_id ) ) {
    812         // If no activity exists, but the comment has been approved, record it into the activity table.
    813         if ( 'approved' == $new_status ) {
    814             return bp_activity_post_type_comment( $comment->comment_ID, true, $activity_post_object );
    815         }
    816 
    817         return;
    818     }
    819 
    820     // Create an activity object
    821     $activity = new BP_Activity_Activity( $activity_id );
    822     if ( empty( $activity->component ) ) {
    823         return;
    824     }
    825 
    826     // Spam/ham the activity if it's not already in that state
    827     if ( 'spam_activity' === $action && ! $activity->is_spam ) {
    828         bp_activity_mark_as_spam( $activity );
    829     } elseif ( 'ham_activity' == $action) {
    830         bp_activity_mark_as_ham( $activity );
    831     }
    832 
    833     // Add "new_post_type_comment" to the whitelisted activity types, so that the activity's Akismet history is generated
    834     $post_type_comment_action = $activity_comment_object->action_id;
    835     $comment_akismet_history = function ( $activity_types ) use ( $post_type_comment_action ) {
    836         $activity_types[] = $post_type_comment_action;
    837 
    838         return $activity_types;
    839     };
    840     add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
    841 
    842     // Make sure the activity change won't edit the comment if sync is on
    843     remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
    844 
    845     // Save the updated activity
    846     $activity->save();
    847 
    848     // Restore the action
    849     add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
    850 
    851     // Remove the "new_blog_comment" activity type whitelist so we don't break anything
    852     remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
    853 }
    854 add_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 );
  • trunk/src/bp-activity/bp-activity-functions.php

    r11837 r11883  
    14181418}
    14191419add_action( 'bp_make_ham_user', 'bp_activity_ham_all_user_data' );
     1420
     1421/**
     1422 * Allow core components and dependent plugins to register activity actions.
     1423 *
     1424 * @since 1.2.0
     1425 */
     1426function bp_register_activity_actions() {
     1427    /**
     1428     * Fires on bp_init to allow core components and dependent plugins to register activity actions.
     1429     *
     1430     * @since 1.2.0
     1431     */
     1432    do_action( 'bp_register_activity_actions' );
     1433}
     1434add_action( 'bp_init', 'bp_register_activity_actions', 8 );
    14201435
    14211436/**
     
    38923907    return (bool) apply_filters( 'bp_activity_do_heartbeat', $retval );
    38933908}
     3909
     3910/**
     3911 * AJAX endpoint for Suggestions API lookups.
     3912 *
     3913 * @since 2.1.0
     3914 */
     3915function bp_ajax_get_suggestions() {
     3916    if ( ! bp_is_user_active() || empty( $_GET['term'] ) || empty( $_GET['type'] ) ) {
     3917        wp_send_json_error( 'missing_parameter' );
     3918        exit;
     3919    }
     3920
     3921    $args = array(
     3922        'term' => sanitize_text_field( $_GET['term'] ),
     3923        'type' => sanitize_text_field( $_GET['type'] ),
     3924    );
     3925
     3926    // Support per-Group suggestions.
     3927    if ( ! empty( $_GET['group-id'] ) ) {
     3928        $args['group_id'] = absint( $_GET['group-id'] );
     3929    }
     3930
     3931    $results = bp_core_get_suggestions( $args );
     3932
     3933    if ( is_wp_error( $results ) ) {
     3934        wp_send_json_error( $results->get_error_message() );
     3935        exit;
     3936    }
     3937
     3938    wp_send_json_success( $results );
     3939}
     3940add_action( 'wp_ajax_bp_get_suggestions', 'bp_ajax_get_suggestions' );
     3941
     3942/**
     3943 * Detect a change in post type status, and initiate an activity update if necessary.
     3944 *
     3945 * @since 2.2.0
     3946 *
     3947 * @todo Support untrashing better.
     3948 *
     3949 * @param string $new_status New status for the post.
     3950 * @param string $old_status Old status for the post.
     3951 * @param object $post       Post data.
     3952 */
     3953function bp_activity_catch_transition_post_type_status( $new_status, $old_status, $post ) {
     3954    if ( ! post_type_supports( $post->post_type, 'buddypress-activity' ) ) {
     3955        return;
     3956    }
     3957
     3958    // This is an edit.
     3959    if ( $new_status === $old_status ) {
     3960        // An edit of an existing post should update the existing activity item.
     3961        if ( $new_status == 'publish' ) {
     3962            $edit = bp_activity_post_type_update( $post );
     3963
     3964            // Post was never recorded into activity stream, so record it now!
     3965            if ( null === $edit ) {
     3966                bp_activity_post_type_publish( $post->ID, $post );
     3967            }
     3968
     3969        // Allow plugins to eventually deal with other post statuses.
     3970        } else {
     3971            /**
     3972             * Fires when editing the post and the new status is not 'publish'.
     3973             *
     3974             * This is a variable filter that is dependent on the post type
     3975             * being untrashed.
     3976             *
     3977             * @since 2.5.0
     3978             *
     3979             * @param WP_Post $post Post data.
     3980             * @param string $new_status New status for the post.
     3981             * @param string $old_status Old status for the post.
     3982             */
     3983            do_action( 'bp_activity_post_type_edit_' . $post->post_type, $post, $new_status, $old_status );
     3984        }
     3985
     3986        return;
     3987    }
     3988
     3989    // Publishing a previously unpublished post.
     3990    if ( 'publish' === $new_status ) {
     3991        // Untrashing the post type - nothing here yet.
     3992        if ( 'trash' == $old_status ) {
     3993
     3994            /**
     3995             * Fires if untrashing post in a post type.
     3996             *
     3997             * This is a variable filter that is dependent on the post type
     3998             * being untrashed.
     3999             *
     4000             * @since 2.2.0
     4001             *
     4002             * @param WP_Post $post Post data.
     4003             */
     4004            do_action( 'bp_activity_post_type_untrash_' . $post->post_type, $post );
     4005        } else {
     4006            // Record the post.
     4007            bp_activity_post_type_publish( $post->ID, $post );
     4008        }
     4009
     4010    // Unpublishing a previously published post.
     4011    } elseif ( 'publish' === $old_status ) {
     4012        // Some form of pending status - only remove the activity entry.
     4013        bp_activity_post_type_unpublish( $post->ID, $post );
     4014
     4015    // For any other cases, allow plugins to eventually deal with it.
     4016    } else {
     4017        /**
     4018         * Fires when the old and the new post status are not 'publish'.
     4019         *
     4020         * This is a variable filter that is dependent on the post type
     4021         * being untrashed.
     4022         *
     4023         * @since 2.5.0
     4024         *
     4025         * @param WP_Post $post Post data.
     4026         * @param string $new_status New status for the post.
     4027         * @param string $old_status Old status for the post.
     4028         */
     4029        do_action( 'bp_activity_post_type_transition_status_' . $post->post_type, $post, $new_status, $old_status );
     4030    }
     4031}
     4032add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
     4033
     4034/**
     4035 * When a post type comment status transition occurs, update the relevant activity's status.
     4036 *
     4037 * @since 2.5.0
     4038 *
     4039 * @param string     $new_status New comment status.
     4040 * @param string     $old_status Previous comment status.
     4041 * @param WP_Comment $comment Comment data.
     4042 */
     4043function bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ) {
     4044    $post_type = get_post_type( $comment->comment_post_ID );
     4045    if ( ! $post_type ) {
     4046        return;
     4047    }
     4048
     4049    // Get the post type tracking args.
     4050    $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type );
     4051
     4052    // Bail if the activity type does not exist
     4053    if ( empty( $activity_post_object->comments_tracking->action_id ) ) {
     4054        return false;
     4055
     4056    // Set the $activity_comment_object
     4057    } else {
     4058        $activity_comment_object = $activity_post_object->comments_tracking;
     4059    }
     4060
     4061    // Init an empty activity ID
     4062    $activity_id = 0;
     4063
     4064    /**
     4065     * Activity currently doesn't have any concept of a trash, or an unapproved/approved state.
     4066     *
     4067     * If a blog comment transitions to a "delete" or "hold" status, delete the activity item.
     4068     * If a blog comment transitions to trashed, or spammed, mark the activity as spam.
     4069     * If a blog comment transitions to approved (and the activity exists), mark the activity as ham.
     4070     * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam.
     4071     * Otherwise, record the comment into the activity stream.
     4072     */
     4073
     4074    // This clause handles delete/hold.
     4075    if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {
     4076        return bp_activity_post_type_remove_comment( $comment->comment_ID, $activity_post_object );
     4077
     4078    // These clauses handle trash, spam, and un-spams.
     4079    } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) {
     4080        $action = 'spam_activity';
     4081    } elseif ( 'approved' == $new_status ) {
     4082        $action = 'ham_activity';
     4083    }
     4084
     4085    // Get the activity
     4086    if ( bp_disable_blogforum_comments() ) {
     4087        $activity_id = bp_activity_get_activity_id( array(
     4088            'component'         => $activity_comment_object->component_id,
     4089            'item_id'           => get_current_blog_id(),
     4090            'secondary_item_id' => $comment->comment_ID,
     4091            'type'              => $activity_comment_object->action_id,
     4092        ) );
     4093    } else {
     4094        $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
     4095    }
     4096
     4097    /**
     4098     * Leave a chance to plugins to manage activity comments differently.
     4099     *
     4100     * @since  2.5.0
     4101     *
     4102     * @param bool        $value       True to override BuddyPress management.
     4103     * @param string      $post_type   The post type name.
     4104     * @param int         $activity_id The post type activity (0 if not found).
     4105     * @param string      $new_status  The new status of the post type comment.
     4106     * @param string      $old_status  The old status of the post type comment.
     4107     * @param WP_Comment  $comment Comment data.
     4108     */
     4109    if ( true === apply_filters( 'bp_activity_pre_transition_post_type_comment_status', false, $post_type, $activity_id, $new_status, $old_status, $comment ) ) {
     4110        return false;
     4111    }
     4112
     4113    // Check activity item exists
     4114    if ( empty( $activity_id ) ) {
     4115        // If no activity exists, but the comment has been approved, record it into the activity table.
     4116        if ( 'approved' == $new_status ) {
     4117            return bp_activity_post_type_comment( $comment->comment_ID, true, $activity_post_object );
     4118        }
     4119
     4120        return;
     4121    }
     4122
     4123    // Create an activity object
     4124    $activity = new BP_Activity_Activity( $activity_id );
     4125    if ( empty( $activity->component ) ) {
     4126        return;
     4127    }
     4128
     4129    // Spam/ham the activity if it's not already in that state
     4130    if ( 'spam_activity' === $action && ! $activity->is_spam ) {
     4131        bp_activity_mark_as_spam( $activity );
     4132    } elseif ( 'ham_activity' == $action) {
     4133        bp_activity_mark_as_ham( $activity );
     4134    }
     4135
     4136    // Add "new_post_type_comment" to the whitelisted activity types, so that the activity's Akismet history is generated
     4137    $post_type_comment_action = $activity_comment_object->action_id;
     4138    $comment_akismet_history = function ( $activity_types ) use ( $post_type_comment_action ) {
     4139        $activity_types[] = $post_type_comment_action;
     4140
     4141        return $activity_types;
     4142    };
     4143    add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
     4144
     4145    // Make sure the activity change won't edit the comment if sync is on
     4146    remove_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     4147
     4148    // Save the updated activity
     4149    $activity->save();
     4150
     4151    // Restore the action
     4152    add_action( 'bp_activity_before_save', 'bp_blogs_sync_activity_edit_to_post_comment', 20 );
     4153
     4154    // Remove the "new_blog_comment" activity type whitelist so we don't break anything
     4155    remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );
     4156}
     4157add_action( 'transition_comment_status', 'bp_activity_transition_post_type_comment_status', 10, 3 );
Note: See TracChangeset for help on using the changeset viewer.