mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-07-04 18:05:45 +03:00
Manage Pledge: Enable Manage Form functionality
* Fix issue with fetching data in `get_pledge_meta` when no submission has been POST'd * Rename to `subset`, since `context` is also used elsewhere * Show form content when there are form errors * Fix warnings on new pledge form * Skip wpautop before save, so editors don’t need to edit HTML unless they want to
This commit is contained in:
parent
b898dc23f3
commit
60b5ca5d2a
4 changed files with 165 additions and 72 deletions
|
@ -22,6 +22,7 @@ add_shortcode( '5ftf_pledge_form_manage', __NAMESPACE__ . '\render_form_manage'
|
|||
*/
|
||||
function render_form_new() {
|
||||
$action = isset( $_GET['action'] ) ? filter_input( INPUT_GET, 'action' ) : filter_input( INPUT_POST, 'action' );
|
||||
$pledge_id = 0;
|
||||
$data = get_form_submission();
|
||||
$errors = [];
|
||||
$pledge = null;
|
||||
|
@ -65,7 +66,7 @@ function render_form_new() {
|
|||
*/
|
||||
function process_form_new() {
|
||||
$submission = get_form_submission();
|
||||
$has_error = check_invalid_submission( $submission );
|
||||
$has_error = check_invalid_submission( $submission, 'create' );
|
||||
if ( $has_error ) {
|
||||
return $has_error;
|
||||
}
|
||||
|
@ -149,56 +150,99 @@ function process_pledge_confirmation_email( $pledge_id, $action, $unverified_tok
|
|||
* @return false|string
|
||||
*/
|
||||
function render_form_manage() {
|
||||
$action = filter_input( INPUT_POST, 'action' );
|
||||
$messages = [];
|
||||
$updated = false;
|
||||
/*
|
||||
* Prevent Gutenberg from executing this on the Edit Post screen.
|
||||
* See https://github.com/WordPress/gutenberg/issues/18394
|
||||
*/
|
||||
if ( is_admin() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// @todo Get pledge ID from somewhere.
|
||||
$data = PledgeMeta\get_pledge_meta();
|
||||
$is_manage = true;
|
||||
$messages = [];
|
||||
$errors = [];
|
||||
|
||||
$action = sanitize_text_field( $_REQUEST['action'] ?? '' );
|
||||
$pledge_id = absint( $_REQUEST['pledge_id'] ?? 0 );
|
||||
$auth_token = sanitize_text_field( $_REQUEST['auth_token'] ?? '' );
|
||||
$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() );
|
||||
|
||||
ob_start();
|
||||
require FiveForTheFuture\PATH . 'views/partial-result-messages.php';
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
$contributors = Contributor\get_pledge_contributors( $pledge_id, $status = 'all' );
|
||||
|
||||
if ( 'Update Pledge' === $action ) {
|
||||
$processed = process_form_manage();
|
||||
$results = process_form_manage( $pledge_id, $auth_token );
|
||||
|
||||
if ( is_wp_error( $processed ) ) {
|
||||
$messages = array_merge( $messages, $processed->get_error_messages() );
|
||||
} elseif ( 'success' === $processed ) {
|
||||
$updated = true;
|
||||
if ( is_wp_error( $results ) ) {
|
||||
$errors = $results->get_error_messages();
|
||||
} else {
|
||||
$messages = array( __( 'Your pledge has been updated.', 'wporg-5ftf' ) );
|
||||
}
|
||||
}
|
||||
|
||||
$data = PledgeMeta\get_pledge_meta( $pledge_id );
|
||||
|
||||
ob_start();
|
||||
$readonly = false;
|
||||
$is_manage = true;
|
||||
require FiveForTheFuture\PATH . 'views/form-pledge-manage.php';
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a submission from the Manage Existing Pledge form.
|
||||
* Process a submission from the Manage Pledge form.
|
||||
*
|
||||
* TODO This doesn't actually update any data yet when the form is submitted.
|
||||
*
|
||||
* @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error.
|
||||
* @return WP_Error|true An error if the pledge could not be saved. Otherwise true.
|
||||
*/
|
||||
function process_form_manage() {
|
||||
function process_form_manage( $pledge_id, $auth_token ) {
|
||||
$errors = array();
|
||||
$nonce = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
|
||||
$nonce_action = 'manage_pledge_' . $pledge_id;
|
||||
$has_valid_nonce = wp_verify_nonce( $nonce, $nonce_action );
|
||||
|
||||
/*
|
||||
* This should be redundant, since it's also called by `render_form_manage()`, but it's good to also do it here
|
||||
* just in case other code changes in the future, or this gets called by another flow, etc.
|
||||
*/
|
||||
$can_view_form = Auth\can_manage_pledge( $pledge_id, $auth_token );
|
||||
|
||||
if ( ! $has_valid_nonce || ! $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 )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$submission = get_form_submission();
|
||||
$has_error = check_invalid_submission( $submission );
|
||||
$has_error = check_invalid_submission( $submission, 'update' );
|
||||
if ( $has_error ) {
|
||||
return $has_error;
|
||||
}
|
||||
|
||||
// todo email any new contributors for confirmation
|
||||
// notify any removed contributors?
|
||||
// ask them to update their profiles?
|
||||
// automatically update contributor profiles?
|
||||
// anything else?
|
||||
PledgeMeta\save_pledge_meta( $pledge_id, $submission );
|
||||
|
||||
// @todo Upload & attach logo.
|
||||
// @todo Save contributors.
|
||||
|
||||
// If we made it to here, we've successfully saved the pledge.
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and sanitize $_POST values from a form submission.
|
||||
*
|
||||
* @return array|bool
|
||||
* @return array
|
||||
*/
|
||||
function get_form_submission() {
|
||||
$input_filters = array_merge(
|
||||
|
@ -213,7 +257,8 @@ function get_form_submission() {
|
|||
|
||||
$result = filter_input_array( INPUT_POST, $input_filters );
|
||||
if ( ! $result ) {
|
||||
return array_fill_keys( array_keys( $input_filters ), '' );
|
||||
$result = array_fill_keys( array_keys( $input_filters ), '' );
|
||||
$result['empty_post'] = true;
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
@ -222,10 +267,13 @@ function get_form_submission() {
|
|||
/**
|
||||
* Check the submission for valid data.
|
||||
*
|
||||
* @param array $submission The user input.
|
||||
* @param string $context Whether this is a new pledge (`create`) or an edit to an existing one (`update`).
|
||||
*
|
||||
* @return false|WP_Error Return any errors in the submission, or false if no errors.
|
||||
*/
|
||||
function check_invalid_submission( $submission ) {
|
||||
$has_required = PledgeMeta\has_required_pledge_meta( $submission );
|
||||
function check_invalid_submission( $submission, $context ) {
|
||||
$has_required = PledgeMeta\has_required_pledge_meta( $submission, $context );
|
||||
if ( is_wp_error( $has_required ) ) {
|
||||
return $has_required;
|
||||
}
|
||||
|
@ -237,20 +285,22 @@ function check_invalid_submission( $submission ) {
|
|||
Pledge\CPT_ID
|
||||
);
|
||||
|
||||
if ( Pledge\has_existing_pledge( $email, 'email' ) ) {
|
||||
return new WP_Error(
|
||||
'existing_pledge_email',
|
||||
__( 'This email address is already connected to an existing pledge.', 'wporg-5ftf' )
|
||||
);
|
||||
}
|
||||
if ( 'create' === $context ) {
|
||||
if ( Pledge\has_existing_pledge( $email, 'email' ) ) {
|
||||
return new WP_Error(
|
||||
'existing_pledge_email',
|
||||
__( 'This email address is already connected to an existing pledge.', 'wporg-5ftf' )
|
||||
);
|
||||
}
|
||||
|
||||
$domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] );
|
||||
$domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] );
|
||||
|
||||
if ( Pledge\has_existing_pledge( $domain, 'domain' ) ) {
|
||||
return new WP_Error(
|
||||
'existing_pledge_domain',
|
||||
__( 'A pledge already exists for this domain.', 'wporg-5ftf' )
|
||||
);
|
||||
if ( Pledge\has_existing_pledge( $domain, 'domain' ) ) {
|
||||
return new WP_Error(
|
||||
'existing_pledge_domain',
|
||||
__( 'A pledge already exists for this domain.', 'wporg-5ftf' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue