Customize: Fix reliability of just-in-time publishing for changesets that miss their schedule when visiting `customize.php`.

When just doing `wp_publish_post()` for the changeset from `customize.php`, any option-based settings will fail to get saved because `WP_Customize_Manager` would have already been loaded with `settings_previewed`, resulting in `update_option()` calls being short-circuited. So an admin-ajax request to `customize_save` is used to work around this.

Props westonruter, jeremyfelt, dlh for testing, LittleBigThing for testing.
Amends [41626].
See #28721, #39221.
Fixes #42457 for trunk.


git-svn-id: https://develop.svn.wordpress.org/trunk@42138 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-11-09 04:43:09 +00:00
parent d671098003
commit c994a2e9bb
1 changed files with 26 additions and 2 deletions

View File

@ -42,10 +42,34 @@ if ( $wp_customize->changeset_post_id() ) {
get_post_time( 'G', true, $changeset_post ) < time() get_post_time( 'G', true, $changeset_post ) < time()
); );
if ( $missed_schedule ) { if ( $missed_schedule ) {
wp_publish_post( $changeset_post->ID ); /*
* Note that an Ajax request spawns here instead of just calling `wp_publish_post( $changeset_post->ID )`.
*
* Because WP_Customize_Manager is not instantiated for customize.php with the `settings_previewed=false`
* argument, settings cannot be reliably saved. Some logic short-circuits if the current value is the
* same as the value being saved. This is particularly true for options via `update_option()`.
*
* By opening an Ajax request, this is avoided and the changeset is published. See #39221.
*/
$nonces = $wp_customize->get_nonces();
$request_args = array(
'nonce' => $nonces['save'],
'customize_changeset_uuid' => $wp_customize->changeset_uuid(),
'wp_customize' => 'on',
'customize_changeset_status' => 'publish',
);
ob_start();
?>
<?php wp_print_scripts( array( 'wp-util' ) ); ?>
<script>
wp.ajax.post( 'customize_save', <?php echo wp_json_encode( $request_args ); ?> );
</script>
<?php
$script = ob_get_clean();
wp_die( wp_die(
'<h1>' . __( 'Your scheduled changes just published' ) . '</h1>' . '<h1>' . __( 'Your scheduled changes just published' ) . '</h1>' .
'<p><a href="' . esc_url( remove_query_arg( 'changeset_uuid' ) ) . '">' . __( 'Customize New Changes' ) . '</a></p>', '<p><a href="' . esc_url( remove_query_arg( 'changeset_uuid' ) ) . '">' . __( 'Customize New Changes' ) . '</a></p>' . $script,
200 200
); );
} }