| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codemanas\InactiveLogout; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Modal |
| 7 |
* @package Codemanas\InactiveLogout |
| 8 |
*/ |
| 9 |
class Modal { |
| 10 |
|
| 11 |
private const DEFAULT_TOAST_MESSAGE = 'You have been automatically logged out due to inactivity'; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
add_action( 'wp_footer', array( $this, 'dialog_modal' ), 1 ); |
| 15 |
add_action( 'admin_footer', array( $this, 'dialog_modal' ), 1 ); |
| 16 |
add_action( 'wp_head', [ $this, 'toastStyles' ] ); |
| 17 |
add_action( 'login_footer', [ $this, 'toastContent' ] ); |
| 18 |
add_action( 'login_head', [ $this, 'toastStyles' ] ); |
| 19 |
add_action( 'template_redirect', [ $this, 'maybeClearToastFlag' ] ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Toast CSS |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function toastStyles() { |
| 28 |
if ( ! empty( $this->showToast() ) && ! is_user_logged_in() ) { |
| 29 |
echo $this->getToastStyle(); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
public function dialog_modal() { |
| 34 |
if ( is_user_logged_in() ) { |
| 35 |
?> |
| 36 |
<!--START INACTIVE LOGOUT MODAL CONTENT--> |
| 37 |
<div id="ina-logout-modal-container" class="ina-logout-modal-container"></div> |
| 38 |
<!--END INACTIVE LOGOUT MODAL CONTENT--> |
| 39 |
<?php |
| 40 |
} else { |
| 41 |
$this->toastContent(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public function toastContent() { |
| 46 |
if ( ! empty( $this->showToast() ) ) { |
| 47 |
$message = Helpers::getSettings( 'after_redirection_toast_message' ); |
| 48 |
$message = ! empty( $message ) ? $message : self::DEFAULT_TOAST_MESSAGE; |
| 49 |
echo $this->getToastHTML( $message ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public static function getToastHTML( $message ) { |
| 54 |
ob_start(); |
| 55 |
?> |
| 56 |
<div class="ina-logout-toast-container"> |
| 57 |
<div aria-live="assertive" role="alert" class="ina-logout-toast__content"> |
| 58 |
<svg viewBox="0 0 24 24" width="100%" height="100%" fill="#f1c40f"> |
| 59 |
<path d="M12 0a12 12 0 1012 12A12.013 12.013 0 0012 0zm.25 5a1.5 1.5 0 11-1.5 1.5 1.5 1.5 0 011.5-1.5zm2.25 13.5h-4a1 1 0 010-2h.75a.25.25 0 00.25-.25v-4.5a.25.25 0 00-.25-.25h-.75a1 1 0 010-2h1a2 2 0 012 2v4.75a.25.25 0 00.25.25h.75a1 1 0 110 2z"></path> |
| 60 |
</svg> |
| 61 |
<p><?php esc_html_e( $message ); ?>.</p> |
| 62 |
</div> |
| 63 |
<button class="ina-logout-toast__close" type="button" role="button" aria-label="Close notification"> |
| 64 |
<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24"> |
| 65 |
<path d="M 4.7070312 3.2929688 L 3.2929688 4.7070312 L 10.585938 12 L 3.2929688 19.292969 L 4.7070312 20.707031 L 12 13.414062 L 19.292969 20.707031 L 20.707031 19.292969 L 13.414062 12 L 20.707031 4.7070312 L 19.292969 3.2929688 L 12 10.585938 L 4.7070312 3.2929688 z"></path> |
| 66 |
</svg> |
| 67 |
</button> |
| 68 |
</div> |
| 69 |
<script> |
| 70 |
document.addEventListener('DOMContentLoaded', function () { |
| 71 |
const closeBtn = document.querySelector('.ina-logout-toast__close'); |
| 72 |
const container = document.querySelector('.ina-logout-toast-container'); |
| 73 |
if (closeBtn && container) { |
| 74 |
closeBtn.addEventListener('click', () => container.remove()); |
| 75 |
} |
| 76 |
}); |
| 77 |
</script> |
| 78 |
<?php |
| 79 |
return ob_get_clean(); |
| 80 |
} |
| 81 |
|
| 82 |
private function showToast() { |
| 83 |
return ! empty( $_COOKIE['ina_redirection_logged_out'] ) && ! empty( Helpers::getSettings( 'show_toast_notification' ) ); |
| 84 |
} |
| 85 |
|
| 86 |
public function maybeClearToastFlag() { |
| 87 |
if ( ! empty( $_COOKIE['ina_redirection_logged_out'] ) ) { |
| 88 |
setcookie( 'ina_redirection_logged_out', '', time() - 3600, '/' ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public function getToastStyle() { |
| 93 |
ob_start(); |
| 94 |
?> |
| 95 |
<style> |
| 96 |
.ina-logout-toast-container { |
| 97 |
z-index: 99999; |
| 98 |
position: fixed; |
| 99 |
width: 400px; |
| 100 |
box-sizing: border-box; |
| 101 |
color: #000; |
| 102 |
top: 20px; |
| 103 |
right: 20px; |
| 104 |
padding: 20px; |
| 105 |
background: #fff; |
| 106 |
line-height: 26px; |
| 107 |
border-radius: 6px; |
| 108 |
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1019607843); |
| 109 |
animation: bounceInRightToastr 0.4s ease both; |
| 110 |
} |
| 111 |
|
| 112 |
@keyframes bounceInRightToastr { |
| 113 |
0% { |
| 114 |
opacity: 0; |
| 115 |
transform: translateX(300%); |
| 116 |
} |
| 117 |
60% { |
| 118 |
opacity: 1; |
| 119 |
transform: translateX(-25px); |
| 120 |
} |
| 121 |
80% { |
| 122 |
transform: translateX(10px); |
| 123 |
} |
| 124 |
100% { |
| 125 |
transform: translateX(0); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
.ina-logout-toast__content { |
| 130 |
display: flex; |
| 131 |
gap: 10px; |
| 132 |
align-items: center; |
| 133 |
position: relative; |
| 134 |
} |
| 135 |
|
| 136 |
.ina-logout-toast__content p { |
| 137 |
margin: 0; |
| 138 |
font-size: 1rem; |
| 139 |
color: #545454; |
| 140 |
} |
| 141 |
|
| 142 |
.ina-logout-toast__content svg { |
| 143 |
width: 40px; |
| 144 |
height: 40px; |
| 145 |
} |
| 146 |
|
| 147 |
.ina-logout-toast__close { |
| 148 |
position: absolute; |
| 149 |
right: 5px; |
| 150 |
top: 5px; |
| 151 |
background: transparent; |
| 152 |
border: none; |
| 153 |
cursor: pointer; |
| 154 |
} |
| 155 |
</style> |
| 156 |
<?php |
| 157 |
return ob_get_clean(); |
| 158 |
} |
| 159 |
|
| 160 |
private static $_instance = null; |
| 161 |
|
| 162 |
public static function instance() { |
| 163 |
if ( is_null( self::$_instance ) ) { |
| 164 |
self::$_instance = new self(); |
| 165 |
} |
| 166 |
|
| 167 |
return self::$_instance; |
| 168 |
} |
| 169 |
} |