WP_CLI

Runner::setup_bootstrap_hooks()privateWP-CLI 1.0

Set up hooks meant to run during the WordPress bootstrap process

Method of the class: Runner{}

Hooks from the method

Return

null. Nothing.

Usage

// private - for code of main (parent) class only
$result = $this->setup_bootstrap_hooks();

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

private function setup_bootstrap_hooks() {

	if ( $this->config['skip-plugins'] ) {
		$this->setup_skip_plugins_filters();
	}

	if ( $this->config['skip-themes'] ) {
		WP_CLI::add_wp_hook( 'setup_theme', [ $this, 'action_setup_theme_wp_cli_skip_themes' ], 999 );
	}

	if ( $this->cmd_starts_with( [ 'help' ] ) ) {
		// Try to trap errors on help.
		$help_handler = [ $this, 'help_wp_die_handler' ]; // Avoid any cross PHP version issues by not using $this in anon function.
		WP_CLI::add_wp_hook(
			'wp_die_handler',
			function () use ( $help_handler ) {
				return $help_handler;
			}
		);
	} else {
		WP_CLI::add_wp_hook(
			'wp_die_handler',
			static function () {
				return '\WP_CLI\Utils\wp_die_handler';
			}
		);
	}

	// Prevent code from performing a redirect
	WP_CLI::add_wp_hook( 'wp_redirect', 'WP_CLI\\Utils\\wp_redirect_handler' );

	WP_CLI::add_wp_hook(
		'nocache_headers',
		static function ( $headers ) {
			// WordPress might be calling nocache_headers() because of a dead db
			global $wpdb;
			if ( ! empty( $wpdb->error ) ) {
				Utils\wp_die_handler( $wpdb->error );
			}
			// Otherwise, WP might be calling nocache_headers() because WP isn't installed
			Utils\wp_not_installed();
			return $headers;
		}
	);

	WP_CLI::add_wp_hook(
		'setup_theme',
		static function () {
			// Polyfill is_customize_preview(), as it is needed by TwentyTwenty to
			// check for starter content.
			if ( ! function_exists( 'is_customize_preview' ) ) {
				function is_customize_preview() {
					return false;
				}
			}
		},
		0
	);

	// ALTERNATE_WP_CRON might trigger a redirect, which we can't handle
	if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
		WP_CLI::add_wp_hook(
			'muplugins_loaded',
			static function () {
				remove_action( 'init', 'wp_cron' );
			}
		);
	}

	// Get rid of warnings when converting single site to multisite
	if ( defined( 'WP_INSTALLING' ) && $this->is_multisite() ) {
		$values = [
			'ms_files_rewriting'             => null,
			'active_sitewide_plugins'        => [],
			'_site_transient_update_core'    => null,
			'_site_transient_update_themes'  => null,
			'_site_transient_update_plugins' => null,
			'WPLANG'                         => '',
		];
		foreach ( $values as $key => $value ) {
			WP_CLI::add_wp_hook(
				"pre_site_option_$key",
				static function () use ( $values, $key ) {
					return $values[ $key ];
				}
			);
		}
	}

	// Always permit operations against sites, regardless of status
	WP_CLI::add_wp_hook( 'ms_site_check', '__return_true' );

	// Always permit operations against WordPress, regardless of maintenance mode
	WP_CLI::add_wp_hook(
		'enable_maintenance_mode',
		static function () {
			return false;
		}
	);

	// Use our own debug mode handling instead of WP core
	WP_CLI::add_wp_hook(
		'enable_wp_debug_mode_checks',
		static function ( $ret ) {
			Utils\wp_debug_mode();
			return false;
		}
	);

	// Never load advanced-cache.php drop-in when WP-CLI is operating
	WP_CLI::add_wp_hook(
		'enable_loading_advanced_cache_dropin',
		static function () {
			return false;
		}
	);

	// In a multisite installation, die if unable to find site given in --url parameter
	if ( $this->is_multisite() ) {
		$run_on_site_not_found = false;
		if ( $this->cmd_starts_with( [ 'cache', 'flush' ] ) ) {
			$run_on_site_not_found = 'cache flush';
		}
		if ( $this->cmd_starts_with( [ 'search-replace' ] ) ) {
			// Table-specified
			// Bits: search-replace <search> <replace> [<table>...]
			// Or not against a specific blog
			if ( count( $this->arguments ) > 3
				|| ! empty( $this->assoc_args['network'] )
				|| ! empty( $this->assoc_args['all-tables'] )
				|| ! empty( $this->assoc_args['all-tables-with-prefix'] ) ) {
				$run_on_site_not_found = 'search-replace';
			}
		}
		if ( $run_on_site_not_found
			&& Utils\wp_version_compare( '4.0', '>=' ) ) {
			WP_CLI::add_wp_hook(
				'ms_site_not_found',
				static function () use ( $run_on_site_not_found ) {
					// esc_sql() isn't yet loaded, but needed.
					if ( 'search-replace' === $run_on_site_not_found ) {
						require_once ABSPATH . WPINC . '/formatting.php';
					}
					// PHP 5.3 compatible implementation of run_command_and_exit().
					$runner = WP_CLI::get_runner();
					$runner->run_command( $runner->arguments, $runner->assoc_args );
					exit;
				},
				1
			);
		}
		WP_CLI::add_wp_hook(
			'ms_site_not_found',
			static function ( $current_site, $domain, $path ) {
				$url         = $domain . $path;
				$message     = $url ? "Site '{$url}' not found." : 'Site not found.';
				$has_param   = isset( WP_CLI::get_runner()->config['url'] );
				$has_const   = defined( 'DOMAIN_CURRENT_SITE' );
				$explanation = '';
				if ( $has_param ) {
					$explanation = 'Verify `--url=<url>` matches an existing site.';
				} else {
					$explanation = "Define DOMAIN_CURRENT_SITE in 'wp-config.php' or use `--url=<url>` to override.";

					if ( $has_const ) {
						$explanation = 'Verify DOMAIN_CURRENT_SITE matches an existing site or use `--url=<url>` to override.';
					}
				}
				if ( $explanation ) {
					$message .= ' ' . $explanation;
				}
				WP_CLI::error( $message );
			},
			10,
			3
		);
	}

	// The APC cache is not available on the command-line, so bail, to prevent cache poisoning
	WP_CLI::add_wp_hook(
		'muplugins_loaded',
		static function () {
			if ( $GLOBALS['_wp_using_ext_object_cache'] && class_exists( 'APC_Object_Cache' ) ) {
				WP_CLI::warning( 'Running WP-CLI while the APC object cache is activated can result in cache corruption.' );
				WP_CLI::confirm( 'Given the consequences, do you wish to continue?' );
			}
		},
		0
	);

	// Handle --user parameter
	if ( ! defined( 'WP_INSTALLING' ) ) {
		$config = $this->config;
		WP_CLI::add_wp_hook(
			'init',
			static function () use ( $config ) {
				if ( isset( $config['user'] ) ) {
					$fetcher = new Fetchers\User();
					$user    = $fetcher->get_check( $config['user'] );
					wp_set_current_user( $user->ID );
				} else {
					add_action( 'init', 'kses_remove_filters', 11 );
				}
			},
			0
		);
	}

	// Avoid uncaught exception when using wp_mail() without defined $_SERVER['SERVER_NAME']
	WP_CLI::add_wp_hook(
		'wp_mail_from',
		static function ( $from_email ) {
			if ( '[email protected]' === $from_email ) {
				$sitename = strtolower( Utils\parse_url( site_url(), PHP_URL_HOST ) );
				if ( substr( $sitename, 0, 4 ) === 'www.' ) {
					$sitename = substr( $sitename, 4 );
				}
				$from_email = '[email protected]' . $sitename;
			}
			return $from_email;
		}
	);

	// Don't apply set_url_scheme in get_home_url() or get_site_url().
	WP_CLI::add_wp_hook(
		'home_url',
		static function ( $url, $path, $scheme, $blog_id ) {
			if ( empty( $blog_id ) || ! is_multisite() ) {
				$url = get_option( 'home' );
			} else {
				switch_to_blog( $blog_id );
				$url = get_option( 'home' );
				restore_current_blog();
			}

			if ( $path && is_string( $path ) ) {
				$url .= '/' . ltrim( $path, '/' );
			}

			return $url;
		},
		0,
		4
	);
	WP_CLI::add_wp_hook(
		'site_url',
		static function ( $url, $path, $scheme, $blog_id ) {
			if ( empty( $blog_id ) || ! is_multisite() ) {
				$url = get_option( 'siteurl' );
			} else {
				switch_to_blog( $blog_id );
				$url = get_option( 'siteurl' );
				restore_current_blog();
			}

			if ( $path && is_string( $path ) ) {
				$url .= '/' . ltrim( $path, '/' );
			}

			return $url;
		},
		0,
		4
	);

	// Set up hook for plugins and themes to conditionally add WP-CLI commands.
	WP_CLI::add_wp_hook(
		'init',
		static function () {
			do_action( 'cli_init' );
		}
	);
}