This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Wordpress maintenance mode instead of custom one
- Loading branch information
Lucas Simeon
committed
Aug 10, 2022
1 parent
315a238
commit 969611f
Showing
5 changed files
with
34 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1 { color: red; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -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(); |