class-w357-settings.php
10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/* ======================================================
# Login as User for WordPress - v1.4.5 (free version)
# -------------------------------------------------------
# For WordPress
# Author: Web357
# Copyright @ 2014-2022 Web357. All rights reserved.
# License: GNU/GPLv3, http://www.gnu.org/licenses/gpl-3.0.html
# Website: https:/www.web357.com
# Demo: https://demo.web357.com/wordpress/login-as-user/wp-admin/
# Support: support@web357.com
# Last modified: Wednesday 07 June 2023, 07:08:44 PM
========================================================= */
/**
* Define the internationalization functionality
*/
class LoginAsUser_settings {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* This fields
*
* @var [class]
*/
public $fields;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->fields = new LoginAsUser_fields();
}
/**
* Adds the option in WordPress Admin menu
*
* @since 1.0.0
* @access private
*/
public function options_page()
{
add_options_page(
esc_html__( 'Login as User settings', 'login-as-user'),
'Login as User',
'manage_options',
'login-as-user',
array($this, 'options_page_content')
);
}
/**
* Adds the admin page content
*
* @since 1.0.0
* @access public
*/
public function options_page_content()
{
include_once(plugin_dir_path( dirname( __FILE__ ) ) . 'admin/settings-view.php');
}
/**
* Function that will validate all fields.
*/
public function validateSettings( $fields )
{
$options = get_option( 'login_as_user_options' );
$valid_fields = array();
$message = null;
$type = null;
// Validate "redirect_to" Field
$redirect_to = trim( $fields['redirect_to'] );
$redirect_to = strip_tags( stripslashes( $redirect_to ) );
$redirect_to_frontend_url = esc_url_raw ( home_url('/') . $redirect_to );
if (wp_http_validate_url($redirect_to_frontend_url) && substr($redirect_to, 0, 1) != '/' && substr($redirect_to, 0, 1) != '\\' && substr($redirect_to, 0, 8) != 'wp-admin' && substr($redirect_to, 0, 8) != 'wp-login')
{
$message = __('Option saved successfully!', 'login-as-user');
$type = 'updated';
$valid_fields['redirect_to'] = $redirect_to;
}
else
{
$valid_fields['redirect_to'] = '';
$message = __('Error. The URL is not valid: ' . home_url('/') . $fields['redirect_to'] . '.', 'login-as-user');
$message .= (substr($redirect_to, 0, 1) == '/' || substr($redirect_to, 0, 1) == '\\') ? __('<br>Please remove the slash in front of URL.', 'login-as-user') : '';
$message .= (substr($redirect_to, 0, 8) == 'wp-admin') ? __('<br>You can\'t redirect the user to the admin page.', 'login-as-user') : '';
$message .= (substr($redirect_to, 0, 8) == 'wp-login') ? __('<br>You can\'t redirect the user to the login page.', 'login-as-user') : '';
$type = 'error';
}
// add_settings_error( $setting, $code, $message, $type )
add_settings_error('my_option_notice', 'my_option_notice', $message, $type);
// Validate "login_as_type" Field
$login_as_type = trim( $fields['login_as_type'] );
$login_as_type = strip_tags( stripslashes( $login_as_type ) );
$valid_fields['login_as_type'] = $login_as_type;
// Validate "login_as_type_characters_limit" Field
$login_as_type_characters_limit = trim( $fields['login_as_type_characters_limit'] );
$login_as_type_characters_limit = strip_tags( stripslashes( $login_as_type_characters_limit ) );
$valid_fields['login_as_type_characters_limit'] = $login_as_type_characters_limit;
// Validate "login_as_user_toolbar_position" Field
$login_as_user_toolbar_position = trim( $fields['login_as_user_toolbar_position'] );
$login_as_user_toolbar_position = strip_tags( stripslashes( $login_as_user_toolbar_position ) );
$valid_fields['login_as_user_toolbar_position'] = $login_as_user_toolbar_position;
// Validate "license_key" Field
$license_key = trim( $fields['license_key'] );
$license_key = strip_tags( stripslashes( $license_key ) );
$valid_fields['license_key'] = $license_key;
return apply_filters( 'validateSettings', $valid_fields, $fields);
}
/**
* Initialize the settings link
*
* @access public
*/
public function settings_link($links)
{
$link = 'options-general.php?page=' . 'login-as-user';
$settings_link = '<a href="'.esc_url($link).'">'.esc_html__( 'Settings', 'login-as-user' ).'</a>';
array_push( $links, $settings_link );
return $links;
}
/**
* Initialize the settings page
*
* @since 3.2.0
* @access public
*/
public function settings_init()
{
/**
* REGISTER SETTINGS
*/
register_setting( 'login-as-user', 'login_as_user_options', array($this, 'validateSettings'));
/**
* SECTIONS
*/
add_settings_section(
'base_settings_section',
'',
'',
'login-as-user'
);
/**
* Define Vars
*/
// Type of the Toolbar Content
$options = get_option( 'login_as_user_options' );
/**
* FIELDS
*/
// Link to redirect after login as user
add_settings_field(
'redirect_to',
__( 'URL Redirection', 'login-as-user' ),
array($this->fields, 'textField'),
'login-as-user',
'base_settings_section',
[
'label-for' => 'redirect_to',
'name' => 'redirect_to',
'class' => 'license_key',
'default_value' => '',
'size' => 60,
'maxlength' => 250,
'placeholder' => __('example: my-account/orders', 'login-as-user'),
'desc' => __('This is the URL path that you (Admin) will be redirected after logging in as a User.<br> For example: maybe you want to be redirected in user\'s orders page to see if an order has been completed successfully or cancelled, or just editing his profile details.<br>Leave it blank if you would like to be redirected to the home page (default)', 'login-as-user'),
'prefix' => '<span class="lac-input-prefix">'.home_url('/').'</span>'
]
);
// Login as... button
add_settings_field(
'login_as_type',
esc_html__( '"Login as...«option»" button', 'login-as-user' ),
array($this->fields, 'selectField'),
'login-as-user',
'base_settings_section',
[
'id' => 'login_as_type',
'default_value' => 'user_login',
'options' => [
['id' => '0', 'label' => esc_html__( 'Nickname (username)', 'login-as-user' ), 'value' => 'user_login'],
['id' => '1', 'label' => esc_html__( 'First name', 'login-as-user' ), 'value' => 'user_firstname'],
['id' => '2', 'label' => esc_html__( 'Last name', 'login-as-user' ), 'value' => 'user_lastname'],
['id' => '3', 'label' => esc_html__( 'Full name (first & last)', 'login-as-user' ), 'value' => 'user_fullname'],
['id' => '4', 'label' => esc_html__( 'None (display only the user icon)', 'login-as-user' ), 'value' => 'only_icon'],
],
'desc' => __('Choose which string will be displayed on the "Login as User" button.<br>For example Login as «Yiannis», or Login as «Christodoulou», or Login as «Johnathan99», or Login as «Yiannis Christodoulou».', 'login-as-user'),
]
);
// Characters limit of login as name
add_settings_field(
'login_as_type_characters_limit',
esc_html__( 'Show only the first X characters on the "Login as...«option»" button', 'login-as-user' ),
array($this->fields, 'selectField'),
'login-as-user',
'base_settings_section',
[
'id' => 'login_as_type_characters_limit',
'default_value' => '0',
'options' => [
['id' => '0', 'label' => esc_html__('All characters (default)', 'login-as-user'), 'value' => '0'],
['id' => '1', 'label' => '1', 'value' => '1'],
['id' => '2', 'label' => '2', 'value' => '2'],
['id' => '3', 'label' => '3', 'value' => '3'],
['id' => '4', 'label' => '4', 'value' => '4'],
['id' => '5', 'label' => '5', 'value' => '5'],
['id' => '6', 'label' => '6', 'value' => '6'],
['id' => '7', 'label' => '7', 'value' => '7'],
['id' => '8', 'label' => '8', 'value' => '8'],
['id' => '9', 'label' => '9', 'value' => '9'],
['id' => '10', 'label' => '10', 'value' => '10'],
['id' => '11', 'label' => '11', 'value' => '11'],
['id' => '12', 'label' => '12', 'value' => '12'],
['id' => '13', 'label' => '13', 'value' => '13'],
['id' => '14', 'label' => '14', 'value' => '14'],
['id' => '15', 'label' => '15', 'value' => '15'],
],
'desc' => __('Show only the first X characters of the username, or first/last name, or full name, on the "Login as...«option»" button. <br>For example, if you choose 5, the button will be displayed as Login as «Yiann...», or Login as «Chris...», or Login as «Johna...», or Login as «Yiann...».', 'login-as-user'),
]
);
// Characters limit of login as name
add_settings_field(
'login_as_user_toolbar_position',
esc_html__( 'Toolbar\'s position', 'login-as-user' ),
array($this->fields, 'selectField'),
'login-as-user',
'base_settings_section',
[
'id' => 'login_as_user_toolbar_position',
'default_value' => 'top',
'options' => [
['id' => 'top', 'label' => esc_html__('Top', 'login-as-user'), 'value' => 'top'],
['id' => 'bottom', 'label' => esc_html__('Bottom', 'login-as-user'), 'value' => 'bottom'],
],
'desc' => __('Choose the position of the "Login as user" toolbar.', 'login-as-user'),
]
);
// License Key
add_settings_field(
'license_key',
esc_html__( 'License Key', 'login-as-user' ),
array($this->fields, 'hiddenField'),
'login-as-user',
'base_settings_section',
[
'label-for' => 'license_key',
'name' => 'license_key',
'class' => 'license_key hidden',
'default_value' => '',
'size' => 60,
'maxlength' => 60,
'placeholder' => __('Enter your license key from web357.com', 'login-as-user'),
'desc' => __('In order to update commercial Web357 plugins, you have to enter the Web357 License Key.<br>You can find the License Key in your account settings at Web357.com, in the <a href="//www.web357.com/my-account/web357-license-manager" target="_blank"><strong>Web357 License Key Manager</strong></a> section.', 'login-as-user')
]
);
}
}