Skip to content

Commit ad95a7c

Browse files
Apply image crop on editor and frontend with eager loading
1 parent 09a8d92 commit ad95a7c

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

php/class-delivery.php

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,21 +1007,46 @@ public function get_media_tags( $content, $tags = 'img|video' ) {
10071007
}
10081008

10091009
/**
1010-
* Get the size dimensions from the parent figure class if available.
1010+
* Get the image size dimensions from the tags if available.
10111011
*
1012-
* @param string $element The img tag element.
1013-
* @param string $content The full HTML content.
1012+
* @param array $tags The media tags found in the content.
1013+
* @param array $relation The relation data for the media item.
10141014
*
1015-
* @return array|null An array with width and height, or null if not found.
1015+
* @return array An array with width and height, or empty if not found.
10161016
*/
1017-
private function get_size_from_parent( $element, $content ) {
1018-
$size_slug = $this->get_size_slug_from_parent_figure_class( $element, $content );
1017+
private function get_image_size_within_tags( $tags, $relation ) {
1018+
// If we don't have a post ID, we can't find a size, so return empty.
1019+
if ( empty( $relation['post_id'] ) ) {
1020+
return array();
1021+
}
1022+
$post_id = intval( $relation['post_id'] );
10191023

1020-
// If no size slug is found in the parent class, there's no specific size to use for transformation.
1021-
if ( empty( $size_slug ) ) {
1022-
return null;
1024+
// Look through the tags to find one that matches our post ID and has a size slug, then get the size from that slug.
1025+
foreach ( $tags as $set ) {
1026+
// If this set doesn't have a size slug, skip.
1027+
if ( empty( $set['atts']['data-size-slug'] ) ) {
1028+
continue;
1029+
}
1030+
1031+
// If this tag's post ID matches our relation's post ID, get the size from the slug and return it.
1032+
if ( $post_id === $set['id'] ) {
1033+
$size = $this->get_size_from_slug( $set['atts']['data-size-slug'] );
1034+
return ( empty( $size ) ) ? array() : $size; // Return the size if found, otherwise return empty array.
1035+
}
10231036
}
10241037

1038+
return array();
1039+
}
1040+
1041+
/**
1042+
* Get the size dimensions based on the size slug.
1043+
*
1044+
* @param string $size_slug The WordPress size slug (e.g., 'thumbnail', 'medium', 'large').
1045+
*
1046+
* @return array|null An array with width and height, or null if not found.
1047+
*/
1048+
private function get_size_from_slug( $size_slug ) {
1049+
10251050
// Get the dimensions of the WordPress size from options.
10261051
$size_width = get_option( $size_slug . '_size_w' );
10271052
$size_height = get_option( $size_slug . '_size_h' );
@@ -1143,7 +1168,9 @@ function ( $tag ) use ( $content ) {
11431168
$public_id = ! is_admin() ? $relation['public_id'] . '.' . $relation['format'] : null;
11441169
// Get merged transformations including overlays.
11451170
$merged_transformations = Relate::get_transformations( $relation['post_id'], true );
1146-
$cloudinary_url = $this->media->cloudinary_url( $relation['post_id'], array(), $merged_transformations, $public_id );
1171+
1172+
$size = $this->get_image_size_within_tags( $tags, $relation );
1173+
$cloudinary_url = $this->media->cloudinary_url( $relation['post_id'], $size, $merged_transformations, $public_id );
11471174
if ( empty( $cloudinary_url ) ) {
11481175
continue;
11491176
}
@@ -1281,6 +1308,15 @@ protected function standardize_tag( $tag_element ) {
12811308
$size = $has_wp_size;
12821309
}
12831310
}
1311+
1312+
// Retrieve size from the parent figure class of the image if it exists.
1313+
if ( ! empty( $tag_element['atts']['data-size-slug'] ) && 'img' === $tag_element['tag'] ) {
1314+
$slug_size = $this->get_size_from_slug( $tag_element['atts']['data-size-slug'] );
1315+
if ( ! empty( $slug_size ) ) {
1316+
$size = $slug_size;
1317+
}
1318+
}
1319+
12841320
// Unset srcset and sizes.
12851321
unset( $tag_element['atts']['srcset'], $tag_element['atts']['sizes'] );
12861322

@@ -1616,7 +1652,17 @@ public function parse_element( $element, $content = '' ) {
16161652

16171653
// If no size found in URL, try to extract from parent figure element so we can apply cropping if needed.
16181654
if ( empty( $has_size ) && $has_sized_transformation ) {
1619-
$has_size = $this->get_size_from_parent( $tag_element['original'], $content );
1655+
$size_slug = $this->get_size_slug_from_parent_figure_class( $tag_element['original'], $content );
1656+
1657+
if ( ! empty( $size_slug ) ) {
1658+
$has_size = $this->get_size_from_slug( $size_slug );
1659+
if ( ! empty( $has_size ) ) {
1660+
$attributes['data-size-slug'] = $size_slug;
1661+
$tag_element['width'] = $has_size[0];
1662+
$tag_element['height'] = $has_size[1];
1663+
1664+
}
1665+
}
16201666
}
16211667

16221668
if ( ! empty( $has_size ) && ! empty( $item['height'] ) ) {

0 commit comments

Comments
 (0)