WP_CLI::launch_self()public staticWP-CLI 1.0

Run a WP-CLI command in a new process reusing the current runtime arguments.

Use WP_CLI::runcommand() instead, which is easier to use and works better.

Note: While this command does persist a limited set of runtime arguments, it does not persist environment variables. Practically speaking, WP-CLI packages won't be loaded when using WP_CLI::launch_self() because the launched process doesn't have access to the current process $HOME.

Method of the class: WP_CLI{}

No Hooks.

Return

Int|ProcessRun. The command exit status, or a ProcessRun instance

Usage

$result = WP_CLI::launch_self( $command, $args, $assoc_args, $exit_on_error, $return_detailed, $runtime_args );
$command(string) (required)
WP-CLI command to call.
$args(array)
Positional arguments to include when calling the command.
Default: []
$assoc_args(array)
Associative arguments to include when calling the command.
Default: []
$exit_on_error(true|false)
Whether to exit if the command returns an elevated return code.
Default: true
$return_detailed(true|false)
Whether to return an exit status (default) or detailed execution results.
Default: false
$runtime_args(array)
Override one or more global args (path,url,user,allow-root)
Default: []

WP_CLI::launch_self() code WP-CLI 2.8.0-alpha

public static function launch_self( $command, $args = [], $assoc_args = [], $exit_on_error = true, $return_detailed = false, $runtime_args = [] ) {
	$reused_runtime_args = [
		'path',
		'url',
		'user',
		'allow-root',
	];

	foreach ( $reused_runtime_args as $key ) {
		if ( isset( $runtime_args[ $key ] ) ) {
			$assoc_args[ $key ] = $runtime_args[ $key ];
			continue;
		}

		$value = self::get_runner()->config[ $key ];
		if ( $value ) {
			$assoc_args[ $key ] = $value;
		}
	}

	$php_bin = escapeshellarg( Utils\get_php_binary() );

	$script_path = $GLOBALS['argv'][0];

	if ( getenv( 'WP_CLI_CONFIG_PATH' ) ) {
		$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
	} else {
		$config_path = Utils\get_home_dir() . '/.wp-cli/config.yml';
	}
	$config_path = escapeshellarg( $config_path );

	$args       = implode( ' ', array_map( 'escapeshellarg', $args ) );
	$assoc_args = Utils\assoc_args_to_str( $assoc_args );

	$full_command = "WP_CLI_CONFIG_PATH={$config_path} {$php_bin} {$script_path} {$command} {$args} {$assoc_args}";

	return self::launch( $full_command, $exit_on_error, $return_detailed );
}