/*
Plugin Name: Protección IA Content Booster 2.3
Plugin URI: https://thenext.social/
Description: Optimiza tu contenido para ser fuente de IA y voz, mejora visibilidad y estructura semántica automáticamente.
Version: 2.3
Author: Tu Nombre
Author URI: https://thenext.social/
License: GPL2
*/
// (1) Agregar esquema Article y FAQPage con respuestas reales multilinea
add_action('wp_head', function() {
if (is_single()) {
global $post;
$author_name = get_the_author_meta('display_name', $post->post_author);
$post_title = get_the_title($post);
$post_url = get_permalink($post);
$post_date = get_the_date('c', $post);
$modified_date = get_the_modified_date('c', $post);
$site_name = get_bloginfo('name');
$logo_url = get_site_icon_url();
$schema = [
"@context" => "https://schema.org",
"@type" => "Article",
"headline" => $post_title,
"datePublished" => $post_date,
"dateModified" => $modified_date,
"author" => ["@type" => "Person", "name" => $author_name],
"publisher" => [
"@type" => "Organization",
"name" => $site_name,
"logo" => ["@type" => "ImageObject", "url" => $logo_url]
],
"mainEntityOfPage" => ["@type" => "WebPage", "@id" => $post_url]
];
echo '';
$content = wp_strip_all_tags($post->post_content);
preg_match_all('/(?:¿)?(.*?)[?]/', $content, $questions);
if (!empty($questions[1])) {
$faq = ["@context" => "https://schema.org", "@type" => "FAQPage", "mainEntity" => []];
foreach ($questions[1] as $q) {
$pregunta_regex = '/¿' . preg_quote($q, '/') . '\?/'u;
if (preg_match($pregunta_regex, $content, $match, PREG_OFFSET_CAPTURE)) {
$inicio = $match[0][1] + strlen($match[0][0]);
$resto = substr($content, $inicio);
$fin = preg_match('/\n\s*\n|¿.*?\?/', $resto, $mfin, PREG_OFFSET_CAPTURE) ? $mfin[0][1] : strlen($resto);
$respuesta = trim(substr($resto, 0, $fin));
} else {
$respuesta = 'Consulta la respuesta completa en el artículo.';
}
$faq["mainEntity"][] = [
"@type" => "Question",
"name" => "¿" . trim($q) . "?",
"acceptedAnswer" => ["@type" => "Answer", "text" => $respuesta]
];
}
echo '';
}
}
});
// (2) Mostrar sugerencias dinámicas por preguntas
add_filter('the_content', function($content) {
if (!is_single()) return $content;
global $post;
$post_content = wp_strip_all_tags($post->post_content);
preg_match_all('/(?:¿)?(.*?)[?]/', $post_content, $questions);
$bloque_total = '';
if (!empty($questions[1])) {
foreach ($questions[1] as $pregunta_base) {
if (strlen($pregunta_base) < 10) continue;
$args = ['post_type' => 'post', 'posts_per_page' => 3, 'post__not_in' => [$post->ID], 's' => $pregunta_base];
$query = new WP_Query($args);
if ($query->have_posts()) {
$bloque = '
¿Te interesa saber más sobre: ' . esc_html($pregunta_base) . '?
';
wp_reset_postdata();
$bloque_total .= $bloque;
}
}
}
return $content . $bloque_total;
}, 30);
// (3) Escáner y etiquetas IA desde cero con barra de progreso
add_action('admin_menu', function() {
add_submenu_page('tools.php', 'Escanear Posts Antiguos IA', 'Escanear Posts IA', 'manage_options', 'escanear-posts-ia', 'escanear_posts_ia_page');
});
function escanear_posts_ia_page() {
if (!current_user_can('manage_options')) return;
if (isset($_POST['escanear_posts_ia'])) {
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'post_tag')");
$args = ['post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1];
$query = new WP_Query($args);
$total = $query->found_posts;
$procesados = $etiquetas_creadas = $posts_actualizados = 0;
if ($query->have_posts()) {
echo 'Procesando publicaciones...
';
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$content = wp_strip_all_tags(get_post_field('post_content', $post_id));
$conceptos = [];
if (preg_match_all('/(?:¿)?(.*?)[?]/', $content, $matches)) {
foreach ($matches[1] as $pregunta) {
$pregunta = trim($pregunta);
if (strlen($pregunta) >= 10 && strlen($pregunta) <= 100) {
$conceptos[] = $pregunta;
}
}
}
$conceptos = array_unique($conceptos);
$conceptos = array_slice($conceptos, 0, 5);
$nuevas_etiquetas = [];
foreach ($conceptos as $concepto) {
$term = term_exists($concepto, 'post_tag');
if (!$term) {
$new_term = wp_insert_term($concepto, 'post_tag');
if (!is_wp_error($new_term)) $etiquetas_creadas++;
}
$nuevas_etiquetas[] = $concepto;
}
if (!empty($nuevas_etiquetas)) {
wp_set_post_terms($post_id, $nuevas_etiquetas, 'post_tag', true);
$posts_actualizados++;
}
$procesados++;
echo '';
flush();
}
wp_reset_postdata();
echo '
';
}
echo '✅ Escaneo completo.
Posts procesados: ' . $procesados . '
Etiquetas nuevas creadas: ' . $etiquetas_creadas . '
Posts actualizados: ' . $posts_actualizados . '
';
}
echo 'Escanear Posts Antiguos IA
Haz clic para reiniciar y etiquetar publicaciones a partir de preguntas detectadas.
';
}
// 4. Exportar resultados a CSV y JSON
function exportar_ia_tags_a_csv_y_json() {
if (!current_user_can('manage_options')) return;
$args = ['post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1];
$query = new WP_Query($args);
$data = [];
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$tags = wp_get_post_tags($post_id, ['fields' => 'names']);
if (!empty($tags)) {
$data[] = [
'id' => $post_id,
'title' => get_the_title(),
'url' => get_permalink(),
'tags' => implode('; ', $tags)
];
}
}
wp_reset_postdata();
}
$upload_dir = wp_upload_dir();
$csv_file = $upload_dir['basedir'] . '/ia-tags.csv';
$json_file = $upload_dir['basedir'] . '/ia-tags.json';
$csv = fopen($csv_file, 'w');
fputcsv($csv, ['ID', 'Título', 'URL', 'Etiquetas']);
foreach ($data as $row) fputcsv($csv, $row);
fclose($csv);
file_put_contents($json_file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo '';
}
add_action('admin_menu', function() {
add_submenu_page('tools.php', 'Exportar Etiquetas IA', 'Exportar Etiquetas IA', 'manage_options', 'exportar-etiquetas-ia', 'exportar_ia_tags_a_csv_y_json');
});