Make WordPress Core

Changeset 56042


Ignore:
Timestamp:
06/26/2023 07:55:28 PM (10 months ago)
Author:
flixos90
Message:

General: Introduce WP_DEVELOPMENT_MODE constant to signify context-specific development mode.

In recent releases, WordPress core added several instances of cache usage around specific files. While those caches are safe to use in a production context, in development certain nuances apply for whether or not those caches make sense to use. Initially, WP_DEBUG was used as a temporary workaround, but it was clear that a more granular method to signify a specific development mode was required: For example, caches around theme.json should be disabled when working on a theme as otherwise it would disrupt the theme developer's workflow, but when working on a plugin or WordPress core, this consideration does not apply.

This changeset introduces a WP_DEVELOPMENT_MODE constant which, for now, can be set to either "core", "plugin", "theme", or an empty string, the latter of which means no development mode, which is also the default. A new function wp_get_development_mode() is the recommended way to retrieve that configuration value.

With the new function available, this changeset replaces all existing instances of the aforementioned WP_DEBUG workaround to use wp_get_development_mode() with a more specific check.

Props azaozz, sergeybiryukov, peterwilsoncc, spacedmonkey.
Fixes #57487.

Location:
trunk
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/.env

    r55920 r56042  
    6060LOCAL_SCRIPT_DEBUG=true
    6161LOCAL_WP_ENVIRONMENT_TYPE=local
     62LOCAL_WP_DEVELOPMENT_MODE=core
    6263
    6364# The URL to use when running e2e tests.
  • trunk/src/wp-includes/default-constants.php

    r56032 r56042  
    7878    }
    7979
     80    /*
     81     * Add define( 'WP_DEVELOPMENT_MODE', 'core' ) or define( 'WP_DEVELOPMENT_MODE', 'plugin' ) or
     82     * define( 'WP_DEVELOPMENT_MODE', 'theme' ) to wp-config.php to signify development mode for WordPress core, a
     83     * plugin, or a theme respectively.
     84     */
     85    if ( ! defined( 'WP_DEVELOPMENT_MODE' ) ) {
     86        define( 'WP_DEVELOPMENT_MODE', '' );
     87    }
     88
    8089    // Add define( 'WP_DEBUG', true ); to wp-config.php to enable display of notices during development.
    8190    if ( ! defined( 'WP_DEBUG' ) ) {
    82         if ( 'development' === wp_get_environment_type() ) {
     91        if ( wp_get_development_mode() || 'development' === wp_get_environment_type() ) {
    8392            define( 'WP_DEBUG', true );
    8493        } else {
  • trunk/src/wp-includes/global-styles-and-settings.php

    r56038 r56042  
    6767
    6868    /*
    69      * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
     69     * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
    7070     * developer's workflow.
    71      *
    72      * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
    73      */
    74     $can_use_cached = ! WP_DEBUG;
     71     */
     72    $can_use_cached = wp_get_development_mode() !== 'theme';
    7573
    7674    $settings = false;
     
    152150function wp_get_global_stylesheet( $types = array() ) {
    153151    /*
    154      * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
     152     * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
    155153     * developer's workflow.
    156      *
    157      * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
    158      */
    159     $can_use_cached = empty( $types ) && ! WP_DEBUG;
     154     */
     155    $can_use_cached = empty( $types ) && wp_get_development_mode() !== 'theme';
    160156
    161157    /*
     
    253249    }
    254250    /*
    255      * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
     251     * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
    256252     * developer's workflow.
    257      *
    258      * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
    259      */
    260     $can_use_cached = ! WP_DEBUG;
     253     */
     254    $can_use_cached = wp_get_development_mode() !== 'theme';
    261255
    262256    /*
     
    304298function wp_get_global_styles_svg_filters() {
    305299    /*
    306      * Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
     300     * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
    307301     * developer's workflow.
    308      *
    309      * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
    310      */
    311     $can_use_cached = ! WP_DEBUG;
     302     */
     303    $can_use_cached = wp_get_development_mode() !== 'theme';
    312304    $cache_group    = 'theme_json';
    313305    $cache_key      = 'wp_get_global_styles_svg_filters';
     
    403395        null !== $theme_has_support &&
    404396        /*
    405          * Ignore static cache when `WP_DEBUG` is enabled. Why? To avoid interfering with
     397         * Ignore static cache when the development mode is set to 'theme', to avoid interfering with
    406398         * the theme developer's workflow.
    407          *
    408          * @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
    409399         */
    410         ! WP_DEBUG &&
     400        wp_get_development_mode() !== 'theme' &&
    411401        /*
    412402         * Ignore cache when automated test suites are running. Why? To ensure
  • trunk/src/wp-includes/load.php

    r56032 r56042  
    260260
    261261    return $current_env;
     262}
     263
     264/**
     265 * Retrieves the current development mode.
     266 *
     267 * The development mode affects how certain parts of the WordPress application behave, which is relevant when
     268 * developing for WordPress.
     269 *
     270 * Valid developer modes are 'core', 'plugin', 'theme', or an empty string to disable developer mode.
     271 *
     272 * Developer mode is considered separately from `WP_DEBUG` and {@see wp_get_environment_type()}. It does not affect
     273 * debugging output, but rather functional nuances in WordPress.
     274 *
     275 * @since 6.3.0
     276 *
     277 * @return string The current development mode.
     278 */
     279function wp_get_development_mode() {
     280    static $current_mode = null;
     281
     282    if ( ! defined( 'WP_RUN_CORE_TESTS' ) && null !== $current_mode ) {
     283        return $current_mode;
     284    }
     285
     286    $development_mode = WP_DEVELOPMENT_MODE;
     287
     288    // Exclusively for core tests, rely on a global `$_wp_tests_development_mode`.
     289    if ( defined( 'WP_RUN_CORE_TESTS' ) && isset( $GLOBALS['_wp_tests_development_mode'] ) ) {
     290        $development_mode = $GLOBALS['_wp_tests_development_mode'];
     291    }
     292
     293    $valid_modes = array(
     294        'core',
     295        'plugin',
     296        'theme',
     297        '',
     298    );
     299    if ( ! in_array( $development_mode, $valid_modes, true ) ) {
     300        $development_mode = '';
     301    }
     302
     303    $current_mode = $development_mode;
     304
     305    return $current_mode;
    262306}
    263307
  • trunk/tests/phpunit/tests/theme/wpGetGlobalStylesSvgFilters.php

    r55185 r56042  
    3535        $this->assertNotSame( $svg_for_default_theme, $svg_for_block_theme, 'Cache value should have changed' );
    3636    }
     37
     38    /**
     39     * Tests that the function relies on the development mode for whether to use caching.
     40     *
     41     * @ticket 57487
     42     *
     43     * @covers ::wp_get_global_styles_svg_filters
     44     */
     45    public function test_caching_is_used_when_developing_theme() {
     46        global $_wp_tests_development_mode;
     47
     48        switch_theme( 'block-theme' );
     49
     50        // Store SVG in cache.
     51        $svg = '<svg></svg>';
     52        wp_cache_set( 'wp_get_global_styles_svg_filters', $svg, 'theme_json' );
     53
     54        // By default, caching should be used, so the above value will be returned.
     55        $_wp_tests_development_mode = '';
     56        $this->assertSame( $svg, wp_get_global_styles_svg_filters(), 'Caching was not used despite development mode disabled' );
     57
     58        // When the development mode is set to 'theme', caching should not be used.
     59        $_wp_tests_development_mode = 'theme';
     60        $this->assertNotSame( $svg, wp_get_global_styles_svg_filters(), 'Caching was used despite theme development mode' );
     61    }
    3762}
  • trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php

    r55572 r56042  
    222222
    223223    /**
     224     * Tests that the function relies on the development mode for whether to use caching.
     225     *
     226     * @ticket 57487
     227     */
     228    public function test_caching_is_used_when_developing_theme() {
     229        global $_wp_tests_development_mode;
     230
     231        $this->maybe_switch_theme( 'block-theme' );
     232
     233        // Store CSS in cache.
     234        $css = '.my-class { display: block; }';
     235        wp_cache_set( 'wp_get_global_stylesheet', $css, 'theme_json' );
     236
     237        // By default, caching should be used, so the above value will be returned.
     238        $_wp_tests_development_mode = '';
     239        $this->assertSame( $css, wp_get_global_stylesheet(), 'Caching was not used despite development mode disabled' );
     240
     241        // When the development mode is set to 'theme', caching should not be used.
     242        $_wp_tests_development_mode = 'theme';
     243        $this->assertNotSame( $css, wp_get_global_stylesheet(), 'Caching was used despite theme development mode' );
     244    }
     245
     246    /**
    224247     * Adds the 'editor-font-sizes' theme support with custom font sizes.
    225248     *
  • trunk/tools/local-env/scripts/install.js

    r54096 r56042  
    1717wp_cli( `config set SCRIPT_DEBUG ${process.env.LOCAL_SCRIPT_DEBUG} --raw --type=constant` );
    1818wp_cli( `config set WP_ENVIRONMENT_TYPE ${process.env.LOCAL_WP_ENVIRONMENT_TYPE} --type=constant` );
     19wp_cli( `config set WP_DEVELOPMENT_MODE ${process.env.LOCAL_WP_DEVELOPMENT_MODE} --type=constant` );
    1920
    2021// Move wp-config.php to the base directory, so it doesn't get mixed up in the src or build directories.
Note: See TracChangeset for help on using the changeset viewer.