/** * Note: Test needs reviewing when #16215 is fixed because I'm not sure the test current tests the "correct" behavior * @ticket 20982 * @ticket 16215 */ function test_revision_restore_updates_edit_last_post_meta() { $admin_user_id = $this->factory->user->create(array('role' => 'administrator')); $editor_user_id = $this->factory->user->create(array('role' => 'editor')); $author_user_id = $this->factory->user->create(array('role' => 'author')); //create a post as Author wp_set_current_user($author_user_id); $post = get_default_post_to_edit('post', true); $post_id = $post->ID; wp_update_post(array('post_status' => 'draft', 'post_content' => 'I cant spel werds.', 'ID' => $post_id)); //update post as Editor wp_set_current_user($editor_user_id); wp_update_post(array('post_content' => 'The Editor was in fixing your typos.', 'ID' => $post_id)); //restore back as Admin wp_set_current_user($admin_user_id); $revisions = wp_get_post_revisions($post->ID); $this->assertCount(2, $revisions); $lastrevision = end($revisions); $this->assertEquals('I cant spel werds.', $lastrevision->post_content); // #16215 $this->assertEquals($author_user_id, $lastrevision->post_author); wp_restore_post_revision($lastrevision->ID); //is post_meta correctly set to revision author $this->assertEquals($admin_user_id, get_post_meta($post_id, '_edit_last', true)); //after restoring user wp_set_current_user(0); }
/** * Restore a post revision * * @since 3.5.0 * * @uses wp_restore_post_revision() * * @param array $args Method parameters. Contains: * - int $blog_id (unused) * - string $username * - string $password * - int $post_id * @return bool|IXR_Error false if there was an error restoring, true if success. */ public function wp_restoreRevision($args) { if (!$this->minimum_args($args, 3)) { return $this->error; } $this->escape($args); $username = $args[1]; $password = $args[2]; $revision_id = (int) $args[3]; if (!($user = $this->login($username, $password))) { return $this->error; } /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */ do_action('xmlrpc_call', 'wp.restoreRevision'); if (!($revision = wp_get_post_revision($revision_id))) { return new IXR_Error(404, __('Invalid post ID')); } if (wp_is_post_autosave($revision)) { return new IXR_Error(404, __('Invalid post ID')); } if (!($post = get_post($revision->post_parent))) { return new IXR_Error(404, __('Invalid post ID')); } if (!current_user_can('edit_post', $revision->post_parent)) { return new IXR_Error(401, __('Sorry, you cannot edit this post.')); } // Check if revisions are disabled. if (!wp_revisions_enabled($post)) { return new IXR_Error(401, __('Sorry, revisions are disabled.')); } $post = wp_restore_post_revision($revision_id); return (bool) $post; }
break; } if (!($post = get_post($revision->post_parent))) { break; } // Revisions disabled (previously checked autosaves && ! wp_is_post_autosave( $revision )) if (!wp_revisions_enabled($post)) { $redirect = 'edit.php?post_type=' . $post->post_type; break; } // Don't allow revision restore when post is locked if (wp_check_post_lock($post->ID)) { break; } check_admin_referer("restore-post_{$revision->ID}"); wp_restore_post_revision($revision->ID); $redirect = add_query_arg(array('message' => 5, 'revision' => $revision->ID), get_edit_post_link($post->ID, 'url')); break; case 'view': case 'edit': default: if (!($revision = wp_get_post_revision($revision_id))) { break; } if (!($post = get_post($revision->post_parent))) { break; } if (!current_user_can('read_post', $revision->ID) || !current_user_can('read_post', $post->ID)) { break; } // Revisions disabled and we're not looking at an autosave
function insert_post($update = false, $freshness = 2) { global $wpdb; $dbpost = $this->normalize_post(true); if (!is_null($dbpost)) { $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks // This is a ridiculous f*****g kludge necessitated by WordPress 2.6 munging authorship meta-data add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); // Kludge to prevent kses filters from stripping the // content of posts when updating without a logged in // user who has `unfiltered_html` capability. $mungers = array('wp_filter_kses', 'wp_filter_post_kses'); $removed = array(); foreach ($mungers as $munger) { if (has_filter('content_save_pre', $munger)) { remove_filter('content_save_pre', $munger); $removed[] = $munger; } } if ($update and function_exists('get_post_field')) { // Don't munge status fields that the user may // have reset manually $doNotMunge = array('post_status', 'comment_status', 'ping_status'); foreach ($doNotMunge as $field) { $dbpost[$field] = get_post_field($field, $this->wp_id()); } } // WP3's wp_insert_post scans current_user_can() for the // tax_input, with no apparent way to override. Ugh. add_action('transition_post_status', array($this, 'add_terms'), -10001, 3); // WP3 appears to override whatever you give it for // post_modified. Ugh. add_action('transition_post_status', array($this, 'fix_post_modified_ts'), -10000, 3); if ($update) { $this->post['ID'] = $this->wp_id(); $dbpost['ID'] = $this->post['ID']; } // O.K., is this a new post? If so, we need to create // the basic post record before we do anything else. if ($this->this_revision_needs_original_post()) { // *sigh*, for handling inconsistent slash expectations < 3.6 $sdbpost = $this->db_sanitize_post($dbpost); // Go ahead and insert the first post record to // anchor the revision history. $this->_wp_id = wp_insert_post($sdbpost, true); $dbpost['ID'] = $this->_wp_id; } // Now that we've made sure the original exists, insert // this version here as a revision. $revision_id = _wp_put_post_revision($dbpost, false); if (!$this->this_revision_needs_original_post()) { if ($this->this_revision_is_current()) { wp_restore_post_revision($revision_id); } else { // If we do not activate this revision, then the // add_rss_meta will not be called, which is // more or less as it should be, but that means // we have to actively record this revision's // update hash from here. $postId = $this->post['ID']; $key = 'syndication_item_hash'; $hash = $this->update_hash(); FeedWordPress::diagnostic('syndicated_posts:meta_data', "Adding post meta-datum to post [{$postId}]: [{$key}] = " . FeedWordPress::val($hash, true)); add_post_meta($postId, $key, $hash, false); } } remove_action('transition_post_status', array($this, 'add_terms'), -10001, 3); remove_action('transition_post_status', array($this, 'fix_post_modified_ts'), -10000, 3); // Turn off ridiculous f*****g kludges #1 and #2 remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); foreach ($removed as $filter) { add_filter('content_save_pre', $filter); } $this->validate_post_id($dbpost, $update, array(__CLASS__, __FUNCTION__)); } }
/** * Restore a post revision * * @since 3.5.0 * * @uses wp_restore_post_revision() * * @param array $args Method parameters. Contains: * - int $blog_id * - string $username * - string $password * - int $post_id * @return bool false if there was an error restoring, true if success. */ function wp_restoreRevision($args) { if (!$this->minimum_args($args, 3)) { return $this->error; } $this->escape($args); $blog_id = (int) $args[0]; $username = $args[1]; $password = $args[2]; $revision_id = (int) $args[3]; if (!($user = $this->login($username, $password))) { return $this->error; } do_action('xmlrpc_call', 'wp.restoreRevision'); if (!($revision = wp_get_post_revision($revision_id))) { return new IXR_Error(404, __('Invalid post ID')); } if (wp_is_post_autosave($revision)) { return new IXR_Error(404, __('Invalid post ID')); } if (!($post = get_post($revision->post_parent))) { return new IXR_Error(404, __('Invalid post ID')); } if (!current_user_can('edit_post', $revision->post_parent)) { return new IXR_Error(401, __('Sorry, you cannot edit this post.')); } // Check if revisions are disabled. if (!WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions')) { return new IXR_Error(401, __('Sorry, revisions are disabled.')); } $post = wp_restore_post_revision($revision_id); return (bool) $post; }
function tdomf_moderation_handler() { $message .= ""; # this means a post was deleted # if (isset($_REQUEST['deleted'])) { $message .= __("Submissions deleted. ", "tdomf"); } // bulk actions if (isset($_REQUEST['doaction']) && isset($_REQUEST['action']) && isset($_REQUEST['post'])) { $posts = $_REQUEST['post']; $action = $_REQUEST['action']; if ($action != -1 && is_array($posts) && !empty($posts)) { check_admin_referer('tdomf-moderate-bulk'); switch ($action) { case 'spam_recheck': $spam_list = array(); $ham_list = array(); foreach ($posts as $post) { if (tdomf_check_submissions_spam($post)) { $ham_list[] = $post; } else { $spam_list[] = $post; } } tdomf_log_message('Akismet thinks these submissions are spam: ' . implode(", ", $spam_list)); $message .= sprintf(__("Marked these submissions as spam: %s.", "tdomf"), implode(", ", $spam_list)); tdomf_log_message('Akismet thinks these posts are not spam: ' . implode(", ", $ham_list)); $message .= " "; $message .= sprintf(__("Marked these submissions as not spam: %s.", "tdomf"), implode(", ", $ham_list)); break; case 'delete': foreach ($posts as $p) { wp_delete_post($p); } tdomf_log_message('Deleted ' . implode(", ", $posts) . ' posts'); $message .= sprintf(__("Deleted submissions: %s", "tdomf"), implode(", ", $posts)); break; case 'publish_now': $list = ""; foreach ($posts as $p) { if (!get_post_meta($p, TDOMF_KEY_SPAM)) { // if we're going to publish the post, then it's not spam! tdomf_ham_post($p); } tdomf_publish_post($p, false); $list .= "<a href=\"" . get_permalink($p) . "\">" . $p . "</a>, "; } tdomf_log_message("Published {$list} posts"); $message .= sprintf(__("Attempted to published these submissions immediately: %s", "tdomf"), $list); break; case 'publish': $list = ""; foreach ($posts as $p) { if (!get_post_meta($p, TDOMF_KEY_SPAM)) { // if we're going to publish the post, then it's not spam! tdomf_ham_post($p); } tdomf_publish_post($p); $list .= "<a href=\"" . get_permalink($p) . "\">" . $p . "</a>, "; } tdomf_log_message("Published or queued {$list} posts"); $message .= sprintf(__("Attempted to publish or queue these submissions: %s", "tdomf"), $list); break; case 'unpublish': foreach ($posts as $p) { tdomf_unpublish_post($p); } tdomf_log_message("Un-published " . implode(", ", $posts) . " posts"); $message .= sprintf(__("Attempted to un-publish theses submissions: %s", "tdomf"), implode(", ", $posts)); break; case 'spamit': $spams = array(); foreach ($posts as $p) { if (!get_post_meta($p, TDOMF_KEY_SPAM)) { tdomf_spam_post($p); $spams[] = $p; } } tdomf_log_message("Marked as spam " . implode(", ", $spams) . " posts"); $message .= sprintf(__("Marked these submissions as spam: %s", "tdomf"), implode(", ", $spams)); break; case 'hamit': $hams = array(); foreach ($posts as $p) { if (get_post_meta($p, TDOMF_KEY_SPAM)) { tdomf_spam_post($p); $hams[] = $p; } } if (!empty($hams)) { tdomf_log_message("Marked as ham " . implode(", ", $hams) . " posts"); $message .= sprintf(__("Marked these submissions as not spam: %s", "tdomf"), implode(", ", $hams)); } break; case 'lock': $locks = array(); foreach ($posts as $p) { if (!get_post_meta($p, TDOMF_KEY_LOCK)) { add_post_meta($p, TDOMF_KEY_LOCK, true, true); $locks[] = $p; } } if (!empty($locks)) { tdomf_log_message("Locked " . implode(", ", $locks) . " posts"); $message .= sprintf(__("Locked these posts/pages from editing: %s", "tdomf"), implode(", ", $locks)); } break; case 'unlock': $locks = array(); foreach ($posts as $p) { if (get_post_meta($p, TDOMF_KEY_LOCK)) { delete_post_meta($p, TDOMF_KEY_LOCK); $locks[] = $p; } } if (!empty($locks)) { tdomf_log_message("Unlocked " . implode(", ", $locks) . " posts"); $message .= sprintf(__("Unlocked these posts/pages: %s", "tdomf"), implode(", ", $locks)); } break; case 'edit_spam_recheck': $spam_list = array(); $ham_list = array(); $edit_spam_list = array(); $edit_ham_list = array(); foreach ($posts as $post) { $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1)); if ($last_edit != false && !empty($last_edit)) { if (tdomf_check_edit_spam($last_edit[0]->edit_id, false)) { $ham_list[] = $post; $edit_ham_list[] = $last_edit[0]->edit_id; } else { $spam_list[] = $post; $edit_spam_list[] = $last_edit[0]->edit_id; } } } tdomf_log_message('Akismet thinks these edits are spam: ' . implode(", ", $edit_spam_list)); $message .= sprintf(__("Marked last contribution on these submissions as spam: %s.", "tdomf"), implode(", ", $spam_list)); tdomf_log_message('Akismet thinks these edits are not spam: ' . implode(", ", $edit_ham_list)); $message .= " "; $message .= sprintf(__("Marked last contribution on these submissions as not spam: %s.", "tdomf"), implode(", ", $ham_list)); break; case 'edit_approve': $edit_list = array(); $post_list = array(); foreach ($posts as $post) { $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1)); if (!empty($last_edit) && $last_edit[0]->state != 'approved') { $edit_list[] = $last_edit[0]->edit_id; $post_list[] = $post; $user_id = $last_edit[0]->user_id; if ($last_edit[0]->state == 'spam') { tdomf_hamit_edit($last_edit[0]); } wp_restore_post_revision($edit->revision_id); tdomf_set_state_edit('approved', $last_edit[0]->edit_id); if ($user_id > 0) { tdomf_trust_user($user_id); } } } tdomf_log_message('These edits have been approved: ' . implode(", ", $edit_list)); $message .= sprintf(__("Approved contributions on these submissions: %s.", "tdomf"), implode(", ", $post_list)); break; case 'edit_revert': $edit_list = array(); $post_list = array(); foreach ($posts as $post) { $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1)); if (!empty($last_edit) && $last_edit[0]->state == 'approved' && $last_edit[0]->revision_id != 0 && $last_edit[0]->current_revision_id != 0) { $edit_list[] = $last_edit[0]->edit_id; $post_list[] = $post; wp_restore_post_revision($last_edit[0]->current_revision_id); tdomf_set_state_edit('unapproved', $last_edit[0]->edit_id); } } tdomf_log_message('These edits have been reverted: ' . implode(", ", $edit_list)); $message .= sprintf(__("Latest contribution on these submissions have been reverted: %s.", "tdomf"), implode(", ", $post_list)); break; case 'edit_delete': $edit_list = array(); $post_list = array(); foreach ($posts as $post) { $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1)); if (!empty($last_edit) && $last_edit[0]->state != 'approved') { $edit_list[] = $last_edit[0]->edit_id; $post_list[] = $post; if ($last_edit[0]->revision_id != 0) { wp_delete_post_revision($edit->revision_id); tdomf_log_message("Deleting revision " . $last_edit[0]->revision_id . " on post " . $post); } if ($last_edit[0]->current_revision_id != 0) { wp_delete_post_revision($last_edit[0]->current_revision_id); tdomf_log_message("Deleting revision " . $last_edit[0]->current_revision_id . " on post " . $post); } } tdomf_delete_edits($edit_list); } tdomf_log_message('These edits have been deleted: ' . implode(", ", $edit_list)); $message .= sprintf(__("Latest contribution on these submissions have been deleted: %s.", "tdomf"), implode(", ", $post_list)); break; case 'edit_spamit': $edit_list = array(); $post_list = array(); foreach ($posts as $post) { $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1)); if (!empty($last_edit) && $last_edit[0]->state != 'spam') { $edit_list[] = $last_edit[0]->edit_id; $post_list[] = $post; tdomf_spamit_edit($last_edit[0]); } } tdomf_log_message('These edits have been marked as spam: ' . implode(", ", $edit_list)); $message .= sprintf(__("Latest contribution on these submissions have been marked as spam: %s.", "tdomf"), implode(", ", $post_list)); break; case 'edit_hamit': $edit_list = array(); $post_list = array(); foreach ($posts as $post) { $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1)); if (!empty($last_edit) && $last_edit[0]->state == 'soam') { $edit_list[] = $last_edit[0]->edit_id; $post_list[] = $post; tdomf_hamit_edit($last_edit[0]); } } tdomf_log_message('These edits have been marked as not spam: ' . implode(", ", $edit_list)); $message .= sprintf(__("Latest contribution on these submissions have been marked as not being spam: %s.", "tdomf"), implode(", ", $post_list)); break; default: tdomf_log_message('Unexpected bulk action ' . $action . ' in moderation screen!', TDOMF_LOG_BAD); $message .= sprintf(__("Unrecognised bulk action %s,", "tdomf"), $action); break; } } // else no posts selected or bulk actions // individual actions // operations on posts/pages (submissions) } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'publish_now') { $post_id = $_REQUEST['post']; check_admin_referer('tdomf-publish_' . $post_id); // if we're going to publish the post, then it's not spam! tdomf_ham_post($post_id); tdomf_publish_post($post_id, false); tdomf_log_message("Published post {$post_id}"); $message .= sprintf(__("Published post <a href=\"%s\">%d</a>.", "tdomf"), get_permalink($post_id), $post_id); } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'publish') { $post_id = $_REQUEST['post']; check_admin_referer('tdomf-publish_' . $post_id); // if we're going to publish the post, then it's not spam! tdomf_ham_post($post_id); tdomf_publish_post($post_id); tdomf_log_message("Published post {$post_id}"); $message .= sprintf(__("Published post <a href=\"%s\">%d</a>.", "tdomf"), get_permalink($post_id), $post_id); } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'unpublish') { $post_id = $_REQUEST['post']; check_admin_referer('tdomf-unpublish_' . $post_id); tdomf_unpublish_post($post_id); tdomf_log_message("Unpublished post {$post_id}"); $message .= sprintf(__("Unpublished post %d.", "tdomf"), $post_id); } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'spamit') { $post_id = $_REQUEST['post']; check_admin_referer('tdomf-spamit_' . $post_id); if (!get_post_meta($post_id, TDOMF_KEY_SPAM)) { tdomf_spam_post($post_id); tdomf_log_message("Post {$post_id} submitted as spam"); $message .= sprintf(__("Post %d flagged as spam", "tdomf"), $post_id); } else { $message .= sprintf(__("Did not flag post %d as being spam as it is already flagged appropriately.", "tdomf"), $post_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'hamit') { $post_id = $_REQUEST['post']; check_admin_referer('tdomf-hamit_' . $post_id); if (get_post_meta($post_id, TDOMF_KEY_SPAM)) { tdomf_ham_post($post_id); tdomf_log_message("Post {$post_id} submitted as ham"); $message .= sprintf(__("Post %d flagged as not being spam", "tdomf"), $post_id); } else { $message .= sprintf(__("Did not flag post %d as not being spam as it is already flagged appropriately.", "tdomf"), $post_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'lock') { $post_id = $_REQUEST['post']; check_admin_referer('tdomf-lock_' . $post_id); if (!get_post_meta($post_id, TDOMF_KEY_LOCK)) { add_post_meta($post_id, TDOMF_KEY_LOCK, true, true); tdomf_log_message("Post {$post_id} locked"); $message .= sprintf(__("Post %d is now locked from editing", "tdomf"), $post_id); } else { $message .= sprintf(__("Post %d is already locked from editing.", "tdomf"), $post_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'unlock') { $post_id = $_REQUEST['post']; check_admin_referer('tdomf-unlock_' . $post_id); if (get_post_meta($post_id, TDOMF_KEY_LOCK)) { delete_post_meta($post_id, TDOMF_KEY_LOCK); tdomf_log_message("Post {$post_id} unlocked"); $message .= sprintf(__("Post %d is now unlocked.", "tdomf"), $post_id); } else { $message .= sprintf(__("Post %d is already unlocked.", "tdomf"), $post_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'approve_edit') { $edit_id = $_REQUEST['edit']; check_admin_referer('tdomf-approve_edit_' . $edit_id); $edit = tdomf_get_edit($edit_id); if ($edit && ($edit->state == 'spam' || $edit->state == 'unapproved')) { if ($edit->state == 'spam') { tdomf_hamit_edit($edit); } wp_restore_post_revision($edit->revision_id); tdomf_set_state_edit('approved', $edit_id); if ($edit->user_id > 0) { tdomf_trust_user($edit->user_id); } tdomf_log_message("Edit {$edit_id} has been approved on post " . $edit->post_id); $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has been approved and published', "tdomf"), get_permalink($edit->post_id), $edit->post_id); } else { tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD); $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'revert_edit') { $edit_id = $_REQUEST['edit']; check_admin_referer('tdomf-revert_edit_' . $edit_id); $edit = tdomf_get_edit($edit_id); if ($edit && $edit->state == 'approved' && $edit->revision_id != 0 && $edit->current_revision_id != 0) { wp_restore_post_revision($edit->current_revision_id); tdomf_set_state_edit('unapproved', $edit_id); tdomf_log_message("Edit {$edit_id} on post " . $edit->post_id . " has been reverted"); $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has reverted to previous revision', "tdomf"), get_permalink($edit->post_id), $edit->post_id); } else { tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD); $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'delete_edit') { $edit_id = $_REQUEST['edit']; check_admin_referer('tdomf-delete_edit_' . $edit_id); $edit = tdomf_get_edit($edit_id); if ($edit && $edit->state != 'approved') { $post_id = $edit->post_id; if ($edit->revision_id != 0) { wp_delete_post_revision($edit->revision_id); tdomf_log_message("Deleting revision {$revision_id} on post " . $post_id); } if ($edit->current_revision_id != 0) { wp_delete_post_revision($edit->current_revision_id); tdomf_log_message("Deleting revision {$current_revision_id} on post " . $post_id); } tdomf_delete_edits(array($edit_id)); tdomf_log_message("Edit {$edit_id} on post " . $post_id . " has been deleted"); $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has deleted', "tdomf"), get_permalink($edit->post_id), $edit->post_id); } else { tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD); $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'spamit_edit') { $edit_id = $_REQUEST['edit']; check_admin_referer('tdomf-spamit_edit_' . $edit_id); $edit = tdomf_get_edit($edit_id); if ($edit && $edit->state != 'spam') { tdomf_spamit_edit($edit); tdomf_log_message("Marking edit {$edit_id} as spam!"); $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has been flagged as spam', "tdomf"), get_permalink($edit->post_id), $edit->post_id); } else { tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD); $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id); } } else { if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'hamit_edit') { $edit_id = $_REQUEST['edit']; check_admin_referer('tdomf-hamit_edit_' . $edit_id); $edit = tdomf_get_edit($edit_id); if ($edit && $edit->state == 'spam') { tdomf_spamit_edit($edit); tdomf_log_message("Marking edit {$edit_id} as not spam!"); $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has been flagged as not being spam', "tdomf"), get_permalink($edit->post_id), $edit->post_id); } else { tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD); $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id); } } } } } } } } } } } } } } if (!empty($message)) { ?> <div id="message" class="updated fade"><p><?php echo $message; ?> </p></div> <?php } }
function theme($content) { global $post; $revision_id = isset($_REQUEST['revision']) ? absint($_REQUEST['revision']) : 0; $left = isset($_REQUEST['left']) ? absint($_REQUEST['left']) : 0; $right = isset($_REQUEST['right']) ? absint($_REQUEST['right']) : 0; $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view'; $new_content = ''; if ($action != 'edit') { $top = ""; $btop = ""; $btop .= '<div class="incsub_wiki incsub_wiki_single">'; $btop .= '<div class="incsub_wiki_tabs incsub_wiki_tabs_top">' . $this->tabs() . '<div class="incsub_wiki_clear"></div></div>'; switch ($action) { case 'discussion': break; case 'edit': set_include_path(get_include_path() . PATH_SEPARATOR . ABSPATH . 'wp-admin'); $post_type_object = get_post_type_object($post->post_type); $p = $post; if (empty($post->ID)) { wp_die(__('You attempted to edit an item that doesn’t exist. Perhaps it was deleted?')); } if (!current_user_can($post_type_object->cap->edit_post, $post_id)) { wp_die(__('You are not allowed to edit this item.')); } if ('trash' == $post->post_status) { wp_die(__('You can’t edit this item because it is in the Trash. Please restore it and try again.')); } if (null == $post_type_object) { wp_die(__('Unknown post type.')); } $post_type = $post->post_type; if ($last = $this->check_post_lock($post->ID)) { add_action('admin_notices', '_admin_notice_post_locked'); } else { $this->set_post_lock($post->ID); wp_enqueue_script('autosave'); } $title = $post_type_object->labels->edit_item; $post = $this->post_to_edit($post_id); $new_content = ''; break; case 'restore': if (!($revision = wp_get_post_revision($revision_id))) { break; } if (!current_user_can('edit_post', $revision->post_parent)) { break; } if (!($post = get_post($revision->post_parent))) { break; } // Revisions disabled and we're not looking at an autosave if ((!WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions')) && !wp_is_post_autosave($revision)) { $redirect = get_permalink() . '?action=edit'; break; } check_admin_referer("restore-post_{$post->ID}|{$revision->ID}"); wp_restore_post_revision($revision->ID); $redirect = add_query_arg(array('message' => 5, 'revision' => $revision->ID), get_permalink() . '?action=edit'); break; case 'diff': if (!($left_revision = get_post($left))) { break; } if (!($right_revision = get_post($right))) { break; } // If we're comparing a revision to itself, redirect to the 'view' page for that revision or the edit page for that post if ($left_revision->ID == $right_revision->ID) { $redirect = get_edit_post_link($left_revision->ID); include ABSPATH . 'wp-admin/js/revisions-js.php'; break; } // Don't allow reverse diffs? if (strtotime($right_revision->post_modified_gmt) < strtotime($left_revision->post_modified_gmt)) { $redirect = add_query_arg(array('left' => $right, 'right' => $left)); break; } if ($left_revision->ID == $right_revision->post_parent) { // right is a revision of left $post =& $left_revision; } elseif ($left_revision->post_parent == $right_revision->ID) { // left is a revision of right $post =& $right_revision; } elseif ($left_revision->post_parent == $right_revision->post_parent) { // both are revisions of common parent $post = get_post($left_revision->post_parent); } else { break; } // Don't diff two unrelated revisions if (!WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions')) { // Revisions disabled if (!wp_is_post_autosave($left_revision) && !wp_is_post_autosave($right_revision) || $post->ID !== $left_revision->ID && $post->ID !== $right_revision->ID) { $redirect = get_permalink() . '?action=edit'; break; } } if ($left_revision->ID == $right_revision->ID || !wp_get_post_revision($left_revision->ID) && !wp_get_post_revision($right_revision->ID)) { break; } $post_title = '<a href="' . get_permalink() . '?action=edit' . '">' . get_the_title() . '</a>'; $h2 = sprintf(__('Compare Revisions of “%1$s”'), $post_title); $title = __('Revisions'); $left = $left_revision->ID; $right = $right_revision->ID; case 'history': $args = array('format' => 'form-table', 'parent' => false, 'right' => $right, 'left' => $left); if (!WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions')) { $args['type'] = 'autosave'; } if (!isset($h2)) { $post_title = '<a href="' . get_permalink() . '?action=edit' . '">' . get_the_title() . '</a>'; $revisions = wp_get_post_revisions($post->ID); $revision = array_shift($revisions); $revision_title = wp_post_revision_title($revision, false); $h2 = sprintf(__('Revision for “%1$s” created on %2$s'), $post_title, $revision_title); } $new_content .= '<h3 class="long-header">' . $h2 . '</h3>'; $new_content .= '<table class="form-table ie-fixed">'; $new_content .= '<col class="th" />'; if ('diff' == $action) { $new_content .= '<tr id="revision">'; $new_content .= '<th scope="row"></th>'; $new_content .= '<th scope="col" class="th-full">'; $new_content .= '<span class="alignleft">' . sprintf(__('Older: %s', $this->translation_domain), wp_post_revision_title($left_revision, false)) . '</span>'; $new_content .= '<span class="alignright">' . sprintf(__('Newer: %s', $this->translation_domain), wp_post_revision_title($right_revision, false)) . '</span>'; $new_content .= '</th>'; $new_content .= '</tr>'; } // use get_post_to_edit filters? $identical = true; foreach (_wp_post_revision_fields() as $field => $field_title) { if ('diff' == $action) { $left_content = apply_filters("_wp_post_revision_field_{$field}", $left_revision->{$field}, $field); $right_content = apply_filters("_wp_post_revision_field_{$field}", $right_revision->{$field}, $field); if (!($rcontent = wp_text_diff($left_content, $right_content))) { continue; } // There is no difference between left and right $identical = false; } else { add_filter("_wp_post_revision_field_{$field}", 'htmlspecialchars'); $rcontent = apply_filters("_wp_post_revision_field_{$field}", $revision->{$field}, $field); } $new_content .= '<tr id="revision-field-<?php echo $field; ?>">'; $new_content .= '<th scope="row">' . esc_html($field_title) . '</th>'; $new_content .= '<td><div class="pre">' . $rcontent . '</div></td>'; $new_content .= '</tr>'; } if ('diff' == $action && $identical) { $new_content .= '<tr><td colspan="2"><div class="updated"><p>' . __('These revisions are identical.', $this->translation_domain) . '</p></div></td></tr>'; } $new_content .= '</table>'; $new_content .= '<br class="clear" />'; $new_content .= '<div class="incsub_wiki_revisions">' . $this->list_post_revisions($post, $args) . '</div>'; $redirect = false; break; default: $crumbs = array(); foreach ($post->ancestors as $parent_pid) { $parent_post = get_post($parent_pid); $crumbs[] = '<a href="' . get_permalink($parent_pid) . '" class="incsub_wiki_crumbs">' . $parent_post->post_title . '</a>'; } $crumbs[] = '<span class="incsub_wiki_crumbs">' . $post->post_title . '</span>'; sort($crumbs); $top .= join(get_option("incsub_meta_seperator", " > "), $crumbs); $children = get_children('post_parent=' . $post->ID . '&post_type=incsub_wiki'); $crumbs = array(); foreach ($children as $child) { $crumbs[] = '<a href="' . get_permalink($child->ID) . '" class="incsub_wiki_crumbs">' . $child->post_title . '</a>'; } $bottom = "<h3>" . __('Sub Wikis', $this->translation_domain) . "</h3> <ul><li>"; $bottom .= join("</li><li>", $crumbs); if (count($crumbs) == 0) { $bottom = ""; } else { $bottom .= "</li></ul>"; } $revisions = wp_get_post_revisions($post->ID); if (current_user_can('edit_wiki')) { $bottom .= '<div class="incsub_wiki-meta">'; if (is_array($revisions) && count($revisions) > 0) { $revision = array_shift($revisions); } $bottom .= '</div>'; } $notification_meta = get_post_custom($post->ID, array('incsub_wiki_email_notification' => 'enabled')); if ($notification_meta['incsub_wiki_email_notification'][0] == 'enabled' && !$this->is_subscribed()) { if (is_user_logged_in()) { $bottom .= '<div class="incsub_wiki-subscribe"><a href="' . wp_nonce_url(add_query_arg(array('post_id' => $post->ID, 'subscribe' => 1)), "wiki-subscribe-wiki_{$post->ID}") . '">' . __('Notify me of changes', $this->translation_domain) . '</a></div>'; } else { if (!empty($_COOKIE['incsub_wiki_email'])) { $user_email = $_COOKIE['incsub_wiki_email']; } else { $user_email = ""; } $bottom .= '<div class="incsub_wiki-subscribe">' . '<form action="" method="post">' . '<label>' . __('E-mail', $this->translation_domain) . ': <input type="text" name="email" id="email" value="' . $user_email . '" /></label> ' . '<input type="hidden" name="post_id" id="post_id" value="' . $post->ID . '" />' . '<input type="submit" name="subscribe" id="subscribe" value="' . __('Notify me of changes', $this->translation_domain) . '" />' . '<input type="hidden" name="_wpnonce" id="_wpnonce" value="' . wp_create_nonce("wiki-subscribe-wiki_{$post->ID}") . '" />' . '</form>' . '</div>'; } } $new_content = $btop . '<div class="incsub_wiki_top">' . $top . '</div>' . $new_content; $new_content .= '<div class="incsub_wiki_content">' . $content . '</div>'; $new_content .= '<div class="incsub_wiki_bottom">' . $bottom . '</div>'; $redirect = false; } $new_content .= '</div>'; } if (!comments_open()) { $new_content .= '<style type="text/css">' . '#comments { display: none; }' . '</style>'; } else { $new_content .= '<style type="text/css">' . '.hentry { margin-bottom: 5px; }' . '</style>'; } // Empty post_type means either malformed object found, or no valid parent was found. if (isset($redirect) && !$redirect && empty($post->post_type)) { $redirect = 'edit.php'; } if (!empty($redirect)) { echo '<script type="text/javascript">' . 'window.location = "' . $redirect . '";' . '</script>'; exit; } return $new_content; }
protected function rollback_post_types($post_types) { $success = true; foreach ($post_types as $post_type => $posts) { if (!empty($posts)) { // reverse the array so that we delete children first $_posts = array_reverse($posts); foreach ($_posts as $guid => $post) { if ($post == 'new') { $_post = new cfd_post(array('guid' => $guid)); wp_delete_post($_post->post->ID, true); } else { if ($post['post']['post_type'] == 'attachment') { if ($this->import_post($post) == false) { $success = false; $this->add_import_message('post_types.' . $post_type, '__error__', sprintf(__('Unknown error. Unable to revert post "%s" to revision "%s".', 'cf-deploy'), $guid, $post)); } } else { $result = wp_restore_post_revision($post); if (empty($result)) { $success = false; $this->add_import_message('post_types.' . $post_type, '__error__', sprintf(__('Unknown error. Unable to revert post "%s" to revision "%s".', 'cf-deploy'), $guid, $post)); } } } } } } return $success; }
/** * Test the revisions system for storage of meta values */ function test_revisions_stores_meta_values() { /** * Set Up. */ // Set up a new post $original_post_id = $post_id = $this->factory->post->create(); // And update to store an initial revision wp_update_post(array('post_content' => 'some initial content', 'ID' => $post_id)); // One revision so far $revisions = wp_get_post_revisions($post_id); $this->assertCount(1, $revisions); /** * First set up a meta value */ // Store a custom meta value, which is not revisioned by default update_post_meta($post_id, 'meta_revision_test', 'original'); // Update the post, storing a revision wp_update_post(array('post_content' => 'some more content', 'ID' => $post_id)); $revisions = wp_get_post_revisions($post_id); $this->assertCount(2, $revisions); // Next, store some updated meta values for the same key update_post_meta($post_id, 'meta_revision_test', 'update1'); // Save the post, changing content to force a revision wp_update_post(array('post_content' => 'some updated content', 'ID' => $post_id)); $revisions = wp_get_post_revisions($post_id); $this->assertCount(3, $revisions); /** * Now restore the original revision */ // Restore the previous revision $revisions = (array) wp_get_post_revisions($post_id); // Go back two to load the previous revision array_shift($revisions); $last_revision = array_shift($revisions); // Restore! wp_restore_post_revision($last_revision->ID); wp_update_post(array('ID' => $post_id)); $revisions = wp_get_post_revisions($post_id); $this->assertCount(4, $revisions); /** * Check the meta values to verify they are NOT revisioned - they are not revisioned by default. */ // Custom post meta should NOT be restored, orignal value should not be restored, value still 'update1' $this->assertEquals('update1', get_post_meta($post_id, 'meta_revision_test', true)); update_post_meta($post_id, 'meta_revision_test', 'update2'); /* * Test the revisioning of custom meta when enabled by the wp_post_revision_meta_keys filter */ // Add the custom field to be revised via the wp_post_revision_meta_keys filter add_filter('wp_post_revision_meta_keys', array($this, 'add_revisioned_keys')); // Save the post, changing content to force a revision wp_update_post(array('post_content' => 'more updated content', 'ID' => $post_id)); $revisions = wp_get_post_revisions($post_id); $this->assertCount(5, $revisions); // Store custom meta values, which should now be revisioned update_post_meta($post_id, 'meta_revision_test', 'update3'); /** * Save the post again, custom meta should now be revisioned * * Note that a revision is saved even though there is no change * in post content, because the revisioned post_meta has changed * */ wp_update_post(array('ID' => $post_id)); // This revision contains the existing post meta ('update3') $revisions = wp_get_post_revisions($post_id); $this->assertCount(6, $revisions); // Verify that previous post meta is set $this->assertEquals('update3', get_post_meta($post_id, 'meta_revision_test', true)); // Restore the previous revision $revisions = wp_get_post_revisions($post_id); // Go back two to load the previous revision array_shift($revisions); $last_revision = array_shift($revisions); wp_restore_post_revision($last_revision->ID); /** * Verify that previous post meta is restored. */ $this->assertEquals('update2', get_post_meta($post_id, 'meta_revision_test', true)); // Try storing a blank meta update_post_meta($post_id, 'meta_revision_test', ''); wp_update_post(array('ID' => $post_id)); update_post_meta($post_id, 'meta_revision_test', 'update 4'); wp_update_post(array('ID' => $post_id)); // Restore the previous revision $revisions = wp_get_post_revisions($post_id); array_shift($revisions); $last_revision = array_shift($revisions); wp_restore_post_revision($last_revision->ID); /** * Verify that previous blank post meta is restored */ $this->assertEquals('', get_post_meta($post_id, 'meta_revision_test', true)); /* * Test not tracking a key - remove the key from the revisioned meta. */ remove_all_filters('wp_post_revision_meta_keys'); // Meta should no longer be revisioned update_post_meta($post_id, 'meta_revision_test', 'update 5'); wp_update_post(array('ID' => $post_id, 'post_content' => 'changed content')); update_post_meta($post_id, 'meta_revision_test', 'update 6'); wp_update_post(array('ID' => $post_id, 'post_content' => 'go updated content')); // Restore the previous revision $revisions = wp_get_post_revisions($post_id); array_shift($revisions); $last_revision = array_shift($revisions); wp_restore_post_revision($last_revision->ID); /** * Verify that previous post meta is NOT restored. */ $this->assertEquals('update 6', get_post_meta($post_id, 'meta_revision_test', true)); // Add the custom field to be revised via the wp_post_revision_meta_keys filter add_filter('wp_post_revision_meta_keys', array($this, 'add_revisioned_keys')); /** * Test the revisioning of multiple meta keys */ // Add three values for meta update_post_meta($post_id, 'meta_revision_test', 'update 7'); add_post_meta($post_id, 'meta_revision_test', 'update 7 number 2'); add_post_meta($post_id, 'meta_revision_test', 'update 7 number 3'); wp_update_post(array('ID' => $post_id)); // Update all three values update_post_meta($post_id, 'meta_revision_test', 'update 8', 'update 7'); update_post_meta($post_id, 'meta_revision_test', 'update 8 number 2', 'update 7 number 2'); update_post_meta($post_id, 'meta_revision_test', 'update 8 number 3', 'update 7 number 3'); wp_update_post(array('ID' => $post_id)); // Restore the previous revision $revisions = wp_get_post_revisions($post_id); array_shift($revisions); $last_revision = array_shift($revisions); wp_restore_post_revision($last_revision->ID); /** * Verify that multiple metas stored correctly. */ $this->assertEquals(array('update 7', 'update 7 number 2', 'update 7 number 3'), get_post_meta($post_id, 'meta_revision_test')); /** * Test the revisioning of a multidimensional array. */ $test_array = array('a' => array('1', '2', '3'), 'b' => 'ok', 'c' => array('multi' => array('a', 'b', 'c'), 'not' => 'ok')); // Clear any old value. delete_post_meta($post_id, 'meta_revision_test'); // Set the test meta to the array. update_post_meta($post_id, 'meta_revision_test', $test_array); // Update to save. wp_update_post(array('ID' => $post_id)); // Set the test meta blank. update_post_meta($post_id, 'meta_revision_test', ''); // Update to save. wp_update_post(array('ID' => $post_id)); // Restore the previous revision $revisions = wp_get_post_revisions($post_id); array_shift($revisions); $last_revision = array_shift($revisions); wp_restore_post_revision($last_revision->ID); /** * Verify multidimensional array stored correctly. */ $stored_array = get_post_meta($post_id, 'meta_revision_test'); $this->assertEquals($test_array, $stored_array[0]); // Cleanup! wp_delete_post($original_post_id); }
/** * Determines what the user is trying to do on this page view. * * This determination is made mostly on the basis of the information passed in the URL * parameters. This function is also responsible for some of the object setup (getting the * revision post(s), etc). * * This is cribbed nearly wholesale from wp-admin/revision.php. In the future I would like * to clean it up to be less WordPressy and more pluginish. * * @package BuddyPress Docs * @since 1.1 */ function setup_action() { global $bp; if (!bp_docs_is_existing_doc()) { return; } wp_enqueue_script('list-revisions'); $redirect = false; switch ($this->action) { case 'restore': if (!($this->revision = wp_get_post_revision($this->revision_id))) { break; } if (!current_user_can('bp_docs_edit')) { break; } if (!($post = get_post($this->revision->post_parent))) { break; } // Revisions disabled and we're not looking at an autosave if (!wp_revisions_enabled($post) && !wp_is_post_autosave($this->revision)) { $redirect = 'edit.php?post_type=' . $post->post_type; break; } $referer = 'restore-post_' . $post->ID . '|' . $this->revision->ID; check_admin_referer($referer); wp_restore_post_revision($this->revision->ID); bp_core_add_message(sprintf(__('You have successfully restored the Doc to the revision from %s.', 'bp-docs'), $this->revision->post_date)); $redirect = get_permalink($post->ID) . '/' . BP_DOCS_HISTORY_SLUG . '/'; break; case 'diff': if (!($this->left_revision = get_post($this->left))) { break; } if (!($this->right_revision = get_post($this->right))) { break; } // Don't allow reverse diffs? if (strtotime($this->right_revision->post_modified_gmt) < strtotime($this->left_revision->post_modified_gmt)) { $redirect = add_query_arg(array('left' => $this->right, 'right' => $this->left)); break; } if ($this->left_revision->ID == $this->right_revision->post_parent) { // right is a revision of left $post =& $this->left_revision; } elseif ($this->left_revision->post_parent == $this->right_revision->ID) { // left is a revision of right $post =& $this->right_revision; } elseif ($this->left_revision->post_parent == $this->right_revision->post_parent) { // both are revisions of common parent $post = get_post($this->left_revision->post_parent); } else { break; } // Don't diff two unrelated revisions if (!wp_revisions_enabled($post)) { // Revisions disabled if (!wp_is_post_autosave($this->left_revision) && !wp_is_post_autosave($this->right_revision) || $post->ID !== $this->left_revision->ID && $post->ID !== $this->right_revision->ID) { $redirect = 'edit.php?post_type=' . $post->post_type; break; } } if ($this->left_revision->ID == $this->right_revision->ID || !wp_get_post_revision($this->left_revision->ID) && !wp_get_post_revision($this->right_revision->ID)) { break; } $post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>'; $h2 = sprintf(__('Compare Revisions of “%1$s”', 'bp-docs'), $post_title); $title = __('Revisions', 'bp-docs'); $this->left = $this->left_revision->ID; $this->right = $this->right_revision->ID; $redirect = false; break; case 'view': default: if (!($this->revision = wp_get_post_revision($this->revision_id))) { if ($this->revision = get_post($this->revision_id)) { $this->is_latest = true; } else { break; } } if (!($post = get_post($this->revision->post_parent))) { break; } // Revisions disabled and we're not looking at an autosave if (!wp_revisions_enabled($post) && !wp_is_post_autosave($this->revision)) { $redirect = 'edit.php?post_type=' . $post->post_type; break; } $post_title = '<a href="' . get_edit_post_link() . '">' . get_the_title() . '</a>'; $revision_title = wp_post_revision_title($this->revision, false); $h2 = sprintf(__('Revision for “%1$s” created on %2$s', 'bp-docs'), $post_title, $revision_title); $title = __('Revisions', 'bp-docs'); // Sets up the diff radio buttons $this->left = $this->revision->ID; $this->right = $post->ID; $redirect = false; break; } if ($redirect) { bp_core_redirect($redirect); } $this->setup_is_identical(); }
function save_post($post_id, $post) { global $wpdb; $is_new = false; if ($post->post_type == 'page') { return $post_id; } //remove_action( 'save_post', array(&$this, 'save_post'), 1, 2 ); if ($this->done) { return $post_id; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } if ($post->post_status == 'auto-draft' || $post->post_status == 'inherit') { return $post_id; } // if using workflow //if((isset($_POST['aw_submit_to_workflow']) && $_POST['aw_submit_to_workflow'] == on)){ if (!current_user_can('publish_' . $post->post_type . 's')) { // WordPress always appends an 's' to the end of the capability if ($post->post_status == 'pending') { $this->resetApproval($post_id); } // Get custom fields $custom = get_post_custom($post_id); // Get revisions from db $revisions = wp_get_post_revisions($post_id, array('posts_per_page' => 1)); $last_revision = array_pop($revisions); // Is this a new post if (count($revisions) <= 1 || empty($last_revision) || empty($last_revision->post_content)) { $is_new = true; } // wp_update_post( array( 'ID' => $post_id, 'post_status' => 'pending' ) ); if (!$is_new) { // Set published content to previous version $last_revision = $last_revision->ID; wp_restore_post_revision($last_revision); } update_post_meta($post_id, '_in_progress', 1); // If submitting to workflow if (isset($_POST['aw_submit_to_workflow']) && $_POST['aw_submit_to_workflow'] == 'on') { update_post_meta($post_id, '_waiting_for_approval', 1); $this->notify_approvers($post); } } else { if ($post->post_status == 'publish') { if ($this->options->force_workflow == 'on' && !$this->isAllApproved($post_id)) { // reset to pending if nobody yet approved wp_update_post(array('ID' => $post_id, 'post_status' => 'pending')); } else { $this->done = true; $this->clear_post_workflow_settings($post_id); } } else { if ($post->post_status == 'pending') { $this->resetApproval($post_id); update_post_meta($post_id, '_in_progress', 1); update_post_meta($post_id, '_waiting_for_approval', 1); } } } return $post_id; }
/** * Revert to the previous revision and notify the reviewers if necessary * @param int $post_ID the ID of the post being saved * @param stdClass $post the post object */ function save_post($post_ID, $post) { if ($this->done) { return $post_ID; } if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_ID; } if ('auto-draft' == $post->post_status || 'inherit' == $post->post_status) { return $post_ID; } $this->done = true; if (!isset($_REQUEST['_dpn_nonce']) || !wp_verify_nonce($_REQUEST['_dpn_nonce'], 'dpn-nonce')) { return $post_ID; } if (!isset($_REQUEST['dpn_notify']) || 0 == $_REQUEST['dpn_notify']) { return $post_ID; } if (1 == $_REQUEST['dpn_notify'] || 3 == $_REQUEST['dpn_notify']) { $dowhat = 'draft'; } else { $dowhat = 'publish'; } if (1 == $_REQUEST['dpn_notify'] || 2 == $_REQUEST['dpn_notify']) { $notify = true; } else { $notify = false; } if (isset($_REQUEST['dpn_notify_address']) && strstr($_REQUEST['dpn_notify_address'], ',')) { $_REQUEST['dpn_notify_address'] = explode(',', str_replace(' ', '', $_REQUEST['dpn_notify_address'])); } $revs = isset($_REQUEST['dpn_notify_address']) && !empty($_REQUEST['dpn_notify_address']) ? $_REQUEST['dpn_notify_address'] : array(); if (!is_array($revs)) { $revs = array_map('trim', explode(',', $revs)); } $revs = array_filter($revs, 'is_email'); $this->reviewers = !empty($revs) ? $revs : $this->get_reviewers(); global $wpdb; if ('draft' == $dowhat) { $last_revision = array_pop(wp_get_post_revisions($post_ID, array('posts_per_page' => 1))); if (empty($last_revision) || empty($last_revision->post_content)) { return $post_ID; } $last_revision = $last_revision->ID; wp_restore_post_revision($last_revision); } if ($notify) { $this->notify_reviewer($post_ID, $post, $dowhat); } return $post_ID; }
/** * Undo an entire import. All newly-created posts and associated comments are deleted. * Any categories and tags that were created during the import are also * deleted, but only if they have no reference count once the posts/comments are gone. * * For posts that were updated (rather than created), the last revision is restored. * If no revisions are available then the post is deleted. * * The import profile is retained and is not deleted, but the status is set to 'UNDO'. * * The instance will update itself back to the options database. */ function undo($id) { global $current_user, $blog_id; $original_blog_id = $blog_id; $import = TI_Import::get($id); if ($import === false) { return new WP_Error('ERROR', sprintf(__('Unable to read import id %s for undo'), $id)); } get_currentuserinfo(); $import->log(__("Undo started by: {$current_user->user_login}"), 'INFO'); $import->import_start(); foreach ((array) $import->get_imported_posts() as $post) { // Switch blogs if needed if (is_multisite() && isset($post['blog_id']) && $post['blog_id'] != $blog_id) { switch_to_blog($post['blog_id']); } // If post was updated during import, and revisions is on, then try to roll back to previous version if (defined('WP_POST_REVISIONS') && WP_POST_REVISIONS && isset($post['updated']) && $post['updated'] && isset($post['revision_id'])) { $result = wp_restore_post_revision($post['revision_id']); if (is_wp_error($result)) { $import->log(sprintf(__("Unable to restore original version of post %s (%s): %s"), $post['post_title'], $post['post_id'], $result->get_error_message())); } if (!$result) { $import->log(sprintf(__("Unable to restore original version of post %s (%s)"), $post['post_title'], $post['post_id'])); } } else { // If no revisions, or post was created during import, then delete it $result = wp_delete_post($post['post_id']); if (is_wp_error($result)) { $import->log(sprintf(__("Error deleting post %s (%s): %s"), $post['post_title'], $post['post_id'], $result->get_error_message())); } } } // NOTE: counts seem to be incorrect for custom taxonomies - they include deleted posts - see wordpress trac #14084, #14073, #14392 // Delete tags, categories and custom taxonomies foreach ((array) $import->imported_terms as $taxonomy => $terms) { foreach ($terms as $term) { // Switch blogs if needed if (is_multisite() && isset($term->blog_id) && $term->blog_id != $blog_id) { switch_to_blog($term->blog_id); } // Get current name in case it's changed $wp_term = get_term($term->term_id, $taxonomy); if (!is_wp_error($wp_term) && $wp_term) { // get_term() returns either wp_error or null $term->name = $wp_term->name; } // Term doesn't exist if (!$wp_term || is_wp_error($wp_term)) { $import->log(sprintf(__('Could not delete term "%s" in taxonomy "%s" because it no longer exists'), $term->name, $taxonomy), 'WARNING'); continue; } // Term still in use if ($wp_term->count > 0) { $import->log(sprintf(__('Term "%s" in taxonomy "%s" was not deleted because it is still in use'), $term->name, $taxonomy), 'WARNING'); continue; } // Delete term $result = wp_delete_term($term->term_id, $taxonomy); if (!$result) { $import->log(sprintf(__('Uknown error deleting term "%s" in taxonomy "%s"'), $term->name, $taxonomy), 'ERROR'); } if (is_wp_error($result)) { $import->log(sprintf(__('Error deleting term "%s" in taxonomy "%s" : "%s"'), $term->name, $taxonomy, $result->get_error_message()), 'ERROR'); } } } // Switch back to original blog before writing out the import logs to the database if ($original_blog_id != $blog_id) { switch_to_blog($original_blog_id); } // Update terms cache $import->clean_term_cache(); // Set status and save back to db $import->log(__('Undo finished.'), 'INFO'); $import->status = 'UNDO'; $import->save(); return true; }