返回介绍

wp_autosave()

发布于 2017-09-11 11:34:35 字数 3093 浏览 979 评论 0 收藏 0

wp_autosave( array $post_data )

Save a post submitted with XHR


description

Intended for use with heartbeat and autosave.js


参数

$post_data

(array) (Required) Associative array of the submitted post data.


返回值

(mixed) The value 0 or WP_Error on failure. The saved post ID on success. The ID can be the draft post_id or the autosave revision post_id.


源代码

File: wp-admin/includes/post.php

function wp_autosave( $post_data ) {
	// Back-compat
	if ( ! defined( 'DOING_AUTOSAVE' ) )
		define( 'DOING_AUTOSAVE', true );

	$post_id = (int) $post_data['post_id'];
	$post_data['ID'] = $post_data['post_ID'] = $post_id;

	if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) {
		return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) );
	}

	$post = get_post( $post_id );

	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
		return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) );
	}

	if ( 'auto-draft' == $post->post_status )
		$post_data['post_status'] = 'draft';

	if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) )
		$post_data['post_category'] = explode( ',', $post_data['catslist'] );

	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
		// Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
		return edit_post( wp_slash( $post_data ) );
	} else {
		// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
		return wp_create_post_autosave( wp_slash( $post_data ) );
	}
}

更新日志

Versiondescription
3.9.0Introduced.

相关函数

Uses

  • wp-admin/includes/post.php: wp_check_post_lock()
  • wp-admin/includes/post.php: wp_create_post_autosave()
  • wp-admin/includes/post.php: edit_post()
  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: wp_slash()
  • wp-includes/pluggable.php: wp_verify_nonce()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/post.php: get_post()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 5 more uses Hide more uses

Used By

  • wp-admin/includes/misc.php: heartbeat_autosave()

User Contributed Notes

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

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

发布评论

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