Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Use Wordpress maintenance mode instead of custom one
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Simeon committed Aug 10, 2022
1 parent 315a238 commit 969611f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Studio Meta maintenance mode

Put your site under maintenance with a 503 HTTP status and a dedicated page.
Put your site under maintenance depends on server environment variables.

## Usage

Expand All @@ -9,6 +9,6 @@ Put your site under maintenance with a 503 HTTP status and a dedicated page.
MAINTENANCE_ENABLED=true|false # Enable/Disable maitenance mode.
MAINTENANCE_IPS=42.42.42.42.42 # Exclude a list of IPs from maitenance.
```
2. Customize the maintenance page thanks to `maintenance.php` and `maintenance.css` files.
2. You can customize the maintenance page by adding a `maintenance.php` file in `WP_CONTENT_DIR` (see https://developer.wordpress.org/reference/functions/wp_maintenance/). You can find a maintenance page example in [this folder](`./example/maintenance.php`)

> If you use a cache plugin, don't forget to clean caches.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 { color: red; }
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title><?php bloginfo( 'name' ) . ( ! empty( get_bloginfo( 'description' ) ) ? '' . bloginfo( 'description' ) : '' ); ?></title>
<link rel="stylesheet" href="/wp-content/mu-plugins/studiometa-maintenance-mode/maintenance.css">
<link rel="stylesheet" href="<?php get_stylesheet_uri(); ?>/wp-content/maintenance.css">
</head>

<body <?php body_class(); ?>>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
<?php
/**
* Display maintenance page when maintenace is enabled.
* Plugin Name: Studio Meta - Maintenance mode
* Description: Display maintenance page when maintenace is enabled.
* Version: 1.0.0
* Author: Studio Meta
* Author URI: https://www.studiometa.fr/
*
* @package studiometa/create-wordpress-project
* @author Studio Meta <[email protected]>
* @copyright 2021 Studio Meta
* @license https://opensource.org/licenses/MIT
* @since 1.0.0
* @version 1.0.0
*/

/**
* StudiometaMaintenanceMode class.
*/
class StudiometaMaintenanceMode {
/**
* Constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'redirect_to_maintenance' ) );
add_action( 'wp_loaded', array( $this, 'toggle_maintenance' ) );
}

public function toggle_maintenance() {
if ( ! $this->is_maintenance_mode() ) {
return;
}

if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
$protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) && 'HTTP/1.1' === $_SERVER['SERVER_PROTOCOL'] ? 'HTTP/1.1' : 'HTTP/1.0';

header( $protocol . ' 503 Service Unavailable', true, 503 );
require_once WP_CONTENT_DIR . '/maintenance.php';
die();
}

require_once ABSPATH . WPINC . '/functions.php';
wp_load_translations_early();

wp_die(
__( 'Briefly unavailable for scheduled maintenance. Check back in a minute.' ),
__( 'Maintenance' ),
503
);
}

/**
Expand All @@ -30,8 +53,7 @@ protected function is_maintenance_mode() {
if (
'true' !== getenv( 'MAINTENANCE_ENABLED' )
|| is_admin()
|| current_user_can( 'administrator' )
|| ! file_exists( __DIR__ . '/maintenance.php' )
|| current_user_can( 'manage_options' )
|| 'wp-login.php' === $GLOBALS['pagenow']
) {
return false;
Expand All @@ -46,24 +68,6 @@ protected function is_maintenance_mode() {

return true;
}

/**
* Redirect to maintenance mode.
*
* @return void
*/
public function redirect_to_maintenance() {
if ( ! $this->is_maintenance_mode() ) {
return;
}

$protocol = isset( $_SERVER['SERVER_PROTOCOL'] ) && 'HTTP/1.1' === $_SERVER['SERVER_PROTOCOL'] ? 'HTTP/1.1' : 'HTTP/1.0';

header( $protocol . ' 503 Service Unavailable', true, 503 );
header( 'Retry-After: 3600' );
include_once __DIR__ . '/maintenance.php';
die();
}
}

new StudiometaMaintenanceMode();

0 comments on commit 969611f

Please sign in to comment.