Skip to content

Instantly share code, notes, and snippets.

@mauriciodulce
Created July 10, 2019 20:32
Show Gist options
  • Save mauriciodulce/f8c26d2cb23363e195d3152852316147 to your computer and use it in GitHub Desktop.
Save mauriciodulce/f8c26d2cb23363e195d3152852316147 to your computer and use it in GitHub Desktop.
<?php
define('WP_CACHE', 1); // Added by WP Rocket
/**
* The base configuration for WordPress, modified to work in both server and
* local development environments with WP Engine.
*
* The WP Engine configuration adheres to the well-known 12-Factor App principles
* of "Store config in the environment" (https://12factor.net/config) and
* "Dev/Prod Parity" (https://12factor.net/dev-prod-parity). This means you should
* use the same files and test environments on laptops as well as servers, and
* that configuration differences should be supplied by enviroment variables
* rather than things which are checked into version control.
*
* The original WordPress project documentation can be found here:
* https://codex.wordpress.org/Editing_wp-config.php
*
* @package WPEngine_DevKit
*/
define('WP_MEMORY_LIMIT', '512M');
$wpengine_is_server_environment = isset( $_ENV['WPENGINE_ACCOUNT'] );
/***** Pull data from config.json when it's available in WP Engine production */
$wpengine_configuration = null;
if ( $wpengine_is_server_environment && file_exists( dirname( __FILE__ ) . '/_wpeprivate/config.json' ) ) {
$wpengine_configuration = @json_decode( file_get_contents( dirname( __FILE__ ) . '/_wpeprivate/config.json' ) );
$wpengine_configuration_array = @json_decode( file_get_contents( dirname( __FILE__ ) . '/_wpeprivate/config.json' ), true );
} elseif ( file_exists( dirname( __FILE__ ) . '/_wpeprivate/excerpt.json' ) ) {
$wpengine_configuration = @json_decode( file_get_contents( dirname( __FILE__ ) . '/_wpeprivate/excerpt.json' ) );
$wpengine_configuration_array = @json_decode( file_get_contents( dirname( __FILE__ ) . '/_wpeprivate/excerpt.json' ), true );
}
// Make sure we're setting the platform_config global for the mu plugin.
global $wpengine_platform_config;
$wpengine_platform_config = $wpengine_configuration_array['wpengine_platform_config'];
/***** Apply WP Engine configuration */
if ( ! empty( $wpengine_configuration ) ) {
$environment_mapping = array( // Map environment variables to the JSON configuration variable.
'DB_NAME' => 'WPENGINE_SESSION_DB_SCHEMA',
'DB_USER' => 'WPENGINE_SESSION_DB_USERNAME',
'DB_PASSWORD' => 'WPENGINE_SESSION_DB_PASSWORD',
'DB_HOST' => 'WPENGINE_SESSION_DB_HOST',
);
foreach ( $environment_mapping as $define_key => $config_constant_key ) {
if ( empty( $_ENV[ $define_key ] ) && ! defined( $define_key ) && isset( $wpengine_configuration->constants->$config_constant_key ) ) {
define( $define_key, $wpengine_configuration->constants->$config_constant_key );
}
}
unset( $environment_mapping ); // @todo: clean up.
}
/***** LEGACY SECTION: For legacy WP Engine platform. Do not remove. */
if ( ! defined( 'DB_NAME' ) && ( $wpengine_is_server_environment ) ) : // Override MySQL configuration in legacy mode.
// Placeholder for where legacy system will place live values.
define( 'DB_NAME', 'foo' );
define( 'DB_USER', 'foo' );
define( 'DB_PASSWORD', 'foo' );
define( 'DB_HOST', 'foo' );
endif;
/***** END LEGACY SECTION */
// Include config values provided by the WP Engine server environment.
if ( file_exists( dirname( __FILE__ ) . '/.wpengine-conf/wp-config-site.php' ) ) {
require_once dirname( __FILE__ ) . '/.wpengine-conf/wp-config-site.php';
}
/***** Development Configuration */
// This allows a non-WP Engine environment to override configuration or behavior,
// whether or not you use the recommended environment-based configuration system.
if ( ! $wpengine_is_server_environment && file_exists( dirname( __FILE__ ) . '/wp-config-dev.php' ) ) {
require_once dirname( __FILE__ ) . '/wp-config-dev.php';
}
/***** General Override Configuration */
// This allows code to override configuration or behavior, both on WP Engine and
// in local development environments,
// whether or not you use the recommended environment-based configuration system.
if ( file_exists( dirname( __FILE__ ) . '/wp-config-custom.php' ) ) {
require_once dirname( __FILE__ ) . '/wp-config-custom.php';
}
/**
* Environment accessor function.
*
* By default we take configuration from the environment variables, which is
* best-practice for containers, legacy web servers, and more. However, this
* function can be defined ahead of time to implement a custom system.
*/
if ( ! function_exists( 'wpengine_get_environment' ) ) {
/**
* Retrieves information on the WP Engine environment
*
* @param mixed $key The key of the item to retrieve.
* @param mixed $default A default value should the key not be found.
*/
function wpengine_get_environment( $key, $default = null ) {
if ( ! array_key_exists( $key, $_ENV ) ) {
return $default;
}
return $_ENV[ $key ];
}
}
/****** AUTOMATIC SALT CREATION */
// The most secure way to supply salts is from the environment. If that's not
// available, and if salts haven't already been supplied in some other way, use
// the secure salt-generation service to create secure but stable salts.
if ( ! defined( 'AUTH_KEY' ) && ! wpengine_get_environment( 'AUTH_KEY' ) ) {
$salt_path = dirname( __FILE__ ) . '/_wpeprivate/salt.php'; // create and save salts here.
if ( ! file_exists( $salt_path ) ) { // do we need to create it now?
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api.wordpress.org/secret-key/1.1/salt/' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYSTATUS, 0 );
$salt_php = curl_exec( $ch );
curl_close( $ch );
file_put_contents( $salt_path, "<?php\n" . $salt_php );
}
require_once $salt_path;
}
// Load all of the following PHP define's from the environment.
// Match keys to default values for if the key isn't present.
$defines_from_environment = array(
'DB_NAME' => null, // The name of the database for WordPress.
'DB_USER' => null, // MySQL database username.
'DB_PASSWORD' => '', // MySQL database password.
'DB_HOST' => 'localhost', // MySQL Hostname.
'DB_CHARSET' => 'utf8', // Database Charset to use in creating database tables.
'DB_COLLATE' => '', // The Database Collate type. Don't change this if in doubt.
'AUTH_KEY' => null,
'SECURE_AUTH_KEY' => null,
'LOGGED_IN_KEY' => null,
'NONCE_KEY' => null,
'AUTH_SALT' => null,
'SECURE_AUTH_SALT' => null,
'LOGGED_IN_SALT' => null,
'NONCE_SALT' => null,
'WP_DEBUG' => false,
);
foreach ( $defines_from_environment as $key => $default ) {
if ( ! defined( $key ) ) { // Allow previous configuration to override live configuration.
define( $key, wpengine_get_environment( $key, $default ) );
}
}
// Traefik can't force this by itself so we need to build it here.
if (
(
(
isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) &&
'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']
) ||
(
isset( $_SERVER['WPE_FORCE_SSL'] ) &&
'true' === $_SERVER['WPE_FORCE_SSL']
)
) &&
(
! isset( $_SERVER['HTTPS'] ) ||
'on' !== $_SERVER['HTTPS']
)
) {
$_SERVER['HTTPS'] = 'on';
}
/******* HOME & SITE URLS */
// WordPress hard-codes hostnames, but when switching between environments
// like laptop, staging, and production, you want WordPress to automatically match
// the hostname being used by the current browser. This also helps with
// multisite/multi-domain configurations.
if ( ! defined( 'WP_CLI' ) ) {
$url_scheme = 'http';
if ( ( ! empty( $_SERVER['HTTPS'] ) && 'off' !== $_SERVER['HTTPS'] ) ||
( ! empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ||
( ! empty( $_SERVER['HTTP_USER_AGENT_HTTPS'] ) && 'ON' === $_SERVER['HTTP_USER_AGENT_HTTPS'] )
) {
$url_scheme = 'https';
}
// phpcs:disable
$url_hostname = $_SERVER['HTTP_HOST']; // WPCS: sanitization ok. XSS ok.
// phpcs:enable
if ( isset( $_SERVER['HTTP_X_FORWARDED_HOST'] ) && ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
$url_hostname = $_SERVER['HTTP_X_FORWARDED_HOST']; // WPCS: sanitization ok. XSS ok.
}
define( 'WP_HOME', "$url_scheme://$url_hostname" );
define( 'WP_SITEURL', WP_HOME );
}
// Point to memcached server using the memcache hostname.
$memcached_servers = array( 'default' => array( 0 => 'memcache' ) );
/**
* Define remaining server constants needed to prevent PHP errors and warnings.
*/
define( 'WPE_CLUSTER_ID', '0' );
define( 'PWP_NAME', '0' );
define( 'WPE_APIKEY', getenv( 'WPE_API_KEY' ) );
define( 'WPE_CLUSTER_TYPE', 'pod' );
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
if ( ! isset( $table_prefix ) ) {
$table_prefix = 'wp_'; // WPCS: override ok.
}
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment