pitch_form_options; $public_facing_options = $assignment_desk->public_facing_options; wp_enqueue_script('jquery-datepicker-js', ASSIGNMENT_DESK_URL .'js/jquery.datepicker.js', array('jquery-ui-core')); wp_enqueue_script('ad-public-views', ASSIGNMENT_DESK_URL . 'js/public_views.js', array('jquery', 'jquery-datepicker-js')); wp_enqueue_style('ad-publix', ASSIGNMENT_DESK_URL . 'css/public.css'); // Run save_pitch_form() at WordPress initialization $_REQUEST['assignment_desk_messages']['pitch_form'] = $this->save_pitch_form(); $_REQUEST['assignment_desk_messages']['volunteer_form'] = $this->save_volunteer_form(); $_REQUEST['assignment_desk_messages']['voting'] = $this->save_voting_form(); add_filter( 'the_content', array(&$this, 'show_all_posts') ); // Only add voting if its enabled if ( $public_facing_options['public_facing_voting_enabled'] ) { add_filter( 'the_content', array(&$this, 'prepend_voting_to_post') ); } // Only add volunteering if its enabled if ( $public_facing_options['public_facing_volunteering_enabled'] ) { add_filter( 'the_content', array(&$this, 'append_volunteering_to_post') ); } // Only show pitch forms if the functionality is enabled if ( $pitch_form_options['pitch_form_enabled'] ) { add_filter( 'the_content', array(&$this, 'show_pitch_form') ); } } function show_pitch_form( $the_content ) { global $assignment_desk, $current_user; $options = $assignment_desk->pitch_form_options; if ($assignment_desk->edit_flow_exists()) { global $edit_flow; } wp_get_current_user(); $user_roles = $assignment_desk->custom_taxonomies->get_user_roles(); $category_args = array( 'type' => 'post', 'child_of' => 0, 'orderby' => 'id', 'order' => 'ASC', 'hide_empty'=> 0, 'hierarchical'=> True ); $categories = get_categories($category_args); $template_tag = ''; if ( is_user_logged_in() ) { $pitch_form = ''; if ( $_REQUEST['assignment_desk_messages']['pitch_form']['success'] ) { $pitch_form .= '

Pitch submitted successfully.

'; } else if ( count($_REQUEST['assignment_desk_messages']['pitch_form']['errors']) ) { $pitch_form .= '

Please correct the error(s) below.

'; } $pitch_form .= '
'; // Title if ( $options['pitch_form_title_label'] ) { $title_label = $options['pitch_form_title_label']; } else { $title_label = 'Title'; } $pitch_form .= '
' . ''; if ( $options['pitch_form_title_description'] ) { $pitch_form .= '

' . $options['pitch_form_title_description'] . '

'; } if ( $_REQUEST['assignment_desk_messages']['pitch_form']['errors']['title'] ) { $pitch_form .= '

' . $_REQUEST['assignment_desk_messages']['pitch_form']['errors']['title'] . '

'; } $pitch_form .= '
'; // Description if ( $options['pitch_form_description_enabled'] ) { if ( $options['pitch_form_description_label'] ) { $description_label = $options['pitch_form_description_label']; } else { $description_label = 'Description'; } $pitch_form .= '
' . ''; if ( $options['pitch_form_description_description'] ) { $pitch_form .= '

' . $options['pitch_form_description_description'] . '

'; } $pitch_form .= '
'; } // Due Date if ( $options['pitch_form_duedate_enabled'] ) { if ( $options['pitch_form_dudedate_label'] ) { $duedate_label = $options['pitch_form_dudedate_label']; } else { $duedate_label = 'Due Date'; } $pitch_form .= '
'; $pitch_form .= ''; if ( $options['pitch_form_dudedate_description'] ) { $pitch_form .= '

' . $options['pitch_form_dudedate_description'] . '

'; } if ( $_REQUEST['assignment_desk_messages']['pitch_form']['errors']['duedate'] ) { $pitch_form .= '

' . $_REQUEST['assignment_desk_messages']['pitch_form']['errors']['duedate'] . '

