WP_CLI::get_cache PHP Method

get_cache() public static method

public static get_cache ( ) : WP_CLI\FileCache
return WP_CLI\FileCache
    public static function get_cache()
    {
        static $cache;
        if (!$cache) {
            $home = getenv('HOME');
            if (!$home) {
                // sometime in windows $HOME is not defined
                $home = getenv('HOMEDRIVE') . getenv('HOMEPATH');
            }
            $dir = getenv('WP_CLI_CACHE_DIR') ?: "{$home}/.wp-cli/cache";
            // 6 months, 300mb
            $cache = new FileCache($dir, 15552000, 314572800);
            // clean older files on shutdown with 1/50 probability
            if (0 === mt_rand(0, 50)) {
                register_shutdown_function(function () use($cache) {
                    $cache->clean();
                });
            }
        }
        return $cache;
    }

Usage Example

Esempio n. 1
0
File: core.php Progetto: nb/wp-cli
 /**
  * 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 --version=3.3
  *
  * @when before_wp_load
  */
 public function download($args, $assoc_args)
 {
     if (!isset($assoc_args['force']) && is_readable(ABSPATH . 'wp-load.php')) {
         WP_CLI::error('WordPress files seem to already be present here.');
     }
     if (!is_dir(ABSPATH)) {
         WP_CLI::log(sprintf('Creating directory %s', ABSPATH));
         WP_CLI::launch(Utils\esc_cmd('mkdir -p %s', ABSPATH));
     }
     $locale = isset($assoc_args['locale']) ? $assoc_args['locale'] : 'en_US';
     if (isset($assoc_args['version'])) {
         $version = $assoc_args['version'];
         if ('en_US' === $locale) {
             $download_url = 'https://wordpress.org/wordpress-' . $version . '.tar.gz';
         } else {
             $download_url = sprintf('https://%s.wordpress.org/wordpress-%s-%s.tar.gz', substr($assoc_args['locale'], 0, 2), $version, $locale);
         }
     } else {
         $offer = $this->get_download_offer($locale);
         $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/{$locale}-{$version}.tar.gz";
     $cache_file = $cache->has($cache_key);
     if ($cache_file) {
         WP_CLI::log("Using cached file '{$cache_file}'...");
         self::_extract($cache_file, ABSPATH);
     } else {
         // We need to use a temporary file because piping from cURL to tar is flaky
         // on MinGW (and probably in other environments too).
         $temp = sys_get_temp_dir() . '/' . uniqid('wp_') . '.tar.gz';
         $headers = array('Accept' => 'application/json');
         $options = array('timeout' => 600, 'filename' => $temp);
         self::_request('GET', $download_url, $headers, $options);
         self::_extract($temp, ABSPATH);
         $cache->import($cache_key, $temp);
         unlink($temp);
     }
     WP_CLI::success('WordPress downloaded.');
 }
All Usage Examples Of WP_CLI::get_cache