From 908188ddf2644422a7616ac8ba7d266a040a6ff9 Mon Sep 17 00:00:00 2001 From: rafa Date: Tue, 11 Aug 2026 07:29:56 -0400 Subject: [PATCH] =?UTF-8?q?A=C3=B1adir=20evangelio=20plegable=20a=20coment?= =?UTF-8?q?arios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: rafa Signed-off-by: rafa --- docs/guia-publicacion-carta-inma.md | 9 ++ mu-plugins/fea-evangelio-comentarios.php | 159 +++++++++++++++++++++++ mu-plugins/fea-homepage.php | 25 +--- scripts/backfill_evangelio_meta.php | 26 ++++ 4 files changed, 200 insertions(+), 19 deletions(-) create mode 100644 mu-plugins/fea-evangelio-comentarios.php create mode 100644 scripts/backfill_evangelio_meta.php diff --git a/docs/guia-publicacion-carta-inma.md b/docs/guia-publicacion-carta-inma.md index 3ebad8c..9122bbb 100644 --- a/docs/guia-publicacion-carta-inma.md +++ b/docs/guia-publicacion-carta-inma.md @@ -407,5 +407,14 @@ traducción inmediata, avisar a Rafa directamente además de pedírselo a Hermes | Asignar `_carta_id` a un artículo | `POST /wp-json/fea/v1/carta-id/{id}` con body `{"carta_id": N}` | | Borrar `_carta_id` de un artículo | `DELETE /wp-json/fea/v1/carta-id/{id}` | +## Regla para la sección Evangelio y comentarios al Evangelio + +La sección empieza siempre con el enlace al post del evangelio de esa semana (por +ejemplo, `MATEO 14, 22-33`) y después van los comentarios. Si ese post no existe, +avisad antes de publicar la carta: no empecéis la sección por un comentario. + +No hay que completar ningún campo nuevo. A partir de ese enlace, el sitio asigna la +cita bíblica automáticamente a los comentarios nuevos. + Campos útiles del post: `title`, `content` (HTML), `status` (`draft`/`publish`/`future`), `date` (ISO 8601 para programar), `categories` (array de IDs), `featured_media` (ID de adjunto). diff --git a/mu-plugins/fea-evangelio-comentarios.php b/mu-plugins/fea-evangelio-comentarios.php new file mode 100644 index 0000000..bd00496 --- /dev/null +++ b/mu-plugins/fea-evangelio-comentarios.php @@ -0,0 +1,159 @@ + 'MATEO', 'Mc' => 'MARCOS', 'Lc' => 'LUCAS', 'Jn' => 'JUAN']; + return $books[$m[1]] . ' ' . preg_replace('/\s+/u', ' ', trim($m[2])); +} + +/** Localiza la lectura española, sin depender del slug (que puede llevar sufijos). */ +function fea_evangelio_reading_id($cita) { + global $wpdb; + static $cache = []; + $title = fea_evangelio_reading_title($cita); + if ($title === '') return 0; + if (isset($cache[$title])) return $cache[$title]; + // La coincidencia se hace siempre contra la lectura original en español; después + // Polylang la sustituye por la traducción del comentario cuando exista. + $cat = 1645; + $sql = $wpdb->prepare( + "SELECT p.ID FROM {$wpdb->posts} p + INNER JOIN {$wpdb->term_relationships} tr ON tr.object_id = p.ID + INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id + WHERE p.post_type = 'post' AND p.post_status = 'publish' AND p.post_title = %s + AND tt.taxonomy = 'category' AND tt.term_id = %d + ORDER BY p.post_date DESC LIMIT 1", + $title, $cat + ); + return $cache[$title] = (int) $wpdb->get_var($sql); +} + +/** Devuelve los evangelios enlazados en la sección pertinente de una carta. */ +function fea_evangelio_from_carta($carta_id) { + if (!function_exists('fea_parse_carta_sections')) return []; + if (function_exists('pll_get_post')) $carta_id = (int) (pll_get_post((int) $carta_id, 'es') ?: $carta_id); + $links = fea_parse_carta_sections((int) $carta_id)['evangelio'] ?? []; + $result = []; + foreach ($links as $id) { + $post = get_post($id); + if (!$post) continue; + $cita = get_post_meta($id, '* Cita biblica', true); + if (!$cita && preg_match('/^(MATEO|MARCOS|LUCAS|JUAN)\s+(.+)$/u', trim($post->post_title), $m)) { + $abbr = ['MATEO' => 'Mt', 'MARCOS' => 'Mc', 'LUCAS' => 'Lc', 'JUAN' => 'Jn'][$m[1]]; + $cita = $abbr . ' ' . $m[2]; + } + if (fea_evangelio_reading_title($cita)) $result[(string) $id] = trim($cita); + } + return $result; +} + +/** Busca una carta en las tres categorías históricas, dentro de cuatro días. */ +function fea_evangelio_nearest_carta_id(WP_Post $post) { + $date = mysql2date('Y-m-d', $post->post_date, false); + $cartas = get_posts([ + 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 10, + 'category__in' => array_map('fea_evangelio_category', [6, 21, 22]), + 'date_query' => [[ + 'after' => date('Y-m-d', strtotime($date . ' -4 days')), + 'before' => date('Y-m-d', strtotime($date . ' +4 days')), 'inclusive' => true, + ]], + 'orderby' => 'date', 'order' => 'DESC', + ]); + if (!$cartas) return 0; + usort($cartas, function($a, $b) use ($post) { + return abs(strtotime($a->post_date) - strtotime($post->post_date)) <=> abs(strtotime($b->post_date) - strtotime($post->post_date)); + }); + return (int) $cartas[0]->ID; +} + +/** Escribe solo metadatos ausentes cuando la carta identifica un único evangelio. */ +function fea_evangelio_capture_meta($post_id) { + $post = get_post($post_id); + if (!$post || $post->post_type !== 'post' || $post->post_status !== 'publish' || !fea_evangelio_is_comment($post_id)) return false; + if (get_post_meta($post_id, '_cita_evangelio', true)) return false; + $carta_id = (int) get_post_meta($post_id, '_carta_id', true); + if (!$carta_id) $carta_id = fea_evangelio_nearest_carta_id($post); + if (!$carta_id) return false; + $evangelios = fea_evangelio_from_carta($carta_id); + if (count($evangelios) !== 1) { + error_log(sprintf('Fe Adulta: comentario %d sin cita; carta %d tiene %d evangelios.', $post_id, $carta_id, count($evangelios))); + return false; + } + update_post_meta($post_id, '_cita_evangelio', reset($evangelios)); + if (!get_post_meta($post_id, '_carta_id', true)) update_post_meta($post_id, '_carta_id', $carta_id); + return true; +} + +function fea_evangelio_summary_label($lang) { + $labels = [ + 'es' => 'Leer el evangelio', 'en' => 'Read the Gospel', 'fr' => 'Lire l’Évangile', + 'it' => 'Leggi il Vangelo', 'pt' => 'Ler o Evangelho', + ]; + return $labels[$lang] ?? $labels['es']; +} + +add_action('transition_post_status', function($new_status, $old_status, $post) { + if ($new_status === 'publish' && $old_status !== 'publish') fea_evangelio_capture_meta($post->ID); +}, 20, 3); + +/** Añade referencia y texto plegado, cerrado por defecto, antes del comentario. */ +add_filter('the_content', function($content) { + if (!is_single() || is_admin()) return $content; + global $post; + if (!$post || !fea_evangelio_is_comment($post->ID)) return $content; + $cita = trim((string) fea_evangelio_comment_meta($post->ID, '_cita_evangelio')); + if (!$cita) return $content; + $reading_id = fea_evangelio_reading_id($cita); + $lang = function_exists('pll_current_language') ? pll_current_language() : 'es'; + if ($reading_id && $lang && $lang !== 'es' && function_exists('pll_get_post')) $reading_id = (int) (pll_get_post($reading_id, $lang) ?: $reading_id); + $reading = $reading_id ? get_post($reading_id) : null; + $starts_with_cita = preg_match('/^\s*' . preg_quote($cita, '/') . '(?:[\s\.,:;\-–—]|$)/iu', wp_strip_all_tags($content)); + $html = '
'; + if (!$starts_with_cita) $html .= '

