Files
ladill-woo-manager/app/Http/Controllers/Woo/ConnectWordPressController.php
T
isaaccladandCursor cebf0d2e61
Deploy Ladill Woo Manager / deploy (push) Successful in 51s
Add Woo Manager Pro with multi-store switching and free-tier limits.
Free: 1 store and 20 products. Pro/Business mirrors Invoice pricing (GHS 49/149)
with wallet renewals, Paystack prepay, session-based store switcher, and scoped UI.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 01:14:08 +00:00

113 lines
3.7 KiB
PHP

<?php
namespace App\Http\Controllers\Woo;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Woo\Concerns\ResolvesWooContext;
use App\Http\Controllers\Woo\Concerns\ResolvesWooContext;
use App\Models\WooStore;
use App\Services\Woo\InstallTokenService;
use App\Services\Woo\SubscriptionService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Support\CurrentWooStore;
use Illuminate\View\View;
class ConnectWordPressController extends Controller
{
use ResolvesWooContext;
public function __construct(
private InstallTokenService $tokens,
private SubscriptionService $subscriptions,
) {}
public function start(Request $request): RedirectResponse|View
{
$validated = $request->validate([
'site_url' => ['required', 'url', 'max:500'],
'return_url' => ['required', 'url', 'max:2048'],
'site_name' => ['nullable', 'string', 'max:160'],
]);
$siteUrl = $this->normalizeSiteUrl($validated['site_url']);
if (! $this->isAllowedReturnUrl($validated['return_url'], $siteUrl)) {
abort(422, 'Return URL must belong to the WooCommerce site.');
}
$request->session()->put('woo.connect', [
'site_url' => $siteUrl,
'return_url' => $validated['return_url'],
'site_name' => $validated['site_name'] ?? null,
]);
if (! $request->user()) {
return redirect()->route('sso.connect', [
'redirect' => $request->fullUrl(),
'interactive' => 1,
]);
}
return $this->complete($request);
}
public function complete(Request $request): RedirectResponse|View
{
$pending = (array) $request->session()->pull('woo.connect', []);
if ($pending === []) {
return redirect()->route('woo.dashboard');
}
$user = $request->user();
abort_if(! $user, 401);
$siteUrl = (string) ($pending['site_url'] ?? '');
$returnUrl = (string) ($pending['return_url'] ?? '');
if (! $this->subscriptions->canConnectStore($user, $siteUrl)) {
return redirect()->route('woo.pro.index')
->with('upsell', 'Free plan includes '.(int) config('woo.free.max_stores', 1).' store. Upgrade to Pro to connect more WooCommerce sites.');
}
$store = WooStore::query()->updateOrCreate(
[
'user_id' => $user->id,
'site_url' => $siteUrl,
],
[
'site_name' => $pending['site_name'] ?? parse_url($siteUrl, PHP_URL_HOST),
'return_url' => $returnUrl,
'status' => WooStore::STATUS_PENDING,
],
);
$issued = $this->tokens->issue($store);
$separator = str_contains($returnUrl, '?') ? '&' : '?';
session([CurrentWooStore::SESSION_KEY => $store->id]);
return redirect()->away($returnUrl.$separator.http_build_query([
'ladill_connect' => '1',
'install_token' => $issued['token'],
'store_id' => $store->public_id,
]));
}
private function normalizeSiteUrl(string $url): string
{
$parsed = parse_url($url);
$scheme = $parsed['scheme'] ?? 'https';
$host = strtolower((string) ($parsed['host'] ?? ''));
return rtrim($scheme.'://'.$host, '/');
}
private function isAllowedReturnUrl(string $returnUrl, string $siteUrl): bool
{
$returnHost = strtolower((string) (parse_url($returnUrl, PHP_URL_HOST) ?: ''));
$siteHost = strtolower((string) (parse_url($siteUrl, PHP_URL_HOST) ?: ''));
return $returnHost !== '' && $returnHost === $siteHost;
}
}