Skip to content

Fix AI sitegen sideload images #847

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

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion includes/RestApi/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
use NewfoldLabs\WP\Module\Onboarding\Permissions;
use NewfoldLabs\WP\Module\Onboarding\Services\AppService;

/**
* AppController class for handling onboarding application REST API endpoints.
*
* This controller manages the REST API routes and handlers for the onboarding
* application functionality. It provides endpoints for starting and completing
* the onboarding process.
*/
class AppController {

/**
Expand All @@ -21,6 +28,13 @@ class AppController {
*/
protected $rest_base = '/app';

/**
* Register the REST API routes for the onboarding application.
*
* Registers two main endpoints:
* - /app/start: Initiates the onboarding process
* - /app/complete: Completes the onboarding process with selected homepage
*/
public function register_routes() {
\register_rest_route(
$this->namespace,
Expand Down Expand Up @@ -74,7 +88,7 @@ public function start(): \WP_REST_Response {
*/
public function complete( \WP_REST_Request $request ): \WP_REST_Response {
$data = json_decode( $request->get_body(), true );
$selected_sitegen_homepage = $data['selected_sitegen_homepage'];
$selected_sitegen_homepage = $data['selected_sitegen_homepage'] ?? false;
if ( ! $selected_sitegen_homepage ) {
return new \WP_REST_Response(
array( 'error' => 'Selected sitegen homepage is required.' ),
Expand Down
46 changes: 43 additions & 3 deletions includes/Services/SiteGenImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class SiteGenImageService {
*/
public static function process_homepage_images_immediate_async( $post_id, $content ) {
// Extract image URLs from content
preg_match_all( '/<img[^>]+src=["\']([^"\']+)["\']/i', $content, $matches );
$image_urls = isset( $matches[1] ) ? $matches[1] : array();
$image_urls = self::extract_all_image_urls( $content );

if ( empty( $image_urls ) ) {
return;
}
Expand All @@ -37,6 +37,33 @@ public static function process_homepage_images_immediate_async( $post_id, $conte
}
}

/**
* Extract all image URLs from content specifically targeting Unsplash and patterns.hiive.cloud domains.
*
* @param string $content The content to extract image URLs from.
* @return array Array of unique image URLs.
*/
private static function extract_all_image_urls( $content ) {
$image_urls = array();

// Extract Unsplash images
preg_match_all( '/https?:\/\/([^\/]+\.)?unsplash\.com\/[^\s"\'<>]+/i', $content, $matches );
if ( isset( $matches[0] ) ) {
$image_urls = array_merge( $image_urls, $matches[0] );
}

// Extract patterns.hiive.cloud images
preg_match_all( '/https?:\/\/patterns\.hiive\.cloud\/[^\s"\'<>]+/i', $content, $matches );
if ( isset( $matches[0] ) ) {
$image_urls = array_merge( $image_urls, $matches[0] );
}

// Decode HTML entities in URLs to ensure proper replacement
$image_urls = array_map( 'html_entity_decode', $image_urls );

return array_values( array_unique( $image_urls ) );
}

/**
* Uploads images to the WordPress media library as attachments.
*
Expand All @@ -57,7 +84,7 @@ public static function upload_images_to_wp_media_library( $image_urls, $post_id
self::connect_to_filesystem();

$uploaded_image_urls = array();
$total_images = count( $image_urls );
$total_images = count( $image_urls );
$successful_uploads = 0;

try {
Expand Down Expand Up @@ -177,6 +204,19 @@ public static function update_post_content_with_new_image_urls( $post_id, $url_m
if ( ! empty( $new_url ) ) {
// Use str_replace for exact URL replacement
$new_content = str_replace( $original_url, $new_url, $content );

// If no replacement happened, try with HTML entity encoded version
if ( $new_content === $content ) {
$encoded_url = htmlspecialchars( $original_url, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
$new_content = str_replace( $encoded_url, $new_url, $content );
}

// If still no replacement, try with double-encoded version (common in WordPress)
if ( $new_content === $content ) {
$double_encoded_url = htmlspecialchars( htmlspecialchars( $original_url, ENT_QUOTES | ENT_HTML5, 'UTF-8' ), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
$new_content = str_replace( $double_encoded_url, $new_url, $content );
}

if ( $new_content !== $content ) {
$content = $new_content;
$updated = true;
Expand Down
Loading