init();
}
function init() {
// Set up metabox and related actions
add_action('admin_menu', array(&$this, 'add_post_meta_box'));
// 20, 2 = execute our method very late (10 is default) and send 2 args,
// the second being the post object
add_action('save_post', array(&$this, 'save_post_meta_box'), 9, 2);
add_action('edit_post', array(&$this, 'save_post_meta_box'), 9, 2);
add_action('publish_post', array(&$this, 'save_post_meta_box'), 9, 2);
$this->enqueue_admin_css();
$this->enqueue_admin_javascript();
add_action( 'admin_print_scripts', array(&$this, 'javascript_variables') );
}
/**
* 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');
wp_enqueue_style('ad-fancybox', ASSIGNMENT_DESK_URL . 'js/fancybox/jquery.fancybox-1.3.1.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;
// AJAX link used for the autosuggest
if ($assignment_desk->coauthors_plus_exists()) {
$admin_url = admin_url();
echo '';
} else {
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.
*
* If the person who pitched the story is NOT currently a member of the
* WP blog we store their email address in the _ad_pitched_by field;
*
* When the post is saved we try to look up the user by email. Maybe
* they became a member or were assigned a story.
*/
function display_assignment_info(){
global $post, $wpdb;
echo "
";
$pitched_by = get_post_meta($post->ID, '_ad_pitched_by', true);
$users = $wpdb->get_results("SELECT ID, user_nicename
FROM $wpdb->users");
echo "";
// Try to resolve the user email to a user
if (is_email($pitched_by)){
$user = $wpdb->get_row($wpdb->prepare("SELECT ID, user_nicename
FROM $wpdb->users
WHERE user_email = %s", $pitched_by));
if ($user){
$pitched_by = $user->ID;
}
else {
echo $pitched_by;
}
}
// Only display a form if
if(!is_email($pitched_by)){
echo "";
}
// @todo - Origin? (community or staff)
echo '
';
}
/**
* Print the assignment status form.
* If there is no status the assignment is the default
* If the post is not in the assignment status don't show the form.
* @todo Check user editing permissions
*/
function display_assignment_status(){
global $post, $wpdb, $assignment_desk, $current_user;
wp_get_current_user();
echo '
';
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';
// List all of the assignment statuses
$assignment_statuses = get_terms($assignment_desk->custom_taxonomies->assignment_status_label,
array( 'get' => 'all'));
echo '
Role: ";
echo "";
}
/**
* Loren ipsum bitches
*/
function display_participants() {
global $assignment_desk, $post, $wpdb;
$user_roles = $assignment_desk->custom_taxonomies->get_user_roles(array('order' => "-name"));
// Load all existing participants from separate custom fields into
// one array so that we can list it later
$all_participants = array();
$total_participants = 0;
foreach ( $user_roles as $user_role ) {
$role_participants = get_post_meta($post->ID, "_ad_participant_role_$user_role->term_id");
$role_participants = $role_participants[0];
if (count($role_participants)) {
$all_participants[$user_role->term_id] = $role_participants;
$total_participants = $total_participants + count($role_participants);
}
}
// Get all of the users in the database
$all_users = $wpdb->get_results("SELECT * FROM $wpdb->users");
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 ' ';
} 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;
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; }
//if (!wp_verify_nonce($_POST['ad-noncename'], plugin_basename(__FILE__))){
// return $post_id;
// }
// The user who pitched this story
update_post_meta($post_id, '_ad_pitched_by', (int)$_POST['_ad_pitched_by']);
// 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) {
if ( in_array($user_type->term_id, $_POST['ad-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 {
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();
// For each User Role, save participant ID and status
foreach ( $user_roles as $user_role ) {
$raw_role_participants = array();
$all_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);
$all_role_participants[$participant[0]] = $participant[1];
$all_participants[$participant[0]][] = $user_role->term_id;
}
}
update_post_meta($post_id, "_ad_participant_role_$user_role->term_id", $all_role_participants);
}
// 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);
}
}
}
function send_assignment_email($post_id, $username){
// Get the template from the settings
// Fill it out
// Send it off
}
}
} // end if(!class_exists)
?>