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

Ensure that revisions get created when changesets are updated via explicit saves #111

Merged
merged 12 commits into from Feb 28, 2017
Merged
3 changes: 2 additions & 1 deletion js/customize-snapshots.js
Expand Up @@ -1083,7 +1083,7 @@
},

/**
* Remove 'customize_changeset_status' if its already set.
* Remove 'customize_changeset_status' if its already set; replace with customize_snapshots_create_revision param.
*
* @return {void}
*/
Expand Down Expand Up @@ -1113,6 +1113,7 @@
isSameStatus = api.state( 'changesetStatus' ).get() === originalOptions.data.customize_changeset_status;
if ( 'customize_save' === originalOptions.data.action && isSameStatus && options.data ) {
options.data = removeParam( options.data, 'customize_changeset_status' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the customize_changeset_status param getting removed, again? Is this still needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we are checking customize_snapshots_create_revision customize_changeset_status does not need to be removed now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sayedtaqui but core will (somewhat unintuitively) create a new revision every time the customize_changeset_status is supplied. Wasn't there a reason for the param to be removed? Did it have something to do with passing the date?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter Yes we wanted to auto save the date and title in editbox, but we could not do wp.customize.previewer.save() without status because the default status is publish, and we did not want to create revision each time it auto saves the date/title.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so then we need to still include the logic for removing the status parameter, but then an additional change is needed right? We need to omit the request for a revision when the date and title are modified?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, thats correct.

options.data += '&customize_snapshots_create_revision=1';
}
} );
}
Expand Down
33 changes: 33 additions & 0 deletions php/class-customize-snapshot-manager.php
Expand Up @@ -104,6 +104,8 @@ function hooks() {
add_action( 'admin_bar_menu', array( $this, 'remove_all_non_snapshot_admin_bar_links' ), 100000 );
add_action( 'wp_before_admin_bar_render', array( $this, 'print_admin_bar_styles' ) );
add_filter( 'removable_query_args', array( $this, 'filter_removable_query_args' ) );
add_filter( 'wp_save_post_revision_post_has_changed', array( $this, '_filter_revision_post_has_changed' ), 20, 3 );
add_filter( 'save_post_customize_changeset', array( $this, 'create_initial_changeset_revision' ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! It should be add_action here.

add_filter( 'wp_insert_post_data', array( $this, 'prepare_snapshot_post_content_for_publish' ) );
}

Expand Down Expand Up @@ -342,6 +344,37 @@ public function snapshot() {
return $this->snapshot;
}

/**
* Create initial changeset revision.
*
* This should be removed once #30854 is resolved.
*
* @link https://core.trac.wordpress.org/ticket/30854
*/
public function create_initial_changeset_revision( $post_id ) {
if ( 0 === count( wp_get_post_revisions( $post_id ) ) ) {
wp_save_post_revision( $post_id );
}
}

/**
* When a customize_save Ajax action is being made, ensure a revision is allowed if customize_snapshots_create_revision query param is present.
*
* @see \WP_Customize_Manager::_filter_revision_post_has_changed()
*
* @param bool $post_has_changed Whether the post has changed.
* @param \WP_Post $last_revision The last revision post object.
* @param \WP_Post $post The post object.
* @return bool Whether a revision should be made.
*/
public function _filter_revision_post_has_changed( $post_has_changed, $last_revision, $post ) {
unset( $last_revision );
if ( 'customize_changeset' === $post->post_type && ! empty( $_POST['customize_snapshots_create_revision'] ) ) {
$post_has_changed = true;
}
return $post_has_changed;
}

/**
* Prepare snapshot post content for publishing.
*
Expand Down