|
6 | 6 | use NewfoldLabs\WP\Module\Onboarding\Tasks\ImageSideloadTask;
|
7 | 7 |
|
8 | 8 | /**
|
9 |
| - * ThemeGeneratorService for the onboarding module. |
| 9 | + * SiteGenImageService for the onboarding module. |
10 | 10 | *
|
11 | 11 | * Handles image processing and media library operations for the onboarding flow.
|
12 | 12 | */
|
13 |
| -class ThemeGeneratorService { |
| 13 | +class SiteGenImageService { |
14 | 14 |
|
15 | 15 | /**
|
16 | 16 | * 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
|
57 | 57 | self::connect_to_filesystem();
|
58 | 58 |
|
59 | 59 | $uploaded_image_urls = array();
|
| 60 | + $total_images = count( $image_urls ); |
| 61 | + $successful_uploads = 0; |
| 62 | + |
60 | 63 | try {
|
61 | 64 | foreach ( $image_urls as $image_url ) {
|
62 | 65 | // Check if the URL is valid.
|
@@ -142,15 +145,89 @@ public static function upload_images_to_wp_media_library( $image_urls, $post_id
|
142 | 145 | $attachment_url = null;
|
143 | 146 | }
|
144 | 147 | $uploaded_image_urls[ $image_url ] = $attachment_url;
|
| 148 | + $successful_uploads++; |
145 | 149 | }
|
146 | 150 | }
|
147 | 151 | } catch ( \Exception $e ) {
|
148 |
| - // Log error. |
| 152 | + // Log error silently |
149 | 153 | }
|
150 |
| - |
151 | 154 | return $uploaded_image_urls;
|
152 | 155 | }
|
153 | 156 |
|
| 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 | + |
154 | 231 | /**
|
155 | 232 | * Connect to the WordPress filesystem.
|
156 | 233 | *
|
|
0 commit comments