@@ -21,8 +21,8 @@ class SiteGenImageService {
21
21
*/
22
22
public static function process_homepage_images_immediate_async ( $ post_id , $ content ) {
23
23
// 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
+
26
26
if ( empty ( $ image_urls ) ) {
27
27
return ;
28
28
}
@@ -37,6 +37,33 @@ public static function process_homepage_images_immediate_async( $post_id, $conte
37
37
}
38
38
}
39
39
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
+
40
67
/**
41
68
* Uploads images to the WordPress media library as attachments.
42
69
*
@@ -57,7 +84,7 @@ public static function upload_images_to_wp_media_library( $image_urls, $post_id
57
84
self ::connect_to_filesystem ();
58
85
59
86
$ uploaded_image_urls = array ();
60
- $ total_images = count ( $ image_urls );
87
+ $ total_images = count ( $ image_urls );
61
88
$ successful_uploads = 0 ;
62
89
63
90
try {
@@ -177,6 +204,19 @@ public static function update_post_content_with_new_image_urls( $post_id, $url_m
177
204
if ( ! empty ( $ new_url ) ) {
178
205
// Use str_replace for exact URL replacement
179
206
$ 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
+
180
220
if ( $ new_content !== $ content ) {
181
221
$ content = $ new_content ;
182
222
$ updated = true ;
0 commit comments