[ Index ]

PHP Cross Reference of WordPress

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
  55                      // after another page is 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              /*
  72               * WebKit doesn't throw an error if the iframe fails to load
  73               * because of "X-Frame-Options: DENY" header.
  74               * Wait for 10 seconds and switch to the fallback text.
  75               */
  76              setTimeout( function() {
  77                  if ( ! loaded ) {
  78                      wrap.addClass('fallback');
  79                      form.remove();
  80                      noframe.focus();
  81                  }
  82              }, 10000 );
  83          } else {
  84              noframe.focus();
  85          }
  86      }
  87  
  88      /**
  89       * Hides the authentication form popup.
  90       *
  91       * @since 3.6.0
  92       * @private
  93       */
  94  	function hide() {
  95          $(window).off( 'beforeunload.wp-auth-check' );
  96  
  97          // When on the Edit Post screen, speed up heartbeat
  98          // after the user logs in to quickly refresh nonces.
  99          if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) &&
 100              typeof wp !== 'undefined' && wp.heartbeat ) {
 101  
 102              $(document).off( 'heartbeat-tick.wp-auth-check' );
 103              wp.heartbeat.connectNow();
 104          }
 105  
 106          wrap.fadeOut( 200, function() {
 107              wrap.addClass('hidden').css('display', '');
 108              $('#wp-auth-check-frame').remove();
 109              $( 'body' ).removeClass( 'modal-open' );
 110          });
 111      }
 112  
 113      /**
 114       * Schedules when the next time the authentication check will be done.
 115       *
 116       * @since 3.6.0
 117       * @private
 118       */
 119  	function schedule() {
 120          // In seconds, default 3 min.
 121          var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180;
 122          next = ( new Date() ).getTime() + ( interval * 1000 );
 123      }
 124  
 125      /**
 126       * Binds to the Heartbeat Tick event.
 127       *
 128       * - Shows the authentication form popup if user is not logged in.
 129       * - Hides the authentication form popup if it is already visible and user is
 130       *   logged in.
 131       *
 132       * @ignore
 133       *
 134       * @since 3.6.0
 135       *
 136       * @param {Object} e The heartbeat-tick event that has been triggered.
 137       * @param {Object} data Response data.
 138       */
 139      $( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) {
 140          if ( 'wp-auth-check' in data ) {
 141              schedule();
 142              if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) {
 143                  show();
 144              } else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) {
 145                  hide();
 146              }
 147          }
 148  
 149      /**
 150       * Binds to the Heartbeat Send event.
 151       *
 152       * @ignore
 153       *
 154       * @since 3.6.0
 155       *
 156       * @param {Object} e The heartbeat-send event that has been triggered.
 157       * @param {Object} data Response data.
 158       */
 159      }).on( 'heartbeat-send.wp-auth-check', function( e, data ) {
 160          if ( ( new Date() ).getTime() > next ) {
 161              data['wp-auth-check'] = true;
 162          }
 163  
 164      }).ready( function() {
 165          schedule();
 166  
 167          /**
 168           * Hides the authentication form popup when the close icon is clicked.
 169           *
 170           * @ignore
 171           *
 172           * @since 3.6.0
 173           */
 174          wrap = $('#wp-auth-check-wrap');
 175          wrap.find('.wp-auth-check-close').on( 'click', function() {
 176              hide();
 177          });
 178      });
 179  
 180  }(jQuery));


Generated: Wed Apr 1 01:00:04 2020 Cross-referenced by PHPXref 0.7.1