diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php
index 099a66f64a..db44df78c9 100644
--- a/wp-admin/admin-ajax.php
+++ b/wp-admin/admin-ajax.php
@@ -38,7 +38,7 @@ do_action( 'admin_init' );
$core_actions_get = array(
'fetch-list', 'ajax-tag-search', 'wp-compression-test', 'imgedit-preview', 'oembed-cache',
- 'autocomplete-user', 'dashboard-widgets',
+ 'autocomplete-user', 'dashboard-widgets', 'logged-in',
);
$core_actions_post = array(
diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php
index 9666ce7983..5ced90166f 100644
--- a/wp-admin/includes/ajax-actions.php
+++ b/wp-admin/includes/ajax-actions.php
@@ -212,6 +212,10 @@ function wp_ajax_dashboard_widgets() {
wp_die();
}
+function wp_ajax_logged_in() {
+ wp_die( 1 );
+}
+
/*
* Ajax helper.
*/
diff --git a/wp-admin/js/customize-controls.dev.js b/wp-admin/js/customize-controls.dev.js
index 1318e26c79..cb4e6e20a6 100644
--- a/wp-admin/js/customize-controls.dev.js
+++ b/wp-admin/js/customize-controls.dev.js
@@ -288,9 +288,7 @@
sensitivity: 2000,
initialize: function( params, options ) {
- var loaded = false,
- ready = false,
- deferred = $.Deferred(),
+ var deferred = $.Deferred(),
self = this;
// This is the promise object.
@@ -304,18 +302,31 @@
this.add( 'previewUrl', params.previewUrl );
- this.bind( 'ready', function() {
+ this.query = $.extend( params.query || {}, { customize_messenger_channel: this.channel() });
+
+ this.run( deferred );
+ },
+
+ run: function( deferred ) {
+ var self = this,
+ loaded = false,
+ ready = false;
+
+ if ( this._ready )
+ this.unbind( 'ready', this._ready );
+
+ this._ready = function() {
ready = true;
if ( loaded )
deferred.resolveWith( self );
- });
+ };
- params.query = $.extend( params.query || {}, { customize_messenger_channel: this.channel() });
+ this.bind( 'ready', this._ready );
this.request = $.ajax( this.previewUrl(), {
type: 'POST',
- data: params.query,
+ data: this.query,
xhrFields: {
withCredentials: true
}
@@ -339,7 +350,7 @@
// Check if the user is not logged in.
if ( '0' === response ) {
- deferred.rejectWith( self, [ 'logged out' ] );
+ self.login( deferred );
return;
}
@@ -359,10 +370,6 @@
// Strip the signature from the request.
response = response.slice( 0, index ) + response.slice( index + signature.length );
- // Create the iframe and inject the html content.
- // Strip the signature from the request.
- response = response.slice( 0, index ) + response.slice( index + signature.length );
-
// Create the iframe and inject the html content.
self.iframe = $('').appendTo( self.previewer.container );
@@ -388,6 +395,37 @@
});
},
+ login: function( deferred ) {
+ var self = this,
+ reject;
+
+ reject = function() {
+ deferred.rejectWith( self, [ 'logged out' ] );
+ };
+
+ if ( this.triedLogin )
+ return reject();
+
+ // Check if we have an admin cookie.
+ $.get( api.settings.url.ajax, {
+ action: 'logged-in'
+ }).fail( reject ).done( function( response ) {
+ var iframe;
+
+ if ( '1' !== response )
+ reject();
+
+ iframe = $('').hide();
+ iframe.appendTo( self.previewer.container );
+ iframe.load( function() {
+ self.triedLogin = true;
+
+ iframe.remove();
+ self.run( deferred );
+ });
+ });
+ },
+
destroy: function() {
api.Messenger.prototype.destroy.call( this );
this.request.abort();