/** * Save manual snapshot edited data */ function anno_snapshot_edit_save($data, $postarr) { if (isset($_POST['anno_snapshot_edit_save']) && !empty($postarr['post_ID'])) { //remove_action('wp_insert_post', 'anno_users_snapshot', 10, 2); $post_id = $postarr['post_ID']; $authors = empty($_POST['anno_snapshot']) ? array() : $_POST['anno_snapshot']; update_post_meta($post_id, '_anno_author_snapshot', $authors); // Delete all anno authors meta, it will be reassigned in the next step, no built in WP methods to do this in a single query $anno_author_ids = anno_get_authors($post_id); foreach ($anno_author_ids as $key => $id) { $anno_author_ids[$key] = '_anno_author_' . $id; $formats[] = '%s'; } global $wpdb; array_unshift($anno_author_ids, $post_id); $sql = "DELETE FROM {$wpdb->postmeta} WHERE `post_id` = %d AND `meta_key` LIKE '_anno_author_%%' AND `meta_key` NOT IN ('_anno_author_snapshot', '_anno_author_order')"; $sql = $wpdb->prepare($sql, $anno_author_ids); $wpdb->query($sql); delete_post_meta($post_id, '_anno_author_order'); // Add the users back into the meta foreach ($authors as $id => $author_data) { // This also updates _anno_author_order anno_add_user_to_post('author', $id, $post_id); } // Check if primary author has been removed (WP Owner) assign to current user, do not add them to the snapshot if (!in_array($data['post_author'], array_keys($authors))) { // Do not re-add the old author remove_action('post_updated', 'annowf_switch_authors', '10, 3'); $data['post_author'] = get_current_user_id(); } } return $data; }
/** * AJAX handler for adding a user to a post with a given type * * @param string $type Type of user to add to the post (co_author, reviewer) * @return array Array of data pertaining to user added and JSON data. */ function annowf_add_user($type) { check_ajax_referer('anno_manage_' . $type, '_ajax_nonce-manage-' . $type); $message = 'error'; $html = ''; if (isset($_POST['post_id']) && isset($_POST['user'])) { $user_login = trim($_POST['user']); $user = get_user_by('login', $user_login); // Check if the user already exists if we're adding via email if (empty($user) && anno_is_valid_email($user_login)) { $users = get_users(array('search' => $user_login)); if (!empty($users) && is_array($users)) { $user = $users[0]; } } if (!empty($user)) { $post = get_post($_POST['post_id']); $co_authors = anno_get_authors(absint($_POST['post_id'])); $reviewers = anno_get_reviewers(absint($_POST['post_id'])); if ($type == 'reviewer') { $type_string = _x('reviewer', 'noun describing user', 'anno'); } else { $type_string = _x('co-author', 'noun describing user', 'anno'); } if ($post->post_author == $user->ID) { $html = sprintf(_x('Cannot add author as a %s', 'Adding user error message for article meta box', 'anno'), $type_string); } else { if (in_array($user->ID, $co_authors)) { $html = sprintf(_x('Cannot add %s as %s. User is already a co-author', 'Adding user error message for article meta box', 'anno'), $user->user_login, $type_string); } else { if (in_array($user->ID, $reviewers)) { $html = sprintf(_x('Cannot add %s as %s. User is already a reviewer', 'Adding user error message for article meta box', 'anno'), $user->user_login, $type_string); } else { if (anno_add_user_to_post($type, $user->ID, absint($_POST['post_id']))) { $message = 'success'; ob_start(); annowf_user_li_markup($user, $type); $html = ob_get_contents(); ob_end_clean(); } } } } } else { $html = sprintf(_x('User \'%s\' not found', 'Adding user error message for article meta box', 'anno'), $user_login); } } return array('message' => $message, 'html' => $html, 'user' => $user); }
function annowf_add_user_to_post($type, $user_id, $post_id) { return anno_add_user_to_post($type, $user_id, $post_id); }