pitch_form_options; $public_facing_options = $assignment_desk->public_facing_options; // The datepicker UI is used on the pitch submission form. Only load if enabled if ( $pitch_form_options['pitch_form_enabled'] ) { 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-public', ASSIGNMENT_DESK_URL . 'css/public.css'); add_filter( 'the_content', array( &$this, 'show_all_posts' ) ); add_filter( 'the_posts', array( &$this, 'show_single_post' ) ); add_filter( 'the_content', array( &$this, 'handle_single_post_metadata' ) ); add_action( 'parse_request', array( &$this, 'process_form_submissions' ) ); // 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 commenting if its enabled add_filter( 'comments_open', array(&$this, 'enable_disable_commenting') ); if ( $public_facing_options['public_facing_commenting_enabled'] ) { add_action( 'comment_on_draft', array(&$this, 'handle_comment_post'), 1 ); } // Only add volunteering if its enabled if ( $public_facing_options['public_facing_volunteering_enabled'] ) { add_filter( 'the_content', array(&$this, 'append_actions_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') ); } } /** * Initialize first use of the plugin with default settings * @todo Finish this method */ function activate_once() { } /** * Process any form saves */ function process_form_submissions() { global $assignment_desk; $pitch_form_options = $assignment_desk->pitch_form_options; $public_facing_options = $assignment_desk->public_facing_options; // Only process voting if its enabled if ( $public_facing_options['public_facing_voting_enabled'] ) { $this->save_voting_form(); } // Only process volunteering if its enabled if ( $public_facing_options['public_facing_volunteering_enabled'] ) { $_REQUEST['assignment_desk_messages']['volunteer_form'] = $this->save_volunteer_form(); } // Only process pitch forms if the functionality is enabled if ( $pitch_form_options['pitch_form_enabled'] ) { $_REQUEST['assignment_desk_messages']['pitch_form'] = $this->save_pitch_form(); } } /** * Helper function which returns a value if the variable is set */ function return_if_set( $var = null ) { if ( isset($var) ) { return $var; } else { return null; } } /** * Show the pitch form on post or pages with template tag if enabled */ function show_pitch_form( $the_content ) { global $assignment_desk; $options = $assignment_desk->pitch_form_options; if ( $assignment_desk->edit_flow_exists() ) { global $edit_flow; } $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 = ''; $pitch_form = ''; // Messages to the User appear at the top of the form if ( isset($_REQUEST['assignment_desk_messages']['pitch_form']['success']) ) { if ( $options['pitch_form_success_message'] ) { $search = array( '%title%', '%duedate%', '%description%', '%post_link%', '%location%', ); $replace = array( $_REQUEST['assignment_desk_title'], $_REQUEST['assignment_desk_duedate'], $_REQUEST['assignment_desk_description'], get_permalink($_REQUEST['assignment_desk_messages']['pitch_form']['success']['post_id']), $_REQUEST['assignment_desk_location'], ); $success_message = str_replace($search, $replace, $options['pitch_form_success_message']); } else { $success_message = _('Pitch submitted successfully. Thanks!'); } $pitch_form .= '
' . $success_message . '
'; } else if ( count($_REQUEST['assignment_desk_messages']['pitch_form']['errors']) ) { $pitch_form .= '
Please correct the error(s) below.
'; } if ( isset($_REQUEST['assignment_desk_messages']['pitch_form']['errors']['secret']) ) { $pitch_form .= '

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

