private function set_history_file() { $data = getcwd() . get_current_user(); $this->history_file = \WP_CLI\Utils\get_temp_dir() . 'wp-cli-history-' . md5($data); }
/** * Update WP-CLI to the latest release. * * Default behavior is to check the releases API for the newest stable * version, and prompt if one is available. * * Use `--stable` to install or reinstall the latest stable version. * * Use `--nightly` to install the latest built version of the master branch. * While not recommended for production, nightly contains the latest and * greatest, and should be stable enough for development and staging * environments. * * Only works for the Phar installation mechanism. * * ## OPTIONS * * [--patch] * : Only perform patch updates. * * [--minor] * : Only perform minor updates. * * [--major] * : Only perform major updates. * * [--stable] * : Update to the latest stable release. Skips update check. * * [--nightly] * : Update to the latest built version of the master branch. Potentially unstable. * * [--yes] * : Do not prompt for confirmation. * * ## EXAMPLES * * # Update CLI. * $ wp cli update * You have version 0.24.0. Would you like to update to 0.24.1? [y/n] y * Downloading from https://github.com/wp-cli/wp-cli/releases/download/v0.24.1/wp-cli-0.24.1.phar... * New version works. Proceeding to replace. * Success: Updated WP-CLI to 0.24.1. */ public function update($_, $assoc_args) { if (!Utils\inside_phar()) { WP_CLI::error("You can only self-update Phar files."); } $old_phar = realpath($_SERVER['argv'][0]); if (!is_writable($old_phar)) { WP_CLI::error(sprintf("%s is not writable by current user.", $old_phar)); } else { if (!is_writeable(dirname($old_phar))) { WP_CLI::error(sprintf("%s is not writable by current user.", dirname($old_phar))); } } if (Utils\get_flag_value($assoc_args, 'nightly')) { WP_CLI::confirm(sprintf('You have version %s. Would you like to update to the latest nightly?', WP_CLI_VERSION), $assoc_args); $download_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar'; $md5_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar.md5'; } else { if (Utils\get_flag_value($assoc_args, 'stable')) { WP_CLI::confirm(sprintf('You have version %s. Would you like to update to the latest stable release?', WP_CLI_VERSION), $assoc_args); $download_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar'; $md5_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar.md5'; } else { $updates = $this->get_updates($assoc_args); if (empty($updates)) { $update_type = $this->get_update_type_str($assoc_args); WP_CLI::success("WP-CLI is at the latest{$update_type}version."); return; } $newest = $updates[0]; WP_CLI::confirm(sprintf('You have version %s. Would you like to update to %s?', WP_CLI_VERSION, $newest['version']), $assoc_args); $download_url = $newest['package_url']; $md5_url = str_replace('.phar', '.phar.md5', $download_url); } } WP_CLI::log(sprintf('Downloading from %s...', $download_url)); $temp = \WP_CLI\Utils\get_temp_dir() . uniqid('wp_') . '.phar'; $headers = array(); $options = array('timeout' => 600, 'filename' => $temp); Utils\http_request('GET', $download_url, null, $headers, $options); $md5_response = Utils\http_request('GET', $md5_url); if (20 != substr($md5_response->status_code, 0, 2)) { WP_CLI::error("Couldn't access md5 hash for release (HTTP code {$md5_response->status_code})."); } $md5_file = md5_file($temp); $release_hash = trim($md5_response->body); if ($md5_file === $release_hash) { WP_CLI::log('md5 hash verified: ' . $release_hash); } else { WP_CLI::error("md5 hash for download ({$md5_file}) is different than the release hash ({$release_hash})."); } $allow_root = WP_CLI::get_runner()->config['allow-root'] ? '--allow-root' : ''; $php_binary = WP_CLI::get_php_binary(); $process = WP_CLI\Process::create("{$php_binary} {$temp} --info {$allow_root}"); $result = $process->run(); if (0 !== $result->return_code || false === stripos($result->stdout, 'WP-CLI version:')) { $multi_line = explode(PHP_EOL, $result->stderr); WP_CLI::error_multi_line($multi_line); WP_CLI::error('The downloaded PHAR is broken, try running wp cli update again.'); } WP_CLI::log('New version works. Proceeding to replace.'); $mode = fileperms($old_phar) & 511; if (false === @chmod($temp, $mode)) { WP_CLI::error(sprintf("Cannot chmod %s.", $temp)); } class_exists('\\cli\\Colors'); // This autoloads \cli\Colors - after we move the file we no longer have access to this class. if (false === @rename($temp, $old_phar)) { WP_CLI::error(sprintf("Cannot move %s to %s", $temp, $old_phar)); } if (Utils\get_flag_value($assoc_args, 'nightly')) { $updated_version = 'the latest nightly release'; } else { if (Utils\get_flag_value($assoc_args, 'stable')) { $updated_version = 'the latest stable release'; } else { $updated_version = $newest['version']; } } WP_CLI::success(sprintf('Updated WP-CLI to %s.', $updated_version)); }
/** * Download core WordPress files. * * ## OPTIONS * * [--path=<path>] * : Specify the path in which to install WordPress. * * [--locale=<locale>] * : Select which language you want to download. * * [--version=<version>] * : Select which version you want to download. * * [--force] * : Overwrites existing files, if present. * * ## EXAMPLES * * wp core download --locale=nl_NL * * @when before_wp_load */ public function download($args, $assoc_args) { $download_dir = !empty($assoc_args['path']) ? $assoc_args['path'] : ABSPATH; if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'force') && is_readable($download_dir . 'wp-load.php')) { WP_CLI::error('WordPress files seem to already be present here.'); } if (!is_dir($download_dir)) { WP_CLI::log(sprintf('Creating directory %s', $download_dir)); $mkdir = \WP_CLI\Utils\is_windows() ? 'mkdir %s' : 'mkdir -p %s'; WP_CLI::launch(Utils\esc_cmd($mkdir, $download_dir)); } $locale = \WP_CLI\Utils\get_flag_value($assoc_args, 'locale', 'en_US'); if (isset($assoc_args['version'])) { $version = $assoc_args['version']; $download_url = $this->get_download_url($version, $locale, 'tar.gz'); } else { $offer = $this->get_download_offer($locale); if (!$offer) { WP_CLI::error("The requested locale ({$locale}) was not found."); } $version = $offer['current']; $download_url = str_replace('.zip', '.tar.gz', $offer['download']); } WP_CLI::log(sprintf('Downloading WordPress %s (%s)...', $version, $locale)); $cache = WP_CLI::get_cache(); $cache_key = "core/wordpress-{$version}-{$locale}.tar.gz"; $cache_file = $cache->has($cache_key); $bad_cache = false; if ($cache_file) { WP_CLI::log("Using cached file '{$cache_file}'..."); try { self::_extract($cache_file, $download_dir); } catch (Exception $e) { 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 = \WP_CLI\Utils\get_temp_dir() . uniqid('wp_') . '.tar.gz'; $headers = array('Accept' => 'application/json'); $options = array('timeout' => 600, 'filename' => $temp); $response = Utils\http_request('GET', $download_url, null, $headers, $options); if (404 == $response->status_code) { WP_CLI::error("Release not found. Double-check locale or version."); } else { if (20 != substr($response->status_code, 0, 2)) { WP_CLI::error("Couldn't access download URL (HTTP code {$response->status_code})"); } } try { self::_extract($temp, $download_dir); } catch (Exception $e) { WP_CLI::error("Couldn't extract WordPress archive. " . $e->getMessage()); } $cache->import($cache_key, $temp); unlink($temp); } WP_CLI::success('WordPress downloaded.'); }
/** * Fetch most recent update matching the requirements. Returns the available versions if there are updates, or empty if no update available. * * ## OPTIONS * * [--patch] * : Only perform patch updates * * [--minor] * : Only perform minor updates * * [--major] * : Only perform major updates * * [--nightly] * : Update to the latest built version of the master branch. Potentially unstable. * * [--yes] * : Do not prompt for confirmation */ public function update($_, $assoc_args) { if (!Utils\inside_phar()) { WP_CLI::error("You can only self-update Phar files."); } $old_phar = realpath($_SERVER['argv'][0]); if (!is_writable($old_phar)) { WP_CLI::error(sprintf("%s is not writable by current user", $old_phar)); } else { if (!is_writeable(dirname($old_phar))) { WP_CLI::error(sprintf("%s is not writable by current user", dirname($old_phar))); } } if (isset($assoc_args['nightly'])) { WP_CLI::confirm(sprintf('You have version %s. Would you like to update to the latest nightly?', WP_CLI_VERSION), $assoc_args); $download_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar'; } else { $updates = $this->get_updates($assoc_args); if (empty($updates)) { $update_type = $this->get_update_type_str($assoc_args); WP_CLI::success("WP-CLI is at the latest{$update_type}version."); exit(0); } $newest = $updates[0]; WP_CLI::confirm(sprintf('You have version %s. Would you like to update to %s?', WP_CLI_VERSION, $newest['version']), $assoc_args); $download_url = $newest['package_url']; } WP_CLI::log(sprintf('Downloading from %s...', $download_url)); $temp = \WP_CLI\Utils\get_temp_dir() . uniqid('wp_') . '.phar'; $headers = array(); $options = array('timeout' => 600, 'filename' => $temp); Utils\http_request('GET', $download_url, null, $headers, $options); $allow_root = WP_CLI::get_runner()->config['allow-root'] ? '--allow-root' : ''; $php_binary = WP_CLI::get_php_binary(); $process = WP_CLI\Process::create("{$php_binary} {$temp} --version {$allow_root}"); $result = $process->run(); if (0 !== $result->return_code) { $multi_line = explode(PHP_EOL, $result->stderr); WP_CLI::error_multi_line($multi_line); WP_CLI::error('The downloaded PHAR is broken, try running wp cli update again.'); } WP_CLI::log('New version works. Proceeding to replace.'); $mode = fileperms($old_phar) & 511; if (false === @chmod($temp, $mode)) { WP_CLI::error(sprintf("Cannot chmod %s", $temp)); } class_exists('\\cli\\Colors'); // this autoloads \cli\Colors - after we move the file we no longer have access to this class if (false === @rename($temp, $old_phar)) { WP_CLI::error(sprintf("Cannot move %s to %s", $temp, $old_phar)); } if (isset($assoc_args['nightly'])) { $updated_version = 'the latest nightly release'; } else { $updated_version = $newest['version']; } WP_CLI::success(sprintf('Updated WP-CLI to %s', $updated_version)); }
/** * Download core WordPress files. * * Downloads and extracts WordPress core files to the specified path. Uses * an archive file stored in cache if WordPress has been previously * downloaded. * * ## OPTIONS * * [--path=<path>] * : Specify the path in which to install WordPress. * * [--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' * * [--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 */ public function download($args, $assoc_args) { $download_dir = !empty($assoc_args['path']) ? $assoc_args['path'] : ABSPATH; $wordpress_present = is_readable($download_dir . 'wp-load.php'); if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'force') && $wordpress_present) { 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(sprintf("Insufficient permission to create directory '%s'.", $download_dir)); } WP_CLI::log(sprintf("Creating directory '%s'.", $download_dir)); $mkdir = \WP_CLI\Utils\is_windows() ? 'mkdir %s' : 'mkdir -p %s'; WP_CLI::launch(Utils\esc_cmd($mkdir, $download_dir)); } if (!is_writable($download_dir)) { WP_CLI::error(sprintf("'%s' is not writable by current user.", $download_dir)); } $locale = \WP_CLI\Utils\get_flag_value($assoc_args, 'locale', 'en_US'); if (isset($assoc_args['version']) && 'latest' !== $assoc_args['version']) { $version = $assoc_args['version']; $version = in_array(strtolower($version), array('trunk', 'nightly')) ? 'nightly' : $version; //nightly builds are only available in .zip format $ext = 'nightly' === $version ? 'zip' : 'tar.gz'; $download_url = $this->get_download_url($version, $locale, $ext); } else { $offer = $this->get_download_offer($locale); if (!$offer) { WP_CLI::error("The requested locale ({$locale}) was not found."); } $version = $offer['current']; $download_url = str_replace('.zip', '.tar.gz', $offer['download']); } 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')) { global $wp_version; require_once $download_dir . 'wp-includes/version.php'; $from_version = $wp_version; } WP_CLI::log(sprintf('Downloading WordPress %s (%s)...', $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.'); } } $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}'..."); try { Extractor::extract($cache_file, $download_dir); } catch (Exception $e) { 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 = \WP_CLI\Utils\get_temp_dir() . uniqid('wp_') . ".{$extension}"; $headers = array('Accept' => 'application/json'); $options = array('timeout' => 600, 'filename' => $temp); $response = Utils\http_request('GET', $download_url, null, $headers, $options); if (404 == $response->status_code) { WP_CLI::error("Release not found. Double-check locale or version."); } else { if (20 != 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 != 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.'); } try { Extractor::extract($temp, $download_dir); } catch (Exception $e) { WP_CLI::error("Couldn't extract WordPress archive. " . $e->getMessage()); } if ('nightly' !== $version) { $cache->import($cache_key, $temp); } unlink($temp); } if ($wordpress_present) { $this->cleanup_extra_files($from_version, $version, $locale); } WP_CLI::success('WordPress downloaded.'); }