Skip to content

Commit

Permalink
Deploying version 2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmjones committed Apr 29, 2019
1 parent ff028e5 commit dc35394
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 202 deletions.
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# WP Offload Media Lite for Amazon S3 and DigitalOcean Spaces #
# WP Offload Media Lite for Amazon S3, DigitalOcean Spaces, and Google Cloud Storage #
**Contributors:** bradt, deliciousbrains, ianmjones
**Tags:** uploads, amazon, s3, amazon s3, digitalocean, digitalocean spaces, google cloud storage, gcs, mirror, admin, media, cdn, cloudfront
**Requires at least:** 4.7
**Tested up to:** 5.1
**Tested up to:** 5.2
**Requires PHP:** 5.5
**Stable tag:** 2.1
**Stable tag:** 2.2-dev
**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 @@ -61,16 +61,16 @@ If you upgrade to the pro version of [WP Offload Media](https://deliciousbrains.
## Screenshots ##

### 1. Select Cloud Storage Provider ###
![Select Cloud Storage Provider](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3-and-digitalocean-spaces/assets/screenshot-1.png)
![Select Cloud Storage Provider](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3,-digitalocean-spaces,-and-google-cloud-storage/assets/screenshot-1.png)

### 2. Select or Create Bucket ###
![Select or Create Bucket](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3-and-digitalocean-spaces/assets/screenshot-2.png)
![Select or Create Bucket](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3,-digitalocean-spaces,-and-google-cloud-storage/assets/screenshot-2.png)

### 3. Settings Screen ###
![Settings Screen](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3-and-digitalocean-spaces/assets/screenshot-3.png)
![Settings Screen](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3,-digitalocean-spaces,-and-google-cloud-storage/assets/screenshot-3.png)

### 4. Custom Domain Used With CDN ###
![Custom Domain Used With CDN](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3-and-digitalocean-spaces/assets/screenshot-4.png)
![Custom Domain Used With CDN](https://raw.githubusercontent.com/deliciousbrains/wp-wp-offload-media-lite-for-amazon-s3,-digitalocean-spaces,-and-google-cloud-storage/assets/screenshot-4.png)


## Upgrade Notice ##
Expand All @@ -86,6 +86,17 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### WP Offload Media Lite 2.1.1 - 2019-04-29 ###
* New: Multisite domain mapping via WordPress MU Domain Mapping plugin is now supported
* Improvement: Local to Provider content filtering performance improvements
* Improvement: Warning notice shown when changing storage provider and media already offloaded
* Bug fix: Media title not retaining characters stripped from filename
* Bug fix: Warning: is_readable(): open_basedir restriction in effect. File(~/.aws/config) is not within the allowed path(s)
* Bug fix: Fatal error when GCS Key File not accessible
* Bug fix: Non-image offloads on subsites with 4 digit IDs get duplicate subsite ID in bucket path
* Bug fix: No srcset added to img tag if filename includes non-ASCII characters
* Bug fix: Full size image URL saved to img tag src attribute when thumbnail picked if filename includes non-ASCII characters

### WP Offload Media Lite 2.1 - 2019-03-05 ###
* [Release Summary Blog Post](https://deliciousbrains.com/wp-offload-media-2-1-released/?utm_campaign=changelogs&utm_source=wordpress.org&utm_medium=free%2Bplugin%2Blisting)
* New: Google Cloud Storage is now supported
Expand Down
62 changes: 37 additions & 25 deletions classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ function init( $plugin_file_path ) {
add_filter( 'wp_video_shortcode', array( $this, 'wp_media_shortcode' ), 100, 5 );

// Communication with provider, plugin needs to be setup
add_filter( 'wp_handle_upload_prefilter', array( $this, 'wp_handle_upload_prefilter' ), 1 );
add_filter( 'wp_handle_sideload_prefilter', array( $this, 'wp_handle_upload_prefilter' ), 1 );
add_filter( 'wp_unique_filename', array( $this, 'wp_unique_filename' ), 10, 3 );
add_filter( 'wp_update_attachment_metadata', array( $this, 'wp_update_attachment_metadata' ), 110, 2 );
add_filter( 'delete_attachment', array( $this, 'delete_attachment' ), 20 );
add_filter( 'update_attached_file', array( $this, 'update_attached_file' ), 100, 2 );
Expand Down Expand Up @@ -1431,17 +1430,17 @@ function get_object_version_string() {
*
* @param string $url
*
* @return null|string
* @return null|string YYYY/MM format.
*/
function get_folder_time_from_url( $url ) {
if ( ! is_string( $url ) ) {
return null;
}

preg_match( '@[0-9]{4}/[0-9]{2}@', $url, $matches );
preg_match( '@[0-9]{4}/[0-9]{2}/@', $url, $matches );

if ( isset( $matches[0] ) ) {
return $matches[0];
return untrailingslashit( $matches[0] );
}

return null;
Expand Down Expand Up @@ -1490,42 +1489,44 @@ function get_attachment_folder_year_month( $post_id, $data = null ) {
}

/**
* Filter file details before upload.
* Filters the result when generating a unique file name.
*
* @param string $filename Unique file name.
* @param string $ext File extension, eg. ".png".
* @param string $dir Directory path.
*
* @param array $file An array of data for a single file.
* @return string
* @since 4.5.0
*
* @return array $file The altered file array with AWS unique filename.
*/
public function wp_handle_upload_prefilter( $file ) {
public function wp_unique_filename( $filename, $ext, $dir ) {
// Get Post ID if uploaded in post screen.
$post_id = filter_input( INPUT_POST, 'post_id', FILTER_VALIDATE_INT );

$file['name'] = $this->filter_unique_filename( $file['name'], $post_id );
$filename = $this->filter_unique_filename( $filename, $ext, $dir, $post_id );

return $file;
return $filename;
}

/**
* Create unique names for file to be uploaded to AWS.
* This only applies when the remove local file option is enabled.
*
* @param string $filename Unique file name.
* @param string $ext File extension, eg. ".png".
* @param string $dir Directory path.
* @param int $post_id Attachment's parent Post ID.
*
* @return string
*/
public function filter_unique_filename( $filename, $post_id = null ) {
public function filter_unique_filename( $filename, $ext, $dir, $post_id = null ) {
if ( ! $this->get_setting( 'copy-to-s3' ) || ! $this->is_plugin_setup( true ) ) {
return $filename;
}

// sanitize the file name before we begin processing
$filename = sanitize_file_name( $filename );

// Get base filename without extension.
$ext = pathinfo( $filename, PATHINFO_EXTENSION );
$ext = $ext ? ".$ext" : '';
$name = wp_basename( $filename, $ext );
$name = wp_basename( $filename, $ext );

// Edge case: if file is named '.ext', treat as an empty name.
if ( $name === $ext ) {
Expand Down Expand Up @@ -4276,15 +4277,18 @@ public function get_memory_limit() {
*
* @param string $prefix
* @param null|bool $uploaded_to_provider
* null - All attachments
* true - Attachments only uploaded to S3
* false - Attachments not uploaded to S3
* null - All attachments
* true - Attachments only uploaded to provider
* false - Attachments not uploaded to provider
* @param bool $skip_transient Whether to force database query and skip transient, default false
*
* @return int
*/
public function count_attachments( $prefix, $uploaded_to_provider = null ) {
public function count_attachments( $prefix, $uploaded_to_provider = null, $skip_transient = false ) {
global $wpdb;

$transient_key = 'as3cf_' . $prefix . '_media_count';

$sql = "SELECT COUNT(DISTINCT p.ID)
FROM `{$prefix}posts` p";

Expand All @@ -4297,11 +4301,19 @@ public function count_attachments( $prefix, $uploaded_to_provider = null ) {

$operator = $uploaded_to_provider ? 'not ' : '';
$where .= " AND pm.`post_id` is {$operator}null";

$transient_key .= ( $uploaded_to_provider ) ? '_offloaded' : 'not_offloaded';
}

$sql .= ' ' . $where;

return (int) $wpdb->get_var( $sql );
if ( true === $skip_transient || false === ( $count = get_site_transient( $transient_key ) ) ) {
$count = (int) $wpdb->get_var( $sql );

set_site_transient( $transient_key, $count, 2 * MINUTE_IN_SECONDS );
}

return $count;
}

/**
Expand Down Expand Up @@ -4789,9 +4801,9 @@ public function maybe_fix_local_subsite_url( $url ) {
$siteurl = trailingslashit( get_option( 'siteurl' ) );

if ( is_multisite() && ! $this->is_current_blog( get_current_blog_id() ) && 0 !== strpos( $url, $siteurl ) ) {
// Replace network URL with subsite's URL.
$network_siteurl = trailingslashit( network_site_url() );
$url = str_replace( $network_siteurl, $siteurl, $url );
// Replace original URL with subsite's current URL.
$orig_siteurl = trailingslashit( apply_filters( 'as3cf_get_orig_siteurl', network_site_url() ) );
$url = str_replace( $orig_siteurl, $siteurl, $url );
}

return $url;
Expand Down
30 changes: 19 additions & 11 deletions classes/as3cf-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,15 @@ protected function get_urls_from_content( $content, $cache, &$to_cache ) {
$attachment_id = null;
$bare_url = AS3CF_Utils::reduce_url( $url );

if ( isset( $cache[ $bare_url ] ) ) {
// If attachment ID recently or previously cached, skip full search.
if ( isset( $to_cache[ $bare_url ] ) ) {
$attachment_id = $to_cache[ $bare_url ];

if ( $this->is_failure( $attachment_id ) ) {
// Attachment ID failure, continue
continue;
}
} elseif ( isset( $cache[ $bare_url ] ) ) {
$attachment_id = $cache[ $bare_url ];

if ( $this->is_failure( $attachment_id ) ) {
Expand Down Expand Up @@ -493,18 +501,18 @@ protected function attachment_id_matches_src( $attachment_id, $url ) {
return false;
}

$base_url = AS3CF_Utils::reduce_url( $this->get_base_url( $attachment_id ) );
$base_url = $this->as3cf->encode_filename_in_path( AS3CF_Utils::reduce_url( $this->get_base_url( $attachment_id ) ) );
$basename = wp_basename( $base_url );

// Add full size URL
$base_urls[] = $base_url;

// Add additional image size URLs
foreach ( $meta['sizes'] as $size ) {
$base_urls[] = str_replace( $basename, $size['file'], $base_url );
$base_urls[] = str_replace( $basename, $this->as3cf->encode_filename_in_path( $size['file'] ), $base_url );
}

$url = AS3CF_Utils::reduce_url( $url );
$url = $this->as3cf->encode_filename_in_path( AS3CF_Utils::reduce_url( $url ) );

if ( in_array( $url, $base_urls ) ) {
// Match found, return true
Expand Down Expand Up @@ -582,10 +590,10 @@ protected function get_size_string_from_url( $attachment_id, $url ) {
return null;
}

$basename = wp_basename( $this->as3cf->maybe_remove_query_string( $url ) );
$basename = $this->as3cf->encode_filename_in_path( wp_basename( $this->as3cf->maybe_remove_query_string( $url ) ) );

foreach ( $meta['sizes'] as $size => $file ) {
if ( $basename === $file['file'] ) {
if ( $basename === $this->as3cf->encode_filename_in_path( $file['file'] ) ) {
return $size;
}
}
Expand Down Expand Up @@ -651,7 +659,7 @@ protected function url_replaced( $find, $replace, $content ) {
/**
* Get post cache
*
* @param null|int|WP_Post $post Optional. Post ID or post object. Defaults to current post.
* @param null|int|WP_Post $post Optional. Post ID or post object. Defaults to current post.
*
* @return array
*/
Expand All @@ -678,8 +686,8 @@ public function get_post_cache( $post = null ) {
/**
* Set the cache for the given post.
*
* @param null|int|WP_Post $post Optional. Post ID or post object. Defaults to current post.
* @param $data
* @param null|int|WP_Post $post Optional. Post ID or post object. Defaults to current post.
* @param $data
*/
protected function set_post_cache( $post, $data ) {
$post_id = AS3CF_Utils::get_post_id( $post );
Expand Down Expand Up @@ -905,8 +913,8 @@ public static function merge_cache( $existing_cache, $merge_cache ) {
}

if ( ! empty( $merge_cache ) ) {
$add_cache_keys = array_map( 'AS3CF_Utils::reduce_url', array_keys( $merge_cache ) );
$merge_cache = array_combine( $add_cache_keys, $merge_cache );
$add_cache_keys = array_map( 'AS3CF_Utils::reduce_url', array_keys( $merge_cache ) );
$merge_cache = array_combine( $add_cache_keys, $merge_cache );
}

return array_merge( $existing_cache, $merge_cache );
Expand Down
Loading

0 comments on commit dc35394

Please sign in to comment.