2017 © Pedro Peláez
 

library wp-config-transformer

Programmatically edit a wp-config.php file.

image

wp-cli/wp-config-transformer

Programmatically edit a wp-config.php file.

  • Friday, July 20, 2018
  • by schlessera
  • Repository
  • 7 Watchers
  • 16 Stars
  • 116,286 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 7 Versions
  • 55 % Grown

The README.md

WP Config Transformer

Programmatically edit a wp-config.php file., (*1)

Build Status, (*2)

Quick links: Using | Options | How it works | Testing, (*3)

Using

Instantiate

$config_transformer = new WPConfigTransformer( '/path/to/wp-config.php' );

Edit constants

$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true ) );
$config_transformer->add( 'constant', 'MY_SPECIAL_CONFIG', 'foo' );
$config_transformer->remove( 'constant', 'MY_SPECIAL_CONFIG' );

Edit variables

$config_transformer->update( 'variable', 'table_prefix', 'wp_custom_' );
$config_transformer->add( 'variable', 'my_special_global', 'foo' );
$config_transformer->remove( 'variable', 'my_special_global' );

Check for existence

if ( $config_transformer->exists( 'constant', 'MY_SPECIAL_CONFIG' ) ) {
    // do stuff
}

if ( $config_transformer->exists( 'variable', 'my_special_global' ) ) {
    // do stuff
}

Options

Special behaviors when adding or updating configs are available using the options array., (*4)

Normalization

In contrast to the "edit in place" strategy above, there is the option to normalize the output during a config update and effectively replace the existing syntax with output that adheres to WP Coding Standards., (*5)

Let's reconsider a poorly-formatted example:, (*6)

                 define   (    'WP_DEBUG'   ,
    false, false     )
;

This time running:, (*7)

$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true, 'normalize' => true ) );

Now we will get an output of:, (*8)

define( 'WP_DEBUG', true );

Nice!, (*9)

Raw format

Suppose you want to change your ABSPATH config (gasp!). To do that, we can run:, (*10)

$config_transformer->update( 'constant', 'ABSPATH', "dirname( __FILE__ ) . '/somewhere/else/'", array( 'raw' => true ) );

The raw option means that instead of placing the value inside the config as a string "dirname( __FILE__ ) . '/somewhere/else/'" it will become unquoted (and executable) syntax dirname( __FILE__ ) . '/somewhere/else/'., (*11)

Anchor string

The anchor string is the piece of text that additions will be anchored to., (*12)

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'anchor' => '/** Absolute path to the WordPress directory' ) ); // Default

Anchor placement

By default, new configs will be placed before the anchor string., (*13)

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'placement' => 'before' ) ); // Default
$config_transformer->update( 'constant', 'BAZ', 'qux', array( 'placement' => 'after' ) );

Anchor separator

By default, the separator between a new config and its anchor string is an EOL ("\n" on *nix and "\r\n" on Windows)., (*14)

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'separator' => PHP_EOL . PHP_EOL ) ); // Default
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'separator' => PHP_EOL ) );

Add if missing

By default, when attempting to update a config that doesn't exist, one will be added. This behavior can be overridden by specifying the add option and setting it to false., (*15)

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => true ) ); // Default
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => false ) );

If the constant FOO exists, it will be updated in-place. And if not, the update will return false:, (*16)

$config_transformer->exists( 'constant', 'FOO' ); // Returns false
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => false ) ); // Returns false

How it works

Parsing configs

Constants: https://regex101.com/r/6AeNGP/4, (*17)

Variables: https://regex101.com/r/cSLZZz/4, (*18)

Editing in place

Due to the unsemantic nature of the wp-config.php file, and PHP's loose syntax in general, the WP Config Transformer takes an "edit in place" strategy in order to preserve the original formatting and whatever other obscurities may be taking place in the block. After all, we only care about transforming values, not constant or variable names., (*19)

