Make WordPress Core


Ignore:
Timestamp:
11/09/2017 04:43:09 AM (7 years ago)
Author:
westonruter
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/customize.php

    r41839 r42138  
    4343    );
    4444    if ( $missed_schedule ) {
    45         wp_publish_post( $changeset_post->ID );
     45        /*
     46         * Note that an Ajax request spawns here instead of just calling `wp_publish_post( $changeset_post->ID )`.
     47         *
     48         * Because WP_Customize_Manager is not instantiated for customize.php with the `settings_previewed=false`
     49         * argument, settings cannot be reliably saved. Some logic short-circuits if the current value is the
     50         * same as the value being saved. This is particularly true for options via `update_option()`.
     51         *
     52         * By opening an Ajax request, this is avoided and the changeset is published. See #39221.
     53         */
     54        $nonces = $wp_customize->get_nonces();
     55        $request_args = array(
     56            'nonce' => $nonces['save'],
     57            'customize_changeset_uuid' => $wp_customize->changeset_uuid(),
     58            'wp_customize' => 'on',
     59            'customize_changeset_status' => 'publish',
     60        );
     61        ob_start();
     62        ?>
     63        <?php wp_print_scripts( array( 'wp-util' ) ); ?>
     64        <script>
     65            wp.ajax.post( 'customize_save', <?php echo wp_json_encode( $request_args ); ?> );
     66        </script>
     67        <?php
     68        $script = ob_get_clean();
     69
    4670        wp_die(
    4771            '<h1>' . __( 'Your scheduled changes just published' ) . '</h1>' .
    48             '<p><a href="' . esc_url( remove_query_arg( 'changeset_uuid' ) ) . '">' . __( 'Customize New Changes' ) . '</a></p>',
     72            '<p><a href="' . esc_url( remove_query_arg( 'changeset_uuid' ) ) . '">' . __( 'Customize New Changes' ) . '</a></p>' . $script,
    4973            200
    5074        );
Note: See TracChangeset for help on using the changeset viewer.