From 652451abfd293f0d510008f0049e7fd08e5c844a Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Mon, 18 Nov 2019 14:39:52 -0500 Subject: [PATCH] Move "send mange email" code to ajax endpoint # Conflicts: # plugins/wporg-5ftf/includes/endpoints.php --- plugins/wporg-5ftf/includes/endpoints.php | 44 +++++++++++++++++++++ plugins/wporg-5ftf/includes/pledge-form.php | 44 --------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/plugins/wporg-5ftf/includes/endpoints.php b/plugins/wporg-5ftf/includes/endpoints.php index 70b4c70..3f555af 100644 --- a/plugins/wporg-5ftf/includes/endpoints.php +++ b/plugins/wporg-5ftf/includes/endpoints.php @@ -6,9 +6,13 @@ namespace WordPressDotOrg\FiveForTheFuture\Endpoints; use WordPressDotOrg\FiveForTheFuture\{ Auth, Contributor, Email, PledgeForm }; +use const WordPressDotOrg\FiveForTheFuture\PledgeMeta\META_PREFIX; add_action( 'wp_ajax_manage-contributors', __NAMESPACE__ . '\manage_contributors_handler' ); +add_action( 'wp_ajax_send-manage-email', __NAMESPACE__ . '\send_manage_email_handler' ); +add_action( 'wp_ajax_nopriv_send-manage-email', __NAMESPACE__ . '\send_manage_email_handler' ); + /** * Handle the AJAX request for managing contributors on a pledge. * This responds to adding, removing, and resending emails to contributors. @@ -77,3 +81,43 @@ function manage_contributors_handler() { // No matching action, we can just exit. wp_die(); } + +/** + * Handle the AJAX request for managing a pledge. + * This responds to a request for a pledge manage link. + */ +function send_manage_email_handler() { + check_ajax_referer( 'send-manage-email', '_ajax_nonce' ); + + $pledge_id = filter_input( INPUT_POST, 'pledge_id', FILTER_VALIDATE_INT ); + $email = filter_input( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL ); + $valid_email = get_post( $pledge_id )->{ META_PREFIX . 'org-pledge-email' }; + + if ( $valid_email && $valid_email === $email ) { + $message_sent = Email\send_manage_pledge_link( $pledge_id ); + + if ( $message_sent ) { + $result = [ + 'success' => true, + 'message' => __( "Thanks! We've emailed you a link you can open in order to update your pledge.", 'wporg-5ftf' ), + ]; + } else { + $result = [ + 'success' => false, + 'message' => __( 'There was an error while trying to send the email.', 'wporg-5ftf' ), + ]; + } + } else { + $error_message = sprintf( + __( 'That\'s not the address that we have for this pledge, please try a different one. If none of the addresses you try are working, please email us for help.', 'wporg-5ftf' ), + get_permalink( get_page_by_path( 'report' ) ) + ); + + $result = [ + 'success' => false, + 'message' => $error_message, + ]; + } + + wp_die( wp_json_encode( $result ) ); +} diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index b510fcd..18d2a03 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -185,53 +185,9 @@ function render_manage_link_request() { return; } - $result = process_manage_link_request(); - - if ( is_wp_error( $result ) ) { - $errors = array( $result->get_error_message() ); - } elseif ( ! is_null( $result ) ) { - $messages = array( $result ); - } - require_once FiveForTheFuture\get_views_path() . 'form-pledge-request-manage-link.php'; } -/** - * Process a request for a pledge management link. - * - * @return null|string|WP_Error `null` if the form wasn't submitted; `string` with a success message; - * `WP_Error` with an error message. - */ -function process_manage_link_request() { - if ( ! filter_input( INPUT_POST, 'get_manage_pledge_link' ) ) { - return null; - } - - $unverified_pledge_id = filter_input( INPUT_POST, 'pledge_id', FILTER_VALIDATE_INT ); - $unverified_admin_email = filter_input( INPUT_POST, 'pledge_admin_address', FILTER_VALIDATE_EMAIL ); - $valid_admin_email = get_post( $unverified_pledge_id )->{ PledgeMeta\META_PREFIX . 'org-pledge-email' }; - - if ( $valid_admin_email && $valid_admin_email === $unverified_admin_email ) { - $verified_pledge_id = $unverified_pledge_id; // The addresses will only match is the pledge ID is valid. - $message_sent = Email\send_manage_pledge_link( $verified_pledge_id ); - - if ( $message_sent ) { - $result = __( "Thanks! We've emailed you a link you can open in order to update your pledge.", 'wporg-5ftf' ); - } else { - $result = new WP_Error( 'email_failed', __( 'There was an error while trying to send the email.', 'wporg-5ftf' ) ); - } - } else { - $error_message = sprintf( - __( 'That\'s not the address that we have for this pledge, please try a different one. If none of the addresses you try are working, please email us for help.', 'wporg-5ftf' ), - get_permalink( get_page_by_path( 'report' ) ) - ); - - $result = new WP_Error( 'invalid_pledge_email', $error_message ); - } - - return $result; -} - /** * Process a submission from the Manage Existing Pledge form. *