Skip to content

Commit c2e0491

Browse files
authored
Merge pull request #847 from newfold-labs/fix/ai-sitegen-sideload-images
Fix AI sitegen sideload images
2 parents 63d6947 + a0b136c commit c2e0491

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

includes/RestApi/AppController.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
use NewfoldLabs\WP\Module\Onboarding\Permissions;
66
use NewfoldLabs\WP\Module\Onboarding\Services\AppService;
77

8+
/**
9+
* AppController class for handling onboarding application REST API endpoints.
10+
*
11+
* This controller manages the REST API routes and handlers for the onboarding
12+
* application functionality. It provides endpoints for starting and completing
13+
* the onboarding process.
14+
*/
815
class AppController {
916

1017
/**
@@ -21,6 +28,13 @@ class AppController {
2128
*/
2229
protected $rest_base = '/app';
2330

31+
/**
32+
* Register the REST API routes for the onboarding application.
33+
*
34+
* Registers two main endpoints:
35+
* - /app/start: Initiates the onboarding process
36+
* - /app/complete: Completes the onboarding process with selected homepage
37+
*/
2438
public function register_routes() {
2539
\register_rest_route(
2640
$this->namespace,
@@ -74,7 +88,7 @@ public function start(): \WP_REST_Response {
7488
*/
7589
public function complete( \WP_REST_Request $request ): \WP_REST_Response {
7690
$data = json_decode( $request->get_body(), true );
77-
$selected_sitegen_homepage = $data['selected_sitegen_homepage'];
91+
$selected_sitegen_homepage = $data['selected_sitegen_homepage'] ?? false;
7892
if ( ! $selected_sitegen_homepage ) {
7993
return new \WP_REST_Response(
8094
array( 'error' => 'Selected sitegen homepage is required.' ),

includes/Services/SiteGenImageService.php

Lines changed: 43 additions & 3 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
*
@@ -57,7 +84,7 @@ public static function upload_images_to_wp_media_library( $image_urls, $post_id
5784
self::connect_to_filesystem();
5885

5986
$uploaded_image_urls = array();
60-
$total_images = count( $image_urls );
87+
$total_images = count( $image_urls );
6188
$successful_uploads = 0;
6289

6390
try {
@@ -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)