PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.8.5
ووسلام – همگام سازی ووکامرس و باسلام v1.8.5
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.8.5, at includes/Admin/Product/Data/Services/PhotoService.php

127 lines 3.7 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\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 }