Support » Plugin: W3 Total Cache » Is there a way to force uploading assets to CDN through CLI?

  • Resolved Borja

    (@piesblancos)


    Hello!

    First of all thanks for the awesome job you’ve done with this plugin.

    We would like to know if there’s any way to force some of the assets to be uploaded to the CDN via WP-CLI or any other non-visual way. We can’t access the dashboard so all operations are done through command line and it would be great to be able to push them from there.

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Marko Vasiljevic

    (@vmarko)

    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

    Borja

    (@piesblancos)

    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

    • This reply was modified 3 months ago by  Borja.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.