Skip to content

Commit 3933c4f

Browse files
committed
take control over srcset
1 parent 8495d43 commit 3933c4f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ function ( $item ) use ( $crop ) {
10801080
'width' => $breakpoint['width'],
10811081
);
10821082
$sources[ $breakpoint['width'] ] = array(
1083-
'url' => $this->cloudinary_url( $attachment_id, $size, $transformations, $cloudinary_id, true ),
1083+
'url' => $this->cloudinary_url( $attachment_id, $size, $transformations, $cloudinary_id, $image_meta['overwrite_transformations'] ),
10841084
'descriptor' => 'w',
10851085
'value' => $breakpoint['width'],
10861086
);
@@ -1106,7 +1106,7 @@ function ( $item ) use ( $crop ) {
11061106
// Use current sources, but convert the URLS.
11071107
foreach ( $sources as &$source ) {
11081108
if ( ! $this->is_cloudinary_url( $source['url'] ) ) {
1109-
$source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, true ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
1109+
$source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, $image_meta['overwrite_transformations'] ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
11101110
}
11111111
}
11121112

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/media/class-filter.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,11 @@ public function filter_out_local( $content ) {
364364
$new_tag = str_replace( $poster, $cloudinary_url, $new_tag );
365365
}
366366
}
367-
368-
$content = str_replace( $asset, $new_tag, $content );
367+
$image_meta = wp_get_attachment_metadata( $attachment_id );
368+
$image_meta['file'] = pathinfo( $cloudinary_id, PATHINFO_FILENAME ) . '/' . pathinfo( $cloudinary_id, PATHINFO_BASENAME );
369+
$image_meta['overwrite_transformations'] = $overwrite_transformations;
370+
$new_tag = wp_image_add_srcset_and_sizes( $new_tag, $image_meta, $attachment_id );
371+
$content = str_replace( $asset, $new_tag, $content );
369372
}
370373

371374
return $this->filter_video_shortcodes( $content );
@@ -700,7 +703,7 @@ public function setup_hooks() {
700703
// Filter URLS within content.
701704
add_action( 'wp_insert_post_data', array( $this, 'filter_out_cloudinary' ) );
702705
add_filter( 'the_editor_content', array( $this, 'filter_out_local' ) );
703-
add_filter( 'the_content', array( $this, 'filter_out_local' ), 9 ); // Early to hook before responsive srcsets.
706+
add_filter( 'the_content', array( $this, 'filter_out_local' ), 100 );
704707
add_filter( 'wp_prepare_attachment_for_js', array( $this, 'filter_attachment_for_js' ), 11 );
705708

706709
// Add support for custom header.
@@ -735,5 +738,15 @@ function ( $type ) use ( $filter ) {
735738

736739
// Filter for block rendering.
737740
add_filter( 'render_block_data', array( $this, 'filter_image_block_pre_render' ), 10, 2 );
741+
742+
// Cancel out breakpoints till later.
743+
add_filter(
744+
'wp_img_tag_add_srcset_and_sizes_attr',
745+
function ( $add, $image, $context, $attachment_id ) {
746+
return ! $this->media->has_public_id( $attachment_id );
747+
},
748+
10,
749+
4
750+
);
738751
}
739752
}

0 commit comments

Comments
 (0)