Skip to content

Commit

Permalink
Volunteers: Automatically draft post when application submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
iandunn committed Jun 15, 2023
1 parent 3d83f0c commit f5c385e
Showing 1 changed file with 44 additions and 0 deletions.
Expand Up @@ -28,6 +28,7 @@ public function __construct() {
add_action( 'template_redirect', array( $this, 'populate_form_based_on_user' ), 9 );
add_action( 'grunion_pre_message_sent', array( $this, 'call_for_sponsors' ), 10, 3 );
add_action( 'grunion_pre_message_sent', array( $this, 'call_for_speakers' ), 10, 3 );
add_action( 'grunion_pre_message_sent', array( $this, 'call_for_volunteers' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -371,6 +372,49 @@ public function call_for_speakers( $submission_id, $all_values, $extra_values )
}
}

/**
* Create draft Volunteer post from a Call for Volunteers form submission.
*
* @todo If creating volunteer fails, report to organizer so that submission doesn't get missed
*
* @param int $submission_id
* @param array $all_values
* @param array $extra_values
*/
public function call_for_volunteers( $submission_id, $all_values, $extra_values ) {
if ( 'call-for-volunteers' != $this->get_form_key( $submission_id ) ) {
return;
}

global $current_user;

$all_values = $this->get_unprefixed_grunion_form_values( $all_values );
$volunteer_user_id = $this->get_user_id_from_username( $all_values['WordPress.org Username'] ?? '' );

if ( ! $volunteer_user_id ) {
$volunteer_user_id = $current_user->ID;
$all_values['WordPress.org Username'] = $current_user->user_login;
}

$volunteer_user = get_user_by( 'id', $volunteer_user_id );

$draft_id = wp_insert_post( array(
'post_type' => 'wcb_volunteer',
'post_title' => sanitize_text_field( $all_values['Name'] ),
'post_status' => 'draft',
'post_author' => $this->get_user_id_from_username( 'wordcamp' ),
) );

if ( $draft_id ) {
$first_time = strtolower( $all_values['Is this the first time you have volunteered at a WordPress event?'] ) ?? '';
$first_time = in_array( $first_time, array( 'yes', 'no', 'unsure' ), true ) ? $first_time : '';

update_post_meta( $draft_id, '_wcb_volunteer_email', is_email( $all_values['Email'] ) ?? '' );
update_post_meta( $draft_id, '_wcpt_user_name', $volunteer_user->user_login ?? '' );
update_post_meta( $draft_id, '_wcb_volunteer_first_time', $first_time );
}
}

/**
* Get speaker post based on WordPress.org user name
*
Expand Down

0 comments on commit f5c385e

Please sign in to comment.