From 7c7d3656d0cbf6c30a438520fde5fea96561dcc1 Mon Sep 17 00:00:00 2001 From: Marius Cristea Date: Thu, 2 Jun 2022 13:35:39 +0100 Subject: [PATCH 1/3] fix maintenance mode inconsistency by using the $upgrading value in checking if the site is in maintenance mode or not. --- features/maintenance-mode.feature | 10 ++++++++++ src/MaintenanceModeCommand.php | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index 68da153..b08061f 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -55,3 +55,13 @@ Feature: Manage maintenance mode of WordPress install. """ Error: Maintenance mode already deactivated. """ + + When I run `wp maintenance-mode activate` + Then STDOUT should be: + """ + Enabling Maintenance mode... + Success: Activated Maintenance mode. + """ + + When I try `wp --exec="file_put_contents('.maintenance', 'abspath() ) . '.maintenance'; - return $wp_filesystem->exists( $maintenance_file ); + if ( ! $wp_filesystem->exists( $maintenance_file ) ) { + return false; + } + + require ABSPATH . '.maintenance'; + /** + * We use the timestamp defined in the .maintenance file + * to check if the maintenance is available. + * + * @var int $upgrading Upgrade time. + */ + + if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) { + return false; + } + return true; } /** From c4df9a33acfe09733e0bfa5bb7f38ba93f354c99 Mon Sep 17 00:00:00 2001 From: Marius Cristea Date: Thu, 2 Jun 2022 14:49:57 +0100 Subject: [PATCH 2/3] split test into two separate commands. --- features/maintenance-mode.feature | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index b08061f..60181cd 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -63,5 +63,6 @@ Feature: Manage maintenance mode of WordPress install. Success: Activated Maintenance mode. """ - When I try `wp --exec="file_put_contents('.maintenance', ' Date: Fri, 3 Nov 2023 14:25:34 +0100 Subject: [PATCH 3/3] Use regex --- src/MaintenanceModeCommand.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/MaintenanceModeCommand.php b/src/MaintenanceModeCommand.php index 7128c6f..cc14608 100644 --- a/src/MaintenanceModeCommand.php +++ b/src/MaintenanceModeCommand.php @@ -136,13 +136,15 @@ private function get_maintenance_mode_status() { return false; } - require ABSPATH . '.maintenance'; - /** - * We use the timestamp defined in the .maintenance file - * to check if the maintenance is available. - * - * @var int $upgrading Upgrade time. - */ + // We use the timestamp defined in the .maintenance file + // to check if the maintenance is available. + $upgrading = 0; + + $contents = $wp_filesystem->get_contents( $maintenance_file ); + $matches = []; + if ( preg_match( '/upgrading = (\d+);/i', $contents, $matches ) ) { + $upgrading = (int) $matches[1]; + } if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) { return false;