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

Conversation

@westonruter
Copy link
Contributor

@westonruter westonruter commented Jun 5, 2016

This fixes an issue whereby:

  1. You change a featured image.
  2. You hit Save & Publish.
  3. You see the featured image refresh in the preview once saved.

The reason for the secondary refresh of the featured image is because I incorrectly used WP_Customize_Setting::value() as opposed to WP_Customize_Setting::js_value(). For postmeta containing post IDs for media controls like Featured Image, this is particularly important because all scalar postmeta (e.g. numbers and bools) get saved as strings (since a meta_value is a TEXT field). We implement a js_value to cast the underlying string attachment ID from a string to an integer for JS. Since I didn't do this, when the Customizer saves and it sends back the sanitized saved values to update in the JS models, any integer post IDs get replaced with string post IDs. The result is the secondary partial refresh.

If, on the other hand, a refresh is done due to there not being a partial (and so falling back to refesh), the resulting behavior at saving is an infinite reload, since the Customizer keeps trying to sync a string value into the preview where there is an integer value already exported, triggering a change on that setting value.


To prevent partial refresh from causing an infinite reload this patch should also be applied to core (see #37032), as it prevents any setting changes from triggering a partial refresh until active has been received (after the initial setting sync):

diff --git src/wp-includes/js/customize-selective-refresh.js src/wp-includes/js/customize-selective-refresh.js
index 7efee3d..9a11b24 100644
--- src/wp-includes/js/customize-selective-refresh.js
+++ src/wp-includes/js/customize-selective-refresh.js
@@ -2,7 +2,7 @@

 wp.customize.selectiveRefresh = ( function( $, api ) {
    'use strict';
-   var self, Partial, Placement;
+   var self, Partial, Placement, active = false;

    self = {
        ready: $.Deferred(),
@@ -779,6 +779,9 @@ wp.customize.selectiveRefresh = ( function( $, api ) {
         */
        handleSettingChange = function( newValue, oldValue ) {
            var setting = this;
+           if ( ! active ) {
+               return;
+           }
            self.partial.each( function( partial ) {
                if ( partial.isRelatedSetting( setting, newValue, oldValue ) ) {
                    partial.refresh();
@@ -848,6 +851,7 @@ wp.customize.selectiveRefresh = ( function( $, api ) {
        } );

        api.preview.bind( 'active', function() {
+           active = true;

            // Make all partials ready.
            self.partial.each( function( partial ) {

@westonruter westonruter added this to the 0.6.1 milestone Jun 5, 2016
@westonruter westonruter added the bug label Jun 6, 2016
@valendesigns valendesigns merged commit ec4b01f into develop Jun 9, 2016
@valendesigns valendesigns deleted the bugfix/send-js-value branch August 23, 2017 02:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants