Deploy Ladill Woo Manager / deploy (push) Successful in 24s
Sync full image sets through the plugin, with upload/URL support in the product editor and thumbnails in the list. Co-authored-by: Cursor <cursoragent@cursor.com>
107 lines
3.1 KiB
PHP
107 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Woo;
|
|
|
|
use App\Models\WooStore;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
class ProductMediaService
|
|
{
|
|
public function __construct(private PluginClient $client) {}
|
|
|
|
/** @return list<array{id?: int, src: string, alt?: string, position?: int}> */
|
|
public function buildImagesFromRequest(Request $request, WooStore $store): array
|
|
{
|
|
/** @var list<array<string, mixed>> $images */
|
|
$images = json_decode((string) $request->input('product_images', '[]'), true) ?? [];
|
|
if (! is_array($images)) {
|
|
$images = [];
|
|
}
|
|
|
|
if ($request->hasFile('featured_image_upload')) {
|
|
$uploaded = $this->upload($store, $request->file('featured_image_upload'), (string) $request->input('featured_image_alt', ''));
|
|
if ($uploaded !== null) {
|
|
$images[0] = $uploaded;
|
|
}
|
|
} elseif ($src = trim((string) $request->input('featured_image_src', ''))) {
|
|
$images[0] = [
|
|
'src' => $src,
|
|
'alt' => trim((string) $request->input('featured_image_alt', '')),
|
|
];
|
|
}
|
|
|
|
foreach ($request->file('gallery_uploads', []) as $file) {
|
|
if ($file instanceof UploadedFile && $file->isValid()) {
|
|
$uploaded = $this->upload($store, $file, '');
|
|
if ($uploaded !== null) {
|
|
$images[] = $uploaded;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ((array) $request->input('gallery_image_srcs', []) as $src) {
|
|
if (! is_string($src)) {
|
|
continue;
|
|
}
|
|
$src = trim($src);
|
|
if ($src !== '') {
|
|
$images[] = ['src' => $src, 'alt' => ''];
|
|
}
|
|
}
|
|
|
|
return $this->normalizeImages($images);
|
|
}
|
|
|
|
/** @return array{id: int, src: string, alt: string}|null */
|
|
public function upload(WooStore $store, UploadedFile $file, string $alt = ''): ?array
|
|
{
|
|
$response = $this->client->uploadMedia($store, $file, $alt);
|
|
|
|
if (! is_array($response) || empty($response['src'])) {
|
|
return null;
|
|
}
|
|
|
|
return [
|
|
'id' => (int) ($response['id'] ?? 0),
|
|
'src' => (string) $response['src'],
|
|
'alt' => (string) ($response['alt'] ?? $alt),
|
|
];
|
|
}
|
|
|
|
/** @param list<array<string, mixed>> $images */
|
|
private function normalizeImages(array $images): array
|
|
{
|
|
$normalized = [];
|
|
|
|
foreach (array_values($images) as $position => $image) {
|
|
if (! is_array($image)) {
|
|
continue;
|
|
}
|
|
|
|
$src = trim((string) ($image['src'] ?? ''));
|
|
$id = (int) ($image['id'] ?? 0);
|
|
|
|
if ($src === '' && $id <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$row = [
|
|
'alt' => trim((string) ($image['alt'] ?? '')),
|
|
'position' => $position,
|
|
];
|
|
|
|
if ($id > 0) {
|
|
$row['id'] = $id;
|
|
}
|
|
if ($src !== '') {
|
|
$row['src'] = $src;
|
|
}
|
|
|
|
$normalized[] = $row;
|
|
}
|
|
|
|
return $normalized;
|
|
}
|
|
}
|