Skip to content

Commit 09a8d92

Browse files
Pick up size for cropping from img parent figure class name
1 parent 7cf3446 commit 09a8d92

File tree

1 file changed

+70
-2
lines changed

1 file changed

+70
-2
lines changed

php/class-delivery.php

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,62 @@ public function get_media_tags( $content, $tags = 'img|video' ) {
10061006
return $media;
10071007
}
10081008

1009+
/**
1010+
* Get the size dimensions from the parent figure class if available.
1011+
*
1012+
* @param string $element The img tag element.
1013+
* @param string $content The full HTML content.
1014+
*
1015+
* @return array|null An array with width and height, or null if not found.
1016+
*/
1017+
private function get_size_from_parent( $element, $content ) {
1018+
$size_slug = $this->get_size_slug_from_parent_figure_class( $element, $content );
1019+
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;
1023+
}
1024+
1025+
// Get the dimensions of the WordPress size from options.
1026+
$size_width = get_option( $size_slug . '_size_w' );
1027+
$size_height = get_option( $size_slug . '_size_h' );
1028+
1029+
// Check if we have valid dimensions. If not, return null to indicate no specific size.
1030+
if ( empty( $size_width ) || empty( $size_height ) ) {
1031+
return null;
1032+
}
1033+
1034+
return array( (int) $size_width, (int) $size_height );
1035+
}
1036+
1037+
/**
1038+
* Extract WordPress image size from parent figure element.
1039+
*
1040+
* @param string $element The img tag element.
1041+
* @param string $content The full HTML content.
1042+
*
1043+
* @return string|null The WordPress size slug (e.g., 'thumbnail', 'medium'), or null if not found.
1044+
*/
1045+
private function get_size_slug_from_parent_figure_class( $element, $content ) {
1046+
// If content is empty, we can't find a parent figure, so return null.
1047+
if ( empty( $content ) ) {
1048+
return null;
1049+
}
1050+
1051+
// Escape the element for use in regex.
1052+
$escaped_element = preg_quote( $element, '#' );
1053+
1054+
// Pattern: <figure class="...size-{size}...">...img element...[optional figcaption]</figure> .
1055+
$pattern = '#<figure\s+[^>]*class="[^"]*\bsize-(\w+)\b[^"]*"[^>]*>.*?' . $escaped_element . '.*?</figure>#is';
1056+
1057+
// Look for the parent figure tag that contains this img element.
1058+
if ( preg_match( $pattern, $content, $matches ) ) {
1059+
return $matches[1];
1060+
}
1061+
1062+
return null;
1063+
}
1064+
10091065
/**
10101066
* Convert media tags from Local to Cloudinary, and register with String_Replace.
10111067
*
@@ -1025,7 +1081,12 @@ public function convert_tags( $content, $context = 'view' ) {
10251081
}
10261082

10271083
$tags = $this->get_media_tags( $content, 'img|video|article|source' );
1028-
$tags = array_map( array( $this, 'parse_element' ), $tags );
1084+
$tags = array_map(
1085+
function ( $tag ) use ( $content ) {
1086+
return $this->parse_element( $tag, $content );
1087+
},
1088+
$tags
1089+
);
10291090
$tags = array_filter( $tags );
10301091

10311092
$replacements = array();
@@ -1437,10 +1498,11 @@ public function rebuild_tag( $tag_element ) {
14371498
* Parse an html element into tag, and attributes.
14381499
*
14391500
* @param string $element The HTML element.
1501+
* @param string $content Optional full HTML content for parent context.
14401502
*
14411503
* @return array|null
14421504
*/
1443-
public function parse_element( $element ) {
1505+
public function parse_element( $element, $content = '' ) {
14441506
/**
14451507
* Filter to skip parsing an element.
14461508
*
@@ -1551,6 +1613,12 @@ public function parse_element( $element ) {
15511613
if ( in_array( $tag_element['tag'], array( 'img', 'source' ), true ) ) {
15521614
// Check if this is a crop or a scale.
15531615
$has_size = $this->media->get_size_from_url( $this->sanitize_url( $raw_url ) );
1616+
1617+
// If no size found in URL, try to extract from parent figure element so we can apply cropping if needed.
1618+
if ( empty( $has_size ) && $has_sized_transformation ) {
1619+
$has_size = $this->get_size_from_parent( $tag_element['original'], $content );
1620+
}
1621+
15541622
if ( ! empty( $has_size ) && ! empty( $item['height'] ) ) {
15551623
$file_ratio = round( $has_size[0] / $has_size[1], 2 );
15561624
$original_ratio = round( $item['width'] / $item['height'], 2 );

0 commit comments

Comments
 (0)