Skip to content

Commit

Permalink
Replace env with $_ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Jun 30, 2022
1 parent 75dc3d2 commit dad87a4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 83 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,7 @@ WordPlate has archived the `wordplate/framework` package and moved everything in

1. Replace your `public/wp-config.php` file [the one in this repository](public/wp-config.php). Remember to save any custom constans defined in your `wp-config.php` file.

1. Add the [`src/helpers.php`](public/wp-config.php) file from this repository and autoload it in the `composer.json` file:

```diff
+"autoload": {
+ "files": [
+ "src/helpers.php"
+ ]
+}
```
1. Replace any usage of the `env` function with the native [`$_ENV`](https://github.com/vlucas/phpdotenv#usage) PHP superglobal.

1. Run `composer update` in the root of your project.

Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "vinkla/wordplate",
"type": "project",
"description": "The WordPlate boilerplate",
"keywords": ["wordplate", "wordpress"],
"license": "MIT",
"type": "project",
"keywords": [
"wordplate",
"wordpress"
],
"require": {
"php": "^8.0",
"composer/installers": "^2.1",
Expand All @@ -24,11 +27,6 @@
]
}
],
"autoload": {
"files": [
"src/helpers.php"
]
},
"config": {
"allow-plugins": {
"composer/installers": true,
Expand Down
64 changes: 32 additions & 32 deletions public/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,76 @@
Dotenv::createImmutable(realpath(__DIR__ . '/../'))->safeLoad();

// Set the environment type.
define('WP_ENVIRONMENT_TYPE', env('WP_ENVIRONMENT_TYPE', 'production'));
define('WP_ENVIRONMENT_TYPE', $_ENV['WP_ENVIRONMENT_TYPE'] ?? 'production');

// For developers: WordPress debugging mode.
$isDebugModeEnabled = env('WP_DEBUG', false);
$isDebugModeEnabled = $_ENV['WP_DEBUG'] ?? false;
define('WP_DEBUG', $isDebugModeEnabled);
define('WP_DEBUG_LOG', env('WP_DEBUG_LOG', false));
define('WP_DEBUG_DISPLAY', env('WP_DEBUG_DISPLAY', $isDebugModeEnabled));
define('SCRIPT_DEBUG', env('SCRIPT_DEBUG', $isDebugModeEnabled));
define('WP_DEBUG_LOG', $_ENV['WP_DEBUG_LOG'] ?? false);
define('WP_DEBUG_DISPLAY', $_ENV['WP_DEBUG_DISPLAY'] ?? $isDebugModeEnabled);
define('SCRIPT_DEBUG', $_ENV['SCRIPT_DEBUG'] ?? $isDebugModeEnabled);

// The database configuration with database name, username, password,
// hostname charset and database collae type.
define('DB_NAME', env('DB_NAME'));
define('DB_USER', env('DB_USER'));
define('DB_PASSWORD', env('DB_PASSWORD'));
define('DB_HOST', env('DB_HOST', '127.0.0.1'));
define('DB_CHARSET', env('DB_CHARSET', 'utf8mb4'));
define('DB_COLLATE', env('DB_COLLATE', 'utf8mb4_unicode_ci'));
define('DB_NAME', $_ENV['DB_NAME']);
define('DB_USER', $_ENV['DB_USER']);
define('DB_PASSWORD', $_ENV['DB_PASSWORD']);
define('DB_HOST', $_ENV['DB_HOST'] ?? '127.0.0.1');
define('DB_CHARSET', $_ENV['DB_CHARSET'] ?? 'utf8mb4');
define('DB_COLLATE', $_ENV['DB_COLLATE'] ?? 'utf8mb4_unicode_ci');

// Detect HTTPS behind a reverse proxy or a load balancer.
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
}

// Set the unique authentication keys and salts.
define('AUTH_KEY', env('AUTH_KEY'));
define('SECURE_AUTH_KEY', env('SECURE_AUTH_KEY'));
define('LOGGED_IN_KEY', env('LOGGED_IN_KEY'));
define('NONCE_KEY', env('NONCE_KEY'));
define('AUTH_SALT', env('AUTH_SALT'));
define('SECURE_AUTH_SALT', env('SECURE_AUTH_SALT'));
define('LOGGED_IN_SALT', env('LOGGED_IN_SALT'));
define('NONCE_SALT', env('NONCE_SALT'));
define('AUTH_KEY', $_ENV['AUTH_KEY']);
define('SECURE_AUTH_KEY', $_ENV['SECURE_AUTH_KEY']);
define('LOGGED_IN_KEY', $_ENV['LOGGED_IN_KEY']);
define('NONCE_KEY', $_ENV['NONCE_KEY']);
define('AUTH_SALT', $_ENV['AUTH_SALT']);
define('SECURE_AUTH_SALT', $_ENV['SECURE_AUTH_SALT']);
define('LOGGED_IN_SALT', $_ENV['LOGGED_IN_SALT']);
define('NONCE_SALT', $_ENV['NONCE_SALT']);

// Set the home url to the current domain.
define('WP_HOME', env('WP_HOME', (new Request())->getSchemeAndHttpHost()));
define('WP_HOME', $_ENV['WP_HOME'] ?? (new Request())->getSchemeAndHttpHost());

// Set the WordPress directory path.
define('WP_SITEURL', env('WP_SITEURL', sprintf('%s/%s', WP_HOME, env('WP_DIR', 'wordpress'))));
define('WP_SITEURL', $_ENV['WP_SITEURL'] ?? sprintf('%s/%s', WP_HOME, $_ENV['WP_DIR'] ?? 'wordpress'));

// Set the WordPress content directory path.
define('WP_CONTENT_DIR', env('WP_CONTENT_DIR', realpath(__DIR__ . '/../public')));
define('WP_CONTENT_URL', env('WP_CONTENT_URL', WP_HOME));
define('WP_CONTENT_DIR', $_ENV['WP_CONTENT_DIR'] ?? realpath(__DIR__ . '/../public'));
define('WP_CONTENT_URL', $_ENV['WP_CONTENT_URL'] ?? WP_HOME);

// Set the trash to less days to optimize WordPress.
define('EMPTY_TRASH_DAYS', env('EMPTY_TRASH_DAYS', 7));
define('EMPTY_TRASH_DAYS', $_ENV['EMPTY_TRASH_DAYS'] ?? 7);

// Set the default WordPress theme.
define('WP_DEFAULT_THEME', env('WP_DEFAULT_THEME', 'wordplate'));
define('WP_DEFAULT_THEME', $_ENV['WP_DEFAULT_THEME'] ?? 'wordplate');

// Constant to configure core updates.
define('WP_AUTO_UPDATE_CORE', env('WP_AUTO_UPDATE_CORE', 'minor'));
define('WP_AUTO_UPDATE_CORE', $_ENV['WP_AUTO_UPDATE_CORE'] ?? 'minor');

// Specify the number of post revisions.
define('WP_POST_REVISIONS', env('WP_POST_REVISIONS', 2));
define('WP_POST_REVISIONS', $_ENV['WP_POST_REVISIONS'] ?? 2);

// Cleanup WordPress image edits.
define('IMAGE_EDIT_OVERWRITE', env('IMAGE_EDIT_OVERWRITE', true));
define('IMAGE_EDIT_OVERWRITE', $_ENV['IMAGE_EDIT_OVERWRITE'] ?? true);

// Prevent file edititing from the dashboard.
define('DISALLOW_FILE_EDIT', env('DISALLOW_FILE_EDIT', true));
define('DISALLOW_FILE_EDIT', $_ENV['DISALLOW_FILE_EDIT'] ?? true);

// Disable technical issues emails.
define('WP_DISABLE_FATAL_ERROR_HANDLER', env('WP_DISABLE_FATAL_ERROR_HANDLER', false));
define('WP_DISABLE_FATAL_ERROR_HANDLER', $_ENV['WP_DISABLE_FATAL_ERROR_HANDLER'] ?? false);

// Set the absolute path to the WordPress directory.
if (!defined('ABSPATH')) {
define('ABSPATH', sprintf('%s/%s/', realpath(__DIR__ . '/../public'), env('WP_DIR', 'wordpress')));
define('ABSPATH', sprintf('%s/%s/', realpath(__DIR__ . '/../public'), $_ENV['WP_DIR'] ?? 'wordpress'));
}

// Set the database table prefix.
$table_prefix = env('DB_TABLE_PREFIX', 'wp_');
$table_prefix = $_ENV['DB_TABLE_PREFIX'] ?? 'wp_';

require_once ABSPATH . 'wp-settings.php';
35 changes: 0 additions & 35 deletions src/helpers.php

This file was deleted.

4 comments on commit dad87a4

@GrahamCampbell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$_SERVER is the best place to read env variables from, btw. ;)

@vinkla
Copy link
Owner Author

@vinkla vinkla commented on dad87a4 Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long time Graham! Why is $_SERVER preferable?

@GrahamCampbell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the closest constant to getenv in PHP. There are various env variables loaded by PHP that don't get put in $_ENV. If you're only interested in stuff put in a .env file loaded by phpdotenv, it doesn't matter, though.

@vinkla
Copy link
Owner Author

@vinkla vinkla commented on dad87a4 Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for letting me know. We'll stick with $_ENV since we're only using variables loaded by phpdotenv.

Please sign in to comment.