To achieve this, the following steps are performed:, (*20)

  1. A PHP block containing a config is split into distinct parts.
  2. Only the part containing the config value is targeted for replacement.
  3. The parts are reassembled with the new value in place.
  4. The old PHP block is replaced with the new PHP block.

Consider the following horrifically-valid PHP block, that also happens to be using the optional (and rare) 3rd argument for constant case-sensitivity:, (*21)

                 define   (    'WP_DEBUG'   ,
    false, false     )
;

The "edit in place" strategy means that running:, (*22)

$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true ) );

Will give us a result that safely changes only the value, leaving the formatting and additional argument(s) unscathed:, (*23)

                 define   (    'WP_DEBUG'   ,
    true, false     )
;

Option forwarding

Any option supported by the add() method can also be passed through the update() method and forwarded along when the config does not exist., (*24)

For example, you want to update the FOO constant in-place if it exists, otherwise it should be added to a special location:, (*25)

$config_transformer->update( 'constant', 'FOO', 'bar', array( 'anchor' => '/** My special location' ) );

Which has the same effect as the long-form logic:, (*26)

if ( $config_transformer->exists( 'constant', 'FOO' ) ) {
    $config_transformer->update( 'constant', 'FOO', 'bar' );
} else {
    $config_transformer->add( 'constant', 'FOO', 'bar', array( 'anchor' => '/** My special area' ) );
}

Of course the exception to this is if you are using the add => false option, in which case the update will return false and no config will be added., (*27)

Known issues

  1. Regex will only match one config definition per line.

CORRECT, (*28)

define( 'WP_DEBUG', true );
define( 'WP_SCRIPT_DEBUG', true );
$table_prefix = 'wp_';
$my_var = 'foo';

INCORRECT, (*29)

define( 'WP_DEBUG', true ); define( 'WP_SCRIPT_DEBUG', true );
$table_prefix = 'wp_'; $my_var = 'foo';
  1. If the third argument in define() is used, it must be a boolean.

CORRECT, (*30)

define( 'WP_DEBUG', true, false );
define( 'WP_DEBUG', true, FALSE );
define( 'foo', true, true );
define( 'foo', true, TRUE );

INCORRECT, (*31)

define( 'WP_DEBUG', true, 0 );
define( 'WP_DEBUG', true, 'yes' );
define( 'WP_DEBUG', true, 'this comma, will break everything' );

Testing

$ composer global require phpunit/phpunit
$ composer install
$ phpunit

The Versions

20/07 2018

dev-master

9999999-dev

Programmatically edit a wp-config.php file.

  Sources   Download

MIT

The Requires

  • php >=5.3.29

 

The Development Requires

by Frankie Jarrett

20/03 2018

v1.2.1

1.2.1.0

Programmatically edit a wp-config.php file.

  Sources   Download

MIT

The Requires

  • php >=5.3.29

 

The Development Requires

by Frankie Jarrett

20/03 2018

dev-2-escape-dollar

dev-2-escape-dollar

Programmatically edit a wp-config.php file.

  Sources   Download

MIT

The Requires

  • php >=5.3.29

 

The Development Requires

by Frankie Jarrett

20/03 2018

dev-build-master

dev-build-master

Programmatically edit a wp-config.php file.

  Sources   Download

MIT

The Requires

  • php >=5.3.29

 

The Development Requires

by Frankie Jarrett

27/01 2018

v1.2.0

1.2.0.0

Programmatically edit a wp-config.php file.

  Sources   Download

MIT

The Requires

  • php >=5.3.29

 

The Development Requires

by Frankie Jarrett

27/01 2018

v1.1.0

1.1.0.0

Programmatically edit a wp-config.php file.

  Sources   Download

MIT

The Requires

  • php >=5.3.29

 

The Development Requires

by Frankie Jarrett

24/01 2018

v1.0.0

1.0.0.0

Programmatically edit a wp-config.php file.

  Sources   Download

MIT

The Requires

  • php >=5.3.29

 

The Development Requires

by Frankie Jarrett