-
-
Save jmazzi/49825de3ee8d6f7c8b609958683baadf to your computer and use it in GitHub Desktop.
During `wp import`, cache remote files locally for subsequent imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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