Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 60 additions & 43 deletions inc/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
}
Expand Down