Skip to content
Merged
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
31 changes: 31 additions & 0 deletions features/maintenance-mode.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,34 @@ 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.
"""

Scenario: Check maintenance mode status when expression is used.

When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading=(time()-601);'); "`
And I try `wp maintenance-mode is-active`
Then the return code should be 1
And STDERR should contain:
"""
Warning: Unable to read the maintenance file timestamp, non-numeric value detected.
"""

Scenario: Check maintenance mode status when numeric timestamp is used.

When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading=' . ( time() + 100 ) . ';'); "`
And I run `wp maintenance-mode is-active`
Then the return code should be 0

When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading =' . ( time() + 100 ) . ';') ; "`
And I run `wp maintenance-mode is-active`
Then the return code should be 0

When I run `wp eval "file_put_contents('.maintenance', '<?php \$upgrading= ' . ( time() + 100 ) . ';'); "`
And I run `wp maintenance-mode is-active`
Then the return code should be 0
21 changes: 20 additions & 1 deletion src/MaintenanceModeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,26 @@ private function get_maintenance_mode_status() {

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

return $wp_filesystem->exists( $maintenance_file );
if ( ! $wp_filesystem->exists( $maintenance_file ) ) {
return false;
}

// 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\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.' );
}
// The logic here is based on the core WordPress `wp_is_maintenance_mode()` function.
if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) {
return false;
}
return true;
}

/**
Expand Down