Skip to content

Commit 76608be

Browse files
committed
fixing image fetching and replacement
1. parsing images from unsplash and patterns.hiive.cloud 2. making sure the images are replaced in content as well
1 parent 177f1f2 commit 76608be

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

includes/Services/SiteGenImageService.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class SiteGenImageService {
2121
*/
2222
public static function process_homepage_images_immediate_async( $post_id, $content ) {
2323
// Extract image URLs from content
24-
preg_match_all( '/<img[^>]+src=["\']([^"\']+)["\']/i', $content, $matches );
25-
$image_urls = isset( $matches[1] ) ? $matches[1] : array();
24+
$image_urls = self::extract_all_image_urls( $content );
25+
2626
if ( empty( $image_urls ) ) {
2727
return;
2828
}
@@ -37,6 +37,33 @@ public static function process_homepage_images_immediate_async( $post_id, $conte
3737
}
3838
}
3939

40+
/**
41+
* Extract all image URLs from content specifically targeting Unsplash and patterns.hiive.cloud domains.
42+
*
43+
* @param string $content The content to extract image URLs from.
44+
* @return array Array of unique image URLs.
45+
*/
46+
private static function extract_all_image_urls( $content ) {
47+
$image_urls = array();
48+
49+
// Extract Unsplash images
50+
preg_match_all( '/https?:\/\/([^\/]+\.)?unsplash\.com\/[^\s"\'<>]+/i', $content, $matches );
51+
if ( isset( $matches[0] ) ) {
52+
$image_urls = array_merge( $image_urls, $matches[0] );
53+
}
54+
55+
// Extract patterns.hiive.cloud images
56+
preg_match_all( '/https?:\/\/patterns\.hiive\.cloud\/[^\s"\'<>]+/i', $content, $matches );
57+
if ( isset( $matches[0] ) ) {
58+
$image_urls = array_merge( $image_urls, $matches[0] );
59+
}
60+
61+
// Decode HTML entities in URLs to ensure proper replacement
62+
$image_urls = array_map( 'html_entity_decode', $image_urls );
63+
64+
return array_values( array_unique( $image_urls ) );
65+
}
66+
4067
/**
4168
* Uploads images to the WordPress media library as attachments.
4269
*
@@ -177,6 +204,19 @@ public static function update_post_content_with_new_image_urls( $post_id, $url_m
177204
if ( ! empty( $new_url ) ) {
178205
// Use str_replace for exact URL replacement
179206
$new_content = str_replace( $original_url, $new_url, $content );
207+
208+
// If no replacement happened, try with HTML entity encoded version
209+
if ( $new_content === $content ) {
210+
$encoded_url = htmlspecialchars( $original_url, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
211+
$new_content = str_replace( $encoded_url, $new_url, $content );
212+
}
213+
214+
// If still no replacement, try with double-encoded version (common in WordPress)
215+
if ( $new_content === $content ) {
216+
$double_encoded_url = htmlspecialchars( htmlspecialchars( $original_url, ENT_QUOTES | ENT_HTML5, 'UTF-8' ), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
217+
$new_content = str_replace( $double_encoded_url, $new_url, $content );
218+
}
219+
180220
if ( $new_content !== $content ) {
181221
$content = $new_content;
182222
$updated = true;

0 commit comments

Comments
 (0)