wp/wp-includes/js/wp-auth-check.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     1 /* global adminpage */
     1 // Interim login dialog
     2 // Interim login dialog
     2 (function($){
     3 (function($){
     3 	var wrap, check, next;
     4 	var wrap, next;
     4 
     5 
     5 	function show() {
     6 	function show() {
     6 		var parent = $('#wp-auth-check'), form = $('#wp-auth-check-form'), noframe = wrap.find('.wp-auth-fallback-expired'), frame, loaded = false;
     7 		var parent = $('#wp-auth-check'),
       
     8 			form = $('#wp-auth-check-form'),
       
     9 			noframe = wrap.find('.wp-auth-fallback-expired'),
       
    10 			frame, loaded = false;
     7 
    11 
     8 		if ( form.length ) {
    12 		if ( form.length ) {
     9 			// Add unload confirmation to counter (frame-busting) JS redirects
    13 			// Add unload confirmation to counter (frame-busting) JS redirects
    10 			$(window).on( 'beforeunload.wp-auth-check', function(e) {
    14 			$(window).on( 'beforeunload.wp-auth-check', function(e) {
    11 				e.originalEvent.returnValue = window.authcheckL10n.beforeunload;
    15 				e.originalEvent.returnValue = window.authcheckL10n.beforeunload;
    12 			});
    16 			});
    13 
    17 
    14 			frame = $('<iframe id="wp-auth-check-frame" frameborder="0">').attr( 'title', noframe.text() );
    18 			frame = $('<iframe id="wp-auth-check-frame" frameborder="0">').attr( 'title', noframe.text() );
    15 			frame.load( function(e) {
    19 			frame.load( function() {
    16 				var height, body;
    20 				var height, body;
    17 
    21 
    18 				loaded = true;
    22 				loaded = true;
    19 
    23 
    20 				try {
    24 				try {
    43 			}).attr( 'src', form.data('src') );
    47 			}).attr( 'src', form.data('src') );
    44 
    48 
    45 			$('#wp-auth-check-form').append( frame );
    49 			$('#wp-auth-check-form').append( frame );
    46 		}
    50 		}
    47 
    51 
       
    52 		$( 'body' ).addClass( 'modal-open' );
    48 		wrap.removeClass('hidden');
    53 		wrap.removeClass('hidden');
    49 
    54 
    50 		if ( frame ) {
    55 		if ( frame ) {
    51 			frame.focus();
    56 			frame.focus();
    52 			// WebKit doesn't throw an error if the iframe fails to load because of "X-Frame-Options: DENY" header.
    57 			// WebKit doesn't throw an error if the iframe fails to load because of "X-Frame-Options: DENY" header.
    65 
    70 
    66 	function hide() {
    71 	function hide() {
    67 		$(window).off( 'beforeunload.wp-auth-check' );
    72 		$(window).off( 'beforeunload.wp-auth-check' );
    68 
    73 
    69 		// When on the Edit Post screen, speed up heartbeat after the user logs in to quickly refresh nonces
    74 		// When on the Edit Post screen, speed up heartbeat after the user logs in to quickly refresh nonces
    70 		if ( typeof adminpage != 'undefined' && ( adminpage == 'post-php' || adminpage == 'post-new-php' )
    75 		if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) &&
    71 			 && typeof wp != 'undefined' && wp.heartbeat ) {
    76 			typeof wp !== 'undefined' && wp.heartbeat ) {
    72 
    77 
    73 			wp.heartbeat.interval( 'fast', 1 );
    78 			wp.heartbeat.connectNow();
    74 		}
    79 		}
    75 
    80 
    76 		wrap.fadeOut( 200, function() {
    81 		wrap.fadeOut( 200, function() {
    77 			wrap.addClass('hidden').css('display', '');
    82 			wrap.addClass('hidden').css('display', '');
    78 			$('#wp-auth-check-frame').remove();
    83 			$('#wp-auth-check-frame').remove();
       
    84 			$( 'body' ).removeClass( 'modal-open' );
    79 		});
    85 		});
    80 	}
    86 	}
    81 
    87 
    82 	function schedule() {
    88 	function schedule() {
    83 		var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180; // in seconds, default 3 min.
    89 		var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180; // in seconds, default 3 min.
    85 	}
    91 	}
    86 
    92 
    87 	$( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) {
    93 	$( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) {
    88 		if ( 'wp-auth-check' in data ) {
    94 		if ( 'wp-auth-check' in data ) {
    89 			schedule();
    95 			schedule();
    90 			if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') )
    96 			if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) {
    91 				show();
    97 				show();
    92 			else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') )
    98 			} else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) {
    93 				hide();
    99 				hide();
       
   100 			}
    94 		}
   101 		}
    95 	}).on( 'heartbeat-send.wp-auth-check', function( e, data ) {
   102 	}).on( 'heartbeat-send.wp-auth-check', function( e, data ) {
    96 		if ( ( new Date() ).getTime() > next )
   103 		if ( ( new Date() ).getTime() > next ) {
    97 			data['wp-auth-check'] = true;
   104 			data['wp-auth-check'] = true;
       
   105 		}
    98 	}).ready( function() {
   106 	}).ready( function() {
    99 		schedule();
   107 		schedule();
   100 		wrap = $('#wp-auth-check-wrap');
   108 		wrap = $('#wp-auth-check-wrap');
   101 		wrap.find('.wp-auth-check-close').on( 'click', function(e) {
   109 		wrap.find('.wp-auth-check-close').on( 'click', function() {
   102 			hide();
   110 			hide();
   103 		});
   111 		});
   104 	});
   112 	});
   105 
   113 
   106 }(jQuery));
   114 }(jQuery));