Skip to content

Commit ded67b2

Browse files
committed
rename service file and added the image replacement code
1 parent 60f48ae commit ded67b2

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

includes/Services/ThemeGeneratorService.php renamed to includes/Services/SiteGenImageService.php

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
use NewfoldLabs\WP\Module\Onboarding\Tasks\ImageSideloadTask;
77

88
/**
9-
* ThemeGeneratorService for the onboarding module.
9+
* SiteGenImageService for the onboarding module.
1010
*
1111
* Handles image processing and media library operations for the onboarding flow.
1212
*/
13-
class ThemeGeneratorService {
13+
class SiteGenImageService {
1414

1515
/**
1616
* Process homepage images immediately in background (non-blocking).
@@ -57,6 +57,9 @@ public static function upload_images_to_wp_media_library( $image_urls, $post_id
5757
self::connect_to_filesystem();
5858

5959
$uploaded_image_urls = array();
60+
$total_images = count( $image_urls );
61+
$successful_uploads = 0;
62+
6063
try {
6164
foreach ( $image_urls as $image_url ) {
6265
// Check if the URL is valid.
@@ -142,15 +145,89 @@ public static function upload_images_to_wp_media_library( $image_urls, $post_id
142145
$attachment_url = null;
143146
}
144147
$uploaded_image_urls[ $image_url ] = $attachment_url;
148+
$successful_uploads++;
145149
}
146150
}
147151
} catch ( \Exception $e ) {
148-
// Log error.
152+
// Log error silently
149153
}
150-
151154
return $uploaded_image_urls;
152155
}
153156

157+
/**
158+
* Update post content by replacing original image URLs with WordPress media library URLs.
159+
*
160+
* @param int $post_id The post ID to update.
161+
* @param array $url_mapping Array mapping original URLs to new WordPress URLs.
162+
* @return bool True on success, false on failure.
163+
*/
164+
public static function update_post_content_with_new_image_urls( $post_id, $url_mapping ) {
165+
// Get the current post content
166+
$post = get_post( $post_id );
167+
if ( ! $post ) {
168+
return false;
169+
}
170+
171+
$content = $post->post_content;
172+
$updated = false;
173+
$replaced_count = 0;
174+
175+
// Replace each original URL with the new WordPress URL
176+
foreach ( $url_mapping as $original_url => $new_url ) {
177+
if ( ! empty( $new_url ) ) {
178+
// Use str_replace for exact URL replacement
179+
$new_content = str_replace( $original_url, $new_url, $content );
180+
if ( $new_content !== $content ) {
181+
$content = $new_content;
182+
$updated = true;
183+
$replaced_count++;
184+
}
185+
}
186+
}
187+
188+
// Update the post if content changed
189+
if ( $updated ) {
190+
$update_result = wp_update_post( array(
191+
'ID' => $post_id,
192+
'post_content' => $content,
193+
) );
194+
195+
if ( is_wp_error( $update_result ) ) {
196+
return false;
197+
}
198+
199+
return true;
200+
}
201+
202+
return true; // No changes needed
203+
}
204+
205+
/**
206+
* Get the current status of image processing for a post.
207+
*
208+
* @param int $post_id The post ID to check.
209+
* @return array Status information including processed images and pending tasks.
210+
*/
211+
public static function get_image_processing_status( $post_id ) {
212+
$queue = \NewfoldLabs\WP\Module\Onboarding\TaskManagers\ImageSideloadTaskManager::get_queue();
213+
$stats = \NewfoldLabs\WP\Module\Onboarding\TaskManagers\ImageSideloadTaskManager::get_stats();
214+
215+
$post_tasks = array();
216+
foreach ( $queue as $task ) {
217+
if ( $task['post_id'] === $post_id ) {
218+
$post_tasks[] = $task;
219+
}
220+
}
221+
222+
return array(
223+
'post_id' => $post_id,
224+
'queue_status' => \NewfoldLabs\WP\Module\Onboarding\TaskManagers\ImageSideloadTaskManager::get_status(),
225+
'queue_stats' => $stats,
226+
'post_tasks' => $post_tasks,
227+
'post_task_count' => count( $post_tasks ),
228+
);
229+
}
230+
154231
/**
155232
* Connect to the WordPress filesystem.
156233
*

includes/Services/SiteGenService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SitePagesService;
66
use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService as LegacySiteGenService;
7-
use NewfoldLabs\WP\Module\Onboarding\Services\ThemeGeneratorService;
7+
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenImageService;
88
use NewfoldLabs\WP\Module\Onboarding\Services\ReduxStateService;
99

1010
/**
@@ -81,7 +81,7 @@ public function publish_homepage( string $selected_sitegen_homepage ) {
8181
}
8282

8383
// Process images immediately in background (non-blocking)
84-
ThemeGeneratorService::process_homepage_images_immediate_async( $post_id, $content );
84+
SiteGenImageService::process_homepage_images_immediate_async( $post_id, $content );
8585

8686
// Add the homepage to the site navigation.
8787
$this->add_page_to_navigation( $post_id, $title, get_permalink( $post_id ) );

includes/Tasks/ImageSideloadTask.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace NewfoldLabs\WP\Module\Onboarding\Tasks;
44

5-
use NewfoldLabs\WP\Module\Onboarding\Services\ThemeGeneratorService;
5+
use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenImageService;
66

77
/**
88
* Task for sideloading images to WordPress media library
@@ -42,12 +42,20 @@ public function __construct( $post_id, $image_urls ) {
4242
public function execute() {
4343
try {
4444
// Upload images to WordPress media library
45-
$result = ThemeGeneratorService::upload_images_to_wp_media_library( $this->image_urls, $this->post_id );
45+
$result = SiteGenImageService::upload_images_to_wp_media_library( $this->image_urls, $this->post_id );
4646

4747
if ( is_wp_error( $result ) ) {
4848
return $result;
4949
}
5050

51+
// Update post content with new image URLs
52+
if ( ! empty( $result ) ) {
53+
$content_updated = SiteGenImageService::update_post_content_with_new_image_urls( $this->post_id, $result );
54+
if ( ! $content_updated ) {
55+
return new \WP_Error( 'content_update_failed', 'Failed to update post content with new image URLs' );
56+
}
57+
}
58+
5159
return true;
5260
} catch ( \Exception $e ) {
5361
return new \WP_Error( 'image_sideload_failed', $e->getMessage() );

0 commit comments

Comments
 (0)