WordPress.org

Make WordPress Core

Ticket #39487: 39487.diff

File 39487.diff, 2.6 KB (added by dlh, 6 months ago)
  • src/wp-includes/customize/class-wp-customize-selective-refresh.php

    diff --git src/wp-includes/customize/class-wp-customize-selective-refresh.php src/wp-includes/customize/class-wp-customize-selective-refresh.php
    index 0b5c446131..b0a8f77452 100644
    final class WP_Customize_Selective_Refresh { 
    6565                require_once( ABSPATH . WPINC . '/customize/class-wp-customize-partial.php' );
    6666
    6767                add_action( 'customize_preview_init', array( $this, 'init_preview' ) );
     68                add_action( 'customize_preview_init', [ $this, 'check_transport_of_settings_with_partials' ] );
     69                add_action( 'customize_controls_init', [ $this, 'check_transport_of_settings_with_partials' ] );
    6870        }
    6971
    7072        /**
    final class WP_Customize_Selective_Refresh { 
    7274         *
    7375         * @since 4.5.0
    7476         *
    75          * @return array Partials.
     77         * @return WP_Customize_Partial[] Partials.
    7678         */
    7779        public function partials() {
    7880                return $this->partials;
    final class WP_Customize_Selective_Refresh { 
    211213                echo sprintf( '<script>var _customizePartialRefreshExports = %s;</script>', wp_json_encode( $exports ) );
    212214        }
    213215
     216        /**
     217         * Ensure that settings associated with partials use the 'postMessage' transport.
     218         */
     219        public function check_transport_of_settings_with_partials() {
     220                foreach ( $this->partials() as $partial ) {
     221                        /*
     222                         * It's possible to register a partial for a setting while intending
     223                         * to use the partial in only certain circumstances. Before
     224                         * enforcing the 'postMessage' transport for such settings, check
     225                         * that the partial is configured to request a full refresh of the
     226                         * preview if the selective refresh fails because the partial isn't
     227                         * actually used in the preview.
     228                         */
     229                        if ( ! $partial->fallback_refresh ) {
     230                                continue;
     231                        }
     232
     233                        foreach ( $partial->settings as $setting_id ) {
     234                                $setting = $this->manager->get_setting( $setting_id );
     235
     236                                if ( $setting instanceof WP_Customize_Setting ) {
     237                                        // Change only the 'refresh' transport, not custom transports.
     238                                        if ( 'refresh' !== $setting->transport ) {
     239                                                continue;
     240                                        }
     241
     242                                        /*
     243                                         * If the partial is unsupported, don't change any settings.
     244                                         *
     245                                         * Save this check until the end to avoid unnecessarily
     246                                         * re-checking the capabilities for partials and all their
     247                                         * settings when the settings already use 'postMessage'.
     248                                         */
     249                                        if ( ! $partial->check_capabilities() ) {
     250                                                continue 2;
     251                                        }
     252
     253                                        $setting->transport = 'postMessage';
     254                                }
     255                        }
     256                }
     257        }
     258
    214259        /**
    215260         * Registers dynamically-created partials.
    216261         *