diff --git a/includes/RestApi/AppController.php b/includes/RestApi/AppController.php index 3fa6b4a16..894a9e48e 100644 --- a/includes/RestApi/AppController.php +++ b/includes/RestApi/AppController.php @@ -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 { /** @@ -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, @@ -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.' ), diff --git a/includes/Services/SiteGenImageService.php b/includes/Services/SiteGenImageService.php index 446eb1905..74a9b0bef 100644 --- a/includes/Services/SiteGenImageService.php +++ b/includes/Services/SiteGenImageService.php @@ -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( '/]+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; } @@ -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. * @@ -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 { @@ -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;