返回介绍

wp_create_post_autosave()

发布于 2017-09-11 11:44:01 字数 3592 浏览 1254 评论 0 收藏 0

wp_create_post_autosave( mixed $post_data )

Creates autosave data for the specified post from $_POST data.


description


参数

$post_data

(mixed) (Required) Associative array containing the post data or int post ID.


返回值

(mixed) The autosave revision ID. WP_Error or 0 on error.


源代码

File: wp-admin/includes/post.php

function wp_create_post_autosave( $post_data ) {
	if ( is_numeric( $post_data ) ) {
		$post_id = $post_data;
		$post_data = $_POST;
	} else {
		$post_id = (int) $post_data['post_ID'];
	}

	$post_data = _wp_translate_postdata( true, $post_data );
	if ( is_wp_error( $post_data ) )
		return $post_data;

	$post_author = get_current_user_id();

	// Store one autosave per author. If there is already an autosave, overwrite it.
	if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
		$new_autosave = _wp_post_revision_data( $post_data, true );
		$new_autosave['ID'] = $old_autosave->ID;
		$new_autosave['post_author'] = $post_author;

		// If the new autosave has the same content as the post, delete the autosave.
		$post = get_post( $post_id );
		$autosave_is_different = false;
		foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
			if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
				$autosave_is_different = true;
				break;
			}
		}

		if ( ! $autosave_is_different ) {
			wp_delete_post_revision( $old_autosave->ID );
			return 0;
		}

		/**
		 * Fires before an autosave is stored.
		 *
		 * @since 4.1.0
		 *
		 * @param array $new_autosave Post array - the autosave that is about to be saved.
		 */
		do_action( 'wp_creating_autosave', $new_autosave );

		return wp_update_post( $new_autosave );
	}

	// _wp_put_post_revision() expects unescaped.
	$post_data = wp_unslash( $post_data );

	// Otherwise create the new autosave as a special post revision
	return _wp_put_post_revision( $post_data, true );
}

更新日志

Versiondescription
2.6.0Introduced.

相关函数

Uses

  • wp-includes/revision.php: _wp_post_revision_data()
  • wp-admin/includes/post.php: wp_creating_autosave
  • wp-admin/includes/post.php: _wp_translate_postdata()
  • wp-includes/formatting.php: normalize_whitespace()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/plugin.php: do_action()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/post.php: wp_update_post()
  • wp-includes/post.php: get_post()
  • wp-includes/revision.php: wp_delete_post_revision()
  • wp-includes/revision.php: _wp_put_post_revision()
  • wp-includes/revision.php: wp_get_post_autosave()
  • wp-includes/revision.php: _wp_post_revision_fields()
  • wp-includes/load.php: is_wp_error()
  • Show 9 more uses Hide more uses

Used By

  • wp-admin/includes/post.php: post_preview()
  • wp-admin/includes/post.php: wp_autosave()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文