WordPress.org

Make WordPress Core

Changeset 37701


Ignore:
Timestamp:
06/14/2016 07:27:41 PM (4 years ago)
Author:
westonruter
Message:

Customize: Ensure MediaControl fetches the necessary attachment data for rendering when dynamically added via JS.

Fixes #36521.
Props TimothyBlynJacobs, westonruter.

File:
1 edited

Legend:

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

    r37700 r37701  
    19291929                });
    19301930
    1931             control.setting.bind( function( value ) {
    1932 
    1933                 // Send attachment information to the preview for possible use in `postMessage` transport.
    1934                 wp.media.attachment( value ).fetch().done( function() {
    1935                     wp.customize.previewer.send( control.setting.id + '-attachment-data', this.attributes );
     1931            /**
     1932             * Set attachment data and render content.
     1933             *
     1934             * Note that BackgroundImage.prototype.ready applies this ready method
     1935             * to itself. Since BackgroundImage is an UploadControl, the value
     1936             * is the attachment URL instead of the attachment ID. In this case
     1937             * we skip fetching the attachment data because we have no ID available,
     1938             * and it is the responsibility of the UploadControl to set the control's
     1939             * attachmentData before calling the renderContent method.
     1940             *
     1941             * @param {number|string} value Attachment
     1942             */
     1943            function setAttachmentDataAndRenderContent( value ) {
     1944                var hasAttachmentData = $.Deferred();
     1945
     1946                if ( control.extended( api.UploadControl ) ) {
     1947                    hasAttachmentData.resolve();
     1948                } else {
     1949                    value = parseInt( value, 10 );
     1950                    if ( _.isNaN( value ) || value <= 0 ) {
     1951                        delete control.params.attachment;
     1952                        hasAttachmentData.resolve();
     1953                    } else if ( control.params.attachment && control.params.attachment.id === value ) {
     1954                        hasAttachmentData.resolve();
     1955                    }
     1956                }
     1957
     1958                // Fetch the attachment data.
     1959                if ( 'pending' === hasAttachmentData.state() ) {
     1960                    wp.media.attachment( value ).fetch().done( function() {
     1961                        control.params.attachment = this.attributes;
     1962                        hasAttachmentData.resolve();
     1963
     1964                        // Send attachment information to the preview for possible use in `postMessage` transport.
     1965                        wp.customize.previewer.send( control.setting.id + '-attachment-data', this.attributes );
     1966                    } );
     1967                }
     1968
     1969                hasAttachmentData.done( function() {
     1970                    control.renderContent();
    19361971                } );
    1937 
    1938                 // Re-render whenever the control's setting changes.
    1939                 control.renderContent();
    1940             } );
     1972            }
     1973
     1974            // Ensure attachment data is initially set (for dynamically-instantiated controls).
     1975            setAttachmentDataAndRenderContent( control.setting() );
     1976
     1977            // Update the attachment data and re-render the control when the setting changes.
     1978            control.setting.bind( setAttachmentDataAndRenderContent );
    19411979        },
    19421980
Note: See TracChangeset for help on using the changeset viewer.