diff --git a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/README.md b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/README.md index b393a5a..340fc67 100644 --- a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/README.md +++ b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/README.md @@ -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 @@ -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. diff --git a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.css b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.css new file mode 100644 index 0000000..5ce768c --- /dev/null +++ b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.css @@ -0,0 +1 @@ +h1 { color: red; } diff --git a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/maintenance.php b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.php similarity index 85% rename from template/web/wp-content/mu-plugins/studiometa-maintenance-mode/maintenance.php rename to template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.php index 0b0f273..848458e 100644 --- a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/maintenance.php +++ b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.php @@ -14,7 +14,7 @@ <?php bloginfo( 'name' ) . ( ! empty( get_bloginfo( 'description' ) ) ? ' – ' . bloginfo( 'description' ) : '' ); ?> - + > diff --git a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/maintenance.css b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/maintenance.css deleted file mode 100644 index e69de29..0000000 diff --git a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/class-studiometamaintenancemode.php b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/studiometa-maintenance-mode.php similarity index 51% rename from template/web/wp-content/mu-plugins/studiometa-maintenance-mode/class-studiometamaintenancemode.php rename to template/web/wp-content/mu-plugins/studiometa-maintenance-mode/studiometa-maintenance-mode.php index 1412f04..0ddbedd 100644 --- a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/class-studiometamaintenancemode.php +++ b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/studiometa-maintenance-mode.php @@ -1,12 +1,15 @@ * @copyright 2021 Studio Meta * @license https://opensource.org/licenses/MIT - * @since 1.0.0 * @version 1.0.0 */ @@ -14,11 +17,31 @@ * 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 + ); } /** @@ -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; @@ -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();