Add WooCommerce product and category management.
Deploy Ladill Woo Manager / deploy (push) Successful in 55s
Deploy Ladill Woo Manager / deploy (push) Successful in 55s
Sync catalog via plugin webhooks and REST proxy, with list/create/edit UI and updated logo from monolith assets. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Woo;
|
||||
|
||||
use App\Models\WooStore;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PluginClient
|
||||
{
|
||||
/** @return array<string, mixed>|null */
|
||||
public function get(WooStore $store, string $path, array $query = []): ?array
|
||||
{
|
||||
return $this->request($store, 'GET', $path, $query);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $payload */
|
||||
public function post(WooStore $store, string $path, array $payload = []): ?array
|
||||
{
|
||||
return $this->request($store, 'POST', $path, $payload);
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $payload */
|
||||
public function put(WooStore $store, string $path, array $payload = []): ?array
|
||||
{
|
||||
return $this->request($store, 'PUT', $path, $payload);
|
||||
}
|
||||
|
||||
public function delete(WooStore $store, string $path): bool
|
||||
{
|
||||
return $this->raw($store, 'DELETE', $path)->successful();
|
||||
}
|
||||
|
||||
/** @return array<string, mixed>|null */
|
||||
private function request(WooStore $store, string $method, string $path, array $data = []): ?array
|
||||
{
|
||||
$response = $this->raw($store, $method, $path, $data);
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::warning('Woo plugin API request failed', [
|
||||
'store_id' => $store->id,
|
||||
'method' => $method,
|
||||
'path' => $path,
|
||||
'status' => $response->status(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var array<string, mixed>|null $json */
|
||||
$json = $response->json();
|
||||
|
||||
return is_array($json) ? $json : null;
|
||||
}
|
||||
|
||||
private function raw(WooStore $store, string $method, string $path, array $data = []): Response
|
||||
{
|
||||
$site = rtrim((string) $store->site_url, '/');
|
||||
$url = $site.'/wp-json/ladill-woo/v1/'.ltrim($path, '/');
|
||||
|
||||
$pending = Http::acceptJson()
|
||||
->timeout(20)
|
||||
->withHeaders(['X-Ladill-Store' => $store->public_id]);
|
||||
|
||||
return match (strtoupper($method)) {
|
||||
'GET' => $pending->get($url, $data),
|
||||
'POST' => $pending->asJson()->post($url, $data),
|
||||
'PUT' => $pending->asJson()->put($url, $data),
|
||||
'DELETE' => $pending->delete($url, $data),
|
||||
default => throw new \InvalidArgumentException("Unsupported method: {$method}"),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user