From 7da0387df2a47caad69b578c4395396cb67ee6d8 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Mon, 28 Oct 2019 12:53:37 -0700 Subject: [PATCH] Log: Record event when contributor is removed from pledge --- plugins/wporg-5ftf/includes/contributor.php | 14 +++++++++- plugins/wporg-5ftf/includes/pledge-log.php | 30 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 875dc5f..f249352 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -170,7 +170,19 @@ function add_pledge_contributors( $pledge_id, $contributors ) { * @return false|WP_Post|null */ function remove_contributor( $contributor_post_id ) { - return wp_trash_post( $contributor_post_id ); + $pledge_id = get_post( $contributor_post_id )->post_parent; + $result = wp_trash_post( $contributor_post_id ); + + /** + * Action: Fires when a contributor is removed from a pledge. + * + * @param int $pledge_id + * @param int $contributor_post_id + * @param WP_Post|false|null $result + */ + do_action( FiveForTheFuture\PREFIX . '_remove_contributor', $pledge_id, $contributor_post_id, $result ); + + return $result; } /** diff --git a/plugins/wporg-5ftf/includes/pledge-log.php b/plugins/wporg-5ftf/includes/pledge-log.php index 4b775e8..08e6b76 100644 --- a/plugins/wporg-5ftf/includes/pledge-log.php +++ b/plugins/wporg-5ftf/includes/pledge-log.php @@ -18,6 +18,7 @@ add_action( 'updated_postmeta', __NAMESPACE__ . '\capture_updated_postmeta', 99, add_action( 'added_post_meta', __NAMESPACE__ . '\capture_added_post_meta', 99, 4 ); add_action( 'transition_post_status', __NAMESPACE__ . '\capture_transition_post_status', 99, 3 ); add_action( FiveForTheFuture\PREFIX . '_add_pledge_contributors', __NAMESPACE__ . '\capture_add_pledge_contributors', 99, 3 ); +add_action( FiveForTheFuture\PREFIX . '_remove_contributor', __NAMESPACE__ . '\capture_remove_contributor', 99, 3 ); /** * Adds a meta box for the log on the custom post type. @@ -266,3 +267,32 @@ function capture_add_pledge_contributors( $pledge_id, $contributors, $results ) get_current_user_id() ); } + +/** + * Record a log for the event when a contributor is removed from a pledge. + * + * @param int $pledge_id The post ID of the pledge. + * @param int $contributor_post_id The post ID of the pledge. + * @param WP_Post|false|null $result The result of the attempt to trash the post. + * + * @return void + */ +function capture_remove_contributor( $pledge_id, $contributor_post_id, $result ) { + // If the result isn't a post object, then it was already trashed, or didn't exist. + if ( $result instanceof WP_Post ) { + $contributor_post = get_post( $contributor_post_id ); + + add_log_entry( + $pledge_id, + 'contributor_removed', + sprintf( + 'Contributor removed: %s', + esc_html( $contributor_post->post_title ) + ), + array( + 'previous_status' => $contributor_post->_wp_trash_meta_status, + ), + get_current_user_id() + ); + } +}