PluginProbe
Inactive Logout / trunk
Inactive Logout vtrunk
3.6.3 trunk 1.9.9 2.0.2 3.3.1 3.3.2 3.3.3 3.3.4 3.4.0 3.4.1 3.4.10 3.4.11 3.4.12 3.4.13 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 All 31 releases
inactive-logout / core / Helpers.php

Helpers.php in Inactive Logout trunk, at core/Helpers.php

246 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Codemanas\InactiveLogout;
4
5 /**
6 * Class with a few helpers
7 *
8 * @package inactive-logout
9 */
10 class Helpers {
11
12 public static $message;
13
14 /**
15 * Convert seconds to minutes.
16 *
17 * @param int $value Number of seconds.
18 *
19 * @return string
20 */
21 public static function convertToMinutes( $value ) {
22 $minutes = floor( $value / 60 );
23
24 return $minutes . ' ' . esc_html__( 'Minute(s)', 'inactive-logout' );
25 }
26
27 /**
28 * Get all roles.
29 *
30 * @return array List of roles.
31 */
32 public static function getAllRoles() {
33 global $wp_roles;
34
35 $result = array();
36 foreach ( $wp_roles->roles as $role => $details ) {
37 $result[ $role ] = $details['name'];
38 }
39
40 $result['no_role'] = 'No User Roles assigned users';
41
42 return $result;
43 }
44
45 /**
46 * Check role is available in settings for multi-user.
47 *
48 * @param null|string $role Name of role, default is null.
49 *
50 * @return bool Returns true if passed role is available, Otherwise false.
51 */
52 public static function CheckRoleForMultiUser( $role = null ) {
53 $selected = false;
54 if ( ! empty( $role ) ) {
55 $ina_multiuser_settings = self::get_option( '__ina_multiusers_settings' );
56 if ( ! empty( $ina_multiuser_settings ) ) {
57 foreach ( $ina_multiuser_settings as $ina_multiuser_setting ) {
58 if ( in_array( $role, $ina_multiuser_setting, true ) ) {
59 $selected = true;
60 }
61 }
62 }
63 }
64
65 return $selected;
66 }
67
68 private static function get_bool_option( $key, $default = false ) {
69 return (bool) Helpers::get_option( $key ) ?? $default;
70 }
71
72 private static function get_option_with_default( $key, $default ) {
73 $value = Helpers::get_option( $key );
74
75 return ! empty( $value ) ? $value : $default;
76 }
77
78 /**
79 * Get whole settings or a desired setting
80 *
81 * @param $type
82 *
83 * @return false|mixed|string|null
84 */
85 public static function getSettings( $type = '' ) {
86 $settings = get_option( '__ina_general_settings' );
87 if ( ! empty( $type ) ) {
88 return ! empty( $settings[ $type ] ) ? $settings[ $type ] : '';
89 }
90
91 return $settings;
92 }
93
94 /**
95 * Get inactive logout settings
96 *
97 * @return \stdClass
98 */
99 public static function getInactiveSettingsData() {
100 $settings = new \stdClass();
101 $settings->logout_time = self::get_option_with_default( '__ina_logout_time', 15 * 60 );
102 $settings->prompt_countdown_timer = self::get_option_with_default( '__ina_countdown_timeout', 10 );
103 $settings->disable_prompt_timer = self::get_bool_option( '__ina_disable_countdown' );
104 $settings->warn_only_enable = self::get_bool_option( '__ina_warn_message_enabled' );
105 $settings->popup_behaviour = ( $settings->warn_only_enable && ! empty( Helpers::get_option( '__ina_popup_behaviour' ) ) ) ? Helpers::get_option( '__ina_popup_behaviour' ) : false;
106 $settings->concurrent_enabled = self::get_bool_option( '__ina_concurrent_login' );
107 $settings->enabled_redirect = self::get_bool_option( '__ina_enable_redirect' );
108 $settings->redirect_page_link = self::get_option_with_default( '__ina_redirect_page_link', false );
109 $settings->automatic_redirect = self::get_bool_option( '__ina_disable_automatic_redirect_on_logout' );
110 $settings->debugger = self::get_bool_option( '__ina_enable_debugger' );
111
112 if ( ! empty( Helpers::get_option( '__ina_enable_timeout_multiusers' ) ) ) {
113 global $current_user;
114 $user_roles = $current_user->roles ?? [];
115 $multi_user_settings = Helpers::get_option( '__ina_multiusers_settings' ) ?? [];
116 if ( ! empty( $multi_user_settings ) ) {
117 $result = array_values( array_filter( $multi_user_settings, function ( $data ) use ( $user_roles ) {
118 if ( empty( $user_roles ) ) {
119 return isset( $data['role'] ) && $data['role'] === 'no_role';
120 }
121
122 return isset( $data['role'] ) && in_array( $data['role'], $user_roles, true );
123 } ) );
124
125 if ( ! empty( $result ) ) {
126 $settings->advanced = $result[0];
127 }
128 }
129 }
130
131 return $settings;
132 }
133
134 /**
135 * Check if pro version is active
136 *
137 * @return bool
138 */
139 public static function is_pro_version_active() {
140 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
141 if ( is_plugin_active( 'inactive-logout-addon/inactive-logout-addon.php' ) ) {
142 return true;
143 } else {
144 return false;
145 }
146 }
147
148 /**
149 * Get Option based on multisite or only one site
150 *
151 * @param $key
152 *
153 * @return mixed|void
154 */
155 public static function get_option( $key ) {
156 if ( is_multisite() ) {
157 $network_id = get_main_network_id();
158 $override = get_network_option( $network_id, '__ina_overrideby_multisite_setting' );
159 if ( ! empty( $override ) ) {
160 return get_network_option( $network_id, $key );
161 }
162
163 if ( is_main_site() || is_network_admin() ) {
164 return get_site_option( $key );
165 } else {
166 return get_option( $key );
167 }
168 }
169
170 return get_option( $key );
171 }
172
173 public static function isMultisite(): bool {
174 return is_multisite() && get_current_blog_id() == get_main_network_id();
175 }
176
177 /**
178 * Update option
179 *
180 * @param $key
181 * @param $value
182 * @param bool $autoload
183 */
184 public static function update_option( $key, $value, $autoload = true ) {
185 if ( self::isMultisite() ) {
186 update_site_option( $key, $value );
187 } else {
188 update_option( $key, $value, $autoload );
189 }
190 }
191
192 /**
193 * Get Logout redirect page link.
194 *
195 * @param bool $settings
196 *
197 * @return false|mixed|string
198 */
199 public static function getLogoutRedirectPage( $settings = false ) {
200 if ( empty( $settings ) ) {
201 $settings = Helpers::getInactiveSettingsData();
202 }
203
204 if ( ! empty( $settings->advanced ) && ! empty( $settings->advanced['redirect_page'] ) ) {
205 $redirect_link = get_the_permalink( $settings->advanced['redirect_page'] );
206
207 return ! empty( $redirect_link ) ? $redirect_link : $settings->advanced['redirect_page'];
208 } elseif ( ! empty( $settings->enabled_redirect ) ) {
209 if ( 'custom-page-redirect' == $settings->redirect_page_link ) {
210 $ina_redirect_page_link = Helpers::get_option( '__ina_custom_redirect_text_field' );
211 $redirect_link = $ina_redirect_page_link;
212 } else {
213 $redirect_link = get_the_permalink( $settings->redirect_page_link );
214 }
215 }
216
217 return ! empty( $redirect_link ) ? $redirect_link : false;
218 }
219
220 /**
221 * Get the admin notice message
222 *
223 * @return mixed|null
224 */
225 public static function getMessage() {
226 $session_message = Helpers::get_option( '__ina_saved_options' );
227 if ( $session_message ) {
228 Helpers::update_option( '__ina_saved_options', '', false );
229
230 return $session_message;
231 }
232
233 return apply_filters( 'ina_admin_get_message', self::$message );
234 }
235
236 /**
237 * Set admin notice message
238 *
239 * @param $message
240 *
241 * @return void
242 */
243 public static function set_message( $message ) {
244 self::$message = $message;
245 }
246 }