author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
5 | 1 |
/* global adminpage */ |
0 | 2 |
// Interim login dialog |
3 |
(function($){ |
|
5 | 4 |
var wrap, next; |
0 | 5 |
|
6 |
function show() { |
|
5 | 7 |
var parent = $('#wp-auth-check'), |
8 |
form = $('#wp-auth-check-form'), |
|
9 |
noframe = wrap.find('.wp-auth-fallback-expired'), |
|
10 |
frame, loaded = false; |
|
0 | 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() ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
frame.on( 'load', function() { |
0 | 20 |
var height, body; |
21 |
||
22 |
loaded = true; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
// Remove the spinner to avoid unnecessary CPU/GPU usage. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
form.removeClass( 'loading' ); |
0 | 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 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
form.append( frame ); |
0 | 52 |
} |
53 |
||
5 | 54 |
$( 'body' ).addClass( 'modal-open' ); |
0 | 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 |
|
5 | 77 |
if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) && |
78 |
typeof wp !== 'undefined' && wp.heartbeat ) { |
|
0 | 79 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
$(document).off( 'heartbeat-tick.wp-auth-check' ); |
5 | 81 |
wp.heartbeat.connectNow(); |
0 | 82 |
} |
83 |
||
84 |
wrap.fadeOut( 200, function() { |
|
85 |
wrap.addClass('hidden').css('display', ''); |
|
86 |
$('#wp-auth-check-frame').remove(); |
|
5 | 87 |
$( 'body' ).removeClass( 'modal-open' ); |
0 | 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(); |
|
5 | 99 |
if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) { |
0 | 100 |
show(); |
5 | 101 |
} else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) { |
0 | 102 |
hide(); |
5 | 103 |
} |
0 | 104 |
} |
105 |
}).on( 'heartbeat-send.wp-auth-check', function( e, data ) { |
|
5 | 106 |
if ( ( new Date() ).getTime() > next ) { |
0 | 107 |
data['wp-auth-check'] = true; |
5 | 108 |
} |
0 | 109 |
}).ready( function() { |
110 |
schedule(); |
|
111 |
wrap = $('#wp-auth-check-wrap'); |
|
5 | 112 |
wrap.find('.wp-auth-check-close').on( 'click', function() { |
0 | 113 |
hide(); |
114 |
}); |
|
115 |
}); |
|
116 |
||
117 |
}(jQuery)); |