WordPress Version: 6.4
/**
* Autosave the revisioned meta fields.
*
* 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.
*
* @since 6.4.0
*
* @param array $new_autosave The new post data being autosaved.
*/
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 thru 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 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 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]);
}
}
}
}