From 183f7a95bc51d8b2306cb089ef6ef1b07b67e5de Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Mon, 16 Dec 2019 17:33:43 -0500 Subject: [PATCH] Manage Pledge: Notify removed contributors (#109) If a contributor is removed from a pledge, they should be notified of the change. This will send an email to any confirmed contributors letting them know. --- plugins/wporg-5ftf/includes/contributor.php | 12 +++++++++--- plugins/wporg-5ftf/includes/email.php | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 22c893d..22b282f 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -2,7 +2,7 @@ namespace WordPressDotOrg\FiveForTheFuture\Contributor; use WordPressDotOrg\FiveForTheFuture; -use WordPressDotOrg\FiveForTheFuture\{ Pledge, XProfile }; +use WordPressDotOrg\FiveForTheFuture\{ Email, Pledge, XProfile }; use WP_Error, WP_Post, WP_User; defined( 'WPINC' ) || die(); @@ -177,8 +177,13 @@ function add_pledge_contributors( $pledge_id, $contributors ) { * @return false|WP_Post|null */ function remove_contributor( $contributor_post_id ) { - $pledge_id = get_post( $contributor_post_id )->post_parent; - $result = wp_trash_post( $contributor_post_id ); + $contributor = get_post( $contributor_post_id ); + $pledge_id = $contributor->post_parent; + $result = wp_trash_post( $contributor_post_id ); + + if ( $result && 'publish' === $contributor->post_status ) { + Email\send_contributor_removed_email( $pledge_id, $contributor ); + } /** * Action: Fires when a contributor is removed from a pledge. @@ -326,6 +331,7 @@ function get_contributor_user_ids( $contributor_posts ) { "; $user_ids = $wpdb->get_col( + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- phpcs is confused by the variable, but it does correctly prepare. $wpdb->prepare( $query, $usernames ) ); diff --git a/plugins/wporg-5ftf/includes/email.php b/plugins/wporg-5ftf/includes/email.php index 1988a59..cf3c0c1 100644 --- a/plugins/wporg-5ftf/includes/email.php +++ b/plugins/wporg-5ftf/includes/email.php @@ -115,11 +115,29 @@ function send_contributor_confirmation_emails( $pledge_id, $contributor_id = nul "If {$pledge->post_title} isn't sponsoring your contributions, then you can ignore this email, and you won't be listed on their pledge."; - $user = get_user_by( 'login', $contributor->post_title ); send_email( $user->user_email, $subject, $message, $pledge_id ); } } +/** + * Send the removed contributor an email to notify them after removal. + * + * @param int $pledge_id + * @param WP_Post $contributor + */ +function send_contributor_removed_email( $pledge_id, $contributor ) { + $pledge = get_post( $pledge_id ); + $subject = "Removed from {$pledge->post_title} Five for the Future pledge"; + $message = "Howdy {$contributor->post_title},\n\n"; + $message .= sprintf( + 'This email is to notify you that your WordPress.org contributor profile is no longer linked to %1$s’s Five for the Future pledge. If this is unexpected news, it’s best to reach out directly to %1$s with questions. Have a great day!', + $pledge->post_title + ); + + $user = get_user_by( 'login', $contributor->post_title ); + send_email( $user->user_email, $subject, $message, $pledge_id ); +} + /** * Email the pledge admin a temporary link they can use to manage their pledge. *