| | 1 | <?php |
| | 2 | /** |
| | 3 | * Test the user's current authorization state |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @since 3.6.0 |
| | 7 | */ |
| | 8 | class WP_Auth_Check { |
| | 9 | |
| | 10 | /** |
| | 11 | * Holds the singleton instance of this object |
| | 12 | */ |
| | 13 | private static $_instance = null; |
| | 14 | |
| | 15 | /** |
| | 16 | * Private constructor because we're a singleton |
| | 17 | */ |
| | 18 | private function __construct() {} |
| | 19 | |
| | 20 | /** |
| | 21 | * Initialize the singleton |
| | 22 | */ |
| | 23 | public static function get_instance() { |
| | 24 | $this_class = get_called_class(); // gets the right class when this is extended |
| | 25 | if ( ! ( self::$_instance instanceof $this_class ) ) { |
| | 26 | self::$_instance = new $this_class; |
| | 27 | self::$_instance->_init(); |
| | 28 | } |
| | 29 | |
| | 30 | return self::$_instance; |
| | 31 | } |
| | 32 | |
| | 33 | /** |
| | 34 | * Object init, sets up hooks. Not done in the constructor so that the |
| | 35 | * _init() method may be extended without breaking the singleton. |
| | 36 | */ |
| | 37 | protected function _init() { |
| | 38 | if ( is_admin() ) { |
| | 39 | add_action( 'admin_footer', array( $this, 'enqueue_scripts' ) ); |
| | 40 | add_action( 'admin_print_footer_scripts', array( $this, 'footer_js' ) ); |
| | 41 | } elseif ( is_user_logged_in() ) { |
| | 42 | add_action( 'wp_footer', array( $this, 'enqueue_scripts' ) ); |
| | 43 | add_action( 'wp_print_footer_scripts', array( $this, 'footer_js' ) ); |
| | 44 | } |
| | 45 | |
| | 46 | add_filter( 'heartbeat_received', array( $this, 'test_login' ), 10, 2 ); |
| | 47 | add_filter( 'heartbeat_nopriv_received', array( $this, 'nopriv_login' ), 10, 2 ); |
| | 48 | } |
| | 49 | |
| | 50 | public function test_login( $response, $data ) { |
| | 51 | if ( array_key_exists('wp-auth-check', $data) && ( ! isset( $_COOKIE[LOGGED_IN_COOKIE] ) || ! wp_validate_auth_cookie() || ! empty( $GLOBALS['login_grace_period'] ) ) ) |
| | 52 | $response['wp-auth-check-html'] = $this->show_notice(); |
| | 53 | |
| | 54 | return $response; |
| | 55 | } |
| | 56 | |
| | 57 | public function nopriv_login( $response, $data ) { |
| | 58 | if ( array_key_exists('wp-auth-check', $data) ) |
| | 59 | $response['wp-auth-check-html'] = $this->show_notice(); |
| | 60 | |
| | 61 | return $response; |
| | 62 | } |
| | 63 | |
| | 64 | public function footer_js() { |
| | 65 | ?> |
| | 66 | <script> |
| | 67 | (function($){ |
| | 68 | $( document ).on( 'heartbeat-tick.wp-auth-check', function( e, data ) { |
| | 69 | var element = $('#wp-auth-check-notice-wrap'); |
| | 70 | |
| | 71 | if ( data['wp-auth-check-html'] && ! element.length ) { |
| | 72 | $('body').append( data['wp-auth-check-html'] ); |
| | 73 | } else if ( !data['wp-auth-check-html'] && element.length ) { |
| | 74 | if ( $('#wp-auth-check-notice').length ) |
| | 75 | element.remove(); |
| | 76 | } |
| | 77 | }).on( 'heartbeat-send.wp-auth-check', function( e, data ) { |
| | 78 | data['wp-auth-check'] = 1; |
| | 79 | }); |
| | 80 | }(jQuery)); |
| | 81 | </script> |
| | 82 | <?php |
| | 83 | } |
| | 84 | |
| | 85 | /** |
| | 86 | * Enqueue scripts |
| | 87 | */ |
| | 88 | public function enqueue_scripts() { |
| | 89 | // This will also enqueue jQuery |
| | 90 | wp_enqueue_script( 'heartbeat' ); |
| | 91 | } |
| | 92 | |
| | 93 | /** |
| | 94 | * Adds a div for displaying the login notice |
| | 95 | */ |
| | 96 | public function show_notice() { |
| | 97 | $warning = !empty( $GLOBALS['login_grace_period'] ) ? __( 'Your login will expire soon, please log in again.' ) : __( 'Oops! Looks like you are not logged in.' ); |
| | 98 | |
| | 99 | // Inline CSS because it keeps the notice container portable. |
| | 100 | return ' |
| | 101 | <div id="wp-auth-check-notice-wrap"> |
| | 102 | <style type="text/css" scoped> |
| | 103 | #wp-auth-check-notice, |
| | 104 | #wp-auth-check-form { |
| | 105 | background-color: rgba(0, 0, 0, 0.7); |
| | 106 | color: #FFFFFF; |
| | 107 | font-size: 1.5em; |
| | 108 | text-align: center; |
| | 109 | position: fixed; |
| | 110 | margin: 0; |
| | 111 | padding: 3%; |
| | 112 | -webkit-border-radius: 10px; |
| | 113 | border-radius: 10px; |
| | 114 | z-index: 1000000000; |
| | 115 | text-align: center; |
| | 116 | } |
| | 117 | #wp-auth-check-notice { |
| | 118 | top: 25%; |
| | 119 | left: 25%; |
| | 120 | width: 50%; |
| | 121 | } |
| | 122 | #wp-auth-check-form { |
| | 123 | display: none; |
| | 124 | top: 7%; |
| | 125 | left: 7%; |
| | 126 | width: 80%; |
| | 127 | height: 75%; |
| | 128 | padding: 3% 3% 6%; |
| | 129 | } |
| | 130 | #wp-auth-check-form iframe { |
| | 131 | width: 100%; |
| | 132 | height: 100%; |
| | 133 | border-radius: 3px; |
| | 134 | } |
| | 135 | </style> |
| | 136 | <div id="wp-auth-check-inner"> |
| | 137 | <div id="wp-auth-check-notice"> |
| | 138 | <p>' . $warning . '</p> |
| | 139 | <p><a href="#" class="button wp-auth-check-open">' . __( 'Log in' ) . '</a></p> |
| | 140 | <p>' . __( 'You will not leave this screen.' ) . '</p> |
| | 141 | </div> |
| | 142 | <div id="wp-auth-check-form"> |
| | 143 | <iframe src="' . esc_url( add_query_arg( array( 'interim-login' => 1 ), wp_login_url() ) ) . '"></iframe> |
| | 144 | <p><a href="#" class="button wp-auth-check-close">' . __( 'Close' ) . '</a></p> |
| | 145 | </div> |
| | 146 | </div> |
| | 147 | <script type="text/javascript"> |
| | 148 | (function($){ |
| | 149 | var notice = $("#wp-auth-check-notice"), form = $("#wp-auth-check-form"), buttons; |
| | 150 | |
| | 151 | notice.find("a.wp-auth-check-open").on("click", function(e){ |
| | 152 | notice.fadeOut(200, function(){ $(this).remove(); }); |
| | 153 | form.show().fadeIn(250); |
| | 154 | }); |
| | 155 | |
| | 156 | form.find("a.wp-auth-check-close").on("click", function(e){ |
| | 157 | form.fadeOut(200, function(){ $("#wp-auth-check-notice-wrap").remove(); }); |
| | 158 | }); |
| | 159 | |
| | 160 | if ( ! $("#wp-auth-check-notice-wrap").length ) |
| | 161 | $("body").append( data["wp-auth-check-html"] ); |
| | 162 | }(jQuery)); |
| | 163 | </script> |
| | 164 | </div> |
| | 165 | '; |
| | 166 | } |
| | 167 | } |
| | 168 | |