Refinements to the pledge form and associated meta

This commit is contained in:
Corey McKrill 2019-10-01 14:17:02 -07:00
parent 7092faebcb
commit abbfe6dad1
No known key found for this signature in database
GPG key ID: C2C0746F7BF17E38
3 changed files with 180 additions and 177 deletions

View file

@ -17,19 +17,15 @@ add_shortcode( 'five_for_the_future_pledge_form', __NAMESPACE__ . '\render_short
/** /**
* *
* *
* @param $attributes
* @param $content
*
* @return false|string * @return false|string
*/ */
function render_shortcode( $attributes, $content ) { function render_shortcode() {
$action = filter_input( INPUT_POST, 'action' ); $action = filter_input( INPUT_POST, 'action' );
$messages = []; $messages = [];
$complete = false; $complete = false;
$html = '';
if ( 'Submit' === $action ) { if ( 'Submit Pledge' === $action ) {
$processed = process_form( $_POST ); $processed = process_form();
if ( is_wp_error( $processed ) ) { if ( is_wp_error( $processed ) ) {
$messages = array_merge( $messages, $processed->get_error_messages() ); $messages = array_merge( $messages, $processed->get_error_messages() );
@ -38,34 +34,61 @@ function render_shortcode( $attributes, $content ) {
} }
} }
if ( $complete ) {
$html = wpautop( __( 'Thank you for your submission.', 'wporg' ) );
} else {
ob_start(); ob_start();
require FiveForTheFuture\PATH . 'views/pledge-form.php'; require FiveForTheFuture\PATH . 'views/pledge-form.php';
$html = ob_get_clean();
}
return $html; return ob_get_clean();
} }
/** /**
* *
* *
* @param array $form_values * @return array
*/
function get_input_filters() {
return array_merge(
// Inputs that correspond to meta values.
wp_list_pluck( PledgeMeta\get_pledge_meta_config(), 'php_filter' ),
// Inputs with no corresponding meta value.
array(
'contributor-wporg-usernames' => [
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_REQUIRE_ARRAY,
],
'pledge-agreement' => FILTER_VALIDATE_BOOLEAN,
)
);
}
/**
*
* *
* @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error. * @return string|WP_Error String "success" if the form processed correctly. Otherwise WP_Error.
*/ */
function process_form( array $form_values ) { function process_form() {
$required_fields = PledgeMeta\has_required_pledge_meta( $form_values ); $submission = filter_input_array( INPUT_POST, get_input_filters() );
if ( is_wp_error( $required_fields ) ) { $submission['org-domain'] = get_normalized_domain_from_url( $submission['org-url'] );
return $required_fields;
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( $name = sanitize_meta(
PledgeMeta\META_PREFIX . 'company-name', PledgeMeta\META_PREFIX . 'org-name',
$form_values['company-name'], $submission['org-name'],
'post', 'post',
Pledge\CPT_ID Pledge\CPT_ID
); );
@ -76,12 +99,46 @@ function process_form( array $form_values ) {
return $created; return $created;
} }
PledgeMeta\save_pledge_meta( $created, $form_values ); PledgeMeta\save_pledge_meta( $created, $submission );
// save teams contirbuted to as terms
return 'success'; return 'success';
} }
/**
*
*
* @param string $url
*
* @return string
*/
function get_normalized_domain_from_url( $url ) {
$domain = wp_parse_url( $url, PHP_URL_HOST );
$domain = preg_replace( '#^www\.#', '', $domain );
return $domain;
}
/**
*
*
* @param string $domain
*
* @return bool
*/
function has_existing_pledge( $domain ) {
$matching_pledge = get_posts( array(
'post_type' => Pledge\CPT_ID,
'post_status' => array( 'pending', 'publish' ),
'meta_query' => array(
'key' => PledgeMeta\META_PREFIX . 'org-domain',
'value' => $domain,
'compare' => 'LIKE',
),
) );
return ! empty( $matching_pledge );
}
/** /**
* *
* *

View file

@ -11,7 +11,7 @@ use WP_Post, WP_Error;
defined( 'WPINC' ) || die(); defined( 'WPINC' ) || die();
const META_PREFIX = FiveForTheFuture\PREFIX . '-'; const META_PREFIX = FiveForTheFuture\PREFIX . '_';
add_action( 'init', __NAMESPACE__ . '\register_pledge_meta' ); add_action( 'init', __NAMESPACE__ . '\register_pledge_meta' );
add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' ); add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' );
@ -24,50 +24,40 @@ add_action( 'save_post', __NAMESPACE__ . '\save_pledge', 10, 2 );
*/ */
function get_pledge_meta_config() { function get_pledge_meta_config() {
return [ return [
'company-name' => [ 'org-name' => [
'show_in_rest' => true, 'single' => true,
'sanitize_callback' => 'sanitize_text_field', 'sanitize_callback' => 'sanitize_text_field',
'required' => true,
],
'company-url' => [
'show_in_rest' => true, 'show_in_rest' => true,
'required' => true,
'php_filter' => FILTER_SANITIZE_STRING
],
'org-url' => [
'single' => true,
'sanitize_callback' => 'esc_url_raw', 'sanitize_callback' => 'esc_url_raw',
'required' => true,
],
'company-email' => [
'show_in_rest' => false,
'sanitize_callback' => 'sanitize_email',
'required' => true,
],
'company-phone' => [
'show_in_rest' => false,
'sanitize_callback' => 'sanitize_text_field',
'required' => false,
],
'company-total-employees' => [
'show_in_rest' => true, 'show_in_rest' => true,
'sanitize_callback' => 'absint',
'required' => true, 'required' => true,
'php_filter' => FILTER_VALIDATE_URL,
], ],
'contact-name' => [ 'org-domain' => [
'show_in_rest' => false, 'single' => true,
'sanitize_callback' => 'sanitize_text_field', 'sanitize_callback' => 'sanitize_text_field',
'required' => true,
],
'contact-wporg-username' => [
'show_in_rest' => false, 'show_in_rest' => false,
'required' => true,
'php_filter' => FILTER_SANITIZE_STRING,
],
'org-description' => [
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
'show_in_rest' => true,
'required' => true,
'php_filter' => FILTER_SANITIZE_STRING
],
'admin-wporg-username' => [
'single' => true,
'sanitize_callback' => 'sanitize_user', 'sanitize_callback' => 'sanitize_user',
'required' => true,
],
'pledge-hours' => [
'show_in_rest' => true,
'sanitize_callback' => 'absint',
'required' => true,
],
'pledge-agreement' => [
'show_in_rest' => false, 'show_in_rest' => false,
'sanitize_callback' => 'wp_validate_boolean',
'required' => true, 'required' => true,
'php_filter' => FILTER_SANITIZE_STRING
], ],
]; ];
} }
@ -165,11 +155,13 @@ function save_pledge( $pledge_id, $pledge ) {
return; return;
} }
if ( is_wp_error( has_required_pledge_meta( $_POST ) ) ) { $submitted_meta = filter_input_array( INPUT_POST, wp_list_pluck( get_pledge_meta_config(), 'php_filter' ) );
if ( is_wp_error( has_required_pledge_meta( $submitted_meta ) ) ) {
return; return;
} }
save_pledge_meta( $pledge_id, $_POST ); save_pledge_meta( $pledge_id, $submitted_meta );
} }
/** /**
@ -192,12 +184,12 @@ function save_pledge_meta( $pledge_id, $new_values ) {
delete_post_meta( $pledge_id, $meta_key ); delete_post_meta( $pledge_id, $meta_key );
} }
} }
// maybe set the wporg username as the company author, so they can edit it themselves to keep it updated,
// then make the user a contributor if they don't already have a role on the site
// setup cron to automatically email once per quarter
// "here's all the info we have: x, y, z"
// is that still accurate? if not, click here to update it
// if want to be removed from public listing, emailing support@wordcamp.org
// don't let them edit the "featured" taxonomy, only admins
} }
// maybe set the wporg username as the company author, so they can edit it themselves to keep it updated,
// then make the user a contributor if they don't already have a role on the site
// setup cron to automatically email once per quarter
// "here's all the info we have: x, y, z"
// is that still accurate? if not, click here to update it
// if want to be removed from public listing, emailing support@wordcamp.org
// don't let them edit the "featured" taxonomy, only admins

View file

@ -4,152 +4,106 @@
*/ */
/** @var array $messages */ /** @var array $messages */
/** @var bool $complete */
?> ?>
<p>
<a href="#">Manage an existing pledge</a>
</p>
<?php if ( ! empty( $messages ) ) : ?> <?php if ( ! empty( $messages ) ) : ?>
<?php foreach ( $messages as $message ) : ?> <?php foreach ( $messages as $message ) : ?>
<div class="notice notice-error"> <div class="notice notice-error">
<?php echo wpautop( $message ); ?> <?php echo wpautop( $message ); ?>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
<?php endif; ?> <?php endif; ?>
<form action="" method="post"> <?php if ( true === $complete ) : ?>
<fieldset>
<legend><?php _e( 'Company Details', 'wporg' ); ?></legend>
<div class="notice notice-info">
<?php echo wpautop( __( 'Thank you for your submission. You will receive an email confirmation.', 'wporg' ) ); ?>
</div>
<?php else : ?>
<form action="" method="post">
<div> <div>
<label for="5ftf-company-name"> <label for="5ftf-org-name">
<?php _e( 'Company Name', 'wporg' ); ?> <?php _e( 'Organization Name', 'wporg' ); ?>
</label>
<input <input
type="text" type="text"
id="5ftf-company-name" id="5ftf-org-name"
name="company-name" name="org-name"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'company-name' ) ); ?>" value="<?php echo esc_attr( filter_input( INPUT_POST, 'org-name' ) ); ?>"
required required
/> />
</label>
</div> </div>
<div> <div>
<label for="5ftf-company-url"> Logo <strong>TODO</strong>
<?php _e( 'Company URL', 'wporg' ); ?>
<input
type="url"
id="5ftf-company-url"
name="company-url"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'company-url' ) ); ?>"
required
/>
</label>
</div> </div>
<div> <div>
<label for="5ftf-company-email"> <label for="5ftf-org-description">
<?php _e( 'Company Email', 'wporg' ); ?> <?php _e( 'Organization Blurb', 'wporg' ); ?>
<input
type="email"
id="5ftf-company-email"
name="company-email"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'company-email' ) ); ?>"
required
/>
</label> </label>
<textarea
id="5ftf-org-description"
name="org-description"
required
>
<?php echo esc_html( filter_input( INPUT_POST, 'org-description' ) ); ?>
</textarea>
<span class="field-help">280 characters</span>
</div> </div>
<div> <div>
<label for="5ftf-company-phone"> <label for="5ftf-admin-wporg-username">
<?php _e( 'Company Phone Number', 'wporg' ); ?> <?php _e( 'Admin Username', 'wporg' ); ?>
</label>
<input <input
type="text" type="text"
id="5ftf-company-phone" id="5ftf-admin-wporg-username"
name="company-phone" name="admin-wporg-username"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'company-phone' ) ); ?>" value="<?php echo esc_attr( filter_input( INPUT_POST, 'admin-wporg-username' ) ); ?>"
/>
</label>
</div>
<div>
<label for="5ftf-company-total-employees">
<?php _e( 'Total Employees', 'wporg' ); ?>
<input
type="number"
id="5ftf-company-total-employees"
name="company-total-employees"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'company-total-employees' ) ); ?>"
required required
/> />
</label> <span class="field-help">This user will be responsible for managing your organization's pledge.</span>
</div> </div>
</fieldset>
<fieldset>
<legend><?php _e( 'Pledge Manager', 'wporg' ); ?></legend>
<div> <div>
<label for="5ftf-contact-name"> <label for="5ftf-contributor-wporg-usernames">
<?php _e( 'Name', 'wporg' ); ?> <?php _e( 'Contributing Employee Usernames', 'wporg' ); ?>
</label>
<input <input
type="text" type="text"
id="5ftf-contact-name" id="5ftf-contributor-wporg-usernames"
name="contact-name" name="contributor-wporg-usernames"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'contact-name' ) ); ?>" value="<?php echo esc_attr( filter_input( INPUT_POST, 'contributor-wporg-usernames' ) ); ?>"
required required
/> />
</label> <span class="field-help">Separate each username with a comma.</span>
</div> </div>
<div> <div>
<label for="5ftf-contact-wporg-username"> <label for="5ftf-pledge-agreement">
<?php _e( 'WordPress.org User Name', 'wporg' ); ?>
<input
type="text"
id="5ftf-contact-wporg-username"
name="contact-wporg-username"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'contact-wporg-username' ) ); ?>"
required
/>
</label>
</div>
</fieldset>
<fieldset>
<legend><?php _e( 'Pledge', 'wporg' ); ?></legend>
<div>
<label for="5ftf-pledge-hours">
<?php _e( 'Pledged Hours Per Week', 'wporg' ); ?>
<input
type="number"
id="5ftf-pledge-hours"
name="pledge-hours"
value="<?php echo esc_attr( filter_input( INPUT_POST, 'pledge-hours' ) ); ?>"
required
/>
</label>
</div>
<div>
<?php
printf(
__( 'The company pledges that it meets the <a href="%s">expectations</a> of the Five For The Future program and that it will dedicate this amount of employee time per week to the WordPress project', 'wporg' ),
esc_url( '#' )
);
?>
<label>
<input <input
type="checkbox" type="checkbox"
id="5ftf-pledge-agreement" id="5ftf-pledge-agreement"
name="pledge-agreement" name="pledge-agreement"
<?php checked( filter_input( INPUT_POST, 'pledge-agreement', FILTER_VALIDATE_BOOLEAN ) ) ?>
required required
/> />
<?php _e( 'Yes', 'wporg' ); ?> <?php _e( 'I agree', 'wporg' ); ?>
</label> </label>
</div> </div>
</fieldset>
<div> <div>
<input type="submit" id="5ftf-pledge-submit" name="action" class="button button-primary" value="<?php esc_attr_e( 'Submit', 'wporg' ); ?>" /> <input type="submit" id="5ftf-pledge-submit" name="action" class="button button-primary" value="<?php esc_attr_e( 'Submit Pledge', 'wporg' ); ?>" />
</div> </div>
</form> </form>
<?php endif; ?>