| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\Data\Services; |
| 4 |
|
| 5 |
use SyncBasalam\Logger\Logger; |
| 6 |
use SyncBasalam\Services\FileUploader; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
|
| 10 |
class PhotoService |
| 11 |
{ |
| 12 |
private $fileUploader; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
$this->fileUploader = new FileUploader(); |
| 17 |
} |
| 18 |
|
| 19 |
public function getMainPhotoId($product): ?int |
| 20 |
{ |
| 21 |
$mainImageId = $product->get_image_id(); |
| 22 |
if (!$mainImageId) return null; |
| 23 |
|
| 24 |
$existingPhoto = $this->getExistingPhoto($mainImageId); |
| 25 |
if ($existingPhoto) return $existingPhoto['file_id']; |
| 26 |
|
| 27 |
$uploadedPhoto = $this->uploadPhoto($mainImageId); |
| 28 |
if ($uploadedPhoto) { |
| 29 |
$this->storePhotoRecord($mainImageId, $uploadedPhoto); |
| 30 |
return $uploadedPhoto['file_id']; |
| 31 |
} |
| 32 |
|
| 33 |
return null; |
| 34 |
} |
| 35 |
|
| 36 |
public function getGalleryPhotoIds($product) |
| 37 |
{ |
| 38 |
$galleryPhotoIds = []; |
| 39 |
$galleryImageIds = $product->get_gallery_image_ids(); |
| 40 |
|
| 41 |
if (empty($galleryImageIds)) return []; |
| 42 |
|
| 43 |
$galleryImageIds = array_slice($galleryImageIds, 0, 20); |
| 44 |
foreach ($galleryImageIds as $imageId) { |
| 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 |
return $this->fileUploader->upload($imagePathOrUrl); |
| 68 |
} catch (\Throwable $e) { |
| 69 |
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Message is escaped; $e is the previous Throwable passed for exception chaining, not output. |
| 70 |
throw new \RuntimeException(esc_html('آپلود تصویر � |
| 71 |
حصول به باسلا� |
| 72 |
نا� |
| 73 |
وفق بود: ' . $e->getMessage()), 0, $e); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
private function getImagePathOrUrl(int $imageId): ?string |
| 78 |
{ |
| 79 |
$url = wp_get_attachment_url($imageId); |
| 80 |
$path = get_attached_file($imageId); |
| 81 |
|
| 82 |
if ($path && file_exists($path)) return $path; |
| 83 |
|
| 84 |
return $url; |
| 85 |
} |
| 86 |
|
| 87 |
private function getExistingPhoto(int $wooPhotoId) |
| 88 |
{ |
| 89 |
global $wpdb; |
| 90 |
$tableName = $wpdb->prefix . 'sync_basalam_uploaded_media'; |
| 91 |
$sourceIdentity = 'attachment:' . $wooPhotoId; |
| 92 |
|
| 93 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom plugin table; identifier from $wpdb->prefix, not user input. |
| 94 |
$result = $wpdb->get_row($wpdb->prepare( |
| 95 |
"SELECT media_id AS file_id, media_url AS url, created_at |
| 96 |
FROM $tableName |
| 97 |
WHERE type = %s AND source_identity = %s", |
| 98 |
'photo', |
| 99 |
$sourceIdentity |
| 100 |
), ARRAY_A); |
| 101 |
|
| 102 |
if (!$result) return null; |
| 103 |
|
| 104 |
$createdAt = strtotime($result['created_at']); |
| 105 |
$now = current_time('timestamp'); |
| 106 |
$fourteenDays = 14 * DAY_IN_SECONDS; |
| 107 |
|
| 108 |
$age = $now - $createdAt; |
| 109 |
|
| 110 |
if ($age >= $fourteenDays) { |
| 111 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table; no object cache for these operational queries. |
| 112 |
$wpdb->delete( |
| 113 |
$tableName, |
| 114 |
['type' => 'photo', 'source_identity' => $sourceIdentity], |
| 115 |
['%s', '%s'] |
| 116 |
); |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
return $result; |
| 121 |
} |
| 122 |
|
| 123 |
private function storePhotoRecord(int $wooPhotoId, array $basalamPhoto): void |
| 124 |
{ |
| 125 |
global $wpdb; |
| 126 |
$tableName = $wpdb->prefix . 'sync_basalam_uploaded_media'; |
| 127 |
|
| 128 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table; no object cache for these operational queries. |
| 129 |
$wpdb->replace( |
| 130 |
$tableName, |
| 131 |
[ |
| 132 |
'type' => 'photo', |
| 133 |
'source_identity' => 'attachment:' . $wooPhotoId, |
| 134 |
'media_id' => (int) $basalamPhoto['file_id'], |
| 135 |
'media_url' => $basalamPhoto['url'] ?? '', |
| 136 |
'created_at' => current_time('mysql'), |
| 137 |
], |
| 138 |
['%s', '%s', '%d', '%s', '%s'] |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|