mirror of
https://gh.wpcy.net/https://github.com/fairpm/aspirecloud.git
synced 2026-06-20 02:22:28 +08:00
65 lines
2.3 KiB
PHP
65 lines
2.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\API\WpOrg\Themes;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\WpOrg\Theme;
|
|
use App\Utils\Regex;
|
|
use App\Values\WpOrg\Themes\ThemeUpdateCheckRequest;
|
|
use App\Values\WpOrg\Themes\ThemeUpdateCheckResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class ThemeUpdatesController extends Controller
|
|
{
|
|
public function __invoke(Request $request): JsonResponse|Response
|
|
{
|
|
try {
|
|
$req = ThemeUpdateCheckRequest::from($request);
|
|
|
|
$isValid = fn($item) => !isset($item['UpdateURI'])
|
|
|| !$item['UpdateURI']
|
|
|| Regex::match('!(?:https?://)?(?:wordpress\.org|w\.org)/themes?/!', $item['UpdateURI']);
|
|
|
|
$reqThemes = collect($req->themes)->filter($isValid);
|
|
|
|
/** @noinspection PhpParamsInspection (fails to parse the full signature for Collection::partition) */
|
|
$themes = Theme::query()
|
|
->whereIn('slug', $reqThemes->keys())
|
|
->get()
|
|
->partition(fn(Theme $theme) => version_compare(
|
|
$theme->version,
|
|
$reqThemes[$theme->slug]['Version'] ?? null,
|
|
'>',
|
|
));
|
|
|
|
return $this->sendResponse(ThemeUpdateCheckResponse::fromResults($themes[0], $themes[1]));
|
|
} catch (ValidationException $e) {
|
|
// Handle validation errors and return a custom response
|
|
$firstErrorMessage = collect($e->errors())->flatten()->first();
|
|
return $this->sendResponse(['error' => $firstErrorMessage], 400);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send response based on API version.
|
|
*
|
|
* @param array<string,mixed>|ThemeUpdateCheckResponse $response
|
|
*/
|
|
private function sendResponse(
|
|
array|ThemeUpdateCheckResponse $response,
|
|
int $statusCode = 200,
|
|
): JsonResponse|Response {
|
|
$version = request()->route('version');
|
|
if ($version === '1.0') {
|
|
if ($response instanceof ThemeUpdateCheckResponse) {
|
|
$response = $response->toArray();
|
|
}
|
|
return response(serialize((object)$response), $statusCode);
|
|
}
|
|
return response()->json($response, $statusCode);
|
|
}
|
|
}
|