PluginProbe ʕ •ᴥ•ʔ
Admin and Site Enhancements (ASE) / 4.9.2
Admin and Site Enhancements (ASE) v4.9.2
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
944 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 // Get which role slug is currently set to "View as"
142 $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true );
143
144 if ( empty( $viewing_admin_as ) ) {
145 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
146 }
147
148 // Get the role name, translated if available, from the role slug
149 $wp_roles = wp_roles()->roles;
150
151 foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) {
152
153 if ( $wp_role_slug == $viewing_admin_as ) {
154
155 $viewing_admin_as_role_name = $wp_role_info['name'];
156
157 }
158
159 }
160
161 if ( ! isset( $viewing_admin_as_role_name ) ) {
162
163 $viewing_admin_as_role_name = $viewing_admin_as;
164
165 }
166
167 $translated_name_for_viewing_admin_as = ucfirst( $viewing_admin_as_role_name );
168
169 // Add parent menu basd on the role being set to "View as"
170
171 if ( 'administrator' == $viewing_admin_as ) {
172
173 // Add parent menu
174 $wp_admin_bar->add_menu( array(
175 'id' => 'asenha-view-admin-as-role',
176 'parent' => 'top-secondary',
177 'title' => 'View as <span style="font-size:0.8125em;">&#9660;</span>',
178 'href' => '#',
179 'meta' => array(
180 'title' => 'View admin pages and the site (logged-in) as one of the following user roles.'
181 ),
182 ) );
183
184 } else {
185
186 // Add parent menu
187 $wp_admin_bar->add_menu( array(
188 'id' => 'asenha-view-admin-as-role',
189 'parent' => 'top-secondary',
190 'title' => 'Viewing as ' . $translated_name_for_viewing_admin_as . ' <span style="font-size:0.8125em;">&#9660;</span>',
191 'href' => '#',
192 ) );
193
194 }
195
196 // Get available role(s) to switch to
197 $roles_to_switch_to = $this->get_roles_to_switch_to();
198
199 // Add role(s) to switch to as sub-menu
200
201 if ( 'administrator' == $viewing_admin_as ) {
202
203 // Add submenu for each role other than Administrator
204
205 $i = 1;
206
207 foreach ( $roles_to_switch_to as $role_slug => $data ) {
208
209 $wp_admin_bar->add_menu( array(
210 'id' => 'role' . $i . '_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager
211 'parent' => 'asenha-view-admin-as-role',
212 'title' => $data['role_name'], // role name, e.g. Editor, Shop Manager
213 'href' => $data['nonce_url'], // nonce URL for each role
214 ) );
215
216 $i++;
217
218 }
219
220 } else {
221
222 // Add submenu to switch back to Administrator role
223
224 foreach ( $roles_to_switch_to as $role_slug => $data ) {
225
226 $wp_admin_bar->add_menu( array(
227 'id' => 'role_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager
228 'parent' => 'asenha-view-admin-as-role',
229 'title' => 'Switch back to ' . $data['role_name'], // role name, e.g. Editor, Shop Manager
230 'href' => $data['nonce_url'], // nonce URL for each role
231
232 ) );
233
234 }
235
236 }
237
238 }
239
240 /**
241 * Get roles availble to switch to
242 *
243 * @since 1.8.0
244 */
245 private function get_roles_to_switch_to() {
246
247 $current_user = wp_get_current_user();
248 $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s)
249
250 // Get full list of roles defined in WordPress
251 $wp_roles = wp_roles()->roles;
252
253 $roles_to_switch_to = array();
254
255 // Get which role slug is currently active for viewing
256 $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true );
257
258 if ( 'administrator' == $viewing_admin_as ) {
259
260 // Exclude 'Administrator' from the "View as" menu
261
262 foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) {
263
264 if ( ! in_array( $wp_role_slug,$current_user_role_slugs ) ) {
265
266 $roles_to_switch_to[$wp_role_slug] = array(
267 'role_name' => $wp_role_info['name'], // role name, e.g. Editor, Shop Manager
268 'nonce_url' => wp_nonce_url(
269 add_query_arg( array(
270 'action' => 'switch_role_to',
271 'role' => $wp_role_slug,
272 ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action
273 'asenha_view_admin_as_' . $wp_role_slug, // the nonce $action name
274 'nonce', // the nonce url parameter name
275 ), // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df
276 );
277
278 }
279
280 }
281
282 } else {
283
284 // Only show switch back to Administrator in the "View as" menu
285
286 $roles_to_switch_to['administrator'] = array(
287 'role_name' => 'Administrator', // role name, e.g. Editor, Shop Manager
288 'nonce_url' => wp_nonce_url(
289 add_query_arg( array(
290 'action' => 'switch_back_to_administrator',
291 'role' => 'administrator',
292 ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action
293 'asenha_view_admin_as_administrator', // the nonce $action name
294 'nonce', // the nonce url parameter name
295 ), // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df
296 );
297 }
298
299 return $roles_to_switch_to; // array of $role_slug => $nonce_url
300
301 }
302
303 /**
304 * Switch user role to view admin and site
305 *
306 * @since 1.8.0
307 */
308 public function role_switcher_to_view_admin_as() {
309
310 $current_user = wp_get_current_user();
311 $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s)
312
313 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['role'] ) && isset( $_REQUEST['nonce'] ) ) {
314
315 $action = sanitize_text_field( $_REQUEST['action'] );
316 $new_role = sanitize_text_field( $_REQUEST['role'] );
317 $nonce = sanitize_text_field( $_REQUEST['nonce'] );
318
319 if ( 'switch_role_to' === $action ) {
320
321 // Check nonce validity and role existence
322
323 $wp_roles = array_keys( wp_roles()->roles ); // indexed array of all WP roles
324
325 if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_' . $new_role ) || ! in_array( $new_role, $wp_roles ) ) {
326 return; // cancel role switching
327 }
328
329 // Get original roles (before role switching) of the current user
330 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
331
332 // Store original user role(s) before switching it to another role
333
334 if ( empty( $original_role_slugs ) ) {
335
336 update_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', $current_user_role_slugs );
337
338 }
339
340 // Remove all current roles from current user.
341 foreach ( $current_user_role_slugs as $current_user_role_slug ) {
342
343 $current_user->remove_role( $current_user_role_slug );
344
345 }
346
347 // Add new role to current user
348 $current_user->add_role( $new_role );
349
350 // Mark that the user has switched to a non-administrator role
351 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', $new_role );
352
353 // Redirect to admin dashboard
354 wp_safe_redirect( get_admin_url() );
355 exit;
356
357 }
358
359 if ( 'switch_back_to_administrator' === $action ) {
360
361 // Check nonce validity
362
363 if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_administrator' ) || ( $new_role != 'administrator' ) ) {
364 return; // cancel role switching
365 }
366
367 // Remove all current roles from current user.
368 foreach ( $current_user_role_slugs as $current_role_slug ) {
369
370 $current_user->remove_role( $current_role_slug );
371
372 }
373
374 // Get original roles (before role switching) of the current user
375 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
376
377 // Add the original roles to the current user
378 foreach ( $original_role_slugs as $original_role_slug ) {
379
380 $current_user->add_role( $original_role_slug );
381
382 }
383
384 // Mark that the user has switched back to an administrator role
385 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
386
387 }
388
389 } elseif ( isset( $_REQUEST['reset-view-as'] ) ) {
390
391 $reset_view_as = sanitize_text_field( $_REQUEST['reset-view-as'] );
392
393 if ( $reset_view_as == 'yes' ) {
394
395 // Remove all current roles from current user.
396 foreach ( $current_user_role_slugs as $current_role_slug ) {
397
398 $current_user->remove_role( $current_role_slug );
399
400 }
401
402 // Get original roles (before role switching) of the current user
403 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
404
405 // Add the original roles to the current user
406 foreach ( $original_role_slugs as $original_role_slug ) {
407
408 $current_user->add_role( $original_role_slug );
409
410 }
411
412 // Mark that the user has switched back to an administrator role
413 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
414
415 // Redirect to admin dashboard
416 wp_safe_redirect( get_admin_url() );
417 exit;
418
419 }
420
421 }
422
423 }
424
425 /**
426 * Show custom error page on switch failure, which causes inability to view admin dashboard/pages
427 *
428 * @since 1.8.0
429 */
430 public function custom_error_page_on_switch_failure( $callback ) {
431
432 ?>
433 <!DOCTYPE html>
434 <html>
435 <head>
436 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8; ?>" />
437 <meta name="viewport" content="width=device-width">
438 <title>WordPress Error</title>
439 <style type="text/css">
440 html {
441 background: #f1f1f1;
442 }
443 body {
444 background: #fff;
445 border: 1px solid #ccd0d4;
446 color: #444;
447 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
448 margin: 2em auto;
449 padding: 1em 2em;
450 max-width: 700px;
451 -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
452 box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
453 }
454 h1 {
455 border-bottom: 1px solid #dadada;
456 clear: both;
457 color: #666;
458 font-size: 24px;
459 margin: 30px 0 0 0;
460 padding: 0;
461 padding-bottom: 7px;
462 }
463 #error-page {
464 margin-top: 50px;
465 }
466 #error-page p,
467 #error-page .wp-die-message {
468 font-size: 14px;
469 line-height: 1.5;
470 margin: 20px 0;
471 }
472 #error-page .wp-die-message:last-of-type {
473 display: none;
474 }
475 #error-page code {
476 font-family: Consolas, Monaco, monospace;
477 }
478 a {
479 color: #0073aa;
480 }
481 a:hover,
482 a:active {
483 color: #006799;
484 }
485 a:focus {
486 color: #124964;
487 -webkit-box-shadow:
488 0 0 0 1px #5b9dd9,
489 0 0 2px 1px rgba(30, 140, 190, 0.8);
490 box-shadow:
491 0 0 0 1px #5b9dd9,
492 0 0 2px 1px rgba(30, 140, 190, 0.8);
493 outline: none;
494 }
495 </style>
496 </head>
497 <body id="error-page">
498 <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>
499 </body>
500 </html>
501 <?php
502
503 }
504
505 /**
506 * Show Password Protection admin bar status icon
507 *
508 * @since 4.1.0
509 */
510 public function show_password_protection_admin_bar_icon() {
511 add_action( 'wp_before_admin_bar_render', [ $this, 'add_password_protection_admin_bar_item' ] );
512 add_action( 'admin_head', [ $this, 'add_password_protection_admin_bar_item_styles' ] );
513 add_action( 'wp_head', [ $this, 'add_password_protection_admin_bar_item_styles' ] );
514 }
515
516 /**
517 * Add WP Admin Bar item
518 *
519 * @since 4.1.0
520 */
521 public function add_password_protection_admin_bar_item() {
522 global $wp_admin_bar;
523
524 if ( is_user_logged_in() ) {
525 if ( current_user_can( 'manage_options' ) ) {
526 $wp_admin_bar->add_menu( array(
527 'id' => 'password_protection',
528 'title' => '',
529 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ),
530 'meta' => array(
531 'title' => 'Password protection is currently enabled for this site.',
532 ),
533 ) );
534 }
535 }
536
537 }
538
539 /**
540 * Add icon and CSS for admin bar item
541 *
542 * @since 4.1.0
543 */
544 public function add_password_protection_admin_bar_item_styles() {
545
546 if ( is_user_logged_in() ) {
547 if ( current_user_can( 'manage_options' ) ) {
548
549 ?>
550 <style>
551 #wp-admin-bar-password_protection {
552 background-color: #c32121 !important;
553 transition: .25s;
554 }
555 #wp-admin-bar-password_protection > .ab-item {
556 color: #fff !important;
557 }
558 #wp-admin-bar-password_protection > .ab-item:before {
559 content: "\f160";
560 top: 2px;
561 color: #fff !important;
562 margin-right: 0px;
563 }
564 #wp-admin-bar-password_protection:hover > .ab-item {
565 background-color: #af1d1d !important;
566 color: #fff;
567 }
568 </style>
569 <?php
570
571 }
572 }
573
574 }
575
576 /**
577 * Disable page caching
578 *
579 * @since 4.1.0
580 */
581 public function maybe_disable_page_caching() {
582
583 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
584 define( 'DONOTCACHEPAGE', true );
585 }
586
587 }
588
589 /**
590 * Maybe show login form
591 *
592 * @since 4.1.0
593 */
594 public function maybe_show_login_form() {
595
596 // When user is logged-in as in an administrator
597 if ( is_user_logged_in() ) {
598 if ( current_user_can( 'manage_options' ) ) {
599 return; // Do not load login form or perform redirection to the login form
600 }
601 }
602
603 // When site visitor has entered correct password
604 $auth_cookie = $_COOKIE['asenha_password_protection'];
605
606 if ( 'authenticated' == $auth_cookie ) {
607 return; // Do not load login form or perform redirection to the login form
608 }
609
610 if ( isset( $_REQUEST['protected-page'] ) && 'view' == $_REQUEST['protected-page'] ) {// Show login form
611
612 $password_protected_login_page_template = ASENHA_PATH . 'includes/password-protected-login.php';
613
614 load_template( $password_protected_login_page_template );
615 exit();
616
617 } else { // Redirect to login form
618
619 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . sanitize_text_field( $_SERVER['HTTP_HOST'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] );
620
621 $args = array(
622 'protected-page' => 'view',
623 'source' => urlencode( $current_url ),
624 );
625
626 $pwd_protect_login_url = add_query_arg( $args, home_url() );
627
628 nocache_headers();
629 wp_safe_redirect( $pwd_protect_login_url );
630 exit();
631
632 }
633
634 }
635
636 /**
637 * Maybe process login to access protected page content
638 *
639 * @since 4.1.0
640 */
641 public function maybe_process_login() {
642
643 global $password_protected_errors;
644 $password_protected_errors = new WP_Error();
645
646 if ( isset( $_REQUEST['protected_page_pwd'] ) ) {
647
648 $password_input = $_REQUEST['protected_page_pwd'];
649
650 $options = get_option( ASENHA_SLUG_U, array() );
651 $stored_password = $options['password_protection_password'];
652
653 if ( ! empty( $password_input ) ) {
654
655 if ( $password_input == base64_decode( $stored_password ) ) { // Password is correct
656
657 // Set auth cookie
658 // $expiration = time() + DAY_IN_SECONDS; // in 24 hours
659 $expiration = 0; // by the end of browsing session
660 setcookie( 'asenha_password_protection', 'authenticated', $expiration, COOKIEPATH, COOKIE_DOMAIN, true, true );
661
662 // Redirect
663 $redirect_to_url = isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : '';
664 wp_safe_redirect( $redirect_to_url );
665 exit();
666
667 } else { // Password is incorrect
668
669 // Add error message
670 $password_protected_errors->add( 'incorrect_password', 'Incorrect password.' );
671
672 }
673
674 } else { // Password input is empty
675
676 // Add error message
677 $password_protected_errors->add( 'empty_password', 'Password can not be empty.' );
678
679 }
680
681 }
682 }
683
684 /**
685 * Add custom login error messages
686 *
687 * @since 4.1.0
688 */
689 public function add_login_error_messages() {
690
691 global $password_protected_errors;
692
693 if ( $password_protected_errors->get_error_code() ) {
694
695 $messages = '';
696 $errors = '';
697
698 // Extract the error message
699
700 foreach( $password_protected_errors->get_error_codes() as $code ) {
701
702 $severity = $password_protected_errors->get_error_data( $code );
703
704 foreach( $password_protected_errors->get_error_messages( $code ) as $error ) {
705 if ( 'message' == $severity ) {
706 $messages .= $error . '<br />';
707 } else {
708 $errors .= $error . '<br />';
709 }
710 }
711
712 }
713
714 // Output the error message
715
716 if ( ! empty( $messages ) ) {
717 echo '<p class="message">' . $messages . '</p>';
718 }
719
720 if ( ! empty( $errors ) ) {
721 echo '<div id="login_error">' . $errors . '</div>';
722 }
723
724 }
725
726 }
727
728 /**
729 * Redirect 404 to homepage
730 *
731 * @since 1.7.0
732 */
733 public function redirect_404_to_homepage() {
734
735 if ( ! is_404() || is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) {
736
737 return;
738
739 } else {
740
741 // wp_safe_redirect( home_url(), 301 );
742
743 header( 'HTTP/1.1 301 Moved Permanently');
744 header( 'Location: ' . home_url() );
745 exit();
746
747 }
748
749 }
750
751 /**
752 * Send emails using external SMTP service
753 *
754 * @since 4.6.0
755 */
756 public function deliver_email_via_smtp( $phpmailer ) {
757
758 $options = get_option( ASENHA_SLUG_U, array() );
759 $smtp_host = $options['smtp_host'];
760 $smtp_port = $options['smtp_port'];
761 $smtp_security = $options['smtp_security'];
762 $smtp_username = $options['smtp_username'];
763 $smtp_password = base64_decode( $options['smtp_password'] );
764 $smtp_default_from_name = $options['smtp_default_from_name'];
765 $smtp_default_from_email = $options['smtp_default_from_email'];
766 $smtp_debug = $options['smtp_debug'];
767
768 // Do nothing if host or password is empty
769 if ( empty( $smtp_host ) || empty( $smtp_password ) ) {
770 return;
771 }
772
773 // Send using SMTP
774 $phpmailer->isSMTP(); // phpcs:ignore
775
776 // Enanble SMTP authentication
777 $phpmailer->SMTPAuth = true; // phpcs:ignore
778
779 // Set some other defaults
780 // $phpmailer->CharSet = 'utf-8'; // phpcs:ignore
781 $phpmailer->XMailer = 'Admin and Site Enhancements v' . ASENHA_VERSION . ' - a WordPress plugin'; // phpcs:ignore
782
783 $phpmailer->Host = $smtp_host; // phpcs:ignore
784 $phpmailer->Port = $smtp_port; // phpcs:ignore
785 $phpmailer->SMTPSecure = $smtp_security; // phpcs:ignore
786 $phpmailer->Username = $smtp_username; // phpcs:ignore
787 $phpmailer->Password = $smtp_password; // phpcs:ignore
788
789 // 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.
790
791 $from_name = $phpmailer->FromName;
792
793 if ( ( 'WordPress' === $from_name ) && ! empty( $smtp_default_from_name ) ) {
794 $phpmailer->FromName = $smtp_default_from_name;
795 }
796
797 $from_email_beginning = substr( $phpmailer->From, 0, 0 ); // Get the first 9 characters of the current FROM email
798
799 if ( ( 'wordpress' === $from_email_beginning ) && ! empty( $smtp_default_from_email ) ) {
800 $phpmailer->From = $smtp_default_from_email;
801 }
802
803 // If debug mode is enabled, send debug info (SMTP::DEBUG_CONNECTION) to WordPress debug.log file set in wp-config.php
804 // Reference: https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging
805
806 if ( $smtp_debug ) {
807 $phpmailer->SMTPDebug = 3; //phpcs:ignore
808 $phpmailer->Debugoutput = 'error_log'; //phpcs:ignore
809 }
810
811 }
812
813 /**
814 * Redirect for when maintenance mode is enabled
815 *
816 * @since 4.7.0
817 */
818 public function maintenance_mode_redirect() {
819
820 $current_url = sanitize_text_field( $_SERVER['REQUEST_URI'] );
821 $current_url_parts = explode( '/', $current_url );
822
823 // Bypass wp-admin pages and logged-in administrator on the frontend
824 if ( ! in_array( 'wp-admin', $current_url_parts ) || ( false !== strpos( 'wp-login.php', $current_url ) ) ) {
825 if ( ! current_user_can( 'manage_options' ) ) {
826
827 header( 'HTTP/1.1 503 Service Unavailable', true, 503 );
828 header( 'Status: 503 Service Unavailable' );
829 header( 'Retry-After: 3600' ); // Tell search engine bots to return after 3600 seconds, i.e. 1 hour
830
831 $options = get_option( ASENHA_SLUG_U, array() );
832 $heading = $options['maintenance_page_heading'];
833 $description = $options['maintenance_page_description'];
834 $background = $options['maintenance_page_background'];
835
836 if ( 'lines' === $background ) { // https://bgjar.com/curve-line
837 $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\")";
838 } elseif ( 'stripes' === $background ) { // https://bgjar.com/shiny-overlay
839 $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\")";
840 } elseif ( 'curves' === $background ) { // https://www.svgbackgrounds.com/
841 $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\")";
842 } else {}
843
844 ?>
845 <html>
846 <head>
847 <title>Under maintenance</title>
848 <link rel="stylesheet" id="asenha-maintenance" href="<?php echo ASENHA_URL . 'assets/css/maintenance.css' ?>" media="all">
849 <meta name="viewport" content="width=device-width">
850 <style>
851 body {
852 background-image: <?php echo $background_image; ?>;
853 }
854 </style>
855 </head>
856 <body>
857 <div class="page-wrapper">
858 <div class="message-box">
859 <h1><?php echo $heading; ?></h1>
860 <div class="description"><?php echo $description; ?></div>
861 </div>
862 </div>
863 </body>
864 </html>
865 <?php
866 exit();
867
868 }
869 }
870
871 }
872
873 /**
874 * Show Password Protection admin bar status icon
875 *
876 * @since 4.1.0
877 */
878 public function show_maintenance_mode_admin_bar_icon() {
879 add_action( 'wp_before_admin_bar_render', [ $this, 'add_maintenance_mode_admin_bar_item' ] );
880 add_action( 'admin_head', [ $this, 'add_maintenance_mode_admin_bar_item_styles' ] );
881 add_action( 'wp_head', [ $this, 'add_maintenance_mode_admin_bar_item_styles' ] );
882 }
883
884 /**
885 * Add WP Admin Bar item
886 *
887 * @since 4.1.0
888 */
889 public function add_maintenance_mode_admin_bar_item() {
890 global $wp_admin_bar;
891
892 if ( is_user_logged_in() ) {
893 if ( current_user_can( 'manage_options' ) ) {
894 $wp_admin_bar->add_menu( array(
895 'id' => 'maintenance_mode',
896 'title' => '',
897 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ),
898 'meta' => array(
899 'title' => 'Maintenance mode is currently enabled for this site.',
900 ),
901 ) );
902 }
903 }
904
905 }
906
907 /**
908 * Add icon and CSS for admin bar item
909 *
910 * @since 4.1.0
911 */
912 public function add_maintenance_mode_admin_bar_item_styles() {
913
914 if ( is_user_logged_in() ) {
915 if ( current_user_can( 'manage_options' ) ) {
916
917 ?>
918 <style>
919 #wp-admin-bar-maintenance_mode {
920 background-color: #ff800c !important;
921 transition: .25s;
922 }
923 #wp-admin-bar-maintenance_mode > .ab-item {
924 color: #fff !important;
925 }
926 #wp-admin-bar-maintenance_mode > .ab-item:before {
927 content: "\f308";
928 top: 2px;
929 color: #fff !important;
930 margin-right: 0px;
931 }
932 #wp-admin-bar-maintenance_mode:hover > .ab-item {
933 background-color: #e5730a !important;
934 color: #fff;
935 }
936 </style>
937 <?php
938
939 }
940 }
941
942 }
943
944 }