| | 1 | /** |
| | 2 | * WP Auth Check object |
| | 3 | */ |
| | 4 | wp_auth_check = { |
| | 5 | |
| | 6 | /** |
| | 7 | * Holds the button state so we aren't unnecessarily enabling/disabling |
| | 8 | * and so we don't have to continually check a bunch of stuff |
| | 9 | * |
| | 10 | * @var bool |
| | 11 | */ |
| | 12 | buttons_disabled: false, |
| | 13 | |
| | 14 | /** |
| | 15 | * Holds the ID of the notice container so that the notice parts can be abstracted out |
| | 16 | * |
| | 17 | * @var string |
| | 18 | */ |
| | 19 | notice_container_id: "wp-auth-check-notice", |
| | 20 | |
| | 21 | /** |
| | 22 | * Holds the notice container's jQuery object |
| | 23 | * |
| | 24 | * @var obj|null |
| | 25 | */ |
| | 26 | notice_container: null, |
| | 27 | |
| | 28 | /** |
| | 29 | * Boots up the wp_auth_check object and heartbeat listener |
| | 30 | * |
| | 31 | * @return void |
| | 32 | */ |
| | 33 | init: function() { |
| | 34 | if ( "true" == wp_auth_check_opts.is_interim_login ) { |
| | 35 | this.hijack_interim_login_close_button(); |
| | 36 | return; // no auth check on the login screen |
| | 37 | } |
| | 38 | |
| | 39 | jQuery( document ).on( "heartbeat-tick.auth-check", function( e, data ) { |
| | 40 | if ( ! data["auth-check"] ) { |
| | 41 | wp_auth_check.show_login_notice(); |
| | 42 | return; |
| | 43 | } |
| | 44 | |
| | 45 | if ( "logged_in" == data["auth-check"] ) { |
| | 46 | wp_auth_check.hide_login_notice(); |
| | 47 | return; |
| | 48 | } |
| | 49 | |
| | 50 | var container_html = ( data["auth-check-html"] ) ? data["auth-check-html"] : ""; |
| | 51 | |
| | 52 | wp_auth_check.show_login_notice( container_html ); |
| | 53 | } ); |
| | 54 | |
| | 55 | }, |
| | 56 | |
| | 57 | /** |
| | 58 | * Display a login window and disable the save buttons |
| | 59 | * show_notice() has been abstracted out to make it easier to refactor it |
| | 60 | * into a more generic notification object for general use. |
| | 61 | * |
| | 62 | * @return void |
| | 63 | */ |
| | 64 | show_login_notice: function( container_html ) { |
| | 65 | if ( this.notice_is_visible() ) { |
| | 66 | return; |
| | 67 | } |
| | 68 | |
| | 69 | var message = "<p>" + wp_auth_check_text.not_logged_in + "</p>"; |
| | 70 | message += '<p><a href="#" class="button" onclick="wp_auth_check.show_interim_login();">' + wp_auth_check_text.log_in + '</a></p>'; |
| | 71 | message += "<p>" + wp_auth_check_text.after_login_button + "</p>"; |
| | 72 | |
| | 73 | wp_auth_check.show_notice( message, container_html ); |
| | 74 | |
| | 75 | wp_auth_check.disable_action_buttons(); |
| | 76 | }, |
| | 77 | |
| | 78 | /** |
| | 79 | * Hides the notice container and re-enables action buttons |
| | 80 | * hide_notice() has been abstracted out to make it easier to refactor it |
| | 81 | * into a more generic notification object for general use. |
| | 82 | * |
| | 83 | * @return void |
| | 84 | */ |
| | 85 | hide_login_notice: function() { |
| | 86 | if ( ! this.notice_is_visible() ) { |
| | 87 | return; |
| | 88 | } |
| | 89 | |
| | 90 | this.hide_notice(); |
| | 91 | |
| | 92 | this.enable_action_buttons(); |
| | 93 | }, |
| | 94 | |
| | 95 | /** |
| | 96 | * Fills the notice window with the interim login page |
| | 97 | * |
| | 98 | * @return void |
| | 99 | */ |
| | 100 | show_interim_login: function() { |
| | 101 | this.notice_container.html( "" ).html( '<iframe width="' + this.notice_container.width() + '" height="' + this.notice_container.height() + '" src="' + wp_auth_check_opts.login_url + '"></iframe>' ); |
| | 102 | }, |
| | 103 | |
| | 104 | /** |
| | 105 | * Targets the "Close" button on wp-login.php's "interim login" |
| | 106 | * screen, and binds the click event to close the modal dialogue. |
| | 107 | * |
| | 108 | * @return void |
| | 109 | */ |
| | 110 | hijack_interim_login_close_button: function() { |
| | 111 | jQuery( "#login :button" ).click( function( e ) { |
| | 112 | parent.window.wp_auth_check.hide_notice(); |
| | 113 | e.preventDefault(); |
| | 114 | return false; |
| | 115 | } ); |
| | 116 | }, |
| | 117 | |
| | 118 | /** |
| | 119 | * Creates a nice notice overlay on the screen. |
| | 120 | * Based on P2's newNotification() function |
| | 121 | * |
| | 122 | * @param message HTML to put in the notice container |
| | 123 | * @param container_html |
| | 124 | * @return void |
| | 125 | */ |
| | 126 | show_notice: function( message, container_html ) { |
| | 127 | if ( this.notice_is_visible() ) { |
| | 128 | return; |
| | 129 | } |
| | 130 | |
| | 131 | jQuery("body").append( container_html ); |
| | 132 | |
| | 133 | this.notice_container = jQuery( "#" + this.notice_container_id ); |
| | 134 | |
| | 135 | // Calculate the highest z-index on the page, so that this can overlay |
| | 136 | // on top of it |
| | 137 | var highest_z_index = parseInt( this.notice_container.css( "z-index" ) ); |
| | 138 | jQuery( "body" ).find( "*" ).each( function() { |
| | 139 | var this_z_index = parseInt( jQuery( this ).css( "z-index" ) ); |
| | 140 | if ( ( ! isNaN( this_z_index ) ) && this_z_index > highest_z_index ) { |
| | 141 | highest_z_index = this_z_index; |
| | 142 | } |
| | 143 | } ); |
| | 144 | |
| | 145 | this.notice_container |
| | 146 | .css( "z-index", ( highest_z_index + 1 ) ) |
| | 147 | .stop( true ) |
| | 148 | .prepend( message ) |
| | 149 | .fadeIn(); |
| | 150 | }, |
| | 151 | |
| | 152 | /** |
| | 153 | * Hides the #wp-auth-check-notice modal dialogue and re-enables save buttons |
| | 154 | * |
| | 155 | * @return void |
| | 156 | */ |
| | 157 | hide_notice: function() { |
| | 158 | if ( ! this.notice_is_visible() ) { |
| | 159 | return; |
| | 160 | } |
| | 161 | |
| | 162 | this.notice_container |
| | 163 | .stop( true ) |
| | 164 | .fadeOut( "fast" ) |
| | 165 | .remove(); |
| | 166 | |
| | 167 | this.notice_container = null; |
| | 168 | }, |
| | 169 | |
| | 170 | /** |
| | 171 | * Helper method for checking whether the notice container is present and visible |
| | 172 | * |
| | 173 | * @return bool |
| | 174 | */ |
| | 175 | notice_is_visible: function() { |
| | 176 | return ( this.notice_container && this.notice_container.is( ":visible" ) ); |
| | 177 | }, |
| | 178 | |
| | 179 | /** |
| | 180 | * Disable the save/publish/update/move to trash buttons. |
| | 181 | * |
| | 182 | * @return void |
| | 183 | */ |
| | 184 | disable_action_buttons: function() { |
| | 185 | if ( true == wp_auth_check.buttons_disabled ) { |
| | 186 | return; |
| | 187 | } |
| | 188 | |
| | 189 | blockSave = true; |
| | 190 | |
| | 191 | jQuery( ":button, :submit, #post-preview, .submitdelete", "#submitpost" ).each( function() { |
| | 192 | var t = jQuery( this ); |
| | 193 | if ( t.hasClass( "button-primary" ) ) |
| | 194 | t.addClass( "button-primary-disabled" ); |
| | 195 | else |
| | 196 | t.addClass( "button-disabled" ); |
| | 197 | |
| | 198 | t.prop( "disabled", true ); |
| | 199 | } ); |
| | 200 | |
| | 201 | wp_auth_check.buttons_disabled = true; |
| | 202 | }, |
| | 203 | |
| | 204 | /** |
| | 205 | * Re-enable the save/publish/update/move to trash buttons. |
| | 206 | * |
| | 207 | * @return void |
| | 208 | */ |
| | 209 | enable_action_buttons: function() { |
| | 210 | if ( false == wp_auth_check.buttons_disabled ) { |
| | 211 | return; |
| | 212 | } |
| | 213 | |
| | 214 | blockSave = false; |
| | 215 | |
| | 216 | jQuery( ":button, :submit, #post-preview, .submitdelete", "#submitpost" ).each( function() { |
| | 217 | var t = jQuery( this ); |
| | 218 | if ( t.hasClass( "button-primary-disabled" ) ) |
| | 219 | t.removeClass( "button-primary-disabled" ); |
| | 220 | else |
| | 221 | t.removeClass( "button-disabled" ); |
| | 222 | |
| | 223 | t.prop( "disabled", false ); |
| | 224 | } ); |
| | 225 | |
| | 226 | wp_auth_check.buttons_disabled = false; |
| | 227 | } |
| | 228 | |
| | 229 | } |
| | 230 | |
| | 231 | jQuery( document ).ready( function() { |
| | 232 | wp_auth_check.init(); |
| | 233 | } ); |
| | 234 | |
| | 235 | //EOF |
| | 236 | No newline at end of file |