2026-07-21 14:04:45 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Support\Publishing;
|
|
|
|
|
|
|
|
|
|
use App\Models\Post;
|
2026-07-21 20:03:25 +03:00
|
|
|
use App\Models\Site;
|
2026-07-21 14:04:45 +03:00
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
|
|
|
|
class WordPressPublisher implements Publisher
|
|
|
|
|
{
|
|
|
|
|
public function supports(Post $post): bool
|
|
|
|
|
{
|
|
|
|
|
return $post->type === 'blog';
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 21:24:49 +03:00
|
|
|
public function publish(Post $post, ?SocialAccount $socialAccount = null): PublishResult
|
2026-07-21 14:04:45 +03:00
|
|
|
{
|
|
|
|
|
if (! $post->title) {
|
|
|
|
|
return PublishResult::rejected("Post {$post->id} has no title, required to publish to WordPress.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$site = $post->site;
|
|
|
|
|
|
|
|
|
|
if ($site->cms_type !== 'wordpress') {
|
|
|
|
|
return PublishResult::rejected("Site '{$site->name}' is not configured as WordPress (cms_type={$site->cms_type}).");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! $site->wp_username || ! $site->wp_app_password) {
|
|
|
|
|
return PublishResult::rejected("Site '{$site->name}' is missing WordPress credentials.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 20:07:47 +03:00
|
|
|
$payload = [
|
|
|
|
|
'title' => $post->title,
|
|
|
|
|
'content' => $post->body,
|
|
|
|
|
'status' => 'publish',
|
|
|
|
|
];
|
2026-07-21 20:03:25 +03:00
|
|
|
|
2026-07-21 20:07:47 +03:00
|
|
|
if ($post->image_url) {
|
|
|
|
|
$mediaId = $this->uploadFeaturedImage($post, $site);
|
|
|
|
|
|
|
|
|
|
if ($mediaId instanceof PublishResult) {
|
|
|
|
|
return $mediaId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$payload['featured_media'] = $mediaId;
|
2026-07-21 20:03:25 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
try {
|
|
|
|
|
$response = Http::withBasicAuth($site->wp_username, $site->wp_app_password)
|
2026-07-21 20:07:47 +03:00
|
|
|
->post(rtrim($site->url, '/').'/wp-json/wp/v2/posts', $payload);
|
2026-07-21 14:04:45 +03:00
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
|
return PublishResult::failed("could not connect to site: {$e->getMessage()}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($response->failed()) {
|
|
|
|
|
return PublishResult::failed($response->json('message') ?? 'unexpected response from WordPress');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$link = $response->json('link');
|
|
|
|
|
|
|
|
|
|
if (! $link) {
|
|
|
|
|
return PublishResult::failed('unexpected response from WordPress: missing post link');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PublishResult::success($link);
|
|
|
|
|
}
|
2026-07-21 20:03:25 +03:00
|
|
|
|
|
|
|
|
private function uploadFeaturedImage(Post $post, Site $site): int|PublishResult
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$image = Http::get($post->image_url);
|
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
|
return PublishResult::failed("could not download image: {$e->getMessage()}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($image->failed()) {
|
|
|
|
|
return PublishResult::failed('could not download image: unexpected response from image URL');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$filename = basename(parse_url($post->image_url, PHP_URL_PATH) ?: '') ?: 'image.jpg';
|
|
|
|
|
$contentType = $image->header('Content-Type') ?: 'application/octet-stream';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$response = Http::withBasicAuth($site->wp_username, $site->wp_app_password)
|
|
|
|
|
->withBody($image->body(), $contentType)
|
|
|
|
|
->withHeaders(['Content-Disposition' => "attachment; filename=\"{$filename}\""])
|
|
|
|
|
->post(rtrim($site->url, '/').'/wp-json/wp/v2/media');
|
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
|
return PublishResult::failed("could not connect to site: {$e->getMessage()}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($response->failed()) {
|
|
|
|
|
return PublishResult::failed($response->json('message') ?? 'unexpected response from WordPress while uploading the image');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mediaId = $response->json('id');
|
|
|
|
|
|
|
|
|
|
if (! $mediaId) {
|
|
|
|
|
return PublishResult::failed('unexpected response from WordPress: missing media id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mediaId;
|
|
|
|
|
}
|
2026-07-21 14:04:45 +03:00
|
|
|
}
|