WordPress.org

Ready to get started?Download WordPress

Forums

W3 Total Cache
[resolved] Trigger upload image to CDN? (6 posts)

  1. isay
    Member
    Posted 1 year ago #

    Hi, great plugin!
    I'm using the w3-total-cache plugin to upload and store images in CDN. I have a network setup where another plugin ThreeWP Broadcast distributes posts and attachment images from the master site to the children. The setup works nearly perfectly, but I need to do some kind of manual hack to go all the way..

    The problem, I believe, is that the ThreeWP Broadcast plugin do a copy of attached images from master site to child but without triggering the w3-total-cache CDN upload. Thus images and attachments end up on the child site's local server but not in CDN (ie. broken image links on front page ).

    What I'm thinking is to add some code in ThreeWP Broadcast to force a trigger of the upload-to-CDN-hook...

    ThreeBroadcast code snippet (copy of attachments):

    private function copy_attachment( $attachment_data, $post_id) {
    // Copy the file to the blog's upload directory
    		$upload_dir = wp_upload_dir();
    
    		if ( ! file_exists( $attachment_data->filename_path() ) )
    			return false;
    		copy( $attachment_data->filename_path(), $upload_dir['path'] . '/' . $attachment_data->filename_base() );
    
    		// And now create the attachment stuff.
    		// This is taken almost directly from http://codex.wordpress.org/Function_Reference/wp_insert_attachment
    		$wp_filetype = wp_check_filetype( $attachment_data->filename_base(), null );
    		$attachment = array(
    			'post_mime_type' => $wp_filetype['type'],
    			'post_title' => preg_replace( '/\.[^.]+$/', '', $attachment_data->filename_base() ),
    			'post_content' => '',
    			'post_status' => 'inherit'
    		);
    		$attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $attachment_data->filename_base(), $post_id );
    
    		// Now to handle the metadata.
    		// 1. Create new metadata for this attachment.
    
    		require_once(ABSPATH . "wp-admin" . '/includes/image.php' );
    		$attach_data = wp_generate_attachment_metadata( $attach_id, $upload_dir['path'] . '/' . $attachment_data->filename_base() );
    
    		// 2. Write the old metadata first.
    		foreach( $attachment_data->post_custom as $key => $value )
    		{
    			$value = reset( $value );
    			$value = maybe_unserialize( $value );
    			switch( $key )
    			{
    				// Some values need to handle completely different upload paths (from different months, for example).
    				case '_wp_attached_file':
    					$value = $attach_data[ 'file' ];
    					break;
    			}
    			update_post_meta( $attach_id, $key, $value );
    		}
    
    		// 3. Overwrite the metadata that needs to be overwritten with fresh data.
    		wp_update_attachment_metadata( $attach_id,  $attach_data );
    
    return $attach_id;
    }

    I have tried to add an extra update_attachement_file hoping it would trigger the hook...

    update_attached_file($attach_id, $upload_dir['path'] . '/' . $attachment_data->filename_base());

    .. but it fails.

    Any hints on how to do this?

    Best regards,
    Mike

    http://wordpress.org/extend/plugins/w3-total-cache/

  2. isay
    Member
    Posted 1 year ago #

    How can I check if the original thumbnail is uploaded to CDN?

  3. mudandroutes
    Member
    Posted 1 year ago #

    I'm after the same sort of feature. I've got the posts and files broadcasting to the main site, and the attachments are all there, but thumbnails aren't being upped to amazon automatically resulting in a right mess on the site. :-(

    I was thinking if there was a way of reverting to local files when the cdn files are unavailable, and then exporting to CDN every few weeks or month depending on traffic it would be some sort of workaround that might be easier to implement???

    Hopefully someone has a suggestion...

  4. isay
    Member
    Posted 1 year ago #

    I got this to work finally! I made a "manual" forced upload of the attachment images...

    This is the code I added to the private function copy_attachment( $attachment_data, $post_id) function located in ThreeWP_Broadcast.php near the end just after wp_update_attachment_metadata( $attach_id, $attach_data ) and before the last row return $attach_id:

    /*
    			Start add by MS
    			Upload images to Amazon CDN
    		*/
    
    		if ( defined( 'W3TC_LIB_W3_DIR' ) ) {
    			require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
    			$w3_plugin_cdn =& w3_instance('W3_Plugin_CdnCommon');
    			$upload_info = w3_upload_info();
    
    			$local_file = $upload_dir['path'] . '/' . $attachment_data->filename_base();
    			$remote_file = ltrim($upload_dir['path'] . '/' . $attachment_data->filename_base(), '/');
    
    			// Remove "wp-content/blogs.dir/5/" from the image path in remote file
    			$remove_prefix_to_path = ltrim($upload_dir['basedir'], '/');
    			$remove_prefix_to_path = str_replace ( 'files', '' , $remove_prefix_to_path);
    			$remove_prefix_to_path = str_replace ( 'uploads', '' , $remove_prefix_to_path);
    			$remote_file = str_replace ( $remove_prefix_to_path, '' , $remote_file);
    
    			$all_image_sizes = get_intermediate_image_sizes();
    			if ($w3_plugin_cdn) {
    
    				// Add original size image
    				$files = array(
    					// local_file1	=> remote_file1,
    					$local_file 		=> $remote_file,
    					);
    
    				// Add all other sizes of images
    				$i = 0;
    				$all_files = $attachment_data->file_metadata();
    				foreach ($all_image_sizes as $size) {
    					$i++;
    					//echo '<p>File '.$i.': '.$all_files["sizes"][$size]["file"].'</p>';
    					if (count($all_files["sizes"][$size]["file"]) > 0)
    						$files[
    							str_replace (
    								$attachment_data->filename_base() ,
    								$all_files["sizes"][$size]["file"] ,
    								$local_file )
    						] = str_replace (
    										$attachment_data->filename_base() ,
    										$all_files["sizes"][$size]["file"] ,
    										$remote_file );
    
    				}
    
    		    $results = array();
    		    $w3_plugin_cdn->upload($files, false, $results);
    		    $response = array(
    		        'results' => $results
    		    );
    				/* //DEBUG
    				if ($post_id == 1374) {
    					echo '<p>response = ';
    					print_r($response);
    					echo '</p>';
    
    					echo '<p>files = ';
    					print_r($files);
    					echo '</p>';			
    
    					echo '<p>w3_plugin_cdn = ';
    					print_r($w3_plugin_cdn);
    					echo '</p>';
    
    				}
    				*/
    			}
    		}
    		/*
    			End add by MS
    		*/
  5. DFC005
    Member
    Posted 1 year ago #

    Sorry to drag up an old thread but have just tried the above code and it doesn't seem to work. I assume the plugins have both probably had a few updates since this was done.

    Any chance of some updated code?

  6. Frederick Townes
    Member
    Plugin Author

    Posted 1 year ago #

    Testing the ThreeWP plugin there are no media added to subsites libraries when broadcasting posts. IMHO, the question is if this is a W3TC issue or ThreeWP Broadcast issue.

    W3TC supports wp_insert_attachment etc so there is perhaps something with ThreeWP usage that results in a conflict. GIven that other CDN plugins have issue as well, it might be issue best handled by ThreeWP Broadcast.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic