PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.9.1
ووسلام – همگام سازی ووکامرس و باسلام v1.9.1
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / includes / Admin / Product / Data / Services / PhotoService.php

PhotoService.php in ووسلام – همگام سازی ووکامرس و باسلام 1.9.1, at includes/Admin/Product/Data/Services/PhotoService.php

138 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 throw new \RuntimeException('آپلود تصویر �
70 حصول به باسلا�
71 نا�
72 وفق بود: ' . $e->getMessage(), 0, $e);
73 }
74 }
75
76 private function getImagePathOrUrl(int $imageId): ?string
77 {
78 $url = wp_get_attachment_url($imageId);
79 $path = get_attached_file($imageId);
80
81 if ($path && file_exists($path)) return $path;
82
83 return $url;
84 }
85
86 private function getExistingPhoto(int $wooPhotoId)
87 {
88 global $wpdb;
89 $tableName = $wpdb->prefix . 'sync_basalam_uploaded_media';
90 $sourceIdentity = 'attachment:' . $wooPhotoId;
91
92 $result = $wpdb->get_row($wpdb->prepare(
93 "SELECT media_id AS file_id, media_url AS url, created_at
94 FROM $tableName
95 WHERE type = %s AND source_identity = %s",
96 'photo',
97 $sourceIdentity
98 ), ARRAY_A);
99
100 if (!$result) return null;
101
102 $createdAt = strtotime($result['created_at']);
103 $now = current_time('timestamp');
104 $fourteenDays = 14 * DAY_IN_SECONDS;
105
106 $age = $now - $createdAt;
107
108 if ($age >= $fourteenDays) {
109 $wpdb->delete(
110 $tableName,
111 ['type' => 'photo', 'source_identity' => $sourceIdentity],
112 ['%s', '%s']
113 );
114 return null;
115 }
116
117 return $result;
118 }
119
120 private function storePhotoRecord(int $wooPhotoId, array $basalamPhoto): void
121 {
122 global $wpdb;
123 $tableName = $wpdb->prefix . 'sync_basalam_uploaded_media';
124
125 $wpdb->replace(
126 $tableName,
127 [
128 'type' => 'photo',
129 'source_identity' => 'attachment:' . $wooPhotoId,
130 'media_id' => (int) $basalamPhoto['file_id'],
131 'media_url' => $basalamPhoto['url'] ?? '',
132 'created_at' => current_time('mysql'),
133 ],
134 ['%s', '%s', '%d', '%s', '%s']
135 );
136 }
137 }
138