Skip to content

Commit 2e9845a

Browse files
selulswissspidydanielbachhuber
authored
Evaluate $upgrading numeric value when checking if maintenance mode is active (#22)
--------- Co-authored-by: Pascal Birchler <pascalb@google.com> Co-authored-by: Daniel Bachhuber <daniel@bachhuber.co>
1 parent 599f8f0 commit 2e9845a

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

features/maintenance-mode.feature

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,34 @@ Feature: Manage maintenance mode of WordPress install.
5555
"""
5656
Error: Maintenance mode already deactivated.
5757
"""
58+
59+
When I run `wp maintenance-mode activate`
60+
Then STDOUT should be:
61+
"""
62+
Enabling Maintenance mode...
63+
Success: Activated Maintenance mode.
64+
"""
65+
66+
Scenario: Check maintenance mode status when expression is used.
67+
68+
When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading=(time()-601);'); "`
69+
And I try `wp maintenance-mode is-active`
70+
Then the return code should be 1
71+
And STDERR should contain:
72+
"""
73+
Warning: Unable to read the maintenance file timestamp, non-numeric value detected.
74+
"""
75+
76+
Scenario: Check maintenance mode status when numeric timestamp is used.
77+
78+
When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading=' . ( time() + 100 ) . ';'); "`
79+
And I run `wp maintenance-mode is-active`
80+
Then the return code should be 0
81+
82+
When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading =' . ( time() + 100 ) . ';') ; "`
83+
And I run `wp maintenance-mode is-active`
84+
Then the return code should be 0
85+
86+
When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading= ' . ( time() + 100 ) . ';'); "`
87+
And I run `wp maintenance-mode is-active`
88+
Then the return code should be 0

src/MaintenanceModeCommand.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,26 @@ private function get_maintenance_mode_status() {
132132

133133
$maintenance_file = trailingslashit( $wp_filesystem->abspath() ) . '.maintenance';
134134

135-
return $wp_filesystem->exists( $maintenance_file );
135+
if ( ! $wp_filesystem->exists( $maintenance_file ) ) {
136+
return false;
137+
}
138+
139+
// We use the timestamp defined in the .maintenance file
140+
// to check if the maintenance is available.
141+
$upgrading = 0;
142+
143+
$contents = $wp_filesystem->get_contents( $maintenance_file );
144+
$matches = [];
145+
if ( preg_match( '/upgrading\s*=\s*(\d+)\s*;/i', $contents, $matches ) ) {
146+
$upgrading = (int) $matches[1];
147+
} else {
148+
WP_CLI::warning( 'Unable to read the maintenance file timestamp, non-numeric value detected.' );
149+
}
150+
// The logic here is based on the core WordPress `wp_is_maintenance_mode()` function.
151+
if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) {
152+
return false;
153+
}
154+
return true;
136155
}
137156

138157
/**

0 commit comments

Comments
 (0)