Skip to content

Instantly share code, notes, and snippets.

@joshbeckman
Created March 19, 2014 22:09
Show Gist options
  • Save joshbeckman/9652508 to your computer and use it in GitHub Desktop.
Save joshbeckman/9652508 to your computer and use it in GitHub Desktop.
ThreeWP Broadcast support for Amazon S3-hosted images
/** ...Continued from within file */
/**
@brief Creates a new attachment.
@details
The $o object is an extension of Broadcasting_Data and must contain:
- @i attachment_data An attachment_data object containing the attachmend info.
@param object $o Options.
@return @i int The attachment's new post ID.
@since 20130530
@version 20131003
*/
public function copy_attachment( $o )
{
//File won't exist on disk for Amazon S3 for WordPress with CloudFront uploads
$AWS_uploaded = isset($o->attachment_data->post_custom['amazonS3_info']);
if ( !$AWS_uploaded ) {
$this->debug('Not in AWS');
if ( ! file_exists( $o->attachment_data->filename_path ) )
return false;
// Copy the file to the blog's upload directory
$upload_dir = wp_upload_dir();
copy( $o->attachment_data->filename_path, $upload_dir[ 'path' ] . '/' . $o->attachment_data->filename_base );
} else {
$this->debug('Uploaded to AWS');
}
// 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( $o->attachment_data->filename_base, null );
$attachment = [
'guid' => $upload_dir[ 'url' ] . '/' . $o->attachment_data->filename_base,
'menu_order' => $o->attachment_data->post->menu_order,
'post_author' => $o->attachment_data->post->post_author,
'post_excerpt' => $o->attachment_data->post->post_excerpt,
'post_mime_type' => $wp_filetype[ 'type' ],
'post_title' => $o->attachment_data->post->post_title,
'post_content' => '',
'post_status' => 'inherit',
];
//File won't exist on disk for Amazon S3 for WordPress with CloudFront uploads
if ( !$AWS_uploaded ) {
$this->debug('Inserting local attachment');
$o->attachment_id = wp_insert_attachment( $attachment, $upload_dir[ 'path' ] . '/' . $o->attachment_data->filename_base, $o->attachment_data->post->post_parent );
} else {
$this->debug('Inserting AWS attachment');
$s3_info = maybe_unserialize( reset($o->attachment_data->post_custom['amazonS3_info']) );
$o->attachment_id = wp_insert_attachment( $attachment, 'http://'.$s3_info['bucket'].'/'.$s3_info['key'], $o->attachment_data->post->post_parent );
// $o->attachment_id = wp_insert_attachment( $attachment, false, $o->attachment_data->post->post_parent );
}
$this->debug('Attachment ID: %s', $o->attachment_id);
// Now to maybe handle the metadata.
if ( $o->attachment_data->file_metadata )
{
// 1. Create new metadata for this attachment.
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $o->attachment_id, $upload_dir[ 'path' ] . '/' . $o->attachment_data->filename_base );
$this->debug('Generated attach_data: %s', serialize($attach_data));
$this->debug('Attachment inherited post_custom: %s', serialize($o->attachment_data->post_custom));
// 2. Write the old metadata first.
foreach( $o->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':
if (!$AWS_uploaded) {
$value = $attach_data[ 'file' ];
}
break;
}
update_post_meta( $o->attachment_id, $key, $value );
}
// 3. Overwrite the metadata that needs to be overwritten with fresh data.
if (!$AWS_uploaded) {
wp_update_attachment_metadata( $o->attachment_id, $attach_data );
}
}
}
/** ..Continued with rest of file */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment