WP_CLI

Runner::start()publicWP-CLI 1.0

Method of the class: Runner{}

No Hooks.

Return

null. Nothing (null).

Usage

$Runner = new Runner();
$Runner->start();

Runner::start() code WP-CLI 2.8.0-alpha

public function start() {
	// Enable PHP error reporting to stderr if testing. Will need to be re-enabled after WP loads.
	if ( getenv( 'BEHAT_RUN' ) ) {
		$this->enable_error_reporting();
	}

	WP_CLI::debug( $this->global_config_path_debug, 'bootstrap' );
	WP_CLI::debug( $this->project_config_path_debug, 'bootstrap' );
	WP_CLI::debug( 'argv: ' . implode( ' ', $GLOBALS['argv'] ), 'bootstrap' );

	$this->check_root();
	if ( $this->alias ) {
		if ( '@all' === $this->alias && ! isset( $this->aliases['@all'] ) ) {
			WP_CLI::error( "Cannot use '@all' when no aliases are registered." );
		}

		if ( '@all' === $this->alias && is_string( $this->aliases['@all'] ) ) {
			$aliases = array_keys( $this->aliases );
			$k       = array_search( '@all', $aliases, true );
			unset( $aliases[ $k ] );
			$this->run_alias_group( $aliases );
			exit;
		}

		if ( ! array_key_exists( $this->alias, $this->aliases ) ) {
			$error_msg  = "Alias '{$this->alias}' not found.";
			$suggestion = Utils\get_suggestion( $this->alias, array_keys( $this->aliases ), $threshold = 2 );
			if ( $suggestion ) {
				$error_msg .= PHP_EOL . "Did you mean '{$suggestion}'?";
			}
			WP_CLI::error( $error_msg );
		}
		// Numerically indexed means a group of aliases
		if ( isset( $this->aliases[ $this->alias ][0] ) ) {
			$group_aliases = $this->aliases[ $this->alias ];
			$all_aliases   = array_keys( $this->aliases );
			$diff          = array_diff( $group_aliases, $all_aliases );
			if ( ! empty( $diff ) ) {
				WP_CLI::error( "Group '{$this->alias}' contains one or more invalid aliases: " . implode( ', ', $diff ) );
			}
			$this->run_alias_group( $group_aliases );
			exit;
		}

		$this->set_alias( $this->alias );
	}

	if ( empty( $this->arguments ) ) {
		$this->arguments[] = 'help';
	}

	// Protect 'cli info' from most of the runtime,
	// except when the command will be run over SSH
	if ( 'cli' === $this->arguments[0] && ! empty( $this->arguments[1] ) && 'info' === $this->arguments[1] && ! $this->config['ssh'] ) {
		$this->run_command_and_exit();
	}

	if ( isset( $this->config['http'] ) && ! class_exists( '\WP_REST_CLI\Runner' ) ) {
		WP_CLI::error( "RESTful WP-CLI needs to be installed. Try 'wp package install wp-cli/restful'." );
	}

	if ( $this->config['ssh'] ) {
		$this->run_ssh_command( $this->config['ssh'] );
		return;
	}

	// Handle --path parameter
	self::set_wp_root( $this->find_wp_root() );

	// First try at showing man page - if help command and either haven't found 'version.php' or 'wp-config.php' (so won't be loading WP & adding commands) or help on subcommand.
	if ( $this->cmd_starts_with( [ 'help' ] )
		&& ( ! $this->wp_exists()
			|| ! Utils\locate_wp_config()
			|| count( $this->arguments ) > 2
		) ) {
		$this->auto_check_update();
		$this->run_command( $this->arguments, $this->assoc_args );
		// Help didn't exit so failed to find the command at this stage.
	}

	// Handle --url parameter
	$url = self::guess_url( $this->config );
	if ( $url ) {
		WP_CLI::set_url( $url );
	}

	$this->do_early_invoke( 'before_wp_load' );

	$this->check_wp_version();

	if ( $this->cmd_starts_with( [ 'config', 'create' ] ) ) {
		$this->run_command_and_exit();
	}

	if ( ! Utils\locate_wp_config() ) {
		WP_CLI::error(
			"'wp-config.php' not found.\n" .
			'Either create one manually or use `wp config create`.'
		);
	}

	/*
	 * Set the MySQLi error reporting off because WordPress handles its own.
	 * This is due to the default value change from `MYSQLI_REPORT_OFF`
	 * to `MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT` in PHP 8.1.
	 */
	if ( function_exists( 'mysqli_report' ) ) {
		mysqli_report( 0 ); // phpcs:ignore WordPress.DB.RestrictedFunctions.mysql_mysqli_report
	}

	// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Declaring WP native constants.

	if ( $this->cmd_starts_with( [ 'core', 'is-installed' ] )
		|| $this->cmd_starts_with( [ 'core', 'update-db' ] ) ) {
		define( 'WP_INSTALLING', true );
	}

	if (
		count( $this->arguments ) >= 2 &&
		'core' === $this->arguments[0] &&
		in_array( $this->arguments[1], [ 'install', 'multisite-install' ], true )
	) {
		define( 'WP_INSTALLING', true );

		// We really need a URL here
		if ( ! isset( $_SERVER['HTTP_HOST'] ) ) {
			$url = 'https://example.com';
			WP_CLI::set_url( $url );
		}

		if ( 'multisite-install' === $this->arguments[1] ) {
			// need to fake some globals to skip the checks in wp-includes/ms-settings.php
			$url_parts = Utils\parse_url( $url );
			self::fake_current_site_blog( $url_parts );

			if ( ! defined( 'COOKIEHASH' ) ) {
				define( 'COOKIEHASH', md5( $url_parts['host'] ) );
			}
		}
	}

	if ( $this->cmd_starts_with( [ 'import' ] ) ) {
		define( 'WP_LOAD_IMPORTERS', true );
		define( 'WP_IMPORTING', true );
	}

	if ( $this->cmd_starts_with( [ 'cron', 'event', 'run' ] ) ) {
		define( 'DOING_CRON', true );
	}
	// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound

	$this->load_wordpress();

	$this->run_command_and_exit();

}