2019-11-20 18:01:00 +02:00
< ? php
/**
* Handle submissions to admin - ajax . php .
*/
namespace WordPressDotOrg\FiveForTheFuture\Endpoints ;
2019-11-21 23:30:50 +02:00
use WordPressDotOrg\FiveForTheFuture\ { Auth , Contributor , Email };
2019-11-21 01:04:40 +02:00
use const WordPressDotOrg\FiveForTheFuture\PledgeMeta\META_PREFIX ;
2019-11-20 18:01:00 +02:00
2019-11-26 19:57:14 +02:00
add_action ( 'wp_ajax_manage-contributors' , __NAMESPACE__ . '\manage_contributors_handler' );
add_action ( 'wp_ajax_nopriv_manage-contributors' , __NAMESPACE__ . '\manage_contributors_handler' );
2019-11-20 18:01:00 +02:00
2019-11-21 01:04:40 +02:00
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' );
2019-11-20 18:01:00 +02:00
/**
* Handle the AJAX request for managing contributors on a pledge .
* This responds to adding , removing , and resending emails to contributors .
*/
function manage_contributors_handler () {
check_ajax_referer ( 'manage-contributors' , '_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 );
$token = filter_input ( INPUT_POST , '_token' );
$authenticated = Auth\can_manage_pledge ( $pledge_id , $token );
if ( is_wp_error ( $authenticated ) ) {
2020-11-13 22:16:06 +02:00
wp_die ( wp_json_encode ( array (
2019-11-20 18:01:00 +02:00
'success' => false ,
2019-12-03 22:09:35 +02:00
'message' => __ ( " Sorry, you don't have permissions to do that. " , 'wporg-5ftf' ),
2020-11-13 22:16:06 +02:00
) ) );
2019-11-20 18:01:00 +02:00
}
switch ( $action ) {
case 'resend-contributor-confirmation' :
$contribution = get_post ( $contributor_id );
Email\send_contributor_confirmation_emails ( $pledge_id , $contributor_id );
2020-11-13 22:16:06 +02:00
wp_die ( wp_json_encode ( array (
2019-11-20 18:01:00 +02:00
'success' => true ,
'message' => sprintf ( __ ( 'Confirmation email sent to %s.' , 'wporg-5ftf' ), $contribution -> post_title ),
2020-11-13 22:16:06 +02:00
) ) );
2019-11-20 18:01:00 +02:00
break ;
case 'remove-contributor' :
// Trash contributor.
Contributor\remove_contributor ( $contributor_id );
2020-11-13 22:16:06 +02:00
wp_die ( wp_json_encode ( array (
2019-11-20 18:01:00 +02:00
'success' => true ,
'contributors' => Contributor\get_pledge_contributors_data ( $pledge_id ),
2020-11-13 22:16:06 +02:00
) ) );
2019-11-20 18:01:00 +02:00
break ;
case 'add-contributor' :
2019-12-03 22:09:35 +02:00
$pledge = get_post ( $pledge_id );
2022-03-18 00:07:30 +02:00
$new_contributors = Contributor\parse_contributors ( $_POST [ 'contributors' ], $pledge -> ID );
2019-11-20 18:01:00 +02:00
if ( is_wp_error ( $new_contributors ) ) {
2020-11-13 22:16:06 +02:00
wp_die ( wp_json_encode ( array (
2019-11-20 18:01:00 +02:00
'success' => false ,
'message' => $new_contributors -> get_error_message (),
2020-11-13 22:16:06 +02:00
) ) );
2019-11-20 18:01:00 +02:00
}
2019-12-03 22:09:35 +02:00
2019-11-20 18:01:00 +02:00
$contributor_ids = Contributor\add_pledge_contributors ( $pledge_id , $new_contributors );
if ( 'publish' === $pledge -> post_status ) {
foreach ( $contributor_ids as $contributor_id ) {
Email\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 );
2020-11-13 22:16:06 +02:00
wp_die ( wp_json_encode ( array (
2019-11-20 18:01:00 +02:00
'success' => true ,
'contributors' => $contributors ,
2020-11-13 22:16:06 +02:00
) ) );
2019-11-20 18:01:00 +02:00
break ;
}
// No matching action, we can just exit.
wp_die ();
}
2019-11-21 01:04:40 +02:00
/**
* 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 );
2022-03-29 00:17:29 +03:00
$email = strtolower ( filter_input ( INPUT_POST , 'email' , FILTER_VALIDATE_EMAIL ) );
2022-08-25 10:59:36 +03:00
$valid_email = strtolower ( get_post ( $pledge_id ) -> { META_PREFIX . 'org-pledge-email' } ? ? '' );
2019-11-21 01:04:40 +02:00
if ( $valid_email && $valid_email === $email ) {
$message_sent = Email\send_manage_pledge_link ( $pledge_id );
if ( $message_sent ) {
2020-11-13 22:16:06 +02:00
$result = array (
2019-11-21 01:04:40 +02:00
'success' => true ,
2019-11-23 20:24:37 +02:00
'message' => __ ( 'Thanks! We’ ve emailed you a link you can open in order to update your pledge.' , 'wporg-5ftf' ),
2020-11-13 22:16:06 +02:00
);
2019-11-21 01:04:40 +02:00
} else {
2020-11-13 22:16:06 +02:00
$result = array (
2019-11-21 01:04:40 +02:00
'success' => false ,
'message' => __ ( 'There was an error while trying to send the email.' , 'wporg-5ftf' ),
2020-11-13 22:16:06 +02:00
);
2019-11-21 01:04:40 +02:00
}
} else {
$error_message = sprintf (
2019-11-23 20:24:37 +02:00
__ ( 'That’ s not the address that we have for this pledge. If you don’ t know the email associated with this pledge, <a href="%s">please contact us for help.</a>' , 'wporg-5ftf' ),
2019-11-21 01:04:40 +02:00
get_permalink ( get_page_by_path ( 'report' ) )
);
2020-11-13 22:16:06 +02:00
$result = array (
2019-11-21 01:04:40 +02:00
'success' => false ,
'message' => $error_message ,
2020-11-13 22:16:06 +02:00
);
2019-11-21 01:04:40 +02:00
}
wp_die ( wp_json_encode ( $result ) );
}