Skip to content

Commit

Permalink
Added my own phpdotenv dependency and improved the process of definin…
Browse files Browse the repository at this point in the history
…g constants in `wp-config.php` file.
  • Loading branch information
wpscholar committed Feb 15, 2019
1 parent da6b75b commit fd02d68
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 69 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Remove the .example extension on this file to activate it.
# Change the settings below as needed to customize it for your environment.

DB_NAME=wp
DB_NAME=local
DB_USER=root
DB_PASSWORD=root
DEBUG=true
WP_DEBUG=true
SCRIPT_DEBUG=true
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
!.idea/workspace.xml

# Composer
/vendor/
/vendor

# WordPress
/wp/
/wp
/salt.php

# WordPress: Config File
Expand All @@ -33,4 +33,4 @@
!/content/uploads/index.php

# dotEnv Configuration
.env
.env
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Setup a new WordPress installation via Composer

## Requirements

PHP 5.3.2+
PHP 5.6+

## Prerequisites

Install Composer by running `curl -sS https://getcomposer.org/installer | php && mv composer.phar /usr/local/bin/composer` on Linux/Unix/OSX
Install [Composer](https://getcomposer.org/doc/00-intro.md)

## Installation

Expand All @@ -22,4 +22,4 @@ Replace `{directory}` above with the folder name of your new project.

## Configuration

Open up the `.env` file and edit your database credentials.
Open up the `.env` file and edit your database credentials.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wpscholar/wp-skeleton",
"description": "Setup a new WordPress installation via Composer.",
"license": "GPL-2.0+",
"license": "GPL-2.0-or-later",
"type": "project",
"keywords": [
"WordPress",
Expand Down Expand Up @@ -32,7 +32,7 @@
"require": {
"johnpbloch/wordpress": "@stable",
"composer/installers": "@stable",
"vlucas/phpdotenv": "^2.2"
"wpscholar/phpdotenv": "^1.0"
},
"suggest": {
"wp-cli/wp-cli": "@stable"
Expand Down
3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?php

define( 'WP_USE_THEMES', true );
require( __DIR__ . '/wp/wp-blog-header.php' );
require( __DIR__ . '/wp/wp-blog-header.php' );
97 changes: 39 additions & 58 deletions wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,55 @@

require __DIR__ . '/vendor/autoload.php';

if ( file_exists( __DIR__ . '/.env' ) ) {
$dotenv = new Dotenv\Dotenv( __DIR__ );
$dotenv->load();
$dotenv->required( [ 'DB_NAME', 'DB_USER', 'DB_PASSWORD' ] );
}

if ( file_exists( __DIR__ . '/wp-config-local.php' ) ) {
require __DIR__ . '/wp-config-local.php';
} else if ( file_exists( dirname( __DIR__ ) . '/wp-config-local.php' ) ) {
require dirname( __DIR__ ) . '/wp-config-local.php';
}
use wpscholar\phpdotenv\Loader;

if ( ! defined( 'APP_DOMAIN' ) ) {
define( 'APP_DOMAIN', $_SERVER['HTTP_HOST'] );
}
/**
* Trick Local by Flywheel into reading these values
*
* define( 'DB_HOST', 'localhost' );
* define( 'DB_NAME', 'local' );
* define( 'DB_USER', 'root' );
* define( 'DB_PASSWORD', 'root' );
*/

$is_ssl = (boolean) getenv( 'HTTPS' ) || 443 == getenv( 'SERVER_PORT' ) || 'https' === getenv( 'HTTP_X_FORWARDED_PROTO' );
$scheme = $is_ssl ? 'https' : 'http';

define( 'WP_HOME', $scheme . '://' . APP_DOMAIN );
define( 'WP_SITEURL', WP_HOME . '/wp' );

define( 'WP_CONTENT_DIR', __DIR__ . '/content' );
define( 'WP_CONTENT_URL', WP_HOME . '/content' );

if ( ! defined( 'DB_NAME' ) ) {
define( 'DB_NAME', getenv( 'DB_NAME' ) );
}

if ( ! defined( 'DB_USER' ) ) {
define( 'DB_USER', getenv( 'DB_USER' ) );
}

if ( ! defined( 'DB_PASSWORD' ) ) {
define( 'DB_PASSWORD', getenv( 'DB_PASSWORD' ) );
}

if ( ! defined( 'DB_HOST' ) ) {
define( 'DB_HOST', 'localhost' );
}

if ( ! defined( 'DB_CHARSET' ) ) {
define( 'DB_CHARSET', 'utf8' );
}

if ( ! defined( 'DB_COLLATE' ) ) {
define( 'DB_COLLATE', '' );
}

if ( ! defined( 'WP_DEBUG' ) ) {
define( 'WP_DEBUG', filter_var( getenv( 'DEBUG' ), FILTER_VALIDATE_BOOLEAN ) );
}

if ( ! defined( 'DISALLOW_FILE_EDIT' ) ) {
define( 'DISALLOW_FILE_EDIT', true );
}

if ( ! isset( $table_prefix ) ) {
$table_prefix = 'wp_';
}
$loader = new Loader();
$loader
->config( [ 'adapters' => 'define' ] )
->required( [
'DB_NAME',
'DB_USER',
'DB_PASSWORD',
] )
->setDefaults( [
'ABSPATH' => __DIR__ . '/wp',
'DB_CHARSET' => 'utf8',
'DB_COLLATE' => '',
'DB_HOST' => 'localhost',
'WP_DEBUG' => false,
'WP_TABLE_PREFIX' => 'wp_',
] )
->parse( __DIR__ . '/.env' )
->set( 'WP_HOME', $scheme . '://' . $_SERVER['HTTP_HOST'] )
->set( 'WP_SITEURL', $loader->get( 'WP_HOME' ) . '/wp' )
->set( 'WP_CONTENT_DIR', __DIR__ . '/content' )
->set( 'WP_CONTENT_URL', $loader->get( 'WP_HOME' ) . '/content' )
->set( 'DISALLOW_FILE_EDIT', true )
->load();

$table_prefix = WP_TABLE_PREFIX;

// https://api.wordpress.org/secret-key/1.1/salt/
if ( file_exists( __DIR__ . '/salt.php' ) ) {
require __DIR__ . '/salt.php';
}

if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/wp' );
if ( file_exists( __DIR__ . '/wp-config-local.php' ) ) {
require __DIR__ . '/wp-config-local.php';
} else if ( file_exists( dirname( __DIR__ ) . '/wp-config-local.php' ) ) {
require dirname( __DIR__ ) . '/wp-config-local.php';
}

require_once( ABSPATH . 'wp-settings.php' );
require_once( ABSPATH . 'wp-settings.php' );

0 comments on commit fd02d68

Please sign in to comment.