Skip to content

Commit

Permalink
Deploying version 1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfarlan committed Jan 12, 2017
1 parent dee44bb commit f920838
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 77 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
**Contributors:** bradt, deliciousbrains
**Tags:** uploads, amazon, s3, amazon s3, mirror, admin, media, cdn, cloudfront
**Requires at least:** 4.4
**Tested up to:** 4.7
**Stable tag:** 1.1.4
**Tested up to:** 4.7.1
**Stable tag:** 1.1.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 @@ -69,6 +69,11 @@ This version requires PHP 5.3.3+ and the Amazon Web Services plugin

## Changelog ##

### WP Offload S3 Lite 1.1.5 - 2017-01-12 ###
* Improvement: Filter custom CSS - S3 URLs will no longer be saved to the database
* Bug fix: PDF previews have incorrect MIME type
* Bug fix: Original PDF not removed from S3 on attachment delete when image previews exist

### WP Offload S3 Lite 1.1.4 - 2016-12-13 ###
* New: Upgrade routine to replace all S3 URLs in post excerpts with local URLs
* Improvement: Performance improvements
Expand Down
47 changes: 29 additions & 18 deletions classes/amazon-s3-and-cloudfront.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,9 +1017,10 @@ function upload_attachment_to_s3( $post_id, $data = null, $file_path = null, $fo
$acl = apply_filters( 'as3cf_upload_acl_sizes', self::DEFAULT_ACL, $size, $post_id, $data );

$additional_images[] = array(
'Key' => $prefix . basename( $file_path ),
'SourceFile' => $file_path,
'ACL' => $acl,
'Key' => $prefix . basename( $file_path ),
'SourceFile' => $file_path,
'ACL' => $acl,
'ContentType' => $this->get_mime_type( $file_path ),
);

if ( self::DEFAULT_ACL !== $acl ) {
Expand Down Expand Up @@ -1093,6 +1094,19 @@ function upload_attachment_to_s3( $post_id, $data = null, $file_path = null, $fo
return $s3object;
}

/**
* Get a file's real mime type
*
* @param string $file_path
*
* @return string
*/
protected function get_mime_type( $file_path ) {
$file_type = wp_check_filetype_and_ext( $file_path, basename( $file_path ) );

return $file_type['type'];
}

/**
* Should gzip file
*
Expand Down Expand Up @@ -3676,10 +3690,10 @@ function get_all_blog_table_prefixes( $exclude_blog_ids = array() ) {
* @return array
*/
public function get_attachment_file_paths( $attachment_id, $exists_locally = true, $meta = false, $include_backups = true ) {
$paths = array();
$file_path = get_attached_file( $attachment_id, true );
$file_name = basename( $file_path );
$backups = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
$paths = array(
'original' => $file_path,
);

if ( ! $meta ) {
$meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
Expand All @@ -3689,15 +3703,13 @@ public function get_attachment_file_paths( $attachment_id, $exists_locally = tru
return $paths;
}

$original_file = $file_path; // Not all attachments will have meta
$file_name = basename( $file_path );

if ( isset( $meta['file'] ) ) {
$original_file = str_replace( $file_name, basename( $meta['file'] ), $file_path );
// Thumb
if ( isset( $meta['thumb'] ) ) {
$paths['thumb'] = str_replace( $file_name, $meta['thumb'], $file_path );
}

// Original file
$paths['full'] = $original_file;

// Sizes
if ( isset( $meta['sizes'] ) ) {
foreach ( $meta['sizes'] as $size => $file ) {
Expand All @@ -3707,15 +3719,14 @@ public function get_attachment_file_paths( $attachment_id, $exists_locally = tru
}
}

// Thumb
if ( isset( $meta['thumb'] ) ) {
$paths[] = str_replace( $file_name, $meta['thumb'], $file_path );
}
$backups = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );

// Backups
if ( $include_backups && is_array( $backups ) ) {
foreach ( $backups as $backup ) {
$paths[] = str_replace( $file_name, $backup['file'], $file_path );
foreach ( $backups as $size => $file ) {
if ( isset( $file['file'] ) ) {
$paths[ $size ] = str_replace( $file_name, $file['file'], $file_path );
}
}
}

Expand Down
86 changes: 75 additions & 11 deletions classes/as3cf-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ protected function get_urls_from_img_src( $content, &$to_cache ) {
protected function get_urls_from_content( $content, $cache, &$to_cache ) {
$url_pairs = array();

if ( ! preg_match_all( '/(http|https)?:?\/\/[^"\'\s<>\\\]*/', $content, $matches ) || ! isset( $matches[0] ) ) {
if ( ! preg_match_all( '/(http|https)?:?\/\/[^"\'\s<>()\\\]*/', $content, $matches ) || ! isset( $matches[0] ) ) {
// No URLs found, return
return $url_pairs;
}
Expand Down Expand Up @@ -495,17 +495,19 @@ protected function replace_urls( $content, $url_pairs ) {
/**
* Get post cache
*
* @param bool|int $post_id
*
* @return array
*/
protected function get_post_cache() {
global $post;
protected function get_post_cache( $post_id = false ) {
$post_id = $this->get_post_id( $post_id );

if ( ! isset( $post->ID ) ) {
if ( ! $post_id ) {
// Post ID not found, return empty cache
return array();
}

$cache = get_post_meta( $post->ID, 'amazonS3_cache', true );
$cache = get_post_meta( $post_id, 'amazonS3_cache', true );

if ( empty( $cache ) ) {
$cache = array();
Expand All @@ -517,18 +519,40 @@ protected function get_post_cache() {
/**
* Maybe update post cache
*
* @param array $to_cache
* @param array $to_cache
* @param bool|int $post_id
*/
protected function maybe_update_post_cache( $to_cache ) {
global $post;
protected function maybe_update_post_cache( $to_cache, $post_id = false ) {
$post_id = $this->get_post_id( $post_id );

if ( ! isset( $post->ID ) || empty( $to_cache ) ) {
if ( ! $post_id || empty( $to_cache ) ) {
return;
}

$urls = array_merge( $this->get_post_cache(), $to_cache );
$urls = array_merge( $this->get_post_cache( $post_id ), $to_cache );

update_post_meta( $post_id, 'amazonS3_cache', $urls );
}

/**
* Get post ID.
*
* @param bool|int $post_id
*
* @return bool|int
*/
protected function get_post_id( $post_id ) {
if ( false !== $post_id ) {
return $post_id;
}

global $post;

if ( isset( $post->ID ) ) {
return $post->ID;
}

update_post_meta( $post->ID, 'amazonS3_cache', $urls );
return false;
}

/**
Expand Down Expand Up @@ -644,6 +668,46 @@ protected function remove_aws_query_strings( $content, $base_url = '' ) {
return $content;
}

/**
* Filter custom CSS.
*
* @param string $css
* @param string $stylesheet
*
* @return string
*/
protected function filter_custom_css( $css, $stylesheet ) {
if ( empty( $css ) ) {
return $css;
}

$post_id = $this->get_custom_css_post_id( $stylesheet );
$cache = $this->get_post_cache( $post_id );
$to_cache = array();
$css = $this->process_content( $css, $cache, $to_cache );

$this->maybe_update_post_cache( $to_cache, $post_id );

return $css;
}

/**
* Get custom CSS post ID.
*
* @param string $stylesheet
*
* @return int
*/
protected function get_custom_css_post_id( $stylesheet ) {
$post = wp_get_custom_css_post( $stylesheet );

if ( ! $post ) {
return 0;
}

return $post->ID;
}

/**
* Does URL need replacing?
*
Expand Down
28 changes: 27 additions & 1 deletion classes/filters/as3cf-local-to-s3.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ protected function init() {
// Customizer
add_filter( 'theme_mod_background_image', array( $this, 'filter_customizer_image' ) );
add_filter( 'theme_mod_header_image', array( $this, 'filter_customizer_image' ) );
add_filter( 'customize_value_custom_css', array( $this, 'filter_customize_value_custom_css' ), 10, 2 );
add_filter( 'wp_get_custom_css', array( $this, 'filter_wp_get_custom_css' ), 10, 2 );
// Posts
add_action( 'the_post', array( $this, 'filter_post_data' ) );
add_filter( 'content_pagination', array( $this, 'filter_content_pagination' ) );
Expand All @@ -23,6 +25,30 @@ protected function init() {
add_filter( 'widget_form_callback', array( $this, 'filter_widget_form' ), 10, 2 );
}

/**
* Filter customize value custom CSS.
*
* @param mixed $value
* @param WP_Customize_Custom_CSS_Setting $setting
*
* @return mixed
*/
public function filter_customize_value_custom_css( $value, $setting ) {
return $this->filter_custom_css( $value, $setting->stylesheet );
}

/**
* Filter `wp_get_custom_css`.
*
* @param string $css
* @param string $stylesheet
*
* @return string
*/
public function filter_wp_get_custom_css( $css, $stylesheet ) {
return $this->filter_custom_css( $css, $stylesheet );
}

/**
* Filter post data.
*
Expand All @@ -34,7 +60,7 @@ public function filter_post_data( $post ) {
$cache = $this->get_post_cache();
$to_cache = array();

if ( count( $pages ) === 1 ) {
if ( 1 === count( $pages ) && ! empty( $pages[0] ) ) {
// Post already filtered and available on global $page array, continue
$post->post_content = $pages[0];
} else {
Expand Down
24 changes: 24 additions & 0 deletions classes/filters/as3cf-s3-to-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,28 @@ protected function init() {
add_filter( 'pre_set_theme_mod_background_image', array( $this, 'filter_customizer_image' ), 10, 2 );
add_filter( 'pre_set_theme_mod_header_image', array( $this, 'filter_customizer_image' ), 10, 2 );
add_filter( 'pre_set_theme_mod_header_image_data', array( $this, 'filter_header_image_data' ), 10, 2 );
add_filter( 'update_custom_css_data', array( $this, 'filter_update_custom_css_data' ), 10, 2 );
// Posts
add_filter( 'content_save_pre', array( $this, 'filter_post' ) );
add_filter( 'excerpt_save_pre', array( $this, 'filter_post' ) );
// Widgets
add_filter( 'widget_update_callback', array( $this, 'filter_widget_update' ), 10, 4 );
}

/**
* Filter update custom CSS data.
*
* @param array $data
* @param array $args
*
* @return array
*/
public function filter_update_custom_css_data( $data, $args ) {
$data['css'] = $this->filter_custom_css( $data['css'], $args['stylesheet'] );

return $data;
}

/**
* Filter widget update.
*
Expand All @@ -44,6 +59,15 @@ public function filter_widget_update( $instance, $new_instance, $old_instance, $
return $instance;
}

/**
* Should filter content.
*
* @return bool
*/
protected function should_filter_content() {
return true;
}

/**
* Does URL need replacing?
*
Expand Down
Loading

0 comments on commit f920838

Please sign in to comment.