' . esc_html($cita) . '

'; + if ($reading && trim(wp_strip_all_tags($reading->post_content)) !== '') { + $html .= '
' . esc_html(fea_evangelio_summary_label($lang)) . '' + . wpautop(do_shortcode($reading->post_content)) . '
'; + } + return $html . '
' . $content; +}, 8); + +add_action('wp_head', function() { + if (!is_single()) return; + global $post; + if (!$post || !fea_evangelio_is_comment($post->ID)) return; + ?> + + ID, '_carta_id', true); - $cita = get_post_meta($post->ID, '_cita_evangelio', true); + $carta_id = function_exists('fea_evangelio_comment_meta') + ? fea_evangelio_comment_meta($post->ID, '_carta_id') + : get_post_meta($post->ID, '_carta_id', true); + $cita = function_exists('fea_evangelio_comment_meta') + ? fea_evangelio_comment_meta($post->ID, '_cita_evangelio') + : get_post_meta($post->ID, '_cita_evangelio', true); if (!$carta_id && !$cita) return $content; @@ -2066,23 +2070,6 @@ add_filter('the_content', function($content) { } } - // --- Cita del evangelio — link directo al anchor en el índice --- - if ($cita) { - $book_post_id = ['Mt' => 43908, 'Mc' => 43909, 'Lc' => 43907, 'Jn' => 43906]; - $abbr = substr($cita, 0, 2); - $anchor = fea_cita_to_anchor($cita); - $index_url = isset($book_post_id[$abbr]) - ? esc_url(get_permalink($book_post_id[$abbr]) . '#' . $anchor) - : ''; - - $html .= '
' - . 'Evangelio' - . ($index_url - ? '' . esc_html($cita) . '' - : '' . esc_html($cita) . '') - . '
'; - } - // --- Otros comentarios de esta misma cita --- if ($cita) { $others = get_posts([ diff --git a/scripts/backfill_evangelio_meta.php b/scripts/backfill_evangelio_meta.php new file mode 100644 index 0000000..28ff038 --- /dev/null +++ b/scripts/backfill_evangelio_meta.php @@ -0,0 +1,26 @@ + 'post', 'post_status' => 'publish', 'posts_per_page' => -1, + 'category__in' => [fea_evangelio_category(1647)], + 'meta_query' => [['key' => '_cita_evangelio', 'compare' => 'NOT EXISTS']], + 'orderby' => 'date', 'order' => 'ASC', +]); +$out = fopen('php://output', 'w'); +fputcsv($out, ['post_id', 'fecha', 'carta_id', 'cita_propuesta', 'resultado']); +foreach ($posts as $post) { + $before_carta = (int) get_post_meta($post->ID, '_carta_id', true); + // Las cartas deducidas solo se proponen: la cola histórica requiere revisión humana. + $carta_id = $before_carta ?: fea_evangelio_nearest_carta_id($post); + $evangelios = $carta_id ? fea_evangelio_from_carta($carta_id) : []; + $cita = $before_carta && count($evangelios) === 1 ? reset($evangelios) : ''; + $result = $cita ? ($apply ? 'aplicado' : 'propuesta') : (!$before_carta ? 'revision_manual_carta_por_fecha' : (count($evangelios) > 1 ? 'revision_manual_multi_evangelio' : 'sin_evangelio')); + if ($apply && $cita) { + update_post_meta($post->ID, '_cita_evangelio', $cita); + if (!$before_carta) update_post_meta($post->ID, '_carta_id', $carta_id); + } + fputcsv($out, [$post->ID, $post->post_date, $carta_id ?: '', $cita, $result]); +}