WP_CLI::do_hook()public staticWP-CLI 1.0

Execute callbacks registered to a given hook.

See WP_CLI::add_hook() for details on WP-CLI's internal hook system. Commands can provide and call their own hooks.

Method of the class: WP_CLI{}

No Hooks.

Return

null|Mixed. Returns the first optional argument if optional arguments were passed, otherwise returns null.

Usage

$result = WP_CLI::do_hook( $when, ...$args );
$when(string) (required)
Identifier for the hook.
...$args(mixed) (required)
Arguments that will be passed onto the callback provided by WP_CLI::add_hook().

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

public static function do_hook( $when, ...$args ) {
	self::$hooks_passed[ $when ] = $args;

	$has_args = count( $args ) > 0;

	if ( ! isset( self::$hooks[ $when ] ) ) {
		if ( $has_args ) {
			return $args[0];
		}

		return null;
	}

	self::debug(
		sprintf(
			'Processing hook "%s" with %d callbacks',
			$when,
			count( self::$hooks[ $when ] )
		),
		'hooks'
	);

	foreach ( self::$hooks[ $when ] as $callback ) {
		self::debug(
			sprintf(
				'On hook "%s": %s',
				$when,
				Utils\describe_callable( $callback )
			),
			'hooks'
		);

		if ( $has_args ) {
			$return_value = $callback( ...$args );
			if ( isset( $return_value ) ) {
				$args[0] = $return_value;
			}
		} else {
			$callback();
		}
	}

	if ( $has_args ) {
		return $args[0];
	}

	return null;
}