Make WordPress Core


Ignore:
Timestamp:
08/02/2017 05:34:32 AM (9 years ago)
Author:
westonruter
Message:

Customize: Introduce settings_previewed arg and getter on WP_Customize_Manager which controls whether WP_Customize_Setting::preview() should be called on settings.

The settings_previewed property eliminates the need for the Customizer components from having to look at global doing_ajax state. This is in particular needed when saving settings, as some settings will short-circuit the update operation if they detect no changes are introduced. This is also needed for low-level integrations with the Customizer, such as in REST API endpoints under development.

Props stubgo, westonruter, utkarshpatel for testing.
See #38900.
Fixes #39221.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r41162 r41205  
    175175
    176176        /**
     177         * Whether settings should be previewed.
     178         *
     179         * @since 4.9.0
     180         * @var bool
     181         */
     182        protected $settings_previewed;
     183
     184        /**
    177185         * Unsanitized values for Customize Settings parsed from $_POST['customized'].
    178186         *
    … …  
    214222         *     Args.
    215223         *
    216          *     @type string $changeset_uuid    Changeset UUID, the post_name for the customize_changeset post containing the customized state. Defaults to new UUID.
    217          *     @type string $theme             Theme to be previewed (for theme switch). Defaults to customize_theme or theme query params.
    218          *     @type string $messenger_channel Messenger channel. Defaults to customize_messenger_channel query param.
     224         *     @type string $changeset_uuid     Changeset UUID, the post_name for the customize_changeset post containing the customized state. Defaults to new UUID.
     225         *     @type string $theme              Theme to be previewed (for theme switch). Defaults to customize_theme or theme query params.
     226         *     @type string $messenger_channel  Messenger channel. Defaults to customize_messenger_channel query param.
     227         *     @type bool   $settings_previewed If settings should be previewed. Defaults to true.
    219228         * }
    220229         */
    … …  
    222231
    223232                $args = array_merge(
    224                         array_fill_keys( array( 'changeset_uuid', 'theme', 'messenger_channel' ), null ),
     233                        array_fill_keys( array( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed' ), null ),
    225234                        $args
    226235                );
    … …  
    243252                }
    244253
     254                if ( ! isset( $args['settings_previewed'] ) ) {
     255                        $args['settings_previewed'] = true;
     256                }
     257
    245258                $this->original_stylesheet = get_stylesheet();
    246259                $this->theme = wp_get_theme( $args['theme'] );
    247260                $this->messenger_channel = $args['messenger_channel'];
     261                $this->settings_previewed = ! empty( $args['settings_previewed'] );
    248262                $this->_changeset_uuid = $args['changeset_uuid'];
    249263
    … …  
    623637
    624638        /**
     639         * Gets whether settings are or will be previewed.
     640         *
     641         * @since 4.9.0
     642         * @see WP_Customize_Setting::preview()
     643         *
     644         * @return bool
     645         */
     646        public function settings_previewed() {
     647                return $this->settings_previewed;
     648        }
     649
     650        /**
    625651         * Get the changeset UUID.
    626652         *
    … …  
    729755                do_action( 'customize_register', $this );
    730756
    731                 /*
    732                  * Note that settings must be previewed here even outside the customizer preview
    733                  * and also in the customizer pane itself. This is to enable loading an existing
    734                  * changeset into the customizer. Previewing the settings only has to be prevented
    735                  * in the case of a customize_save action because then update_option()
    736                  * may short-circuit because it will detect that there are no changes to
    737                  * make.
    738                  */
    739                 if ( ! $this->doing_ajax( 'customize_save' ) ) {
     757                if ( $this->settings_previewed() ) {
    740758                        foreach ( $this->settings as $setting ) {
    741759                                $setting->preview();
Note: See TracChangeset for help on using the changeset viewer.