home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / js / wp-auth-check.js < prev    next >
Encoding:
JavaScript  |  2016-01-13  |  3.2 KB  |  118 lines

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