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

71
inc/context.php Normal file
View File

@@ -0,0 +1,71 @@
<?php
/**
* Contexte Twig global (menus, contenu général, axes courants, URL annonces,
* sélecteur de langue). Branché sur le filtre timber/context.
*/
function add_to_context($context) {
$current_lang = thalim_current_language();
// menus
$menu_slug = ($current_lang === 'en') ? 'Navigation-en' : 'Navigation';
$context['menu'] = Timber::get_menu($menu_slug);
$footer_menu_slug = ($current_lang === 'en') ? 'Footer-en' : 'Footer';
$context['footer_menu'] = Timber::get_menu($footer_menu_slug);
// contenus généraux (single post, bilingual)
$gc_posts = Timber::get_posts([
'post_type' => 'contenu_general',
'posts_per_page' => 1,
'orderby' => 'ID',
'order' => 'ASC',
]);
$gc_post = $gc_posts[0] ?? null;
if ( $gc_post ) {
$context['gc'] = [
'umr' => thalim_bilingual( $gc_post->umr ?: '', $current_lang ),
'thalim' => thalim_bilingual( $gc_post->thalim ?: '', $current_lang ),
'siecles' => thalim_bilingual( $gc_post->siecles ?: '', $current_lang ),
'presentation' => ( $current_lang === 'en' && $gc_post->presentation_en ) ? $gc_post->presentation_en : $gc_post->presentation,
'presentation_detail' => ( $current_lang === 'en' && $gc_post->presentation_detail_en ) ? $gc_post->presentation_detail_en : $gc_post->presentation_detail,
];
} else {
$context['gc'] = [];
}
$context['current_language'] = $current_lang;
// Axes thématiques courants (annee_fin >= current year) for navigation dropdown
$current_year = (int) date('Y');
$all_axes = get_terms(['taxonomy' => 'axe_thematique', 'hide_empty' => false, 'orderby' => 'name']);
$axes_courants = [];
if (!is_wp_error($all_axes)) {
foreach ($all_axes as $axe) {
$fin = (int) get_term_meta($axe->term_id, 'annee_fin', true);
if ($fin >= $current_year) {
$link = get_term_link($axe);
if (!is_wp_error($link)) {
$axes_courants[] = [
'name' => thalim_bilingual($axe->name, $current_lang),
'link' => $link,
'ordre' => (int) get_term_meta($axe->term_id, 'ordre_daffichage', true),
];
}
}
}
usort($axes_courants, fn($a, $b) => $a['ordre'] <=> $b['ordre']);
}
$context['axes_courants'] = $axes_courants;
// Annonces page URL (language-aware)
$annonces_page = get_page_by_path('annonces');
$annonces_base = $annonces_page ? get_permalink($annonces_page->ID) : home_url('/annonces/');
$context['annonces_url'] = $annonces_base;
// Language switcher
$context['languages'] = thalim_language_switcher();
return $context;
}
add_filter('timber/context', 'add_to_context');