Pledge Management: Allow pledge admins to remove the pledge (#123)

* Rename generic "frontend" file to dialog

* Add a "remove pledge" action

* Process pledge deactivation

* Trigger an email on user-initiated pledge deactivation

* Show an error when trying to manage a deactivated pledge

* Add a label for "deactivated" pledges
This commit is contained in:
Kelly Dwan 2019-12-10 14:07:48 -05:00 committed by GitHub
parent 619af97bd1
commit bdbf6d573e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 184 additions and 26 deletions

View file

@ -123,15 +123,40 @@ function render_form_manage() {
$can_view_form = Auth\can_manage_pledge( $pledge_id, $auth_token );
if ( is_wp_error( $can_view_form ) ) {
// Can't manage pledge, only show errors.
$errors = array( $can_view_form->get_error_message() );
} else if ( ! in_array( get_post_status( $pledge_id ), array( 'pending', 'publish' ), true ) ) {
$errors = array(
sprintf(
__( 'This pledge has been removed from Five for the Future. If this was a mistake, please <a href="%s">contact us</a> to reactivate your pledge.', 'wporg-5ftf' ),
get_permalink( get_page_by_path( 'report' ) )
),
);
}
if ( count( $errors ) > 0 ) {
ob_start();
require FiveForTheFuture\PATH . 'views/partial-result-messages.php';
return ob_get_clean();
}
if ( 'Update Pledge' === $action ) {
if ( 'remove-pledge' === $action ) {
$results = process_form_remove( $pledge_id, $auth_token );
if ( is_wp_error( $results ) ) {
$errors = $results->get_error_messages();
} else {
$messages = array(
sprintf(
__( 'Your pledge has been removed. If this was a mistake, please <a href="%s">contact us</a> to reactivate your pledge.', 'wporg-5ftf' ),
get_permalink( get_page_by_path( 'report' ) )
),
);
}
ob_start();
require FiveForTheFuture\PATH . 'views/partial-result-messages.php';
return ob_get_clean();
} else if ( 'Update Pledge' === $action ) {
$results = process_form_manage( $pledge_id, $auth_token );
if ( is_wp_error( $results ) ) {
@ -215,12 +240,42 @@ function process_form_manage( $pledge_id, $auth_token ) {
}
}
// @todo Save contributors.
// If we made it to here, we've successfully saved the pledge.
return true;
}
/**
* Process a submission from the Manage Pledge form.
*
* @return WP_Error|true An error if the pledge could not be saved. Otherwise true.
*/
function process_form_remove( $pledge_id, $auth_token ) {
$errors = array();
$nonce = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
$nonce_action = 'remove_pledge_' . $pledge_id;
$has_valid_nonce = wp_verify_nonce( $nonce, $nonce_action );
$can_view_form = Auth\can_manage_pledge( $pledge_id, $auth_token );
if ( ! $has_valid_nonce || is_wp_error( $can_view_form ) ) {
return new WP_Error(
'invalid_token',
sprintf(
__( 'Your link has expired, please <a href="%s">obtain a new one</a>.', 'wporg-5ftf' ),
get_permalink( $pledge_id )
)
);
}
$result = Pledge\deactivate( $pledge_id, true );
if ( is_wp_error( $result ) ) {
return $result;
}
// If we made it to here, we've successfully removed the pledge.
return true;
}
/**
* Process a request to confirm a company's email address.
*