From 7c7d3656d0cbf6c30a438520fde5fea96561dcc1 Mon Sep 17 00:00:00 2001 From: Marius Cristea Date: Thu, 2 Jun 2022 13:35:39 +0100 Subject: [PATCH 01/11] 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 02/11] 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 03/11] 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; From 8fe2f30d9671f21e0598f57f51dbae89d208bdea Mon Sep 17 00:00:00 2001 From: selul Date: Sat, 4 Nov 2023 14:56:40 +0200 Subject: [PATCH 04/11] Adds warning when non numeric value is detected. --- features/maintenance-mode.feature | 16 ++++++++++++++++ src/MaintenanceModeCommand.php | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index 60181cd..d422d61 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -66,3 +66,19 @@ Feature: Manage maintenance mode of WordPress install. When I run `wp eval "file_put_contents('.maintenance', 'get_contents( $maintenance_file ); $matches = []; - if ( preg_match( '/upgrading = (\d+);/i', $contents, $matches ) ) { + if ( preg_match( '/upgrading\s*=\s*(\d+)\s*;/i', $contents, $matches ) ) { $upgrading = (int) $matches[1]; + } else { + WP_CLI::warning( 'Unable to read the maintenance file timestamp, non-numeric value detected.' ); } if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) { From 7ef179fe947423440d6a5b190093ff8df7a1f189 Mon Sep 17 00:00:00 2001 From: Marius Cristea Date: Sat, 4 Nov 2023 15:27:52 +0200 Subject: [PATCH 05/11] [skip ci] Update features/maintenance-mode.feature Co-authored-by: Daniel Bachhuber --- features/maintenance-mode.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index d422d61..1a21432 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -72,7 +72,7 @@ Feature: Manage maintenance mode of WordPress install. """ When I run `wp eval "file_put_contents('.maintenance', ' Date: Sat, 4 Nov 2023 15:28:03 +0200 Subject: [PATCH 06/11] [skip ci] Update features/maintenance-mode.feature Co-authored-by: Daniel Bachhuber --- features/maintenance-mode.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index 1a21432..cac5a4c 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -76,7 +76,7 @@ Feature: Manage maintenance mode of WordPress install. Then the return code should be 0 When I run `wp eval "file_put_contents('.maintenance', ' Date: Sat, 4 Nov 2023 15:28:17 +0200 Subject: [PATCH 07/11] [skip ci] Update features/maintenance-mode.feature Co-authored-by: Daniel Bachhuber --- features/maintenance-mode.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index cac5a4c..24f3c33 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -80,5 +80,5 @@ Feature: Manage maintenance mode of WordPress install. Then the return code should be 0 When I run `wp eval "file_put_contents('.maintenance', ' Date: Sat, 4 Nov 2023 15:52:29 +0200 Subject: [PATCH 08/11] Restructure status check commands in two scenarios. --- features/maintenance-mode.feature | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index 24f3c33..592b340 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -63,6 +63,8 @@ Feature: Manage maintenance mode of WordPress install. Success: Activated Maintenance mode. """ + Scenario: Check maintenance mode status when expression is used. + When I run `wp eval "file_put_contents('.maintenance', ' Date: Mon, 6 Nov 2023 09:40:03 +0200 Subject: [PATCH 09/11] Update features/maintenance-mode.feature Co-authored-by: Daniel Bachhuber --- features/maintenance-mode.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index 592b340..a27ce55 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -70,7 +70,7 @@ Feature: Manage maintenance mode of WordPress install. Then the return code should be 1 Then STDERR should contain: """ - Unable to read the maintenance file timestamp, non-numeric value detected. + Warning: Unable to read the maintenance file timestamp, non-numeric value detected. """ Scenario: Check maintenance mode status when numeric timestamp is used. From c767cb5bd829861da1f6ada20abb07a0ab590ca1 Mon Sep 17 00:00:00 2001 From: selul Date: Mon, 6 Nov 2023 09:42:53 +0200 Subject: [PATCH 10/11] Adds code comment reference to wp_is_maintenance_mode --- src/MaintenanceModeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MaintenanceModeCommand.php b/src/MaintenanceModeCommand.php index 33fab70..cc66d8f 100644 --- a/src/MaintenanceModeCommand.php +++ b/src/MaintenanceModeCommand.php @@ -147,7 +147,7 @@ private function get_maintenance_mode_status() { } else { WP_CLI::warning( 'Unable to read the maintenance file timestamp, non-numeric value detected.' ); } - + // The logic here is based on the core WordPress `wp_is_maintenance_mode()` function. if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) { return false; } From a407fb0999e8823f8a0bb9c9ee6210a828b08115 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 6 Nov 2023 04:26:22 -0800 Subject: [PATCH 11/11] Fix Behat statement --- features/maintenance-mode.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/maintenance-mode.feature b/features/maintenance-mode.feature index a27ce55..eab4568 100644 --- a/features/maintenance-mode.feature +++ b/features/maintenance-mode.feature @@ -68,7 +68,7 @@ Feature: Manage maintenance mode of WordPress install. When I run `wp eval "file_put_contents('.maintenance', '