| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codemanas\InactiveLogout; |
| 4 |
|
| 5 |
use Codemanas\InactiveLogout\Controllers\AdminController; |
| 6 |
use Codemanas\InactiveLogout\Controllers\AjaxController; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Bootstrap |
| 10 |
* |
| 11 |
* @package Codemanas\InactiveLogout |
| 12 |
*/ |
| 13 |
class Base { |
| 14 |
|
| 15 |
private $settings; |
| 16 |
|
| 17 |
public $message; |
| 18 |
|
| 19 |
/** |
| 20 |
* Bootstrap constructor. |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
if ( version_compare( INACTIVE_LOGOUT_REQUIRED_PHP, PHP_VERSION, '>' ) ) { |
| 24 |
$this->message = sprintf( |
| 25 |
esc_html__( 'PHP version %1$s or greater is required for %2$s. Please update your PHP version in order to use the plugin.', 'inactive-logout' ), |
| 26 |
INACTIVE_LOGOUT_REQUIRED_PHP, |
| 27 |
esc_html__( 'Inactive Logout', 'inactive-logout' ) |
| 28 |
); |
| 29 |
|
| 30 |
add_action( 'admin_notices', [ $this, 'admin_notice' ] ); |
| 31 |
} else { |
| 32 |
add_action( 'plugins_loaded', [ $this, 'loaded' ] ); |
| 33 |
add_action( 'init', [ $this, 'init' ] ); |
| 34 |
|
| 35 |
add_action( 'wp_enqueue_scripts', [ $this, 'scripts' ] ); |
| 36 |
add_action( 'admin_enqueue_scripts', [ $this, 'scripts' ] ); |
| 37 |
add_filter( 'plugin_action_links', [ $this, 'action_link' ], 10, 2 ); |
| 38 |
add_filter( 'auth_cookie_expiration', [ $this, 'auth_expiration' ], 10, 3 ); |
| 39 |
} |
| 40 |
|
| 41 |
add_action( 'in_plugin_update_message-' . INACTIVE_LOGOUT_ABS_NAME, function ( $plugin_data ) { |
| 42 |
$this->version_update_warning( INACTIVE_LOGOUT_VERSION, $plugin_data['new_version'] ); |
| 43 |
} ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Major Version Upgrade Notice |
| 48 |
* |
| 49 |
* @param $current_version |
| 50 |
* @param $new_version |
| 51 |
* |
| 52 |
* @since 3.0.0 |
| 53 |
* @author Deepen |
| 54 |
*/ |
| 55 |
public function version_update_warning( $current_version, $new_version ) { |
| 56 |
$current_version_minor_part = explode( '.', $current_version )[0]; |
| 57 |
$new_version_minor_part = explode( '.', $new_version )[0]; |
| 58 |
|
| 59 |
if ( $current_version_minor_part === $new_version_minor_part ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
?> |
| 63 |
<hr class="ina-major-update-warning__separator"/> |
| 64 |
<div class="ina-major-update-warning"> |
| 65 |
<div class="ina-major-update-warning__icon"> |
| 66 |
<span class="dashicons dashicons-info-outline"></span> |
| 67 |
</div> |
| 68 |
<div class="ina-major-update-warning_wrapper"> |
| 69 |
<div class="ina-major-update-warning__title"> |
| 70 |
<?php esc_html_e( 'Heads up, Please backup before upgrade!', 'inactive-logout' ); ?> |
| 71 |
</div> |
| 72 |
<div class="ina-major-update-warning__message"> |
| 73 |
<?php |
| 74 |
esc_html_e( 'The latest update includes some substantial changes across different areas of the plugin. We highly recommend you backup your site before upgrading, and make sure you first update in a staging environment', 'inactive-logout' ); |
| 75 |
?> |
| 76 |
</div> |
| 77 |
</div> |
| 78 |
</div> |
| 79 |
<?php |
| 80 |
} |
| 81 |
|
| 82 |
public function admin_notice() { |
| 83 |
printf( '<div class="notice notice-error is-dismissible"><p>%1$s</p></div>', $this->message ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Init the methods |
| 88 |
* |
| 89 |
* @since 3.0.0 |
| 90 |
* @author Deepen |
| 91 |
*/ |
| 92 |
public function loaded() { |
| 93 |
$this->settings = Helpers::getInactiveSettingsData(); |
| 94 |
$this->loadDependencies(); |
| 95 |
|
| 96 |
load_plugin_textdomain( 'inactive-logout', false, trailingslashit( basename( dirname( __DIR__ ) ) ) . 'lang/' ); |
| 97 |
} |
| 98 |
|
| 99 |
public function init() { |
| 100 |
$this->settings = Helpers::getInactiveSettingsData(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Load the dependencies |
| 105 |
* |
| 106 |
* @since 3.0.0 |
| 107 |
* @author Deepen |
| 108 |
*/ |
| 109 |
public function loadDependencies() { |
| 110 |
$disable_feature = ! empty( $this->settings->advanced['disabled_feature'] ) ? $this->settings->advanced['disabled_feature'] : false; |
| 111 |
if ( ! $disable_feature ) { |
| 112 |
#if ( ! $disable_feature && ( is_user_logged_in() || $this->settings->popup_behaviour == "2" ) ) { |
| 113 |
Modal::instance(); |
| 114 |
} |
| 115 |
|
| 116 |
Compatibility::getInstance(); |
| 117 |
AjaxController::getInstance(); |
| 118 |
Users::getInstance(); |
| 119 |
AdminController::getInstance(); |
| 120 |
|
| 121 |
if ( ( ! empty( $this->settings->advanced ) && ! empty( $this->settings->advanced['disabled_concurrent_login'] ) ) || ! empty( $this->settings->concurrent_enabled ) ) { |
| 122 |
ConcurrentLogin::getInstance(); |
| 123 |
} |
| 124 |
|
| 125 |
if ( ! wp_doing_ajax() && ( ( ! empty( $this->settings->advanced ) && ! empty( $this->settings->advanced['redirect_page'] ) ) || ! empty( $this->settings->enabled_redirect ) ) ) { |
| 126 |
LogoutHandler::getInstance(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Enqueue scripts |
| 132 |
* |
| 133 |
* @param $hook |
| 134 |
* |
| 135 |
* @author Deepen |
| 136 |
* |
| 137 |
* @since 3.0.0 |
| 138 |
*/ |
| 139 |
public function scripts( $hook ) { |
| 140 |
if ( is_user_logged_in() || $this->settings->popup_behaviour == "2" ) { |
| 141 |
if ( $hook == "settings_page_inactive-logout" || $hook == "plugins.php" || $hook == "toplevel_page_inactive-logout" ) { |
| 142 |
wp_enqueue_style( 'inactive-logout-admin', INACTIVE_LOGOUT_DIR_URI . 'public/scripts/admin.css', false, INACTIVE_LOGOUT_VERSION ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( $hook == "settings_page_inactive-logout" || $hook == "toplevel_page_inactive-logout" ) { |
| 146 |
//Remove all admin notices from inactive settings screen page. |
| 147 |
remove_all_actions( 'admin_notices' ); |
| 148 |
|
| 149 |
//Enqueue editor |
| 150 |
wp_enqueue_editor(); |
| 151 |
|
| 152 |
//Vendor scripts |
| 153 |
wp_enqueue_script( 'inactive-logout-select2', INACTIVE_LOGOUT_DIR_URI . 'public/vendor/select2/js/select2.full.min.js', [ 'jquery' ], INACTIVE_LOGOUT_VERSION, true ); |
| 154 |
wp_enqueue_style( 'inactive-logout-select2', INACTIVE_LOGOUT_DIR_URI . 'public/vendor/select2/css/select2.min.css', false, INACTIVE_LOGOUT_VERSION ); |
| 155 |
wp_enqueue_script( 'inactive-logout-admin', INACTIVE_LOGOUT_DIR_URI . 'public/scripts/admin.js', [], INACTIVE_LOGOUT_VERSION, true ); |
| 156 |
wp_localize_script( 'inactive-logout-admin', 'inactive_logout', [ |
| 157 |
'wp_version' => get_bloginfo( 'version' ), |
| 158 |
'ina_version' => INACTIVE_LOGOUT_VERSION, |
| 159 |
'security' => wp_create_nonce( '_ina_security_nonce' ), |
| 160 |
] ); |
| 161 |
} |
| 162 |
|
| 163 |
$disable_feature = false; |
| 164 |
$ina_logout_time = $this->settings->logout_time; |
| 165 |
|
| 166 |
//if advanced options |
| 167 |
if ( ! empty( $this->settings->advanced ) ) { |
| 168 |
$ina_logout_time = ! empty( $this->settings->advanced['timeout'] ) ? $this->settings->advanced['timeout'] * 60 : 15 * 60; |
| 169 |
$disable_feature = ! empty( $this->settings->advanced['disabled_feature'] ) ? $this->settings->advanced['disabled_feature'] : false; |
| 170 |
} |
| 171 |
|
| 172 |
//Message content |
| 173 |
if ( ! empty( $this->settings->warn_only_enable ) ) { |
| 174 |
$message_content = Helpers::get_option( '__ina_warn_message' ); |
| 175 |
$replaced_content = str_replace( '{wakup_timout}', Helpers::convertToMinutes( $ina_logout_time ), $message_content ); |
| 176 |
if ( function_exists( 'icl_register_string' ) ) { |
| 177 |
icl_register_string( 'inactive-logout', 'inactive_logout_dynamic_wakeup_text', esc_html( $replaced_content ) ); |
| 178 |
$message_content = wpautop( icl_t( 'inactive-logout', 'inactive_logout_dynamic_wakeup_text', $replaced_content ) ); |
| 179 |
} else { |
| 180 |
$message_content = wpautop( $replaced_content ); |
| 181 |
} |
| 182 |
} else { |
| 183 |
if ( function_exists( 'icl_register_string' ) ) { |
| 184 |
icl_register_string( 'inactive-logout', 'inactive_logout_dynamic_popup_text', esc_html( Helpers::get_option( '__ina_logout_message' ) ) ); |
| 185 |
icl_register_string( 'inactive-logout', 'inactive_logout_dynamic_after_logout_text', esc_html( Helpers::get_option( '__ina_after_logout_message' ) ) ); |
| 186 |
$message_content = wpautop( icl_t( 'inactive-logout', 'inactive_logout_dynamic_popup_text', Helpers::get_option( '__ina_logout_message' ) ) ); |
| 187 |
$after_logout_content = wpautop( icl_t( 'inactive-logout', 'inactive_logout_dynamic_popup_text', Helpers::get_option( '__ina_after_logout_message' ) ) ); |
| 188 |
} else { |
| 189 |
$message_content = wpautop( Helpers::get_option( '__ina_logout_message' ) ); |
| 190 |
$after_logout_content = wpautop( Helpers::get_option( '__ina_after_logout_message' ) ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if ( ! $disable_feature ) { |
| 195 |
$ina_popup_modal = Helpers::get_option( '__ina_logout_popup_localizations' ); |
| 196 |
$data = [ |
| 197 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 198 |
'modal' => apply_filters( 'ina_popup_modal_text', [ |
| 199 |
'ok' => ! empty( $ina_popup_modal ) && ! empty( $ina_popup_modal['text_ok'] ) ? $ina_popup_modal['text_ok'] : __( 'Ok', 'inactive-logout' ), |
| 200 |
'close' => ! empty( $ina_popup_modal ) && ! empty( $ina_popup_modal['text_close'] ) ? $ina_popup_modal['text_close'] : __( 'Close without Reloading', 'inactive-logout' ), |
| 201 |
'login_wait' => __( 'Logging in. Please wait', 'inactive-logout' ), |
| 202 |
'continue' => ! empty( $ina_popup_modal ) && ! empty( $ina_popup_modal['continue_browsing_text'] ) ? $ina_popup_modal['continue_browsing_text'] : __( 'Continue Browsing', 'inactive-logout' ), |
| 203 |
'wakeup_continue' => ! empty( $ina_popup_modal ) && ! empty( $ina_popup_modal['wakeup_cta'] ) ? $ina_popup_modal['wakeup_cta'] : __( 'Ok', 'inactive-logout' ), |
| 204 |
'headerText' => ! empty( $ina_popup_modal ) && ! empty( $ina_popup_modal['popup_heading_text'] ) ? $ina_popup_modal['popup_heading_text'] : __( 'Session Timeout', 'inactive-logout' ), |
| 205 |
'message' => $message_content, |
| 206 |
'logout_message' => ! empty( $after_logout_content ) ? $after_logout_content : apply_filters( 'ina__logout_message', '<p>' . esc_html__( 'You have been logged out because of inactivity.', 'inactive-logout' ) . '</p>' ), |
| 207 |
'disableCloseWithoutReloadBtn' => ! empty( Helpers::get_option( '__ina_disable_close_without_reload' ) ) ? Helpers::get_option( '__ina_disable_close_without_reload' ) : '' |
| 208 |
] ), |
| 209 |
'settings' => [ |
| 210 |
'timeout' => ! empty( $ina_logout_time ) ? absint( $ina_logout_time ) : 15 * 60, |
| 211 |
'disable_countdown' => ! empty( $this->settings->disable_prompt_timer ), |
| 212 |
'warn_message_enabled' => ! empty( $this->settings->warn_only_enable ), |
| 213 |
'countdown_timeout' => ! empty( $this->settings->prompt_countdown_timer ) ? absint( $this->settings->prompt_countdown_timer ) : 10, |
| 214 |
'disable_automatic_redirect' => ! empty( $this->settings->enabled_redirect ) && ! empty( $this->settings->automatic_redirect ) |
| 215 |
], |
| 216 |
'security' => wp_create_nonce( '_inaajax' ), |
| 217 |
]; |
| 218 |
|
| 219 |
$data['settings']['redirect'] = Helpers::getLogoutRedirectPage( $this->settings ); |
| 220 |
if ( $this->settings->debugger ) { |
| 221 |
$date_format = get_option( 'date_format' ); |
| 222 |
$time_format = get_option( 'time_format' ); |
| 223 |
$data['settings']['debug_js'] = true; |
| 224 |
$data['settings']['debug_msg']['logout'] = ! empty( $ina_popup_modal['bottom_countdown_logout_text'] ) ? $ina_popup_modal['bottom_countdown_logout_text'] : __( 'You will be logged out in', 'inactive-logout' ); |
| 225 |
$data['settings']['debug_msg']['last_active'] = ! empty( $ina_popup_modal['bottom_countdown_last_active_text'] ) ? $ina_popup_modal['bottom_countdown_last_active_text'] : __( 'You were last active on', 'inactive-logout' ); |
| 226 |
$data['settings']['debug_msg']['active'] = __( 'Tracking activity!', 'inactive-logout' ); |
| 227 |
$data['settings']['debug_msg']['date_time_format'] = $date_format . ' ' . $time_format; |
| 228 |
} |
| 229 |
|
| 230 |
$data['settings'] = apply_filters( 'ina_logout_settings', $data['settings'] ); |
| 231 |
|
| 232 |
$dependencies = require_once INACTIVE_LOGOUT_DIR_PATH . 'build/index.asset.php'; |
| 233 |
$dependencies = ! empty( $dependencies ) && ! empty( $dependencies['dependencies'] ) ? $dependencies['dependencies'] : []; |
| 234 |
|
| 235 |
wp_enqueue_style( 'inactive-logout', INACTIVE_LOGOUT_BUILD_URI . '/index.css', false, INACTIVE_LOGOUT_VERSION ); |
| 236 |
wp_enqueue_script( 'inactive-logout', INACTIVE_LOGOUT_BUILD_URI . '/index.js', $dependencies, INACTIVE_LOGOUT_VERSION, true ); |
| 237 |
wp_localize_script( 'inactive-logout', 'inactiveLogout', $data ); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Show configure link in main plugins page. |
| 244 |
* |
| 245 |
* @param $actions |
| 246 |
* @param $plugin_file |
| 247 |
* |
| 248 |
* @return array |
| 249 |
* @author Deepen |
| 250 |
* |
| 251 |
* @since 2.0.0 |
| 252 |
*/ |
| 253 |
function action_link( $actions, $plugin_file ) { |
| 254 |
static $plugin; |
| 255 |
|
| 256 |
if ( ! isset( $plugin ) ) { |
| 257 |
$plugin = INACTIVE_LOGOUT_ABS_NAME; |
| 258 |
} |
| 259 |
|
| 260 |
if ( $plugin == $plugin_file ) { |
| 261 |
$settings = array( 'settings' => '<a href="options-general.php?page=inactive-logout">' . __( 'Settings', 'inactive-logout' ) . '</a>' ); |
| 262 |
|
| 263 |
$actions = array_merge( $settings, $actions ); |
| 264 |
} |
| 265 |
|
| 266 |
return $actions; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Set Default WordPress authencation Cookie Time |
| 271 |
* |
| 272 |
* @param $expiration |
| 273 |
* @param $user_id |
| 274 |
* @param $remember |
| 275 |
* |
| 276 |
* @return int |
| 277 |
* @author Deepen |
| 278 |
* |
| 279 |
* @since 2.0.0 |
| 280 |
*/ |
| 281 |
public function auth_expiration( $expiration, $user_id, $remember ) { |
| 282 |
if ( ! $remember ) { |
| 283 |
$expiration = apply_filters( 'ina_change_login_exp_time', 86400 ); //1 day |
| 284 |
} |
| 285 |
|
| 286 |
return $expiration; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Saving options for multisite. |
| 291 |
*/ |
| 292 |
protected function _activate_multisite() { |
| 293 |
$logout_time = get_option( '__ina_logout_time' ); |
| 294 |
if ( empty( $logout_time ) ) { |
| 295 |
$time = 15 * 60; // 15 Minutes |
| 296 |
update_option( '__ina_logout_time', $time ); |
| 297 |
} |
| 298 |
|
| 299 |
$logout_message = get_option( '__ina_logout_message' ); |
| 300 |
if ( empty( $logout_message ) ) { |
| 301 |
update_option( '__ina_logout_message', '<p>You are being timed-out out due to inactivity. Please choose to stay signed in or to logoff.</p><p>Otherwise, you will be logged off automatically.</p>' ); |
| 302 |
} |
| 303 |
|
| 304 |
$warn_message = get_option( '__ina_warn_message' ); |
| 305 |
if ( empty( $warn_message ) ) { |
| 306 |
update_option( '__ina_warn_message', '<h3>Wakeup!</h3><p>You have been inactive for {wakup_timout}. Press continue to continue browsing.</p>' ); |
| 307 |
} |
| 308 |
|
| 309 |
$after_logout_message = get_option( '__ina_after_logout_message' ); |
| 310 |
if ( empty( $after_logout_message ) ) { |
| 311 |
update_option( '__ina_after_logout_message', '<p>You have been logged out because of inactivity.</p>' ); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Managing things when plugin is deactivated. |
| 317 |
*/ |
| 318 |
public static function deactivate() { |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Plugin activation callback. |
| 324 |
* |
| 325 |
* @see register_deactivation_hook() |
| 326 |
*/ |
| 327 |
public static function activate() { |
| 328 |
if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 329 |
global $wpdb; |
| 330 |
$old_blog = $wpdb->blogid; |
| 331 |
|
| 332 |
// Get all blog ids. |
| 333 |
$blogids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); // WPCS: db call ok, cache ok. |
| 334 |
foreach ( $blogids as $blog_id ) { |
| 335 |
switch_to_blog( $blog_id ); |
| 336 |
self::instance()->_activate_multisite(); |
| 337 |
} |
| 338 |
switch_to_blog( $old_blog ); |
| 339 |
|
| 340 |
return; |
| 341 |
} else { |
| 342 |
self::instance()->_activate_multisite(); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
private static $_instance = null; |
| 347 |
|
| 348 |
public static function instance() { |
| 349 |
if ( is_null( self::$_instance ) ) { |
| 350 |
self::$_instance = new self(); |
| 351 |
} |
| 352 |
|
| 353 |
return self::$_instance; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
Base::instance(); |