src/Core_Command.php
public function download( $args, $assoc_args ) {

		$download_dir = ! empty( $assoc_args['path'] )
			? ( rtrim( $assoc_args['path'], '/\\' ) . '/' )
			: ABSPATH;

		$wordpress_present = is_readable( $download_dir . 'wp-load.php' );

		if ( $wordpress_present && ! Utils\get_flag_value( $assoc_args, 'force' ) ) {
			WP_CLI::error( 'WordPress files seem to already be present here.' );
		}

		if ( ! is_dir( $download_dir ) ) {
			if ( ! is_writable( dirname( $download_dir ) ) ) {
				WP_CLI::error( "Insufficient permission to create directory '{$download_dir}'." );
			}

			WP_CLI::log( "Creating directory '{$download_dir}'." );
			if ( ! @mkdir( $download_dir, 0777, true /*recursive*/ ) ) {
				$error = error_get_last();
				WP_CLI::error( "Failed to create directory '{$download_dir}': {$error['message']}." );
			}
		}

		if ( ! is_writable( $download_dir ) ) {
			WP_CLI::error( "'{$download_dir}' is not writable by current user." );
		}

		$locale       = Utils\get_flag_value( $assoc_args, 'locale', 'en_US' );
		$skip_content = Utils\get_flag_value( $assoc_args, 'skip-content' );

		if ( isset( $assoc_args['version'] ) && 'latest' !== $assoc_args['version'] ) {
			$version = $assoc_args['version'];
			if ( in_array( strtolower( $version ), [ 'trunk', 'nightly' ], true ) ) {
				$version = 'nightly';
			}

			// Nightly builds and skip content are only available in .zip format.
			$extension = ( ( 'nightly' === $version ) || $skip_content )
				? 'zip'
				: 'tar.gz';

			$download_url = $this->get_download_url( $version, $locale, $extension );
		} else {
			$offer = $this->get_download_offer( $locale );
			if ( ! $offer ) {
				WP_CLI::error( "The requested locale ({$locale}) was not found." );
			}
			$version      = $offer->current;
			$download_url = $offer->download;
			if ( ! $skip_content ) {
				$download_url = str_replace( '.zip', '.tar.gz', $download_url );
			}
		}

		if ( 'nightly' === $version && 'en_US' !== $locale ) {
			WP_CLI::error( 'Nightly builds are only available for the en_US locale.' );
		}

		$from_version = '';
		if ( file_exists( $download_dir . 'wp-includes/version.php' ) ) {
			$wp_details   = self::get_wp_details( $download_dir );
			$from_version = $wp_details['wp_version'];
		}

		WP_CLI::log( "Downloading WordPress {$version} ({$locale})..." );

		$path_parts = pathinfo( $download_url );
		$extension  = 'tar.gz';
		if ( 'zip' === $path_parts['extension'] ) {
			$extension = 'zip';
			if ( ! class_exists( 'ZipArchive' ) ) {
				WP_CLI::error( 'Extracting a zip file requires ZipArchive.' );
			}
		}

		if ( $skip_content && 'zip' !== $extension ) {
			WP_CLI::error( 'Skip content is only available for ZIP files.' );
		}

		$cache      = WP_CLI::get_cache();
		$cache_key  = "core/wordpress-{$version}-{$locale}.{$extension}";
		$cache_file = $cache->has( $cache_key );

		$bad_cache = false;
		if ( $cache_file ) {
			WP_CLI::log( "Using cached file '{$cache_file}'..." );
			$skip_content_cache_file = $skip_content ? self::strip_content_dir( $cache_file ) : null;
			try {
				Extractor::extract( $skip_content_cache_file ?: $cache_file, $download_dir );
			} catch ( Exception $exception ) {
				WP_CLI::warning( 'Extraction failed, downloading a new copy...' );
				$bad_cache = true;
			}
		}

		if ( ! $cache_file || $bad_cache ) {
			// We need to use a temporary file because piping from cURL to tar is flaky
			// on MinGW (and probably in other environments too).
			$temp = Utils\get_temp_dir() . uniqid( 'wp_' ) . ".{$extension}";
			register_shutdown_function(
				function () use ( $temp ) {
					if ( file_exists( $temp ) ) {
						unlink( $temp );
					}
				}
			);

			$headers = [ 'Accept' => 'application/json' ];
			$options = [
				'timeout'  => 600,  // 10 minutes ought to be enough for everybody
				'filename' => $temp,
			];

			$response = Utils\http_request( 'GET', $download_url, null, $headers, $options );

			if ( 404 === (int) $response->status_code ) {
				WP_CLI::error( 'Release not found. Double-check locale or version.' );
			} elseif ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
				WP_CLI::error( "Couldn't access download URL (HTTP code {$response->status_code})." );
			}

			if ( 'nightly' !== $version ) {
				$md5_response = Utils\http_request( 'GET', $download_url . '.md5' );
				if ( 20 !== (int) substr( $md5_response->status_code, 0, 2 ) ) {
					WP_CLI::error( "Couldn't access md5 hash for release (HTTP code {$response->status_code})." );
				}

				$md5_file = md5_file( $temp );

				if ( $md5_file === $md5_response->body ) {
					WP_CLI::log( 'md5 hash verified: ' . $md5_file );
				} else {
					WP_CLI::error( "md5 hash for download ({$md5_file}) is different than the release hash ({$md5_response->body})." );
				}
			} else {
				WP_CLI::warning( 'md5 hash checks are not available for nightly downloads.' );
			}

			$skip_content_temp = $skip_content ? self::strip_content_dir( $temp ) : null;

			try {
				Extractor::extract( $skip_content_temp ?: $temp, $download_dir );
			} catch ( Exception $exception ) {
				WP_CLI::error( "Couldn't extract WordPress archive. {$exception->getMessage()}" );
			}

			if ( 'nightly' !== $version ) {
				$cache->import( $cache_key, $temp );
			}
		}

		if ( $wordpress_present ) {
			$this->cleanup_extra_files( $from_version, $version, $locale );
		}

		WP_CLI::success( 'WordPress downloaded.' );
	}
