PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
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 trunk 1.2.0 All 47 releases
fluent-cart / app / Services / Async / ImageAttachService.php

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

143 lines 4.1 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, bool $cdnMode = false)
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'], $cdnMode);
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, bool $cdnMode = false)
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'], $cdnMode);
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, bool $cdnMode = false): array
79 {
80 if ($cdnMode) {
81 return [
82 'id' => null,
83 'title' => $title ?: basename($url),
84 'url' => $url,
85 ];
86 }
87
88 $uploadUrl = wp_upload_dir()['baseurl'];
89 $uploadUrl = Str::of($uploadUrl)->replace('http://', '')->replace('https://', '')->toString();
90 $tmpUrl = Str::of($url)->replace('http://', '')->replace('https://', '')->toString();
91
92 $localImageAttachment = null;
93 if (Str::startsWith($tmpUrl, $uploadUrl)) {
94 $localImageAttachment = $this->prepareAttachmentForJsFromUrl($url);
95 }
96
97 if (!empty($localImageAttachment)) {
98 return $localImageAttachment;
99 }
100
101 $attachment_id = media_sideload_image($url, 0, $title, 'id');
102
103 if ($attachment_id instanceof \WP_Error) {
104 return [];
105 }
106 $media = wp_prepare_attachment_for_js($attachment_id);
107 return [
108 'id' => $attachment_id,
109 'title' => $media['title'],
110 'url' => $media['url']
111 ];
112 }
113
114 public function prepareAttachmentForJsFromUrl(string $url): ?array
115 {
116 $url = Str::of($url)->replace('http://', '')->replace('https://', '')->toString();
117 $attachment = (new DynamicModel([], 'posts'))
118 ->newQuery()
119 ->where('post_type', 'attachment')
120 ->where('guid', 'LIKE', '%' . $url)
121 ->orderByDesc('ID')
122 ->first();
123
124
125 if (!empty($attachment)) {
126 return [
127 'id' => $attachment->ID,
128 'title' => $attachment->post_title,
129 'url' => $attachment->guid
130 ];
131 }
132 return null;
133 }
134
135 protected function requireFiles()
136 {
137 if (!function_exists('media_handle_upload')) {
138 require_once(ABSPATH . 'wp-admin/includes/image.php');
139 require_once(ABSPATH . 'wp-admin/includes/file.php');
140 require_once(ABSPATH . 'wp-admin/includes/media.php');
141 }
142 }
143 }