PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.20
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.20
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Services / Async / ImageAttachService.php

ImageAttachService.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.20, at app/Services/Async/ImageAttachService.php

135 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 FluentCart\App\Services\Async;
4
5 use FluentCart\App\CPT\FluentProducts;
6 use FluentCart\App\Models\DynamicModel;
7 use FluentCart\App\Models\Product;
8 use FluentCart\App\Models\ProductVariation;
9 use FluentCart\Framework\Support\Str;
10
11 class ImageAttachService
12 {
13
14 public function __construct()
15 {
16 $this->requireFiles();
17 }
18
19 public static function attachImageToProduct(array $product, array $images)
20 {
21
22 if (empty($images)) {
23 return;
24 }
25
26 $gallery = [];
27 $instance = new ImageAttachService();
28
29 foreach ($images as $image) {
30 $value = $instance->addImageFromUrl($image, $product['post_title']);
31 if (!empty($value)) {
32 $gallery[] = $value;
33 }
34 }
35
36 if (!empty($gallery)) {
37 update_post_meta($product['ID'], FluentProducts::CPT_NAME . '-gallery-image', $gallery);
38 }
39
40 }
41
42 public static function attachImageToVariant($variantId, array $images)
43 {
44 $variant = ProductVariation::query()->find($variantId);
45 if (empty($variant)) {
46 return;
47 }
48
49 if (empty($images)) {
50 return;
51 }
52
53 $instance = new ImageAttachService();
54
55 $metaValue = [];
56 foreach ($images as $image) {
57 $value = $instance->addImageFromUrl($image, $variant['variation_title']);
58 if (!empty($value)) {
59 $metaValue[] = $value;
60 }
61
62 }
63 if (!empty($metaValue)) {
64 $media = [
65 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
66 'meta_key' => 'product_thumbnail',
67 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
68 'meta_value' => $metaValue,
69 'object_id' => $variant['id'],
70 'object_type' => 'product_variant_info'
71
72 ];
73 $variant->media()->create($media);
74 }
75
76 }
77
78 protected function addImageFromUrl($url, $title): array
79 {
80 $uploadUrl = wp_upload_dir()['baseurl'];
81 $uploadUrl = Str::of($uploadUrl)->replace('http://', '')->replace('https://', '')->toString();
82 $tmpUrl = Str::of($url)->replace('http://', '')->replace('https://', '')->toString();
83
84 $localImageAttachment = null;
85 if (Str::startsWith($tmpUrl, $uploadUrl)) {
86 $localImageAttachment = $this->prepareAttachmentForJsFromUrl($url);
87 }
88
89 if (!empty($localImageAttachment)) {
90 return $localImageAttachment;
91 }
92
93 $attachment_id = media_sideload_image($url, 0, $title, 'id');
94
95 if ($attachment_id instanceof \WP_Error) {
96 return [];
97 }
98 $media = wp_prepare_attachment_for_js($attachment_id);
99 return [
100 'id' => $attachment_id,
101 'title' => $media['title'],
102 'url' => $media['url']
103 ];
104 }
105
106 public function prepareAttachmentForJsFromUrl(string $url): ?array
107 {
108 $url = Str::of($url)->replace('http://', '')->replace('https://', '')->toString();
109 $attachment = (new DynamicModel([], 'posts'))
110 ->newQuery()
111 ->where('post_type', 'attachment')
112 ->where('guid', 'LIKE', '%' . $url)
113 ->orderByDesc('ID')
114 ->first();
115
116
117 if (!empty($attachment)) {
118 return [
119 'id' => $attachment->ID,
120 'title' => $attachment->post_title,
121 'url' => $attachment->guid
122 ];
123 }
124 return null;
125 }
126
127 protected function requireFiles()
128 {
129 if (!function_exists('media_handle_upload')) {
130 require_once(ABSPATH . 'wp-admin/includes/image.php');
131 require_once(ABSPATH . 'wp-admin/includes/file.php');
132 require_once(ABSPATH . 'wp-admin/includes/media.php');
133 }
134 }
135 }