Surface Google, Pinterest, Snapchat, and shipping status from the WordPress plugin with deep links to finish setup in WooCommerce. Co-authored-by: Cursor <cursoragent@cursor.com>
115 lines
3.9 KiB
PHP
115 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Woo;
|
|
|
|
use App\Models\WooStore;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class IntegrationStatusService
|
|
{
|
|
public function __construct(private PluginClient $client) {}
|
|
|
|
/**
|
|
* @return array{
|
|
* store: ?WooStore,
|
|
* reachable: bool,
|
|
* integrations: list<array<string, mixed>>
|
|
* }
|
|
*/
|
|
public function forStore(?WooStore $store): array
|
|
{
|
|
$catalog = config('woo_integrations', []);
|
|
|
|
if (! $store) {
|
|
return [
|
|
'store' => null,
|
|
'reachable' => false,
|
|
'integrations' => $this->catalogOnly($catalog),
|
|
];
|
|
}
|
|
|
|
$remote = $this->client->get($store, 'integrations/status');
|
|
$reachable = is_array($remote);
|
|
|
|
if (! $reachable) {
|
|
Log::info('Woo integration status unavailable', ['store_id' => $store->id]);
|
|
}
|
|
|
|
$remoteItems = is_array($remote['integrations'] ?? null) ? $remote['integrations'] : [];
|
|
|
|
$integrations = [];
|
|
foreach ($catalog as $key => $meta) {
|
|
$remoteStatus = is_array($remoteItems[$key] ?? null) ? $remoteItems[$key] : [];
|
|
$integrations[] = $this->mergeIntegration($key, $meta, $remoteStatus, $store, $reachable);
|
|
}
|
|
|
|
return [
|
|
'store' => $store,
|
|
'reachable' => $reachable,
|
|
'integrations' => $integrations,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $meta
|
|
* @param array<string, mixed> $remoteStatus
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function mergeIntegration(string $key, array $meta, array $remoteStatus, WooStore $store, bool $reachable): array
|
|
{
|
|
$state = $reachable
|
|
? (string) ($remoteStatus['state'] ?? 'unknown')
|
|
: 'unavailable';
|
|
|
|
$manageUrl = $remoteStatus['manage_url'] ?? null;
|
|
if (is_string($manageUrl) && $manageUrl !== '' && ! str_starts_with($manageUrl, 'http')) {
|
|
$manageUrl = rtrim((string) $store->site_url, '/').'/'.ltrim($manageUrl, '/');
|
|
}
|
|
|
|
return [
|
|
'key' => $key,
|
|
'name' => (string) ($meta['name'] ?? $key),
|
|
'description' => (string) ($meta['description'] ?? ''),
|
|
'category' => (string) ($meta['category'] ?? 'Integration'),
|
|
'install_url' => $meta['install_url'] ?? null,
|
|
'docs_url' => $meta['docs_url'] ?? null,
|
|
'state' => $state,
|
|
'installed' => (bool) ($remoteStatus['installed'] ?? false),
|
|
'connected' => (bool) ($remoteStatus['connected'] ?? false),
|
|
'summary' => $reachable
|
|
? (string) ($remoteStatus['summary'] ?? '')
|
|
: 'Could not reach your store. Check that the Ladill plugin is active.',
|
|
'manage_url' => is_string($manageUrl) && $manageUrl !== '' ? $manageUrl : null,
|
|
'details' => is_array($remoteStatus['details'] ?? null) ? $remoteStatus['details'] : [],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, array<string, mixed>> $catalog
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
private function catalogOnly(array $catalog): array
|
|
{
|
|
$integrations = [];
|
|
|
|
foreach ($catalog as $key => $meta) {
|
|
$integrations[] = [
|
|
'key' => $key,
|
|
'name' => (string) ($meta['name'] ?? $key),
|
|
'description' => (string) ($meta['description'] ?? ''),
|
|
'category' => (string) ($meta['category'] ?? 'Integration'),
|
|
'install_url' => $meta['install_url'] ?? null,
|
|
'docs_url' => $meta['docs_url'] ?? null,
|
|
'state' => 'no_store',
|
|
'installed' => false,
|
|
'connected' => false,
|
|
'summary' => 'Connect a WooCommerce store to see status.',
|
|
'manage_url' => null,
|
|
'details' => [],
|
|
];
|
|
}
|
|
|
|
return $integrations;
|
|
}
|
|
}
|