Skip to content

Commit

Permalink
Deploying version 0.9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
polevaultweb committed Sep 1, 2015
1 parent 6e4239f commit 1090fa2
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 48 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**Tags:** uploads, amazon, s3, mirror, admin, media, cdn, cloudfront
**Requires at least:** 3.7
**Tested up to:** 4.3
**Stable tag:** 0.9.4
**Stable tag:** 0.9.5
**License:** GPLv3

Copies files to Amazon S3 as they are uploaded to the Media Library. Optionally configure Amazon CloudFront for faster delivery.
Expand Down Expand Up @@ -67,6 +67,9 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### 0.9.5 - 2015-09-01 ###
* Bug fix: Fatal error: Cannot use object of type WP_Error as array

### 0.9.4 - 2015-08-27 ###
* New: Update all existing attachments with missing file sizes when the 'Remove Files From Server' option is enabled (automatically runs in the background)
* Improvement: Show when constants are used to set bucket and region options
Expand Down
24 changes: 6 additions & 18 deletions classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function init( $plugin_file_path ) {

new AS3CF_Upgrade_Region_Meta( $this );
new AS3CF_Upgrade_File_Sizes( $this );
new AS3CF_Upgrade_Meta_WP_Error( $this );

add_action( 'aws_admin_menu', array( $this, 'admin_menu' ) );
add_action( 'wp_ajax_as3cf-get-buckets', array( $this, 'ajax_get_buckets' ) );
Expand Down Expand Up @@ -489,7 +490,7 @@ function wp_update_attachment_metadata( $data, $post_id ) {
}

// upload attachment to S3
$data = $this->upload_attachment_to_s3( $post_id, $data );
$this->upload_attachment_to_s3( $post_id, $data );

return $data;
}
Expand All @@ -507,9 +508,7 @@ function wp_update_attachment_metadata( $data, $post_id ) {
* @return array|WP_Error $s3object|$meta If meta is supplied, return it. Else return S3 meta
*/
function upload_attachment_to_s3( $post_id, $data = null, $file_path = null, $force_new_s3_client = false, $remove_local_files = true ) {
$return_metadata = true;
if ( is_null( $data ) ) {
$return_metadata = false;
$data = wp_get_attachment_metadata( $post_id, true );
}

Expand Down Expand Up @@ -628,11 +627,8 @@ function upload_attachment_to_s3( $post_id, $data = null, $file_path = null, $fo
// Store in the attachment meta data for use by WP
$data['filesize'] = $bytes;

if ( ! $return_metadata ) {
// Upload happening outside of 'wp_update_attachment_metadata' filter,
// So update metadata manually
update_post_meta( $post_id, '_wp_attachment_metadata', $data );
}
// Update metadata with filesize
update_post_meta( $post_id, '_wp_attachment_metadata', $data );

// Add to the file size total
$filesize_total += $bytes;
Expand Down Expand Up @@ -692,21 +688,13 @@ function upload_attachment_to_s3( $post_id, $data = null, $file_path = null, $fo
// Make sure we don't have a cached file sizes in the meta
unset( $data['filesize'] );

if ( ! $return_metadata ) {
// Upload happening outside of 'wp_update_attachment_metadata' filter,
// So update metadata manually
update_post_meta( $post_id, '_wp_attachment_metadata', $data );
}
// Remove the filesize from the metadata
update_post_meta( $post_id, '_wp_attachment_metadata', $data );

delete_post_meta( $post_id, 'wpos3_filesize_total' );
}
}

if ( $return_metadata ) {
// If the attachment metadata is supplied, return it
return $data;
}

return $s3object;
}

Expand Down
156 changes: 156 additions & 0 deletions classes/upgrades/as3cf-meta-wp-error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Update Broken _wp_attachment_metadata introduced in 0.9.4
*
* @package amazon-s3-and-cloudfront
* @subpackage Classes/Upgrades/Meta-WP-Error
* @copyright Copyright (c) 2015, Delicious Brains
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 0.9.5
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* AS3CF_Upgrade_Meta_WP_Error Class
*
* This class handles updating the _wp_attachment_metadata
* for attachments that have been removed from the local server
* and have had it corrupted by another plugin
*
* @since 0.9.5
*/
class AS3CF_Upgrade_Meta_WP_Error extends AS3CF_Upgrade {

/**
* Initiate the upgrade
*
* @param object $as3cf Instance of calling class
*/
public function __construct( $as3cf ) {
$this->upgrade_id = 3;
$this->upgrade_name = 'meta_error';
$this->upgrade_type = 'attachments';

$this->running_update_text = __( 'and rebuilding the metadata for attachments that may have been corrupted.', 'as3cf' );

parent::__construct( $as3cf );
}

/**
* Rebuild the attachment metadata for an attachment
*
* @param $attachment
*
* @return bool
*/
function upgrade_attachment( $attachment ) {
$s3object = unserialize( $attachment->s3object );
if ( false === $s3object ) {
error_log( 'Failed to unserialize S3 meta for attachment ' . $attachment->ID . ': ' . $attachment->s3object );
$this->error_count++;

return false;
}

$file = get_attached_file( $attachment->ID, true );

if ( ! file_exists( $file ) ) {
// Copy back the file to the server if doesn't exist so we can successfully
// regenerate the attachment metadata
try {
$args = array(
'Bucket' => $s3object['bucket'],
'Key' => $s3object['key'],
'SaveAs' => $file,
);
$this->as3cf->get_s3client( $s3object['region'], true )->getObject( $args );
} catch ( Exception $e ) {
error_log( sprintf( __( 'There was an error attempting to download the file %s from S3: %s', 'as3cf' ), $s3object['key'], $e->getMessage() ) );

return false;
}
}

// Remove corrupted meta
delete_post_meta( $attachment->ID, '_wp_attachment_metadata' );

require_once ABSPATH . '/wp-admin/includes/image.php';
// Generate new attachment meta
wp_update_attachment_metadata( $attachment->ID, wp_generate_attachment_metadata( $attachment->ID, $file ) );

return true;
}

/**
* Get a count of all attachments without region in their S3 metadata
* for the whole site
*
* @return int
*/
function count_attachments_to_process() {
// get the table prefixes for all the blogs
$table_prefixes = $this->as3cf->get_all_blog_table_prefixes();
$all_count = 0;

foreach ( $table_prefixes as $blog_id => $table_prefix ) {
$count = $this->get_attachments_with_error_metadata( $table_prefix, true );
$all_count += $count;
}

return $all_count;
}

/**
* Get all attachments that don't have region in their S3 meta data for a blog
*
* @param string $prefix
* @param int $limit
*
* @return mixed
*/
function get_attachments_to_process( $prefix, $limit ) {
$attachments = $this->get_attachments_with_error_metadata( $prefix, false, $limit );

return $attachments;
}

/**
* Get S3 attachments that have had their _wp_attachment_metadata corrupted
*
* @param string $prefix
* @param bool|false $count
* @param null|int $limit
*
* @return array|int
*/
function get_attachments_with_error_metadata( $prefix, $count = false, $limit = null ) {
global $wpdb;

$sql = "FROM `{$prefix}postmeta` pm1
LEFT OUTER JOIN `{$prefix}postmeta` pm2
ON pm1.`post_id` = pm2.`post_id`
AND pm2.`meta_key` = '_wp_attachment_metadata'
WHERE pm1.`meta_key` = 'amazonS3_info'
AND pm2.`meta_value` like '%%WP_Error%%'";

if ( $count ) {
$sql = 'SELECT COUNT(*)' . $sql;

return $wpdb->get_var( $sql );
}

$sql = "SELECT pm1.`post_id` as `ID`, pm1.`meta_value` AS 's3object'" . $sql;

if ( ! is_null( $limit ) ) {
$sql .= ' LIMIT %d';

$sql = $wpdb->prepare( $sql, $limit );
}

return $wpdb->get_results( $sql, OBJECT );
}
}
Loading

0 comments on commit 1090fa2

Please sign in to comment.