From dbe5868a417663e3a01c09b07bc0e35e708f59c2 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Mon, 9 Dec 2019 10:52:50 -0500 Subject: [PATCH] Trigger an email on user-initiated pledge deactivation --- plugins/wporg-5ftf/includes/email.php | 22 +++++++++++++++ plugins/wporg-5ftf/includes/pledge-form.php | 8 +----- plugins/wporg-5ftf/includes/pledge.php | 30 ++++++++++++++++++--- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/plugins/wporg-5ftf/includes/email.php b/plugins/wporg-5ftf/includes/email.php index 9558262..1988a59 100644 --- a/plugins/wporg-5ftf/includes/email.php +++ b/plugins/wporg-5ftf/includes/email.php @@ -154,3 +154,25 @@ function send_manage_pledge_link( $pledge_id ) { return $result; } + +/** + * Email pledge manager to notify that the pledge has been removed. + * + * @param WP_Post $pledge The pledge object, used to add the title now that the pledge itself has been deleted. + * + * @return bool + */ +function send_pledge_deactivation_email( $pledge ) { + $message = sprintf( + "Your organization, %s, has been removed from the Five for the Future listing.\n\n" . + 'Please reply to this email if this was a mistake.', + $pledge->post_title + ); + + return send_email( + $pledge->{'5ftf_org-pledge-email'}, + 'Pledge removed from Five for the Future', + $message, + $pledge->ID + ); +} diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index 3cd808d..ca3694f 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -258,13 +258,7 @@ function process_form_remove( $pledge_id, $auth_token ) { ); } - $result = wp_update_post( - array( - 'ID' => $pledge_id, - 'post_status' => Pledge\DEACTIVE_STATUS, - ), - true // Return a WP_Error. - ); + $result = Pledge\deactivate( $pledge_id, true ); if ( is_wp_error( $result ) ) { return $result; diff --git a/plugins/wporg-5ftf/includes/pledge.php b/plugins/wporg-5ftf/includes/pledge.php index d5b350c..903cc17 100755 --- a/plugins/wporg-5ftf/includes/pledge.php +++ b/plugins/wporg-5ftf/includes/pledge.php @@ -205,10 +205,7 @@ function handle_activation_action( $post_id ) { $sendback = remove_query_arg( [ 'deactivated', 'reactivated' ], $sendback ); if ( 'deactivate' === $action ) { - wp_update_post( array( - 'ID' => $post_id, - 'post_status' => DEACTIVE_STATUS, - ) ); + deactivate( $post_id ); wp_redirect( add_query_arg( 'deactivated', 1, $sendback ) ); exit(); } else { @@ -355,6 +352,31 @@ function create_new_pledge( $name ) { return $pledge_id; } +/** + * Remove a pledge from view by setting its status to "deactivated". + * + * @param int $pledge_id The pledge to deactivate. + * @param bool $notify Whether the pledge admin should be notified of the deactivation. + * + * @return int|WP_Error Post ID on success. Otherwise WP_Error. + */ +function deactivate( $pledge_id, $notify = false ) { + $pledge = get_post( $pledge_id ); + $result = wp_update_post( + array( + 'ID' => $pledge_id, + 'post_status' => DEACTIVE_STATUS, + ), + true // Return a WP_Error. + ); + + if ( $notify && ! is_wp_error( $result ) ) { + Email\send_pledge_deactivation_email( $pledge ); + } + + return $result; +} + /** * Filter query for archive & search pages to ensure we're only showing the expected data. *