Skip to content

Commit 874c80f

Browse files
committed
Always use WP-compatible value in WP_ENVIRONMENT_TYPE
Defaulting to production in case can't be custom value can't be mapped. Also: - ensure WP_ENV is always defined - when mapping custom env to WP, check non-word characters, e.g. "mydev" won't map to "development", "my-dev" will.
1 parent 1b95daa commit 874c80f

File tree

4 files changed

+77
-23
lines changed

4 files changed

+77
-23
lines changed

docs/03-WordPress-Integration.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ WordPress does not allow arbitrary values for `WP_ENVIRONMENT_TYPE`, in fact it
147147
- `"staging"`
148148
- `"production"`
149149

150-
Unlike WordPress, WP Starter does not limit `WP_ENVIRONMENT_TYPE` (`WP_ENV` / `WORDPRESS_ENV`) to specific values, and in the case a valuenon supported by WordPress is used, to maximize compatibility, WP Starter will try to "map" different values to one of those supported by WP.
150+
Unlike WordPress, WP Starter does not limit environment to specific values, and in the case a value not supported by WordPress is used, to maximize compatibility, WP Starter will try to "map" different values to one of those supported by WP.
151151

152-
For example, setting `WP_ENVIRONMENT_TYPE` to `"develop"` WP Starter will define a `WP_ENVIRONMENT_TYPE` constant having `"development"` as value.
152+
For example, setting `WP_ENVIRONMENT_TYPE` env variable to `"develop"` WP Starter will define a `WP_ENVIRONMENT_TYPE` constant having `"development"` as value.
153153

154-
The original `"develop"` value will be available in a constant named `WP_ENV`.
154+
The original `"develop"` value will be available in the `WP_ENV` constant.
155155

156156
In the case WP Starter is not able to map an environment to a value supported by WordPress, the original value will be available in both `WP_ENVIRONMENT_TYPE` and `WP_ENV`, but the WordPress [`wp_get_environment_type`](https://developer.wordpress.org/reference/functions/wp_get_environment_type/) function will return `"production"` because that is te default value used by WordPress.
157157

@@ -199,15 +199,13 @@ WP_ENV=something_very_custom
199199
WP Starter will declare:
200200

201201
```php
202-
define('WP_ENVIRONMENT_TYPE', 'something_very_custom');
202+
define('WP_ENVIRONMENT_TYPE', 'production');
203203
define('WP_ENV', 'something_very_custom');
204204
```
205205

206-
WP Starter was not able to map `"something_very_custom"` to any of the four environment types supported by WordPress, so stored it as-is in `WP_ENVIRONMENT_TYPE` constant.
206+
WP Starter was not able to map `"something_very_custom"` to any of the four environment types supported by WordPress, so stored "production" in `WP_ENVIRONMENT_TYPE` constant, because in any case WordPress would have defaulted to that value when calling `wp_get_environment_type()`.
207207

208-
However, because `"something_very_custom"` is not a value supported by WordPress `wp_get_environment_type()` will return `"production"`.
209-
210-
This is why we suggest referring to the constant `WP_ENVIRONMENT_TYPE` instead to the function `wp_get_environment_type()` when there's the desire (or the need) to use environment types that are not one the four supported by WordPress.
208+
This is why we suggest referring to the constant `WP_ENV` instead to the `WP_ENVIRONMENT_TYPE` constant or the function `wp_get_environment_type()` when there's the desire (or the need) to use environment types that are not one the four supported by WordPress.
211209

212210
Finally, it must be noted that is possible to use a custom WP Starter-specific environment and a WordPress compatible environment by setting _both_ `WP_ENV` and `WP_ENVIRONMENT_TYPE`.
213211

src/Env/WordPressEnvBridge.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ public function setupConstants()
587587
}
588588

589589
$envType = $this->determineEnvType();
590+
if (!defined('WP_ENV')) {
591+
define('WP_ENV', $envType);
592+
$names[] = 'WP_ENV';
593+
}
590594
if (!defined('WP_ENVIRONMENT_TYPE')) {
591595
define('WP_ENVIRONMENT_TYPE', $this->determineWpEnvType($envType));
592596
$names[] = 'WP_ENVIRONMENT_TYPE';
@@ -696,12 +700,12 @@ private function determineWpEnvType(string $envType): string
696700
}
697701

698702
foreach (self::ENV_TYPES as $envTypeName => $envTypeMapped) {
699-
if (strpos($envType, $envTypeName) !== false) {
703+
if (preg_match("~(?:^|[^a-z]+){$envTypeName}(?:[^a-z]+|$)~", $envType)) {
700704
return $envTypeMapped;
701705
}
702706
}
703707

704-
return $envType;
708+
return 'production';
705709
}
706710

