/wp-includes/js/wp-auth-check.js

https://github.com/ngocphuuit/wordpress-tax · JavaScript · 112 lines · 89 code · 16 blank · 7 comment · 22 complexity · f1d3e9e205b2c0fecfd16283630f1a2b MD5 · raw file

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