PluginProbe ʕ •ᴥ•ʔ
Admin and Site Enhancements (ASE) / 6.2.4
Admin and Site Enhancements (ASE) v6.2.4
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 2 years ago class-admin-interface.php 2 years ago class-common-methods.php 2 years ago class-content-management.php 2 years ago class-custom-code.php 2 years ago class-deactivation.php 2 years ago class-disable-components.php 2 years ago class-login-logout.php 2 years ago class-optimizations.php 2 years ago class-security.php 2 years ago class-settings-fields-render.php 2 years ago class-settings-sanitization.php 2 years ago class-settings-sections-fields.php 2 years ago class-utilities.php 2 years ago
class-utilities.php
1107 lines
1 <?php
2
3 namespace ASENHA\Classes;
4
5 use WP_Error ;
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 // Get the roles of the user being shown / edited / created
23
24 if ( !empty($user->roles) ) {
25 $user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) );
26 // indexed array of role slugs
27 } else {
28 $user_roles = array();
29 }
30
31 // Only show roles checkboxes for users that can assign roles to other users
32
33 if ( current_user_can( 'promote_users', get_current_user_id() ) ) {
34 ?>
35 <div class="asenha-roles-temporary-container">
36 <table class="form-table">
37 <tr>
38 <th>
39 <label>Roles</label>
40 </th>
41 <td>
42 <?php
43 foreach ( $roles as $role_slug => $role_info ) {
44 $checkbox_id = $role_slug . '_role';
45 $role_name = translate_user_role( $role_info['name'] );
46
47 if ( !empty($user_roles) && in_array( $role_slug, $user_roles ) ) {
48 $checked = 'checked="checked"';
49 } else {
50 $checked = '';
51 }
52
53 // Output roles checkboxes
54 ?>
55 <label for="<?php
56 esc_attr_e( $checkbox_id );
57 ?>"><input type="checkbox" id="<?php
58 esc_attr_e( $checkbox_id );
59 ?>" value="<?php
60 esc_attr_e( $role_slug );
61 ?>" name="asenha_assigned_roles[]" <?php
62 esc_attr_e( $checked );
63 ?> /> <?php
64 esc_html_e( $role_name );
65 ?></label><br />
66 <?php
67 }
68 wp_nonce_field( 'asenha_set_multiple_roles', 'asenha_multiple_roles_nonce' );
69 ?>
70 </td>
71 </tr>
72 </table>
73 </div>
74 <?php
75 }
76
77 }
78
79 /**
80 * Save changes in roles assignment
81 *
82 * @since 4.8.0
83 */
84 public function save_roles_assignment( $user_id )
85 {
86 if ( !current_user_can( 'promote_users', get_current_user_id() ) || !wp_verify_nonce( $_POST['asenha_multiple_roles_nonce'], 'asenha_set_multiple_roles' ) ) {
87 return;
88 }
89 // Get user roles that the current user is allowed to edit
90 $roles = get_editable_roles();
91 // Get the roles of the user being shown / edited / created
92 $user = get_user_by( 'id', (int) $user_id );
93 // WP_User object
94 $user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) );
95 // Current/existing roles
96
97 if ( !empty($_POST['asenha_assigned_roles']) ) {
98 $assigned_roles = array_map( 'sanitize_text_field', $_POST['asenha_assigned_roles'] );
99 // Make sure only valid roles are processed
100 $assigned_roles = array_intersect( $assigned_roles, array_keys( $roles ) );
101 $roles_to_remove = array();
102 $roles_to_add = array();
103
104 if ( empty($assigned_roles) ) {
105 // Remove all current/existing roles
106 $roles_to_remove = $user_roles;
107 } else {
108 // Identify and remove roles not present in the newly assigned roles
109 $roles_to_remove = array_diff( $user_roles, $assigned_roles );
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 // Identify and add roles not present in the existing roles
116 $roles_to_add = array_diff( $assigned_roles, $user_roles );
117 if ( !empty($roles_to_add) ) {
118 foreach ( $roles_to_add as $role_to_add ) {
119 $user->add_role( $role_to_add );
120 }
121 }
122 }
123
124 }
125
126 }
127
128 /**
129 * Add menu bar item to view admin as one of the user roles
130 *
131 * @param $wp_admin_bar The WP_Admin_Bar instance
132 * @link https://developer.wordpress.org/reference/hooks/admin_bar_menu/
133 * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/
134 * @since 1.8.0
135 */
136 public function view_admin_as_admin_bar_menu( $wp_admin_bar )
137 {
138 $options = get_option( ASENHA_SLUG_U, array() );
139 $usernames = ( isset( $options['viewing_admin_as_role_are'] ) ? $options['viewing_admin_as_role_are'] : array() );
140 $current_user = wp_get_current_user();
141 $current_user_roles = array_values( $current_user->roles );
142 // indexed array
143 $current_user_username = $current_user->user_login;
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 if ( empty($viewing_admin_as) ) {
147 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
148 }
149 // Get the role name, translated if available, from the role slug
150 $wp_roles = wp_roles()->roles;
151 foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) {
152 if ( $wp_role_slug == $viewing_admin_as ) {
153 $viewing_admin_as_role_name = $wp_role_info['name'];
154 }
155 }
156 if ( !isset( $viewing_admin_as_role_name ) ) {
157 $viewing_admin_as_role_name = $viewing_admin_as;
158 }
159 $translated_name_for_viewing_admin_as = ucfirst( $viewing_admin_as_role_name );
160 // Add parent menu based on the role being set to "View as"
161
162 if ( 'administrator' == $viewing_admin_as ) {
163 if ( in_array( 'administrator', $current_user_roles ) ) {
164 // Add parent menu for administrators
165 $wp_admin_bar->add_menu( array(
166 'id' => 'asenha-view-admin-as-role',
167 'parent' => 'top-secondary',
168 'title' => 'View as <span style="font-size:0.8125em;">&#9660;</span>',
169 'href' => '#',
170 'meta' => array(
171 'title' => 'View admin pages and the site (logged-in) as one of the following user roles.',
172 ),
173 ) );
174 }
175 } else {
176 // Limit to users performing role switching only. i.e. Don't show role switcher to regularly logging in users.
177 if ( in_array( $current_user_username, $usernames ) ) {
178 // Add parent menu
179 $wp_admin_bar->add_menu( array(
180 'id' => 'asenha-view-admin-as-role',
181 'parent' => 'top-secondary',
182 'title' => 'Viewing as ' . $translated_name_for_viewing_admin_as . ' <span style="font-size:0.8125em;">&#9660;</span>',
183 'href' => '#',
184 ) );
185 }
186 }
187
188 // Get available role(s) to switch to
189 $roles_to_switch_to = $this->get_roles_to_switch_to();
190 // Add role(s) to switch to as sub-menu
191
192 if ( 'administrator' == $viewing_admin_as ) {
193
194 if ( in_array( 'administrator', $current_user_roles ) ) {
195 // Add submenu for each role other than Administrator
196 $i = 1;
197 foreach ( $roles_to_switch_to as $role_slug => $data ) {
198 $wp_admin_bar->add_menu( array(
199 'id' => 'role' . $i . '_' . $role_slug,
200 'parent' => 'asenha-view-admin-as-role',
201 'title' => $data['role_name'],
202 'href' => $data['nonce_url'],
203 ) );
204 $i++;
205 }
206 }
207
208 } else {
209 // Add submenu to switch back to Administrator role
210 // Limit to users performing role switching only. i.e. Don't show role switcher to regularly logging in users.
211 if ( in_array( $current_user_username, $usernames ) ) {
212 foreach ( $roles_to_switch_to as $role_slug => $data ) {
213 $wp_admin_bar->add_menu( array(
214 'id' => 'role_' . $role_slug,
215 'parent' => 'asenha-view-admin-as-role',
216 'title' => 'Switch back to ' . $data['role_name'],
217 'href' => $data['nonce_url'],
218 ) );
219 }
220 }
221 }
222
223 }
224
225 /**
226 * Get roles availble to switch to
227 *
228 * @since 1.8.0
229 */
230 private function get_roles_to_switch_to()
231 {
232 $current_user = wp_get_current_user();
233 $current_user_role_slugs = $current_user->roles;
234 // indexed array of current user role slug(s)
235 // Get full list of roles defined in WordPress
236 $wp_roles = wp_roles()->roles;
237 $roles_to_switch_to = array();
238 // Get which role slug is currently active for viewing
239 $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true );
240
241 if ( 'administrator' == $viewing_admin_as ) {
242 // Exclude 'Administrator' from the "View as" menu
243 foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) {
244 if ( !in_array( $wp_role_slug, $current_user_role_slugs ) ) {
245 $roles_to_switch_to[$wp_role_slug] = array(
246 'role_name' => $wp_role_info['name'],
247 'nonce_url' => wp_nonce_url(
248 add_query_arg( array(
249 'action' => 'switch_role_to',
250 'role' => $wp_role_slug,
251 ) ),
252 // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action
253 'asenha_view_admin_as_' . $wp_role_slug,
254 // the nonce $action name
255 'nonce'
256 ),
257 );
258 }
259 }
260 } else {
261 // Only show switch back to Administrator in the "View as" menu
262 $roles_to_switch_to['administrator'] = array(
263 'role_name' => 'Administrator',
264 'nonce_url' => wp_nonce_url(
265 add_query_arg( array(
266 'action' => 'switch_back_to_administrator',
267 'role' => 'administrator',
268 ) ),
269 // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action
270 'asenha_view_admin_as_administrator',
271 // the nonce $action name
272 'nonce'
273 ),
274 );
275 }
276
277 return $roles_to_switch_to;
278 // array of $role_slug => $nonce_url
279 }
280
281 /**
282 * Switch user role to view admin and site
283 *
284 * @since 1.8.0
285 */
286 public function role_switcher_to_view_admin_as()
287 {
288 $current_user = wp_get_current_user();
289 $current_user_role_slugs = $current_user->roles;
290 // indexed array of current user role slug(s)
291 $current_user_username = $current_user->user_login;
292 $options = get_option( ASENHA_SLUG_U, array() );
293 $options['viewing_admin_as_role_are'] = array();
294
295 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['role'] ) && isset( $_REQUEST['nonce'] ) ) {
296 $action = sanitize_text_field( $_REQUEST['action'] );
297 $new_role = sanitize_text_field( $_REQUEST['role'] );
298 $nonce = sanitize_text_field( $_REQUEST['nonce'] );
299
300 if ( 'switch_role_to' === $action ) {
301 // Check nonce validity and role existence
302 $wp_roles = array_keys( wp_roles()->roles );
303 // indexed array of all WP roles
304
305 if ( !wp_verify_nonce( $nonce, 'asenha_view_admin_as_' . $new_role ) || !in_array( $new_role, $wp_roles ) ) {
306 return;
307 // cancel role switching
308 }
309
310 // Get original roles (before role switching) of the current user
311 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
312 // Store original user role(s) before switching it to another role
313 if ( empty($original_role_slugs) ) {
314 update_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', $current_user_role_slugs );
315 }
316 // Store current user's username in options
317 $options['viewing_admin_as_role_are'][] = $current_user_username;
318 update_option( ASENHA_SLUG_U, $options );
319 // Remove all current roles from current user.
320 foreach ( $current_user_role_slugs as $current_user_role_slug ) {
321 $current_user->remove_role( $current_user_role_slug );
322 }
323 // Add new role to current user
324 $current_user->add_role( $new_role );
325 // Mark that the user has switched to a non-administrator role
326 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', $new_role );
327 // if ( ! in_array( $new_role, array( 'administrator', 'editor', 'author', 'contributor' ) ) ) {
328 // Redirect to profile edit page
329 // wp_safe_redirect( get_edit_profile_url() );
330 // } else {
331 // Redirect to admin dashboard
332 wp_safe_redirect( get_admin_url() );
333 // }
334 exit;
335 }
336
337
338 if ( 'switch_back_to_administrator' === $action ) {
339 // Check nonce validity
340
341 if ( !wp_verify_nonce( $nonce, 'asenha_view_admin_as_administrator' ) || $new_role != 'administrator' ) {
342 return;
343 // cancel role switching
344 }
345
346 // Remove all current roles from current user.
347 foreach ( $current_user_role_slugs as $current_role_slug ) {
348 $current_user->remove_role( $current_role_slug );
349 }
350 // Get original roles (before role switching) of the current user
351 $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true );
352 // Add the original roles to the current user
353 foreach ( $original_role_slugs as $original_role_slug ) {
354 $current_user->add_role( $original_role_slug );
355 }
356 // Remove current user's username from stored usernames.
357 $usernames = $options['viewing_admin_as_role_are'];
358 foreach ( $usernames as $key => $username ) {
359 if ( $current_user_username == $username ) {
360 unset( $usernames[$key] );
361 }
362 }
363 $options['viewing_admin_as_role_are'] = $usernames;
364 update_option( ASENHA_SLUG_U, $options );
365 // Mark that the user has switched back to an administrator role
366 update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' );
367 }
368
369 } elseif ( isset( $_REQUEST['reset-for'] ) ) {
370 $reset_for_username = sanitize_text_field( $_REQUEST['reset-for'] );
371 $options = get_option( ASENHA_SLUG_U, array() );
372 $usernames = $options['viewing_admin_as_role_are'];
373 if ( !empty($reset_for_username) ) {
374
375 if ( in_array( $reset_for_username, $usernames ) ) {
376 $current_user = get_user_by( 'login', $reset_for_username );
377 $current_user_role_slugs = $current_user->roles;
378 // indexed array of current user role slug(s)
379 // Remove all current roles from current user.
380 foreach ( $current_user_role_slugs as $current_role_slug ) {
381 $current_user->remove_role( $current_role_slug );
382 }
383 // Get original roles (before role switching) of the current user
384 $original_role_slugs = get_user_meta( $current_user->ID, '_asenha_view_admin_as_original_roles', true );
385 // Add the original roles to the current user
386 foreach ( $original_role_slugs as $original_role_slug ) {
387 $current_user->add_role( $original_role_slug );
388 }
389 // Mark that the user has switched back to an administrator role
390 update_user_meta( $current_user->ID, '_asenha_viewing_admin_as', 'administrator' );
391 // Remove current user's username from stored usernames.
392 foreach ( $usernames as $key => $username ) {
393 if ( $reset_for_username == $username ) {
394 unset( $usernames[$key] );
395 }
396 }
397 $options['viewing_admin_as_role_are'] = $usernames;
398 update_option( ASENHA_SLUG_U, $options );
399 // Redirect to login URL, including when custom login slug is set and active
400
401 if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) {
402 if ( array_key_exists( 'custom_login_slug', $options ) && !empty($options['custom_login_slug']) ) {
403 $login_url = get_site_url( null, $options['custom_login_slug'] );
404 }
405 } else {
406 $login_url = wp_login_url();
407 }
408
409 // Redirect to admin dashboard
410 // wp_safe_redirect( $login_url );
411 // exit;
412 // Use JS redirect, which works more reliably on the frontend
413 ?>
414 <script>
415 window.location.href='<?php
416 echo $login_url ;
417 ?>';
418 </script>
419 <?php
420 }
421
422 }
423 }
424
425 }
426
427 /**
428 * Add floating button to reset the view/account back to the administrator
429 *
430 * @since 6.1.3
431 */
432 public function add_floating_reset_button()
433 {
434 $options = get_option( ASENHA_SLUG_U, array() );
435 $admin_usernames_viewing_as_role = ( isset( $options['viewing_admin_as_role_are'] ) ? $options['viewing_admin_as_role_are'] : array() );
436 $current_user = wp_get_current_user();
437 $username = $current_user->user_login;
438 // Show for non-admins
439
440 if ( !current_user_can( 'manage_options' ) && in_array( $username, $admin_usernames_viewing_as_role ) ) {
441 ?>
442 <div id="role-view-reset">
443 <a href="<?php
444 echo get_site_url() ;
445 ?>/?reset-for=<?php
446 echo $username ;
447 ?>" class="button button-primary">Switch back to Administrator</a>
448 </div>
449 <?php
450 }
451
452 }
453
454 /**
455 * Show custom error page on switch failure, which causes inability to view admin dashboard/pages
456 *
457 * @since 1.8.0
458 */
459 public function custom_error_page_on_switch_failure( $callback )
460 {
461 ?>
462 <!DOCTYPE html>
463 <html>
464 <head>
465 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8; ?>" />
466 <meta name="viewport" content="width=device-width">
467 <title>WordPress Error</title>
468 <style type="text/css">
469 html {
470 background: #f1f1f1;
471 }
472 body {
473 background: #fff;
474 border: 1px solid #ccd0d4;
475 color: #444;
476 font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
477 margin: 2em auto;
478 padding: 1em 2em;
479 max-width: 700px;
480 -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
481 box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
482 }
483 h1 {
484 border-bottom: 1px solid #dadada;
485 clear: both;
486 color: #666;
487 font-size: 24px;
488 margin: 30px 0 0 0;
489 padding: 0;
490 padding-bottom: 7px;
491 }
492 #error-page {
493 margin-top: 50px;
494 }
495 #error-page p,
496 #error-page .wp-die-message {
497 font-size: 14px;
498 line-height: 1.5;
499 margin: 20px 0;
500 }
501 #error-page code {
502 font-family: Consolas, Monaco, monospace;
503 }
504 a {
505 color: #0073aa;
506 }
507 a:hover,
508 a:active {
509 color: #006799;
510 }
511 a:focus {
512 color: #124964;
513 -webkit-box-shadow:
514 0 0 0 1px #5b9dd9,
515 0 0 2px 1px rgba(30, 140, 190, 0.8);
516 box-shadow:
517 0 0 0 1px #5b9dd9,
518 0 0 2px 1px rgba(30, 140, 190, 0.8);
519 outline: none;
520 }
521 </style>
522 </head>
523 <body id="error-page">
524 <div class="wp-die-message">Something went wrong. Please try logging in.</div>
525 </body>
526 </html>
527 <?php
528 }
529
530 /**
531 * Show Password Protection admin bar status icon
532 *
533 * @since 4.1.0
534 */
535 public function show_password_protection_admin_bar_icon()
536 {
537 add_action( 'wp_before_admin_bar_render', [ $this, 'add_password_protection_admin_bar_item' ] );
538 add_action( 'admin_head', [ $this, 'add_password_protection_admin_bar_item_styles' ] );
539 add_action( 'wp_head', [ $this, 'add_password_protection_admin_bar_item_styles' ] );
540 }
541
542 /**
543 * Add WP Admin Bar item
544 *
545 * @since 4.1.0
546 */
547 public function add_password_protection_admin_bar_item()
548 {
549 global $wp_admin_bar ;
550 if ( is_user_logged_in() ) {
551 if ( current_user_can( 'manage_options' ) ) {
552 $wp_admin_bar->add_menu( array(
553 'id' => 'password_protection',
554 'title' => '',
555 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ),
556 'meta' => array(
557 'title' => 'Password protection is currently enabled for this site.',
558 ),
559 ) );
560 }
561 }
562 }
563
564 /**
565 * Add icon and CSS for admin bar item
566 *
567 * @since 4.1.0
568 */
569 public function add_password_protection_admin_bar_item_styles()
570 {
571 if ( is_user_logged_in() ) {
572 if ( current_user_can( 'manage_options' ) ) {
573 ?>
574 <style>
575 #wp-admin-bar-password_protection {
576 background-color: #c32121 !important;
577 transition: .25s;
578 }
579 #wp-admin-bar-password_protection > .ab-item {
580 color: #fff !important;
581 }
582 #wp-admin-bar-password_protection > .ab-item:before {
583 content: "\f160";
584 top: 2px;
585 color: #fff !important;
586 margin-right: 0px;
587 }
588 #wp-admin-bar-password_protection:hover > .ab-item {
589 background-color: #af1d1d !important;
590 color: #fff;
591 }
592 </style>
593 <?php
594 }
595 }
596 }
597
598 /**
599 * Disable page caching
600 *
601 * @since 4.1.0
602 */
603 public function maybe_disable_page_caching()
604 {
605 if ( !defined( 'DONOTCACHEPAGE' ) ) {
606 define( 'DONOTCACHEPAGE', true );
607 }
608 }
609
610 /**
611 * Maybe show login form
612 *
613 * @since 4.1.0
614 */
615 public function maybe_show_login_form()
616 {
617 // When user is logged-in as in an administrator
618 if ( is_user_logged_in() ) {
619
620 if ( current_user_can( 'manage_options' ) ) {
621 return;
622 // Do not load login form or perform redirection to the login form
623 }
624
625 }
626 // When site visitor has entered correct password, get the auth cookie
627 $auth_cookie = ( isset( $_COOKIE['asenha_password_protection'] ) ? $_COOKIE['asenha_password_protection'] : '' );
628 // Compared against random string set in maybe_process_login()
629
630 if ( wp_check_password( 'MOeldTVhGnL18VfbDtXM7znSYXIUQn3z', $auth_cookie ) ) {
631 return;
632 // Do not load login form or perform redirection to the login form
633 }
634
635
636 if ( isset( $_REQUEST['protected-page'] ) && 'view' == $_REQUEST['protected-page'] ) {
637 // Show login form
638 $password_protected_login_page_template = ASENHA_PATH . 'includes/password-protected-login.php';
639 load_template( $password_protected_login_page_template );
640 exit;
641 } else {
642 // Redirect to login form
643 $current_url = (( is_ssl() ? 'https://' : 'http://' )) . sanitize_text_field( $_SERVER['HTTP_HOST'] ) . sanitize_text_field( $_SERVER['REQUEST_URI'] );
644 $args = array(
645 'protected-page' => 'view',
646 'source' => urlencode( $current_url ),
647 );
648 $pwd_protect_login_url = add_query_arg( $args, home_url() );
649 nocache_headers();
650 wp_safe_redirect( $pwd_protect_login_url );
651 exit;
652 }
653
654 }
655
656 /**
657 * Maybe process login to access protected page content
658 *
659 * @since 4.1.0
660 */
661 public function maybe_process_login()
662 {
663 global $password_protected_errors ;
664 $password_protected_errors = new WP_Error();
665
666 if ( isset( $_REQUEST['protected_page_pwd'] ) ) {
667 $password_input = $_REQUEST['protected_page_pwd'];
668 $options = get_option( ASENHA_SLUG_U, array() );
669 $stored_password = $options['password_protection_password'];
670
671 if ( !empty($password_input) ) {
672
673 if ( $password_input == $stored_password ) {
674 // Password is correct
675 // Set auth cookie
676 // $expiration = time() + DAY_IN_SECONDS; // in 24 hours
677 $expiration = 0;
678 // by the end of browsing session
679 $hashed_cookie_value = wp_hash_password( 'MOeldTVhGnL18VfbDtXM7znSYXIUQn3z' );
680 // random string
681 setcookie(
682 'asenha_password_protection',
683 $hashed_cookie_value,
684 $expiration,
685 COOKIEPATH,
686 COOKIE_DOMAIN,
687 false,
688 true
689 );
690 // Redirect
691 $redirect_to_url = ( isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : '' );
692 wp_safe_redirect( $redirect_to_url );
693 exit;
694 } else {
695 // Password is incorrect
696 // Add error message
697 $password_protected_errors->add( 'incorrect_password', 'Incorrect password.' );
698 }
699
700 } else {
701 // Password input is empty
702 // Add error message
703 $password_protected_errors->add( 'empty_password', 'Password can not be empty.' );
704 }
705
706 }
707
708 }
709
710 /**
711 * Add custom login error messages
712 *
713 * @since 4.1.0
714 */
715 public function add_login_error_messages()
716 {
717 global $password_protected_errors ;
718
719 if ( $password_protected_errors->get_error_code() ) {
720 $messages = '';
721 $errors = '';
722 // Extract the error message
723 foreach ( $password_protected_errors->get_error_codes() as $code ) {
724 $severity = $password_protected_errors->get_error_data( $code );
725 foreach ( $password_protected_errors->get_error_messages( $code ) as $error ) {
726
727 if ( 'message' == $severity ) {
728 $messages .= $error . '<br />';
729 } else {
730 $errors .= $error . '<br />';
731 }
732
733 }
734 }
735 // Output the error message
736 if ( !empty($messages) ) {
737 echo '<p class="message">' . $messages . '</p>' ;
738 }
739 if ( !empty($errors) ) {
740 echo '<div id="login_error">' . $errors . '</div>' ;
741 }
742 }
743
744 }
745
746 /**
747 * Redirect 404 to homepage
748 *
749 * @since 1.7.0
750 */
751 public function redirect_404_to_homepage()
752 {
753
754 if ( !is_404() || is_admin() || defined( 'DOING_CRON' ) && DOING_CRON || defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
755 return;
756 } else {
757 // wp_safe_redirect( home_url(), 301 );
758 header( 'HTTP/1.1 301 Moved Permanently' );
759 header( 'Location: ' . home_url() );
760 exit;
761 }
762
763 }
764
765 /**
766 * Send emails using external SMTP service
767 *
768 * @since 4.6.0
769 */
770 public function deliver_email_via_smtp( $phpmailer )
771 {
772 $options = get_option( ASENHA_SLUG_U, array() );
773 $smtp_host = $options['smtp_host'];
774 $smtp_port = $options['smtp_port'];
775 $smtp_security = $options['smtp_security'];
776 $smtp_username = $options['smtp_username'];
777 $smtp_password = $options['smtp_password'];
778 $smtp_default_from_name = $options['smtp_default_from_name'];
779 $smtp_default_from_email = $options['smtp_default_from_email'];
780 $smtp_force_from = $options['smtp_force_from'];
781 $smtp_debug = $options['smtp_debug'];
782 // Do nothing if host or password is empty
783 // if ( empty( $smtp_host ) || empty( $smtp_password ) ) {
784 // return;
785 // }
786 // 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.
787 $from_name = $phpmailer->FromName;
788 $from_email_beginning = substr( $phpmailer->From, 0, 9 );
789 // Get the first 9 characters of the current FROM email
790
791 if ( $smtp_force_from ) {
792 $phpmailer->FromName = $smtp_default_from_name;
793 $phpmailer->From = $smtp_default_from_email;
794 } else {
795 if ( 'WordPress' === $from_name && !empty($smtp_default_from_name) ) {
796 $phpmailer->FromName = $smtp_default_from_name;
797 }
798 if ( 'wordpress' === $from_email_beginning && !empty($smtp_default_from_email) ) {
799 $phpmailer->From = $smtp_default_from_email;
800 }
801 }
802
803 // Only attempt to send via SMTP if all the required info is present. Otherwise, use default PHP Mailer settings as set by wp_mail()
804
805 if ( !empty($smtp_host) && !empty($smtp_port) && !empty($smtp_security) && !empty($smtp_username) && !empty($smtp_password) ) {
806 // Send using SMTP
807 $phpmailer->isSMTP();
808 // phpcs:ignore
809 // Enanble SMTP authentication
810 $phpmailer->SMTPAuth = true;
811 // phpcs:ignore
812 // Set some other defaults
813 // $phpmailer->CharSet = 'utf-8'; // phpcs:ignore
814 $phpmailer->XMailer = 'Admin and Site Enhancements v' . ASENHA_VERSION . ' - a WordPress plugin';
815 // phpcs:ignore
816 $phpmailer->Host = $smtp_host;
817 // phpcs:ignore
818 $phpmailer->Port = $smtp_port;
819 // phpcs:ignore
820 $phpmailer->SMTPSecure = $smtp_security;
821 // phpcs:ignore
822 $phpmailer->Username = $smtp_username;
823 // phpcs:ignore
824 $phpmailer->Password = $smtp_password;
825 // phpcs:ignore
826 }
827
828 // If debug mode is enabled, send debug info (SMTP::DEBUG_CONNECTION) to WordPress debug.log file set in wp-config.php
829 // Reference: https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging
830
831 if ( $smtp_debug ) {
832 $phpmailer->SMTPDebug = 3;
833 //phpcs:ignore
834 $phpmailer->Debugoutput = 'error_log';
835 //phpcs:ignore
836 }
837
838 }
839
840 /**
841 * Send a test email and use SMTP host if defined in settings
842 *
843 * @since 5.3.0
844 */
845 public function send_test_email()
846 {
847
848 if ( isset( $_REQUEST ) ) {
849 $content = array(
850 array(
851 'title' => 'Hey... are you getting this?',
852 'body' => '<p><strong>Looks like you did!</strong></p>',
853 ),
854 array(
855 'title' => 'There\'s a message for you...',
856 'body' => '<p><strong>Here it is:</strong></p>',
857 ),
858 array(
859 'title' => 'Is it working?',
860 'body' => '<p><strong>Yes, it\'s working!</strong></p>',
861 ),
862 array(
863 'title' => 'Hope you\'re getting this...',
864 'body' => '<p><strong>Looks like this was sent out just fine and you got it.</strong></p>',
865 ),
866 array(
867 'title' => 'Testing delivery configuration...',
868 'body' => '<p><strong>Everything looks good!</strong></p>',
869 ),
870 array(
871 'title' => 'Testing email delivery',
872 'body' => '<p><strong>Looks good!</strong></p>',
873 ),
874 array(
875 'title' => 'Config is looking good',
876 'body' => '<p><strong>Seems like everything has been set up properly!</strong></p>',
877 ),
878 array(
879 'title' => 'All set up',
880 'body' => '<p><strong>Your configuration is working properly.</strong></p>',
881 ),
882 array(
883 'title' => 'Good to go',
884 'body' => '<p><strong>Config is working great.</strong></p>',
885 ),
886 array(
887 'title' => 'Good job',
888 'body' => '<p><strong>Everything is set.</strong></p>',
889 )
890 );
891 $random_number = rand( 0, count( $content ) - 1 );
892 $to = $_REQUEST['email_to'];
893 $title = $content[$random_number]['title'];
894 $body = $content[$random_number]['body'] . '<p>This message was sent from <a href="' . get_bloginfo( 'url' ) . '">' . get_bloginfo( 'url' ) . '</a> on ' . wp_date( 'F j, Y' ) . ' at ' . wp_date( 'H:i:s' ) . ' via ASE.</p>';
895 $headers = array( 'Content-Type: text/html; charset=UTF-8' );
896 $success = wp_mail(
897 $to,
898 $title,
899 $body,
900 $headers
901 );
902
903 if ( $success ) {
904 $response = array(
905 'status' => 'success',
906 );
907 } else {
908 $response = array(
909 'status' => 'failed',
910 );
911 }
912
913 echo json_encode( $response ) ;
914 }
915
916 }
917
918 /**
919 * Redirect for when maintenance mode is enabled
920 *
921 * @since 4.7.0
922 */
923 public function maintenance_mode_redirect()
924 {
925 $current_url = sanitize_text_field( $_SERVER['REQUEST_URI'] );
926 $current_url_parts = explode( '/', $current_url );
927 // Bypass wp-admin pages and logged-in administrator on the frontend
928 if ( !in_array( 'wp-admin', $current_url_parts ) || false !== strpos( 'wp-login.php', $current_url ) ) {
929
930 if ( !current_user_can( 'manage_options' ) ) {
931 header( 'HTTP/1.1 503 Service Unavailable', true, 503 );
932 header( 'Status: 503 Service Unavailable' );
933 header( 'Retry-After: 3600' );
934 // Tell search engine bots to return after 3600 seconds, i.e. 1 hour
935 $options = get_option( ASENHA_SLUG_U, array() );
936 $heading = $options['maintenance_page_heading'];
937 $description = $options['maintenance_page_description'];
938 $background = $options['maintenance_page_background'];
939
940 if ( 'lines' === $background ) {
941 // https://bgjar.com/curve-line
942 $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\")";
943 $background_style = 'background-image: ' . $background_image;
944 } elseif ( 'stripes' === $background ) {
945 // https://bgjar.com/shiny-overlay
946 $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\")";
947 $background_style = 'background-image: ' . $background_image;
948 } elseif ( 'curves' === $background ) {
949 // https://www.svgbackgrounds.com/
950 $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\")";
951 $background_style = 'background-image: ' . $background_image;
952 } elseif ( 'image' === $background ) {
953 $background_style = 'background-image: none;';
954 } elseif ( 'solid_color' === $background ) {
955 $background_style = 'background-color: #ffffff;';
956 } else {
957 }
958
959 ?>
960 <html>
961 <head>
962 <title>Under maintenance</title>
963 <link rel="stylesheet" id="asenha-maintenance" href="<?php
964 echo ASENHA_URL . 'assets/css/maintenance.css' ;
965 ?>" media="all">
966 <meta name="viewport" content="width=device-width">
967 <style>
968 body {
969 <?php
970 echo $background_style ;
971 ?>;
972 background-size: cover;
973 background-position: center center;
974 }
975 </style>
976 </head>
977 <body>
978 <div class="page-wrapper">
979 <div class="message-box">
980 <h1><?php
981 echo $heading ;
982 ?></h1>
983 <div class="description"><?php
984 echo $description ;
985 ?></div>
986 </div>
987 </div>
988 </body>
989 </html>
990 <?php
991 exit;
992 }
993
994 }
995 }
996
997 /**
998 * Show Password Protection admin bar status icon
999 *
1000 * @since 4.1.0
1001 */
1002 public function show_maintenance_mode_admin_bar_icon()
1003 {
1004 add_action( 'wp_before_admin_bar_render', [ $this, 'add_maintenance_mode_admin_bar_item' ] );
1005 add_action( 'admin_head', [ $this, 'add_maintenance_mode_admin_bar_item_styles' ] );
1006 add_action( 'wp_head', [ $this, 'add_maintenance_mode_admin_bar_item_styles' ] );
1007 }
1008
1009 /**
1010 * Add WP Admin Bar item
1011 *
1012 * @since 4.1.0
1013 */
1014 public function add_maintenance_mode_admin_bar_item()
1015 {
1016 global $wp_admin_bar ;
1017 if ( is_user_logged_in() ) {
1018 if ( current_user_can( 'manage_options' ) ) {
1019 $wp_admin_bar->add_menu( array(
1020 'id' => 'maintenance_mode',
1021 'title' => '',
1022 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ),
1023 'meta' => array(
1024 'title' => 'Maintenance mode is currently enabled for this site.',
1025 ),
1026 ) );
1027 }
1028 }
1029 }
1030
1031 /**
1032 * Add icon and CSS for admin bar item
1033 *
1034 * @since 4.1.0
1035 */
1036 public function add_maintenance_mode_admin_bar_item_styles()
1037 {
1038 if ( is_user_logged_in() ) {
1039 if ( current_user_can( 'manage_options' ) ) {
1040 ?>
1041 <style>
1042 #wp-admin-bar-maintenance_mode {
1043 background-color: #ff800c !important;
1044 transition: .25s;
1045 }
1046 #wp-admin-bar-maintenance_mode > .ab-item {
1047 color: #fff !important;
1048 }
1049 #wp-admin-bar-maintenance_mode > .ab-item:before {
1050 content: "\f308";
1051 top: 2px;
1052 color: #fff !important;
1053 margin-right: 0px;
1054 }
1055 #wp-admin-bar-maintenance_mode:hover > .ab-item {
1056 background-color: #e5730a !important;
1057 color: #fff;
1058 }
1059 </style>
1060 <?php
1061 }
1062 }
1063 }
1064
1065 /**
1066 * Display system summary in the "At a Glance" dashboard widget
1067 *
1068 * @since 5.6.0
1069 */
1070 public function display_system_summary()
1071 {
1072 // When user is logged-in as in an administrator
1073 if ( is_user_logged_in() ) {
1074
1075 if ( current_user_can( 'manage_options' ) ) {
1076 $server_software_raw = str_replace( "/", " ", $_SERVER['SERVER_SOFTWARE'] );
1077 $server_software_parts = explode( " (", $server_software_raw );
1078 $server_software = ucfirst( $server_software_parts[0] );
1079 $php_version = phpversion();
1080
1081 if ( mysqli_connect(
1082 DB_HOST,
1083 DB_USER,
1084 DB_PASSWORD,
1085 DB_NAME
1086 ) ) {
1087 $db_full_version = mysqli_get_server_info( mysqli_connect(
1088 DB_HOST,
1089 DB_USER,
1090 DB_PASSWORD,
1091 DB_NAME
1092 ) );
1093 $db_version_parts = explode( ':', $db_full_version );
1094 $db_short_version = $db_version_parts[0];
1095 $db_separator = ' | ';
1096 } else {
1097 $db_short_version = '';
1098 $db_separator = '';
1099 }
1100
1101 echo '<div class="system-summary"><a href="' . admin_url( 'site-health.php?tab=debug' ) . '">System</a>: ' . esc_html( $server_software ) . ' | PHP ' . esc_html( $php_version ) . ' (' . php_sapi_name() . ')' . esc_html( $db_separator ) . esc_html( $db_short_version ) . '</div>' ;
1102 }
1103
1104 }
1105 }
1106
1107 }