From 213531ca0845061133ef5a0f1a009a14e61a0931 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Thu, 3 Oct 2019 16:14:33 -0700 Subject: [PATCH] Revisions to pledge forms --- plugins/wporg-5ftf/includes/pledge-form.php | 167 ++++++++++++------ .../wporg-5ftf/views/form-pledge-manage.php | 8 + .../{pledge-form.php => form-pledge-new.php} | 0 3 files changed, 118 insertions(+), 57 deletions(-) create mode 100644 plugins/wporg-5ftf/views/form-pledge-manage.php rename plugins/wporg-5ftf/views/{pledge-form.php => form-pledge-new.php} (100%) diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index 4117670..11a5d20 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -12,20 +12,21 @@ use WP_Error; defined( 'WPINC' ) || die(); -add_shortcode( 'five_for_the_future_pledge_form', __NAMESPACE__ . '\render_shortcode' ); +add_shortcode( '5ftf_pledge_form_new', __NAMESPACE__ . '\render_form_new' ); +add_shortcode( '5ftf_pledge_form_manage', __NAMESPACE__ . '\render_form_manage' ); /** - * + * Render the form(s) for creating new pledges. * * @return false|string */ -function render_shortcode() { +function render_form_new() { $action = filter_input( INPUT_POST, 'action' ); $messages = []; $complete = false; if ( 'Submit Pledge' === $action ) { - $processed = process_form(); + $processed = process_form_new(); if ( is_wp_error( $processed ) ) { $messages = array_merge( $messages, $processed->get_error_messages() ); @@ -35,11 +36,102 @@ function render_shortcode() { } ob_start(); - require FiveForTheFuture\PATH . 'views/pledge-form.php'; + require FiveForTheFuture\PATH . 'views/form-pledge-new.php'; return ob_get_clean(); } +/** + * + * + * @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error. + */ +function process_form_new() { + $submission = filter_input_array( INPUT_POST, get_input_filters() ); + + $has_required = PledgeMeta\has_required_pledge_meta( $submission ); + + if ( is_wp_error( $has_required ) ) { + return $has_required; + } + + $domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] ); + + if ( has_existing_pledge( $domain ) ) { + return new WP_Error( + 'existing_pledge', + __( 'A pledge already exists for this domain.', 'wporg' ) + ); + } + + $name = sanitize_meta( + PledgeMeta\META_PREFIX . 'org-name', + $submission['org-name'], + 'post', + Pledge\CPT_ID + ); + + $created = create_new_pledge( $name ); + + if ( is_wp_error( $created ) ) { + return $created; + } + + //PledgeMeta\save_pledge_meta( $created, $submission ); + + return 'success'; +} + +/** + * Render the form(s) for managing existing pledges. + * + * @return false|string + */ +function render_form_manage() { + $action = filter_input( INPUT_POST, 'action' ); + $messages = []; + $updated = false; + + if ( 'Update Pledge' === $action ) { + $processed = process_form_manage(); + + if ( is_wp_error( $processed ) ) { + $messages = array_merge( $messages, $processed->get_error_messages() ); + } elseif ( 'success' === $processed ) { + $updated = true; + } + } + + ob_start(); + require FiveForTheFuture\PATH . 'views/form-pledge-manage.php'; + + return ob_get_clean(); +} + +/** + * + * + * @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error. + */ +function process_form_manage() { + $submission = filter_input_array( INPUT_POST, get_input_filters() ); + + $has_required = PledgeMeta\has_required_pledge_meta( $submission ); + + if ( is_wp_error( $has_required ) ) { + return $has_required; + } + + $domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] ); + + if ( has_existing_pledge( $domain ) ) { + return new WP_Error( + 'existing_pledge', + __( 'A pledge already exists for this domain.', 'wporg' ) + ); + } +} + /** * * @@ -60,67 +152,29 @@ function get_input_filters() { ); } -/** - * - * - * @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error. - */ -function process_form() { - $submission = filter_input_array( INPUT_POST, get_input_filters() ); - - $submission['org-domain'] = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] ); - - if ( in_array( null, $submission, true ) || in_array( false, $submission, true ) ) { - return new WP_Error( - 'invalid_submission', - __( 'Some fields have missing or invalid information.', 'wporg' ) - ); - } - - $has_existing_pledge = has_existing_pledge( $submission['org-domain'] ); - - if ( $has_existing_pledge ) { - return new WP_Error( - 'existing_pledge', - __( 'A pledge already exists for this domain.', 'wporg' ) - ); - } - - $name = sanitize_meta( - PledgeMeta\META_PREFIX . 'org-name', - $submission['org-name'], - 'post', - Pledge\CPT_ID - ); - - $created = create_new_pledge( $name ); - - if ( is_wp_error( $created ) ) { - return $created; - } - - PledgeMeta\save_pledge_meta( $created, $submission ); - - return 'success'; -} - /** * * * @param string $domain + * @param int $current_pledge_id * * @return bool */ -function has_existing_pledge( $domain ) { - $matching_pledge = get_posts( array( +function has_existing_pledge( $domain, int $current_pledge_id = 0 ) { + $args = array( 'post_type' => Pledge\CPT_ID, 'post_status' => array( 'pending', 'publish' ), 'meta_query' => array( - 'key' => PledgeMeta\META_PREFIX . 'org-domain', - 'value' => $domain, - 'compare' => 'LIKE', + 'key' => PledgeMeta\META_PREFIX . 'org-domain', + 'value' => $domain, ), - ) ); + ); + + if ( $current_pledge_id ) { + $args['exclude'] = array( $current_pledge_id ); + } + + $matching_pledge = get_posts( $args ); return ! empty( $matching_pledge ); } @@ -136,8 +190,7 @@ function create_new_pledge( $name ) { $args = [ 'post_type' => Pledge\CPT_ID, 'post_title' => $name, - 'post_status' => 'pending', - 'post_author' => get_current_user_id(), // TODO is this how we want to do this? + 'post_status' => 'draft', ]; return wp_insert_post( $args, true ); diff --git a/plugins/wporg-5ftf/views/form-pledge-manage.php b/plugins/wporg-5ftf/views/form-pledge-manage.php new file mode 100644 index 0000000..0fa4736 --- /dev/null +++ b/plugins/wporg-5ftf/views/form-pledge-manage.php @@ -0,0 +1,8 @@ + diff --git a/plugins/wporg-5ftf/views/pledge-form.php b/plugins/wporg-5ftf/views/form-pledge-new.php similarity index 100% rename from plugins/wporg-5ftf/views/pledge-form.php rename to plugins/wporg-5ftf/views/form-pledge-new.php