Skip to content

Commit

Permalink
Deploying version 2.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmjones committed Sep 8, 2020
1 parent f390776 commit ec4361c
Show file tree
Hide file tree
Showing 36 changed files with 220 additions and 1,343 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
**Requires at least:** 4.9
**Tested up to:** 5.5
**Requires PHP:** 5.5
**Stable tag:** 2.4.3
**Stable tag:** 2.4.4
**License:** GPLv3

Copies files to Amazon S3, DigitalOcean Spaces or Google Cloud Storage as they are uploaded to the Media Library. Optionally configure Amazon CloudFront or another CDN for faster delivery.
Expand Down Expand Up @@ -89,6 +89,14 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### WP Offload Media Lite 2.4.4 - 2020-09-08 ###
* Improvement: Updated AWS PHP SDK to v3.151.6
* Bug fix: Files for duplicate thumbnail sizes not removed from server after initial offload
* Bug fix: PHP Fatal error: Class 'DeliciousBrains\WP_Offload_Media\Aws3\Symfony\Polyfill\Intl\Idn\Idn' not found
* Bug fix: PHP Recoverable fatal error: Object of class WP_Error could not be converted to string in .../wp-includes/post.php on line 504
* Bug fix: PHP message: PHP Warning: is_readable(): open_basedir restriction in effect
* Bug fix: URLs not rewritten for RSS feed enclosures

### WP Offload Media Lite 2.4.3 - 2020-09-01 ###
* Improvement: Updated AWS PHP SDK to v3.151.3
* Bug fix: PHP Fatal error: Class 'DeliciousBrains\WP_Offload_Media\Aws3\Symfony\Polyfill\Intl\Idn\Idn' not found
Expand Down
91 changes: 69 additions & 22 deletions classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,9 @@ function remove_attachment_files_from_provider( $post_id, Media_Library_Item $as
}

$objects_to_remove = array();
$paths_to_remove = array_unique( $paths );

