Make WordPress Core

Changeset 31062


Ignore:
Timestamp:
01/06/2015 09:46:54 PM (12 years ago)
Author:
ocean90
Message:

Customizer: Send JSON success for customize_save and allow response to be filtered.

props westonruter.
fixes #29098.

Location:
trunk/src
Files:
2 edited

Legend:

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

    r30991 r31062  
    19461946                                var self  = this,
    19471947                                        query = $.extend( this.query(), {
    1948                                                 action: 'customize_save',
    19491948                                                nonce:  this.nonce.save
    19501949                                        } ),
     
    19561955
    19571956                                submit = function () {
    1958                                         var request = $.post( api.settings.url.ajax, query );
     1957                                        var request = wp.ajax.post( 'customize_save', query );
    19591958
    19601959                                        api.trigger( 'save', request );
     
    19641963                                        } );
    19651964
    1966                                         request.done( function( response ) {
    1967                                                 // Check if the user is logged out.
     1965                                        request.fail( function ( response ) {
    19681966                                                if ( '0' === response ) {
     1967                                                        response = 'not_logged_in';
     1968                                                } else if ( '-1' === response ) {
     1969                                                        // Back-compat in case any other check_ajax_referer() call is dying
     1970                                                        response = 'invalid_nonce';
     1971                                                }
     1972
     1973                                                if ( 'invalid_nonce' === response ) {
     1974                                                        self.cheatin();
     1975                                                } else if ( 'not_logged_in' === response ) {
    19691976                                                        self.preview.iframe.hide();
    19701977                                                        self.login().done( function() {
     
    19721979                                                                self.preview.iframe.show();
    19731980                                                        } );
    1974                                                         return;
    19751981                                                }
    1976 
    1977                                                 // Check for cheaters.
    1978                                                 if ( '-1' === response ) {
    1979                                                         self.cheatin();
    1980                                                         return;
    1981                                                 }
    1982 
     1982                                                api.trigger( 'error', response );
     1983                                        } );
     1984
     1985                                        request.done( function( response ) {
    19831986                                                // Clear setting dirty states
    19841987                                                api.each( function ( value ) {
    19851988                                                        value._dirty = false;
    19861989                                                } );
    1987                                                 api.trigger( 'saved' );
     1990
     1991                                                api.trigger( 'saved', response );
    19881992                                        } );
    19891993                                };
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r30885 r31062  
    629629         */
    630630        public function save() {
    631                 if ( ! $this->is_preview() )
    632                         die;
    633 
    634                 check_ajax_referer( 'save-customize_' . $this->get_stylesheet(), 'nonce' );
     631                if ( ! $this->is_preview() ) {
     632                        wp_send_json_error( 'not_preview' );
     633                }
     634
     635                $action = 'save-customize_' . $this->get_stylesheet();
     636                if ( ! check_ajax_referer( $action, 'nonce', false ) ) {
     637                        wp_send_json_error( 'invalid_nonce' );
     638                }
    635639
    636640                // Do we have to switch themes?
     
    667671                do_action( 'customize_save_after', $this );
    668672
    669                 die;
     673                /**
     674                 * Filter response data for a successful customize_save Ajax request.
     675                 *
     676                 * This filter does not apply if there was a nonce or authentication failure.
     677                 *
     678                 * @since 4.2.0
     679                 *
     680                 * @param array $data
     681                 * @param WP_Customize_Manager $this WP_Customize_Manager instance.
     682                 */
     683                $response = apply_filters( 'customize_save_response', array(), $this );
     684                wp_send_json_success( $response );
    670685        }
    671686
Note: See TracChangeset for help on using the changeset viewer.