From b90efd24ff2a6ca8d7c0e72e405a7c6259659bb1 Mon Sep 17 00:00:00 2001 From: Taylor Lovett Date: Mon, 31 Aug 2020 16:25:27 -0400 Subject: [PATCH] Allow for using cloudinary on content images that don't have IDS --- inc/helpers.php | 103 ++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 43 deletions(-) diff --git a/inc/helpers.php b/inc/helpers.php index 793ba94..2a4e0d9 100644 --- a/inc/helpers.php +++ b/inc/helpers.php @@ -31,54 +31,71 @@ function cloudinary_update_content_images( $content = '' ) { } foreach ( $all_images as $image ) { + + + $src = preg_match( '/ src="([^"]*)"/', $image, $match_src ) ? $match_src[1] : ''; + // Only look for images with an attachment ID. - if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && ( $attachment_id = absint( $class_id[1] ) ) ) { // @codingStandardsIgnoreLine - $src = preg_match( '/ src="([^"]*)"/', $image, $match_src ) ? $match_src[1] : ''; - $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; - $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; - $class = preg_match( '/ class="([^"]*)"/', $image, $match_class ) ? $match_class[1] : ''; - - if ( ! empty( $class ) ) { - $size = preg_match( '/size-([a-zA-Z0-9-_]+)?/', $class, $match_size ) ? $match_size[1] : ''; - } else { - $size = ''; - } + if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && ! empty( $class_id[1] ) ) { + $attachment_id_or_url = absint( $class_id[1] ); - $updated_image = apply_filters( 'cloudinary_content_image', $image, $attachment_id, $src, $width, $height ); - - // Check if filter updated the image. - if ( $updated_image !== $image ) { - // Filter updated the image, use this image. - $content = str_replace( $image, $updated_image, $content ); - } elseif ( ! empty( $src ) ) { - // Filter hasn't updated the image, let's update it now. - if ( ! empty( $width ) && ! empty( $height ) ) { - // Let's see if we can find this image's crop type. - $hard_crop = false; - if ( ! empty( $size ) ) { - $dimensions = JB\Cloudinary\Frontend::get_instance()->get_image_size( $size ); - if ( ! empty( $dimensions ) && (bool) $dimensions['crop'] ) { - $hard_crop = true; - } - } + $replace_image = true; + } else { + $attachment_id_or_url = preg_match( '/ src="([^"]*)"/', $image, $match_src ) ? $match_src[1] : ''; - // We have a width and height, let's use them to transform the image. - $updated_src = cloudinary_url( $attachment_id, array( - 'transform' => array( - 'width' => $width, - 'height' => $height, - 'crop' => cloudinary_default_crop( $hard_crop ), - ), - ) ); - } else { - // No width and height from the image, let's default to the full URL. - $updated_src = cloudinary_url( $src ); - } + $upload_dir = wp_upload_dir(); + + $replace_image = preg_match( '#^' . $upload_dir['baseurl'] . '#', $attachment_id_or_url ); + } + + if ( ! apply_filters( 'cloudinary_replace_content_image', $replace_image, $attachment_id_or_url, $src, $image ) ) { + continue; + } + + $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; + $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; + $class = preg_match( '/ class="([^"]*)"/', $image, $match_class ) ? $match_class[1] : ''; + + if ( ! empty( $class ) ) { + $size = preg_match( '/size-([a-zA-Z0-9-_]+)?/', $class, $match_size ) ? $match_size[1] : ''; + } else { + $size = ''; + } + + $updated_image = apply_filters( 'cloudinary_content_image', $image, $attachment_id_or_url, $src, $width, $height ); - if ( ! empty( $updated_src ) ) { - $updated_image = str_replace( $src, $updated_src, $image ); - $content = str_replace( $image, $updated_image, $content ); + // Check if filter updated the image. + if ( $updated_image !== $image ) { + // Filter updated the image, use this image. + $content = str_replace( $image, $updated_image, $content ); + } elseif ( ! empty( $src ) ) { + // Filter hasn't updated the image, let's update it now. + if ( ! empty( $width ) && ! empty( $height ) ) { + // Let's see if we can find this image's crop type. + $hard_crop = false; + if ( ! empty( $size ) ) { + $dimensions = JB\Cloudinary\Frontend::get_instance()->get_image_size( $size ); + if ( ! empty( $dimensions ) && (bool) $dimensions['crop'] ) { + $hard_crop = true; + } } + + // We have a width and height, let's use them to transform the image. + $updated_src = cloudinary_url( $attachment_id_or_url, array( + 'transform' => array( + 'width' => $width, + 'height' => $height, + 'crop' => cloudinary_default_crop( $hard_crop ), + ), + ) ); + } else { + // No width and height from the image, let's default to the full URL. + $updated_src = cloudinary_url( $src ); + } + + if ( ! empty( $updated_src ) ) { + $updated_image = str_replace( $src, $updated_src, $image ); + $content = str_replace( $image, $updated_image, $content ); } } }