Refactoring : sécurité (XSS), découpage en modules inc/* et js/admin/*, IDs résolus par slug, perf (caches, cron Gravatar, assets auto-hébergés), tests

This commit is contained in:
2026-06-10 21:30:25 +02:00
parent e6b73df516
commit 9280c3b9ce
44 changed files with 3209 additions and 2907 deletions

View File

@@ -11,8 +11,6 @@ function thalim_get_card_data($post_id) {
$data = [
'card_image' => null,
'card_membres' => [],
'card_axes' => [],
'card_etiquettes' => [],
'parent_slug' => '',
'card_category_name' => '',
'card_category_url' => '',
@@ -23,12 +21,13 @@ function thalim_get_card_data($post_id) {
];
// Category-based date formatting:
// - Séminaire (cat 11, not cat 12): "Du X au Y" from first/last linked séance dates
// - Ouvrage (cat 15): year only — includes the post.date fallback (overrides Twig default d/m/Y)
// - Séminaire (hors séances): "Du X au Y" from first/last linked séance dates
// - Ouvrage: year only — includes the post.date fallback (overrides Twig default d/m/Y)
// - Default: date_de_debut > datetime > (empty → Twig falls back to post.date('d/m/Y'))
$seance_cat = thalim_cat_id('seance');
$cat_ids = wp_get_post_categories($post_id);
$is_seminaire = in_array(11, $cat_ids, true) && !in_array(12, $cat_ids, true);
$is_ouvrage = in_array(15, $cat_ids, true);
$is_seminaire = in_array(thalim_cat_id('seminaires'), $cat_ids, true) && !in_array($seance_cat, $cat_ids, true);
$is_ouvrage = in_array(thalim_cat_id('ouvrages'), $cat_ids, true);
if ($is_seminaire) {
// Aggregate timestamps from linked séances (Pods `seances` meta = array of post IDs)
@@ -86,10 +85,10 @@ function thalim_get_card_data($post_id) {
// Resolve top-level parent category slug for color theming and direct category name for display
$categories = wp_get_post_categories($post_id, ['fields' => 'all']);
$excluded_ids = [12, 31];
$excluded_ids = array_filter([ $seance_cat, thalim_cat_id('non-classe') ]);
$is_seance = false;
foreach ($categories as $cat) {
if ($cat->term_id === 12) { $is_seance = true; }
if ($cat->term_id === $seance_cat) { $is_seance = true; }
}
foreach ($categories as $cat) {
if (in_array($cat->term_id, $excluded_ids)) continue;
@@ -107,28 +106,21 @@ function thalim_get_card_data($post_id) {
// Séances de séminaire: link to parent séminaire with hash, derive color from parent's categories
if ($is_seance) {
// Always show the category label for séances even though cat 12 is excluded from color resolution
// Always show the category label for séances even though the séance
// category is excluded from color resolution
if (!$data['card_category_name']) {
$seance_cat = get_category(12);
if ($seance_cat) {
$data['card_category_name'] = thalim_cat_name($seance_cat);
$data['card_category_url'] = get_category_link(12);
$seance_term = get_category($seance_cat);
if ($seance_term) {
$data['card_category_name'] = thalim_cat_name($seance_term);
$data['card_category_url'] = get_category_link($seance_cat);
}
}
global $wpdb;
$parent_id = $wpdb->get_var($wpdb->prepare(
"SELECT pm.post_id FROM {$wpdb->postmeta} pm
JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = 'seances' AND pm.meta_value = %s
AND p.post_status = 'publish'
LIMIT 1",
(string) $post_id
));
$parent_id = thalim_get_seance_parent_id($post_id);
if ($parent_id) {
$data['card_link'] = get_permalink((int) $parent_id) . '#seance-' . $post_id;
$data['card_link'] = get_permalink($parent_id) . '#seance-' . $post_id;
// Derive color from parent séminaire's categories if not already set
if (!$data['parent_slug']) {
foreach (wp_get_post_categories((int) $parent_id, ['fields' => 'all']) as $cat) {
foreach (wp_get_post_categories($parent_id, ['fields' => 'all']) as $cat) {
if (in_array($cat->term_id, $excluded_ids)) continue;
$ancestor_ids = get_ancestors($cat->term_id, 'category');
$root = !empty($ancestor_ids) ? get_category(end($ancestor_ids)) : $cat;
@@ -186,32 +178,44 @@ function thalim_get_card_data($post_id) {
}
}
// Axes thématiques (post IDs → titles)
$axe_ids = get_post_meta($post_id, 'axes_thematiques', false);
foreach ($axe_ids as $axe_id) {
$axe = get_post($axe_id);
if ($axe) {
$data['card_axes'][] = $axe->post_title;
}
}
// Etiquettes (post IDs → titles)
$tag_ids = get_post_meta($post_id, 'etiquettes', false);
foreach ($tag_ids as $tag_id) {
$tag_post = get_post($tag_id);
if ($tag_post) {
$data['card_etiquettes'][] = $tag_post->post_title;
}
}
return $data;
}
/**
* Build card data map for a collection of posts.
* Returns an array keyed by post ID.
*
* Précharge en lot les caches meta/termes des posts et les utilisateurs
* référencés (membres/autre_membres) pour éviter les requêtes N+1 de
* thalim_get_card_data() (une grille de 12 cartes passait par des dizaines
* de get_post_meta/get_userdata individuels).
*/
function thalim_get_cards_data($posts) {
$ids = [];
foreach ($posts as $post) {
$ids[] = $post->ID;
}
if ($ids) {
// Meta + termes en 2 requêtes pour tout le lot
update_meta_cache('post', $ids);
update_object_term_cache($ids, 'post');
// Utilisateurs référencés par les cartes (lecture servie par le cache meta)
$user_ids = [];
foreach ($ids as $pid) {
foreach (['membres', 'autre_membres'] as $key) {
foreach (get_post_meta($pid, $key, false) as $uid) {
$user_ids[] = (int) $uid;
}
}
}
$user_ids = array_values(array_unique(array_filter($user_ids)));
if ($user_ids) {
cache_users($user_ids);
}
}
$cards = [];
foreach ($posts as $post) {
$cards[$post->ID] = thalim_get_card_data($post->ID);