You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

118 lines
3.2 KiB

  1. /* global adminpage */
  2. // Interim login dialog
  3. (function($){
  4. var wrap, next;
  5. function show() {
  6. var parent = $('#wp-auth-check'),
  7. form = $('#wp-auth-check-form'),
  8. noframe = wrap.find('.wp-auth-fallback-expired'),
  9. frame, loaded = false;
  10. if ( form.length ) {
  11. // Add unload confirmation to counter (frame-busting) JS redirects
  12. $(window).on( 'beforeunload.wp-auth-check', function(e) {
  13. e.originalEvent.returnValue = window.authcheckL10n.beforeunload;
  14. });
  15. frame = $('<iframe id="wp-auth-check-frame" frameborder="0">').attr( 'title', noframe.text() );
  16. frame.on( 'load', function() {
  17. var height, body;
  18. loaded = true;
  19. // Remove the spinner to avoid unnecessary CPU/GPU usage.
  20. form.removeClass( 'loading' );
  21. try {
  22. body = $(this).contents().find('body');
  23. height = body.height();
  24. } catch(e) {
  25. wrap.addClass('fallback');
  26. parent.css( 'max-height', '' );
  27. form.remove();
  28. noframe.focus();
  29. return;
  30. }
  31. if ( height ) {
  32. if ( body && body.hasClass('interim-login-success') )
  33. hide();
  34. else
  35. parent.css( 'max-height', height + 40 + 'px' );
  36. } else if ( ! body || ! body.length ) {
  37. // Catch "silent" iframe origin exceptions in WebKit after another page is loaded in the iframe
  38. wrap.addClass('fallback');
  39. parent.css( 'max-height', '' );
  40. form.remove();
  41. noframe.focus();
  42. }
  43. }).attr( 'src', form.data('src') );
  44. form.append( frame );
  45. }
  46. $( 'body' ).addClass( 'modal-open' );
  47. wrap.removeClass('hidden');
  48. if ( frame ) {
  49. frame.focus();
  50. // WebKit doesn't throw an error if the iframe fails to load because of "X-Frame-Options: DENY" header.
  51. // Wait for 10 sec. and switch to the fallback text.
  52. setTimeout( function() {
  53. if ( ! loaded ) {
  54. wrap.addClass('fallback');
  55. form.remove();
  56. noframe.focus();
  57. }
  58. }, 10000 );
  59. } else {
  60. noframe.focus();
  61. }
  62. }
  63. function hide() {
  64. $(window).off( 'beforeunload.wp-auth-check' );
  65. // When on the Edit Post screen, speed up heartbeat after the user logs in to quickly refresh nonces
  66. if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) &&
  67. typeof wp !== 'undefined' && wp.heartbeat ) {
  68. $(document).off( 'heartbeat-tick.wp-auth-check' );
  69. wp.heartbeat.connectNow();
  70. }
  71. wrap.fadeOut( 200, function() {
  72. wrap.addClass('hidden').css('display', '');
  73. $('#wp-auth-check-frame').remove();
  74. $( 'body' ).removeClass( 'modal-open' );
  75. });
  76. }
  77. function schedule() {
  78. var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180; // in seconds, default 3 min.
  79. next = ( new Date() ).getTime() + ( interval * 1000 );
  80. }
  81. $( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) {
  82. if ( 'wp-auth-check' in data ) {
  83. schedule();
  84. if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) {
  85. show();
  86. } else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) {
  87. hide();
  88. }
  89. }
  90. }).on( 'heartbeat-send.wp-auth-check', function( e, data ) {
  91. if ( ( new Date() ).getTime() > next ) {
  92. data['wp-auth-check'] = true;
  93. }
  94. }).ready( function() {
  95. schedule();
  96. wrap = $('#wp-auth-check-wrap');
  97. wrap.find('.wp-auth-check-close').on( 'click', function() {
  98. hide();
  99. });
  100. });
  101. }(jQuery));