Skip to content

Commit 214812e

Browse files
Extract image size methods into media class
1 parent ad95a7c commit 214812e

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

php/class-delivery.php

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,63 +1030,14 @@ private function get_image_size_within_tags( $tags, $relation ) {
10301030

10311031
// If this tag's post ID matches our relation's post ID, get the size from the slug and return it.
10321032
if ( $post_id === $set['id'] ) {
1033-
$size = $this->get_size_from_slug( $set['atts']['data-size-slug'] );
1033+
$size = $this->media->get_size_from_slug( $set['atts']['data-size-slug'] );
10341034
return ( empty( $size ) ) ? array() : $size; // Return the size if found, otherwise return empty array.
10351035
}
10361036
}
10371037

10381038
return array();
10391039
}
10401040

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-
1050-
// Get the dimensions of the WordPress size from options.
1051-
$size_width = get_option( $size_slug . '_size_w' );
1052-
$size_height = get_option( $size_slug . '_size_h' );
1053-
1054-
// Check if we have valid dimensions. If not, return null to indicate no specific size.
1055-
if ( empty( $size_width ) || empty( $size_height ) ) {
1056-
return null;
1057-
}
1058-
1059-
return array( (int) $size_width, (int) $size_height );
1060-
}
1061-
1062-
/**
1063-
* Extract WordPress image size from parent figure element.
1064-
*
1065-
* @param string $element The img tag element.
1066-
* @param string $content The full HTML content.
1067-
*
1068-
* @return string|null The WordPress size slug (e.g., 'thumbnail', 'medium'), or null if not found.
1069-
*/
1070-
private function get_size_slug_from_parent_figure_class( $element, $content ) {
1071-
// If content is empty, we can't find a parent figure, so return null.
1072-
if ( empty( $content ) ) {
1073-
return null;
1074-
}
1075-
1076-
// Escape the element for use in regex.
1077-
$escaped_element = preg_quote( $element, '#' );
1078-
1079-
// Pattern: <figure class="...size-{size}...">...img element...[optional figcaption]</figure> .
1080-
$pattern = '#<figure\s+[^>]*class="[^"]*\bsize-(\w+)\b[^"]*"[^>]*>.*?' . $escaped_element . '.*?</figure>#is';
1081-
1082-
// Look for the parent figure tag that contains this img element.
1083-
if ( preg_match( $pattern, $content, $matches ) ) {
1084-
return $matches[1];
1085-
}
1086-
1087-
return null;
1088-
}
1089-
10901041
/**
10911042
* Convert media tags from Local to Cloudinary, and register with String_Replace.
10921043
*
@@ -1311,7 +1262,7 @@ protected function standardize_tag( $tag_element ) {
13111262

13121263
// Retrieve size from the parent figure class of the image if it exists.
13131264
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'] );
1265+
$slug_size = $this->media->get_size_from_slug( $tag_element['atts']['data-size-slug'] );
13151266
if ( ! empty( $slug_size ) ) {
13161267
$size = $slug_size;
13171268
}
@@ -1652,15 +1603,14 @@ public function parse_element( $element, $content = '' ) {
16521603

16531604
// If no size found in URL, try to extract from parent figure element so we can apply cropping if needed.
16541605
if ( empty( $has_size ) && $has_sized_transformation ) {
1655-
$size_slug = $this->get_size_slug_from_parent_figure_class( $tag_element['original'], $content );
1606+
$size_slug = $this->media->get_size_slug_from_parent_figure_class( $tag_element['original'], $content );
16561607

16571608
if ( ! empty( $size_slug ) ) {
1658-
$has_size = $this->get_size_from_slug( $size_slug );
1609+
$has_size = $this->media->get_size_from_slug( $size_slug );
16591610
if ( ! empty( $has_size ) ) {
16601611
$attributes['data-size-slug'] = $size_slug;
16611612
$tag_element['width'] = $has_size[0];
16621613
$tag_element['height'] = $has_size[1];
1663-
16641614
}
16651615
}
16661616
}

php/class-media.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,4 +3267,53 @@ public function upgrade_settings( $previous_version, $new_version ) {
32673267
$setting->save_value();
32683268
}
32693269
}
3270+
3271+
3272+
/**
3273+
* Get the size dimensions based on the size slug.
3274+
*
3275+
* @param string $size_slug The WordPress size slug (e.g., 'thumbnail', 'medium', 'large').
3276+
*
3277+
* @return array|null An array with width and height, or null if not found.
3278+
*/
3279+
public function get_size_from_slug( $size_slug ) {
3280+
// Get the dimensions of the WordPress size from options.
3281+
$size_width = get_option( $size_slug . '_size_w' );
3282+
$size_height = get_option( $size_slug . '_size_h' );
3283+
3284+
// Check if we have valid dimensions. If not, return null to indicate no specific size.
3285+
if ( empty( $size_width ) || empty( $size_height ) ) {
3286+
return null;
3287+
}
3288+
3289+
return array( (int) $size_width, (int) $size_height );
3290+
}
3291+
3292+
/**
3293+
* Extract WordPress image size from parent figure element.
3294+
*
3295+
* @param string $element The img tag element.
3296+
* @param string $content The full HTML content.
3297+
*
3298+
* @return string|null The WordPress size slug (e.g., 'thumbnail', 'medium'), or null if not found.
3299+
*/
3300+
public function get_size_slug_from_parent_figure_class( $element, $content ) {
3301+
// If content is empty, we can't find a parent figure, so return null.
3302+
if ( empty( $content ) ) {
3303+
return null;
3304+
}
3305+
3306+
// Escape the element for use in regex.
3307+
$escaped_element = preg_quote( $element, '#' );
3308+
3309+
// Pattern: <figure class="...size-{size}...">...img element...[optional figcaption]</figure> .
3310+
$pattern = '#<figure\s+[^>]*class="[^"]*\bsize-(\w+)\b[^"]*"[^>]*>.*?' . $escaped_element . '.*?</figure>#is';
3311+
3312+
// Look for the parent figure tag that contains this img element.
3313+
if ( preg_match( $pattern, $content, $matches ) ) {
3314+
return $matches[1];
3315+
}
3316+
3317+
return null;
3318+
}
32703319
}

0 commit comments

Comments
 (0)