/** * cache wp http api downloads * * @param array $response * @param array $args * @param string $url */ public function filter_http_response($response, $args, $url) { // check if whitelisted if (!isset($this->whitelist[$url])) { return $response; } // check if downloading if ('GET' !== $args['method'] || empty($args['filename'])) { return $response; } // check if download was successful if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { return $response; } // cache downloaded file $this->cache->import($this->whitelist[$url]['key'], $response['filename']); return $response; }
/** * @return 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; }