Skip to content
Merged
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
56 changes: 56 additions & 0 deletions features/config-get-field.feature
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,59 @@ Feature: Get the value of a constant or variable defined in wp-config.php and wp
---
- wp_cli_test
"""

Scenario: Get a boolean true constant from wp-config.php shows literal "true"
Given a WP install
And a wp-config.php file:
"""
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', false );
require_once( ABSPATH . 'wp-settings.php' );
"""

When I run `wp config get WP_DEBUG`
Then STDOUT should be:
"""
true
"""

When I run `wp config get WP_DEBUG_LOG`
Then STDOUT should be:
"""
false
"""

Scenario: Get a boolean constant preserves native type for json and yaml formats
Given a WP install
And a wp-config.php file:
"""
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', false );
require_once( ABSPATH . 'wp-settings.php' );
"""

When I run `wp config get WP_DEBUG --format=json`
Then STDOUT should be:
"""
true
"""

When I run `wp config get WP_DEBUG_LOG --format=json`
Then STDOUT should be:
"""
false
"""

When I run `wp config get WP_DEBUG --format=yaml`
Then STDOUT should be:
"""
---
- true
"""

When I run `wp config get WP_DEBUG_LOG --format=yaml`
Then STDOUT should be:
"""
---
- false
"""
65 changes: 62 additions & 3 deletions features/config-list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,70 @@ Feature: List the values of a wp-config.php file
SECURE_AUTH_SALT='VNH|C>w-z?*dtP4ofy!v%RumM.}ug]mx7$QZW|C-R4T`d-~x|xvL{Xc_5C89K(,^'
LOGGED_IN_SALT='Iwtez|Q`M l7lup; x&ml8^C|Lk&X[3/-l!$`P3GM$7:WI&X$Hn)unjZ9u~g4m[c'
NONCE_SALT='QxcY|80 $f_dRkn*Liu|Ak*aas41g(q5X_h+m8Z$)tf6#TZ+Q,D#%n]g -{=mj1)'
WP_ALLOW_MULTISITE=1
MULTISITE=1
SUBDOMAIN_INSTALL=''
WP_ALLOW_MULTISITE=true
MULTISITE=true
SUBDOMAIN_INSTALL=false
DOMAIN_CURRENT_SITE='example.com'
PATH_CURRENT_SITE='/'
SITE_ID_CURRENT_SITE=1
BLOG_ID_CURRENT_SITE=1
"""

Scenario: List boolean constants shows literal "true" and "false" in table format
Given a WP install
And a wp-config.php file:
"""
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
require_once( ABSPATH . 'wp-settings.php' );
"""

When I run `wp config list --fields=name,value,type --format=table`
Then STDOUT should be a table containing rows:
| name | value | type |
| WP_DEBUG | true | constant |
| WP_DEBUG_LOG | true | constant |
| WP_DEBUG_DISPLAY | false | constant |

Scenario: List boolean constants preserves native boolean type for json format
Given a WP install
And a wp-config.php file:
"""
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
require_once( ABSPATH . 'wp-settings.php' );
"""

When I run `wp config list WP_DEBUG --strict --format=json`
Then STDOUT should contain:
"""
{"name":"WP_DEBUG","value":true,"type":"constant"}
"""

When I run `wp config list WP_DEBUG_DISPLAY --strict --format=json`
Then STDOUT should contain:
"""
{"name":"WP_DEBUG_DISPLAY","value":false,"type":"constant"}
"""

Scenario: List boolean constants shows literal "true" and "false" in dotenv format
Given a WP install
And a wp-config.php file:
"""
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
require_once( ABSPATH . 'wp-settings.php' );
"""

When I run `wp config list WP_DEBUG --strict --format=dotenv`
Then STDOUT should be:
"""
WP_DEBUG=true
"""

When I run `wp config list WP_DEBUG_DISPLAY --strict --format=dotenv`
Then STDOUT should be:
"""
WP_DEBUG_DISPLAY=false
"""
19 changes: 19 additions & 0 deletions src/Config_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,14 @@ public function list_( $args, $assoc_args ) {
return array_walk( $values, array( $this, 'print_dotenv' ) );
}

if ( ! in_array( $assoc_args['format'], [ 'json', 'yaml' ], true ) ) {
foreach ( $values as $index => $value ) {
if ( is_bool( $value['value'] ) ) {
$values[ $index ]['value'] = $value['value'] ? 'true' : 'false';
}
}
}

Utils\format_items( $assoc_args['format'], $values, $assoc_args['fields'] );
}

Expand Down Expand Up @@ -514,6 +522,12 @@ public function list_( $args, $assoc_args ) {
*/
public function get( $args, $assoc_args ) {
$value = $this->get_value( $assoc_args, $args );
if ( is_bool( $value ) ) {
$format = Utils\get_flag_value( $assoc_args, 'format' );
if ( ! in_array( $format, [ 'json', 'yaml' ], true ) ) {
$value = $value ? 'true' : 'false';
}
}
WP_CLI::print_value( $value, $assoc_args );
}

Expand Down Expand Up @@ -1451,6 +1465,11 @@ private function print_dotenv( array $value ) {
$name = strtoupper( $value['name'] );
$variable_value = isset( $value['value'] ) ? $value['value'] : '';

if ( is_bool( $variable_value ) ) {
WP_CLI::line( "{$name}=" . ( $variable_value ? 'true' : 'false' ) );
return;
}

$variable_value = str_replace( "'", "\'", $variable_value );

if ( ! is_numeric( $variable_value ) ) {
Expand Down
Loading