'; } /** * For all of the fields, the admin has the ability to define a label and a description * in the settings. If those aren't defined, then the stock label will show with no description */ $pitch_form .= '
'; // Title if ( $options['pitch_form_title_label'] ) { $title_label = $options['pitch_form_title_label']; } else { $title_label = 'Title'; } $pitch_form .= '
' . 'return_if_set($_POST['assignment_desk_title']) . '"/>'; if ( $options['pitch_form_title_description'] ) { $pitch_form .= '

' . $options['pitch_form_title_description'] . '

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

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

'; } $pitch_form .= '
'; if ( $assignment_desk->edit_flow_exists() ) { // Edit Flow v0.6 and higher offers custom editorial metadata. Otherwise, fall back on old if ( version_compare( EDIT_FLOW_VERSION, '0.6', '>=' ) ) { // Build pitch form with custom editorial metadata $editorial_metadata = $edit_flow->editorial_metadata->get_editorial_metadata_terms(); foreach ( $editorial_metadata as $term ) { $form_key = $edit_flow->editorial_metadata->get_postmeta_key( $term ); $enabled_key = 'pitch_form_' . $term->slug . '_enabled'; $label_key = 'pitch_form_' . $term->slug . '_label'; $description_key = 'pitch_form_' . $term->slug . '_description'; $required_key = 'pitch_form_' . $term->slug . '_required'; // Only show the field if it's enabled if ( $options[$enabled_key] ) { // Build the label and description field $html_label = ( $options[$label_key] ) ? $options[$label_key] : $term->name; $html_description = ( $options[$description_key] ) ? $options[$description_key] : ''; $html_input = ''; // Give us different inputs based on the metadata type switch ( $term_type = $edit_flow->editorial_metadata->get_metadata_type( $term ) ) { case 'checkbox': $html_input = 'return_if_set( $_POST[$form_key] ) ) $html_input = ' checked="checked"'; $html_input .= ' />'; break; case 'date': $html_input = 'return_if_set( $_POST[$form_key] ) . '" '; $html_input .= ' class="ad_datepicker" size="12" />'; break; case 'location': $html_input = 'return_if_set( $_POST[$form_key] ) . '"/>'; break; case 'paragraph': $html_input = ''; break; case 'text': $html_input = 'return_if_set( $_POST[$form_key] ) . '"/>'; break; case 'user': $selected = ( $this->return_if_set( $_POST[$form_key] ) ) ? $this->return_if_set( $_POST[$form_key] ) : false; $user_dropdown_args = array( 'show_option_all' => __( '- Select user -', 'assignment-desk' ), 'name' => $form_key, 'selected' => $selected, 'echo' => 0, ); $html_input = wp_dropdown_users( $user_dropdown_args ); break; default: $html_input = ''; break; } if ( $html_input ) { $pitch_form .= '
'; $pitch_form .= $html_input; if ( $html_description ) { $pitch_form .= '

' . $html_description . '

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

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

'; } $pitch_form .= '
'; } } } } else { // Description if ( isset( $options['pitch_form_description_enabled'] ) && $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'] . '

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

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

'; } $pitch_form .= '
'; } // Due date if ( $options['pitch_form_duedate_enabled'] ) { if ( $options['pitch_form_duedate_label'] ) { $duedate_label = $options['pitch_form_duedate_label']; } else { $duedate_label = 'Due Date'; } $pitch_form .= '
'; $pitch_form .= 'return_if_set($_POST['assignment_desk_duedate']) . '" class="ad_datepicker"/>'; if ( $options['pitch_form_duedate_description'] ) { $pitch_form .= '

' . $options['pitch_form_dudedate_description'] . '

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

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

'; } $pitch_form .= '
'; } // Location if ( isset( $options['pitch_form_location_enabled'] ) && $options['pitch_form_location_enabled'] ) { if ( $options['pitch_form_location_label'] ) { $location_label = $options['pitch_form_location_label']; } else { $location_label = 'Location'; } $pitch_form .= '
' . 'return_if_set($_POST['assignment_desk_location']) . '"/>'; if ( $options['pitch_form_location_description'] ) { $pitch_form .= '

' . $options['pitch_form_location_description'] . '

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

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

'; } $pitch_form .= '
'; } } // END - Check if Edit Flow > v0.6 } // END - if ( $assignment_desk->edit_flow_exists() ) // Categories if ( $options['pitch_form_categories_enabled'] ) { if ( $options['pitch_form_categories_label'] ) { $category_label = $options['pitch_form_categories_label']; } else { $category_label = 'Category'; } $pitch_form .= '
'; $pitch_form .= ''; if ($options['pitch_form_categories_description']) { $pitch_form .= '

' . $options['pitch_form_categories_description'] . '

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

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

'; } $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 .= '
' . 'return_if_set($_POST['assignment_desk_tags']) . '"/>'; if ( $options['pitch_form_tags_description'] ) { $pitch_form .= '

' . $options['pitch_form_tags_description'] . '

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

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

'; } $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'] . '

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

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

'; } $pitch_form .= '
'; } // Allow an alternate form of authentication when the pitch form is loaded do_action( 'ad_alternate_authentication', 'pitch_form_load' ); if ( is_user_logged_in() ) { global $current_user; wp_get_current_user(); // Current user information $pitch_form .= '
' . ' ' . $current_user->display_name . ' <' . $current_user->user_email . '>' . '
'; } else { $pitch_form .= '
' . ' ' . '' . '
' . ''; // Show a registration link if users can register if ( get_option('users_can_register') ) { $pitch_form .= '

If you need a username, you can ' . _('register a new account') . ''; } if ( isset($_REQUEST['assignment_desk_messages']['pitch_form']['errors']['login']) ) { $pitch_form .= '

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

'; } $pitch_form .= '
'; } $pitch_form .= '
'; // Set a random one-time token in the form to prevent duplicate submissions. $_SESSION['assignment_desk_pitch_form_secret'] = md5(uniqid(rand(), true)); $pitch_form .= ""; $pitch_form .= ''; $pitch_form .= '
'; $pitch_form .= '
'; $the_content = str_replace($template_tag, $pitch_form, $the_content); return $the_content; } /** * Saves data after a User submits a pitch form */ function save_pitch_form() { global $assignment_desk; $message = array(); $options = $assignment_desk->general_options; $form_options = $assignment_desk->pitch_form_options; $user_types = $assignment_desk->custom_taxonomies->get_user_types(); if ( $assignment_desk->edit_flow_exists() ) { global $edit_flow; } session_start(); if ( $_POST && isset($_POST['assignment_desk_pitch_submit']) ) { $form_messages = array(); // Check to see whether this is the second time the form has been submitted $form_secret = $_POST['assignment_desk_pitch_form_secret']; if ( !isset( $_SESSION['assignment_desk_pitch_form_secret'] ) || strcasecmp($form_secret, $_SESSION['assignment_desk_pitch_form_secret']) != 0 ) { $form_messages['errors']['secret'] = __('Form invalidates when you refresh your browser. Please start over.'); } // 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') ) { $form_messages['error']['nonce'] = 'Are you a bot?'; } $sanitized_title = strip_tags($_POST['assignment_desk_title']); if ( !$sanitized_title ) { $form_messages['errors']['title'] = 'Please add a title to this pitch.'; } // Allow an alternate form of authentication when the pitch form is saved do_action( 'ad_alternate_authentication', 'pitch_form_save' ); if ( is_user_logged_in() ) { global $current_user; $sanitized_author = $current_user->ID; } else { require_once(ABSPATH . WPINC . '/registration.php'); $credentials['user_login'] = $_POST['assignment_desk_username']; $credentials['user_password'] = $_POST['assignment_desk_password']; $credentials['remember'] = true; $user = wp_signon($credentials); if ( is_wp_error($user) ) { $form_messages['errors']['login'] = $user->get_error_message(); } else { wp_set_current_user($user->ID); $sanitized_author = $user->ID; } } if ( $assignment_desk->edit_flow_exists() ) { // Edit Flow v0.6 and higher offers custom editorial metadata. Otherwise, fall back on old if ( version_compare( EDIT_FLOW_VERSION, '0.6', '>=' ) ) { $terms = $edit_flow->editorial_metadata->get_editorial_metadata_terms(); $all_editorial_metadata = array(); foreach ( $terms as $term ) { // Setup the key for this editorial metadata term (same as what's in $_POST) $form_key = $edit_flow->editorial_metadata->get_postmeta_key( $term ); $required_key = 'pitch_form_' . $term->slug . '_required'; $editorial_metadata = isset( $_POST[$form_key] ) ? $_POST[$form_key] : ''; $type = $edit_flow->editorial_metadata->get_metadata_type( $term ); // Process date formats if ( $type == 'date' ) { $duedate_split = split( '/', $editorial_metadata ); if ( count( $duedate_split ) == 3) { $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"; } $editorial_metadata = strtotime($duedate_day . '-' . $duedate_month . '-' . $duedate_year); if ( !$editorial_metadata ) { $form_messages['errors'][$form_key] = _('Please enter a valid date of the form MM/DD/YYYY'); continue; } } } $editorial_metadata = strip_tags( $editorial_metadata ); // Ensure there's a value if the field is required if ( !$editorial_metadata && $form_options[$required_key] == 'on' ) { $form_messages['errors'][$form_key] = _( $term->name . ' is required.' ); } else { $all_editorial_metadata[$form_key] = $editorial_metadata; } } } else { // Description $sanitized_description = ''; if ( $_POST['assignment_desk_description']) { $sanitized_description = wp_kses($_POST['assignment_desk_description'], $allowedposttags); } else { if ( $form_options['pitch_form_description_required'] == 'on' ) { $form_messages['errors']['description'] = _('Description is required.'); } } // Location $sanitized_location = ''; if ( $_POST['assignment_desk_location'] ) { $sanitized_location = wp_kses($_POST['assignment_desk_location'], $allowedposttags); } else { if ( $form_options['pitch_form_location_required'] == 'on' ) { $form_messages['errors']['location'] = _('Location is required.'); } } // Due date if ( $_POST['assignment_desk_duedate'] ) { // Sanitize the duedate $sanitized_duedate = false; $duedate_split = split('/', $_POST['assignment_desk_duedate']); if ( count($duedate_split) == 3) { $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'); } } else { $form_messages['errors']['duedate'] = _('Please enter a valid date of the form MM/DD/YYYY'); } } else { if ( $form_options['pitch_form_duedate_required'] ) { $form_messages['errors']['duedate'] = _('Due date is required.'); } } } } $sanitized_tags = ''; if ( $_POST['assignment_desk_tags'] ){ $sanitized_tags = $_POST['assignment_desk_tags']; } else { if ( $form_options['pitch_form_tags_required'] ) { $form_messages['errors']['tags'] = _('Tags are required.'); } } $sanitized_categories = ''; if ( $_POST['assignment_desk_categories'] ){ $sanitized_categories = (int)$_POST['assignment_desk_categories']; } else { if ( $form_options['pitch_form_categories_required'] ) { $form_messages['errors']['categories'] = _('Category is required.'); } } $sanitized_volunteer = false; if ( $_POST['assignment_desk_volunteer'] ){ $sanitized_volunteer = $_POST['assignment_desk_volunteer']; if (! is_array($sanitized_volunteer) ) { $sanitized_volunteer = array((int)$sanitized_volunteer); } } else { if ( $form_options['pitch_form_volunteer_required'] ) { $form_messages['errors']['volunteer'] = _('Volunteering is required.'); } } // Don't process the form if any errors have been set 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 ) { // Only handle editorial metadata if Edit Flow exists if ( $assignment_desk->edit_flow_exists() ) { // Edit Flow v0.6 and higher offers custom editorial metadata. Otherwise, fall back on old if ( version_compare( EDIT_FLOW_VERSION, '0.6', '>=' ) ) { foreach ( $all_editorial_metadata as $key => $value ) { update_post_meta( $post_id, $key, $value ); } } else { // Old way of saving post meta update_post_meta( $post_id, '_ef_description', $sanitized_description ); update_post_meta( $post_id, '_ef_duedate', $sanitized_duedate ); 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 ); // All User Types can participate in a new assignment by default foreach ( $user_types as $user_type ) { update_post_meta($post_id, "_ad_participant_type_$user_type->term_id", 'on'); } // Record any roles a User has volunteered for 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); } $this->send_new_pitch_emails($post_id); } $form_messages['success']['post_id'] = $post_id; unset($_POST); return $form_messages; } return null; } /** * Send an email to users accorindg to the pitch_form_notification_emails setting. * @param $post_id int The ID of the new post. */ function send_new_pitch_emails( $post_id ) { global $assignment_desk, $wpdb; $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$post_id"); $submitter = get_userdata((int)get_post_meta($post_id, '_ad_pitched_by_participant', true)); $search = array('%blogname%', '%title%', '%excerpt%', '%description%', '%duedate%', '%location%', '%post_link%', '%dashboard_link%', '%submitter_email%', '%submitter_display_name%', ); $replace = array( get_option('blogname'), $post->post_title, $post->post_excerpt, get_post_meta($post_id, '_ef_description', true), ad_format_ef_duedate((int)get_post_meta($post_id, '_ef_duedate', true)), ad_format_ef_duedate((int)get_post_meta($post_id, '_ef_location', true)), get_permalink($post_id), admin_url(), $submitter->user_email, $submitter->display_name, ); $email_addresses = str_replace('%submitter_email%', $submitter->user_email, $assignment_desk->pitch_form_options['pitch_form_notification_emails']); $email_addresses = explode(',', $email_addresses); $subject = str_replace($search, $replace, $assignment_desk->pitch_form_options['pitch_form_email_template_subject']); $email_template = str_replace($search, $replace, $assignment_desk->pitch_form_options['pitch_form_email_template']); if ( $email_addresses ) { foreach ( $email_addresses as $email_address ) { $email_address = str_replace(' ', '', $email_address); wp_mail($email_address, $subject, $email_template); } } } /** * Print a form giving the user the option to vote on an item * @param int $post_id The Post ID * @return string $voting_form The voting button in HTML */ function voting_button( $post_id = null ) { global $assignment_desk, $current_user; $options = $assignment_desk->public_facing_options; if ( !$post_id ) { global $post; $post_id = $post->ID; } // Allow alternate form of authentication when voting button is loaded do_action( 'ad_alternate_authentication', 'voting_load' ); wp_get_current_user(); $total_votes = (int)get_post_meta( $post_id, '_ad_votes_total', true ); $user_id = $current_user->ID; $voting_form = ''; // Save all of the data we need available in the DOM as hidden input fields $voting_form .= ''; $voting_form .= ''; $voting_form .= ''; // Button to display if the user is logged in and hasn't voted if ( !$this->check_if_user_has_voted( $post_id, $user_id ) && is_user_logged_in() ) { $voting_form .= ''; $voting_form .= ''; if ( $options['public_facing_voting_button'] ) { $voting_button = '' . $options['public_facing_voting_button'] . ''; } else { $voting_button = 'Vote'; } $voting_button .= ' (' . $total_votes . ')'; $voting_form .= $voting_button . ''; } else if ( $this->check_if_user_has_voted( $post_id, $user_id ) && is_user_logged_in() ) { $voting_form .= ''; $voting_form .= ''; $voting_button = 'Thanks! (' . $total_votes . ')'; $voting_form .= $voting_button . ''; } else { $voting_form .= ''; $voting_form .= ''; if ( $options['public_facing_voting_button'] ) { $voting_button = '' . $options['public_facing_voting_button'] . ''; } else { $voting_button = 'Vote'; } $voting_button .= ' (' . $total_votes . ')'; $voting_form .= $voting_button . ''; } $voting_form .= ''; return $voting_form; } /** * Check if the user has voted before * @param int $post_id The Post ID * @param int $user_id The User ID */ function check_if_user_has_voted( $post_id, $user_id ) { global $assignment_desk, $wpdb; $query = "SELECT * FROM $assignment_desk->votes_table_name WHERE post_id=$post_id AND user_id=$user_id;"; $vote = $wpdb->get_results( $query, ARRAY_N ); if ( count( $vote ) ) { return true; } else { return false; } } /** * Get all of the votes for a post * @param int $post_id The Post ID * @return array $all_votes All vote data in an array */ function get_all_votes_for_post( $post_id ) { global $assignment_desk, $wpdb; $query = "SELECT * FROM $assignment_desk->votes_table_name WHERE post_id=$post_id ORDER BY last_updated DESC;"; $all_votes = $wpdb->get_results( $query, ARRAY_N ); if ( isset( $all_votes ) ) { return $all_votes; } else { return array(); } } function update_user_vote_for_post( $post_id, $user_id, $action = 'add' ) { global $assignment_desk, $wpdb; if ( $action == 'add' ) { $query = "INSERT INTO $assignment_desk->votes_table_name (post_id, user_id) VALUES( '" . $wpdb->escape($post_id) . "', " . $wpdb->escape($user_id) . ");"; $result = $wpdb->query( $query ); } else if ( $action == 'remove' ) { $query = "DELETE FROM $assignment_desk->votes_table_name WHERE post_id=" . $wpdb->escape($post_id) . " AND user_id=" . $wpdb->escape($user_id) . ";"; $result = $wpdb->query( $query ); } } /** * Display the avatars for the users who have voted on the item. * @param int $post_id The Post ID * @return string the voting results in HTML. */ function show_all_voting_avatars( $post_id = null ) { global $assignment_desk, $current_user; $options = $assignment_desk->public_facing_options; if ( !$post_id ) { global $post; $post_id = $post->ID; } $all_votes = $this->get_all_votes_for_post( $post_id ); $total_votes = (int)get_post_meta( $post_id, '_ad_votes_total', true ); // Only show avatars if there are lots of votes if ( $total_votes ) { $votes_html = '
'; $i = 0; foreach ( $all_votes as $vote ) { if ( $i >= $options['public_facing_voting_avatars'] ) { break; } $votes_html .= get_avatar( $vote['user_id'], 40 ); $i++; } $votes_html .= '
'; return $votes_html; } } /** * Save the voting form when submitted by the User * @return array messages indicating results. */ function save_voting_form() { global $assignment_desk, $current_user; if ( isset($_GET['action']) && ( $_GET['action'] == 'assignment_desk_add_vote' || $_GET['action'] == 'assignment_desk_delete_vote') ) { // Ensure that it was the user who submitted the form, not a darn bot if ( !wp_verify_nonce( $_GET['nonce'], 'assignment_desk_voting' ) ) { $response_message = 'nonce_error'; } // Allow alternate form of authentication on voting save do_action( 'ad_alternate_authentication', 'voting_save' ); wp_get_current_user(); if ( !is_user_logged_in() ) { $response_message = 'auth_error'; } $post_id = (int)$_GET['post_id']; $sanitized_user_id = $current_user->ID; if ( $_GET['action'] == 'assignment_desk_add_vote' && $sanitized_user_id ) { if ( !$this->check_if_user_has_voted( $post_id, $sanitized_user_id ) ) { $this->update_user_vote_for_post( $post_id, $sanitized_user_id, 'add' ); $total_votes = $this->get_all_votes_for_post( $post_id ); update_post_meta( $post_id, '_ad_votes_total', count($total_votes) ); $response_message = 'added'; } else { $response_message = 'add_error'; } } else if ( $_GET['action'] == 'assignment_desk_delete_vote' && $sanitized_user_id ) { if ( $this->check_if_user_has_voted( $post_id, $sanitized_user_id ) ) { $this->update_user_vote_for_post( $post_id, $sanitized_user_id, 'remove' ); $total_votes = $this->get_all_votes_for_post( $post_id ); update_post_meta( $post_id, '_ad_votes_total', count($total_votes) ); $response_message = 'deleted'; } else { $response_message = 'delete_error'; } } // Give a plain message if its an AJAX request if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) { die( $response_message ); } else { return $response_message; } } } /** * Print a form with available roles and ability to volunteer * @param int $post_id The Post ID * @return string the volunteer form HTML * @todo Better message for logged-out users */ function volunteer_form( $post_id = null ) { global $assignment_desk, $current_user; $pitch_form_options = $assignment_desk->pitch_form_options; if ( !$post_id ) { global $post; $post_id = $post->ID; } // Allow an alternate form of authentication when the volunteer form is loaded do_action( 'ad_alternate_authentication', 'volunteer_form_load' ); // Only logged-in users can volunteer on assignments if ( is_user_logged_in() ) { wp_get_current_user(); $user_roles = $assignment_desk->custom_taxonomies->get_user_roles(); $available_roles = $assignment_desk->custom_taxonomies->get_user_roles_for_post( $post_id ); // 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(); } $current_user_type = (int)get_usermeta( $current_user->ID, 'ad_user_type' ); // Do not equal negative if someone created a new user type on us that // hasn't been saved in association with the post if ( get_post_meta( $post_id, "_ad_participant_type_$current_user_type" , true ) == 'off' ) { return false; } $volunteer_form = ''; $volunteer_form .= '
'; $volunteer_form .= '
'; if ( $pitch_form_options['pitch_form_volunteer_label'] ) { $volunteer_label = $pitch_form_options['pitch_form_volunteer_label']; } else { $volunteer_label = 'Volunteer'; } $volunteer_form .= ''; $volunteer_form .= ''; if ( $pitch_form_options['pitch_form_volunteer_description'] ) { $pitch_form .= '

' . $pitch_form_options['pitch_form_volunteer_description'] . '

'; } $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, $wpdb; if ( isset($_POST['assignment_desk_volunteer_submit']) ) { $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']; } // Allow an alternate form of authentication when the volunteer form is saved do_action( 'ad_alternate_authentication', 'volunteer_form_save' ); if ( !is_user_logged_in() ) { return false; } wp_get_current_user(); $post_id = (int)$_POST['assignment_desk_volunteer_post_id']; $sanitized_roles = $_POST['assignment_desk_volunteer_roles']; $sanitized_user_id = $current_user->ID; // 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; } } } foreach ( $user_roles as $user_role ) { // Get previous roles, $previous_values = get_post_meta($post_id, '_ad_participant_role_' . $user_role->term_id, true); // New participant is a volunteer if ( in_array( $user_role->term_id, $valid_roles ) && !isset( $previous_values[$current_user->ID] ) ) { $previous_values[$current_user->ID] = 'volunteered'; update_usermeta($current_user->ID, '_ad_volunteer', $post_id); } // Invalid role suibmitted by a volunteer? 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 ); // Update the count of total volunteers $volunteers = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key='_ad_volunteer' AND meta_value=$post_id"); update_post_meta($post_id, '_ad_total_volunteers', $volunteers); } } /** * Hook into the WP_Query object to show unpublished posts * Will only show the post if it has a 'public' (defined in settings) assignment status */ function show_single_post( $the_post ) { if ( empty( $the_post ) && is_single() ) { $args = array( 'post_id' => $_GET['p'], 'showposts' => 1 ); $results = ad_get_all_public_posts( $args ); if ( !empty( $results ) ) { $the_post = $results; } } return $the_post; } /** * Get all of the CSS classes we might want on a pitch * * @param $post_id int The post ID * @todo Class for has votes * @todo Class for has volunteers * @todo Class for has comments * @return $classes array All of the classes to include in the HTML */ function get_css_classes_for_pitch( $post_id = null ) { global $assignment_desk; $public_facing_options = $assignment_desk->public_facing_options; $classes = array(); if ( !$post_id ) { global $post; $post_id = $post->ID; } $classes[] = 'assignment-desk-post-status-' . get_post_status( $post_id ); if ( $public_facing_options['public_facing_voting_enabled'] ) { $classes[] = 'assigment-desk-voting-enabled'; } if ( $public_facing_options['public_facing_commenting_enabled'] ) { $classes[] = 'assigment-desk-commenting-enabled'; } if ( $public_facing_options['public_facing_volunteering_enabled'] ) { $classes[] = 'assigment-desk-volunteering-enabled'; } return $classes; } /* * Replace an html comment with ad public pages. */ function show_all_posts( $the_content ) { global $wpdb, $assignment_desk, $post, $edit_flow, $current_user; wp_get_current_user(); $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 = '
'; if ( $_POST['sort_by'] == 'ranking' || $_POST['sort_by'] == 'post_date' || $_POST['sort_by'] == 'due_date' || $_POST['sort_by'] == 'volunteers' ) { $sort_by = $_POST['sort_by']; } else { $sort_by = 'post_date'; } if ( isset($_POST['sort_by_reverse']) && $_POST['sort_by_reverse'] == 'on' ) { $sort_by_reverse = true; } else { $sort_by_reverse = false; } if ( isset($_POST['user_types']) && $_POST['user_types'] != 'all' ) { $user_type_filter = (int)$_POST['user_types']; } else { $user_type_filter = 'all'; } if ( isset($_POST['post_status']) && $_POST['post_status'] != 'all' ) { $post_status_filter = $_POST['post_status']; } else { $post_status_filter = 'all'; } $args = array( 'post_status' => $post_status_filter, 'user_types' => $user_type_filter, 'sort_by' => $sort_by, 'sort_by_reverse' => $sort_by_reverse ); $permalinks_enabled = false; if ( get_option('permalink_structure') ) { $permalinks_enabled = true; $page_in_permalink = strpos($_SERVER['REQUEST_URI'], '/page/'); if ( $page_in_permalink ) { $page = substr($_SERVER['REQUEST_URI'], $page_in_permalink + 6, 1); $args['page'] = (int)$page; } } $paginator = new ad_paginator($args, ad_count_all_public_posts($args)); $all_pitches = ad_get_all_public_posts($paginator->args); $html .= ''; $html .= ''; if ( $options['public_facing_filtering_post_status_enabled'] || $options['public_facing_filtering_participant_type_enabled'] ) { $html .= ''; } if ( $options['public_facing_filtering_post_status_enabled'] ) { $html .= ''; } if ( $options['public_facing_filtering_participant_type_enabled'] ) { $user_types = $assignment_desk->custom_taxonomies->get_user_types(); $html .= ''; } if ( $options['public_facing_filtering_post_status_enabled'] || $options['public_facing_filtering_participant_type_enabled'] ) { $html .= ''; $html .= ''; } // Sorting functionality is optional and configured by the admin if ( $options['public_facing_filtering_sort_by_enabled'] ) { $html .= ''; $html .= ''; $html .= '