enqueue_admin_css();
$this->enqueue_admin_javascript();
add_action( 'admin_print_scripts', array(&$this, 'javascript_variables') );
add_action('wp_ajax_user_check', array(&$this, 'ajax_user_check'));
}
/**
* Adds Assignment Desk meta_box to Post/Page edit pages
*/
function add_post_meta_box() {
global $assignment_desk;
if (function_exists('add_meta_box')) {
add_meta_box('assignment-desk',
__('Assignment Desk', 'assignment-desk'),
array(&$this, 'post_meta_box'),
'post',
'side',
'high');
}
}
/**
* Adds Assignment Desk CSS to Post/Page edit pages
*/
function enqueue_admin_css(){
// Enqueue the ad_post_meta.css
wp_enqueue_style('ad-post-meta-style', ASSIGNMENT_DESK_URL.'css/post.css', false, false, 'all');
}
/**
* Adds Assignment Desk javascript to Post/Page edit pages
*/
function enqueue_admin_javascript() {
wp_enqueue_script('ad-post-js', ASSIGNMENT_DESK_URL .'js/post.js', array('jquery', 'suggest'));
}
/**
* Print out some global JS variables.
*/
function javascript_variables(){
global $assignment_desk;
$admin_url = admin_url();
// AJAX link used for the autosuggest
echo '';
}
/**
* Print a the meta_box fragment that shows a form to choose the person
* who pitched the story.
*
* The ID of the person who pitched the story is saved as a
* custom field under the key _ad_pitched_by_participant.
*/
function display_assignment_info(){
global $post, $wpdb, $assignment_desk;
if( !current_user_can($assignment_desk->define_editor_permissions) ){
return;
}
$pitched_by = get_post_meta( $post->ID, '_ad_pitched_by_participant', true );
$pitched_by_user = get_userdata( $pitched_by );
$pitched_by_timestamp = get_post_meta( $post->ID, '_ad_pitched_by_timestamp', true );
?>
get_results("SELECT ID, user_nicename FROM $wpdb->users"); ?>
OKCancel
';
echo ' ';
// What is the status of this Assignment?
$current_status = wp_get_object_terms($post->ID,
$assignment_desk->custom_taxonomies->assignment_status_label);
// Default assignment status is defined in Assignment Desk Settings
if ( !$current_status ) {
$current_status = $assignment_desk->custom_taxonomies->get_default_assignment_status();
} else {
$current_status = $current_status[0];
}
echo '' . $current_status->name . '';
if (current_user_can($assignment_desk->define_editor_permissions) ) {
echo ' Edit';
echo '
';
$assignment_statuses = $assignment_desk->custom_taxonomies->get_assignment_statuses();
if (count($assignment_statuses)) {
// List all of the assignment statuses
echo " ";
echo 'OK ';
echo 'Cancel';
}
else {
echo 'None defined';
echo "custom_taxonomies->assignment_status_label . " target='_blank'>Create";
}
echo '
';
}
echo '';
}
/**
* Print allowed participant types
* Editor and above can change the permitted participant types
*/
function display_participant_types() {
global $post, $wpdb, $assignment_desk, $current_user;
wp_get_current_user();
$user_types = $assignment_desk->custom_taxonomies->get_user_types();
$participant_types = $assignment_desk->custom_taxonomies->get_user_types_for_post($post->ID);
?>
Role: ";
echo "";
}
/**
* Show the form to add a new participant and current list of participants.
* If coauthors-plus is enabled show a text box with auto-completion for users.
* If coauthors-plus is not enabled we show a list of users in a select box.
*/
function display_participants() {
global $assignment_desk, $post, $wpdb;
$user_roles = $assignment_desk->custom_taxonomies->get_user_roles(array('order' => "-name"));
if ( count($user_roles) && current_user_can($assignment_desk->define_editor_permissions)) :
echo '
';
echo ' ';
// Use auto-suggest if Co-Authors Plus exists
// Otherwise, use a dropdown with all users
if ( $assignment_desk->coauthors_plus_exists() ) {
echo '';
echo ' ';
} else {
echo " ";
}
echo $this->user_role_select($user_roles); ?>
Add
';
}
/**
* Save Assignment Desk post meta data
*/
function save_post_meta_box($post_id, $post) {
global $executed_already, $wpdb, $assignment_desk, $current_user, $ad_user_errors;
wp_get_current_user();
// if ($executed_already){ return; } else { $executed_already = true; }
if ($post->post_type == 'revision') { return;}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; }
if ($executed_already) { return; }
// The user who pitched this story
if ( current_user_can( $assignment_desk->define_editor_permissions ) ) {
update_post_meta($post_id, '_ad_pitched_by_participant', (int)$_POST['ad-pitched-by-participant']);
// Save the pitch time if it doesn't already exist
if ( !get_post_meta( $post_id, '_ad_pitched_by_timestamp', true ) ) {
update_post_meta($post_id, '_ad_pitched_by_timestamp', date_i18n('U'));
}
}
// If current user can edit assignment status, let them
// Otherwise, set to default if contributor
if (current_user_can($assignment_desk->define_editor_permissions)) {
wp_set_object_terms($post_id, (int)$_POST['ad-assignment-status'], $assignment_desk->custom_taxonomies->assignment_status_label);
} else {
$current_status = wp_get_object_terms($post_id, $assignment_desk->custom_taxonomies->assignment_status_label);
if (!$current_status) {
$new_status = $assignment_desk->custom_taxonomies->get_default_assignment_status();
wp_set_object_terms($post_id, (int)$new_status->term_id, $assignment_desk->custom_taxonomies->assignment_status_label);
}
}
// If the current user can edit participant types, allow them to do so
// Otherwise, set all participant types to 'on' if they're unset
$user_types = $assignment_desk->custom_taxonomies->get_user_types();
// Only editors can update the participant types on an assignment
if ( current_user_can( $assignment_desk->define_editor_permissions ) ) {
foreach ($user_types as $user_type) {
$participant_types = array();
// If $_POST['ad-participant-types'] isn't set, then we have no contributor types
if ( $_POST['ad-participant-types'] ) {
$participant_types = $_POST['ad-participant-types'];
if ( in_array($user_type->term_id, $participant_types) ) {
update_post_meta($post_id, "_ad_participant_type_$user_type->term_id", 'on');
} else {
update_post_meta($post_id, "_ad_participant_type_$user_type->term_id", 'off');
}
} else {
update_post_meta($post_id, "_ad_participant_type_$user_type->term_id", 'off');
}
}
} else {
foreach ( $user_types as $user_type ) {
$participant_type_state = get_post_meta($post_id, "_ad_participant_type_$user_type->term_id", true);
if ( $participant_type_state != 'on' && $participant_type_state != 'off' ) {
update_post_meta($post_id, "_ad_participant_type_$user_type->term_id", 'on');
}
}
}
$user_roles = $assignment_desk->custom_taxonomies->get_user_roles();
// If the current user can edit participant types, allow them to do so
// Otherwise, set all participant types to 'on' if they're unset
// Only editors can update the participant types on an assignment
if ( current_user_can( $assignment_desk->define_editor_permissions ) ) {
foreach ($user_roles as $user_role) {
$participant_roles = array();
// If $_POST['ad-participant-roles'] isn't set, then we have no contributor roles
if ( $_POST['ad-participant-roles'] ) {
$participant_roles = $_POST['ad-participant-roles'];
if ( in_array($user_role->term_id, $participant_roles) ) {
update_post_meta($post_id, "_ad_participant_role_status_$user_role->term_id", 'on');
} else {
update_post_meta($post_id, "_ad_participant_role_status_$user_role->term_id", 'off');
}
} else {
update_post_meta($post_id, "_ad_participant_role_status_$user_role->term_id", 'off');
}
}
} else {
foreach ( $user_roles as $user_role ) {
$participant_role_state = get_post_meta($post_id, "_ad_participant_role_status_$user_role->term_id", true);
if ( $participant_role_state != 'on' && $participant_role_state != 'off' ) {
update_post_meta($post_id, "_ad_participant_role_status_$user_role->term_id", 'on');
}
}
}
if (current_user_can($assignment_desk->define_editor_permissions)) {
$all_volunteer_ids = array();
// For each User Role, save participant ID and status
foreach ( $user_roles as $user_role ) {
$role_participants[$user_role->term_id] = get_post_meta($post_id, "_ad_participant_role_$user_role->term_id", true);
}
if ( ! $role_participants ){
$role_participants = array();
}
// Remove a participant from a post
if( $_POST['ad-participant-remove'] ){
if (!is_array($_POST['ad-participant-remove'])){
$_POST['ad-participant-remove'] = array($_POST['ad-participant-remove']);
}
foreach ( $_POST['ad-participant-remove'] as $remove ){
$pieces = explode('|', $remove);
$role_id = (int)$pieces[0];
$user_id = (int)$pieces[1];
if ( $role_id && $user_id ) {
// Remove from the post participants
unset($role_participants[$role_id][$user_id]);
// Remove corresponding user record
$user_participant = get_post_meta($post_id, "_ad_participant_$user_id", true);
if ( $user_participant and is_array($user_participant) ){
unset($user_participant[$role_id]);
update_post_meta($post_id, "_ad_participant_$user_id", $user_participant);
delete_usermeta($user_id, '_ad_volunteer', $post_id);
}
}
}
}
// Assign a participant to a post
if( $_POST['ad-participant-assign'] ){
if (!is_array($_POST['ad-participant-assign'])){
$_POST['ad-participant-assign'] = array($_POST['ad-participant-assign']);
}
foreach ( $_POST['ad-participant-assign'] as $assign ){
$pieces = explode('|', $assign);
$role_id = (int)$pieces[0];
$user_id = (int)$pieces[1];
// Add the user to the post with the pending status and send email.
$role_participants[$role_id][$user_id] = 'pending';
$this->send_assignment_email($post_id, $user_id, $role_id);
// Add the role to the corresponding user record for this post
$user_participant = get_post_meta($post_id, "_ad_participant_$user_id", true);
if ( !$user_participant or !is_array($user_participant) ){
$user_participant = array();
}
$user_participant[] = $role_id;
update_post_meta($post_id, "_ad_participant_$user_id", $user_participant);
}
}
// Update the coauthors
if ( $assignment_desk->coauthors_plus_exists() ) {
global $coauthors_plus;
// Scan participants for those that accepted
$coauthors = array();
foreach ( $user_roles as $user_role ) {
if ( !$role_participants[$user_role->term_id] ) {
continue;
}
foreach ( $role_participants[$user_role->term_id] as $user_id => $status ) {
if ( $status == 'accepted' ) {
$user = get_userdata($user_id);
$coauthors[] = $user->user_login;
}
}
}
$coauthors = array_unique($coauthors);
// If no coauthors assign it to the current user
if ( empty($coauthors) ) {
$coauthors[] = $current_user->user_login;
}
$_POST['coauthors'] = $coauthors;
}
// Update the participants for this role
foreach ( $user_roles as $user_role ) {
update_post_meta($post_id, "_ad_participant_role_{$user_role->term_id}", $role_participants[$user_role->term_id]);
}
// Update the number of unique volunteers
$all_volunteer_ids = array();
foreach ( $user_roles as $user_role ) {
if ( ! $role_participants[$user_role->term_id] ) {
continue;
}
foreach ( $role_participants[$user_role->term_id] as $user_id => $status ) {
if ( $status == 'volunteered' ) {
$all_volunteer_ids[] = $user_id;
}
}
}
$all_volunteer_ids = array_unique($all_volunteer_ids);
$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);
}
// Prevent this function from being called twice for a post during a single request.
$executed_already = true;
}
/**
* We need to store a zero vote and volunteer count so sorting by a meta_value count works correctly.
* Without the 0 meta_value in the db posts will not show up when sorting.
*/
function zero_sort_by_counts($post_id, $post){
$total_votes = get_post_meta($post_id, '_ad_votes_total', true);
if ( !$total_votes ) {
update_post_meta($post_id, '_ad_votes_total', 0);
}
$total_volunteers = get_post_meta($post_id, '_ad_total_volunteers', true);
if ( !$total_volunteers ) {
update_post_meta($post_id, '_ad_total_volunteers', 0);
}
}
/**
* Store the word count as post metadata.
* Avoid having to count the words when generating user stats.
*/
function save_post_word_count($post_id, $post){
update_post_meta($post_id, '_ad_word_count', str_word_count($post->post_content));
}
/**
* Fill out the template for the email a user receives when they're assigned a story.
* Then send the email.
*/
function send_assignment_email($post_id, $user_id, $role_id){
global $assignment_desk;
if ($assignment_desk->general_options['assignment_email_notifications_enabled'] != 'on'){
return;
}
$post = get_post($post_id);
$user = get_userdata($user_id);
$role = get_term($term_id, $assignment_desk->custom_taxonomies->user_role_label);
// Get the template from the settings
$email_template = $assignment_desk->general_options['assignment_email_template'];
$subject = $assignment_desk->general_options['assignment_email_template_subject'];
$search = array( '%blogname%',
'%title%',
'%excerpt%',
'%duedate%',
'%description%',
'%post_link%',
'%display_name%',
'%role%',
'%dashboard_link%',
);
$replace = array(get_option('blogname'),
$post->post_title,
$post->post_excerpt,
ad_format_ef_duedate(get_post_meta($post_id, '_ef_duedate', true)),
get_post_meta($post_id, '_ef_description', true),
get_permalink($post_id),
$user->display_name,
$role->name,
admin_url(),
);
// Fill it out
$email_template = str_replace($search, $replace, $email_template);
$subject = str_replace($search, $replace, $subject);
// Send it off
wp_mail($user->user_email, $subject, $email_template);
}
/**
* Very simple ajax call to validate a user by login.
*/
function ajax_user_check(){
global $current_user, $assignment_desk;
get_currentuserinfo();
if(current_user_can($assignment_desk->define_editor_permissions)) {
if($_GET['q']){
$user = get_userdata((int)$_GET['q']);
if($user){
echo $user->ID;
}
else {
echo '0';
}
}
}
die();
}
}
} // end if(!class_exists)
?>