PluginProbe ʕ •ᴥ•ʔ
Admin and Site Enhancements (ASE) / 5.2.0
Admin and Site Enhancements (ASE) v5.2.0
9.1.1 9.1.0 9.0.2 9.0.1 9.0.0 8.9.2 8.9.1 8.9.0 8.8.8 8.8.7 8.8.6 8.8.5 8.8.4 8.8.3 8.8.2 8.8.1 8.8.0 8.7.3 8.7.2 8.7.1 8.2.1 8.2.2 8.2.3 8.3.0 8.3.1 8.3.2 8.4.0 8.4.1 8.4.2 8.5.0 8.5.1 8.5.2 8.6.0 8.6.1 8.6.2 8.7.0 5.0.1 5.0.2 5.0.2.1 5.0.2.2 5.0.2.3 5.0.2.4 5.1.0 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1 5.5.0 5.5.1 5.5.2 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.8.0 5.8.1 6.0.0 6.0.3 6.0.4 6.0.5 6.0.5.1 6.0.6 6.0.7 6.0.8.1 6.1.0 6.1.3 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.2.7 6.3.0 6.3.1 6.3.2 6.4.0 6.5.0 6.5.1 6.6.0 6.7.0 6.8.0 6.8.2 6.8.3 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13.1 6.9.13.2 6.9.2 6.9.3 6.9.4 6.9.5 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.2 7.0.2.1 7.0.2.2 7.0.2.3 7.0.3 7.1.0 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2.0 7.2.1 7.3.0 7.3.1 7.3.2 7.3.3 7.4.0 7.4.2 7.4.4 7.4.5 7.4.6 7.4.7 7.4.8 7.5.0 7.5.1 7.5.2 7.5.3 7.5.4 7.6.0 7.6.1 7.6.1.1 7.6.10 7.6.11 7.6.2 7.6.3 7.6.4 7.6.5 7.6.6 7.6.7 7.6.7.1 7.6.8 7.6.9 7.7.0 7.8.0 7.8.1 7.8.10 7.8.11 7.8.12 7.8.13 7.8.14 7.8.15 7.8.16 7.8.17 7.8.18 7.8.2 7.8.3 7.8.4 7.8.5 7.8.5.1 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1 7.9.10 7.9.11 7.9.2 7.9.3 7.9.4 7.9.5 7.9.6 7.9.7 7.9.8 7.9.9 8.0.0 8.0.1 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.1.0 8.1.1 8.1.2 8.1.3 8.1.4 8.2.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.2.0 3.3.0 3.4.0 3.5.0 3.6.1 3.7.0 3.8.0 3.9.0 3.9.1 3.9.2 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.9.0 4.9.1 4.9.2 4.9.3 5.0.0
admin-site-enhancements / classes / class-utilities.php
admin-site-enhancements / classes Last commit date
class-activation.php 3 years ago class-admin-interface.php 3 years ago class-common-methods.php 3 years ago class-content-management.php 3 years ago class-custom-code.php 3 years ago class-deactivation.php 3 years ago class-disable-components.php 3 years ago class-login-logout.php 3 years ago class-optimizations.php 3 years ago class-security.php 3 years ago class-settings-fields-render.php 3 years ago class-settings-sanitization.php 3 years ago class-settings-sections-fields.php 3 years ago class-utilities.php 3 years ago
class-utilities.php
960 lines
1 <?php
2
3 namespace ASENHA\Classes;
4 use WP_Error;
5
6 /**
7 * Class related to Utilities features
8 *
9 * @since 1.5.0
10 */
11 class Utilities {
12
13 /**
14 * Add UI to enable multiple user roles selection
15 *
16 * @since 4.8.0
17 */
18 public function add_multiple_roles_ui( $user ) {
19
20 // Get user roles that the current user is allowed to edit
21 $roles = get_editable_roles();
22
23 // Get the roles of the user being shown / edited / created
24 if ( ! empty( $user->roles ) ) {
25 $user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) ); // indexed array of role slugs
26 } else {
27 $user_roles = array();
28 }
29
30 // Only show roles checkboxes for users that can assign roles to other users
31 if ( current_user_can( 'promote_users', get_current_user_id() ) ) {
32
33 ?>
34 <div class="asenha-roles-temporary-container">
35 <table class="form-table">
36 <tr>
37 <th>
38 <label>Roles</label>
39 </th>
40 <td>
41 <?php
42 foreach ( $roles as $role_slug => $role_info ) {
43
44 $checkbox_id = $role_slug . '_role';
45 $role_name = translate_user_role( $role_info['name'] );
46 if ( ! empty( $user_roles ) && in_array( $role_slug, $user_roles ) ) {
47 $checked = 'checked="checked"';
48 } else {
49 $checked = '';
50 }
51
52 // Output roles checkboxes
53 ?>
54 <label for="<?php esc_attr_e( $checkbox_id ); ?>"><input type="checkbox" id="<?php esc_attr_e( $checkbox_id ); ?>" value="<?php esc_attr_e( $role_slug ); ?>" name="asenha_assigned_roles[]" <?php esc_attr_e( $checked ) ?> /> <?php esc_html_e( $role_name ); ?></label><br />
55 <?php
56
57 }
58
59 wp_nonce_field( 'asenha_set_multiple_roles', 'asenha_multiple_roles_nonce' );
60
61 ?>
62 </td>
63 </tr>
64 </table>
65 </div>
66 <?php
67
68 }
69
70 }
71
72 /**
73 * Save changes in roles assignment
74 *
75 * @since 4.8.0
76 */
77 public function save_roles_assignment( $user_id ) {
78
79 if ( ! current_user_can( 'promote_users', get_current_user_id() ) || ! wp_verify_nonce( $_POST['asenha_multiple_roles_nonce'], 'asenha_set_multiple_roles' ) ) {
80 return;
81 }
82
83 // Get user roles that the current user is allowed to edit
84 $roles = get_editable_roles();
85
86 // Get the roles of the user being shown / edited / created
87 $user = get_user_by( 'id', (int) $user_id ); // WP_User object
88 $user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) ); // Current/existing roles
89
90 if ( ! empty( $_POST['asenha_assigned_roles'] ) ) {
91
92 $assigned_roles = array_map( 'sanitize_text_field', $_POST['asenha_assigned_roles'] );
93
94 // Make sure only valid roles are processed
95 $assigned_roles = array_intersect( $assigned_roles, array_keys( $roles ) );
96
97 $roles_to_remove = array();
98 $roles_to_add = array();
99
100 if ( empty( $assigned_roles ) ) {
101
102 // Remove all current/existing roles
103 $roles_to_remove = $user_roles;
104
105 } else {
106
107 // Identify and remove roles not present in the newly assigned roles
108 $roles_to_remove = array_diff( $user_roles, $assigned_roles );
109
110 if ( ! empty( $roles_to_remove ) ) {
111 foreach ( $roles_to_remove as $role_to_remove ) {
112 $user->remove_role( $role_to_remove );
113 }
114 }
115
116 // Identify and add roles not present in the existing roles
117 $roles_to_add = array_diff( $assigned_roles, $user_roles );
118
119 if ( ! empty( $roles_to_add ) ) {
120 foreach ( $roles_to_add as $role_to_add ) {
121 $user->add_role( $role_to_add );
122 }
123 }
124
125 }
126
127 }
128
129 }
130
131 /**
132 * Add menu bar item to view admin as one of the user roles
133 *
134 * @param $wp_admin_bar The WP_Admin_Bar instance
135 * @link https://developer.wordpress.org/reference/hooks/admin_bar_menu/
136 * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/
137 * @since 1.8.0
138 */
139 public function view_admin_as_admin_bar_menu( $wp_admin_bar ) {
140
141 $current_user = wp_get_current_user();
142 $current_user_roles = array_values( $current_user->roles ); // indexed array
143
144 // Get which role slug is currently set to "View as"
145 $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true );
146
147 if ( empty( $viewing_admin_as ) ) {
148 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
149 }
150
151 // Get the role name, translated if available, from the role slug
152 $wp_roles = wp_roles()->roles;
153
154 foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) {
155
156 if ( $wp_role_slug == $viewing_admin_as ) {
157
158 $viewing_admin_as_role_name = $wp_role_info['name'];
159
160 }
161
162 }
163
164 if ( ! isset( $viewing_admin_as_role_name ) ) {
165
166 $viewing_admin_as_role_name = $viewing_admin_as;
167
168 }
169
170 $translated_name_for_viewing_admin_as = ucfirst( $viewing_admin_as_role_name );
171
172 // Add parent menu basd on the role being set to "View as"
173
174 if ( 'administrator' == $viewing_admin_as ) {
175
176 if ( in_array( 'administrator', $current_user_roles ) ) {
177
178 // Add parent menu for administrators
179 $wp_admin_bar->add_menu( array(
180 'id' => 'asenha-view-admin-as-role',
181 'parent' => 'top-secondary',
182 'title' => 'View as <span style="font-size:0.8125em;">&#9660;</span>',
183 'href' => '#',
184 'meta' => array(
185 'title' => 'View admin pages and the site (logged-in) as one of the following user roles.'
186 ),
187 ) );
188
189 }
190
191 } else {
192
193 // Add parent menu
194 $wp_admin_bar->add_menu( array(
195 'id' => 'asenha-view-admin-as-role',
196 'parent' => 'top-secondary',
197 'title' => 'Viewing as ' . $translated_name_for_viewing_admin_as . ' <span style="font-size:0.8125em;">&#9660;</span>',
198 'href' => '#',
199 ) );
200
201 }
202
203 // Get available role(s) to switch to
204 $roles_to_switch_to = $this->get_roles_to_switch_to();
205
206 // Add role(s) to switch to as sub-menu
207
208 if ( 'administrator' == $viewing_admin_as ) {
209
210 if ( in_array( 'administrator', $current_user_roles ) ) {
211
212 // Add submenu for each role other than Administrator
213
214 $i = 1;
215
216 foreach ( $roles_to_switch_to as $role_slug => $data ) {
217
218 $wp_admin_bar->add_menu( array(
219 'id' => 'role' . $i . '_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager
220 'parent' => 'asenha-view-admin-as-role',
221 'title' => $data['role_name'], // role name, e.g. Editor, Shop Manager
222 'href' => $data['nonce_url'], // nonce URL for each role
223 ) );
224
225 $i++;
226
227 }
228
229 }
230
231 } else {
232
233 // Add submenu to switch back to Administrator role
234
235 foreach ( $roles_to_switch_to as $role_slug => $data ) {
236
237 $wp_admin_bar->add_menu( array(
238 'id' => 'role_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager
239 'parent' => 'asenha-view-admin-as-role',
240 'title' => 'Switch back to ' . $data['role_name'], // role name, e.g. Editor, Shop Manager
241 'href' => $data['nonce_url'], // nonce URL for each role
242
243 ) );
244
245 }
246
247 }
248
249 }
250
251 /**
252 * Get roles availble to switch to
253 *
254 * @since 1.8.0
255 */
256 private function get_roles_to_switch_to() {
257
258 $current_user = wp_get_current_user();
259 $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s)
260
261 // Get full list of roles defined in WordPress
262 $wp_roles = wp_roles()->roles;
263
264 $roles_to_switch_to = array();
265
266 // Get which role slug is currently active for viewing
267 $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true );
268
269 if ( 'administrator' == $viewing_admin_as ) {
270
271 // Exclude 'Administrator' from the "View as" menu
272
273 foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) {
274
275 if ( ! in_array( $wp_role_slug,$current_user_role_slugs ) ) {
276
277 $roles_to_switch_to[$wp_role_slug] = array(
278 'role_name' => $wp_role_info['name'], // role name, e.g. Editor, Shop Manager
279 'nonce_url' => wp_nonce_url(
280 add_query_arg( array(
281 'action' => 'switch_role_to',
282 'role' => $wp_role_slug,
283 ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action
284 'asenha_view_admin_as_' . $wp_role_slug, // the nonce $action name
285 'nonce' // the nonce url parameter name
286 ) // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df
287 );
288
289 }
290
291 }
292
293 } else {
294
295 // Only show switch back to Administrator in the "View as" menu
296
297 $roles_to_switch_to['administrator'] = array(
298 'role_name' => 'Administrator', // role name, e.g. Editor, Shop Manager
299 'nonce_url' => wp_nonce_url(
300 add_query_arg( array(
301 'action' => 'switch_back_to_administrator',
302 'role' => 'administrator',
303 ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action
304 'asenha_view_admin_as_administrator', // the nonce $action name
305 'nonce' // the nonce url parameter name
306 ) // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df
307 );
308 }
309
310 return $roles_to_switch_to; // array of $role_slug => $nonce_url
311
312 }
313
314 /**
315 * Switch user role to view admin and site
316 *
317 * @since 1.8.0
318 */
319 public function role_switcher_to_view_admin_as() {
320
321 $current_user = wp_get_current_user();
322 $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s)
323
324 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['role'] ) && isset( $_REQUEST['nonce'] ) ) {
325
326 $action = sanitize_text_field( $_REQUEST['action'] );
327 $new_role = sanitize_text_field( $_REQUEST['role'] );
328 $nonce = sanitize_text_field( $_REQUEST['nonce'] );
329
330 if ( 'switch_role_to' === $action ) {
331
332 // Check nonce validity and role existence
333
334 $wp_roles = array_keys( wp_roles()->roles ); // indexed array of all WP roles
335
336 if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_' . $new_role ) || ! in_array( $new_role, $wp_roles ) ) {
337 return; // cancel role switching
338 }
339
340 // Get original roles (before role switching) of the current user
341 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
342
343 // Store original user role(s) before switching it to another role
344
345 if ( empty( $original_role_slugs ) ) {
346
347 update_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', $current_user_role_slugs );
348
349 }
350
351 // Remove all current roles from current user.
352 foreach ( $current_user_role_slugs as $current_user_role_slug ) {
353
354 $current_user->remove_role( $current_user_role_slug );
355
356 }
357
358 // Add new role to current user
359 $current_user->add_role( $new_role );
360
361 // Mark that the user has switched to a non-administrator role
362 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', $new_role );
363
364 // Redirect to admin dashboard
365 wp_safe_redirect( get_admin_url() );
366 exit;
367
368 }
369
370 if ( 'switch_back_to_administrator' === $action ) {
371
372 // Check nonce validity
373
374 if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_administrator' ) || ( $new_role != 'administrator' ) ) {
375 return; // cancel role switching
376 }
377
378 // Remove all current roles from current user.
379 foreach ( $current_user_role_slugs as $current_role_slug ) {
380
381 $current_user->remove_role( $current_role_slug );
382
383 }
384
385 // Get original roles (before role switching) of the current user
386 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
387
388 // Add the original roles to the current user
389 foreach ( $original_role_slugs as $original_role_slug ) {
390
391 $current_user->add_role( $original_role_slug );
392
393 }
394
395 // Mark that the user has switched back to an administrator role
396 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
397
398 }
399
400 } elseif ( isset( $_REQUEST['reset-view-as'] ) ) {
401
402 $reset_view_as = sanitize_text_field( $_REQUEST['reset-view-as'] );
403
404 if ( $reset_view_as == 'yes' ) {
405
406 // Remove all current roles from current user.
407 foreach ( $current_user_role_slugs as $current_role_slug ) {
408
409 $current_user->remove_role( $current_role_slug );
410
411 }
412
413 // Get original roles (before role switching) of the current user
414 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
415
416 // Add the original roles to the current user
417 foreach ( $original_role_slugs as $original_role_slug ) {
418
419 $current_user->add_role( $original_role_slug );
420
421 }
422
423 // Mark that the user has switched back to an administrator role
424 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
425
426 // Redirect to admin dashboard
427 wp_safe_redirect( get_admin_url() );
428 exit;
429
430 }
431
432 }
433
434 }
435
436 /**
437 * Show custom error page on switch failure, which causes inability to view admin dashboard/pages
438 *
439 * @since 1.8.0
440 */
441 public function custom_error_page_on_switch_failure( $callback ) {
442
443 ?>
444 <!DOCTYPE html>
445 <html>
446 <head>
447 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8; ?>" />
448 <meta name="viewport" content="width=device-width">
449 <title>WordPress Error</title>
450 <style type="text/css">
451 html {
452 background: #f1f1f1;
453 }
454 body {
455 background: #fff;
456 border: 1px solid #ccd0d4;
457 color: #444;
458 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
459 margin: 2em auto;
460 padding: 1em 2em;
461 max-width: 700px;
462 -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
463 box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
464 }
465 h1 {
466 border-bottom: 1px solid #dadada;
467 clear: both;
468 color: #666;
469 font-size: 24px;
470 margin: 30px 0 0 0;
471 padding: 0;
472 padding-bottom: 7px;
473 }
474 #error-page {
475 margin-top: 50px;
476 }
477 #error-page p,
478 #error-page .wp-die-message {
479 font-size: 14px;
480 line-height: 1.5;
481 margin: 20px 0;
482 }
483 #error-page .wp-die-message:last-of-type {
484 display: none;
485 }
486 #error-page code {
487 font-family: Consolas, Monaco, monospace;
488 }
489 a {
490 color: #0073aa;
491 }
492 a:hover,
493 a:active {
494 color: #006799;
495 }
496 a:focus {
497 color: #124964;
498 -webkit-box-shadow:
499 0 0 0 1px #5b9dd9,
500 0 0 2px 1px rgba(30, 140, 190, 0.8);
501 box-shadow:
502 0 0 0 1px #5b9dd9,
503 0 0 2px 1px rgba(30, 140, 190, 0.8);
504 outline: none;
505 }
506 </style>
507 </head>
508 <body id="error-page">
509 <div class="wp-die-message">Something went wrong. <a href="<?php echo home_url( '?reset-view-as=yes' ); ?>">Click here</a> to go back to the admin dashboard.</div>
510 </body>
511 </html>
512 <?php
513
514 }
515
516 /**
517 * Show Password Protection admin bar status icon
518 *
519 * @since 4.1.0
520 */
521 public function show_password_protection_admin_bar_icon() {
522 add_action( 'wp_before_admin_bar_render', [ $this, 'add_password_protection_admin_bar_item' ] );
523 add_action( 'admin_head', [ $this, 'add_password_protection_admin_bar_item_styles' ] );
524 add_action( 'wp_head', [ $this, 'add_password_protection_admin_bar_item_styles' ] );
525 }
526
527 /**
528 * Add WP Admin Bar item
529 *
530 * @since 4.1.0
531 */
532 public function add_password_protection_admin_bar_item() {
533 global $wp_admin_bar;
534
535 if ( is_user_logged_in() ) {
536 if ( current_user_can( 'manage_options' ) ) {
537 $wp_admin_bar->add_menu( array(
538 'id' => 'password_protection',
539 'title' => '',
540 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ),
541 'meta' => array(
542 'title' => 'Password protection is currently enabled for this site.',
543 ),
544 ) );
545 }
546 }
547
548 }
549
550 /**
551 * Add icon and CSS for admin bar item
552 *
553 * @since 4.1.0
554 */
555 public function add_password_protection_admin_bar_item_styles() {
556
557 if ( is_user_logged_in() ) {
558 if ( current_user_can( 'manage_options' ) ) {
559
560 ?>
561 <style>
562 #wp-admin-bar-password_protection {
563 background-color: #c32121 !important;
564 transition: .25s;
565 }
566 #wp-admin-bar-password_protection > .ab-item {
567 color: #fff !important;
568 }
569 #wp-admin-bar-password_protection > .ab-item:before {
570 content: "\f160";
571 top: 2px;
572 color: #fff !important;
573 margin-right: 0px;
574 }
575 #wp-admin-bar-password_protection:hover > .ab-item {
576 background-color: #af1d1d !important;
577 color: #fff;
578 }
579 </style>
580 <?php
581
582 }
583 }
584
585 }
586
587 /**
588 * Disable page caching
589 *
590 * @since 4.1.0
591 */
592 public function maybe_disable_page_caching() {
593
594 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
595 define( 'DONOTCACHEPAGE', true );
596 }
597
598 }
599
600 /**
601 * Maybe show login form
602 *
603 * @since 4.1.0
604 */
605 public function maybe_show_login_form() {
606
607 // When user is logged-in as in an administrator
608 if ( is_user_logged_in() ) {
609 if ( current_user_can( 'manage_options' ) ) {
610 return; // Do not load login form or perform redirection to the login form
611 }
612 }
613
614 // When site visitor has entered correct password
615 $auth_cookie = isset( $_COOKIE['asenha_password_protection'] ) ? $_COOKIE['asenha_password_protection'] : '';
616
617 if ( 'authenticated' == $auth_cookie ) {
618 return; // Do not load login form or perform redirection to the login form
619 }
620
621 if ( isset( $_REQUEST['protected-page'] ) && 'view' == $_REQUEST['protected-page'] ) {// Show login form
622
623 $password_protected_login_page_template = ASENHA_PATH . 'includes/password-protected-login.php';
624
625 load_template( $password_protected_login_page_template );
626 exit();
627
628 } else { // Redirect to login form
629
630 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( $_SERVER['HTTP_HOST'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] );
631
632 $args = array(
633 'protected-page' => 'view',
634 'source' => urlencode( $current_url ),
635 );
636
637 $pwd_protect_login_url = add_query_arg( $args, home_url() );
638
639 nocache_headers();
640 wp_safe_redirect( $pwd_protect_login_url );
641 exit();
642
643 }
644
645 }
646
647 /**
648 * Maybe process login to access protected page content
649 *
650 * @since 4.1.0
651 */
652 public function maybe_process_login() {
653
654 global $password_protected_errors;
655 $password_protected_errors = new WP_Error();
656
657 if ( isset( $_REQUEST['protected_page_pwd'] ) ) {
658
659 $password_input = $_REQUEST['protected_page_pwd'];
660
661 $options = get_option( ASENHA_SLUG_U, array() );
662 $stored_password = $options['password_protection_password'];
663
664 if ( ! empty( $password_input ) ) {
665
666 if ( $password_input == $stored_password ) { // Password is correct
667
668 // Set auth cookie
669 // $expiration = time() + DAY_IN_SECONDS; // in 24 hours
670 $expiration = 0; // by the end of browsing session
671 setcookie( 'asenha_password_protection', 'authenticated', $expiration, COOKIEPATH, COOKIE_DOMAIN, true, true );
672
673 // Redirect
674 $redirect_to_url = isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : '';
675 wp_safe_redirect( $redirect_to_url );
676 exit();
677
678 } else { // Password is incorrect
679
680 // Add error message
681 $password_protected_errors->add( 'incorrect_password', 'Incorrect password.' );
682
683 }
684
685 } else { // Password input is empty
686
687 // Add error message
688 $password_protected_errors->add( 'empty_password', 'Password can not be empty.' );
689
690 }
691
692 }
693 }
694
695 /**
696 * Add custom login error messages
697 *
698 * @since 4.1.0
699 */
700 public function add_login_error_messages() {
701
702 global $password_protected_errors;
703
704 if ( $password_protected_errors->get_error_code() ) {
705
706 $messages = '';
707 $errors = '';
708
709 // Extract the error message
710
711 foreach( $password_protected_errors->get_error_codes() as $code ) {
712
713 $severity = $password_protected_errors->get_error_data( $code );
714
715 foreach( $password_protected_errors->get_error_messages( $code ) as $error ) {
716 if ( 'message' == $severity ) {
717 $messages .= $error . '<br />';
718 } else {
719 $errors .= $error . '<br />';
720 }
721 }
722
723 }
724
725 // Output the error message
726
727 if ( ! empty( $messages ) ) {
728 echo '<p class="message">' . $messages . '</p>';
729 }
730
731 if ( ! empty( $errors ) ) {
732 echo '<div id="login_error">' . $errors . '</div>';
733 }
734
735 }
736
737 }
738
739 /**
740 * Redirect 404 to homepage
741 *
742 * @since 1.7.0
743 */
744 public function redirect_404_to_homepage() {
745
746 if ( ! is_404() || is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) {
747
748 return;
749
750 } else {
751
752 // wp_safe_redirect( home_url(), 301 );
753
754 header( 'HTTP/1.1 301 Moved Permanently');
755 header( 'Location: ' . home_url() );
756 exit();
757
758 }
759
760 }
761
762 /**
763 * Send emails using external SMTP service
764 *
765 * @since 4.6.0
766 */
767 public function deliver_email_via_smtp( $phpmailer ) {
768
769 $options = get_option( ASENHA_SLUG_U, array() );
770 $smtp_host = $options['smtp_host'];
771 $smtp_port = $options['smtp_port'];
772 $smtp_security = $options['smtp_security'];
773 $smtp_username = $options['smtp_username'];
774 $smtp_password = $options['smtp_password'];
775 $smtp_default_from_name = $options['smtp_default_from_name'];
776 $smtp_default_from_email = $options['smtp_default_from_email'];
777 $smtp_force_from = $options['smtp_force_from'];
778 $smtp_debug = $options['smtp_debug'];
779
780 // Do nothing if host or password is empty
781 if ( empty( $smtp_host ) || empty( $smtp_password ) ) {
782 return;
783 }
784
785 // Send using SMTP
786 $phpmailer->isSMTP(); // phpcs:ignore
787
788 // Enanble SMTP authentication
789 $phpmailer->SMTPAuth = true; // phpcs:ignore
790
791 // Set some other defaults
792 // $phpmailer->CharSet = 'utf-8'; // phpcs:ignore
793 $phpmailer->XMailer = 'Admin and Site Enhancements v' . ASENHA_VERSION . ' - a WordPress plugin'; // phpcs:ignore
794
795 $phpmailer->Host = $smtp_host; // phpcs:ignore
796 $phpmailer->Port = $smtp_port; // phpcs:ignore
797 $phpmailer->SMTPSecure = $smtp_security; // phpcs:ignore
798 $phpmailer->Username = $smtp_username; // phpcs:ignore
799 $phpmailer->Password = $smtp_password; // phpcs:ignore
800
801 // Maybe override FROM email and/or name if the sender is "WordPress <wordpress@sitedomain.com>", the default from WordPress core and not yet overridden by another plugin.
802
803 $from_name = $phpmailer->FromName;
804 $from_email_beginning = substr( $phpmailer->From, 0, 9 ); // Get the first 9 characters of the current FROM email
805
806 if ( $smtp_force_from ) {
807 $phpmailer->FromName = $smtp_default_from_name;
808 $phpmailer->From = $smtp_default_from_email;
809 } else {
810 if ( ( 'WordPress' === $from_name ) && ! empty( $smtp_default_from_name ) ) {
811 $phpmailer->FromName = $smtp_default_from_name;
812 }
813
814 if ( ( 'wordpress' === $from_email_beginning ) && ! empty( $smtp_default_from_email ) ) {
815 $phpmailer->From = $smtp_default_from_email;
816 }
817 }
818
819 // If debug mode is enabled, send debug info (SMTP::DEBUG_CONNECTION) to WordPress debug.log file set in wp-config.php
820 // Reference: https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging
821
822 if ( $smtp_debug ) {
823 $phpmailer->SMTPDebug = 3; //phpcs:ignore
824 $phpmailer->Debugoutput = 'error_log'; //phpcs:ignore
825 }
826
827 }
828
829 /**
830 * Redirect for when maintenance mode is enabled
831 *
832 * @since 4.7.0
833 */
834 public function maintenance_mode_redirect() {
835
836 $current_url = sanitize_text_field( $_SERVER['REQUEST_URI'] );
837 $current_url_parts = explode( '/', $current_url );
838
839 // Bypass wp-admin pages and logged-in administrator on the frontend
840 if ( ! in_array( 'wp-admin', $current_url_parts ) || ( false !== strpos( 'wp-login.php', $current_url ) ) ) {
841 if ( ! current_user_can( 'manage_options' ) ) {
842
843 header( 'HTTP/1.1 503 Service Unavailable', true, 503 );
844 header( 'Status: 503 Service Unavailable' );
845 header( 'Retry-After: 3600' ); // Tell search engine bots to return after 3600 seconds, i.e. 1 hour
846
847 $options = get_option( ASENHA_SLUG_U, array() );
848 $heading = $options['maintenance_page_heading'];
849 $description = $options['maintenance_page_description'];
850 $background = $options['maintenance_page_background'];
851
852 if ( 'lines' === $background ) { // https://bgjar.com/curve-line
853 $background_image = "url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:svgjs='http://svgjs.com/svgjs' width='1920' height='1280' preserveAspectRatio='none' viewBox='0 0 1920 1280'%3e%3cg mask='url(%26quot%3b%23SvgjsMask1804%26quot%3b)' fill='none'%3e%3crect width='1920' height='1280' x='0' y='0' fill='url(%23SvgjsLinearGradient1805)'%3e%3c/rect%3e%3cpath d='M2294.46 927.36C2128.65 934.22 2078.52 1270.56 1693.36 1208.96 1308.19 1147.36 1373.24 145.96 1092.25-67.11' stroke='rgba(158%2c 160%2c 161%2c 0.57)' stroke-width='2'%3e%3c/path%3e%3cpath d='M2225.25 303.97C1963.34 332.56 1808.36 909.76 1359.97 905.57 911.59 901.38 820.47-55.06 494.7-167.42' stroke='rgba(158%2c 160%2c 161%2c 0.57)' stroke-width='2'%3e%3c/path%3e%3cpath d='M2247.58 281.19C2070.08 293.95 1967.68 651 1632.53 639.59 1297.39 628.18 1265.17-143.39 1017.49-253.69' stroke='rgba(158%2c 160%2c 161%2c 0.57)' stroke-width='2'%3e%3c/path%3e%3cpath d='M1924.29 917.21C1696.21 904.78 1584.63 530.74 1114.13 494.81 643.63 458.88 546.92-26.2 303.97-50.85' stroke='rgba(158%2c 160%2c 161%2c 0.57)' stroke-width='2'%3e%3c/path%3e%3cpath d='M2009.59 400.31C1847.79 399.06 1696.02 240.31 1382.45 240.31 1068.87 240.31 1083.3 404.62 755.3 400.31 427.31 396 332.72-108.61 128.16-144.89' stroke='rgba(158%2c 160%2c 161%2c 0.57)' stroke-width='2'%3e%3c/path%3e%3c/g%3e%3cdefs%3e%3cmask id='SvgjsMask1804'%3e%3crect width='1920' height='1280' fill='white'%3e%3c/rect%3e%3c/mask%3e%3clinearGradient x1='8.33%25' y1='-12.5%25' x2='91.67%25' y2='112.5%25' gradientUnits='userSpaceOnUse' id='SvgjsLinearGradient1805'%3e%3cstop stop-color='rgba(255%2c 255%2c 255%2c 1)' offset='0'%3e%3c/stop%3e%3cstop stop-color='rgba(193%2c 192%2c 192%2c 1)' offset='1'%3e%3c/stop%3e%3c/linearGradient%3e%3c/defs%3e%3c/svg%3e\")";
854 } elseif ( 'stripes' === $background ) { // https://bgjar.com/shiny-overlay
855 $background_image = "url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' version='1.1' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:svgjs='http://svgjs.com/svgjs' width='2560' height='2560' preserveAspectRatio='none' viewBox='0 0 2560 2560'%3e%3cg mask='url(%26quot%3b%23SvgjsMask1276%26quot%3b)' fill='none'%3e%3crect width='2560' height='2560' x='0' y='0' fill='url(%23SvgjsLinearGradient1277)'%3e%3c/rect%3e%3cpath d='M0 0L524.59 0L0 986.23z' fill='rgba(255%2c 255%2c 255%2c .1)'%3e%3c/path%3e%3cpath d='M0 986.23L524.59 0L684.6500000000001 0L0 1251.4z' fill='rgba(255%2c 255%2c 255%2c .075)'%3e%3c/path%3e%3cpath d='M0 1251.4L684.6500000000001 0L1140.02 0L0 1816.94z' fill='rgba(255%2c 255%2c 255%2c .05)'%3e%3c/path%3e%3cpath d='M0 1816.94L1140.02 0L1666.1399999999999 0L0 1973.71z' fill='rgba(255%2c 255%2c 255%2c .025)'%3e%3c/path%3e%3cpath d='M2560 2560L1477.86 2560L2560 2129.39z' fill='rgba(0%2c 0%2c 0%2c .1)'%3e%3c/path%3e%3cpath d='M2560 2129.39L1477.86 2560L669.0099999999999 2560L2560 1244.5099999999998z' fill='rgba(0%2c 0%2c 0%2c .075)'%3e%3c/path%3e%3cpath d='M2560 1244.51L669.0099999999998 2560L531.5999999999998 2560L2560 928.88z' fill='rgba(0%2c 0%2c 0%2c .05)'%3e%3c/path%3e%3cpath d='M2560 928.8800000000001L531.5999999999997 2560L354.62999999999965 2560L2560 697.8700000000001z' fill='rgba(0%2c 0%2c 0%2c .025)'%3e%3c/path%3e%3c/g%3e%3cdefs%3e%3cmask id='SvgjsMask1276'%3e%3crect width='2560' height='2560' fill='white'%3e%3c/rect%3e%3c/mask%3e%3clinearGradient x1='0%25' y1='0%25' x2='100%25' y2='100%25' gradientUnits='userSpaceOnUse' id='SvgjsLinearGradient1277'%3e%3cstop stop-color='rgba(255%2c 255%2c 255%2c 1)' offset='0'%3e%3c/stop%3e%3cstop stop-color='rgba(172%2c 172%2c 172%2c 1)' offset='1'%3e%3c/stop%3e%3c/linearGradient%3e%3c/defs%3e%3c/svg%3e\")";
856 } elseif ( 'curves' === $background ) { // https://www.svgbackgrounds.com/
857 $background_image = "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg %3E%3Cpath fill='%23e0e0e0' d='M486 705.8c-109.3-21.8-223.4-32.2-335.3-19.4C99.5 692.1 49 703 0 719.8V800h843.8c-115.9-33.2-230.8-68.1-347.6-92.2C492.8 707.1 489.4 706.5 486 705.8z'/%3E%3Cpath fill='%23e2e2e2' d='M1600 0H0v719.8c49-16.8 99.5-27.8 150.7-33.5c111.9-12.7 226-2.4 335.3 19.4c3.4 0.7 6.8 1.4 10.2 2c116.8 24 231.7 59 347.6 92.2H1600V0z'/%3E%3Cpath fill='%23e5e5e5' d='M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5c196.2 52.5 388.7 133.5 593.5 176.6c174.2 36.6 349.5 29.2 518.6-10.2V0H0v574.9c52.3-17.6 106.5-27.7 161.1-30.9C268.4 537.4 375.7 554.2 478.4 581z'/%3E%3Cpath fill='%23e7e7e7' d='M0 0v429.4c55.6-18.4 113.5-27.3 171.4-27.7c102.8-0.8 203.2 22.7 299.3 54.5c3 1 5.9 2 8.9 3c183.6 62 365.7 146.1 562.4 192.1c186.7 43.7 376.3 34.4 557.9-12.6V0H0z'/%3E%3Cpath fill='%23EAEAEA' d='M181.8 259.4c98.2 6 191.9 35.2 281.3 72.1c2.8 1.1 5.5 2.3 8.3 3.4c171 71.6 342.7 158.5 531.3 207.7c198.8 51.8 403.4 40.8 597.3-14.8V0H0v283.2C59 263.6 120.6 255.7 181.8 259.4z'/%3E%3Cpath fill='%23ededed' d='M1600 0H0v136.3c62.3-20.9 127.7-27.5 192.2-19.2c93.6 12.1 180.5 47.7 263.3 89.6c2.6 1.3 5.1 2.6 7.7 3.9c158.4 81.1 319.7 170.9 500.3 223.2c210.5 61 430.8 49 636.6-16.6V0z'/%3E%3Cpath fill='%23f0f0f0' d='M454.9 86.3C600.7 177 751.6 269.3 924.1 325c208.6 67.4 431.3 60.8 637.9-5.3c12.8-4.1 25.4-8.4 38.1-12.9V0H288.1c56 21.3 108.7 50.6 159.7 82C450.2 83.4 452.5 84.9 454.9 86.3z'/%3E%3Cpath fill='%23f2f2f2' d='M1600 0H498c118.1 85.8 243.5 164.5 386.8 216.2c191.8 69.2 400 74.7 595 21.1c40.8-11.2 81.1-25.2 120.3-41.7V0z'/%3E%3Cpath fill='%23f5f5f5' d='M1397.5 154.8c47.2-10.6 93.6-25.3 138.6-43.8c21.7-8.9 43-18.8 63.9-29.5V0H643.4c62.9 41.7 129.7 78.2 202.1 107.4C1020.4 178.1 1214.2 196.1 1397.5 154.8z'/%3E%3Cpath fill='%23F8F8F8' d='M1315.3 72.4c75.3-12.6 148.9-37.1 216.8-72.4h-723C966.8 71 1144.7 101 1315.3 72.4z'/%3E%3C/g%3E%3C/svg%3E\")";
858 } else {}
859
860 ?>
861 <html>
862 <head>
863 <title>Under maintenance</title>
864 <link rel="stylesheet" id="asenha-maintenance" href="<?php echo ASENHA_URL . 'assets/css/maintenance.css' ?>" media="all">
865 <meta name="viewport" content="width=device-width">
866 <style>
867 body {
868 background-image: <?php echo $background_image; ?>;
869 }
870 </style>
871 </head>
872 <body>
873 <div class="page-wrapper">
874 <div class="message-box">
875 <h1><?php echo $heading; ?></h1>
876 <div class="description"><?php echo $description; ?></div>
877 </div>
878 </div>
879 </body>
880 </html>
881 <?php
882 exit();
883
884 }
885 }
886
887 }
888
889 /**
890 * Show Password Protection admin bar status icon
891 *
892 * @since 4.1.0
893 */
894 public function show_maintenance_mode_admin_bar_icon() {
895 add_action( 'wp_before_admin_bar_render', [ $this, 'add_maintenance_mode_admin_bar_item' ] );
896 add_action( 'admin_head', [ $this, 'add_maintenance_mode_admin_bar_item_styles' ] );
897 add_action( 'wp_head', [ $this, 'add_maintenance_mode_admin_bar_item_styles' ] );
898 }
899
900 /**
901 * Add WP Admin Bar item
902 *
903 * @since 4.1.0
904 */
905 public function add_maintenance_mode_admin_bar_item() {
906 global $wp_admin_bar;
907
908 if ( is_user_logged_in() ) {
909 if ( current_user_can( 'manage_options' ) ) {
910 $wp_admin_bar->add_menu( array(
911 'id' => 'maintenance_mode',
912 'title' => '',
913 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ),
914 'meta' => array(
915 'title' => 'Maintenance mode is currently enabled for this site.',
916 ),
917 ) );
918 }
919 }
920
921 }
922
923 /**
924 * Add icon and CSS for admin bar item
925 *
926 * @since 4.1.0
927 */
928 public function add_maintenance_mode_admin_bar_item_styles() {
929
930 if ( is_user_logged_in() ) {
931 if ( current_user_can( 'manage_options' ) ) {
932
933 ?>
934 <style>
935 #wp-admin-bar-maintenance_mode {
936 background-color: #ff800c !important;
937 transition: .25s;
938 }
939 #wp-admin-bar-maintenance_mode > .ab-item {
940 color: #fff !important;
941 }
942 #wp-admin-bar-maintenance_mode > .ab-item:before {
943 content: "\f308";
944 top: 2px;
945 color: #fff !important;
946 margin-right: 0px;
947 }
948 #wp-admin-bar-maintenance_mode:hover > .ab-item {
949 background-color: #e5730a !important;
950 color: #fff;
951 }
952 </style>
953 <?php
954
955 }
956 }
957
958 }
959
960 }