public function download( $args, $assoc_args ) {

		$download_dir = ! empty( $assoc_args['path'] )
			? ( rtrim( $assoc_args['path'], '/\\' ) . '/' )
			: ABSPATH;

		$wordpress_present = is_readable( $download_dir . 'wp-load.php' );

		if ( $wordpress_present && ! Utils\get_flag_value( $assoc_args, 'force' ) ) {
			WP_CLI::error( 'WordPress files seem to already be present here.' );
		}

		if ( ! is_dir( $download_dir ) ) {
			if ( ! is_writable( dirname( $download_dir ) ) ) {
				WP_CLI::error( "Insufficient permission to create directory '{$download_dir}'." );
			}

			WP_CLI::log( "Creating directory '{$download_dir}'." );
			if ( ! @mkdir( $download_dir, 0777, true /*recursive*/ ) ) {
				$error = error_get_last();
				WP_CLI::error( "Failed to create directory '{$download_dir}': {$error['message']}." );
			}
		}

		if ( ! is_writable( $download_dir ) ) {
			WP_CLI::error( "'{$download_dir}' is not writable by current user." );
		}

		$locale       = Utils\get_flag_value( $assoc_args, 'locale', 'en_US' );
		$skip_content = Utils\get_flag_value( $assoc_args, 'skip-content' );

		if ( isset( $assoc_args['version'] ) && 'latest' !== $assoc_args['version'] ) {
			$version = $assoc_args['version'];
			if ( in_array( strtolower( $version ), [ 'trunk', 'nightly' ], true ) ) {
				$version = 'nightly';
			}

			// Nightly builds and skip content are only available in .zip format.
			$extension = ( ( 'nightly' === $version ) || $skip_content )
				? 'zip'
				: 'tar.gz';

			$download_url = $this->get_download_url( $version, $locale, $extension );
		} else {
			$offer = $this->get_download_offer( $locale );
			if ( ! $offer ) {
				WP_CLI::error( "The requested locale ({$locale}) was not found." );
			}
			$version      = $offer->current;
			$download_url = $offer->download;
			if ( ! $skip_content ) {
				$download_url = str_replace( '.zip', '.tar.gz', $download_url );
			}
		}

		if ( 'nightly' === $version && 'en_US' !== $locale ) {
			WP_CLI::error( 'Nightly builds are only available for the en_US locale.' );
		}

		$from_version = '';
		if ( file_exists( $download_dir . 'wp-includes/version.php' ) ) {
			$wp_details   = self::get_wp_details( $download_dir );
			$from_version = $wp_details['wp_version'];
		}

		WP_CLI::log( "Downloading WordPress {$version} ({$locale})..." );

		$path_parts = pathinfo( $download_url );
		$extension  = 'tar.gz';
		if ( 'zip' === $path_parts['extension'] ) {
			$extension = 'zip';
			if ( ! class_exists( 'ZipArchive' ) ) {
				WP_CLI::error( 'Extracting a zip file requires ZipArchive.' );
			}
		}

		if ( $skip_content && 'zip' !== $extension ) {
			WP_CLI::error( 'Skip content is only available for ZIP files.' );
		}

		$cache      = WP_CLI::get_cache();
		$cache_key  = "core/wordpress-{$version}-{$locale}.{$extension}";
		$cache_file = $cache->has( $cache_key );

		$bad_cache = false;
		if ( $cache_file ) {
			WP_CLI::log( "Using cached file '{$cache_file}'..." );
			$skip_content_cache_file = $skip_content ? self::strip_content_dir( $cache_file ) : null;
			try {
				Extractor::extract( $skip_content_cache_file ?: $cache_file, $download_dir );
			} catch ( Exception $exception ) {
				WP_CLI::warning( 'Extraction failed, downloading a new copy...' );
				$bad_cache = true;
			}
		}

		if ( ! $cache_file || $bad_cache ) {
			// We need to use a temporary file because piping from cURL to tar is flaky
			// on MinGW (and probably in other environments too).
			$temp = Utils\get_temp_dir() . uniqid( 'wp_' ) . ".{$extension}";
			register_shutdown_function(
				function () use ( $temp ) {
					if ( file_exists( $temp ) ) {
						unlink( $temp );
					}
				}
			);

			$headers = [ 'Accept' => 'application/json' ];
			$options = [
				'timeout'  => 600,  // 10 minutes ought to be enough for everybody
				'filename' => $temp,
			];

			$response = Utils\http_request( 'GET', $download_url, null, $headers, $options );

			if ( 404 === (int) $response->status_code ) {
				WP_CLI::error( 'Release not found. Double-check locale or version.' );
			} elseif ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
				WP_CLI::error( "Couldn't access download URL (HTTP code {$response->status_code})." );
			}

			if ( 'nightly' !== $version ) {
				$md5_response = Utils\http_request( 'GET', $download_url . '.md5' );
				if ( 20 !== (int) substr( $md5_response->status_code, 0, 2 ) ) {
					WP_CLI::error( "Couldn't access md5 hash for release (HTTP code {$response->status_code})." );
				}

				$md5_file = md5_file( $temp );

				if ( $md5_file === $md5_response->body ) {
					WP_CLI::log( 'md5 hash verified: ' . $md5_file );
				} else {
					WP_CLI::error( "md5 hash for download ({$md5_file}) is different than the release hash ({$md5_response->body})." );
				}
			} else {
				WP_CLI::warning( 'md5 hash checks are not available for nightly downloads.' );
			}

			$skip_content_temp = $skip_content ? self::strip_content_dir( $temp ) : null;

			try {
				Extractor::extract( $skip_content_temp ?: $temp, $download_dir );
			} catch ( Exception $exception ) {
				WP_CLI::error( "Couldn't extract WordPress archive. {$exception->getMessage()}" );
			}

			if ( 'nightly' !== $version ) {
				$cache->import( $cache_key, $temp );
			}
		}

		if ( $wordpress_present ) {
			$this->cleanup_extra_files( $from_version, $version, $locale );
		}

		WP_CLI::success( 'WordPress downloaded.' );
	}
Downloads core WordPress files.

Downloads and extracts WordPress core files to the specified path. Uses
current directory when no path is specified. Downloaded build is verified
to have the correct md5 and then cached to the local filesytem.
Subsequent uses of command will use the local cache if it still exists.

## OPTIONS

[--path=<path>]
: Specify the path in which to install WordPress. Defaults to current
directory.

[--locale=<locale>]
: Select which language you want to download.

[--version=<version>]
: Select which version you want to download. Accepts a version number, 'latest' or 'nightly'

[--skip-content]
: Download WP without the default themes and plugins.

[--force]
: Overwrites existing files, if present.

## EXAMPLES

$ wp core download --locale=nl_NL
Downloading WordPress 4.5.2 (nl_NL)...
md5 hash verified: c5366d05b521831dd0b29dfc386e56a5
Success: WordPress downloaded.

@when before_wp_load