content-builder.php
Construtor de Conteúdo SEO - Strategya AI News Generator
Descrição
Classe responsável por construir e otimizar conteúdo jornalístico com práticas avançadas de SEO e EEAT (Experience, Expertise, Authoritativeness, Trustworthiness).
Funcionalidades
- Estruturação de conteúdo com H2/H3 otimizados
- Geração automática de meta descriptions
- Criação de excerpts otimizados
- Análise e sugestão de palavras-chave
- Formatação para EEAT compliance
- Validação de estrutura SEO
<?php
if (!defined('ABSPATH')) {
exit;
}
class Strategya_Content_Builder {
private $seo_keywords = [];
private $content_structure = [];
private $language = 'pt-BR';
public function __construct() {
$this->init_hooks();
}
private function init_hooks() {
add_filter('strategya_build_content', [$this, 'build_article_content'], 10, 2);
add_filter('strategya_optimize_seo', [$this, 'optimize_for_seo'], 10, 2);
}
public function build_article_content($ai_content, $params = []) {
try {
if (empty($ai_content)) {
throw new Exception('Conteúdo IA não pode estar vazio');
}
$structured_content = $this->parse_ai_content($ai_content);
$optimized_content = $this->optimize_content_structure($structured_content);
$post_data = [
'title' => $this->extract_title($optimized_content),
'content' => $this->format_content($optimized_content),
'excerpt' => $this->generate_excerpt($optimized_content),
'meta_description' => $this->generate_meta_description($optimized_content),
'tags' => $this->extract_tags($optimized_content),
'seo_score' => $this->calculate_seo_score($optimized_content)
];
return $post_data;
} catch (Exception $e) {
error_log('Strategya Content Builder Error: ' . $e->getMessage());
return false;
}
}
private function parse_ai_content($content) {
$structure = [];
if (preg_match('/^#\s*(.+)$/m', $content, $matches)) {
$structure['title'] = trim($matches[1]);
}
preg_match_all('/^##\s*(.+)$/m', $content, $h2_matches);
$structure['h2_headings'] = $h2_matches[1];
preg_match_all('/^###\s*(.+)$/m', $content, $h3_matches);
$structure['h3_headings'] = $h3_matches[1];
$paragraphs = explode("\n\n", $content);
$structure['paragraphs'] = array_filter($paragraphs, function($p) {
return !empty(trim($p)) && !preg_match('/^#{1,3}\s/', $p);
});
preg_match_all('/^[\*\-]\s*(.+)$/m', $content, $list_matches);
$structure['lists'] = $list_matches[1];
$structure['raw_content'] = $content;
return $structure;
}
private function optimize_content_structure($structure) {
if (isset($structure['title'])) {
$structure['title'] = $this->optimize_title($structure['title']);
}
if (isset($structure['h2_headings'])) {
$structure['h2_headings'] = array_map([$this, 'optimize_heading'], $structure['h2_headings']);
}
if (isset($structure['paragraphs'])) {
$structure['paragraphs'] = $this->optimize_paragraphs($structure['paragraphs']);
}
$structure['keywords'] = $this->extract_keywords($structure['raw_content']);
return $structure;
}
private function optimize_title($title) {
$title = preg_replace('/[^\w\s\-\u00C0-\u017F]/u', '', $title);
if (strlen($title) > 60) {
$title = substr($title, 0, 57) . '...';
}
if (strpos(strtolower($title), 'como') !== false) {
$title = rtrim($title, '.') . ' | Guia Completo';
}
return trim($title);
}
private function optimize_heading($heading) {
$heading = preg_replace('/[^\w\s\-\u00C0-\u017F]/u', '', $heading);
$heading = ucwords(strtolower($heading));
return trim($heading);
}
private function optimize_paragraphs($paragraphs) {
$optimized = [];
foreach ($paragraphs as $paragraph) {
$paragraph = trim($paragraph);
if (empty($paragraph)) continue;
$sentences = preg_split('/[.!?]+/', $paragraph);
$sentences = array_filter($sentences, function($s) {
return !empty(trim($s));
});
if (count($sentences) >= 2) {
$optimized[] = $paragraph;
}
}
return $optimized;
}
private function extract_title($content) {
return isset($content['title']) ? $content['title'] : 'Título não disponível';
}
private function format_content($content) {
$html = '';
if (isset($content['paragraphs'])) {
foreach ($content['paragraphs'] as $paragraph) {
$html .= '<p>' . wp_kses_post($paragraph) . '</p>';
}
}
if (isset($content['h2_headings'])) {
foreach ($content['h2_headings'] as $heading) {
$html .= '<h2>' . wp_kses_post($heading) . '</h2>';
}
}
if (isset($content['lists']) && !empty($content['lists'])) {
$html .= '<ul>';
foreach ($content['lists'] as $item) {
$html .= '<li>' . wp_kses_post($item) . '</li>';
}
$html .= '</ul>';
}
return $html;
}
private function generate_excerpt($content) {
$excerpt = '';
if (isset($content['paragraphs']) && !empty($content['paragraphs'])) {
$first_paragraph = $content['paragraphs'][0];
$excerpt = wp_trim_words($first_paragraph, 25, '...');
}
if (strlen($excerpt) > 160) {
$excerpt = substr($excerpt, 0, 157) . '...';
}
return $excerpt;
}
private function generate_meta_description($content) {
$meta_desc = '';
if (isset($content['paragraphs']) && !empty($content['paragraphs'])) {
$first_paragraph = $content['paragraphs'][0];
$meta_desc = wp_trim_words($first_paragraph, 20, '');
}
if (strlen($meta_desc) > 155) {
$meta_desc = substr($meta_desc, 0, 152) . '...';
}
return $meta_desc;
}
private function extract_tags($content) {
$tags = [];
if (isset($content['keywords'])) {
$tags = array_slice($content['keywords'], 0, 10);
}
$default_tags = ['japao', 'estrangeiros', 'comunidade', 'noticias'];
$tags = array_merge($tags, $default_tags);
return array_unique($tags);
}
private function extract_keywords($content) {
$clean_content = strtolower(preg_replace('/[^\w\s\u00C0-\u017F]/u', ' ', $content));
$words = preg_split('/\s+/', $clean_content);
$stopwords = ['de', 'da', 'do', 'em', 'um', 'uma', 'com', 'por', 'para', 'que', 'como', 'mais', 'muito', 'ser', 'ter', 'seu', 'sua'];
$filtered_words = array_filter($words, function($word) use ($stopwords) {
return strlen($word) >= 4 && !in_array($word, $stopwords);
});
$word_count = array_count_values($filtered_words);
arsort($word_count);
return array_slice(array_keys($word_count), 0, 15);
}
private function calculate_seo_score($content) {
$score = 0;
if (isset($content['title']) && strlen($content['title']) >= 30) {
$score += 20;
}
if (isset($content['h2_headings']) && count($content['h2_headings']) >= 2) {
$score += 20;
}
if (isset($content['paragraphs']) && count($content['paragraphs']) >= 5) {
$score += 20;
}
$content_length = strlen($content['raw_content']);
if ($content_length >= 1500) {
$score += 20;
} elseif ($content_length >= 1000) {
$score += 10;
}
if (isset($content['keywords']) && count($content['keywords']) >= 10) {
$score += 20;
}
return min($score, 100);
}
public function optimize_for_eeat($content, $author_info = []) {
if (isset($content['raw_content'])) {
$content['raw_content'] = $this->add_credibility_signals($content['raw_content']);
}
if (!empty($author_info)) {
$content['author_info'] = $author_info;
}
$content['publish_date'] = current_time('mysql');
return $content;
}
private function add_credibility_signals($content) {
$experience_phrases = [
'Com base em nossa experiência',
'Segundo especialistas',
'De acordo com dados oficiais',
'Conforme relatado por fontes confiáveis'
];
$content .= "\n\n" . '## Fontes e Referências' . "\n";
$content .= 'Este artigo foi baseado em informações de fontes oficiais e confiáveis.';
return $content;
}
public function set_language($language) {
$this->language = $language;
}
public function get_language() {
return $this->language;
}
}
new Strategya_Content_Builder();