'; } $pitch_form .= '
'; } // Categories if ( $options['pitch_form_categories_enabled'] ) { if ( $options['pitch_form_category_label'] ) { $category_label = $options['pitch_form_category_label']; } else { $category_label = 'Category'; } $pitch_form .= '
'; $pitch_form .= ''; if ($options['pitch_form_categories_description']) { $pitch_form .= '

' . $options['pitch_form_categories_description'] . '

'; } $pitch_form .= '
'; } // Tags if ( $options['pitch_form_tags_enabled'] ) { if ( $options['pitch_form_tags_label'] ) { $tags_label = $options['pitch_form_tags_label']; } else { $tags_label = 'Tags'; } $pitch_form .= '
' . ''; if ($options['pitch_form_tags_description']) { $pitch_form .= '

' . $options['pitch_form_tags_description'] . '

'; } $pitch_form .= '
'; } // Location if ( $options['pitch_form_location_enabled'] ) { if ( $options['pitch_form_location_label'] ) { $location_label = $options['pitch_form_location_label']; } else { $location_label = 'Location'; } $pitch_form .= '
' . ''; if ( $options['pitch_form_location_description'] ) { $pitch_form .= '

' . $options['pitch_form_location_description'] . '

'; } $pitch_form .= '
'; } // Volunteer if ( $options['pitch_form_volunteer_enabled'] ) { if ( $options['pitch_form_volunteer_label'] ) { $volunteer_label = $options['pitch_form_volunteer_label']; } else { $volunteer_label = 'Volunteer'; } $pitch_form .= '
'; if ( $options['pitch_form_volunteer_description'] ) { $pitch_form .= '

' . $options['pitch_form_volunteer_description'] . '

