From 42dad47231ce9537d02151c1c7817cf0600f5734 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 18 Aug 2022 13:15:51 -0700 Subject: [PATCH 1/7] Load `config-spec.php` in a "filterable" way --- php/WP_CLI/Configurator.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index 1cb46f3549..05d1521c5a 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -63,7 +63,7 @@ class Configurator { * @param string $path Path to config spec file. */ public function __construct( $path ) { - $this->spec = include $path; + self::load_config_spec( $path ); $defaults = [ 'runtime' => false, @@ -80,6 +80,22 @@ public function __construct( $path ) { } } + /** + * Loads the config spec file. + * + * @param string $path Path to the config spec file. + */ + private function load_config_spec( $path ) { + $config_spec = include $path; + // A way for platforms to modify $config_spec. + // Use with caution! + if ( getenv( 'WP_CLI_INCLUDE_AFTER_CONFIG_SPEC_LOAD' ) + && is_readable( getenv( 'WP_CLI_INCLUDE_AFTER_CONFIG_SPEC_LOAD' ) ) ) { + include getenv( 'WP_CLI_INCLUDE_AFTER_CONFIG_SPEC_LOAD' ); + } + $this->spec = $config_spec; + } + /** * Get declared configuration values as an array. * From f8224681ff3bc5f9f59618ef57e064b244eab1a7 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 22 Aug 2022 12:12:20 -0700 Subject: [PATCH 2/7] Switch to a defined callback. Co-authored-by: Alain Schlesser --- php/WP_CLI/Configurator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index 05d1521c5a..7912738f1b 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -89,9 +89,9 @@ private function load_config_spec( $path ) { $config_spec = include $path; // A way for platforms to modify $config_spec. // Use with caution! - if ( getenv( 'WP_CLI_INCLUDE_AFTER_CONFIG_SPEC_LOAD' ) - && is_readable( getenv( 'WP_CLI_INCLUDE_AFTER_CONFIG_SPEC_LOAD' ) ) ) { - include getenv( 'WP_CLI_INCLUDE_AFTER_CONFIG_SPEC_LOAD' ); + $config_spec_filter_callback = getenv( 'WP_CLI_CONFIG_SPEC_FILTER_CALLBACK' ); + if ( callable( $config_spec_filter_callback ) ) { + $config_spec = $config_spec_filter_callback( $config_spec ); } $this->spec = $config_spec; } From c1b030e0d80bbbd8f216b51cc19245f68b5b3c68 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 22 Aug 2022 12:13:11 -0700 Subject: [PATCH 3/7] Fix function name --- php/WP_CLI/Configurator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index 7912738f1b..4ed56ae7e4 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -90,7 +90,7 @@ private function load_config_spec( $path ) { // A way for platforms to modify $config_spec. // Use with caution! $config_spec_filter_callback = getenv( 'WP_CLI_CONFIG_SPEC_FILTER_CALLBACK' ); - if ( callable( $config_spec_filter_callback ) ) { + if ( is_callable( $config_spec_filter_callback ) ) { $config_spec = $config_spec_filter_callback( $config_spec ); } $this->spec = $config_spec; From f1a8410f1b5074dc6d6891e166eb1300d742ad08 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 22 Aug 2022 12:13:57 -0700 Subject: [PATCH 4/7] Don't call this method statically Co-authored-by: Alain Schlesser --- php/WP_CLI/Configurator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index 4ed56ae7e4..adde2b3b30 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -63,7 +63,7 @@ class Configurator { * @param string $path Path to config spec file. */ public function __construct( $path ) { - self::load_config_spec( $path ); + $this->load_config_spec( $path ); $defaults = [ 'runtime' => false, From db6a967f2c0bdca1c6e4925045a2ecfb65883896 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 22 Aug 2022 12:18:06 -0700 Subject: [PATCH 5/7] Use a constant so the platform can force a value --- php/WP_CLI/Configurator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index adde2b3b30..575aebee4d 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -89,8 +89,8 @@ private function load_config_spec( $path ) { $config_spec = include $path; // A way for platforms to modify $config_spec. // Use with caution! - $config_spec_filter_callback = getenv( 'WP_CLI_CONFIG_SPEC_FILTER_CALLBACK' ); - if ( is_callable( $config_spec_filter_callback ) ) { + $config_spec_filter_callback = defined( 'WP_CLI_CONFIG_SPEC_FILTER_CALLBACK' ) ? constant( 'WP_CLI_CONFIG_SPEC_FILTER_CALLBACK' ) : false; + if ( $config_spec_filter_callback && is_callable( $config_spec_filter_callback ) ) { $config_spec = $config_spec_filter_callback( $config_spec ); } $this->spec = $config_spec; From c95d6ef9cbe3df9aec47d115d743f2ebfa6596f0 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 22 Aug 2022 12:20:11 -0700 Subject: [PATCH 6/7] Allow `WP_CLI_EARLY_REQUIRE` environment to load a file early --- php/wp-cli.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/php/wp-cli.php b/php/wp-cli.php index 9cc9c455cc..153e44b116 100644 --- a/php/wp-cli.php +++ b/php/wp-cli.php @@ -24,4 +24,9 @@ $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; require_once WP_CLI_ROOT . '/php/bootstrap.php'; + +if ( getenv( 'WP_CLI_EARLY_REQUIRE' ) ) { + require_once getenv( 'WP_CLI_EARLY_REQUIRE' ); +} + WP_CLI\bootstrap(); From 4073a3418447f4f2dccabb9773a952554e236a52 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Thu, 25 Aug 2022 13:45:32 -0700 Subject: [PATCH 7/7] Add a test for `WP_CLI_CONFIG_SPEC_FILTER_CALLBACK` --- features/config.feature | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/features/config.feature b/features/config.feature index 55af206bdc..c5080c7dc7 100644 --- a/features/config.feature +++ b/features/config.feature @@ -574,3 +574,27 @@ Feature: Have a config file """ Warning: UTF-8 byte-order mark (BOM) detected in wp-config.php file, stripping it for parsing. """ + + Scenario: Customize config-spec with WP_CLI_CONFIG_SPEC_FILTER_CALLBACK + Given a WP installation + And a wp-cli-early-require.php file: + """ + + """ + + When I run `wp help` + Then STDOUT should contain: + """ + --user= + """ \ No newline at end of file