From 26896828f4773c31b2537c4635bff3f1e59ae08e Mon Sep 17 00:00:00 2001 From: Antoine M Date: Wed, 20 May 2026 10:53:22 +0200 Subject: [PATCH] REFACTOR Post card with external utilities function to handle permalink --- includes/utilities.php | 91 +++++++++- template-parts/components/cards/post-card.php | 168 +++++++++--------- 2 files changed, 169 insertions(+), 90 deletions(-) diff --git a/includes/utilities.php b/includes/utilities.php index 5dcc11e..ed6c747 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -439,7 +439,7 @@ function is_a_collections_page() $current_page_id = (int) get_queried_object_id(); - if ($collection_parent_page_id === $current_page_id || $is_fond_archive_fonds_archives) { + if ($collection_parent_page_id === $current_page_id) { return true; } @@ -452,4 +452,91 @@ function is_a_collections_page() } return false; -} \ No newline at end of file +} + + + + + +function carhop_get_post_card_link_according_to_post_datas($post_id, $card_args = []) +{ + + $post_id = (int) $post_id; + + + $has_external_link = $card_args['has_external_link'] ?? false; + $external_link = $card_args['external_link'] ?? false; + + + + if ($has_external_link && $external_link !== '') { + + return array( + 'link' => $external_link, + 'target' => '_blank', + 'text' => $card_args['external_link_text'] ?? null, + 'is_external' => true, + 'has_valid_link' => true, + ); + } + + + $post_type = get_post_type($post_id); + $link = get_the_permalink($post_id); + $target = '_self'; + $text = null; + $is_external = false; + + + switch ($post_type) { + case 'analyses-etudes': + $type_analyse_etude = get_the_terms($post_id, 'type-analyse-etude')[0] ?? null; + + if ($type_analyse_etude->slug === 'analyse') { + write_log("######"); + $link_type = get_field('link_type', $post_id); + + switch ($link_type) { + case 'pdf': + $pdf = get_field('pdf', $post_id); + if (is_array($pdf) && ! empty($pdf['url'])) { + $link = $pdf['url']; + $target = '_blank'; + $is_external = true; + } + break; + case 'link': + $link_field = get_field('link', $post_id); + if (is_array($link_field) && ! empty($link_field['url'])) { + $link = $link_field['url']; + $target = '_blank'; + $is_external = true; + } + break; + default: + $link = get_the_permalink($post_id); + break; + } + } + break; + + case 'dbmob': + $maitron_url = get_field('maitron_url', $post_id); + write_log($maitron_url); + $link = $maitron_url ?? get_the_permalink($post_id); + $target = '_blank'; + $text = 'Voir la notice'; + break; + default: + $link = get_permalink($post_id) ?: ''; + break; + } + + return array( + 'link' => $link, + 'target' => $target, + 'text' => $text, + 'is_external' => $is_external, + 'has_valid_link' => ($link !== ''), + ); +} diff --git a/template-parts/components/cards/post-card.php b/template-parts/components/cards/post-card.php index 3159511..5d312ef 100644 --- a/template-parts/components/cards/post-card.php +++ b/template-parts/components/cards/post-card.php @@ -6,14 +6,7 @@ $showExcerpt = $args['show_excerpt'] ?? false; $showTags = $args['showTags'] ?? true; -$has_external_link = $args['has_external_link'] ?? false; -$external_link = $args['external_link'] ?? null; -$external_link_text = $args['external_link_text'] ?? null; - -$target = $has_external_link && ! empty($external_link) ? '_blank' : '_self'; - $excerpt = get_the_excerpt($ID); -$link = ($has_external_link && ! empty($external_link)) ? $external_link : get_the_permalink($ID); $has_post_thumbnail = has_post_thumbnail($ID); $thumbnail_url = get_the_post_thumbnail_url($ID) ?? null; @@ -35,97 +28,96 @@ $tags = get_the_terms($ID, 'etiquettes'); $description = get_field('description', $ID); $birth_death_date = get_field('birth_death_date', $ID); +$card_link = carhop_get_post_card_link_according_to_post_datas($ID, $args); +$link = $card_link['link']; +$target = $card_link['target']; +$external_link_text = $card_link['text']; +$has_valid_link = $card_link['has_valid_link']; +$is_external = $card_link['is_external']; -if($current_post_type === 'analyses-etudes'){ - $type_analyse_etude = get_the_terms($ID, 'type-analyse-etude')[0] ?? null; - - if($type_analyse_etude->slug === 'analyse'){ - $link_type = get_field('link_type', $ID); - $target = '_blank' ; - - switch($link_type){ - case 'pdf': - $link = get_field('pdf', $ID)['url'] ?? ''; - break; - case 'link': - $link = get_field('link', $ID)['url'] ?? ''; - break; - default: - $link = get_the_permalink($ID); - break; - } - } -} - +$card_classes = 'card post-card post-card--' . sanitize_html_class((string) $current_post_type); +$card_classes .= $has_post_thumbnail ? ' post-card--has-thumbnail' : ''; +$card_classes .= ! $has_valid_link ? ' post-card--no-link' : ''; ?> - - -
- <?php echo $title; ?> - -
- -
- -
- $current_post_type, - 'current_post_id' => $ID - )); ?> -
-

- -
- - -
- - -
- -
-