Add an image field, required for WordPress posts

Posts get a nullable image_url. WordPressPublisher now downloads it
and uploads it to WordPress's media endpoint before creating the
post, setting it as featured_media — and rejects publishing (without
touching status) if a blog post has no image set, mirroring the
existing title requirement. Every other network treats it as optional.

Not enforced at save time, same as title — you can still draft an
incomplete WordPress post, it just can't be published until an image
is set. Exposed through the UI form (with a thumbnail/""missing""
indicator in the table), the JSON API, and the Python CLI wrapper.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
m105
2026-07-21 20:03:25 +03:00
co-authored by Claude Sonnet 5
parent fd185ab813
commit 42abc658f2
14 changed files with 259 additions and 28 deletions
+1
View File
@@ -21,6 +21,7 @@ class StorePostRequest extends FormRequest
'title' => ['nullable', 'string', 'max:255'],
'body' => ['required', 'string'],
'target' => ['nullable', 'string', 'max:255'],
'image_url' => ['nullable', 'url', 'max:2048'],
'status' => ['sometimes', 'string', 'in:' . implode(',', Post::STATUSES)],
'scheduled_at' => ['nullable', 'date'],
];
+1
View File
@@ -21,6 +21,7 @@ class UpdatePostRequest extends FormRequest
'title' => ['nullable', 'string', 'max:255'],
'body' => ['sometimes', 'required', 'string'],
'target' => ['nullable', 'string', 'max:255'],
'image_url' => ['nullable', 'url', 'max:2048'],
'status' => ['sometimes', 'string', 'in:' . implode(',', Post::STATUSES)],
'scheduled_at' => ['nullable', 'date'],
'published_url' => ['nullable', 'string', 'max:255'],
+1
View File
@@ -17,6 +17,7 @@ class PostResource extends JsonResource
'title' => $this->title,
'body' => $this->body,
'target' => $this->target,
'image_url' => $this->image_url,
'status' => $this->status,
'scheduled_at' => $this->scheduled_at,
'published_url' => $this->published_url,
+6
View File
@@ -29,6 +29,8 @@ class Index extends Component
public string $target = '';
public string $imageUrl = '';
public string $status = 'draft';
public string $scheduledAt = '';
@@ -82,6 +84,7 @@ class Index extends Component
$this->title = (string) $post->title;
$this->body = $post->body;
$this->target = (string) $post->target;
$this->imageUrl = (string) $post->image_url;
$this->status = $post->status;
$this->scheduledAt = $post->scheduled_at?->format('Y-m-d\TH:i') ?? '';
$this->showForm = true;
@@ -96,6 +99,7 @@ class Index extends Component
'title' => ['nullable', 'string', 'max:255'],
'body' => ['required', 'string'],
'target' => ['nullable', 'string', 'max:255'],
'imageUrl' => ['nullable', 'url', 'max:2048'],
'status' => ['required', 'string', 'in:' . implode(',', Post::STATUSES)],
'scheduledAt' => ['nullable', 'date'],
]);
@@ -107,6 +111,7 @@ class Index extends Component
'title' => $validated['title'] ?: null,
'body' => $validated['body'],
'target' => $validated['target'] ?: null,
'image_url' => $validated['imageUrl'] ?: null,
'status' => $validated['status'],
'scheduled_at' => $validated['scheduledAt'] ?: null,
]);
@@ -129,6 +134,7 @@ class Index extends Component
$this->title = '';
$this->body = '';
$this->target = '';
$this->imageUrl = '';
$this->status = 'draft';
$this->scheduledAt = '';
$this->resetValidation();
+1
View File
@@ -24,6 +24,7 @@ class Post extends Model
'title',
'body',
'target',
'image_url',
'status',
'scheduled_at',
'published_url',
@@ -3,6 +3,7 @@
namespace App\Support\Publishing;
use App\Models\Post;
use App\Models\Site;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
@@ -19,6 +20,10 @@ class WordPressPublisher implements Publisher
return PublishResult::rejected("Post {$post->id} has no title, required to publish to WordPress.");
}
if (! $post->image_url) {
return PublishResult::rejected("Post {$post->id} has no image, required to publish to WordPress.");
}
$site = $post->site;
if ($site->cms_type !== 'wordpress') {
@@ -29,12 +34,19 @@ class WordPressPublisher implements Publisher
return PublishResult::rejected("Site '{$site->name}' is missing WordPress credentials.");
}
$mediaId = $this->uploadFeaturedImage($post, $site);
if ($mediaId instanceof PublishResult) {
return $mediaId;
}
try {
$response = Http::withBasicAuth($site->wp_username, $site->wp_app_password)
->post(rtrim($site->url, '/').'/wp-json/wp/v2/posts', [
'title' => $post->title,
'content' => $post->body,
'status' => 'publish',
'featured_media' => $mediaId,
]);
} catch (ConnectionException $e) {
return PublishResult::failed("could not connect to site: {$e->getMessage()}");
@@ -52,4 +64,41 @@ class WordPressPublisher implements Publisher
return PublishResult::success($link);
}
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;
}
}