Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inline background image url rewriting #5

Merged
merged 1 commit into from
Dec 18, 2024
Merged
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
58 changes: 58 additions & 0 deletions includes/classes/ThumborImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,44 @@ public static function parse_images_from_html( $content ) {
return [];
}

/**
* Match all elements with background-image styles in a block of HTML.
*
* @param string $content Some HTML.
* @return array An array of matches, where each match includes:
* - 'element' (full matched element string),
* - 'background_image' (the extracted background image URL),
* - 'attributes' (optional context such as id, class).
*/
public static function parse_background_images_from_html( $content ) {
$background_images = [];

if ( preg_match_all(
'#<(?P<element>[a-zA-Z][^>]*?)style=["\'][^>]*?background-image\s*:\s*url\s*\(\s*(?P<background_image>[^\s)]+)\s*\)[^>]*?["\'][^>]*?>#is',
$content,
$matches
) ) {
foreach ( $matches['background_image'] as $index => $background_image ) {
$attributes = [];

if ( preg_match( '#class=["\'](?P<class>[^"\']+)["\']#i', $matches[0][$index], $class_match ) ) {
$attributes['class'] = $class_match['class'];
}
if ( preg_match( '#id=["\'](?P<id>[^"\']+)["\']#i', $matches[0][$index], $id_match ) ) {
$attributes['id'] = $id_match['id'];
}

$background_images[] = [
'element' => $matches[0][$index],
'background_image' => $background_image,
'attributes' => $attributes,
];
}
}

return $background_images;
}

/**
* Try to determine height and width from strings WP appends to resized image filenames.
*
Expand Down Expand Up @@ -151,6 +189,7 @@ public static function parse_dimensions_from_filename( $src ) {
*/
public static function filter_the_content( $content ) {
$images = static::parse_images_from_html( $content );
$background_images = static::parse_background_images_from_html( $content );

if ( ! empty( $images ) ) {
$content_width = isset( $GLOBALS['content_width'] ) ? $GLOBALS['content_width'] : false;
Expand Down Expand Up @@ -513,6 +552,25 @@ function ( $v ) {
}
}

if ( ! empty( $background_images ) ) {
foreach ( $background_images as $bg_image ) {
$src = $bg_image['background_image'];
$element = $bg_image['element'];

// Validate background image URL.
if ( self::validate_image_url( $src ) ) {
// Generate Thumbor URL.
$thumbor_url = thumbor_url( $src );

// Replace the original background image URL in the element.
$updated_element = str_replace( $src, esc_url( $thumbor_url ), $element );

// Replace the original element in the content.
$content = str_replace( $element, $updated_element, $content );
}
}
}

return $content;
}

Expand Down