REFACTOR Post card with external utilities function to handle permalink

This commit is contained in:
Antoine M 2026-05-20 10:53:22 +02:00
parent 79be66b060
commit 26896828f4
2 changed files with 169 additions and 90 deletions

View File

@ -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;
}
}
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 !== ''),
);
}

View File

@ -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' : '';
?>
<a class="post-card post-card--<?php echo $current_post_type . ' ' . ($has_post_thumbnail ? 'post-card--has-thumbnail' : ''); ?> card" href="<?php echo esc_url($link); ?>" target="<?php echo $target; ?>">
<?php if ($has_post_thumbnail) : ?>
<div class="post-card__thumbnail">
<img src="<?php echo $thumbnail_url; ?>" alt="<?php echo $title; ?>">
<?php if ($has_thumbnail_overlay) : ?>
<div class="thumbnail-overlay"></div>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="card__inner">
<?php get_template_part('template-parts/components/content-meta', null, array(
'current_post_type' => $current_post_type,
'current_post_id' => $ID
)); ?>
<div class="card__content">
<h3 class="card__title"><?php echo $title; ?></h3>
<?php if ($showExcerpt) : ?>
<div class="card__excerpt"><?php echo $excerpt; ?></div>
<?php endif; ?>
<?php if ($birth_death_date) : ?>
<div class="card__birth-death-date"><?php echo $birth_death_date; ?></div>
<?php endif; ?>
<?php if ($description) : ?>
<div class="card__description"><?php echo $description; ?></div>
<?php endif; ?>
</div>
<div class="card__details">
<?php if ($has_valid_link) : ?>
<a class="<?php echo esc_attr(trim($card_classes)); ?>"
href="<?php echo esc_url($link); ?>"
target="<?php echo esc_attr($target); ?>"
<?php echo $is_external ? ' rel="noopener noreferrer"' : ''; ?>>
<?php else : ?>
<div class="<?php echo esc_attr(trim($card_classes)); ?>">
<?php endif; ?>
<?php if ($has_post_thumbnail) : ?>
<div class="post-card__thumbnail">
<img src="<?php echo $thumbnail_url; ?>" alt="<?php echo $title; ?>">
<?php if ($has_thumbnail_overlay) : ?>
<div class="thumbnail-overlay"></div>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="card__inner">
<?php get_template_part('template-parts/components/content-meta', null, array(
'current_post_type' => $current_post_type,
'current_post_id' => $ID
)); ?>
<div class="card__content">
<h3 class="card__title"><?php echo $title; ?></h3>
<?php if ($showExcerpt) : ?>
<div class="card__excerpt"><?php echo $excerpt; ?></div>
<?php endif; ?>
<?php if ($birth_death_date) : ?>
<div class="card__birth-death-date"><?php echo $birth_death_date; ?></div>
<?php endif; ?>
<?php if ($description) : ?>
<div class="card__description"><?php echo $description; ?></div>
<?php endif; ?>
</div>
<div class="card__details">
<div class="post-card__details-text">
<time datetime="<?php echo $date; ?>" class="card__details-date date"><?php echo $date; ?></time>
<div class="post-card__details-text">
<time datetime="<?php echo $date; ?>" class="card__details-date date"><?php echo $date; ?></time>
<?php if ($authors) : ?>
<ul class="post-card__authors">
<?php if ($has_main_author && $main_author) : ?>
<li class="main-author"><?php echo $main_author->post_title; ?></li>
<?php endif; ?>
<?php foreach ($authors as $author) : ?>
<li class="author"><?php echo $author->post_title; ?></li>
<?php endforeach; ?>
<?php if ($has_publication_direction && $publication_directors) : ?>
<?php foreach ($publication_directors as $publication_director) : ?>
<li class="publication-director">Sous la direction de <?php echo $publication_director->post_title; ?></li>
<?php if ($authors) : ?>
<ul class="post-card__authors">
<?php if ($has_main_author && $main_author) : ?>
<li class="main-author"><?php echo $main_author->post_title; ?></li>
<?php endif; ?>
<?php foreach ($authors as $author) : ?>
<li class="author"><?php echo $author->post_title; ?></li>
<?php endforeach; ?>
<?php endif; ?>
<?php if ($editors) : ?>
<li class="editor"><?php echo $editors; ?></li>
<?php endif; ?>
<?php if ($has_publication_direction && $publication_directors) : ?>
<?php foreach ($publication_directors as $publication_director) : ?>
<li class="publication-director">Sous la direction de <?php echo $publication_director->post_title; ?></li>
<?php endforeach; ?>
<?php endif; ?>
<?php if ($editors) : ?>
<li class="editor"><?php echo $editors; ?></li>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
<?php if ($current_post_type === 'outils-pedagogiques' && has_post_thumbnail($ID)) : ?>
<div class="card__thumbnail">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php endif; ?>
<?php if ($showTags && $tags) : ?>
<ul class="tag-list">
<?php foreach ($tags as $tag) : ?>
<li class="tag-list__tag"><?php echo $tag->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<?php if ($current_post_type === 'outils-pedagogiques' && has_post_thumbnail($ID)) : ?>
<div class="card__thumbnail">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php endif; ?>
<?php if ($showTags && $tags) : ?>
<ul class="tag-list">
<?php foreach ($tags as $tag) : ?>
<li class="tag-list__tag"><?php echo $tag->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<?php if ($has_valid_link) : ?>
</a>
<?php else : ?>
</div>
</a>
<?php endif; ?>