From 620477c2ef57446d08550bf1900dc370c6133f86 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 1 Sep 2020 08:42:53 +0200 Subject: [PATCH 1/4] add filter and stop removing cloudinary urls if storage is Cloudinary only --- .../php/media/class-filter.php | 9 ++++++++- .../php/sync/class-storage.php | 19 ++++++++++++++++++- .../php/sync/class-upload-sync.php | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index 2f264dae2..b7d3d72a7 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -243,7 +243,14 @@ public function filter_video_shortcodes( $content ) { * @return array */ public function filter_out_cloudinary( $data ) { - + /** + * Filter to allow programmatic disabling of Filtering out URLS. + * + * @param bool + */ + if ( ! apply_filters( 'cloudinary_can_filter_out_cloudinary', true ) ) { + return $data; + } $content = trim( wp_unslash( $data['post_content'] ) ); $assets = $this->get_media_tags( $content ); diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index 93bf15d84..4b62242f9 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -196,6 +196,7 @@ public function sync( $attachment_id ) { $this->sync->set_signature_item( $attachment_id, 'storage' ); $this->sync->set_signature_item( $attachment_id, 'breakpoints' ); $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['storage'], $this->settings['offload'] ); // Save the state. + $this->sync->managers['upload']->update_content( $attachment_id ); } /** @@ -303,6 +304,22 @@ public function is_ready() { return $this->sync && $this->media && $this->connect && $this->download; } + /** + * Maybe stop filtering out Cloudinary URLs. + * If storage is Cloudinary only, we need to save full Cloudinary URLS, since local's don't exist. + * + * @param bool $filter_out Flag to stop filtering out. + * + * @return bool + */ + public function save_cloudinary_links_maybe( $filter_out ) { + if ( 'cld' === $this->settings['offload'] ) { + $filter_out = false; + } + + return $filter_out; + } + /** * Setup hooks for the filters. */ @@ -332,7 +349,7 @@ public function setup() { // Tag the deactivate button. $plugin_file = pathinfo( dirname( CLDN_CORE ), PATHINFO_BASENAME ) . '/' . basename( CLDN_CORE ); add_filter( 'plugin_action_links_' . $plugin_file, array( $this, 'tag_deactivate_link' ) ); - + add_filter( 'cloudinary_can_filter_out_cloudinary', array( $this, 'save_cloudinary_links_maybe' ) ); } } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php index e4b4388df..ce6fe096d 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-sync.php @@ -325,7 +325,7 @@ public function update_breakpoints( $attachment_id, $breakpoints ) { * * @param int $attachment_id The attachment id to find and init an update. */ - private function update_content( $attachment_id ) { + public function update_content( $attachment_id ) { // Search and update link references in content. $content_search = new \WP_Query( array( 's' => 'wp-image-' . $attachment_id, 'fields' => 'ids', 'posts_per_page' => 1000 ) ); if ( ! empty( $content_search->found_posts ) ) { From 36b6d64ff188b4d197a9d107daa741964133f881 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 1 Sep 2020 09:03:41 +0200 Subject: [PATCH 2/4] Replace adding new filters with moveg the v1 method and skipping if local is cld --- .../php/class-media.php | 11 ++++--- .../php/media/class-filter.php | 32 ++++++++----------- .../php/sync/class-storage.php | 20 +----------- 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php index badcc75b7..d90440e74 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php @@ -630,12 +630,13 @@ public function get_transformations_from_string( $str, $type = 'image' ) { * @return string Cloudinary URL. */ public function attachment_url( $url, $attachment_id ) { + // Previous v1 and Cloudinary only storage. + $previous_url = strpos( $url, untrailingslashit( $this->base_url ) ); + if ( false !== $previous_url ) { + return substr( $url, $previous_url ); + } if ( ! doing_action( 'wp_insert_post_data' ) && false === $this->in_downsize ) { - // Previous v1. - $previous_url = strpos( $url, untrailingslashit( $this->base_url ) ); - if ( false !== $previous_url ) { - $url = substr( $url, $previous_url ); - } elseif ( $this->cloudinary_id( $attachment_id ) ) { + if ( $this->cloudinary_id( $attachment_id ) ) { $url = $this->cloudinary_url( $attachment_id ); } } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index b7d3d72a7..0d1239a78 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -243,14 +243,7 @@ public function filter_video_shortcodes( $content ) { * @return array */ public function filter_out_cloudinary( $data ) { - /** - * Filter to allow programmatic disabling of Filtering out URLS. - * - * @param bool - */ - if ( ! apply_filters( 'cloudinary_can_filter_out_cloudinary', true ) ) { - return $data; - } + $content = trim( wp_unslash( $data['post_content'] ) ); $assets = $this->get_media_tags( $content ); @@ -269,7 +262,10 @@ public function filter_out_cloudinary( $data ) { } else { $local_url = wp_get_attachment_url( $attachment_id ); } - + // Skip since there is no local available. + if ( $this->media->is_cloudinary_url( $local_url ) ) { + continue; + } $inherit_transformations = $this->media->get_transformation_from_meta( $attachment_id ); $transformations = $this->media->get_transformations_from_string( $url ); $transformations = array_filter( $transformations ); @@ -352,7 +348,7 @@ public function filter_out_local( $content ) { if ( $url === $cloudinary_url ) { continue; } - + // Replace old tag. $new_tag = str_replace( $url, $cloudinary_url, $asset ); @@ -363,9 +359,9 @@ public function filter_out_local( $content ) { } // Apply lazy loading attribute - if ( - apply_filters( 'wp_lazy_loading_enabled', true ) && - false === strpos( $new_tag, 'loading="lazy"' ) && + if ( + apply_filters( 'wp_lazy_loading_enabled', true ) && + false === strpos( $new_tag, 'loading="lazy"' ) && $clean ) { $new_tag = str_replace( '/>', ' loading="lazy" />', $new_tag ); @@ -390,7 +386,7 @@ public function filter_out_local( $content ) { /** * Return a Cloudinary URL for an attachment used in JS. * - * @param array $attachment The attachment response array. + * @param array $attachment The attachment response array. * * @return array * @uses filter:wp_prepare_attachment_for_js @@ -433,11 +429,11 @@ public function filter_attachment_for_js( $attachment ) { /** * Return a Cloudinary URL for an attachment used in a REST REQUEST. * - * @uses filter:rest_prepare_attachment - * * @param \WP_REST_Response $attachment The attachment array to be used in JS. * * @return \WP_REST_Response + * @uses filter:rest_prepare_attachment + * */ public function filter_attachment_for_rest( $attachment ) { if ( ! isset( $attachment->data['id'] ) ) { @@ -528,7 +524,7 @@ public function filter_video_embeds( $html, $id, $attachment ) { } if ( ! empty( $attachment['transformations'] ) ) { $transformation_string = Api::generate_transformation_string( $attachment['transformations'] ); - $new_atts .= ' transformations="' . esc_attr( $transformation_string ) . '"'; + $new_atts .= ' transformations="' . esc_attr( $transformation_string ) . '"'; } $html = str_replace( $shortcode['args'], $new_atts, $html ); } @@ -706,7 +702,7 @@ public function setup_hooks() { add_filter( 'the_content', array( $this, 'filter_out_local' ), 9 ); // Early to hook before responsive srcsets. add_filter( 'wp_prepare_attachment_for_js', array( $this, 'filter_attachment_for_js' ), 11 ); - // Add support for custom header. + // Add support for custom header. add_filter( 'get_header_image_tag', array( $this, 'filter_out_local' ) ); // Add transformations. diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index 4b62242f9..10957d991 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -304,22 +304,6 @@ public function is_ready() { return $this->sync && $this->media && $this->connect && $this->download; } - /** - * Maybe stop filtering out Cloudinary URLs. - * If storage is Cloudinary only, we need to save full Cloudinary URLS, since local's don't exist. - * - * @param bool $filter_out Flag to stop filtering out. - * - * @return bool - */ - public function save_cloudinary_links_maybe( $filter_out ) { - if ( 'cld' === $this->settings['offload'] ) { - $filter_out = false; - } - - return $filter_out; - } - /** * Setup hooks for the filters. */ @@ -328,7 +312,7 @@ public function setup() { $this->sync = $this->plugin->get_component( 'sync' ); $this->connect = $this->plugin->get_component( 'connect' ); $this->media = $this->plugin->get_component( 'media' ); - $this->download = $this->sync->managers['download'] ? $this->sync->managers['download'] : new Download_Sync( $plugin ); + $this->download = $this->sync->managers['download'] ? $this->sync->managers['download'] : new Download_Sync( $this->plugin ); if ( $this->is_ready() ) { $defaults = array( @@ -349,8 +333,6 @@ public function setup() { // Tag the deactivate button. $plugin_file = pathinfo( dirname( CLDN_CORE ), PATHINFO_BASENAME ) . '/' . basename( CLDN_CORE ); add_filter( 'plugin_action_links_' . $plugin_file, array( $this, 'tag_deactivate_link' ) ); - add_filter( 'cloudinary_can_filter_out_cloudinary', array( $this, 'save_cloudinary_links_maybe' ) ); - } } } From 1e520193b93c503ba13ce3438cf19541713c9b60 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 1 Sep 2020 09:05:32 +0200 Subject: [PATCH 3/4] fix formatting --- .../php/media/class-filter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php index 0d1239a78..17b35ce6a 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php @@ -524,7 +524,7 @@ public function filter_video_embeds( $html, $id, $attachment ) { } if ( ! empty( $attachment['transformations'] ) ) { $transformation_string = Api::generate_transformation_string( $attachment['transformations'] ); - $new_atts .= ' transformations="' . esc_attr( $transformation_string ) . '"'; + $new_atts .= ' transformations="' . esc_attr( $transformation_string ) . '"'; } $html = str_replace( $shortcode['args'], $new_atts, $html ); } From fa5e1c511ed6b22c239533b42844bdf0d63d0c28 Mon Sep 17 00:00:00 2001 From: David Cramer Date: Tue, 1 Sep 2020 09:09:45 +0200 Subject: [PATCH 4/4] make sure we don't call update content for no reason. --- .../php/sync/class-storage.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index 10957d991..00e7332dc 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -196,7 +196,10 @@ public function sync( $attachment_id ) { $this->sync->set_signature_item( $attachment_id, 'storage' ); $this->sync->set_signature_item( $attachment_id, 'breakpoints' ); $this->media->update_post_meta( $attachment_id, Sync::META_KEYS['storage'], $this->settings['offload'] ); // Save the state. - $this->sync->managers['upload']->update_content( $attachment_id ); + // If bringing media back to WordPress, we need to trigger content update to allow unfiltered Cloudinary URL's to be filtered. + if ( ! empty( $previous_state ) && 'cld' !== $this->settings['offload'] ) { + $this->sync->managers['upload']->update_content( $attachment_id ); + } } /**