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