707711
/**

templates/wp-config.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@
163163

164164
DEFAULT_ENV : {
165165
/** Environment-aware settings. Be creative, but avoid having sensitive settings here. */
166-
$defaultEnv = defined('WP_ENVIRONMENT_TYPE') ? WP_ENVIRONMENT_TYPE : WP_ENV;
167-
switch ($defaultEnv) {
166+
defined('WP_ENVIRONMENT_TYPE') or define('WP_ENVIRONMENT_TYPE', 'production');
167+
switch (WP_ENVIRONMENT_TYPE) {
168168
case 'local':
169169
defined('WP_LOCAL_DEV') or define('WP_LOCAL_DEV', true);
170170
case 'development':
@@ -191,10 +191,10 @@
191191
defined('SCRIPT_DEBUG') or define('SCRIPT_DEBUG', false);
192192
break;
193193
}
194-
$debugInfo['default-env-type'] = [
195-
'label' => 'Env type for defaults',
196-
'value' => $defaultEnv,
197-
'debug' => $defaultEnv,
194+
$debugInfo['wp-env-type'] = [
195+
'label' => 'WordPress env type (used for defaults)',
196+
'value' => WP_ENVIRONMENT_TYPE,
197+
'debug' => WP_ENVIRONMENT_TYPE,
198198
];
199199
} #@@/DEFAULT_ENV
200200

@@ -296,7 +296,7 @@ static function ($name) use ($envLoader) {
296296
} #@@/BEFORE_BOOTSTRAP
297297

298298
CLEAN_UP : {
299-
unset($debugInfo, $envType, $envLoader, $cacheEnv, $defaultEnv);
299+
unset($debugInfo, $envType, $envLoader);
300300
} #@@/CLEAN_UP
301301

302302
###################################################################################################

tests/integration/Env/WordPressEnvBridgeTest.php

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,32 +565,84 @@ public function testLoadWpEnvironmentTypeFromWpEnvWithAlias()
565565
* @test
566566
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
567567
*/
568-
public function testLoadWpEnvironmentTypeFromWpEnvWhenContainingValue()
568+
public function testSetDifferentEnvForWpAndWpStarter()
569569
{
570-
$_ENV['WP_ENVIRONMENT_TYPE'] = 'PREPROD-US';
570+
$_ENV['WP_ENV'] = 'something_very_custom';
571+
$_ENV['WP_ENVIRONMENT_TYPE'] = 'development';
571572
$bridge = new WordPressEnvBridge();
572573
$bridge->load('example.env', $this->fixturesPath());
573574
$bridge->setupConstants();
574575

575-
static::assertSame('preprod-us', $bridge->determineEnvType());
576+
static::assertTrue(defined('WP_ENV'));
576577
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
578+
static::assertSame('something_very_custom', WP_ENV);
579+
static::assertSame('development', WP_ENVIRONMENT_TYPE);
580+
}
581+
582+
/**
583+
* @test
584+
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
585+
*/
586+
public function testLoadWpEnvironmentTypeFromWpEnvWhenStartingWithValue()
587+
{
588+
$_ENV['WP_ENVIRONMENT_TYPE'] = 'PREPROD-US-1';
589+
$bridge = new WordPressEnvBridge();
590+
$bridge->load('example.env', $this->fixturesPath());
591+
$bridge->setupConstants();
592+
593+
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
594+
static::assertTrue(defined('WP_ENV'));
595+
static::assertSame('preprod-us-1', WP_ENV);
596+
static::assertSame('preprod-us-1', $bridge->determineEnvType());
577597
static::assertSame('staging', WP_ENVIRONMENT_TYPE);
578598
}
579599

600+
/**
601+
* @test
602+
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
603+
*/
604+
public function testLoadWpEnvironmentTypeFromWpEnvWhenEndingWithValue()
605+
{
606+
$_ENV['WP_ENVIRONMENT_TYPE'] = 'My.Production';
607+
$bridge = new WordPressEnvBridge();
608+
$bridge->load('example.env', $this->fixturesPath());
609+
$bridge->setupConstants();
610+
611+
static::assertSame('my.production', $bridge->determineEnvType());
612+
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
613+
static::assertSame('production', WP_ENVIRONMENT_TYPE);
614+
}
615+
616+
/**
617+
* @test
618+
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
619+
*/
620+
public function testLoadWpEnvironmentTypeFromWpEnvWhenValueInTheMiddle()
621+
{
622+
$_ENV['WP_ENVIRONMENT_TYPE'] = 'my_dev_one';
623+
$bridge = new WordPressEnvBridge();
624+
$bridge->load('example.env', $this->fixturesPath());
625+
$bridge->setupConstants();
626+
627+
static::assertSame('my_dev_one', $bridge->determineEnvType());
628+
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
629+
static::assertSame('development', WP_ENVIRONMENT_TYPE);
630+
}
631+
580632
/**
581633
* @test
582634
* @covers \WeCodeMore\WpStarter\Env\WordPressEnvBridge
583635
*/
584636
public function testCustomWpEnvironmentThatCantBeMapped()
585637
{
586-
$_ENV['WP_ENVIRONMENT_TYPE'] = 'THIS_CANT_BE_MAPPED';
638+
$_ENV['WP_ENVIRONMENT_TYPE'] = 'my_devone';
587639
$bridge = new WordPressEnvBridge();
588640
$bridge->load('example.env', $this->fixturesPath());
589641
$bridge->setupConstants();
590642

591-
static::assertSame('this_cant_be_mapped', $bridge->determineEnvType());
643+
static::assertSame('my_devone', $bridge->determineEnvType());
592644
static::assertTrue(defined('WP_ENVIRONMENT_TYPE'));
593-
static::assertSame('this_cant_be_mapped', WP_ENVIRONMENT_TYPE);
645+
static::assertSame('production', WP_ENVIRONMENT_TYPE);
594646
}
595647

596648
/**

0 commit comments

Comments
 (0)