<?php
/**
* Store the Content Manger meta in the revision.
*
* @param $post_id
* @param $post
*/
function cma_revisions_save_post($post_id, $post) {
$parent_id = wp_is_post_revision($post_id);
if ($parent_id) {
// If the layout data meta exists, copy it into the revision.
if (isset($_POST['cma_layout_data']) && !empty($_POST['cma_layout_data'])) {
$layout_data = $_POST['cma_layout_data'];
} else {
$parent = get_post($parent_id);
$layout_data = get_post_meta($parent->ID, 'cma_layout_data', true);
}
if (!empty($layout_data)) {
add_metadata('post', $post_id, 'cma_layout_data', $layout_data);
}
}
}
add_action('save_post', 'cma_revisions_save_post', 11, 2);
/**
* Restore a revision when requested.
*
* @param $post_id
* @param $revision_id
*/
function cma_revisions_restore($post_id, $revision_id) {
$layout_data = get_metadata('post', $revision_id, 'cma_layout_data', true);
// update meta data
if (!empty($layout_data)) {
$layout_data = wp_slash($layout_data);
update_post_meta($post_id, 'cma_layout_data', $layout_data);
} else {
delete_post_meta($post_id, 'cma_layout_data');
}
}
add_action('wp_restore_post_revision', 'cma_revisions_restore', 10, 2);
|