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