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 2f264dae2..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 @@ -262,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 ); @@ -345,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 ); @@ -356,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 ); @@ -383,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 @@ -426,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'] ) ) { @@ -699,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 93bf15d84..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,6 +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. + // 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 ); + } } /** @@ -311,7 +315,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( @@ -332,8 +336,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' ) ); - - } } } 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 ) ) {