| 1 |
<?php |
| 2 |
|
| 3 |
namespace Codemanas\InactiveLogout\Controllers\Admin; |
| 4 |
|
| 5 |
use Codemanas\InactiveLogout\Helpers; |
| 6 |
use Codemanas\InactiveLogout\Requests\GeneralSettingRequests; |
| 7 |
use Codemanas\InactiveLogout\Requests\RoleBasedSettingRequests; |
| 8 |
|
| 9 |
class StoreController { |
| 10 |
|
| 11 |
/** |
| 12 |
* Save general settings logic here |
| 13 |
*F |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public function saveGeneralSetting() { |
| 17 |
check_ajax_referer( '_nonce_action_save_timeout_settings', '_save_timeout_settings' ); |
| 18 |
|
| 19 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 20 |
wp_send_json_error( __( 'Unauthorized.', 'inactive-logout' ), 403 ); |
| 21 |
} |
| 22 |
|
| 23 |
do_action( 'ina_before_update_basic_settings' ); |
| 24 |
|
| 25 |
//Update Text Localizations to another field. |
| 26 |
$localizationData = GeneralSettingRequests::getLocalizations(); |
| 27 |
Helpers::update_option( '__ina_logout_popup_localizations', $localizationData ); |
| 28 |
|
| 29 |
$postedData = GeneralSettingRequests::get(); |
| 30 |
|
| 31 |
// If multisite is Active then Add these settings to multisite option table as well. |
| 32 |
if ( Helpers::isMultisite() ) { |
| 33 |
$overrideForAllSites = filter_input( INPUT_POST, 'idle_overrideby_multisite_setting', FILTER_SANITIZE_NUMBER_INT ); |
| 34 |
update_site_option( '__ina_overrideby_multisite_setting', $overrideForAllSites ); |
| 35 |
} |
| 36 |
|
| 37 |
$save_minutes = ( $postedData['logout_time'] > 1440 ) ? 1440 : $postedData['logout_time'] * 60; // 60 minutes |
| 38 |
if ( ! empty( $postedData['logout_time'] ) ) { |
| 39 |
//If redirection is a custom link |
| 40 |
$externalRedirectionLink = ! empty( $postedData['redirect_page_link'] ) && 'custom-page-redirect' === $postedData['redirect_page_link'] ? filter_input( INPUT_POST, 'custom_redirect_text_field' ) : false; |
| 41 |
if ( ! empty( $postedData['enable_redirect'] ) && ! empty( $postedData['redirect_page_link'] ) && 'custom-page-redirect' === $postedData['redirect_page_link'] ) { |
| 42 |
$postedData['custom_redirect_text_field'] = sanitize_url( $externalRedirectionLink ); |
| 43 |
Helpers::update_option( '__ina_custom_redirect_text_field', sanitize_url( $externalRedirectionLink ) ); |
| 44 |
} else { |
| 45 |
$postedData['custom_redirect_text_field'] = null; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Save settings |
| 50 |
* |
| 51 |
* Deprecating in in future versions use "__ina_general_settings" instead |
| 52 |
*/ |
| 53 |
$postedData['logout_time'] = $save_minutes; |
| 54 |
foreach ( $postedData as $key => $value ) { |
| 55 |
Helpers::update_option( "__ina_{$key}", $value ); |
| 56 |
} |
| 57 |
|
| 58 |
//New method to save all data into one row |
| 59 |
Helpers::update_option( "__ina_general_settings", $postedData ); |
| 60 |
} |
| 61 |
|
| 62 |
do_action( 'ina_after_update_basic_settings' ); |
| 63 |
|
| 64 |
Helpers::update_option( '__ina_saved_options', __( 'General Settings saved.', 'inactive-logout' ), false ); |
| 65 |
|
| 66 |
wp_send_json_success(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Save role based settings logic here |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function saveRoleBasedSettings() { |
| 75 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 76 |
wp_die( __( 'Unauthorized.', 'inactive-logout' ), 403 ); |
| 77 |
} |
| 78 |
|
| 79 |
$postedData = RoleBasedSettingRequests::get(); |
| 80 |
|
| 81 |
$container_multi_user_arr = array(); |
| 82 |
|
| 83 |
//Enabled multi-role |
| 84 |
if ( ! empty( $postedData['ina_multiuser_roles'] ) ) { |
| 85 |
foreach ( $postedData['ina_multiuser_roles'] as $k => $ina_multiuser_role ) { |
| 86 |
$user_timeout_minutes = ! empty( $postedData['ina_individual_user_timeout'][ $ina_multiuser_role ] ) ? absint( $postedData['ina_individual_user_timeout'][ $ina_multiuser_role ] ) : 15; |
| 87 |
$redirect_page_link = !empty( $postedData['ina_redirect_page_individual_user'][ $ina_multiuser_role ] ) |
| 88 |
? esc_url_raw( trim( $postedData['ina_redirect_page_individual_user'][ $ina_multiuser_role ] ) ) |
| 89 |
: ''; |
| 90 |
$disabled_for_user = ! empty( $postedData['ina_disable_inactive_logout'][ $ina_multiuser_role ] ); |
| 91 |
$disabled_for_user_concurrent_login = ! empty( $postedData['ina_disable_inactive_concurrent_login'][ $ina_multiuser_role ] ); |
| 92 |
|
| 93 |
// Validate URL |
| 94 |
if ( empty( $redirect_page_link ) || ! wp_http_validate_url( $redirect_page_link ) ) { |
| 95 |
$redirect_page_link = ''; |
| 96 |
} |
| 97 |
|
| 98 |
$container_multi_user_arr[] = array( |
| 99 |
'role' => $ina_multiuser_role, |
| 100 |
'timeout' => min( $user_timeout_minutes, 1440 ), |
| 101 |
'redirect_page' => $redirect_page_link, |
| 102 |
'disabled_feature' => $disabled_for_user ? 1 : null, |
| 103 |
'disabled_concurrent_login' => $disabled_for_user_concurrent_login ? 1 : null, |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
do_action( 'ina_before_update_adv_settings', $container_multi_user_arr ); |
| 109 |
|
| 110 |
Helpers::update_option( '__ina_enable_timeout_multiusers', $postedData['ina_enable_different_role_timeout'] ); |
| 111 |
if ( ! empty( $postedData['ina_enable_different_role_timeout'] ) ) { |
| 112 |
Helpers::update_option( '__ina_multiusers_settings', $container_multi_user_arr ); |
| 113 |
} |
| 114 |
|
| 115 |
do_action( 'ina_after_update_adv_settings', $container_multi_user_arr ); |
| 116 |
|
| 117 |
Helpers::set_message( __( 'Role based settings saved.', 'inactive-logout' ) ); |
| 118 |
} |
| 119 |
|
| 120 |
public function resetRoleBasedSettings() { |
| 121 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 122 |
wp_send_json_error( __( 'Unauthorized.', 'inactive-logout' ), 403 ); |
| 123 |
} |
| 124 |
|
| 125 |
check_ajax_referer( '_ina_security_nonce', 'security' ); |
| 126 |
|
| 127 |
delete_option( '__ina_roles' ); |
| 128 |
delete_option( '__ina_enable_timeout_multiusers' ); |
| 129 |
delete_option( '__ina_multiusers_settings' ); |
| 130 |
|
| 131 |
if ( wp_doing_ajax() && get_current_blog_id() == get_main_network_id() && is_multisite() ) { |
| 132 |
delete_site_option( '__ina_roles' ); |
| 133 |
delete_site_option( '__ina_enable_timeout_multiusers' ); |
| 134 |
delete_site_option( '__ina_multiusers_settings' ); |
| 135 |
} |
| 136 |
|
| 137 |
Helpers::update_option( '__ina_saved_options', __( 'Role based settings reset.', 'inactive-logout' ), false ); |
| 138 |
|
| 139 |
wp_send_json( array( |
| 140 |
'code' => 1, |
| 141 |
'msg' => esc_html__( 'Reset advanced settings successful.', 'inactive-logout' ), |
| 142 |
) ); |
| 143 |
|
| 144 |
wp_die(); |
| 145 |
} |
| 146 |
|
| 147 |
private static ?StoreController $_instance = null; |
| 148 |
|
| 149 |
public static function getInstance(): ?StoreController { |
| 150 |
if ( is_null( self::$_instance ) ) { |
| 151 |
self::$_instance = new self(); |
| 152 |
} |
| 153 |
|
| 154 |
return self::$_instance; |
| 155 |
} |
| 156 |
|
| 157 |
} |