diff --git a/README.md b/README.md index 27f45384..21236021 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/classes/amazon-s3-and-cloudfront.php b/classes/amazon-s3-and-cloudfront.php index b6384546..06948d42 100644 --- a/classes/amazon-s3-and-cloudfront.php +++ b/classes/amazon-s3-and-cloudfront.php @@ -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 ) { @@ -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 * @@ -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 ); @@ -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 ) { @@ -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 ); + } } } diff --git a/classes/as3cf-filter.php b/classes/as3cf-filter.php index b0327c0a..097048fe 100644 --- a/classes/as3cf-filter.php +++ b/classes/as3cf-filter.php @@ -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; } @@ -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(); @@ -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; } /** @@ -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? * diff --git a/classes/filters/as3cf-local-to-s3.php b/classes/filters/as3cf-local-to-s3.php index 07725fde..da925dc5 100644 --- a/classes/filters/as3cf-local-to-s3.php +++ b/classes/filters/as3cf-local-to-s3.php @@ -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' ) ); @@ -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. * @@ -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 { diff --git a/classes/filters/as3cf-s3-to-local.php b/classes/filters/as3cf-s3-to-local.php index 38cc64df..8d487d68 100644 --- a/classes/filters/as3cf-s3-to-local.php +++ b/classes/filters/as3cf-s3-to-local.php @@ -12,6 +12,7 @@ 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' ) ); @@ -19,6 +20,20 @@ protected function init() { 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. * @@ -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? * diff --git a/languages/amazon-s3-and-cloudfront-en.pot b/languages/amazon-s3-and-cloudfront-en.pot index 4bb88f81..14f27846 100644 --- a/languages/amazon-s3-and-cloudfront-en.pot +++ b/languages/amazon-s3-and-cloudfront-en.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: amazon-s3-and-cloudfront\n" "Report-Msgid-Bugs-To: nom@deliciousbrains.com\n" -"POT-Creation-Date: 2016-12-13 11:56+0000\n" +"POT-Creation-Date: 2017-01-12 08:46-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -49,103 +49,103 @@ msgstr "" msgid "Error uploading %s to S3: %s" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2170 +#: classes/amazon-s3-and-cloudfront.php:2184 msgid "Cheatin’ eh?" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2174 +#: classes/amazon-s3-and-cloudfront.php:2188 msgid "You do not have sufficient permissions to access this page." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2180 +#: classes/amazon-s3-and-cloudfront.php:2194 msgid "No bucket name provided." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2439 +#: classes/amazon-s3-and-cloudfront.php:2453 msgid "Error Getting Bucket Region" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2440 +#: classes/amazon-s3-and-cloudfront.php:2454 #, php-format msgid "There was an error attempting to get the region of the bucket %s: %s" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2560 +#: classes/amazon-s3-and-cloudfront.php:2574 msgid "" "This is a test file to check if the user has write permission to S3. Delete " "me if found." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2592 +#: classes/amazon-s3-and-cloudfront.php:2606 #, php-format msgid "" "There was an error attempting to check the permissions of the bucket %s: %s" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2649 +#: classes/amazon-s3-and-cloudfront.php:2663 msgid "Error creating bucket" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2650 +#: classes/amazon-s3-and-cloudfront.php:2664 msgid "Bucket name too short." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2651 +#: classes/amazon-s3-and-cloudfront.php:2665 msgid "Bucket name too long." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2652 +#: classes/amazon-s3-and-cloudfront.php:2666 msgid "" "Invalid character. Bucket names can contain lowercase letters, numbers, " "periods and hyphens." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2653 +#: classes/amazon-s3-and-cloudfront.php:2667 msgid "Error saving bucket" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2654 +#: classes/amazon-s3-and-cloudfront.php:2668 msgid "Error fetching buckets" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2655 +#: classes/amazon-s3-and-cloudfront.php:2669 msgid "Error getting URL preview: " msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2656 +#: classes/amazon-s3-and-cloudfront.php:2670 msgid "The changes you made will be lost if you navigate away from this page" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2657 +#: classes/amazon-s3-and-cloudfront.php:2671 msgid "Getting diagnostic info..." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2658 +#: classes/amazon-s3-and-cloudfront.php:2672 msgid "Error getting diagnostic info: " msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2726 +#: classes/amazon-s3-and-cloudfront.php:2740 msgid "Cheatin' eh?" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2834 +#: classes/amazon-s3-and-cloudfront.php:2848 msgctxt "Show the media library tab" msgid "Media Library" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:2835 +#: classes/amazon-s3-and-cloudfront.php:2849 msgctxt "Show the support tab" msgid "Support" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3052 +#: classes/amazon-s3-and-cloudfront.php:3066 #, php-format msgid "" "WP Offload S3 — The file %s has been given %s " "permissions on Amazon S3." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3071 +#: classes/amazon-s3-and-cloudfront.php:3085 msgid "" "WP Offload S3 Requirement Missing — Looks like you " "don't have an image manipulation library installed on this server and " @@ -153,11 +153,11 @@ msgid "" "Please setup GD or ImageMagick." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3748 +#: classes/amazon-s3-and-cloudfront.php:3759 msgid "Quick Start Guide" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3750 +#: classes/amazon-s3-and-cloudfront.php:3761 #, php-format msgid "" "Looks like we don't have write access to this bucket. It's likely that the " @@ -166,7 +166,7 @@ msgid "" "correctly." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3752 +#: classes/amazon-s3-and-cloudfront.php:3763 #, php-format msgid "" "Looks like we don't have access to the buckets. It's likely that the user " @@ -174,39 +174,39 @@ msgid "" "Please see our %s for instructions on setting up permissions correctly." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3903 +#: classes/amazon-s3-and-cloudfront.php:3914 msgid "WP Offload S3 Activation" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3904 +#: classes/amazon-s3-and-cloudfront.php:3915 msgid "" "WP Offload S3 Lite and WP Offload S3 cannot both be active. We've " "automatically deactivated WP Offload S3 Lite." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3906 +#: classes/amazon-s3-and-cloudfront.php:3917 msgid "WP Offload S3 Lite Activation" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3907 +#: classes/amazon-s3-and-cloudfront.php:3918 msgid "" "WP Offload S3 Lite and WP Offload S3 cannot both be active. We've " "automatically deactivated WP Offload S3." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:3981 +#: classes/amazon-s3-and-cloudfront.php:3992 msgid "More info" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4072 +#: classes/amazon-s3-and-cloudfront.php:4083 msgid "this doc" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4074 +#: classes/amazon-s3-and-cloudfront.php:4085 msgid "WP Offload S3 Feature Removed" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4075 +#: classes/amazon-s3-and-cloudfront.php:4086 #, php-format msgid "" "You had the \"Always non-SSL\" option selected in your settings, but we've " @@ -217,32 +217,32 @@ msgid "" "to the old behavior." msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4103 -#: classes/amazon-s3-and-cloudfront.php:4196 +#: classes/amazon-s3-and-cloudfront.php:4114 +#: classes/amazon-s3-and-cloudfront.php:4207 msgid "Amazon S3" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4197 +#: classes/amazon-s3-and-cloudfront.php:4208 msgctxt "Amazon S3 bucket" msgid "Bucket" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4198 +#: classes/amazon-s3-and-cloudfront.php:4209 msgctxt "Path to file on Amazon S3" msgid "Path" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4199 +#: classes/amazon-s3-and-cloudfront.php:4210 msgctxt "Location of Amazon S3 bucket" msgid "Region" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4200 +#: classes/amazon-s3-and-cloudfront.php:4211 msgctxt "Access control list of the file on Amazon S3" msgid "Access" msgstr "" -#: classes/amazon-s3-and-cloudfront.php:4201 +#: classes/amazon-s3-and-cloudfront.php:4212 msgid "URL" msgstr "" diff --git a/readme.txt b/readme.txt index d547e11b..228af587 100644 --- a/readme.txt +++ b/readme.txt @@ -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. @@ -65,6 +65,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 diff --git a/wordpress-s3.php b/wordpress-s3.php index 60606547..1de9631c 100644 --- a/wordpress-s3.php +++ b/wordpress-s3.php @@ -4,7 +4,7 @@ Plugin URI: http://wordpress.org/extend/plugins/amazon-s3-and-cloudfront/ Description: Automatically copies media uploads to Amazon S3 for storage and delivery. Optionally configure Amazon CloudFront for even faster delivery. Author: Delicious Brains -Version: 1.1.4 +Version: 1.1.5 Author URI: http://deliciousbrains.com/ Network: True Text Domain: amazon-s3-and-cloudfront @@ -26,7 +26,7 @@ // Then completely rewritten. */ -$GLOBALS['aws_meta']['amazon-s3-and-cloudfront']['version'] = '1.1.4'; +$GLOBALS['aws_meta']['amazon-s3-and-cloudfront']['version'] = '1.1.5'; $aws_plugin_version_required = '1.0.1';