Catégories résolues par slug et IDs Pods par nom, tests

This commit is contained in:
2026-06-10 21:30:47 +02:00
parent 66dc9e1f47
commit 021f322719
5 changed files with 185 additions and 33 deletions

View File

@@ -62,7 +62,6 @@ class Thalim_NL_Admin_Page {
'meta_key' => '_newsletter_month',
'orderby' => 'meta_value',
'order' => 'DESC',
'lang' => '',
]);
$past_newsletters = array_filter($past_newsletters, function ($p) {
return !empty(get_post_meta($p->ID, '_newsletter_month', true));
@@ -490,15 +489,40 @@ class Thalim_NL_Admin_Page {
$this->do_triple_storage_category($post_id, THALIM_NL_CAT_NEWSLETTER);
if (function_exists('pll_set_post_language')) {
pll_set_post_language($post_id, 'fr');
}
return $post_id;
}
/**
* Assign a category using the Pods triple-storage pattern (same as HAL importer).
* Résout l'ID du pod `post` et celui de son champ `categorie` par NOM
* dans wp_posts (post_type _pods_pod / _pods_field) — les IDs en dur ne
* survivent pas à une réimportation de base.
*
* @return array{0:int,1:int} [pod_id, field_id] (0 si introuvable)
*/
private function resolve_pods_categorie_ids(): array {
static $ids = null;
if ($ids === null) {
global $wpdb;
$pod_id = (int) $wpdb->get_var(
"SELECT ID FROM {$wpdb->posts}
WHERE post_type = '_pods_pod' AND post_name = 'post' LIMIT 1"
);
$field_id = $pod_id ? (int) $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM {$wpdb->posts}
WHERE post_type = '_pods_field' AND post_name = 'categorie' AND post_parent = %d
LIMIT 1",
$pod_id
)) : 0;
$ids = [$pod_id, $field_id];
}
return $ids;
}
/**
* Assign a category using the Pods quadruple-storage pattern (same as HAL importer).
*
* ⚠ DÉPENDANCE DURE à Pods 3.x : reproduit le stockage interne de Pods
* (postmeta + _pods_* sérialisé + wp_podsrel + catégorie WP native).
*/
private function do_triple_storage_category(int $post_id, int $cat_id): void {
global $wpdb;
@@ -513,11 +537,19 @@ class Thalim_NL_Admin_Page {
update_post_meta($post_id, '_pods_categorie', [$cat_id]);
// 4. wp_podsrel row
[$pod_id, $field_id] = $this->resolve_pods_categorie_ids();
if (!$pod_id || !$field_id) {
error_log(sprintf(
'[thalim-newsletter] Pod/champ Pods "categorie" introuvable — wp_podsrel non écrit pour le post %d',
$post_id
));
return;
}
$wpdb->delete(
$wpdb->prefix . 'podsrel',
[
'pod_id' => THALIM_NL_POD_ID_POST,
'field_id' => THALIM_NL_FIELD_ID_CAT,
'pod_id' => $pod_id,
'field_id' => $field_id,
'item_id' => $post_id,
],
['%d', '%d', '%d']
@@ -525,8 +557,8 @@ class Thalim_NL_Admin_Page {
$wpdb->insert(
$wpdb->prefix . 'podsrel',
[
'pod_id' => THALIM_NL_POD_ID_POST,
'field_id' => THALIM_NL_FIELD_ID_CAT,
'pod_id' => $pod_id,
'field_id' => $field_id,
'item_id' => $post_id,
'related_pod_id' => 0,
'related_field_id' => 0,
@@ -617,7 +649,6 @@ class Thalim_NL_Admin_Page {
'posts_per_page' => 1,
'meta_key' => '_newsletter_month',
'meta_value' => $year_month,
'lang' => '', // Polylang: all languages
]);
return $posts[0] ?? null;
}

View File

@@ -37,13 +37,27 @@ class Thalim_NL_Post_Query {
*/
private const SEANCE_WINDOW_MARGIN_DAYS = 5;
/** Categories to exclude from the newsletter UI */
private const EXCLUDED_CATS = [
9, // Vie du labo (intranet)
12, // Séance de séminaire
20, // Newsletter
31, // Non classé
];
/**
* Categories to exclude from the newsletter UI, résolues par slug
* (fallback sur les IDs historiques si un slug est introuvable).
*/
private static function excluded_cats(): array {
static $ids = null;
if ($ids === null) {
$map = [
'vie-du-labo-intranet' => 9, // Vie du labo (intranet)
'seance-de-seminaire' => 12, // Séance de séminaire
'newsletter' => 20, // Newsletter
'non-classe' => 31, // Non classé
];
$ids = [];
foreach ($map as $slug => $fallback) {
$term = get_term_by('slug', $slug, 'category');
$ids[] = $term ? (int) $term->term_id : $fallback;
}
}
return $ids;
}
/**
* Get all newsletter-eligible categories, grouped by parent.
@@ -61,7 +75,7 @@ class Thalim_NL_Post_Query {
$parents = [];
foreach ($all_cats as $cat) {
if (in_array($cat->term_id, self::EXCLUDED_CATS, true)) {
if (in_array($cat->term_id, self::excluded_cats(), true)) {
continue;
}
if ($cat->parent == 0) {
@@ -74,7 +88,7 @@ class Thalim_NL_Post_Query {
}
foreach ($all_cats as $cat) {
if (in_array($cat->term_id, self::EXCLUDED_CATS, true)) {
if (in_array($cat->term_id, self::excluded_cats(), true)) {
continue;
}
if ($cat->parent == 0) {