';
}
$the_content = str_replace($template_tag, $pitch_form, $the_content);
return $the_content;
}
function save_pitch_form() {
global $assignment_desk, $current_user;
$message = array();
$options = $assignment_desk->general_options;
$user_types = $assignment_desk->custom_taxonomies->get_user_types();
if ($assignment_desk->edit_flow_exists()) {
global $edit_flow;
}
// @todo Sanitize all of the fields
// @todo Validate all of the fields
if ($_POST['assignment_desk_pitch_submit']) {
$form_messages = array();
// Ensure that it was the user who submitted the form, not a bot
if ( !wp_verify_nonce($_POST['assignment_desk_pitch_nonce'], 'assignment_desk_pitch') ) {
return $form_messages['error']['nonce'];
}
$sanitized_title = $_POST['assignment_desk_title'];
if ( !$sanitized_title ) {
$form_messages['errors']['title'] = 'Please add a title to this pitch.';
}
$sanitized_author = (int)$_POST['assignment_desk_author'];
$sanitized_description = wp_kses($_POST['assignment_desk_description'], $allowedposttags);
$sanitized_tags = $_POST['assignment_desk_tags'];
$sanitized_categories = (int)$_POST['assignment_desk_categories'];
$sanitized_location = wp_kses($_POST['assignment_desk_location'], $allowedposttags);
$sanitized_volunteer = $_POST['assignment_desk_volunteer'];
// Sanitize the duedate
$sanitized_duedate = '';
$duedate_split = split('/', $_POST['assignment_desk_duedate']);
if(!count($duedate_split) == 3){
$form_messages['errors']['duedate'] = 'Please enter a valid date of the form MM/DD/YYYY';
}
else {
$duedate_month = (int)$duedate_split[0];
$duedate_day = (int)$duedate_split[1];
$duedate_year = (int)$duedate_split[2];
// Zero pad for strtime
if($duedate_month < 10 ){
$duedate_month = "0$duedate_month";
}
$sanitized_duedate = strtotime($duedate_day . '-' . $duedate_month . '-' . $duedate_year);
if(!$sanitized_duedate){
$form_messages['errors']['duedate'] = 'Please enter a valid date of the form MM/DD/YYYY';
}
}
if ( count($form_messages['errors']) ) {
return $form_messages;
}
$new_pitch = array();
$new_pitch['post_title'] = $sanitized_title;
$new_pitch['post_author'] = $sanitized_author;
$new_pitch['post_content'] = '';
if ( $assignment_desk->edit_flow_exists() ) {
$default_status = get_term_by('term_id', $options['default_workflow_status'], 'post_status');
$new_pitch['post_status'] = $default_status->slug;
} else {
$new_pitch['post_status'] = 'draft';
}
$new_pitch['post_category'] = array($sanitized_categories);
$new_pitch['tags_input'] = $sanitized_tags;
$post_id = wp_insert_post($new_pitch);
// Once the pitch is saved, we can save data to custom fields
if ( $post_id ) {
// Save description to Edit Flow metadata field
update_post_meta($post_id, '_ef_description', $sanitized_description);
if($sanitized_duedate){
// Save duedate to Edit Flow metadata field
update_post_meta($post_id, '_ef_duedate', $sanitized_duedate);
}
// Save location to Edit Flow metadata field
update_post_meta($post_id, '_ef_location', $sanitized_location);
// Save pitched_by_participant and pitched_by_date information
update_post_meta($post_id, '_ad_pitched_by_participant', $sanitized_author);
update_post_meta($post_id, '_ad_pitched_by_timestamp', date_i18n('U'));
// Set assignment status to default setting
$default_status = $assignment_desk->custom_taxonomies->get_default_assignment_status();
wp_set_object_terms($post_id, (int)$default_status->term_id, $assignment_desk->custom_taxonomies->assignment_status_label);
// A new assignment gets all User Types by default
foreach ($user_types as $user_type) {
update_post_meta($post_id, "_ad_participant_type_$user_type->term_id", 'on');
}
if($sanitized_volunteer){
// Save the roles user volunteered for both with each role
// and under the user's row
$all_roles = array();
foreach ($sanitized_volunteer as $volunteered_role) {
$volunteered_role = (int)$volunteered_role;
$all_roles[] = $volunteered_role;
$role_data = array();
$role_data[$sanitized_author] = 'volunteered';
update_post_meta($post_id, "_ad_participant_role_$volunteered_role", $role_data);
}
update_post_meta($post_id, "_ad_participant_$sanitized_author", $sanitized_volunteer);
}
}
$form_messages['success']['post_id'] = $post_id;
return $form_messages;
}
return null;
}
/**
* Print a form giving the user the option to vote on an item
*/
function voting_form( $post_id = null ) {
global $assignment_desk, $current_user;
$options = $assignment_desk->public_facing_options;
if ( !$post_id ) {
global $post;
$post_id = $post->ID;
}
if ( is_user_logged_in() ) {
wp_get_current_user();
$all_votes = get_post_meta( $post_id, '_ad_votes_all', true );
$total_votes = (int)get_post_meta( $post_id, '_ad_votes_total', true );
if ( !is_array($all_votes) ){
$all_votes = array();
}
$user_id = $current_user->ID;
// If the user hasn't voted before, show the vote button
if ( !in_array( $user_id, $all_votes ) ) {
$voting_form = '';
return $voting_form;
} else if ( $_REQUEST['assignment_desk_messages']['voting']['success'] ) {
$voting_message = '
';
return $votes_html;
}
}
function save_voting_form( ) {
global $assignment_desk, $current_user;
if ( $_POST['assignment_desk_voting_submit'] && is_user_logged_in() ) {
$form_messages = array();
// Ensure that it was the user who submitted the form, not a bot
if ( !wp_verify_nonce($_POST['assignment_desk_voting_nonce'], 'assignment_desk_voting') ) {
return $form_messages['error']['nonce'];
}
wp_get_current_user();
$post_id = (int)$_POST['assignment_desk_voting_post_id'];
$sanitized_user_id = (int)$_POST['assignment_desk_voting_user_id'];
// Ensure the user saving is the same user who submitted the form
if ( $sanitized_user_id != $current_user->ID ) {
return false;
}
$all_votes = get_post_meta( $post_id, '_ad_votes_all', true );
$total_votes = (int)get_post_meta( $post_id, '_ad_votes_total', true );
if(!is_array($all_votes)){
$all_votes = array();
}
if ( !in_array( $user_id, $all_votes ) ) {
$all_votes[] = $sanitized_user_id;
update_post_meta( $post_id, '_ad_votes_all', $all_votes );
update_post_meta( $post_id, '_ad_votes_total', count($all_votes) );
$form_messages['success']['message'] = 'Thanks for your vote!';
} else {
$form_messages['error']['message'] = 'Whoops, you already voted.';
}
return $form_messages;
}
}
/**
* Print a form with available roles and ability to volunteer
*/
function volunteer_form( $post_id = null ) {
global $assignment_desk, $current_user;
if ( !$post_id ) {
global $post;
$post_id = $post->ID;
}
if ( is_user_logged_in() ) {
wp_get_current_user();
$user_roles = $assignment_desk->custom_taxonomies->get_user_roles();
// See whether the user has already volunteered for the story
$existing_roles = get_post_meta( $post_id, "_ad_participant_$current_user->ID", true );
if ( !$existing_roles ) {
$existing_roles = array();
}
$volunteer_form = '';
$volunteer_form .= '