WP_CLI\Context

Admin::load_admin_environment()privateWP-CLI 1.0

Load the admin environment.

This tries to load wp-admin/admin.php while trying to avoid issues like re-loading the wp-config.php file (which redeclares constants).

To make this work across WordPress versions, we use the actual file and modify it on-the-fly.

Method of the class: Admin{}

No Hooks.

Return

null. Nothing.

Usage

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

Admin::load_admin_environment() code WP-CLI 2.8.0-alpha

private function load_admin_environment() {
	global $hook_suffix, $pagenow, $wp_db_version, $_wp_submenu_nopriv;

	if ( ! isset( $hook_suffix ) ) {
		$hook_suffix = 'index'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	}

	// Make sure we don't trigger a DB upgrade as that tries to redirect
	// the page.
	$wp_db_version = (int) get_option( 'db_version' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

	// Ensure WP does not iterate over an undefined variable in
	// `user_can_access_admin_page()`.
	if ( ! isset( $_wp_submenu_nopriv ) ) {
		$_wp_submenu_nopriv = []; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	}

	$admin_php_file = file_get_contents( ABSPATH . 'wp-admin/admin.php' );

	// First we remove the opening and closing PHP tags.
	$admin_php_file = preg_replace( '/^<\?php\s+/', '', $admin_php_file );
	$admin_php_file = preg_replace( '/\s+\?>$/', '', $admin_php_file );

	// Then we remove the loading of either wp-config.php or wp-load.php.
	$admin_php_file = preg_replace( '/^\s*(?:include|require).*[\'"]\/?wp-(?:load|config)\.php[\'"]\s*\)?;$/m', '', $admin_php_file );

	// We also remove the authentication redirect.
	$admin_php_file = preg_replace( '/^\s*auth_redirect\(\);$/m', '', $admin_php_file );

	// Finally, we avoid sending headers.
	$admin_php_file   = preg_replace( '/^\s*nocache_headers\(\);$/m', '', $admin_php_file );
	$_GET['noheader'] = true;

	eval( $admin_php_file ); // phpcs:ignore Squiz.PHP.Eval.Discouraged
}