foreach ( $paths as $size => $path ) {
foreach ( $paths_to_remove as $size => $path ) {
$objects_to_remove[] = array(
'Key' => $as3cf_item->key( wp_basename( $path ) ),
);
Expand Down Expand Up @@ -1381,7 +1382,8 @@ function wp_update_attachment_metadata( $data, $post_id ) {
* @throws Exception
*/
public function upload_attachment( $post_id, $data = null, $file_path = null, $force_new_provider_client = false, $remove_local_files = true ) {
static $offloaded = array();
static $offloaded_path_filesizes = array();
static $offloaded_size_paths = array();

$return_metadata = null;
if ( is_null( $data ) ) {
Expand All @@ -1406,6 +1408,21 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f
return $data;
}

// If $file_path was passed in with a non-null value, ensure it's a string
if ( ! is_null( $file_path ) && ! is_string( $file_path ) ) {
$error_msg = sprintf( __( 'Media Library item ID %d. Provided path is not a string', 'amazon-s3-and-cloudfront' ), $post_id );

return $this->return_upload_error( $error_msg );
}

// Ensure WordPress own post meta for relative URL is a string
$attached_file_meta = get_post_meta( $post_id, '_wp_attached_file', true );
if ( ! is_string( $attached_file_meta ) ) {
$error_msg = sprintf( __( 'Media Library item with ID %d has damaged meta data', 'amazon-s3-and-cloudfront' ), $post_id );

return $this->return_upload_error( $error_msg );
}

if ( is_null( $file_path ) ) {
$file_path = get_attached_file( $post_id, true );
}
Expand Down Expand Up @@ -1447,8 +1464,9 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f

// If original offloaded in same process, skip offloading anything it's already processed.
// Otherwise, do not need to offload full file if duplicate and file missing.
if ( ! empty( $offloaded[ $duplicate_item->id() ] ) ) {
$offloaded[ $old_item->id() ] = $offloaded[ $duplicate_item->id() ];
if ( ! empty( $offloaded_path_filesizes[ $duplicate_item->id() ] ) ) {
$offloaded_path_filesizes[ $old_item->id() ] = $offloaded_path_filesizes[ $duplicate_item->id() ];
$offloaded_size_paths[ $old_item->id() ] = $offloaded_size_paths[ $duplicate_item->id() ];
} elseif ( ! file_exists( $file_path ) ) {
$offload_full = false;
}
Expand All @@ -1459,7 +1477,7 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f

// If not already offloaded in request, check full file exists locally before attempting offload.
if ( $offload_full ) {
if ( $old_item && ! empty( $offloaded[ $old_item->id() ][ $file_path ] ) ) {
if ( $old_item && ! empty( $offloaded_path_filesizes[ $old_item->id() ][ $file_path ] ) ) {
$offload_full = false;
} elseif ( ! file_exists( $file_path ) ) {
$error_msg = sprintf( __( 'File %s does not exist', 'amazon-s3-and-cloudfront' ), $file_path );
Expand All @@ -1474,13 +1492,12 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f
// Are there any files not already offloaded if full already offloaded in this request?
if ( false === $offload_full ) {
if ( empty( $file_paths ) ) {
// Item does not have any additional files, we're done.
return $return_metadata;
}

$offloaded_file_paths = empty( $offloaded[ $old_item->id() ] ) ? array() : $offloaded[ $old_item->id() ];
unset( $offloaded_file_paths[ $file_path ] );

if ( ! empty( $offloaded_file_paths ) && empty( array_diff( $file_paths, array_keys( $offloaded_file_paths ) ) ) ) {
if ( ! empty( $offloaded_size_paths[ $old_item->id() ] ) && empty( array_diff_key( $file_paths, $offloaded_size_paths[ $old_item->id() ] ) ) ) {
// Item's additional files all offloaded, we're done.
return $return_metadata;
}
}
Expand Down Expand Up @@ -1545,7 +1562,7 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f

$acl = apply_filters( 'wps3_upload_acl', $acl, $type, $data, $post_id, $this ); // Old naming convention, will be deprecated soon
$acl = apply_filters( 'as3cf_upload_acl', $acl, $data, $post_id );
$is_private = ( ! empty( $acl ) && $this->get_storage_provider()->get_private_acl() === $acl ) ? true : false;
$is_private = ! empty( $acl ) && $this->get_storage_provider()->get_private_acl() === $acl;

$args = array(
'Bucket' => $bucket,
Expand Down Expand Up @@ -1593,8 +1610,9 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f

do_action( 'as3cf_upload_attachment_pre_remove', $post_id, $as3cf_item, $as3cf_item->normalized_path_dir(), $args );

$new_offloads = array();
$files_to_remove = array();
$new_offload_path_filesizes = array();
$new_offload_size_paths = array();
$files_to_remove = array();

$provider_client = $this->get_provider_client( $as3cf_item->region(), $force_new_provider_client );

Expand All @@ -1606,8 +1624,9 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f
// May raise exception, so don't offload anything else if there's an error.
$provider_client->upload_object( $args );

$new_offloads[ $file_path ] = $filesize; // Note: pre `as3cf_object_meta` filter value.
$files_to_remove[] = $file_path; // Note: pre `as3cf_object_meta` filter value.
$new_offload_path_filesizes[ $file_path ] = $filesize; // Note: pre `as3cf_object_meta` filter value.
$new_offload_size_paths['original'] = $file_path;
$files_to_remove[] = $file_path; // Note: pre `as3cf_object_meta` filter value.
} catch ( Exception $e ) {
$error_msg = sprintf( __( 'Error offloading %s to provider: %s', 'amazon-s3-and-cloudfront' ), $file_path, $e->getMessage() );

Expand Down Expand Up @@ -1637,14 +1656,39 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f
if ( ! empty( $acl ) && $this->use_acl_for_intermediate_size( $post_id, $size, $bucket, $as3cf_item ) ) {
$additional_images[ $size ]['ACL'] = $acl;
}
} else {
// If the previous offload of file path was private, this size also needs to be private.
// This is a case of first (in process) offload wins, duplicate file paths should have same access.
if ( ! empty( $private_sizes ) ) {
$duplicate_private = array_intersect( $private_sizes, array_keys( array_intersect( $file_paths, array( $file_path ) ) ) );

if ( ! empty( $duplicate_private ) ) {
$private_sizes[] = $size;
}
}
}
}

$upload_errors = array();

foreach ( $additional_images as $size => $image ) {
// If this file has already been offloaded during this request, skip actual offload.
if ( $old_item && ! empty( $offloaded[ $old_item->id() ][ $image['SourceFile'] ] ) ) {
if ( $old_item && ! empty( $offloaded_path_filesizes[ $old_item->id() ][ $image['SourceFile'] ] ) ) {
// We still have to record whether this size is private based on previous offload of the source file.
// We also need to ensure this file is marked as possibly needing removal from server, and size has been processed.
if ( ! empty( $private_sizes ) ) {
$duplicate_private = array_intersect( $private_sizes, array_keys( array_intersect( $file_paths, array( $image['SourceFile'] ) ) ) );

if ( ! empty( $duplicate_private ) ) {
$private_sizes[] = $size;
}
}

// Processed file, but not this duplicate size, so file may have been re-created by WordPress.
if ( file_exists( $image['SourceFile'] ) ) {
$files_to_remove[] = $image['SourceFile'];
}
$new_offload_size_paths[ $size ] = $image['SourceFile'];
continue;
}

Expand All @@ -1669,10 +1713,11 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f
try {
// May raise exception, but for sizes we'll just log it and maybe try again later if called.
$provider_client->upload_object( $args );
$files_to_remove[] = $image['SourceFile']; // Note: pre `as3cf_object_meta` filter value.
$files_to_remove[] = $image['SourceFile']; // Note: pre `as3cf_object_meta` filter value.
$new_offload_size_paths[ $size ] = $image['SourceFile'];

// May raise exception, we'll log that, and carry on anyway.
$new_offloads[ $image['SourceFile'] ] = (int) filesize( $image['SourceFile'] ); // Note: pre `as3cf_object_meta` filter value.
$new_offload_path_filesizes[ $image['SourceFile'] ] = (int) filesize( $image['SourceFile'] ); // Note: pre `as3cf_object_meta` filter value.
} catch ( Exception $e ) {
$upload_errors[] = $this->return_upload_error( sprintf( __( 'Error offloading %s to provider: %s', 'amazon-s3-and-cloudfront' ), $args['SourceFile'], $e->getMessage() ) );
}
Expand Down Expand Up @@ -1705,8 +1750,8 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f
$files_to_remove = array_unique( $files_to_remove );

$filesize_total = 0;
if ( ! empty( $old_item ) && ! empty( $offloaded[ $old_item->id() ] ) ) {
$filesize_total = array_sum( $offloaded[ $old_item->id() ] );
if ( ! empty( $old_item ) && ! empty( $offloaded_path_filesizes[ $old_item->id() ] ) ) {
$filesize_total = array_sum( $offloaded_path_filesizes[ $old_item->id() ] );
}
// Delete the files and record original file's size before removal.
$this->remove_local_files( $files_to_remove, $post_id, $filesize_total );
Expand Down Expand Up @@ -1750,10 +1795,12 @@ public function upload_attachment( $post_id, $data = null, $file_path = null, $f
$as3cf_item->save();

// Keep track of individual files offloaded during this request.
if ( empty( $offloaded[ $as3cf_item->id() ] ) ) {
$offloaded[ $as3cf_item->id() ] = $new_offloads;
if ( empty( $offloaded_path_filesizes[ $as3cf_item->id() ] ) ) {
$offloaded_path_filesizes[ $as3cf_item->id() ] = $new_offload_path_filesizes;
$offloaded_size_paths[ $as3cf_item->id() ] = $new_offload_size_paths;
} else {
$offloaded[ $as3cf_item->id() ] += $new_offloads;
$offloaded_path_filesizes[ $as3cf_item->id() ] += $new_offload_path_filesizes;
$offloaded_size_paths[ $as3cf_item->id() ] += $new_offload_size_paths;
}

// Keep track of attachments uploaded by this instance.
Expand Down
4 changes: 1 addition & 3 deletions classes/as3cf-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@ public static function get_attachment_file_paths( $attachment_id, $exists_locall
// Allow other processes to add files to be uploaded
$paths = apply_filters( 'as3cf_attachment_file_paths', $paths, $attachment_id, $meta );

// Remove duplicates
$paths = array_unique( $paths );

// Remove paths that don't exist
if ( $exists_locally ) {
foreach ( $paths as $key => $path ) {
Expand Down Expand Up @@ -310,6 +307,7 @@ public static function get_attachment_edited_keys( $attachment_id, Media_Library

/**
* Get intermediate size from attachment filename.
* If multiple sizes exist with same filename, only the first size found will be returned.
*
* @param int $attachment_id
* @param string $filename
Expand Down
1 change: 1 addition & 0 deletions classes/filters/as3cf-local-to-s3.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function init() {
add_filter( 'content_pagination', array( $this, 'filter_content_pagination' ) );
add_filter( 'the_content', array( $this, 'filter_post' ), 100 );
add_filter( 'the_excerpt', array( $this, 'filter_post' ), 100 );
add_filter( 'rss_enclosure', array( $this, 'filter_post' ), 100 );
add_filter( 'content_edit_pre', array( $this, 'filter_post' ) );
add_filter( 'excerpt_edit_pre', array( $this, 'filter_post' ) );
add_filter( 'as3cf_filter_post_local_to_s3', array( $this, 'filter_post' ) ); // Backwards compatibility
Expand Down
5 changes: 5 additions & 0 deletions classes/providers/storage/aws-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ public function __construct( \AS3CF_Plugin_Base $as3cf ) {

// Autoloader.
require_once $as3cf->get_plugin_sdks_dir_path() . '/Aws3/aws-autoloader.php';

if ( ! function_exists( 'idn_to_ascii' ) && ! defined( 'IDNA_DEFAULT' ) ) {
define( 'IDNA_DEFAULT', 0 );
}
}

/**
Expand Down Expand Up @@ -226,6 +230,7 @@ protected function default_client_args() {
's3_us_east_1_regional_endpoint' => apply_filters( 'as3cf_aws_s3_us_east_1_regional_endpoint', 'legacy' ),
'endpoint_discovery' => apply_filters( 'as3cf_disable_aws_endpoint_discovery', true ) ? array( 'enabled' => false ) : array( 'enabled' => true ),
'sts_regional_endpoints' => apply_filters( 'as3cf_aws_sts_regional_endpoints', 'legacy' ),
'use_aws_shared_config_files' => apply_filters( 'as3cf_aws_use_shared_config_files', false ),
);
}

Expand Down
Loading

0 comments on commit ec4361c

Please sign in to comment.