wp/wp-includes/js/wp-auth-check.js
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
       
     1 /**
       
     2  * Interim login dialog.
       
     3  *
       
     4  * @output wp-includes/js/wp-auth-check.js
       
     5  */
       
     6 
     1 /* global adminpage */
     7 /* global adminpage */
     2 // Interim login dialog
       
     3 (function($){
     8 (function($){
     4 	var wrap, next;
     9 	var wrap, next;
     5 
    10 
       
    11 	/**
       
    12 	 * Shows the authentication form popup.
       
    13 	 *
       
    14 	 * @since 3.6.0
       
    15 	 * @private
       
    16 	 */
     6 	function show() {
    17 	function show() {
     7 		var parent = $('#wp-auth-check'),
    18 		var parent = $('#wp-auth-check'),
     8 			form = $('#wp-auth-check-form'),
    19 			form = $('#wp-auth-check-form'),
     9 			noframe = wrap.find('.wp-auth-fallback-expired'),
    20 			noframe = wrap.find('.wp-auth-fallback-expired'),
    10 			frame, loaded = false;
    21 			frame, loaded = false;
    11 
    22 
    12 		if ( form.length ) {
    23 		if ( form.length ) {
    13 			// Add unload confirmation to counter (frame-busting) JS redirects
    24 			// Add unload confirmation to counter (frame-busting) JS redirects.
    14 			$(window).on( 'beforeunload.wp-auth-check', function(e) {
    25 			$(window).on( 'beforeunload.wp-auth-check', function(e) {
    15 				e.originalEvent.returnValue = window.authcheckL10n.beforeunload;
    26 				e.originalEvent.returnValue = window.authcheckL10n.beforeunload;
    16 			});
    27 			});
    17 
    28 
    18 			frame = $('<iframe id="wp-auth-check-frame" frameborder="0">').attr( 'title', noframe.text() );
    29 			frame = $('<iframe id="wp-auth-check-frame" frameborder="0">').attr( 'title', noframe.text() );
    38 					if ( body && body.hasClass('interim-login-success') )
    49 					if ( body && body.hasClass('interim-login-success') )
    39 						hide();
    50 						hide();
    40 					else
    51 					else
    41 						parent.css( 'max-height', height + 40 + 'px' );
    52 						parent.css( 'max-height', height + 40 + 'px' );
    42 				} else if ( ! body || ! body.length ) {
    53 				} else if ( ! body || ! body.length ) {
    43 					// Catch "silent" iframe origin exceptions in WebKit after another page is loaded in the iframe
    54 					// Catch "silent" iframe origin exceptions in WebKit after another page is
       
    55 					// loaded in the iframe.
    44 					wrap.addClass('fallback');
    56 					wrap.addClass('fallback');
    45 					parent.css( 'max-height', '' );
    57 					parent.css( 'max-height', '' );
    46 					form.remove();
    58 					form.remove();
    47 					noframe.focus();
    59 					noframe.focus();
    48 				}
    60 				}
    54 		$( 'body' ).addClass( 'modal-open' );
    66 		$( 'body' ).addClass( 'modal-open' );
    55 		wrap.removeClass('hidden');
    67 		wrap.removeClass('hidden');
    56 
    68 
    57 		if ( frame ) {
    69 		if ( frame ) {
    58 			frame.focus();
    70 			frame.focus();
    59 			// WebKit doesn't throw an error if the iframe fails to load because of "X-Frame-Options: DENY" header.
    71 			// WebKit doesn't throw an error if the iframe fails to load because of
       
    72 			// "X-Frame-Options: DENY" header.
    60 			// Wait for 10 sec. and switch to the fallback text.
    73 			// Wait for 10 sec. and switch to the fallback text.
    61 			setTimeout( function() {
    74 			setTimeout( function() {
    62 				if ( ! loaded ) {
    75 				if ( ! loaded ) {
    63 					wrap.addClass('fallback');
    76 					wrap.addClass('fallback');
    64 					form.remove();
    77 					form.remove();
    68 		} else {
    81 		} else {
    69 			noframe.focus();
    82 			noframe.focus();
    70 		}
    83 		}
    71 	}
    84 	}
    72 
    85 
       
    86 	/**
       
    87 	 * Hides the authentication form popup.
       
    88 	 *
       
    89 	 * @since 3.6.0
       
    90 	 * @private
       
    91 	 */
    73 	function hide() {
    92 	function hide() {
    74 		$(window).off( 'beforeunload.wp-auth-check' );
    93 		$(window).off( 'beforeunload.wp-auth-check' );
    75 
    94 
    76 		// When on the Edit Post screen, speed up heartbeat after the user logs in to quickly refresh nonces
    95 		// When on the Edit Post screen, speed up heartbeat after the user logs in to
       
    96 		// quickly refresh nonces.
    77 		if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) &&
    97 		if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) &&
    78 			typeof wp !== 'undefined' && wp.heartbeat ) {
    98 			typeof wp !== 'undefined' && wp.heartbeat ) {
    79 
    99 
    80 			$(document).off( 'heartbeat-tick.wp-auth-check' );
   100 			$(document).off( 'heartbeat-tick.wp-auth-check' );
    81 			wp.heartbeat.connectNow();
   101 			wp.heartbeat.connectNow();
    86 			$('#wp-auth-check-frame').remove();
   106 			$('#wp-auth-check-frame').remove();
    87 			$( 'body' ).removeClass( 'modal-open' );
   107 			$( 'body' ).removeClass( 'modal-open' );
    88 		});
   108 		});
    89 	}
   109 	}
    90 
   110 
       
   111 	/**
       
   112 	 * Schedules when the next time the authentication check will be done.
       
   113 	 *
       
   114 	 * @since 3.6.0
       
   115 	 * @private
       
   116 	 */
    91 	function schedule() {
   117 	function schedule() {
    92 		var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180; // in seconds, default 3 min.
   118 		// In seconds, default 3 min.
       
   119 		var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180;
    93 		next = ( new Date() ).getTime() + ( interval * 1000 );
   120 		next = ( new Date() ).getTime() + ( interval * 1000 );
    94 	}
   121 	}
    95 
   122 
       
   123 	/**
       
   124 	 * Binds to the Heartbeat Tick event.
       
   125 	 *
       
   126 	 * - Shows the authentication form popup if user is not logged in.
       
   127 	 * - Hides the authentication form popup if it is already visible and user is
       
   128 	 *   logged in.
       
   129 	 *
       
   130 	 * @ignore
       
   131 	 *
       
   132 	 * @since 3.6.0
       
   133 	 *
       
   134 	 * @param {Object} e The heartbeat-tick event that has been triggered.
       
   135 	 * @param {Object} data Response data.
       
   136 	 */
    96 	$( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) {
   137 	$( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) {
    97 		if ( 'wp-auth-check' in data ) {
   138 		if ( 'wp-auth-check' in data ) {
    98 			schedule();
   139 			schedule();
    99 			if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) {
   140 			if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) {
   100 				show();
   141 				show();
   101 			} else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) {
   142 			} else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) {
   102 				hide();
   143 				hide();
   103 			}
   144 			}
   104 		}
   145 		}
       
   146 
       
   147 	/**
       
   148 	 * Binds to the Heartbeat Send event.
       
   149 	 *
       
   150 	 * @ignore
       
   151 	 *
       
   152 	 * @since 3.6.0
       
   153 	 *
       
   154 	 * @param {Object} e The heartbeat-send event that has been triggered.
       
   155 	 * @param {Object} data Response data.
       
   156 	 */
   105 	}).on( 'heartbeat-send.wp-auth-check', function( e, data ) {
   157 	}).on( 'heartbeat-send.wp-auth-check', function( e, data ) {
   106 		if ( ( new Date() ).getTime() > next ) {
   158 		if ( ( new Date() ).getTime() > next ) {
   107 			data['wp-auth-check'] = true;
   159 			data['wp-auth-check'] = true;
   108 		}
   160 		}
       
   161 
   109 	}).ready( function() {
   162 	}).ready( function() {
   110 		schedule();
   163 		schedule();
       
   164 
       
   165 		/**
       
   166 		 * Hides the authentication form popup when the close icon is clicked.
       
   167 		 *
       
   168 		 * @ignore
       
   169 		 *
       
   170 		 * @since 3.6.0
       
   171 		 */
   111 		wrap = $('#wp-auth-check-wrap');
   172 		wrap = $('#wp-auth-check-wrap');
   112 		wrap.find('.wp-auth-check-close').on( 'click', function() {
   173 		wrap.find('.wp-auth-check-close').on( 'click', function() {
   113 			hide();
   174 			hide();
   114 		});
   175 		});
   115 	});
   176 	});