| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Data\Services; |
| 4 |
|
| 5 |
use SyncBasalam\Services\FileUploader; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class PhotoService |
| 10 |
{ |
| 11 |
private $fileUploader; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->fileUploader = new FileUploader(); |
| 16 |
} |
| 17 |
public function getMainPhotoId($product): ?int |
| 18 |
{ |
| 19 |
$mainImageId = $product->get_image_id(); |
| 20 |
if (!$mainImageId) return null; |
| 21 |
|
| 22 |
$existingPhoto = $this->getExistingPhoto($mainImageId); |
| 23 |
if ($existingPhoto) return $existingPhoto['file_id']; |
| 24 |
|
| 25 |
$uploadedPhoto = $this->uploadPhoto($mainImageId); |
| 26 |
if ($uploadedPhoto) { |
| 27 |
$this->storePhotoRecord($mainImageId, $uploadedPhoto); |
| 28 |
return $uploadedPhoto['file_id']; |
| 29 |
} |
| 30 |
|
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
public function getGalleryPhotoIds($product) |
| 35 |
{ |
| 36 |
$galleryPhotoIds = []; |
| 37 |
$galleryImageIds = $product->get_gallery_image_ids(); |
| 38 |
|
| 39 |
if (empty($galleryImageIds)) return []; |
| 40 |
|
| 41 |
// Limit to 10 photos maximum |
| 42 |
$galleryImageIds = array_slice($galleryImageIds, 0, 10); |
| 43 |
foreach ($galleryImageIds as $index => $imageId) { |
| 44 |
|
| 45 |
$existingPhoto = $this->getExistingPhoto($imageId); |
| 46 |
|
| 47 |
if ($existingPhoto) { |
| 48 |
$galleryPhotoIds[] = $existingPhoto['file_id']; |
| 49 |
} else { |
| 50 |
$uploadedPhoto = $this->uploadPhoto($imageId); |
| 51 |
if ($uploadedPhoto) { |
| 52 |
$this->storePhotoRecord($imageId, $uploadedPhoto); |
| 53 |
$galleryPhotoIds[] = $uploadedPhoto['file_id']; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $galleryPhotoIds; |
| 59 |
} |
| 60 |
|
| 61 |
private function uploadPhoto(int $imageId) |
| 62 |
{ |
| 63 |
$imagePathOrUrl = $this->getImagePathOrUrl($imageId); |
| 64 |
if (!$imagePathOrUrl) return null; |
| 65 |
|
| 66 |
try { |
| 67 |
$data = $this->fileUploader->upload($imagePathOrUrl); |
| 68 |
return $data; |
| 69 |
} catch (\Exception $e) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
private function getImagePathOrUrl(int $imageId): ?string |
| 75 |
{ |
| 76 |
$url = wp_get_attachment_url($imageId); |
| 77 |
$path = get_attached_file($imageId); |
| 78 |
|
| 79 |
if ($path && file_exists($path)) return $path; |
| 80 |
|
| 81 |
return $url; |
| 82 |
} |
| 83 |
|
| 84 |
private function getExistingPhoto(int $wooPhotoId) |
| 85 |
{ |
| 86 |
global $wpdb; |
| 87 |
$tableName = $wpdb->prefix . 'sync_basalam_uploaded_photo'; |
| 88 |
|
| 89 |
$result = $wpdb->get_row($wpdb->prepare( |
| 90 |
"SELECT sync_basalam_photo_id AS file_id, sync_basalam_photo_url AS url, created_at |
| 91 |
FROM $tableName |
| 92 |
WHERE woo_photo_id = %d", |
| 93 |
$wooPhotoId |
| 94 |
), ARRAY_A); |
| 95 |
|
| 96 |
if (!$result) return null; |
| 97 |
|
| 98 |
// Check if photo is older than 14 days |
| 99 |
$createdAt = strtotime($result['created_at']); |
| 100 |
$now = current_time('timestamp'); |
| 101 |
$fourteenDays = 14 * DAY_IN_SECONDS; |
| 102 |
|
| 103 |
$age = $now - $createdAt; |
| 104 |
|
| 105 |
if ($age >= $fourteenDays) { |
| 106 |
$wpdb->delete($tableName, ['woo_photo_id' => $wooPhotoId], ['%d']); |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
return $result; |
| 111 |
} |
| 112 |
|
| 113 |
private function storePhotoRecord(int $wooPhotoId, array $basalamPhoto): void |
| 114 |
{ |
| 115 |
global $wpdb; |
| 116 |
$tableName = $wpdb->prefix . 'sync_basalam_uploaded_photo'; |
| 117 |
|
| 118 |
$insertData = [ |
| 119 |
'woo_photo_id' => $wooPhotoId, |
| 120 |
'sync_basalam_photo_id' => $basalamPhoto['file_id'], |
| 121 |
'sync_basalam_photo_url' => $basalamPhoto['url'], |
| 122 |
'created_at' => current_time('mysql'), |
| 123 |
]; |
| 124 |
|
| 125 |
$wpdb->insert($tableName, $insertData, ['%d', '%d', '%s', '%s']); |
| 126 |
} |
| 127 |
} |