2026-07-19 16:54:38 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use App\Models\Post;
|
2026-07-21 14:04:45 +03:00
|
|
|
use App\Support\Publishing\WordPressPublisher;
|
2026-07-19 16:54:38 +03:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
|
|
class PublishPost extends Command
|
|
|
|
|
{
|
|
|
|
|
protected $signature = 'posts:publish {post}';
|
|
|
|
|
|
|
|
|
|
protected $description = 'Publish an existing blog post to its site\'s WordPress REST API';
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
public function handle(WordPressPublisher $publisher): int
|
2026-07-19 16:54:38 +03:00
|
|
|
{
|
|
|
|
|
$post = Post::with('site')->find($this->argument('post'));
|
|
|
|
|
|
|
|
|
|
if (! $post) {
|
|
|
|
|
return $this->failWith("Post {$this->argument('post')} not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($post->type !== 'blog') {
|
|
|
|
|
return $this->failWith("Post {$post->id} has type '{$post->type}', expected 'blog' to publish to a CMS.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
$result = $publisher->publish($post);
|
|
|
|
|
|
|
|
|
|
if ($result->success) {
|
|
|
|
|
$post->update(['status' => 'published', 'published_url' => $result->url, 'publish_error' => null]);
|
|
|
|
|
$this->line(json_encode(['success' => true, 'url' => $result->url]));
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
2026-07-19 16:54:38 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
if ($result->attempted) {
|
|
|
|
|
$post->update(['status' => 'failed', 'publish_error' => $result->error]);
|
2026-07-19 16:54:38 +03:00
|
|
|
}
|
|
|
|
|
|
2026-07-21 14:04:45 +03:00
|
|
|
return $this->failWith($result->error);
|
2026-07-19 16:54:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function failWith(string $message): int
|
|
|
|
|
{
|
|
|
|
|
$this->line(json_encode(['success' => false, 'error' => $message]));
|
|
|
|
|
|
|
|
|
|
return self::FAILURE;
|
|
|
|
|
}
|
|
|
|
|
}
|