[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7