Use JS + AJAX to resend the confirmation email

This commit is contained in:
Kelly Dwan 2019-11-12 16:17:29 -05:00
parent 7dbc725581
commit d5c740ae84
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
5 changed files with 88 additions and 11 deletions

View file

@ -0,0 +1,35 @@
<?php
/**
* Handle submissions to admin-ajax.php.
*/
namespace WordPressDotOrg\FiveForTheFuture\Endpoints;
use WordPressDotOrg\FiveForTheFuture\{ Contributor, PledgeForm };
add_action( 'wp_ajax_manage_contributors', __NAMESPACE__ . '\handler' );
/**
* Handle the AJAX request.
*/
function handler() {
check_ajax_referer( 'manage-pledge', '_ajax_nonce' );
$action = filter_input( INPUT_POST, 'manage_action' );
$pledge_id = filter_input( INPUT_POST, 'pledge_id', FILTER_VALIDATE_INT );
$contributor_id = filter_input( INPUT_POST, 'contributor_id', FILTER_VALIDATE_INT );
switch ( $action ) {
case 'resend-contributor-confirmation':
$contribution = get_post( $contributor_id );
PledgeForm\send_contributor_confirmation_emails( $pledge_id, $contributor_id );
wp_die( wp_json_encode( [
'success' => true,
'message' => sprintf( __( 'Confirmation email sent to %s.', 'wporg-5ftf' ), $contribution->post_title ),
] ) );
break;
}
// No matching action, we can just exit.
wp_die();
}

View file

@ -279,13 +279,6 @@ function save_pledge( $pledge_id, $pledge ) {
get_page_by_path( 'for-organizations' )->ID
);
}
if ( filter_input( INPUT_POST, 'resend-contributor-confirmation' ) ) {
Email\send_contributor_confirmation_emails(
$pledge_id,
filter_input( INPUT_GET, 'resend-contributor-id', FILTER_VALIDATE_INT )
);
}
}
/**
@ -485,8 +478,13 @@ function enqueue_assets() {
$ver = filemtime( FiveForTheFuture\PATH . '/assets/css/admin.css' );
wp_register_style( '5ftf-admin', plugins_url( 'assets/css/admin.css', __DIR__ ), [], $ver );
$ver = filemtime( FiveForTheFuture\PATH . '/assets/js/admin.js' );
wp_register_script( '5ftf-admin', plugins_url( 'assets/js/admin.js', __DIR__ ), [ 'jquery' ], $ver );
wp_localize_script( '5ftf-admin', 'FiveForTheFuture_ManageNonce', wp_create_nonce( 'manage-pledge' ) );
$current_page = get_current_screen();
if ( Pledge\CPT_ID === $current_page->id ) {
wp_enqueue_style( '5ftf-admin' );
wp_enqueue_script( '5ftf-admin' );
}
}