'; } $pitch_form .= '
'; } // @todo Confirm your user information // Author information and submit $pitch_form .= '
' . ''; $pitch_form .= ''; $pitch_form .= '
'; $pitch_form .= '
'; } else { $pitch_form = '
Oops, you have to be logged in to submit a pitch.
'; } $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 ); $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 = '
' . '' . ''; if ( $options['public_facing_voting_button'] ) { $voting_button = $options['public_facing_voting_button']; } else { $voting_button = 'Vote'; } $voting_form .= ''; $voting_form .= '' . '
'; return $voting_form; } else if ( $_REQUEST['assignment_desk_messages']['voting']['success'] ) { $voting_message = '
' . $_REQUEST['assignment_desk_messages']['voting']['success']['message'] . '
'; return $voting_message; } } } function show_all_votes( $post_id = null ) { global $assignment_desk, $current_user; if ( !$post_id ) { global $post; $post_id = $post->ID; } $all_votes = get_post_meta( $post_id, '_ad_votes_all', true ); $total_votes = (int)get_post_meta( $post_id, '_ad_votes_total', true ); if (!$total_votes) { $votes_html = '
No votes yet, you could be the first!
'; return $votes_html; } else { $votes_html = '
' . $total_votes . ''; foreach ( $all_votes as $user_id ) { $votes_html .= get_avatar( $user_id, 40 ); } $votes_html .= '
'; 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 ( !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 .= '
'; $volunteer_form .= '
'; $volunteer_form .= ""; $volunteer_form .= ""; $volunteer_form .= ''; $volunteer_form .= '
custom_taxonomies->get_user_roles(); if ( !$post_id ) { global $post; $post_id = $post->ID; } $show_all_volunteers = '
'; foreach ( $user_roles as $user_role ) { $show_all_volunteers .= '' . $user_role->name . 's: '; $volunteers_for_role = array(); $volunteers_for_role = get_post_meta( $post_id, "_ad_participant_role_$user_role->term_id" ); $show_all_volunteers .= count($volunteers_for_role[0]) . ', '; } $show_all_volunteers = rtrim( $show_all_volunteers, ', ' ); $show_all_volunteers .= '
'; return $show_all_volunteers; } /** * Sanitize the user volunteer information and add them as a volunteer. */ function save_volunteer_form() { global $assignment_desk, $current_user; if ( $_POST['assignment_desk_volunteer_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_volunteering_nonce'], 'assignment_desk_volunteering') ) { return $form_messages['error']['nonce']; } wp_get_current_user(); $post_id = (int)$_POST['assignment_desk_volunteer_post_id']; $sanitized_roles = $_POST['assignment_desk_volunteer_roles']; $sanitized_user_id = (int)$_POST['assignment_desk_volunteer_user_id']; // Ensure the user saving is the same user who submitted the form if ( $sanitized_user_id != $current_user->ID ) { return false; } // Filter the roles to make sure they're valid. $user_roles = $assignment_desk->custom_taxonomies->get_user_roles(); // @todo abstract this to class method $valid_roles = array(); foreach($sanitized_roles as $maybe_role){ $maybe_role = (int)$maybe_role; foreach ( $user_roles as $role ){ if ( $maybe_role == $role->term_id ) { $valid_roles[] = $maybe_role; } } } // Get previous roles, foreach ( $user_roles as $user_role ) { $previous_values = get_post_meta($post_id, '_ad_participant_role_' . $user_role->term_id, true); if ( in_array( $user_role->term_id, $valid_roles ) && !isset( $previous_values[$current_user->ID] ) ) { $previous_values[$current_user->ID] = 'volunteered'; update_usermeta($user_id, '_ad_volunteer', $post_id); } else if ( !in_array( $user_role->term_id, $valid_roles ) && $previous_values[$current_user->ID] == 'volunteered' ) { unset($previous_values[$current_user->ID]); } $new_values = $previous_values; update_post_meta($post_id, '_ad_participant_role_' . $user_role->term_id, $new_values); } // Save the roles associated with the user id as well update_post_meta( $post_id, "_ad_participant_$sanitized_user_id", $valid_roles ); } } /* * Replace an html comment with ad public pages. */ function show_all_posts( $the_content ) { global $wpdb, $assignment_desk, $post; $options = $assignment_desk->public_facing_options; $template_tag = ''; if ( !strpos( $the_content, $template_tag ) ) { return $the_content; } // Save the parent post so we can reset the object later $parent_post = $post; $html = ''; $all_pitches = get_public_feedback_posts(); foreach ( $all_pitches as $pitch ) { $post_id = $pitch->ID; $description = get_post_meta( $post_id, '_ef_description', true ); $location = get_post_meta( $post_id, '_ef_location', true ); $duedate = get_post_meta( $post_id, '_ef_duedate', true ); $duedate = date_i18n( 'M d, Y', $duedate ); $html .= '

' . get_the_title($post_id) . '

'; // Only show voting if it's enabled if ( $options['public_facing_voting_enabled'] ) { $html .= $this->show_all_votes(); $html .= $this->voting_form(); } if ( $description || $duedate || $location ) { $html .= '

'; } if ( $options['public_facing_description_enabled'] && $description ) { $html .= ' ' . $description . '
'; } if ( $options['public_facing_duedate_enabled'] && $duedate ) { $html .= ' ' . $duedate . '
'; } if ( $options['public_facing_location_enabled'] && $location ) { $html .= ' ' . $location . '
'; } if ( $description || $duedate || $location ) { $html .= '

'; } if ( $options['public_facing_volunteering_enabled'] ) { $html .= $this->show_all_volunteers( $post_id ); $html .= $this->volunteer_form( $post_id ); } $html .= "
"; } $the_content = str_replace($template_tag, $html, $the_content); // Reset the $post object to its original state $post = $parent_post; return $the_content; } /** * Prepend voting functionality to the beginning of a post's content */ function prepend_voting_to_post( $the_content ) { global $post, $assignment_desk; if ( is_single() ) { $the_content = $this->voting_form() . $the_content; $the_content = $this->show_all_votes() . $the_content; } return $the_content; } /** * Appending volunteering functionality to the ending of a post's content */ function append_volunteering_to_post( $the_content ) { global $post, $assignment_desk; if ( is_single() ) { $the_content .= $this->show_all_volunteers( $post->ID ); $the_content .= $this->volunteer_form( $post->ID ); } return $the_content; } } // END:class ad_public_controller } // END:if(!class_exists('ad_public_controller')) ?>