本文整理汇总了PHP中wp_save_post_revision函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_save_post_revision函数的具体用法?PHP wp_save_post_revision怎么用?PHP wp_save_post_revision使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_save_post_revision函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
public function setUp()
{
parent::setUp();
$this->author = $this->factory->user->create(array('role' => 'author'));
$this->contributor = $this->factory->user->create(array('role' => 'contributor'));
$this->post_id = $this->factory->post->create(array('post_author' => $this->author, 'post_title' => md5(wp_generate_password()), 'post_content' => md5(wp_generate_password())));
$this->revision_id = wp_save_post_revision($this->post_id);
$this->fake_server = $this->getMock('WP_JSON_Server');
$this->endpoint = new WP_JSON_Posts($this->fake_server);
}
示例2: thb_wp_migration_36
/**
* Function to be run when migrating to a WordPress version after 3.6.
*
* - Post format adaptation: image, link, video, audio, quote
*
* @return void
*/
function thb_wp_migration_36()
{
$posts = get_posts(array('posts_per_page' => -1, 'post_status' => array('image', 'link', 'audio', 'video', 'quote')));
$posts = (array) $posts;
if (count($posts) > 0) {
foreach ($posts as $post) {
$format = get_post_format($post->ID);
switch ($format) {
case 'image':
$featured_image = thb_get_featured_image($post->ID);
if (!empty($featured_image)) {
update_post_meta($post->ID, '_format_image', $featured_image);
// delete_post_meta($post->ID, '_thumbnail_id');
}
break;
case 'link':
$link_url = thb_get_post_meta($post->ID, 'link_url');
if (!empty($link_url)) {
update_post_meta($post->ID, '_format_link_url', $link_url);
// delete_post_meta($post->ID, THB_META_KEY . 'link_url');
}
break;
case 'video':
case 'audio':
$url = thb_get_post_meta($post->ID, $format . '_url');
if (!empty($url)) {
update_post_meta($post->ID, '_format_' . $format . '_embed', $url);
// delete_post_meta($post->ID, THB_META_KEY . $format . '_url');
}
break;
case 'quote':
$quote_text = thb_get_post_meta($post->ID, 'quote');
$quote_url = thb_get_post_meta($post->ID, 'quote_url');
$quote_url = str_replace('http://', '', $quote_url);
$quote_author = thb_get_post_meta($post->ID, 'quote_author');
wp_update_post(array('ID' => $post->ID, 'post_content' => $quote_text));
update_post_meta($post->ID, '_format_quote_source_url', $quote_url);
update_post_meta($post->ID, '_format_quote_source_name', $quote_author);
// delete_post_meta($post->ID, THB_META_KEY . 'quote');
// delete_post_meta($post->ID, THB_META_KEY . 'quote_url');
// delete_post_meta($post->ID, THB_META_KEY . 'quote_author');
break;
}
wp_save_post_revision($post->ID);
}
}
}
示例3: _wp_upgrade_revisions_of_post
/**
* Upgrade the revisions author, add the current post as a revision and set the revisions version to 1
*
* @since 3.6.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param WP_Post $post Post object
* @param array $revisions Current revisions of the post
* @return bool true if the revisions were upgraded, false if problems
*/
function _wp_upgrade_revisions_of_post($post, $revisions)
{
global $wpdb;
// Add post option exclusively
$lock = "revision-upgrade-{$post->ID}";
$now = time();
$result = $wpdb->query($wpdb->prepare("INSERT IGNORE INTO `{$wpdb->options}` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now));
if (!$result) {
// If we couldn't get a lock, see how old the previous lock is
$locked = get_option($lock);
if (!$locked) {
// Can't write to the lock, and can't read the lock.
// Something broken has happened
return false;
}
if ($locked > $now - 3600) {
// Lock is not too old: some other process may be upgrading this post. Bail.
return false;
}
// Lock is too old - update it (below) and continue
}
// If we could get a lock, re-"add" the option to fire all the correct filters.
update_option($lock, $now);
reset($revisions);
$add_last = true;
do {
$this_revision = current($revisions);
$prev_revision = next($revisions);
$this_revision_version = _wp_get_post_revision_version($this_revision);
// Something terrible happened
if (false === $this_revision_version) {
continue;
}
// 1 is the latest revision version, so we're already up to date.
// No need to add a copy of the post as latest revision.
if (0 < $this_revision_version) {
$add_last = false;
continue;
}
// Always update the revision version
$update = array('post_name' => preg_replace('/^(\\d+-(?:autosave|revision))[\\d-]*$/', '$1-v1', $this_revision->post_name));
// If this revision is the oldest revision of the post, i.e. no $prev_revision,
// the correct post_author is probably $post->post_author, but that's only a good guess.
// Update the revision version only and Leave the author as-is.
if ($prev_revision) {
$prev_revision_version = _wp_get_post_revision_version($prev_revision);
// If the previous revision is already up to date, it no longer has the information we need :(
if ($prev_revision_version < 1) {
$update['post_author'] = $prev_revision->post_author;
}
}
// Upgrade this revision
$result = $wpdb->update($wpdb->posts, $update, array('ID' => $this_revision->ID));
if ($result) {
wp_cache_delete($this_revision->ID, 'posts');
}
} while ($prev_revision);
delete_option($lock);
// Add a copy of the post as latest revision.
if ($add_last) {
wp_save_post_revision($post->ID);
}
return true;
}
示例4: bbp_edit_topic_handler
//.........这里部分代码省略.........
$terms = array(bbp_get_topic_tag_tax_id() => array());
// Existing terms
} else {
$terms = array(bbp_get_topic_tag_tax_id() => explode(',', bbp_get_topic_tag_names($topic_id, ',')));
}
/** Additional Actions (Before Save) **************************************/
do_action('bbp_edit_topic_pre_extras', $topic_id);
// Bail if errors
if (bbp_has_errors()) {
return;
}
/** No Errors *************************************************************/
// Add the content of the form to $topic_data as an array
// Just in time manipulation of topic data before being edited
$topic_data = apply_filters('bbp_edit_topic_pre_insert', array('ID' => $topic_id, 'post_title' => $topic_title, 'post_content' => $topic_content, 'post_status' => $topic_status, 'post_parent' => $forum_id, 'post_author' => $topic_author, 'post_type' => bbp_get_topic_post_type(), 'tax_input' => $terms));
// Toggle revisions to avoid duplicates
if (post_type_supports(bbp_get_topic_post_type(), 'revisions')) {
$revisions_removed = true;
remove_post_type_support(bbp_get_topic_post_type(), 'revisions');
}
// Insert topic
$topic_id = wp_update_post($topic_data);
// Toggle revisions back on
if (true === $revisions_removed) {
$revisions_removed = false;
add_post_type_support(bbp_get_topic_post_type(), 'revisions');
}
/** No Errors *************************************************************/
if (!empty($topic_id) && !is_wp_error($topic_id)) {
// Update counts, etc...
do_action('bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic_author, true);
/** Revisions *********************************************************/
// Revision Reason
if (!empty($_POST['bbp_topic_edit_reason'])) {
$topic_edit_reason = esc_attr(strip_tags($_POST['bbp_topic_edit_reason']));
}
// Update revision log
if (!empty($_POST['bbp_log_topic_edit']) && "1" === $_POST['bbp_log_topic_edit']) {
$revision_id = wp_save_post_revision($topic_id);
if (!empty($revision_id)) {
bbp_update_topic_revision_log(array('topic_id' => $topic_id, 'revision_id' => $revision_id, 'author_id' => bbp_get_current_user_id(), 'reason' => $topic_edit_reason));
}
}
/** Move Topic ********************************************************/
// If the new forum id is not equal to the old forum id, run the
// bbp_move_topic action and pass the topic's forum id as the
// first arg and topic id as the second to update counts.
if ($forum_id !== $topic->post_parent) {
bbp_move_topic_handler($topic_id, $topic->post_parent, $forum_id);
}
/** Stickies **********************************************************/
if (!empty($_POST['bbp_stick_topic']) && in_array($_POST['bbp_stick_topic'], array_keys(bbp_get_topic_types()))) {
// What's the caps?
if (current_user_can('moderate')) {
// What's the haps?
switch ($_POST['bbp_stick_topic']) {
// Sticky in forum
case 'stick':
bbp_stick_topic($topic_id);
break;
// Sticky in all forums
// Sticky in all forums
case 'super':
bbp_stick_topic($topic_id, true);
break;
// Normal
// Normal
case 'unstick':
default:
bbp_unstick_topic($topic_id);
break;
}
}
}
/** Additional Actions (After Save) ***********************************/
do_action('bbp_edit_topic_post_extras', $topic_id);
/** Redirect **********************************************************/
// Redirect to
$redirect_to = bbp_get_redirect_to();
// View all?
$view_all = bbp_get_view_all();
// Get the topic URL
$topic_url = bbp_get_topic_permalink($topic_id, $redirect_to);
// Add view all?
if (!empty($view_all)) {
$topic_url = bbp_add_view_all($topic_url);
}
// Allow to be filtered
$topic_url = apply_filters('bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to);
/** Successful Edit ***************************************************/
// Redirect back to new topic
wp_safe_redirect($topic_url);
// For good measure
exit;
/** Errors ****************************************************************/
} else {
$append_error = is_wp_error($topic_id) && $topic_id->get_error_message() ? $topic_id->get_error_message() . ' ' : '';
bbp_add_error('bbp_topic_error', __('<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', 'bbpress'));
}
}
示例5: bbp_edit_reply_handler
//.........这里部分代码省略.........
bbp_add_error('bbp_edit_reply_content', __('<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress'));
}
/** Reply Blacklist *******************************************************/
if (!bbp_check_for_blacklist($anonymous_data, $reply_author, $reply_title, $reply_content)) {
bbp_add_error('bbp_reply_blacklist', __('<strong>ERROR</strong>: Your reply cannot be edited at this time.', 'bbpress'));
}
/** Reply Status **********************************************************/
// Maybe put into moderation
if (!bbp_check_for_moderation($anonymous_data, $reply_author, $reply_title, $reply_content)) {
// Set post status to pending if public
if (bbp_get_public_status_id() === $reply->post_status) {
$reply_status = bbp_get_pending_status_id();
}
// Use existing post_status
} else {
$reply_status = $reply->post_status;
}
/** Reply To **************************************************************/
// Handle Reply To of the reply; $_REQUEST for non-JS submissions
if (isset($_REQUEST['bbp_reply_to'])) {
$reply_to = bbp_validate_reply_to($_REQUEST['bbp_reply_to']);
}
/** Topic Tags ************************************************************/
// Either replace terms
if (bbp_allow_topic_tags() && current_user_can('assign_topic_tags') && !empty($_POST['bbp_topic_tags'])) {
$terms = esc_attr(strip_tags($_POST['bbp_topic_tags']));
// ...or remove them.
} elseif (isset($_POST['bbp_topic_tags'])) {
$terms = '';
// Existing terms
} else {
$terms = bbp_get_topic_tag_names($topic_id);
}
/** Additional Actions (Before Save) **************************************/
do_action('bbp_edit_reply_pre_extras', $reply_id);
// Bail if errors
if (bbp_has_errors()) {
return;
}
/** No Errors *************************************************************/
// Add the content of the form to $reply_data as an array
// Just in time manipulation of reply data before being edited
$reply_data = apply_filters('bbp_edit_reply_pre_insert', array('ID' => $reply_id, 'post_title' => $reply_title, 'post_content' => $reply_content, 'post_status' => $reply_status, 'post_parent' => $topic_id, 'post_author' => $reply_author, 'post_type' => bbp_get_reply_post_type()));
// Toggle revisions to avoid duplicates
if (post_type_supports(bbp_get_reply_post_type(), 'revisions')) {
$revisions_removed = true;
remove_post_type_support(bbp_get_reply_post_type(), 'revisions');
}
// Insert topic
$reply_id = wp_update_post($reply_data);
// Toggle revisions back on
if (true === $revisions_removed) {
$revisions_removed = false;
add_post_type_support(bbp_get_reply_post_type(), 'revisions');
}
/** Topic Tags ************************************************************/
// Just in time manipulation of reply terms before being edited
$terms = apply_filters('bbp_edit_reply_pre_set_terms', $terms, $topic_id, $reply_id);
// Insert terms
$terms = wp_set_post_terms($topic_id, $terms, bbp_get_topic_tag_tax_id(), false);
// Term error
if (is_wp_error($terms)) {
bbp_add_error('bbp_reply_tags', __('<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress'));
}
/** Revisions *************************************************************/
// Revision Reason
if (!empty($_POST['bbp_reply_edit_reason'])) {
$reply_edit_reason = esc_attr(strip_tags($_POST['bbp_reply_edit_reason']));
}
// Update revision log
if (!empty($_POST['bbp_log_reply_edit']) && "1" === $_POST['bbp_log_reply_edit']) {
$revision_id = wp_save_post_revision($reply_id);
if (!empty($revision_id)) {
bbp_update_reply_revision_log(array('reply_id' => $reply_id, 'revision_id' => $revision_id, 'author_id' => bbp_get_current_user_id(), 'reason' => $reply_edit_reason));
}
}
/** No Errors *************************************************************/
if (!empty($reply_id) && !is_wp_error($reply_id)) {
// Update counts, etc...
do_action('bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author, true, $reply_to);
/** Additional Actions (After Save) ***********************************/
do_action('bbp_edit_reply_post_extras', $reply_id);
/** Redirect **********************************************************/
// Redirect to
$redirect_to = bbp_get_redirect_to();
// Get the reply URL
$reply_url = bbp_get_reply_url($reply_id, $redirect_to);
// Allow to be filtered
$reply_url = apply_filters('bbp_edit_reply_redirect_to', $reply_url, $redirect_to);
/** Successful Edit ***************************************************/
// Redirect back to new reply
wp_safe_redirect($reply_url);
// For good measure
exit;
/** Errors ****************************************************************/
} else {
$append_error = is_wp_error($reply_id) && $reply_id->get_error_message() ? $reply_id->get_error_message() . ' ' : '';
bbp_add_error('bbp_reply_error', __('<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress'));
}
}
示例6: set_revision_author
/**
* Sets the author latest revision
* of the provided post ID to the provided user.
*
* @param int $post_id Post ID to update revision author.
* @param int $user_id User ID for revision author.
*
* @return string|WP_Error
*/
protected function set_revision_author($post_id, $user_id)
{
$revision = wp_get_post_revisions($post_id);
if (!$revision) {
$new_revision = wp_save_post_revision($post_id);
if (!$new_revision || is_wp_error($new_revision)) {
return new WP_Error('db_error', 'There was a problem saving a new revision.');
}
// `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($new_revision);
if (!$revision) {
return new WP_Error('db_error', 'There was a problem retrieving the newly recreated revision.');
}
} else {
$revision = array_shift($revision);
}
return $this->set_post_author($revision->ID, $user_id);
}
示例7: pull
/**
* 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);
}
示例8: wp_update_custom_css_post
/**
* Update the `custom_css` post for a given theme.
*
* Inserts a `custom_css` post when one doesn't yet exist.
*
* @since 4.7.0
* @access public
*
* @param string $css CSS, stored in `post_content`.
* @param array $args {
* Args.
*
* @type string $preprocessed Pre-processed CSS, stored in `post_content_filtered`. Normally empty string. Optional.
* @type string $stylesheet Stylesheet (child theme) to update. Optional, defaults to current theme/stylesheet.
* }
* @return WP_Post|WP_Error Post on success, error on failure.
*/
function wp_update_custom_css_post($css, $args = array())
{
$args = wp_parse_args($args, array('preprocessed' => '', 'stylesheet' => get_stylesheet()));
$data = array('css' => $css, 'preprocessed' => $args['preprocessed']);
/**
* Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated.
*
* This filter can be used by plugin that offer CSS pre-processors, to store the original
* pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
* When used in this way, the `post_content_filtered` should be supplied as the setting value
* instead of `post_content` via a the `customize_value_custom_css` filter, for example:
*
* <code>
* add_filter( 'customize_value_custom_css', function( $value, $setting ) {
* $post = wp_get_custom_css_post( $setting->stylesheet );
* if ( $post && ! empty( $post->post_content_filtered ) ) {
* $css = $post->post_content_filtered;
* }
* return $css;
* }, 10, 2 );
* </code>
*
* @since 4.7.0
* @param array $data {
* Custom CSS data.
*
* @type string $css CSS stored in `post_content`.
* @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. Normally empty string.
* }
* @param array $args {
* The args passed into `wp_update_custom_css_post()` merged with defaults.
*
* @type string $css The original CSS passed in to be updated.
* @type string $preprocessed The original preprocessed CSS passed in to be updated.
* @type string $stylesheet The stylesheet (theme) being updated.
* }
*/
$data = apply_filters('update_custom_css_data', $data, array_merge($args, compact('css')));
$post_data = array('post_title' => $args['stylesheet'], 'post_name' => sanitize_title($args['stylesheet']), 'post_type' => 'custom_css', 'post_status' => 'publish', 'post_content' => $data['css'], 'post_content_filtered' => $data['preprocessed']);
// Update post if it already exists, otherwise create a new one.
$post = wp_get_custom_css_post($args['stylesheet']);
if ($post) {
$post_data['ID'] = $post->ID;
$r = wp_update_post(wp_slash($post_data), true);
} else {
$r = wp_insert_post(wp_slash($post_data), true);
// Trigger creation of a revision. This should be removed once #30854 is resolved.
if (!is_wp_error($r) && 0 === count(wp_get_post_revisions($r))) {
wp_save_post_revision($r);
}
}
if (is_wp_error($r)) {
return $r;
}
return get_post($r);
}
示例9: version_post_meta_and_terms
/**
* Meat and potatoes of the Meta Versioning plugin, saves post meta and tax terms to a new revision.
*
* @param int $post_id
* @return int $revision_id
*/
public static function version_post_meta_and_terms($post_id)
{
$post_type = get_post_type($post_id);
$post_meta = false;
$tracked_terms = false;
do_action('pre_version_meta', $post_id);
if (self::tracking_fields_for_post_type($post_type, self::POSTMETA_TYPE)) {
$post_meta = self::get_tracked_post_custom($post_id);
}
if (self::tracking_fields_for_post_type($post_type, self::TAXONOMY_TYPE)) {
$tracked_terms = self::get_tracked_terms($post_id);
}
$revision_id = wp_save_post_revision($post_id);
// This saves the post meta!
if ($revision_id && $post_meta) {
self::save_meta_to_revision($post_meta, $revision_id);
}
if ($revision_id && $tracked_terms) {
self::save_taxonomy_terms_to_revision($tracked_terms, $revision_id);
}
return $revision_id;
}
示例10: deleted_states_by_file_ids
/**
* Sets files as deleted by array of File IDs
*
* @param array $file_ids Array of File IDs
* @return bool whether this saved new files
* @since 0.5.0
*/
public function deleted_states_by_file_ids($file_ids)
{
$changed = false;
foreach ($file_ids as $file_id) {
if (!is_integer($file_id)) {
continue;
}
if (0 === $this->commit_id) {
$this->commit_id = wp_save_post_revision($this->ids['zip']);
}
$revisions = wp_get_post_revisions($file_id);
$prev_revision = array_shift($revisions);
$prev_state = $this->commit_query->state_by_id($prev_revision->ID);
$state_meta = array('status' => 'deleted', 'gist_id' => $prev_state->get_filename());
wp_update_post(array('ID' => $file_id, 'post_type' => 'revision', 'post_status' => 'inherit', 'post_parent' => $this->commit_id));
update_metadata('post', $file_id, "_wpgp_state_meta", $state_meta);
$this->commit_meta['state_ids'][] = $file_id;
}
}