Skip to content

Commit

Permalink
[CMS-802] More closely align config management to roots/bedrock (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
John Spellman authored Jul 25, 2022
1 parent f9b1cd1 commit bc11ddb
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 231 deletions.
13 changes: 13 additions & 0 deletions .env.pantheon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DB_NAME=$_ENV['DB_NAME']
DB_USER=$_ENV['DB_USER']
DB_PASSWORD=$_ENV['DB_PASSWORD']

# Pantheon sets these values for you. If you want to shuffle them you can do so via your dashboard.
AUTH_KEY=$_ENV['AUTH_KEY']
SECURE_AUTH_KEY=$_ENV['SECURE_AUTH_KEY']
LOGGED_IN_KEY=$_ENV['LOGGED_IN_KEY']
NONCE_KEY=$_ENV['NONCE_KEY']
AUTH_SALT=$_ENV['AUTH_SALT']
SECURE_AUTH_SALT=$_ENV['SECURE_AUTH_SALT']
LOGGED_IN_SALT=$_ENV['LOGGED_IN_SALT']
NONCE_SALT=$_ENV['NONCE_SALT']
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ web/.htaccess
.env
.env.*
!.env.example
!.env.pantheon

# Composer
/vendor
Expand Down
33 changes: 21 additions & 12 deletions config/application.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
/**
* Your base production configuration goes in this file. Environment-specific
* overrides go in their respective config/environments/{{WP_ENV}}.php file.
* IMPORTANT NOTE:
* Do not modify this file. This file is maintained by Pantheon.
*
* Your base production configuration goes in this file.
*
* A good default policy is to deviate from the production config as little as
* possible. Try to define as much of your configuration in this file as you
Expand Down Expand Up @@ -30,13 +32,12 @@
* .env.local will override .env if it exists
*/
$env_files = file_exists($root_dir . '/.env.local')
? ['.env', '.env.local']
: ['.env'];
? ['.env', '.env.pantheon', '.env.local']
: ['.env.pantheon'];

$dotenv = Dotenv\Dotenv::createUnsafeImmutable($root_dir, $env_files, false);
if (file_exists($root_dir . '/.env')) {
$dotenv->load();
$dotenv->required(['WP_HOME', 'WP_SITEURL']);
if (!env('DATABASE_URL')) {
$dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD']);
}
Expand All @@ -48,12 +49,6 @@
*/
define('WP_ENV', env('WP_ENV') ?: 'production');

/**
* URLs
*/
Config::define('WP_HOME', env('WP_HOME'));
Config::define('WP_SITEURL', env('WP_SITEURL'));

/**
* Custom Content Directory
*/
Expand Down Expand Up @@ -97,14 +92,28 @@
* Custom Settings
*/
Config::define('AUTOMATIC_UPDATER_DISABLED', true);
Config::define('DISABLE_WP_CRON', env('DISABLE_WP_CRON') ?: false);
// Disable the plugin and theme file editor in the admin
Config::define('DISALLOW_FILE_EDIT', true);
// Disable plugin and theme updates and installation from the admin
Config::define('DISALLOW_FILE_MODS', true);
// Limit the number of post revisions that Wordpress stores (true (default WP): store every revision)
Config::define('WP_POST_REVISIONS', env('WP_POST_REVISIONS') ?: true);

/**
* Pantheon modifications
*/
if (isset($_ENV['PANTHEON_ENVIRONMENT'])) {
Config::define('DB_HOST', $_ENV['DB_HOST'] . ':' . $_ENV['DB_PORT']);
} else {
/**
* URLs
*/
Config::define('WP_HOME', env('WP_HOME'));
Config::define('WP_SITEURL', env('WP_SITEURL'));
Config::define('DB_HOST', env('DB_HOST') ?: 'localhost');
Config::define('DISABLE_WP_CRON', env('DISABLE_WP_CRON') ?: false);
}

/**
* Debugging Settings
*/
Expand Down
20 changes: 0 additions & 20 deletions config/environments/development.php

This file was deleted.

17 changes: 0 additions & 17 deletions config/environments/staging.php

This file was deleted.

208 changes: 54 additions & 154 deletions web/wp-config-pantheon.php
Original file line number Diff line number Diff line change
@@ -1,170 +1,70 @@
<?php
/*
* Don't show deprecations
*/
error_reporting( E_ALL ^ E_DEPRECATED );

/**
* Set root path
* Pantheon platform settings.
*
* IMPORTANT NOTE:
* Do not modify this file. This file is maintained by Pantheon.
*
* Site-specific modifications belong in wp-config.php, not this file. This
* file may change in future releases and modifications would cause conflicts
* when attempting to apply upstream updates.
*/
$rootPath = realpath( __DIR__ . '/..' );

/**
* Include the Composer autoload
*/
require_once( $rootPath . '/vendor/autoload.php' );

/*
* Fetch .env
*/
if ( ! isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && file_exists( $rootPath . '/.env' ) ) {
$dotenv = Dotenv\Dotenv::create($rootPath);
$dotenv->load();
$dotenv->required( array(
'DB_NAME',
'DB_USER',
'DB_HOST',
) )->notEmpty();
use Roots\WPConfig\Config;
use function Env\env;

/** A couple extra tweaks to help things run well on Pantheon. **/
if (isset($_SERVER['HTTP_HOST'])) {
// HTTP is still the default scheme for now.
$scheme = 'http';
// If we have detected that the end use is HTTPS, make sure we pass that
// through here, so <img> tags and the like don't generate mixed-mode
// content warnings.
if (isset($_SERVER['HTTP_USER_AGENT_HTTPS']) && $_SERVER['HTTP_USER_AGENT_HTTPS'] == 'ON') {
$scheme = 'https';
$_SERVER['HTTPS'] = 'on';
}
Config::define('WP_HOME', $scheme . '://' . $_SERVER['HTTP_HOST']);
Config::define('WP_SITEURL', $scheme . '://' . $_SERVER['HTTP_HOST'] . '/wp');
}

// Don't show deprecations; useful under PHP 5.5
error_reporting(E_ALL ^ E_DEPRECATED);
/** Define appropriate location for default tmp directory on Pantheon */
define('WP_TEMP_DIR', sys_get_temp_dir());

/**
* Disallow on server file edits
* Set WP_ENVIRONMENT_TYPE according to the Pantheon Environment
*/
define( 'DISALLOW_FILE_EDIT', true );
define( 'DISALLOW_FILE_MODS', true );
if (getenv('WP_ENVIRONMENT_TYPE') === false) {
switch ($_ENV['PANTHEON_ENVIRONMENT']) {
case 'live':
putenv('WP_ENVIRONMENT_TYPE=production');
break;
case 'test':
putenv('WP_ENVIRONMENT_TYPE=staging');
break;
default:
putenv('WP_ENVIRONMENT_TYPE=development');
break;
}
}

/**
* Force SSL
*/
define( 'FORCE_SSL_ADMIN', true );
if ( ! env('FORCE_SSL_ADMIN') ) {
Config::define( 'FORCE_SSL_ADMIN', true );
}

/**
* Limit post revisions
*/
define( 'WP_POST_REVISIONS', 3 );

/*
* If NOT on Pantheon
*/
if ( ! isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ):
/**
* Define site and home URLs
*/
// HTTP is still the default scheme for now.
$scheme = 'http';
// If we have detected that the end use is HTTPS, make sure we pass that
// through here, so <img> tags and the like don't generate mixed-mode
// content warnings.
if ( isset( $_SERVER['HTTP_USER_AGENT_HTTPS'] ) && $_SERVER['HTTP_USER_AGENT_HTTPS'] == 'ON' ) {
$scheme = 'https';
}
$site_url = getenv( 'WP_HOME' ) !== false ? getenv( 'WP_HOME' ) : $scheme . '://' . $_SERVER['HTTP_HOST'] . '/';
define( 'WP_HOME', $site_url );
define( 'WP_SITEURL', $site_url . 'wp/' );

/**
* Set Database Details
*/
define( 'DB_NAME', getenv( 'DB_NAME' ) );
define( 'DB_USER', getenv( 'DB_USER' ) );
define( 'DB_PASSWORD', getenv( 'DB_PASSWORD' ) !== false ? getenv( 'DB_PASSWORD' ) : '' );
define( 'DB_HOST', getenv( 'DB_HOST' ) );

/**
* Set debug modes
*/
define( 'WP_DEBUG', getenv( 'WP_DEBUG' ) === 'true' ? true : false );
define( 'IS_LOCAL', getenv( 'IS_LOCAL' ) !== false ? true : false );

/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );

endif;

/*
* If on Pantheon
* Defaults you may override
*
* To override, define your constant in your wp-config.php before wp-config-pantheon.php is required.
*/
if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ):

// ** MySQL settings - included in the Pantheon Environment ** //
/** The name of the database for WordPress */
define( 'DB_NAME', $_ENV['DB_NAME'] );

/** MySQL database username */
define( 'DB_USER', $_ENV['DB_USER'] );

/** MySQL database password */
define( 'DB_PASSWORD', $_ENV['DB_PASSWORD'] );

/** MySQL hostname; on Pantheon this includes a specific port number. */
define( 'DB_HOST', $_ENV['DB_HOST'] . ':' . $_ENV['DB_PORT'] );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* Pantheon sets these values for you also. If you want to shuffle them you
* can do so via your dashboard.
*
* @since 2.6.0
*/
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'] );
/**#@-*/

/** A couple extra tweaks to help things run well on Pantheon. **/
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
// HTTP is still the default scheme for now.
$scheme = 'http';
// If we have detected that the end use is HTTPS, make sure we pass that
// through here, so <img> tags and the like don't generate mixed-mode
// content warnings.
if ( isset( $_SERVER['HTTP_USER_AGENT_HTTPS'] ) && $_SERVER['HTTP_USER_AGENT_HTTPS'] == 'ON' ) {
$scheme = 'https';
}
define( 'WP_HOME', $scheme . '://' . $_SERVER['HTTP_HOST'] );
define( 'WP_SITEURL', $scheme . '://' . $_SERVER['HTTP_HOST'] . '/wp' );

}

// Force the use of a safe temp directory when in a container
if ( defined( 'PANTHEON_BINDING' ) ):
define( 'WP_TEMP_DIR', sprintf( '/srv/bindings/%s/tmp', PANTHEON_BINDING ) );
endif;

// FS writes aren't permitted in test or live, so we should let WordPress know to disable relevant UI
if ( in_array( $_ENV['PANTHEON_ENVIRONMENT'], array( 'test', 'live' ) ) && ! defined( 'DISALLOW_FILE_MODS' ) ) :
define( 'DISALLOW_FILE_MODS', true );
endif;

endif;
/** Disable wp-cron.php from running on every page load and rely on Pantheon to run cron via wp-cli */
$network = isset($_ENV["FRAMEWORK"]) && $_ENV["FRAMEWORK"] === "wordpress_network";
if ( ! env( 'DISABLE_WP_CRON' ) && $network === false ) {
Config::define('DISABLE_WP_CRON', true);
}
Loading

0 comments on commit bc11ddb

Please sign in to comment.