Skip to content

Instantly share code, notes, and snippets.

@markoheijnen
Created April 16, 2012 16:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markoheijnen/2399898 to your computer and use it in GitHub Desktop.
Save markoheijnen/2399898 to your computer and use it in GitHub Desktop.
Updating CDN when it is activated and the site using W3 Total cache
<?php
$cdn_activated = false;
if( function_exists( 'w3_instance' ) ) {
$cdn_activated = w3_instance('W3_Config')->get_boolean( 'cdn.enabled' );
@set_time_limit( w3_instance('W3_Config')->get_integer('timelimit.cdn_import') );
}
$upload_dir = wp_upload_dir();
$file = 'test/somefile.png';
$new_location = $upload_dir[ 'baseurl' ] . $file;
if( $cdn_activated ) {
$cdn_type = w3_instance('W3_Config')->get_string( 'cdn.engine' );
$cdn_object = w3_instance('W3_Plugin_CdnCommon');
$results_cdn = array();
$cdn_object->upload( get_files_for_upload( $file ), false, $results_cdn );
if( isset( $results_cdn[0], $results_cdn[0]['remote_path'] ) && ! empty( $results_cdn[0]['remote_path'] ) && empty( $results_cdn[0]['error'] ) ) {
$new_location = $results_cdn[0]['remote_path'];
}
}
function get_files_for_upload( $file ) {
$files = array();
$upload_dir = wp_upload_dir();
$parse_url = parse_url( $upload_dir['baseurl'] );
if( $parse_url ) {
$baseurlpath = ( ! empty( $parse_url['path'] ) ? trim( $parse_url['path'], '/' ) : '' );
} else {
$baseurlpath = 'wp-content/uploads';
}
$upload_dir['baseurlpath'] = '/' . $baseurlpath . '/';
$local_file = $upload_dir['basedir'] . $file;
$remote_file = ltrim( $upload_dir['baseurlpath'] . $file, '/' );
$files[ $local_file ] = $remote_file;
return $files;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment