first commit

This commit is contained in:
2026-02-15 11:37:27 +01:00
commit 884ee9cc88
299 changed files with 41102 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
<?php
namespace App\Services;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use App\Helpers\Logger;
class WordPressService
{
private Client $client;
public function __construct()
{
$this->client = new Client(['timeout' => 30]);
}
public function testConnection(array $site): array
{
try {
$response = $this->client->get($site['url'] . '/wp-json/wp/v2/posts', [
'auth' => [$site['api_user'], $site['api_token']],
'query' => ['per_page' => 1],
]);
if ($response->getStatusCode() === 200) {
return ['success' => true, 'message' => 'Połączenie OK'];
}
return ['success' => false, 'message' => 'Nieoczekiwany kod: ' . $response->getStatusCode()];
} catch (GuzzleException $e) {
Logger::error("WP test connection failed for {$site['url']}: " . $e->getMessage(), 'wordpress');
return ['success' => false, 'message' => 'Błąd: ' . $e->getMessage()];
}
}
public function getCategories(array $site): array|false
{
try {
$response = $this->client->get($site['url'] . '/wp-json/wp/v2/categories', [
'auth' => [$site['api_user'], $site['api_token']],
'query' => ['per_page' => 100],
]);
return json_decode($response->getBody()->getContents(), true);
} catch (GuzzleException $e) {
Logger::error("WP getCategories failed for {$site['url']}: " . $e->getMessage(), 'wordpress');
return false;
}
}
public function uploadMedia(array $site, string $imageData, string $filename): ?int
{
try {
$response = $this->client->post($site['url'] . '/wp-json/wp/v2/media', [
'auth' => [$site['api_user'], $site['api_token']],
'headers' => [
'Content-Disposition' => "attachment; filename=\"{$filename}\"",
'Content-Type' => $this->getMimeType($filename),
],
'body' => $imageData,
]);
$data = json_decode($response->getBody()->getContents(), true);
return $data['id'] ?? null;
} catch (GuzzleException $e) {
Logger::error("WP uploadMedia failed for {$site['url']}: " . $e->getMessage(), 'wordpress');
return null;
}
}
public function createPost(
array $site,
string $title,
string $content,
?int $categoryId = null,
?int $mediaId = null
): ?int {
try {
$postData = [
'title' => $title,
'content' => $content,
'status' => 'publish',
];
if ($categoryId) {
$postData['categories'] = [$categoryId];
}
if ($mediaId) {
$postData['featured_media'] = $mediaId;
}
$response = $this->client->post($site['url'] . '/wp-json/wp/v2/posts', [
'auth' => [$site['api_user'], $site['api_token']],
'json' => $postData,
]);
$data = json_decode($response->getBody()->getContents(), true);
return $data['id'] ?? null;
} catch (GuzzleException $e) {
Logger::error("WP createPost failed for {$site['url']}: " . $e->getMessage(), 'wordpress');
return null;
}
}
private function getMimeType(string $filename): string
{
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
return match ($ext) {
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
default => 'image/jpeg',
};
}
}