Skip to content

Commit

Permalink
Refactor status code handling: move all header related logic inside a…
Browse files Browse the repository at this point in the history
… send_headers callback. Add ability to override the error code via a vip_maintenance_mode_origin_status_code filter
  • Loading branch information
rinatkhaziev committed Jul 30, 2024
1 parent 6bcff2d commit 9011813
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
36 changes: 22 additions & 14 deletions maintenance-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,13 @@ function current_user_can_bypass_vip_maintenance_mode() {
return current_user_can( $required_capability );
}

/**
* Redirects visitors and users without edit_posts capability to the maintenance page
*
* Uses the plugin template when there's no template called `template-maintenance-mode.php` in the theme root folder.
*
* @since 0.1.1
*/
function vip_maintenance_mode_template_redirect() {

add_action( 'send_headers', 'vip_maintenance_mode_send_headers', PHP_INT_MAX );

function vip_maintenance_mode_send_headers() {
if ( current_user_can_bypass_vip_maintenance_mode() ) {
return;
}

/**
* Filters whether to respond with a 503 status code.
*
Expand All @@ -74,11 +69,11 @@ function vip_maintenance_mode_template_redirect() {
*
* @param bool $bool Whether to respond with a 503 status code. Default true.
*/
$respond_503 = apply_filters( 'vip_maintenance_mode_respond_503', true );

if ( true === $respond_503 ) {
status_header( 503 );
$respond_with_status_code = apply_filters( 'vip_maintenance_mode_respond_503', true );
$respond_origin_status_code = apply_filters( 'vip_maintenance_mode_origin_status_code', 503 );

if ( true === $respond_with_status_code ) {
status_header( $respond_origin_status_code );
/**
* Filters the Retry-After value used to indicate how long the service is expected to be unavailable.
*
Expand All @@ -96,7 +91,20 @@ function vip_maintenance_mode_template_redirect() {
}

header( 'X-Maintenance-Mode-WP: true' );

}

/**
* Redirects visitors and users without edit_posts capability to the maintenance page
*
* Uses the plugin template when there's no template called `template-maintenance-mode.php` in the theme root folder.
*
* @since 0.1.1
*/
function vip_maintenance_mode_template_redirect() {
if ( current_user_can_bypass_vip_maintenance_mode() ) {
return;
}

if ( locate_template( 'template-maintenance-mode.php' ) ) {
get_template_part( 'template-maintenance-mode' );
} else {
Expand Down
1 change: 1 addition & 0 deletions vipgo-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ function wpcom_vip_maintenance_mode_do_not_respond_503_for_services( $should_set
}

add_filter( 'vip_maintenance_mode_respond_503', 'wpcom_vip_maintenance_mode_do_not_respond_503_for_services', 30 );
add_filter( 'vip_maintenance_mode_origin_status_code', fn() => 418 );

0 comments on commit 9011813

Please sign in to comment.