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.

171 lines
4.1 KiB

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