/**
* Ajax callback to handle deleting the revision, then redirecting
* back to the post edit page with a confirmation message.
*/
public function revision_delete_action()
{
/**
* Bail if required values unset.
*/
if (!isset($_GET['revision'])) {
return;
}
$revision_id = sanitize_key($_GET['revision']);
/**
* Verify revision ID valud.
*/
if (!($revision = wp_get_post_revision($revision_id))) {
break;
}
/**
* Verify parent post valid.
*/
if (!($post = get_post($revision->post_parent))) {
break;
}
/**
* Verify current user can edit parent post.
*/
if (!current_user_can('edit_post', $post)) {
break;
}
/**
* Verify revisions not disabled and we're not looking at an autosave.
*/
if (!constant('WP_POST_REVISIONS') && !wp_is_post_autosave($revision)) {
break;
}
/**
* Check the nonce.
*/
check_admin_referer("delete-revision_{$post->ID}|{$revision->ID}");
/**
* Every checks out, delete the revision.
*/
wp_delete_post_revision($revision->ID);
wp_redirect(add_query_arg(array('message' => 99, 'revision' => $revision->ID), get_edit_post_link($post->ID, 'url')));
exit;
}
/**
* Deletes a revision.
*
* Deletes the row from the posts table corresponding to the specified revision.
*
* @since 2.6.0
*
* @param int|WP_Post $revision_id Revision ID or revision object.
* @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success.
*/
function wp_delete_post_revision($revision_id)
{
if (!($revision = wp_get_post_revision($revision_id))) {
return $revision;
}
$delete = wp_delete_post($revision->ID);
if ($delete) {
/**
* Fires once a post revision has been deleted.
*
* @since 2.6.0
*
* @param int $revision_id Post revision ID.
* @param object|array $revision Post revision object or array.
*/
do_action('wp_delete_post_revision', $revision->ID, $revision);
}
return $delete;
}
/**
* If a loop started, check if it's the main query, in which case, add filters to append divs.
*
* @since 1.0.0
* @access public
*
* @param WP_Query $wp_query
*/
function replace_current_with_revision($wp_query)
{
if ($wp_query->is_main_query() && isset($_POST['revview_revision_id']) && !empty($_POST['revview_revision_id']) && is_numeric($_POST['revview_revision_id'])) {
$this->requested_revision = wp_get_post_revision($_POST['revview_revision_id']);
if ($this->requested_revision instanceof WP_Post) {
$parent = get_post($this->requested_revision->post_parent);
if ($parent instanceof WP_Post) {
$parent_post_status = get_post_status_object($parent->post_status);
/**
* Filters the status of the parent entry whose revision was requested.
*
* @param bool $parent_post_status_public Whether the post status of the parent entry is public or not.
* @param int $post The ID of the entry parent of the requested revision.
*/
if (apply_filters('revview_allow_revision_load', $parent_post_status->public && empty($parent->post_password), $parent->ID)) {
add_filter('the_title', array($this, 'revview_title'), 0);
add_filter('the_content', array($this, 'revview_content'), 0);
add_filter('the_excerpt', array($this, 'revview_excerpt'), 0);
}
}
}
}
}
/**
* Reads the Webhook payload and syncs posts as necessary
*
* @param stdClass $payload
*
* @return array
*/
public function pull($payload)
{
if (strtolower($payload->repository->full_name) !== strtolower($this->api->repository())) {
$msg = strtolower($payload->repository->full_name) . __(' is an invalid repository.', 'wordpress-github-sync');
WordPress_GitHub_Sync::write_log($msg);
return array('result' => 'error', 'message' => $msg);
}
// the last term in the ref is the branch name
$refs = explode('/', $payload->ref);
$branch = array_pop($refs);
if ('master' !== $branch) {
$msg = __('Not on the master branch.', 'wordpress-github-sync');
WordPress_GitHub_Sync::write_log($msg);
return array('result' => 'error', 'message' => $msg);
}
// We add wpghs to commits we push out, so we shouldn't pull them in again
if ('wpghs' === substr($payload->head_commit->message, -5)) {
$msg = __('Already synced this commit.', 'wordpress-github-sync');
WordPress_GitHub_Sync::write_log($msg);
return array('result' => 'error', 'message' => $msg);
}
$commit = $this->api->get_commit($payload->head_commit->id);
if (is_wp_error($commit)) {
$msg = sprintf(__('Failed getting commit with error: %s', 'wordpress-github-sync'), $commit->get_error_message());
WordPress_GitHub_Sync::write_log($msg);
return array('result' => 'error', 'message' => $msg);
}
$import = new WordPress_GitHub_Sync_Import();
$import->run($commit->tree->sha);
$user = get_user_by('email', $payload->head_commit->author->email);
if (!$user) {
// use the default user
$user = get_user_by('id', get_option('wpghs_default_user'));
}
// if we can't find a user and a default hasn't been set,
// we're just going to set the revision author to 0
update_option('_wpghs_export_user_id', $user ? $user->ID : 0);
global $wpdb;
if ($updated_posts = $import->updated_posts()) {
foreach ($updated_posts as $post_id) {
$revision = wp_get_post_revision($post_id);
if (!$revision) {
$revision = wp_save_post_revision($post_id);
if (!$revision || is_wp_error($revision)) {
// there was a problem saving a new revision
continue;
}
// wp_save_post_revision returns the ID, whereas get_post_revision returns the whole object
// in order to be consistent, let's make sure we have the whole object before continuing
$revision = get_post($revision);
}
$wpdb->update($wpdb->posts, array('post_author' => (int) get_option('_wpghs_export_user_id')), array('ID' => $revision->ID), array('%d'), array('%d'));
}
}
// Deleting posts from a payload is the only place
// we need to search posts by path; another way?
$removed = array();
foreach ($payload->commits as $commit) {
$removed = array_merge($removed, $commit->removed);
}
foreach (array_unique($removed) as $path) {
$post = new WordPress_GitHub_Sync_Post($path);
wp_delete_post($post->id);
}
if ($new_posts = $import->new_posts()) {
// disable the lock to allow exporting
global $wpghs;
$wpghs->push_lock = false;
WordPress_GitHub_Sync::write_log(sprintf(__('Updating new posts with IDs: %s', 'wordpress-github-sync'), implode(', ', $new_posts)));
foreach ($new_posts as $post_id) {
$wpdb->update($wpdb->posts, array('post_author' => (int) get_option('_wpghs_export_user_id')), array('ID' => $post_id), array('%d'), array('%d'));
}
$msg = apply_filters('wpghs_commit_msg_new_posts', 'Updating new posts from WordPress at ' . site_url() . ' (' . get_bloginfo('name') . ')') . ' - wpghs';
$export = new WordPress_GitHub_Sync_Export($new_posts, $msg);
$export->run();
}
$msg = __('Payload processed', 'wordpress-github-sync');
WordPress_GitHub_Sync::write_log($msg);
return array('result' => 'success', 'message' => $msg);
}
/**
* 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;
//.........这里部分代码省略.........
function wp_momento_content_filter($content)
{
if (is_singular() && get_query_var('revision')) {
// Get the revision id
$revision_id = get_query_var('revision');
// Verify that it is a revision
if (wp_is_post_revision($revision_id)) {
// Remove the filer to avoid triggering an infinite loop
remove_filter('the_content', 'wp_momento_content_filter');
// Query this revision from the database
$revision_id = get_query_var('revision');
$revision = wp_get_post_revision($revision_id);
// Render the content using this older data
$rev_content = apply_filters('the_content', $revision->post_content);
// Put the filter override back on so we can use it again
add_filter('the_content', 'wp_momento_content_filter');
// Return the revision content
return $rev_content;
}
// If this a normal post and not a revision
// then nothing special should happen
if (is_single($revision_id)) {
return $content;
}
// If it's none of the above just return the normal content.
// Through perhaps we should have this raise a 404 or something.
return $content;
} else {
return $content;
}
}
/**
* 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;
}
请发表评论