函数文档

wp_ajax_wp_fullscreen_save_post()

💡 云策文档标注

概述

wp_ajax_wp_fullscreen_save_post() 是一个 WordPress AJAX 处理函数,用于从全屏编辑器保存文章。它验证请求、更新文章数据,并返回包含最后编辑信息的 JSON 响应。

关键要点

  • 函数通过 AJAX 处理全屏编辑器的文章保存操作
  • 使用 check_ajax_referer() 验证请求安全性
  • 调用 edit_post() 更新文章,并处理可能的 WP_Error
  • 生成最后编辑时间戳和用户信息,通过 wp_send_json_success() 返回
  • 自 WordPress 4.3.0 起已弃用

代码示例

function wp_ajax_wp_fullscreen_save_post() {
    $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0;
    $post = null;
    if ( $post_id ) {
        $post = get_post( $post_id );
    }
    check_ajax_referer( 'update-post_' . $post_id, '_wpnonce' );
    $post_id = edit_post();
    if ( is_wp_error( $post_id ) ) {
        wp_send_json_error();
    }
    if ( $post ) {
        $last_date = mysql2date( __( 'F j, Y' ), $post->post_modified );
        $last_time = mysql2date( __( 'g:i a' ), $post->post_modified );
    } else {
        $last_date = date_i18n( __( 'F j, Y' ) );
        $last_time = date_i18n( __( 'g:i a' ) );
    }
    $last_id = get_post_meta( $post_id, '_edit_last', true );
    if ( $last_id ) {
        $last_user = get_userdata( $last_id );
        $last_edited = sprintf( __( 'Last edited by %1$s on %2$s at %3$s' ), esc_html( $last_user->display_name ), $last_date, $last_time );
    } else {
        $last_edited = sprintf( __( 'Last edited on %1$s at %2$s' ), $last_date, $last_time );
    }
    wp_send_json_success( array( 'last_edited' => $last_edited ) );
}

注意事项

  • 此函数已弃用,建议开发者避免在新代码中使用
  • 依赖于 $_POST 数据,需确保前端正确传递 post_ID 和 _wpnonce
  • 使用国际化函数如 __() 和 date_i18n() 支持多语言环境

📄 原文内容

Handles saving posts from the fullscreen editor via AJAX.

Source

function wp_ajax_wp_fullscreen_save_post() {
	$post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0;

	$post = null;

	if ( $post_id ) {
		$post = get_post( $post_id );
	}

	check_ajax_referer( 'update-post_' . $post_id, '_wpnonce' );

	$post_id = edit_post();

	if ( is_wp_error( $post_id ) ) {
		wp_send_json_error();
	}

	if ( $post ) {
		$last_date = mysql2date( __( 'F j, Y' ), $post->post_modified );
		$last_time = mysql2date( __( 'g:i a' ), $post->post_modified );
	} else {
		$last_date = date_i18n( __( 'F j, Y' ) );
		$last_time = date_i18n( __( 'g:i a' ) );
	}

	$last_id = get_post_meta( $post_id, '_edit_last', true );
	if ( $last_id ) {
		$last_user = get_userdata( $last_id );
		/* translators: 1: User's display name, 2: Date of last edit, 3: Time of last edit. */
		$last_edited = sprintf( __( 'Last edited by %1$s on %2$s at %3$s' ), esc_html( $last_user->display_name ), $last_date, $last_time );
	} else {
		/* translators: 1: Date of last edit, 2: Time of last edit. */
		$last_edited = sprintf( __( 'Last edited on %1$s at %2$s' ), $last_date, $last_time );
	}

	wp_send_json_success( array( 'last_edited' => $last_edited ) );
}

Changelog

Version Description
4.3.0 Deprecated.
3.1.0 Introduced.