函数文档

wp_autosave_post_revisioned_meta_fields()

💡 云策文档标注

概述

wp_autosave_post_revisioned_meta_fields() 函数用于在自动保存文章时,处理并保存那些被标记为需要版本控制的元字段。它会遍历这些字段,检查是否有变化,并将更新的值附加到自动保存版本中。

关键要点

  • 函数核心功能是自动保存文章版本控制元字段,确保元数据在自动保存时得到更新。
  • 通过 wp_post_revision_meta_keys() 获取指定文章类型的版本控制元键,并逐一检查其值是否发生变化。
  • 使用 delete_metadata() 和 add_metadata() 直接操作元数据,避免使用高级包装函数,以确保处理的是实际修订版本。
  • 函数内部忽略数据清理和非ce检查,因为它挂钩在核心内部钩子上,这些检查已在之前完成。

代码示例

function wp_autosave_post_revisioned_meta_fields( $new_autosave ) {
    $posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST;
    $post_type = get_post_type( $new_autosave['post_parent'] );
    foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {
        if ( isset( $posted_data[ $meta_key ] )
            && get_post_meta( $new_autosave['ID'], $meta_key, true ) !== wp_unslash( $posted_data[ $meta_key ] )
        ) {
            delete_metadata( 'post', $new_autosave['ID'], $meta_key );
            if ( ! empty( $posted_data[ $meta_key ] ) ) {
                add_metadata( 'post', $new_autosave['ID'], $meta_key, $posted_data[ $meta_key ] );
            }
        }
    }
}

注意事项

  • 函数假设数据已通过安全验证,因此不进行额外的清理或非ce检查,开发者在使用时需确保上下文安全。
  • 仅处理非空的元值,空值会被忽略,以避免保存无效数据。
  • 自 WordPress 6.4.0 版本引入,适用于需要集成自动保存功能的插件或主题开发。

📄 原文内容

Autosaves the revisioned meta fields.

Description

Iterates through the revisioned meta fields and checks each to see if they are set, and have a changed value. If so, the meta value is saved and attached to the autosave.

Parameters

$new_autosavearrayrequired
The new post data being autosaved.

Source

function wp_autosave_post_revisioned_meta_fields( $new_autosave ) {
	/*
	 * The post data arrives as either $_POST['data']['wp_autosave'] or the $_POST
	 * itself. This sets $posted_data to the correct variable.
	 *
	 * Ignoring sanitization to avoid altering meta. Ignoring the nonce check because
	 * this is hooked on inner core hooks where a valid nonce was already checked.
	 */
	$posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST;

	$post_type = get_post_type( $new_autosave['post_parent'] );

	/*
	 * Go through the revisioned meta keys and save them as part of the autosave,
	 * if the meta key is part of the posted data, the meta value is not blank,
	 * and the meta value has changes from the last autosaved value.
	 */
	foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {

		if ( isset( $posted_data[ $meta_key ] )
			&& get_post_meta( $new_autosave['ID'], $meta_key, true ) !== wp_unslash( $posted_data[ $meta_key ] )
		) {
			/*
			 * Use the underlying delete_metadata() and add_metadata() functions
			 * vs delete_post_meta() and add_post_meta() to make sure we're working
			 * with the actual revision meta.
			 */
			delete_metadata( 'post', $new_autosave['ID'], $meta_key );

			// One last check to ensure meta value is not empty.
			if ( ! empty( $posted_data[ $meta_key ] ) ) {
				// Add the revisions meta data to the autosave.
				add_metadata( 'post', $new_autosave['ID'], $meta_key, $posted_data[ $meta_key ] );
			}
		}
	}
}

Changelog

Version Description
6.4.0 Introduced.