Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Created March 31, 2016 18:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielbachhuber/6f579fdd55a5065828743dbfd87de58f to your computer and use it in GitHub Desktop.
Save danielbachhuber/6f579fdd55a5065828743dbfd87de58f to your computer and use it in GitHub Desktop.
During `wp import`, cache remote files locally for subsequent imports
<?php
/**
* Run with `wp --require=import-cache.php import ...
*/
WP_CLI::add_hook( 'after_wp_load', function(){
// Only intercept HTTP requests when the importer is running
if ( ! defined( 'WP_IMPORTING') || ! WP_IMPORTING ) {
return;
}
$get_cache = function() {
// @todo this should maybe be configurable in some way
$dir = dirname( __FILE__ ) . '/import-cache/';
if ( ! is_dir( $dir ) ) {
@mkdir( $dir );
}
// @todo File cache defaults to 6 months, 300 MB. Maybe this should be configurable?
return new WP_CLI\FileCache( $dir, 15552000, 314572800 );
};
add_filter( 'pre_http_request', function( $retval, $r, $url ) use ( $get_cache ) {
$key = md5( $url );
$cache = $get_cache();
if ( $cache->has( $key ) ){
WP_CLI::log( " - Importing {$url} from cache..." );
$body = $cache->read( $key );
return array(
// @todo probably want to return some semi-accurate header
'headers' => array(),
'body' => $body,
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => null,
);
}
return $retval;
}, 10, 3 );
// @todo validate against expected content types
add_filter( 'http_response', function( $response, $args, $url ) use( $get_cache ) {
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return $response;
}
WP_CLI::log( " - Saving {$url} to cache..." );
$get_cache()->write( md5( $url ), wp_remote_retrieve_body( $response ) );
return $response;
}, 10, 3 );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment