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