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

Maintenance mode #81

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions template/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=''
Expand Down
4 changes: 4 additions & 0 deletions template/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
@@ -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.
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
@@ -0,0 +1,24 @@
<?php
/**
* The maintenance page.
*
* @package studiometa/create-wordpress-project
* @since 1.0.0
*/

?>

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<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="<?php get_stylesheet_uri(); ?>/wp-content/maintenance.css">
</head>

<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<h1><?php esc_html_e( 'Site under maintenance' ); ?></h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* 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
* @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();