Skip to content

Feature/v3 #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: PHP tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

strategy:
matrix:
php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2']

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

# https://github.com/marketplace/actions/setup-php-action
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, intl
ini-values: post_max_size=256M, max_execution_time=180

- uses: actions/checkout@v2

- name: Check PHP version
run: php -v

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: PHPStan
run: ./vendor/bin/phpstan analyse

- name: PHPUnit
run: ./vendor/bin/phpunit
2 changes: 1 addition & 1 deletion .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
if: ${{ steps.release.outputs.release_created }}
- name: Create release files
run: |
zip -r wordpress-multi-env-config.zip wp-config.*
zip -r wordpress-multi-env-config.zip .wp-config.php wp-config.*
if: ${{ steps.release.outputs.release_created }}
- name: Upload release files
uses: svenstaro/upload-release-action@v2
Expand Down
16 changes: 3 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
# OS generated files #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
/nbproject
/.project
/.settings
/.buildpath
.idea
vendor
.phpunit.result.cache
composer.lock
File renamed without changes.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

## [2.0.1](https://github.com/studio24/wordpress-multi-env-config/compare/v2.0.0...v2.0.1) (2022-08-23)


### Bug Fixes

* 29 - escape * wildcard match ([923c69c](https://github.com/studio24/wordpress-multi-env-config/commit/923c69ceb2044a0338e0b049eecc5db379522e35))
* 32: Can't use array for multisite across multiple environments ([7675bc2](https://github.com/studio24/wordpress-multi-env-config/commit/7675bc275b790e4c7fa1efe6807223d53f758596))
* Create release ZIP file and add docs on contributing and security reports ([057d949](https://github.com/studio24/wordpress-multi-env-config/commit/057d949629c9e24dd2d4da928d12bcf520bb0187))

## [1.0.2](https://github.com/studio24/wordpress-multi-env-config/releases/tag/v1.0.2) (2020-07-15)

* Previous v1 release
29 changes: 28 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,38 @@ All contributions must be made on a branch and must be merged into the main bran

All Pull Requests need at least one approval from the Studio 24 development team.

## Tests

Simple PHPUnit tests can be added to the `tests/` folder. Please try to add tests for any future changes.

## Continuous integration

[GitHub actions](https://github.com/studio24/wordpress-multi-env-config/actions) runs PHPUnit tests and PHPStan for code
quality (checks `tests/*` and the `wp-config.load.php` file). You can set this up locally via:

```
composer install
```

Run PHPUnit via:

```
vendor/bin/phpunit
```

Run PHPStan via:

```
vendor/bin/phpstan analyse
```

## Creating new releases

This repo uses [Release Please](https://github.com/marketplace/actions/release-please-action) to automatically create releases, based on [semantic versioning](https://semver.org/).

To create a new release use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) in your commit message. This will automatically create a Release PR, which is kept up to date with further commits and you can merge it to create the new release when you are ready.
To create a new release use [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) in your commit message.
On merging new code to the `main` branch this will automatically create a release PR, which you can merge to create the
new release when you are ready.

Use the following keywords in your commits:

Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Studio 24 Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
221 changes: 21 additions & 200 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,217 +1,38 @@
# Studio 24 WordPress Multi-Environment Config

This repository contains Studio 24's standard config setup for WordPress, which
loads different config based on current environment. This allows you to have different
site configuration (e.g. debug mode) for different environments (e.g. production and staging).
It is common practise in web development to create multiple environments for a website when testing it, for example
development, staging and production. It is also a common requirement to need different configuration depending on the
environment type, for example different database credentials.

This repository supports different configuration for different environments. It is Studio 24's standard config setup for
WordPress sites.

Credit is due to FocusLabs [EE Master Config](https://github.com/focuslabllc/ee-master-config)
who gave me the inspiration for the organisation of the config files.

Please note the current version is v2, if you need to use the older v1 version [please see the v1 release](https://github.com/studio24/wordpress-multi-env-config/releases/tag/v1.0.2).

## Contributing

Strata is an Open Source project. Find out more about how to [contribute](CONTRIBUTING.md).

## Security Issues

If you discover a security vulnerability within WordPress Multi-Environment Config, please follow our [disclosure procedure](SECURITY.md).

## How it works

The system detects what environment the current website is in and loads the relevant config file for that environment.

By default the environment is defined by the hostname, though you can also set this as an environment variable.

Config files are then loaded according to the current environment. There is support for loading a local config file
for sensitive data, which is intended to not be committed to version control.

### Config files

Up to three different config files are loaded:

1. **Default configuration** (in `wp-config.default.php`, e.g. shared settings such as `$table_prefix`)
2. **Environment configuration** (in `wp-config.{ENVIRONMENT}.php`, e.g. any setting specific to the environment such as database name or debug mode)
3. **Optional local settings** (in `wp-config.local.php`, e.g. any sensitive settings you do not want to commit to version control, e.g. database password)

### Environment values

By default, environment values are:

* `production` (live website)
* `staging` (test website for client review)
* `development` (local development copy of the website)

You can add other environment values by adding these to the `wp-config.env.php` file.

## Setting the environment

The current environment is detected in one of three ways:

### Environment variable

You can set an environment variable called `WP_ENV` to set which environment the website uses in your webserver configuration.

This is commonly done via Apache in your virtual host declaration:

SetEnv WP_ENV production

If you don't use Apache consult your webserver documentation.
## Requirements

### Server hostname
* PHP 7.3+

The current environment can also be detected from matching the hostname with the domain setup in `wp-config.env.php`.
## Current version

### WP-CLI
If you're using [WP-CLI](http://wp-cli.org/) you can specify your environment using an '.env' file.
The current version is v3, see [upgrading from v2](docs/upgrading.md) for details on how to use the new version.

You need to create a file called `.env` and simply write the current environment in this file as a string.
## Installation

Example:
See [installation](docs/install.md) docs.

```
development
```
This file needs to be placed among the wp-config.*.php files (this applies if you keep these files in a sub-folder and the wp-config.php file in the web root).
## Documentation

It is recommended you do not add this file to version control.
* [Docs](docs/README.md)
* [How it works](docs/how-it-works.md)
* [Setting the environment](docs/setting-the-environment.md)
* [Accessing the environment](docs/accessing-the-environment.md)

## The wp-config.env.php file

You need to edit the `wp-config.env.php` file to define some settings related to the current environment URL. This needs to
be set regardless of which method is used to set the environment, since all methods set the WordPress URL via settings
contained in this file.

This file contains a simple array, made up of:

```
environment names =>
domain => The domain name.
This can also be an array of multiple domains.
You can also use a wildcard * to indicate all sub-domains at a domain, which is useful when using
WordPress Multisite. If you use wildcards, set the domain should to a single string, not an array.
path => If WordPress is installed to a sub-folder set it here.
ssl => Whether SSL should be used on this domain. If set, this also sets FORCE_SSL_ADMIN to true.
```

Example usage:

```
$env = [
'production' => [
'domain' => 'domain.com',
'path' => '',
'ssl' => false,
],
'staging' => [
'domain' => 'staging.domain.com',
'path' => '',
'ssl' => false,
],
'development' => [
'domain' => 'domain.local',
'path' => '',
'ssl' => false,
],
];
```

If you use localhost for your local test website, just set the development hostname case to `localhost` rather than `domain.local`.

Example usage when setting a sub-folder, and also serving the live site via SSL:

```
'production' => [
'domain' => 'domain.com',
'path' => 'blog',
'ssl' => true,
],
```

Example usage for using more than one domain for an environment.

```
'production' => [
'domain' => ['domain.com', 'domain2.com'],
'path' => '',
'ssl' => false,
],
```

Example usage when using a wildcard for WordPress multi-site.

```
'production' => [
'domain' => '*.domain.com',
'path' => '',
'ssl' => false,
],
```

## Installing

Please note this requires PHP5.4 or above. You should really be on PHP5.6 at a minimum!

1. Download the required files via [wordpress-multi-env-config.zip](https://github.com/studio24/wordpress-multi-env-config/releases/latest/download/wordpress-multi-env-config.zip)
2. First make a backup of your existing `wp-config.php` file.
3. Copy the following files from this repository to your WordPress installation:

```
wp-config.default.php
wp-config.env.php
wp-config.php
wp-config.load.php
```

3. Set the correct environments you wish to support via the file `wp-config.env.php`, see the documentation above.
4. Create one `wp-config.{environment}.php` file for each environment. You can use the sample files provided in this repository:

```
wp-config.development.php
wp-config.production.php
wp-config.staging.php
wp-config.local.php
```

5. Review your backup `wp-config.php` file and copy config settings to either the default config file or the environment config files as appropriate. It is suggested to:
* If the setting is the same across all environments, add to `wp-config.default.php`
* If the setting is unique to one environment, add to `wp-config.{environment}.php`
* If the setting is sensitive (e.g. database password) add to `wp-config.local.php`
6. Remember to update the authentication unique keys and salts in `wp-config.default.php`
7. If you use version control exclude `wp-config.local.php`, an example below for Git:

```
# .gitignore
wp-config.local.php
```

You should now be able to load up the website in each different environment and everything should work just fine! It should now be safe to delete your backup *wp-config.php* file.

## Moving your config files outside of the document root

If you want to store your config files outside of the document root for additional security, this is very easy.

Simply move all the config files except for `wp-config.php` itself into another folder (which can be outside the doc root).
Next amend the require path for `wp-config.load.php` in `wp-config.php` to point to the new location and everything will work just fine!
## Contributing

Example directory structure:
WordPress Multi-Environment Config is an Open Source project. Find out more about how to [contribute](CONTRIBUTING.md).

```
config/
wp-config.default.php (Config folder outside of doc root)
wp-config.development.php
wp-config.env.php
wp-config.load.php
wp-config.local.php
wp-config.production.php
wp-config.staging.php
web/
wp-config.php (Your website doc root, where WordPress is installed)
```

Example `wp-config.php`
## Security Issues

```
/** Load the Studio 24 WordPress Multi-Environment Config. */
require_once(ABSPATH . '../config/wp-config.load.php');
```
If you discover a security vulnerability within WordPress Multi-Environment Config, please follow our [disclosure procedure](SECURITY.md).
Loading