├── .actrc ├── .distignore ├── .editorconfig ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE ├── PULL_REQUEST_TEMPLATE ├── dependabot.yml └── workflows │ ├── code-quality.yml │ ├── regenerate-readme.yml │ └── testing.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── behat.yml ├── composer.json ├── features └── maintenance-mode.feature ├── maintenance-mode-command.php ├── phpcs.xml.dist ├── src └── MaintenanceModeCommand.php └── wp-cli.yml /.actrc: -------------------------------------------------------------------------------- 1 | # Configuration file for nektos/act. 2 | # See https://github.com/nektos/act#configuration 3 | -P ubuntu-latest=shivammathur/node:latest 4 | -------------------------------------------------------------------------------- /.distignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .git 3 | .gitignore 4 | .gitlab-ci.yml 5 | .editorconfig 6 | .travis.yml 7 | behat.yml 8 | circle.yml 9 | phpcs.xml.dist 10 | phpunit.xml.dist 11 | bin/ 12 | features/ 13 | utils/ 14 | *.zip 15 | *.tar.gz 16 | *.swp 17 | *.txt 18 | *.log 19 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | # WordPress Coding Standards 5 | # https://make.wordpress.org/core/handbook/coding-standards/ 6 | 7 | # From https://github.com/WordPress/wordpress-develop/blob/trunk/.editorconfig with a couple of additions. 8 | 9 | root = true 10 | 11 | [*] 12 | charset = utf-8 13 | end_of_line = lf 14 | insert_final_newline = true 15 | trim_trailing_whitespace = true 16 | indent_style = tab 17 | 18 | [{*.yml,*.feature,.jshintrc,*.json}] 19 | indent_style = space 20 | indent_size = 2 21 | 22 | [*.md] 23 | trim_trailing_whitespace = false 24 | 25 | [{*.txt,wp-config-sample.php}] 26 | end_of_line = crlf 27 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @wp-cli/committers 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE: -------------------------------------------------------------------------------- 1 | 17 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: composer 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | labels: 9 | - scope:distribution 10 | - package-ecosystem: github-actions 11 | directory: "/" 12 | schedule: 13 | interval: daily 14 | open-pull-requests-limit: 10 15 | labels: 16 | - scope:distribution 17 | 18 | -------------------------------------------------------------------------------- /.github/workflows/code-quality.yml: -------------------------------------------------------------------------------- 1 | name: Code Quality Checks 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | - master 9 | 10 | jobs: 11 | code-quality: 12 | uses: wp-cli/.github/.github/workflows/reusable-code-quality.yml@main 13 | -------------------------------------------------------------------------------- /.github/workflows/regenerate-readme.yml: -------------------------------------------------------------------------------- 1 | name: Regenerate README file 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: 7 | - main 8 | - master 9 | paths-ignore: 10 | - "features/**" 11 | - "README.md" 12 | 13 | jobs: 14 | regenerate-readme: 15 | uses: wp-cli/.github/.github/workflows/reusable-regenerate-readme.yml@main 16 | -------------------------------------------------------------------------------- /.github/workflows/testing.yml: -------------------------------------------------------------------------------- 1 | name: Testing 2 | 3 | on: 4 | workflow_dispatch: 5 | pull_request: 6 | push: 7 | branches: 8 | - main 9 | - master 10 | schedule: 11 | - cron: '17 1 * * *' # Run every day on a seemly random time. 12 | 13 | jobs: 14 | test: 15 | uses: wp-cli/.github/.github/workflows/reusable-testing.yml@main 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | wp-cli.local.yml 3 | node_modules/ 4 | vendor/ 5 | *.zip 6 | *.tar.gz 7 | *.swp 8 | *.txt 9 | *.log 10 | composer.lock 11 | phpunit.xml 12 | phpcs.xml 13 | .phpcs.xml 14 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing 2 | ============ 3 | 4 | We appreciate you taking the initiative to contribute to this project. 5 | 6 | Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation. 7 | 8 | For a more thorough introduction, [check out WP-CLI's guide to contributing](https://make.wordpress.org/cli/handbook/contributing/). This package follows those policy and guidelines. 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (C) 2011-2018 WP-CLI Development Group (https://github.com/wp-cli/i18n-command/contributors) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | wp-cli/maintenance-mode-command 2 | =============================== 3 | 4 | Activates, deactivates or checks the status of the maintenance mode of a site. 5 | 6 | [![Testing](https://github.com/wp-cli/maintenance-mode-command/actions/workflows/testing.yml/badge.svg)](https://github.com/wp-cli/maintenance-mode-command/actions/workflows/testing.yml) 7 | 8 | Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contributing) | [Support](#support) 9 | 10 | ## Using 11 | 12 | This package implements the following commands: 13 | 14 | ### wp maintenance-mode 15 | 16 | Activates, deactivates or checks the status of the maintenance mode of a site. 17 | 18 | ~~~ 19 | wp maintenance-mode 20 | ~~~ 21 | 22 | **EXAMPLES** 23 | 24 | # Activate Maintenance mode. 25 | $ wp maintenance-mode activate 26 | Enabling Maintenance mode... 27 | Success: Activated Maintenance mode. 28 | 29 | # Deactivate Maintenance mode. 30 | $ wp maintenance-mode deactivate 31 | Disabling Maintenance mode... 32 | Success: Deactivated Maintenance mode. 33 | 34 | # Display Maintenance mode status. 35 | $ wp maintenance-mode status 36 | Maintenance mode is active. 37 | 38 | # Get Maintenance mode status for scripting purpose. 39 | $ wp maintenance-mode is-active 40 | $ echo $? 41 | 1 42 | 43 | 44 | 45 | ### wp maintenance-mode activate 46 | 47 | Activates maintenance mode. 48 | 49 | ~~~ 50 | wp maintenance-mode activate [--force] 51 | ~~~ 52 | 53 | **OPTIONS** 54 | 55 | [--force] 56 | Force maintenance mode activation operation. 57 | 58 | **EXAMPLES** 59 | 60 | $ wp maintenance-mode activate 61 | Enabling Maintenance mode... 62 | Success: Activated Maintenance mode. 63 | 64 | 65 | 66 | ### wp maintenance-mode deactivate 67 | 68 | Deactivates maintenance mode. 69 | 70 | ~~~ 71 | wp maintenance-mode deactivate 72 | ~~~ 73 | 74 | **EXAMPLES** 75 | 76 | $ wp maintenance-mode deactivate 77 | Disabling Maintenance mode... 78 | Success: Deactivated Maintenance mode. 79 | 80 | 81 | 82 | ### wp maintenance-mode status 83 | 84 | Displays maintenance mode status. 85 | 86 | ~~~ 87 | wp maintenance-mode status 88 | ~~~ 89 | 90 | **EXAMPLES** 91 | 92 | $ wp maintenance-mode status 93 | Maintenance mode is active. 94 | 95 | 96 | 97 | ### wp maintenance-mode is-active 98 | 99 | Detects maintenance mode status. 100 | 101 | ~~~ 102 | wp maintenance-mode is-active 103 | ~~~ 104 | 105 | **EXAMPLES** 106 | 107 | $ wp maintenance-mode is-active 108 | $ echo $? 109 | 1 110 | 111 | ## Installing 112 | 113 | This package is included with WP-CLI itself, no additional installation necessary. 114 | 115 | To install the latest version of this package over what's included in WP-CLI, run: 116 | 117 | wp package install git@github.com:wp-cli/maintenance-mode-command.git 118 | 119 | ## Contributing 120 | 121 | We appreciate you taking the initiative to contribute to this project. 122 | 123 | Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation. 124 | 125 | For a more thorough introduction, [check out WP-CLI's guide to contributing](https://make.wordpress.org/cli/handbook/contributing/). This package follows those policy and guidelines. 126 | 127 | ### Reporting a bug 128 | 129 | Think you’ve found a bug? We’d love for you to help us get it fixed. 130 | 131 | Before you create a new issue, you should [search existing issues](https://github.com/wp-cli/maintenance-mode-command/issues?q=label%3Abug%20) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version. 132 | 133 | Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/wp-cli/maintenance-mode-command/issues/new). Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, [review our bug report documentation](https://make.wordpress.org/cli/handbook/bug-reports/). 134 | 135 | ### Creating a pull request 136 | 137 | Want to contribute a new feature? Please first [open a new issue](https://github.com/wp-cli/maintenance-mode-command/issues/new) to discuss whether the feature is a good fit for the project. 138 | 139 | Once you've decided to commit the time to seeing your pull request through, [please follow our guidelines for creating a pull request](https://make.wordpress.org/cli/handbook/pull-requests/) to make sure it's a pleasant experience. See "[Setting up](https://make.wordpress.org/cli/handbook/pull-requests/#setting-up)" for details specific to working on this package locally. 140 | 141 | ## Support 142 | 143 | GitHub issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support 144 | 145 | 146 | *This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.* 147 | -------------------------------------------------------------------------------- /behat.yml: -------------------------------------------------------------------------------- 1 | default: 2 | suites: 3 | default: 4 | contexts: 5 | - WP_CLI\Tests\Context\FeatureContext 6 | paths: 7 | - features 8 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wp-cli/maintenance-mode-command", 3 | "type": "wp-cli-package", 4 | "description": "Activates, deactivates or checks the status of the maintenance mode of a site.", 5 | "homepage": "https://github.com/wp-cli/maintenance-mode-command", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Thrijith Thankachan", 10 | "email": "thrijith13@gmail.com", 11 | "homepage": "https://thrijith.com" 12 | } 13 | ], 14 | "require": { 15 | "wp-cli/wp-cli": "^2.12" 16 | }, 17 | "require-dev": { 18 | "wp-cli/wp-cli-tests": "^4" 19 | }, 20 | "config": { 21 | "process-timeout": 7200, 22 | "sort-packages": true, 23 | "allow-plugins": { 24 | "dealerdirect/phpcodesniffer-composer-installer": true, 25 | "johnpbloch/wordpress-core-installer": true 26 | }, 27 | "lock": false 28 | }, 29 | "extra": { 30 | "branch-alias": { 31 | "dev-main": "2.x-dev" 32 | }, 33 | "bundled": true, 34 | "commands": [ 35 | "maintenance-mode", 36 | "maintenance-mode activate", 37 | "maintenance-mode deactivate", 38 | "maintenance-mode status", 39 | "maintenance-mode is-active" 40 | ] 41 | }, 42 | "autoload": { 43 | "psr-4": { 44 | "WP_CLI\\MaintenanceMode\\": "src/" 45 | }, 46 | "files": [ 47 | "maintenance-mode-command.php" 48 | ] 49 | }, 50 | "minimum-stability": "dev", 51 | "prefer-stable": true, 52 | "scripts": { 53 | "behat": "run-behat-tests", 54 | "behat-rerun": "rerun-behat-tests", 55 | "lint": "run-linter-tests", 56 | "phpcs": "run-phpcs-tests", 57 | "phpcbf": "run-phpcbf-cleanup", 58 | "phpunit": "run-php-unit-tests", 59 | "prepare-tests": "install-package-tests", 60 | "test": [ 61 | "@lint", 62 | "@phpcs", 63 | "@phpunit", 64 | "@behat" 65 | ] 66 | }, 67 | "support": { 68 | "issues": "https://github.com/wp-cli/maintenance-mode-command/issues" 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /features/maintenance-mode.feature: -------------------------------------------------------------------------------- 1 | Feature: Manage maintenance mode of WordPress install. 2 | 3 | Background: 4 | Given a WP install 5 | 6 | Scenario: Manage maintenance mode. 7 | 8 | When I run `wp maintenance-mode status` 9 | Then STDOUT should be: 10 | """ 11 | Maintenance mode is not active. 12 | """ 13 | 14 | When I run `wp maintenance-mode activate` 15 | Then STDOUT should be: 16 | """ 17 | Enabling Maintenance mode... 18 | Success: Activated Maintenance mode. 19 | """ 20 | 21 | When I run `wp maintenance-mode is-active` 22 | Then the return code should be 0 23 | 24 | When I run `wp maintenance-mode status` 25 | Then STDOUT should be: 26 | """ 27 | Maintenance mode is active. 28 | """ 29 | 30 | When I try `wp maintenance-mode activate` 31 | Then STDERR should be: 32 | """ 33 | Error: Maintenance mode already activated. 34 | """ 35 | 36 | When I run `wp maintenance-mode activate --force` 37 | Then STDOUT should be: 38 | """ 39 | Enabling Maintenance mode... 40 | Success: Activated Maintenance mode. 41 | """ 42 | 43 | When I run `wp maintenance-mode deactivate` 44 | Then STDOUT should be: 45 | """ 46 | Disabling Maintenance mode... 47 | Success: Deactivated Maintenance mode. 48 | """ 49 | 50 | When I try `wp maintenance-mode is-active` 51 | Then the return code should be 1 52 | 53 | When I try `wp maintenance-mode deactivate` 54 | Then STDERR should be: 55 | """ 56 | Error: Maintenance mode already deactivated. 57 | """ 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', ' 2 | 3 | Custom ruleset for WP-CLI maintenance-mode-command 4 | 5 | 12 | 13 | 14 | . 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 38 | 39 | 41 | 42 | 43 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /src/MaintenanceModeCommand.php: -------------------------------------------------------------------------------- 1 | upgrader = new WP_Upgrader( new WP_CLI\UpgraderSkin() ); 55 | $this->upgrader->init(); 56 | } 57 | 58 | /** 59 | * Activates maintenance mode. 60 | * 61 | * ## OPTIONS 62 | * 63 | * [--force] 64 | * : Force maintenance mode activation operation. 65 | * 66 | * ## EXAMPLES 67 | * 68 | * $ wp maintenance-mode activate 69 | * Enabling Maintenance mode... 70 | * Success: Activated Maintenance mode. 71 | */ 72 | public function activate( $_, $assoc_args ) { 73 | if ( $this->get_maintenance_mode_status() && ! WP_CLI\Utils\get_flag_value( $assoc_args, 'force' ) ) { 74 | WP_CLI::error( 'Maintenance mode already activated.' ); 75 | } 76 | 77 | $this->upgrader->maintenance_mode( true ); 78 | WP_CLI::success( 'Activated Maintenance mode.' ); 79 | } 80 | 81 | /** 82 | * Deactivates maintenance mode. 83 | * 84 | * ## EXAMPLES 85 | * 86 | * $ wp maintenance-mode deactivate 87 | * Disabling Maintenance mode... 88 | * Success: Deactivated Maintenance mode. 89 | */ 90 | public function deactivate() { 91 | if ( ! $this->get_maintenance_mode_status() ) { 92 | WP_CLI::error( 'Maintenance mode already deactivated.' ); 93 | } 94 | 95 | $this->upgrader->maintenance_mode( false ); 96 | WP_CLI::success( 'Deactivated Maintenance mode.' ); 97 | } 98 | 99 | /** 100 | * Displays maintenance mode status. 101 | * 102 | * ## EXAMPLES 103 | * 104 | * $ wp maintenance-mode status 105 | * Maintenance mode is active. 106 | */ 107 | public function status() { 108 | $status = $this->get_maintenance_mode_status() ? 'active' : 'not active'; 109 | WP_CLI::line( "Maintenance mode is {$status}." ); 110 | } 111 | 112 | /** 113 | * Detects maintenance mode status. 114 | * 115 | * ## EXAMPLES 116 | * 117 | * $ wp maintenance-mode is-active 118 | * $ echo $? 119 | * 1 120 | * 121 | * @subcommand is-active 122 | */ 123 | public function is_active() { 124 | WP_CLI::halt( $this->get_maintenance_mode_status() ? 0 : 1 ); 125 | } 126 | 127 | /** 128 | * Returns status of maintenance mode. 129 | * 130 | * @return bool 131 | */ 132 | private function get_maintenance_mode_status() { 133 | $wp_filesystem = $this->init_wp_filesystem(); 134 | 135 | $maintenance_file = trailingslashit( $wp_filesystem->abspath() ) . '.maintenance'; 136 | 137 | if ( ! $wp_filesystem->exists( $maintenance_file ) ) { 138 | return false; 139 | } 140 | 141 | // We use the timestamp defined in the .maintenance file 142 | // to check if the maintenance is available. 143 | $upgrading = 0; 144 | 145 | $contents = $wp_filesystem->get_contents( $maintenance_file ); 146 | $matches = []; 147 | if ( preg_match( '/upgrading\s*=\s*(\d+)\s*;/i', $contents, $matches ) ) { 148 | $upgrading = (int) $matches[1]; 149 | } else { 150 | WP_CLI::warning( 'Unable to read the maintenance file timestamp, non-numeric value detected.' ); 151 | } 152 | // The logic here is based on the core WordPress `wp_is_maintenance_mode()` function. 153 | if ( ( time() - $upgrading ) >= 10 * MINUTE_IN_SECONDS ) { 154 | return false; 155 | } 156 | return true; 157 | } 158 | 159 | /** 160 | * Initializes WP_Filesystem. 161 | * 162 | * @return WP_Filesystem_Base 163 | */ 164 | protected function init_wp_filesystem() { 165 | global $wp_filesystem; 166 | WP_Filesystem(); 167 | 168 | return $wp_filesystem; 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /wp-cli.yml: -------------------------------------------------------------------------------- 1 | require: 2 | - maintenance-mode-command.php 3 | --------------------------------------------------------------------------------