diff --git a/template/.env.example b/template/.env.example index 2f8a563..fbd97d0 100644 --- a/template/.env.example +++ b/template/.env.example @@ -26,6 +26,9 @@ DISABLE_PLUGINS_LOCAL=test/test.php DISABLE_PLUGINS_PREPROD=test/test.php DISABLE_PLUGINS_PRODUCTION=test/test.php +MAINTENANCE_ENABLED=true|false +MAINTENANCE_IPS=127.0.0.1,127.0.0.2 + AUTH_KEY='' SECURE_AUTH_KEY='' LOGGED_IN_KEY='' diff --git a/template/readme.md b/template/readme.md index 86384a4..f5fb044 100644 --- a/template/readme.md +++ b/template/readme.md @@ -87,6 +87,10 @@ Par défaut, tout ce qui se trouve dans les sous-dossiers de `web/wp-content` es ## Fonctionnalités additionnelles +### Activation/Désactivation du mode maintenance via variables d'environnement serveur + +Le MU-plugin [Studiometa maintenance mode](./web/wp-content/mu-plugins/studiometa-maintenance-mode/README.md) permet d'activer le mode maintenance du site. [Voir le readme](./web/wp-content/mu-plugins/studiometa-maintenance-mode/README.md) pour plus d'informations. + ### Désactivation de plugins par environnement Le MU-plugin [Studiometa plugin disabler](./web/wp-content/mu-plugins/studiometa-plugin-disabler/README.md) permet de forcer la désactivation des plugins en fonction de l'environnement. [Voir le readme](./web/wp-content/mu-plugins/studiometa-plugin-disabler/README.md) pour plus d'informations. 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 new file mode 100644 index 0000000..340fc67 --- /dev/null +++ b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/README.md @@ -0,0 +1,14 @@ +# Studio Meta maintenance mode + +Put your site under maintenance depends on server environment variables. + +## Usage + +1. The following server environment variables must be set in order to the maintenance be active: +```bash +MAINTENANCE_ENABLED=true|false # Enable/Disable maitenance mode. +MAINTENANCE_IPS=42.42.42.42.42 # Exclude a list of IPs from maitenance. +``` +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/example/maintenance.php b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.php new file mode 100644 index 0000000..848458e --- /dev/null +++ b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/example/maintenance.php @@ -0,0 +1,24 @@ + + + +> + + + + <?php bloginfo( 'name' ) . ( ! empty( get_bloginfo( 'description' ) ) ? ' – ' . bloginfo( 'description' ) : '' ); ?> + + + + > + +

+ + diff --git a/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/studiometa-maintenance-mode.php b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/studiometa-maintenance-mode.php new file mode 100644 index 0000000..061016a --- /dev/null +++ b/template/web/wp-content/mu-plugins/studiometa-maintenance-mode/studiometa-maintenance-mode.php @@ -0,0 +1,104 @@ + + * @copyright 2021 Studio Meta + * @license https://opensource.org/licenses/MIT + * @version 1.0.0 + */ + +/** + * StudiometaMaintenanceMode class. + */ +class StudiometaMaintenanceMode { + public function __construct() { + 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 + ); + } + + /** + * Get the server variable REMOTE_ADDR, or the first ip of HTTP_X_FORWARDED_FOR (when using proxy). + * + * @return string $remote_addr ip of client + */ + private static function get_remote_addr() + { + if (function_exists('apache_request_headers')) { + $headers = apache_request_headers(); + } else { + $headers = $_SERVER; + } + + if (array_key_exists('X-Forwarded-For', $headers)) { + $_SERVER['HTTP_X_FORWARDED_FOR'] = $headers['X-Forwarded-For']; + } + + if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && (!isset($_SERVER['REMOTE_ADDR']) + || preg_match('/^127\..*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^172\.16.*/i', trim($_SERVER['REMOTE_ADDR'])) + || preg_match('/^192\.168\.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^10\..*/i', trim($_SERVER['REMOTE_ADDR'])))) { + if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')) { + $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); + + return $ips[0]; + } else { + return $_SERVER['HTTP_X_FORWARDED_FOR']; + } + } else { + return $_SERVER['REMOTE_ADDR']; + } + } + + /** + * Is maintenance? + * + * @return boolean Is maintenance? + */ + protected function is_maintenance_mode() { + if ( + 'true' !== getenv( 'MAINTENANCE_ENABLED' ) + || is_admin() + || current_user_can( 'manage_options' ) + || 'wp-login.php' === $GLOBALS['pagenow'] + ) { + return false; + } + + $allowed_ips = explode( ',', getenv( 'MAINTENANCE_IPS' ) ); + + if ( $allowed_ips && in_array( $this->get_remote_addr(), $allowed_ips, true ) ) { + return false; + } + + return true; + } +} + +new StudiometaMaintenanceMode();