Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add WP_CLI_REQUIRE env var for including extra php files
As a companion to WP_CLI_EARLY_REQUIRE but doesn't include the files
until after wp has been through the boostrap process. This is the same
as using `--require` from the command line, or require in a yaml config
file. The purpose for another way of requiring a file is for use in
codecov testing, where it otherwise is not practical to conditionally
including a php file at normal runtime using the existing methods in a
testing environment.
  • Loading branch information
mrsdizzie committed Mar 17, 2025
commit c28d9ab13b15a18453fd4a1bd0fe734f0f16be1d
38 changes: 38 additions & 0 deletions features/runcommand.feature
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,41 @@ Feature: Run a WP-CLI command
"""
The used path is: /bad/path/
"""

Scenario: Check that required files are used from command arguments and ENV VAR
Given a WP installation
And a custom-cmd.php file:
"""
<?php
class Custom_Command extends WP_CLI_Command {
/**
* Custom command to test passing command_args via runcommand options
*
* @when after_wp_load
*/
public function echo_test( $args ) {
echo "test" . PHP_EOL;
}
}
WP_CLI::add_command( 'custom-command', 'Custom_Command' );
"""
And a env.php file:
"""
<?php
echo 'ENVIRONMENT REQUIRE' . PHP_EOL;
"""

When I run `WP_CLI_REQUIRE=env.php wp eval 'return null;' --skip-wordpress`
Then STDOUT should be:
"""
ENVIRONMENT REQUIRE
"""

When I run `WP_CLI_REQUIRE=env.php wp --require=custom-cmd.php custom-command echo_test`
Then STDOUT should be:
"""
ENVIRONMENT REQUIRE
test
"""


11 changes: 11 additions & 0 deletions php/WP_CLI/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public function __construct( $path ) {

$this->config[ $key ] = $details['default'];
}

$env_files = getenv( 'WP_CLI_REQUIRE' )
? array_filter( array_map( 'trim', explode( ',', getenv( 'WP_CLI_REQUIRE' ) ) ) )
: [];

if ( ! empty( $env_files ) ) {
if ( ! isset( $this->config['require'] ) ) {
$this->config['require'] = [];
}
$this->config['require'] = array_unique( array_merge( $env_files, $this->config['require'] ) );
}
}

/**
Expand Down