Skip to content

Commit

Permalink
General: Introduce all development mode.
Browse files Browse the repository at this point in the history
Introduce the development mode `all` as a a cover-all mode for the existing `theme`, `plugin` and `core` development modes. Developers can use the `all` mode if they are developing both themes and plugins, for example.

Introduce the utility function `wp_in_development_mode()` allowing developers to detect the mode via a parameter. If the development mode is set to `all` this function will always return `true`. If the development mode is specific then only the chosen mode will return `true`.

Follow up to [56079,56042].

Props flixos90.
Fixes #57487.



git-svn-id: https://develop.svn.wordpress.org/trunk@56223 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Jul 13, 2023
1 parent cdf3ac0 commit 65a048f
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/wp-includes/blocks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function register_core_block_style_handles() {
* Ignore transient cache when the development mode is set to 'core'. Why? To avoid interfering with
* the core developer's workflow.
*/
if ( 'core' !== wp_get_development_mode() ) {
if ( ! wp_in_development_mode( 'core' ) ) {
$transient_name = 'wp_core_block_css_files';
$files = get_transient( $transient_name );
if ( ! $files ) {
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/default-constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ function wp_initial_constants() {

/*
* Add define( 'WP_DEVELOPMENT_MODE', 'core' ) or define( 'WP_DEVELOPMENT_MODE', 'plugin' ) or
* define( 'WP_DEVELOPMENT_MODE', 'theme' ) to wp-config.php to signify development mode for WordPress core, a
* plugin, or a theme respectively.
* define( 'WP_DEVELOPMENT_MODE', 'theme' ) or define( 'WP_DEVELOPMENT_MODE', 'all' ) to wp-config.php
* to signify development mode for WordPress core, a plugin, a theme, or all three types respectively.
*/
if ( ! defined( 'WP_DEVELOPMENT_MODE' ) ) {
define( 'WP_DEVELOPMENT_MODE', '' );
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -5233,7 +5233,7 @@ function wp_get_global_styles_svg_filters() {
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = wp_get_development_mode() !== 'theme';
$can_use_cached = ! wp_in_development_mode( 'theme' );
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_styles_svg_filters';
if ( $can_use_cached ) {
Expand Down
8 changes: 4 additions & 4 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function wp_get_global_settings( $path = array(), $context = array() ) {
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = wp_get_development_mode() !== 'theme';
$can_use_cached = ! wp_in_development_mode( 'theme' );

$settings = false;
if ( $can_use_cached ) {
Expand Down Expand Up @@ -152,7 +152,7 @@ function wp_get_global_stylesheet( $types = array() ) {
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = empty( $types ) && wp_get_development_mode() !== 'theme';
$can_use_cached = empty( $types ) && ! wp_in_development_mode( 'theme' );

/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
Expand Down Expand Up @@ -251,7 +251,7 @@ function wp_get_global_styles_custom_css() {
* Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
* developer's workflow.
*/
$can_use_cached = wp_get_development_mode() !== 'theme';
$can_use_cached = ! wp_in_development_mode( 'theme' );

/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
Expand Down Expand Up @@ -360,7 +360,7 @@ function wp_theme_has_theme_json() {
* Ignore static cache when the development mode is set to 'theme', to avoid interfering with
* the theme developer's workflow.
*/
wp_get_development_mode() !== 'theme'
! wp_in_development_mode( 'theme' )
) {
return $theme_has_support[ $stylesheet ];
}
Expand Down
30 changes: 29 additions & 1 deletion src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,15 @@ function wp_get_environment_type() {
* The development mode affects how certain parts of the WordPress application behave, which is relevant when
* developing for WordPress.
*
* Valid developer modes are 'core', 'plugin', 'theme', or an empty string to disable developer mode.
* Valid development modes are 'core', 'plugin', 'theme', 'all', or an empty string to disable development mode.
* 'all' is a special value to signify that all three development modes 'core', 'plugin', and 'theme' are enabled.
*
* Developer mode is considered separately from `WP_DEBUG` and {@see wp_get_environment_type()}. It does not affect
* debugging output, but rather functional nuances in WordPress.
*
* This function controls the currently set development mode value. To check for whether a specific development mode is
* enabled, use wp_in_development_mode().
*
* @since 6.3.0
*
* @return string The current development mode.
Expand All @@ -298,6 +302,7 @@ function wp_get_development_mode() {
'core',
'plugin',
'theme',
'all',
'',
);
if ( ! in_array( $development_mode, $valid_modes, true ) ) {
Expand All @@ -309,6 +314,29 @@ function wp_get_development_mode() {
return $current_mode;
}

/**
* Checks whether the site is in the given development mode.
*
* @since 6.3.0
*
* @param string $mode Development mode to check for. Either 'core', 'plugin', 'theme', or 'all'.
* @return bool True if the given mode is covered by the current development mode, false otherwise.
*/
function wp_in_development_mode( $mode ) {
$current_mode = wp_get_development_mode();
if ( empty( $current_mode ) ) {
return false;
}

// Return true if the current mode encompasses all modes.
if ( 'all' === $current_mode ) {
return true;
}

// Return true if the current mode is the given mode.
return $mode === $current_mode;
}

/**
* Ensures all of WordPress is not loaded when handling a favicon.ico request.
*
Expand Down
114 changes: 114 additions & 0 deletions tests/phpunit/tests/load/wpGetDevelopmentMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* @group load.php
* @covers ::wp_get_development_mode
* @covers ::wp_in_development_mode
*/
class Test_WP_Get_Development_Mode extends WP_UnitTestCase {

Expand Down Expand Up @@ -43,4 +44,117 @@ public function test_wp_get_development_mode_filter_invalid_value() {
$_wp_tests_development_mode = 'invalid';
$this->assertSame( '', wp_get_development_mode() );
}

/**
* Tests that `wp_in_development_mode()` returns expected results.
*
* @ticket 57487
* @dataProvider data_wp_in_development_mode
*/
public function test_wp_in_development_mode( $current, $given, $expected ) {
global $_wp_tests_development_mode;

$_wp_tests_development_mode = $current;

if ( $expected ) {
$this->assertTrue( wp_in_development_mode( $given ), "{$given} is expected to pass in {$current} mode" );
} else {
$this->assertFalse( wp_in_development_mode( $given ), "{$given} is expected to fail in {$current} mode" );
}
}

/**
* Data provider that returns test scenarios for the `test_wp_in_development_mode()` method.
*
* @return array[]
*/
public function data_wp_in_development_mode() {
return array(
'core mode, testing for core' => array(
'core',
'core',
true,
),
'plugin mode, testing for plugin' => array(
'plugin',
'plugin',
true,
),
'theme mode, testing for theme' => array(
'theme',
'theme',
true,
),
'core mode, testing for plugin' => array(
'core',
'plugin',
false,
),
'core mode, testing for theme' => array(
'core',
'theme',
false,
),
'plugin mode, testing for core' => array(
'plugin',
'core',
false,
),
'plugin mode, testing for theme' => array(
'plugin',
'theme',
false,
),
'theme mode, testing for core' => array(
'theme',
'core',
false,
),
'theme mode, testing for plugin' => array(
'theme',
'plugin',
false,
),
'all mode, testing for core' => array(
'all',
'core',
true,
),
'all mode, testing for plugin' => array(
'all',
'plugin',
true,
),
'all mode, testing for theme' => array(
'all',
'theme',
true,
),
'all mode, testing for all' => array(
'all',
'all',
true,
),
'all mode, testing for non-standard value' => array(
'all',
'random',
true,
),
'invalid mode, testing for core' => array(
'invalid',
'core',
false,
),
'invalid mode, testing for plugin' => array(
'invalid',
'plugin',
false,
),
'invalid mode, testing for theme' => array(
'invalid',
'theme',
false,
),
);
}
}

0 comments on commit 65a048f

Please sign in to comment.