author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* Interim login dialog. |
|
3 |
* |
|
4 |
* @output wp-includes/js/wp-auth-check.js |
|
5 |
*/ |
|
6 |
||
5 | 7 |
/* global adminpage */ |
0 | 8 |
(function($){ |
5 | 9 |
var wrap, next; |
0 | 10 |
|
9 | 11 |
/** |
12 |
* Shows the authentication form popup. |
|
13 |
* |
|
14 |
* @since 3.6.0 |
|
15 |
* @private |
|
16 |
*/ |
|
0 | 17 |
function show() { |
5 | 18 |
var parent = $('#wp-auth-check'), |
19 |
form = $('#wp-auth-check-form'), |
|
20 |
noframe = wrap.find('.wp-auth-fallback-expired'), |
|
21 |
frame, loaded = false; |
|
0 | 22 |
|
23 |
if ( form.length ) { |
|
9 | 24 |
// Add unload confirmation to counter (frame-busting) JS redirects. |
0 | 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() ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
frame.on( 'load', function() { |
0 | 31 |
var height, body; |
32 |
||
33 |
loaded = true; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
// Remove the spinner to avoid unnecessary CPU/GPU usage. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
form.removeClass( 'loading' ); |
0 | 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 ) { |
|
9 | 54 |
// Catch "silent" iframe origin exceptions in WebKit after another page is |
55 |
// loaded in the iframe. |
|
0 | 56 |
wrap.addClass('fallback'); |
57 |
parent.css( 'max-height', '' ); |
|
58 |
form.remove(); |
|
59 |
noframe.focus(); |
|
60 |
} |
|
61 |
}).attr( 'src', form.data('src') ); |
|
62 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
form.append( frame ); |
0 | 64 |
} |
65 |
||
5 | 66 |
$( 'body' ).addClass( 'modal-open' ); |
0 | 67 |
wrap.removeClass('hidden'); |
68 |
||
69 |
if ( frame ) { |
|
70 |
frame.focus(); |
|
9 | 71 |
// WebKit doesn't throw an error if the iframe fails to load because of |
72 |
// "X-Frame-Options: DENY" header. |
|
0 | 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 |
||
9 | 86 |
/** |
87 |
* Hides the authentication form popup. |
|
88 |
* |
|
89 |
* @since 3.6.0 |
|
90 |
* @private |
|
91 |
*/ |
|
0 | 92 |
function hide() { |
93 |
$(window).off( 'beforeunload.wp-auth-check' ); |
|
94 |
||
9 | 95 |
// When on the Edit Post screen, speed up heartbeat after the user logs in to |
96 |
// quickly refresh nonces. |
|
5 | 97 |
if ( typeof adminpage !== 'undefined' && ( adminpage === 'post-php' || adminpage === 'post-new-php' ) && |
98 |
typeof wp !== 'undefined' && wp.heartbeat ) { |
|
0 | 99 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
$(document).off( 'heartbeat-tick.wp-auth-check' ); |
5 | 101 |
wp.heartbeat.connectNow(); |
0 | 102 |
} |
103 |
||
104 |
wrap.fadeOut( 200, function() { |
|
105 |
wrap.addClass('hidden').css('display', ''); |
|
106 |
$('#wp-auth-check-frame').remove(); |
|
5 | 107 |
$( 'body' ).removeClass( 'modal-open' ); |
0 | 108 |
}); |
109 |
} |
|
110 |
||
9 | 111 |
/** |
112 |
* Schedules when the next time the authentication check will be done. |
|
113 |
* |
|
114 |
* @since 3.6.0 |
|
115 |
* @private |
|
116 |
*/ |
|
0 | 117 |
function schedule() { |
9 | 118 |
// In seconds, default 3 min. |
119 |
var interval = parseInt( window.authcheckL10n.interval, 10 ) || 180; |
|
0 | 120 |
next = ( new Date() ).getTime() + ( interval * 1000 ); |
121 |
} |
|
122 |
||
9 | 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 |
*/ |
|
0 | 137 |
$( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) { |
138 |
if ( 'wp-auth-check' in data ) { |
|
139 |
schedule(); |
|
5 | 140 |
if ( ! data['wp-auth-check'] && wrap.hasClass('hidden') ) { |
0 | 141 |
show(); |
5 | 142 |
} else if ( data['wp-auth-check'] && ! wrap.hasClass('hidden') ) { |
0 | 143 |
hide(); |
5 | 144 |
} |
0 | 145 |
} |
9 | 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 |
*/ |
|
0 | 157 |
}).on( 'heartbeat-send.wp-auth-check', function( e, data ) { |
5 | 158 |
if ( ( new Date() ).getTime() > next ) { |
0 | 159 |
data['wp-auth-check'] = true; |
5 | 160 |
} |
9 | 161 |
|
0 | 162 |
}).ready( function() { |
163 |
schedule(); |
|
9 | 164 |
|
165 |
/** |
|
166 |
* Hides the authentication form popup when the close icon is clicked. |
|
167 |
* |
|
168 |
* @ignore |
|
169 |
* |
|
170 |
* @since 3.6.0 |
|
171 |
*/ |
|
0 | 172 |
wrap = $('#wp-auth-check-wrap'); |
5 | 173 |
wrap.find('.wp-auth-check-close').on( 'click', function() { |
0 | 174 |
hide(); |
175 |
}); |
|
176 |
}); |
|
177 |
||
178 |
}(jQuery)); |