This commit is contained in:
2026-04-09 11:44:45 +02:00
parent 7ff7ff3a92
commit 61c66bfd55
79 changed files with 13667 additions and 144 deletions

View File

@@ -11,6 +11,7 @@ class PublisherService
{
private TopicBalancer $topicBalancer;
private OpenAIService $openAI;
private InternalLinkService $internalLinkService;
private ImageService $imageService;
private WordPressService $wordpress;
@@ -18,6 +19,7 @@ class PublisherService
{
$this->topicBalancer = new TopicBalancer();
$this->openAI = new OpenAIService();
$this->internalLinkService = new InternalLinkService();
$this->imageService = new ImageService();
$this->wordpress = new WordPressService();
}
@@ -103,6 +105,18 @@ class PublisherService
private function publishPreparedArticle(array $site, array $topic, array $article, ?int $existingArticleId = null): array
{
$linkingResult = $this->internalLinkService->enrichContentWithInternalLinks(
$site,
(string) ($article['title'] ?? ''),
(string) ($article['content'] ?? '')
);
$article['content'] = (string) ($linkingResult['content'] ?? (string) ($article['content'] ?? ''));
Logger::info(
'Internal linking mode=' . (string) ($linkingResult['mode'] ?? 'unknown')
. ', links_added=' . (int) ($linkingResult['links_added'] ?? 0),
'publish'
);
$imageUrl = null;
$mediaId = null;
$image = $this->imageService->generate((string) $article['title'], (string) $topic['name']);
@@ -122,7 +136,8 @@ class PublisherService
(string) $article['title'],
(string) $article['content'],
$topic['wp_category_id'],
$mediaId
$mediaId,
$this->buildExcerpt((string) $article['content'])
);
if (!$wpPostId) {
@@ -137,12 +152,14 @@ class PublisherService
}
Logger::info("Opublikowano post: wp_post_id={$wpPostId}", 'publish');
$wpPostUrl = $this->wordpress->getPostLink($site, (int) $wpPostId);
if ($existingArticleId !== null) {
Article::update($existingArticleId, [
'title' => (string) $article['title'],
'content' => (string) $article['content'],
'wp_post_id' => $wpPostId,
'wp_post_url' => $wpPostUrl,
'image_url' => $imageUrl,
'status' => 'published',
'ai_model' => $article['model'] ?? null,
@@ -157,6 +174,7 @@ class PublisherService
'title' => (string) $article['title'],
'content' => (string) $article['content'],
'wp_post_id' => $wpPostId,
'wp_post_url' => $wpPostUrl,
'image_url' => $imageUrl,
'status' => 'published',
'ai_model' => $article['model'] ?? null,
@@ -281,4 +299,27 @@ class PublisherService
return ['success' => true, 'message' => $message];
}
private function buildExcerpt(string $htmlContent): string
{
$plain = trim((string) preg_replace('/\s+/u', ' ', strip_tags($htmlContent)));
if ($plain === '') {
return '';
}
$maxLength = 155;
if (mb_strlen($plain) <= $maxLength) {
return $plain;
}
$cut = mb_substr($plain, 0, $maxLength + 1);
$lastSpace = mb_strrpos($cut, ' ');
if ($lastSpace !== false && $lastSpace > 80) {
$cut = mb_substr($cut, 0, $lastSpace);
} else {
$cut = mb_substr($cut, 0, $maxLength);
}
return rtrim($cut, " \t\n\r\0\x0B.,;:!?") . '.';
}
}