Files
thalim-theme/inc/post-card-helpers.php
2026-05-12 23:33:46 +02:00

173 lines
6.0 KiB
PHP

<?php
/**
* Build card display data for a single post.
* Resolves Pods relationship fields (stored as multiple meta rows) into
* ready-to-display values so Twig templates don't need to call PHP functions.
*
* Returns an associative array with resolved card fields.
*/
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' => '',
'card_type' => '',
'card_event_date' => '',
'card_event_date_iso' => '',
'card_link' => '',
];
// Event date — date_de_debut (events), fallback to datetime (communications)
// Used for display instead of post_date when set
foreach (['date_de_debut', 'datetime'] as $date_key) {
$event_raw = get_post_meta($post_id, $date_key, true) ?: '';
if ($event_raw && !str_starts_with($event_raw, '0000-00-00')) {
$ts = strtotime($event_raw);
if ($ts) {
$data['card_event_date'] = date_i18n('d/m/Y', $ts);
$data['card_event_date_iso'] = date('Y-m-d', $ts);
break;
}
}
}
// 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];
$is_seance = false;
foreach ($categories as $cat) {
if ($cat->term_id === 12) { $is_seance = true; }
}
foreach ($categories as $cat) {
if (in_array($cat->term_id, $excluded_ids)) continue;
$ancestor_ids = get_ancestors($cat->term_id, 'category');
if (!empty($ancestor_ids)) {
$root = get_category(end($ancestor_ids));
} else {
$root = $cat;
}
$data['parent_slug'] = $root->slug;
$data['card_category_name'] = thalim_cat_name($cat);
$data['card_category_url'] = get_category_link($cat->term_id);
break;
}
// 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
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);
}
}
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
));
if ($parent_id) {
$data['card_link'] = get_permalink((int) $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) {
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;
$data['parent_slug'] = $root->slug;
break;
}
}
}
}
// Type label (first non-empty type_* field)
$type_fields = [
'type_colloque_journee_d_etude',
'type_soutenance',
'type_evenement_culturel',
'type_media',
'type_captation',
'type_revue_collection',
'type_autre',
];
foreach ($type_fields as $field) {
$val = get_post_meta($post_id, $field, true);
if ($val) {
$data['card_type'] = $val;
break;
}
}
// First image from documents_joints
$doc_ids = get_post_meta($post_id, 'documents_joints', false);
foreach ($doc_ids as $doc_id) {
$mime = get_post_mime_type($doc_id);
if ($mime && str_starts_with($mime, 'image/')) {
$src = wp_get_attachment_image_url($doc_id, 'medium');
if ($src) {
$data['card_image'] = $src;
break;
}
}
}
// Members (user IDs → display names + profile URLs)
// Falls back to autre_membres if membres is empty
$membre_ids = get_post_meta($post_id, 'membres', false);
if (empty($membre_ids)) {
$membre_ids = get_post_meta($post_id, 'autre_membres', false);
}
foreach ($membre_ids as $uid) {
$user = get_userdata($uid);
if ($user) {
$data['card_membres'][] = [
'name' => $user->display_name,
'url' => get_author_posts_url($uid),
];
}
}
// 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.
*/
function thalim_get_cards_data($posts) {
$cards = [];
foreach ($posts as $post) {
$cards[$post->ID] = thalim_get_card_data($post->ID);
}
return $cards;
}