Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ImageSideloadTask | |
0.00% |
0 / 17 |
|
0.00% |
0 / 5 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| get_post_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_image_urls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_id | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Onboarding\Tasks; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Onboarding\Services\SiteGenImageService; |
| 6 | |
| 7 | /** |
| 8 | * Task for sideloading images to WordPress media library |
| 9 | */ |
| 10 | class ImageSideloadTask { |
| 11 | |
| 12 | /** |
| 13 | * The post ID to attach images to |
| 14 | * |
| 15 | * @var int |
| 16 | */ |
| 17 | private $post_id; |
| 18 | |
| 19 | /** |
| 20 | * Array of image URLs to sideload |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | private $image_urls; |
| 25 | |
| 26 | /** |
| 27 | * Constructor |
| 28 | * |
| 29 | * @param int $post_id The post ID to attach images to |
| 30 | * @param array $image_urls Array of image URLs to sideload |
| 31 | */ |
| 32 | public function __construct( $post_id, $image_urls ) { |
| 33 | $this->post_id = $post_id; |
| 34 | $this->image_urls = $image_urls; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Execute the task |
| 39 | * |
| 40 | * @return bool|\WP_Error True on success, WP_Error on failure |
| 41 | */ |
| 42 | public function execute() { |
| 43 | try { |
| 44 | // Upload images to WordPress media library |
| 45 | $result = SiteGenImageService::upload_images_to_wp_media_library( $this->image_urls, $this->post_id ); |
| 46 | |
| 47 | if ( is_wp_error( $result ) ) { |
| 48 | return $result; |
| 49 | } |
| 50 | |
| 51 | if ( false === $result ) { |
| 52 | return new \WP_Error( 'upload_failed', 'Image upload returned false' ); |
| 53 | } |
| 54 | |
| 55 | // Update post content with new image URLs |
| 56 | if ( ! empty( $result ) ) { |
| 57 | $content_updated = SiteGenImageService::update_post_content_with_new_image_urls( $this->post_id, $result ); |
| 58 | if ( ! $content_updated ) { |
| 59 | return new \WP_Error( 'content_update_failed', 'Failed to update post content with new image URLs' ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } catch ( \Exception $e ) { |
| 65 | return new \WP_Error( 'image_sideload_failed', $e->getMessage() ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the post ID |
| 71 | * |
| 72 | * @return int |
| 73 | */ |
| 74 | public function get_post_id() { |
| 75 | return $this->post_id; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the image URLs |
| 80 | * |
| 81 | * @return array |
| 82 | */ |
| 83 | public function get_image_urls() { |
| 84 | return $this->image_urls; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get a unique identifier for this task |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public function get_id() { |
| 93 | return 'image_sideload_' . $this->post_id . '_' . md5( maybe_serialize( $this->image_urls ) ); |
| 94 | } |
| 95 | } |