From d0dc2f0a142efd998f71813650f1a5febbb2099c Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 14 Nov 2019 14:16:39 -0500 Subject: [PATCH] Send contributors the confirmation email when created by the ajax endpoint This will only send the email for published pledges. --- plugins/wporg-5ftf/includes/contributor.php | 4 +++- plugins/wporg-5ftf/includes/endpoints.php | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 36500c4..fabb36f 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -135,7 +135,7 @@ function populate_list_table_columns( $column, $post_id ) { * @param int $pledge_id The post ID of the pledge. * @param array $contributors Array of contributor wporg usernames. * - * @return void + * @return array List of the new contributor post IDs, mapped from username => ID. */ function add_pledge_contributors( $pledge_id, $contributors ) { $results = array(); @@ -162,6 +162,8 @@ function add_pledge_contributors( $pledge_id, $contributors ) { * or an error code on failure. */ do_action( FiveForTheFuture\PREFIX . '_add_pledge_contributors', $pledge_id, $contributors, $results ); + + return $results; } /** diff --git a/plugins/wporg-5ftf/includes/endpoints.php b/plugins/wporg-5ftf/includes/endpoints.php index 850072b..a0d8af3 100644 --- a/plugins/wporg-5ftf/includes/endpoints.php +++ b/plugins/wporg-5ftf/includes/endpoints.php @@ -39,6 +39,7 @@ function handler() { break; case 'add-contributor': + $pledge = get_post( $pledge_id ); $new_contributors = PledgeForm\parse_contributors( $_POST['contributors'] ); if ( is_wp_error( $new_contributors ) ) { wp_die( wp_json_encode( [ @@ -46,7 +47,12 @@ function handler() { 'message' => $new_contributors->get_error_message(), ] ) ); } - Contributor\add_pledge_contributors( $pledge_id, $new_contributors ); + $contributor_ids = Contributor\add_pledge_contributors( $pledge_id, $new_contributors ); + if ( 'publish' === $pledge->post_status ) { + foreach ( $contributor_ids as $contributor_id ) { + PledgeForm\send_contributor_confirmation_emails( $pledge_id, $contributor_id ); + } + } // Fetch all contributors, now that the new ones have been added. $contributors = Contributor\get_pledge_contributors_data( $pledge_id );