| 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 |
} |