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