Move "send mange email" code to ajax endpoint

# Conflicts:
#	plugins/wporg-5ftf/includes/endpoints.php
This commit is contained in:
Kelly Dwan 2019-11-18 14:39:52 -05:00
parent 72b5a159c9
commit 652451abfd
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
2 changed files with 44 additions and 44 deletions

View file

@ -6,9 +6,13 @@
namespace WordPressDotOrg\FiveForTheFuture\Endpoints; namespace WordPressDotOrg\FiveForTheFuture\Endpoints;
use WordPressDotOrg\FiveForTheFuture\{ Auth, Contributor, Email, PledgeForm }; 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_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. * Handle the AJAX request for managing contributors on a pledge.
* This responds to adding, removing, and resending emails to contributors. * 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. // No matching action, we can just exit.
wp_die(); 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 ) );
}

View file

@ -185,53 +185,9 @@ function render_manage_link_request() {
return; 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'; 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 <a href="%s">email us</a> 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. * Process a submission from the Manage Existing Pledge form.
* *