[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/ -> wp-auth-check.js (source)

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


Generated : Sat Sep 19 08:20:30 2026 Cross-referenced by PHPXref