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') );
// Only add voting if its enabled
if ( $public_facing_options['public_facing_voting_enabled'] ) {
$this->save_voting_form();
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'] ) {
$_REQUEST['assignment_desk_messages']['volunteer_form'] = $this->save_volunteer_form();
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'] ) {
$_REQUEST['assignment_desk_messages']['pitch_form'] = $this->save_pitch_form();
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() {
}
/**
* 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 .= '
';
}
/**
* 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 .= '';
$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.';
}
if ( is_user_logged_in() ) {
global $current_user;
$sanitized_author = (int)$_POST['assignment_desk_author'];
} 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;
}
}
$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.');
}
}
$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.');
}
}
$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.');
}
}
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.');
}
}
// 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 description if Edit Flow exists
if ( $assignment_desk->edit_flow_exists() ) {
update_post_meta($post_id, '_ef_description', $sanitized_description);
}
// Only handle duedate if Edit Flow exists
if ( $sanitized_duedate && $assignment_desk->edit_flow_exists() ) {
update_post_meta($post_id, '_ef_duedate', $sanitized_duedate);
}
// Only handle location if Edit Flow exists
if ( $assignment_desk->edit_flow_exists() ) {
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 the voting form HTML.
* @todo Functionality to remove a vote
*/
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;
}
// Only show the voting form to logged-in users
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;
/**
* Only show the vote button if the user hasn't voted before
* Text display is user-configurable but defaults to 'vote'
*/
if ( !in_array( $user_id, $all_votes ) ) {
$voting_form = '';
return $voting_form;
}
}
}
/**
* Display the number of votes and 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_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 );
if ( !$all_votes ) {
$all_votes = array();
}
$total_votes = (int)get_post_meta( $post_id, '_ad_votes_total', true );
if ( !$total_votes ) {
$total_votes = 0;
$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;
// Only logged-in users have the ability to vote
if ( isset($_POST['assignment_desk_voting_submit']) && is_user_logged_in() ) {
$form_messages = array();
// Ensure that it was the user who submitted the form, not a darn bot
if ( !wp_verify_nonce($_POST['assignment_desk_voting_nonce'], 'assignment_desk_voting') ) {
return $form_messages['error']['nonce'] = true;
}
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 );
// Catch if $all_votes has been set yet
if ( !is_array($all_votes) ){
$all_votes = array();
}
// Check whether the user has voted on this post yet. Users can only vote once
if ( !in_array( $sanitized_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
* @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;
}
// 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 .= '