Move "send mange email" code to ajax endpoint (#103)

Add an admin-ajax endpoint to request an authorized email to edit an existing pledge. The email submitted needs to match the submitted organizer email for the given pledge. If it does, an email will be sent out with the link to the pledge management form. If not, it will return an error.

See #98.
This commit is contained in:
Kelly Dwan 2019-11-20 18:04:40 -05:00 committed by GitHub
parent 72b5a159c9
commit a67ef04505
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 44 deletions

View file

@ -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 <a href="%s">email us</a> for help.', 'wporg-5ftf' ),
get_permalink( get_page_by_path( 'report' ) )
);
$result = [
'success' => false,
'message' => $error_message,
];
}
wp_die( wp_json_encode( $result ) );
}