<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// ─────────────────────────────────────────────
// Visibilité : affiché partout sauf pages exclues
// ─────────────────────────────────────────────
function cet_should_display_template( $template_id ) {
$excluded_raw = get_post_meta( $template_id, '_cet_excluded_pages', true );
$excluded = array_filter( array_map( 'absint', explode( ',', (string) $excluded_raw ) ) );
if ( empty( $excluded ) ) return true;
$current_id = (int) get_queried_object_id();
return ! in_array( $current_id, $excluded, true );
}
// ─────────────────────────────────────────────
// Rendu Elementor avec fallback WP classique
// ─────────────────────────────────────────────
function cet_render_elementor_content( $post_id ) {
if ( did_action( 'elementor/loaded' ) &&
class_exists( '\Elementor\Plugin' ) &&
\Elementor\Plugin::$instance->db->is_built_with_elementor( $post_id ) ) {
return \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $post_id );
}
$post = get_post( $post_id );
if ( ! $post ) return '';
return apply_filters( 'the_content', $post->post_content );
}
// ─────────────────────────────────────────────
// [cet_template id="X"] ou [cet_template type="header"]
// ─────────────────────────────────────────────
function cet_template_shortcode( $atts ) {
$atts = shortcode_atts(
array( 'id' => '', 'type' => '' ),
$atts,
'cet_template'
);
$template_id = (int) $atts['id'];
if ( empty( $template_id ) && ! empty( $atts['type'] ) ) {
$template_id = (int) get_option( 'cet_default_' . sanitize_key( $atts['type'] ) );
}
if ( empty( $template_id ) ) return '';
if ( ! cet_should_display_template( $template_id ) ) return '';
return cet_render_elementor_content( $template_id );
}
add_shortcode( 'cet_template', 'cet_template_shortcode' );
// ─────────────────────────────────────────────
// [cet_dynamic_content]
// À placer dans un gabarit wrapper Elementor.
// Injecte le contenu propre à la fiche CPT en cours.
// ─────────────────────────────────────────────
function cet_dynamic_content_shortcode( $atts ) {
// Contexte : on est dans le rendu du wrapper, $GLOBALS['cet_current_post_id']
// est défini par hooks.php avant d'appeler get_builder_content_for_display()
$post_id = isset( $GLOBALS['cet_current_post_id'] ) ? (int) $GLOBALS['cet_current_post_id'] : get_the_ID();
if ( empty( $post_id ) ) return '';
$post = get_post( $post_id );
if ( ! $post ) return '';
// Si le contenu de la fiche est lui-même fait avec Elementor, on le rend via Elementor
if ( did_action( 'elementor/loaded' ) &&
class_exists( '\Elementor\Plugin' ) &&
\Elementor\Plugin::$instance->db->is_built_with_elementor( $post_id ) ) {
return \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $post_id );
}
// Sinon : contenu WordPress classique
return apply_filters( 'the_content', $post->post_content );
}
add_shortcode( 'cet_dynamic_content', 'cet_dynamic_content_shortcode' );
// ─────────────────────────────────────────────
// [cet_recent_posts]
// Affiche les derniers articles publiés, en
// respectant les catégories filtrées du blog.
// Paramètres :
// number : nombre d'articles (défaut 5)
// title : titre du widget (défaut vide)
// orderby : date | title | rand (défaut date)
// ─────────────────────────────────────────────
function cet_recent_posts_shortcode( $atts ) {
$atts = shortcode_atts( array(
'number' => 5,
'title' => '',
'orderby' => 'date',
), $atts, 'cet_recent_posts' );
$number = max( 1, absint( $atts['number'] ) );
$orderby = sanitize_key( $atts['orderby'] );
$title = sanitize_text_field( $atts['title'] );
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $number,
'orderby' => $orderby,
'order' => 'DESC',
'no_found_rows' => true,
);
// Respecte les catégories filtrées dans Gabarits d'archives > Page des articles
$tax_wrappers = (array) get_option( 'cet_tax_wrappers', array() );
if ( ! empty( $tax_wrappers['__blog__']['filter_cats'] ) ) {
$query_args['category__in'] = array_map( 'absint', $tax_wrappers['__blog__']['filter_cats'] );
}
$query = new WP_Query( $query_args );
if ( ! $query->have_posts() ) return '';
ob_start();
?>
<div class="cet-recent-posts">
<?php if ( $title ) : ?>
<p class="cet-recent-posts-title"><?php echo esc_html( $title ); ?></p>
<?php endif; ?>
<ul class="cet-recent-posts-list">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="cet-recent-posts-item">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
</div>
<style>
.cet-recent-posts { font-size: .9rem; }
.cet-recent-posts-title { font-weight: 600; font-size: .85rem; text-transform: uppercase; letter-spacing: .04em; color: #888; margin: 0 0 8px; }
.cet-recent-posts-list { list-style: none; margin: 0; padding: 0; }
.cet-recent-posts-item { border-bottom: 1px solid #f0f0f0; }
.cet-recent-posts-item:last-child { border-bottom: none; }
.cet-recent-posts-item a { display: block; padding: 7px 0; text-decoration: none; color: inherit; line-height: 1.4; }
.cet-recent-posts-item a:hover { text-decoration: underline; }
</style>
<?php
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode( 'cet_recent_posts', 'cet_recent_posts_shortcode' );