Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/04/2018 11:10:35 PM (6 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.

File:
1 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 );
Note: See TracChangeset for help on using the changeset viewer.