Hello @piesblancos,
You can use wp w3-total-cache cdn-purge
.
Something like wp w3-total-cache cdn-purge wp-content/uploads/my-file1.jpg
wp-content/uploads/my-file2.jpg
Thank you @vmarko.
The truth is that way would work if we just wanted to purge a few files, but this would be like the initial upload of all assets. So finally I created my own command based on your code.
/**
* Upload assets to CDN
*
* @param array $args
*/
function export_cdn_assets( $args = array() ) {
try {
$w3_plugin_cdn = Dispatcher::component( 'Cdn_Plugin' );
$common = Dispatcher::component( 'Cdn_Core' );
if( $args ) {
switch( $args[0] ) {
case 'includes':
$files = $w3_plugin_cdn->get_files_includes();
break;
case 'theme':
$files = $w3_plugin_cdn->get_files_theme();
break;
case 'minify':
$files = $w3_plugin_cdn->get_files_minify();
break;
default:
case 'custom':
$files = $w3_plugin_cdn->get_files_custom();
break;
}
} else {
\WP_CLI::log( 'Please select one of the groups of assets to upload: \'includes\', \'theme\', \'minify\' or \'custom\' ');
exit;
}
$upload = array();
$results = array();
foreach ( $files as $file ) {
$local_path = $common->docroot_filename_to_absolute_path( $file );
$remote_path = $common->uri_to_cdn_uri( $common->docroot_filename_to_uri( $file ) );
$d = $common->build_file_descriptor( $local_path, $remote_path );
$d['_original_id'] = $file;
$upload[] = $d;
}
$common->upload( $upload, false, $results, time() + 5 );
foreach ($results as $result) {
\WP_CLI::log( sprintf( 'File: %s => %s', $result['local_path'], $result['error'] ) );
}
} catch( \Exception $e ) {
\WP_CLI::error( __( 'Export CDN assets did failed: %s',
'w3-total-cache' ), $e );
}
}
It’s the first one that I’ve done so it might have some errors, but it’s working so far.
Now with wp w3-total-cache export_cdn_assets theme, would upload all the theme files as its done through the dashboard.
Thank you