WP_CLI::defer_command_addition()private staticWP-CLI 1.0

Defer command addition for a sub-command if the parent command is not yet registered.

Method of the class: WP_CLI{}

No Hooks.

Return

null. Nothing.

Usage

$result = WP_CLI::defer_command_addition( $name, $parent, $callable, $args );
$name(string) (required)
Name for the sub-command.
$parent(string) (required)
Name for the parent command.
$callable(string) (required)
Command implementation as a class, function or closure.
$args(array)
See WP_CLI::add_command() for details.
Default: []

WP_CLI::defer_command_addition() code WP-CLI 2.7.0-alpha

private static function defer_command_addition( $name, $parent, $callable, $args = [] ) {
	$args['is_deferred']               = true;
	self::$deferred_additions[ $name ] = [
		'parent'   => $parent,
		'callable' => $callable,
		'args'     => $args,
	];
	self::add_hook(
		"after_add_command:$parent",
		function () use ( $name ) {
			$deferred_additions = WP_CLI::get_deferred_additions();

			if ( ! array_key_exists( $name, $deferred_additions ) ) {
				return;
			}

			$callable = $deferred_additions[ $name ]['callable'];
			$args     = $deferred_additions[ $name ]['args'];
			WP_CLI::remove_deferred_addition( $name );

			WP_CLI::add_command( $name, $callable, $args );
		}
	);
}