Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Ensure snapshot versions are kept with revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 24, 2016
1 parent b1ceccf commit 7e30012
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions php/class-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public function register() {

add_action( 'add_meta_boxes_' . static::SLUG, array( $this, 'remove_publish_metabox' ), 100 );
add_action( 'load-revision.php', array( $this, 'suspend_kses_for_snapshot_revision_restore' ) );
add_action( '_wp_put_post_revision', array( $this, 'store_version_with_snapshot_revision' ) );
add_action( 'wp_restore_post_revision', array( $this, 'restore_version_from_restored_snapshot_revision' ), 10, 2 );
add_filter( 'bulk_actions-edit-' . static::SLUG, array( $this, 'filter_bulk_actions' ) );
add_filter( 'get_the_excerpt', array( $this, 'filter_snapshot_excerpt' ), 10, 2 );
add_filter( 'post_row_actions', array( $this, 'filter_post_row_actions' ), 10, 2 );
Expand Down Expand Up @@ -195,6 +197,43 @@ function suspend_kses_for_snapshot_revision_restore() {
} );
}

/**
* Store the version number for each post revision post that is made.
*
* @param int $revision_id Post revision ID.
*/
function store_version_with_snapshot_revision( $revision_id ) {
$post = get_post( $revision_id );
$parent_post = get_post( $post->post_parent );
if ( ! $parent_post || static::SLUG !== $parent_post->post_type ) {
return;
}

if ( ! get_metadata( 'post', $revision_id, '_snapshot_version', true ) ) {
update_metadata( 'post', $revision_id, '_snapshot_version', $this->snapshot_manager->plugin->version );
}
}

/**
* Restore version postmeta with restored snapshot revision.
*
* @param int $post_id Post ID.
* @param int $source_revision_id Revision ID.
*/
function restore_version_from_restored_snapshot_revision( $post_id, $source_revision_id ) {
$post = get_post( $post_id );
if ( empty( $post ) || static::SLUG !== $post->post_type ) {
return;
}

$version = get_metadata( 'post', $source_revision_id, '_snapshot_version', true );
if ( $version ) {
update_metadata( 'post', $post_id, '_snapshot_version', $version );
} else {
delete_metadata( 'post', $post_id, '_snapshot_version' );
}
}

/**
* Remove edit bulk action for snapshots.
*
Expand Down Expand Up @@ -395,7 +434,7 @@ public function get_post_content( \WP_Post $post ) {
}

// Update data structure value.
$version = get_post_meta( $post->ID, '_snapshot_version', true );
$version = get_metadata( 'post', $post->ID, '_snapshot_version', true );
if ( empty( $version ) || version_compare( $version, '0.5.0', '<' ) ) {
$migrated_data = array();
foreach ( $data as $setting_id => $setting_params ) {
Expand Down Expand Up @@ -461,10 +500,11 @@ public function save( array $args ) {
$this->restore_kses();

if ( ! is_wp_error( $r ) ) {
$post_id = $r;
if ( ! empty( $args['theme'] ) ) {
update_post_meta( $r, '_snapshot_theme', $args['theme'] );
update_post_meta( $post_id, '_snapshot_theme', $args['theme'] );
}
update_post_meta( $r, '_snapshot_version', $this->snapshot_manager->plugin->version );
update_metadata( 'post', $post_id, '_snapshot_version', $this->snapshot_manager->plugin->version );
}

return $r;
Expand Down

0 comments on commit 7e30012

Please sign in to comment.