Make the image optional for WordPress posts too
Some blog posts just don't have one. WordPressPublisher no longer rejects publishing a type=blog post without image_url — it uploads and attaches it as the featured image when present, and simply skips that step (no featured_media set) otherwise. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,10 +20,6 @@ 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') {
|
||||
@@ -34,20 +30,25 @@ class WordPressPublisher implements Publisher
|
||||
return PublishResult::rejected("Site '{$site->name}' is missing WordPress credentials.");
|
||||
}
|
||||
|
||||
$mediaId = $this->uploadFeaturedImage($post, $site);
|
||||
$payload = [
|
||||
'title' => $post->title,
|
||||
'content' => $post->body,
|
||||
'status' => 'publish',
|
||||
];
|
||||
|
||||
if ($mediaId instanceof PublishResult) {
|
||||
return $mediaId;
|
||||
if ($post->image_url) {
|
||||
$mediaId = $this->uploadFeaturedImage($post, $site);
|
||||
|
||||
if ($mediaId instanceof PublishResult) {
|
||||
return $mediaId;
|
||||
}
|
||||
|
||||
$payload['featured_media'] = $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,
|
||||
]);
|
||||
->post(rtrim($site->url, '/').'/wp-json/wp/v2/posts', $payload);
|
||||
} catch (ConnectionException $e) {
|
||||
return PublishResult::failed("could not connect to site: {$e->getMessage()}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user