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 that we need to compose from PHPH variables so we can use them later.
*/
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);
?>
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
* @todo Might need a non
*/
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']);
}
// 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 (current_user_can($assignment_desk->define_editor_permissions)) {
$all_participants = array();
$all_role_participants = array();
$all_volunteer_ids = array();
// For each User Role, save participant ID and status
foreach ( $user_roles as $user_role ) {
$role_participants = array();
$raw_role_participants = $_POST["ad-participant-role-$user_role->term_id"];
if ( count($raw_role_participants) ) {
foreach ($raw_role_participants as $raw_participant) {
$participant = explode('|', $raw_participant);;
$user = get_userdata((int)$participant[0]);
// Check if the user_login is valid.
if($user){
// array(user_ID => "status", user_ID => "status")
$role_participants[$user->ID] = $participant[1];
// array(user_ID => array(role_ID, role_ID, ...))
if(!in_array($user->ID, $all_participants)){
$all_participants[$user->ID] = array();
}
$all_participants[$user->ID][] = $user_role->term_id;
}
else {
// @todo - Invalid user. Store the error somewhere so it can be displayed
}
}
}
$all_role_participants[$user_role->term_id] = $role_participants;
// Remove a participant
if( $_POST['ad-participant-remove'] ){
$pieces = explode('|', $_POST['ad-participant-remove']);
$role_id = (int)$pieces[0];
$user_id = (int)$pieces[1];
if ( is_array($all_participants[$user_id]) && in_array($role_id, $all_participants[$user_id]) ){
unset($all_participants[$user_id][$role_id]);
}
if ( isset($all_role_participants[$role_id][$user_id]) ) {
unset($all_role_participants[$role_id][$user_id]);
}
}
// Assign a volunteer to the story
if( $_POST['ad-participant-assign'] ){
$pieces = explode('|', $_POST['ad-participant-assign']);
$role_id = (int)$pieces[0];
$user_id = (int)$pieces[1];
if ( isset($all_participants[$user_id]) && (!in_array($role_id, $all_participants[$user_id])) ){
$all_participants[$user_id][] = $role_id;
}
if ( isset($all_role_participants[$role_id][$user_id]) &&
$all_role_participants[$role_id][$user_id] == 'volunteered' ) {
$all_role_participants[$role_id][$user_id] = 'pending';
$this->send_assignment_email($post_id, $user_id, $role_id);
}
}
if ( $assignment_desk->coauthors_plus_exists() ) {
global $coauthors_plus;
// Update the role membership and prepare an array of coauthor IDs.
$coauthors = array();
foreach ( $all_role_participants as $role_id => $role_participants ) {
$existing_role_participants = get_post_meta($post_id, "_ad_participant_role_$role_id", true);
if(!$existing_role_participants){
$existing_role_participants = array();
}
foreach ( $role_participants as $user_id => $status ) {
if( !in_array($user_id, $existing_role_participants)){
$this->send_assignment_email($post_id, $user_id, $role_id);
}
if ( $status == 'accepted') {
$user = get_userdata($user_id);
$coauthors[]= $user->user_login;
}
else if ( $status == 'volunteered' ) {
$all_volunteer_ids[]= $user->ID;
}
}
update_post_meta($post_id, "_ad_participant_role_$role_id", $role_participants);
}
$_POST['coauthors'] = array_unique($coauthors);
$coauthors_plus->add_coauthors($post_id, array_unique($coauthors));
}
// Also save the User Roles associated with a row for each participant
foreach ($all_participants as $participant_id => $user_role_ids) {
update_post_meta($post_id, "_ad_participant_$participant_id", $user_role_ids);
}
}
$all_volunteer_ids = array_unique($all_volunteer_ids);
// Delete stale volunteering records. The volunteer was either assigned or removed.
$wpdb->query("DELETE FROM $wpdb->usermeta
WHERE meta_value=$post_id
AND meta_key='_ad_volunteer'
AND user_id NOT IN (" . implode(',', $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);
}
}
/**
* 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%',
'%post_link%',
'%display_name%',
'%role%',
'%dashboard_link%',
);
$replace = array(get_option('blogname'),
$post->post_title,
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)
?>