Make WordPress Core


Ignore:
Timestamp:
04/25/2020 08:18:00 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Upgrade/Install: Introduce wp_in_maintenance_mode(), a helper function to check if WordPress is currently in maintenance mode.

Props Clorith.
Fixes #49959.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/load.php

    r47612 r47623  
    147147 * Die with a maintenance message when conditions are met.
    148148 *
    149  * Checks for a file in the WordPress root directory named ".maintenance".
    150  * This file will contain the variable $upgrading, set to the time the file
    151  * was created. If the file was created less than 10 minutes ago, WordPress
    152  * enters maintenance mode and displays a message.
    153  *
    154149 * The default message can be replaced by using a drop-in (maintenance.php in
    155150 * the wp-content directory).
    … …  
    157152 * @since 3.0.0
    158153 * @access private
    159  *
    160  * @global int $upgrading the unix timestamp marking when upgrading WordPress began.
    161154 */
    162155function wp_maintenance() {
    163         if ( ! file_exists( ABSPATH . '.maintenance' ) || wp_installing() ) {
    164                 return;
    165         }
    166 
    167         global $upgrading;
    168 
    169         require ABSPATH . '.maintenance';
    170         // If the $upgrading timestamp is older than 10 minutes, don't die.
    171         if ( ( time() - $upgrading ) >= 600 ) {
    172                 return;
    173         }
    174 
    175         /**
    176          * Filters whether to enable maintenance mode.
    177          *
    178          * This filter runs before it can be used by plugins. It is designed for
    179          * non-web runtimes. If this filter returns true, maintenance mode will be
    180          * active and the request will end. If false, the request will be allowed to
    181          * continue processing even if maintenance mode should be active.
    182          *
    183          * @since 4.6.0
    184          *
    185          * @param bool $enable_checks Whether to enable maintenance mode. Default true.
    186          * @param int  $upgrading     The timestamp set in the .maintenance file.
    187          */
    188         if ( ! apply_filters( 'enable_maintenance_mode', true, $upgrading ) ) {
     156        // Return if maintenance mode is disabled.
     157        if ( ! wp_in_maintenance_mode() ) {
    189158                return;
    190159        }
    … …  
    205174                503
    206175        );
     176}
     177
     178/**
     179 * Check if maintenance mode is enabled.
     180 *
     181 * Checks for a file in the WordPress root directory named ".maintenance".
     182 * This file will contain the variable $upgrading, set to the time the file
     183 * was created. If the file was created less than 10 minutes ago, WordPress
     184 * is in maintenance mode.
     185 *
     186 * @since 5.5.0
     187 *
     188 * @global int $upgrading The Unix timestamp marking when upgrading WordPress began.
     189 *
     190 * @return bool True if maintenance mode is enabled, false otherwise.
     191 */
     192function wp_in_maintenance_mode() {
     193        global $upgrading;
     194
     195        if ( ! file_exists( ABSPATH . '.maintenance' ) || wp_installing() ) {
     196                return false;
     197        }
     198
     199        require ABSPATH . '.maintenance';
     200        // If the $upgrading timestamp is older than 10 minutes, consider maintenance over.
     201        if ( ( time() - $upgrading ) >= 600 ) {
     202                return false;
     203        }
     204
     205        /**
     206         * Filters whether to enable maintenance mode.
     207         *
     208         * This filter runs before it can be used by plugins. It is designed for
     209         * non-web runtimes. If this filter returns true, maintenance mode will be
     210         * active and the request will end. If false, the request will be allowed to
     211         * continue processing even if maintenance mode should be active.
     212         *
     213         * @since 4.6.0
     214         *
     215         * @param bool $enable_checks Whether to enable maintenance mode. Default true.
     216         * @param int  $upgrading     The timestamp set in the .maintenance file.
     217         */
     218        if ( ! apply_filters( 'enable_maintenance_mode', true, $upgrading ) ) {
     219                return false;
     220        }
     221
     222        return true;
    207223}
    208224
Note: See TracChangeset for help on using the changeset viewer.