cgt-wp-theme/functions.php
2026-11-12 22:07:17 +01:00

305 lines
14 KiB
PHP

<?php
/**
* Fonctions principales du thème Union Locale CGT Foix Haute-Ariège.
*
* @package CGT_FHA
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'CGT_FHA_VERSION', '1.2.1' );
define( 'CGT_FHA_DIR', get_template_directory() );
define( 'CGT_FHA_URI', get_template_directory_uri() );
/**
* Chargement des modules du thème.
*/
require_once CGT_FHA_DIR . '/inc/theme-options.php';
require_once CGT_FHA_DIR . '/inc/class-cgt-confederal-feed.php';
require_once CGT_FHA_DIR . '/inc/seo-performance.php';
require_once CGT_FHA_DIR . '/inc/class-cgt-gitea-updater.php';
/**
* Configuration des supports WordPress et menus.
*/
add_action( 'after_setup_theme', 'cgt_fha_setup' );
function cgt_fha_setup(): void {
load_theme_textdomain( 'cgt-fha', CGT_FHA_DIR . '/languages' );
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_image_size( 'cgt-card', 640, 380, true );
add_image_size( 'cgt-featured', 960, 560, true );
add_theme_support(
'custom-logo',
array(
'height' => 120,
'width' => 520,
'flex-height' => true,
'flex-width' => true,
)
);
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
'navigation-widgets',
)
);
add_theme_support( 'responsive-embeds' );
add_theme_support( 'align-wide' );
register_nav_menus(
array(
'primary' => __( 'Menu principal (En-tête)', 'cgt-fha' ),
'footer' => __( 'Accès rapides (Pied de page)', 'cgt-fha' ),
)
);
}
/**
* Chargement des styles et scripts frontend (zéro dépendance externe).
*/
add_action( 'wp_enqueue_scripts', 'cgt_fha_enqueue_assets' );
function cgt_fha_enqueue_assets(): void {
$style_ver = file_exists( CGT_FHA_DIR . '/style.css' ) ? (string) filemtime( CGT_FHA_DIR . '/style.css' ) : CGT_FHA_VERSION;
$js_ver = file_exists( CGT_FHA_DIR . '/assets/js/main.js' ) ? (string) filemtime( CGT_FHA_DIR . '/assets/js/main.js' ) : CGT_FHA_VERSION;
wp_enqueue_style( 'cgt-fha-style', get_stylesheet_uri(), array(), $style_ver );
wp_enqueue_script(
'cgt-fha-main',
CGT_FHA_URI . '/assets/js/main.js',
array(),
$js_ver,
array(
'in_footer' => true,
'strategy' => 'defer',
)
);
}
/**
* Chargement des scripts admin uniquement sur la page de réglages du thème.
*/
add_action( 'admin_enqueue_scripts', 'cgt_fha_admin_enqueue_assets' );
function cgt_fha_admin_enqueue_assets( string $hook_suffix ): void {
if ( 'appearance_page_cgt-fha-settings' !== $hook_suffix ) {
return;
}
wp_enqueue_media();
$js_ver = file_exists( CGT_FHA_DIR . '/assets/js/main.js' ) ? (string) filemtime( CGT_FHA_DIR . '/assets/js/main.js' ) : CGT_FHA_VERSION;
wp_enqueue_script( 'cgt-fha-admin', CGT_FHA_URI . '/assets/js/main.js', array(), $js_ver, true );
}
/**
* URL du logo d'en-tête (Personnaliser > Logo personnalisé ou Réglages UL CGT ou bannière par défaut).
*/
function cgt_fha_get_header_logo_url(): string {
$custom_logo_id = (int) get_theme_mod( 'custom_logo' );
if ( $custom_logo_id > 0 ) {
$src = wp_get_attachment_image_url( $custom_logo_id, 'full' );
if ( $src ) {
return $src;
}
}
$opts = cgt_fha_get_options();
return ! empty( $opts['header_logo_url'] )
? (string) $opts['header_logo_url']
: CGT_FHA_URI . '/assets/img/banniere-ul-noir.png';
}
/**
* URL du logo clair du pied de page.
*/
function cgt_fha_get_footer_logo_url(): string {
$opts = cgt_fha_get_options();
return ! empty( $opts['footer_logo_url'] )
? (string) $opts['footer_logo_url']
: CGT_FHA_URI . '/assets/img/banniere-ul-blanc.png';
}
/**
* Retourne l'URL de la page listant toutes les actualités locales.
*/
function cgt_fha_get_local_news_archive_url(): string {
$posts_page_id = (int) get_option( 'page_for_posts' );
if ( $posts_page_id > 0 ) {
$link = get_permalink( $posts_page_id );
if ( $link ) {
return $link;
}
}
$opts = cgt_fha_get_options();
return ! empty( $opts['news_url'] ) ? (string) $opts['news_url'] : home_url( '/actualites/' );
}
/**
* Menu de secours pour la navigation principale si aucun menu n'est encore assigné dans Apparence > Menus.
*/
function cgt_fha_default_primary_menu(): void {
$opts = cgt_fha_get_options();
$items = array(
array(
'label' => 'Accueil',
'url' => home_url( '/' ),
'active' => is_front_page(),
),
array(
'label' => 'Actualités',
'url' => cgt_fha_get_local_news_archive_url(),
'active' => is_home() || is_singular( 'post' ) || is_category() || is_tag(),
),
array(
'label' => 'Calendrier',
'url' => (string) $opts['calendar_url'],
'active' => is_page( 'calendrier' ),
),
array(
'label' => 'Contact',
'url' => (string) $opts['contact_url'],
'active' => is_page( 'contact' ),
),
);
echo '<ul class="cgt-nav__list">';
foreach ( $items as $item ) {
$classes = $item['active'] ? 'menu-item current-menu-item' : 'menu-item';
$aria = $item['active'] ? ' aria-current="page"' : '';
printf(
'<li class="%s"><a href="%s"%s>%s</a></li>',
esc_attr( $classes ),
esc_url( $item['url'] ),
$aria,
esc_html( $item['label'] )
);
}
echo '</ul>';
}
/**
* Menu de secours pour les accès rapides du pied de page.
*/
function cgt_fha_default_footer_menu(): void {
$opts = cgt_fha_get_options();
$items = array(
'Actualités' => cgt_fha_get_local_news_archive_url(),
'Calendrier' => (string) $opts['calendar_url'],
'Se syndiquer' => (string) $opts['join_url'],
'Urgence salarié' => (string) $opts['contact_url'],
'Mentions légales & Confidentialité' => (string) $opts['legal_url'],
);
echo '<ul class="cgt-footer__list">';
foreach ( $items as $label => $url ) {
printf(
'<li><a href="%s">%s</a></li>',
esc_url( $url ),
esc_html( $label )
);
}
echo '</ul>';
}
/**
* Icônes SVG propres intégrées (zéro police d'icônes externe).
*/
function cgt_fha_render_icon( string $name, int $size = 24 ): string {
$s = (int) $size;
$icons = array(
'phone' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.127.96.361 1.903.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.907.339 1.85.573 2.81.7A2 2 0 0 1 22 16.92z"/></svg>',
'mail' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7"/></svg>',
'map-pin' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z"/><circle cx="12" cy="10" r="3"/></svg>',
'clock' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>',
'facebook' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M13.5 22v-8h2.7l.4-3h-3.1V9.1c0-.9.3-1.5 1.6-1.5h1.7V4.9c-.3 0-1.3-.1-2.4-.1-2.4 0-4 1.4-4 4.1V11H8v3h2.9v8h2.6Z"/></svg>',
'instagram' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="2" y="2" width="20" height="20" rx="5"/><path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z"/><line x1="17.5" y1="6.5" x2="17.51" y2="6.5"/></svg>',
'youtube' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M21.8 8s-.2-1.4-.8-2c-.8-.8-1.6-.8-2-.9C16.2 5 12 5 12 5h0s-4.2 0-7 .1c-.4.1-1.2.1-2 .9-.6.6-.8 2-.8 2S2 9.6 2 11.2v1.5C2 14.4 2.2 16 2.2 16s.2 1.4.8 2c.8.8 1.8.8 2.2.9 1.6.1 6.8.1 6.8.1s4.2 0 7-.1c.4-.1 1.2-.1 2-.9.6-.6.8-2 .8-2s.2-1.6.2-3.2v-1.5c0-1.7-.2-3.3-.2-3.3ZM9.9 14.6V9.2l5.4 2.7-5.4 2.7Z"/></svg>',
'menu' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="18" x2="21" y2="18"/></svg>',
'check' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="20 6 9 17 4 12"/></svg>',
// Icônes des 3 cartes de mission.
'fist' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 11V6a2 2 0 0 0-2-2a2 2 0 0 0-2 2"/><path d="M14 10V4a2 2 0 0 0-2-2a2 2 0 0 0-2 2v2"/><path d="M10 10.5V6a2 2 0 0 0-2-2a2 2 0 0 0-2 2v8"/><path d="M18 8a2 2 0 1 1 4 0v6a8 8 0 0 1-8 8h-2c-2.8 0-4.5-.86-5.99-2.34l-3.6-3.6a2 2 0 0 1 2.83-2.82L7 15"/></svg>',
'shield' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>',
'chat' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M14 9a2 2 0 0 1-2 2H6l-4 4V4c0-1.1.9-2 2-2h8a2 2 0 0 1 2 2v5Z"/><path d="M18 9h2a2 2 0 0 1 2 2v11l-4-4h-6a2 2 0 0 1-2-2v-1"/></svg>',
'megaphone' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m3 11 18-5v12L3 14v-3z"/><path d="M11.6 16.8a3 3 0 1 1-5.8-1.6"/></svg>',
'scale' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m16 16 3-8 3 8c-.87.65-1.92 1-3 1s-2.13-.35-3-1Z"/><path d="m2 16 3-8 3 8c-.87.65-1.92 1-3 1s-2.13-.35-3-1Z"/><path d="M7 21h10"/><path d="M12 3v18"/><path d="M3 7h2c2 0 5-1 7-2 2 1 5 2 7 2h2"/></svg>',
'users' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>',
'book' => '<svg xmlns="http://www.w3.org/2000/svg" width="' . $s . '" height="' . $s . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 19.5v-15A2.5 2.5 0 0 1 6.5 2H20v20H6.5a2.5 2.5 0 0 1 0-5H20"/></svg>',
);
return $icons[ $name ] ?? $icons['fist'];
}
/**
* Rendu réutilisable d'une carte d'article local (front-page, home, archive).
*/
function cgt_fha_render_local_post_card(): void {
$cats = get_the_category();
$cat_name = 'Vie syndicale';
if ( ! empty( $cats ) ) {
foreach ( $cats as $cat ) {
if ( ! in_array( $cat->slug, array( 'uncategorized', 'non-classe' ), true ) && 'Uncategorized' !== $cat->name ) {
$cat_name = $cat->name;
break;
}
}
}
$placeholder = CGT_FHA_URI . '/assets/img/placeholder.svg';
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'cgt-news-card' ); ?>>
<a href="<?php the_permalink(); ?>" class="cgt-news-card__thumb-link" aria-hidden="true" tabindex="-1">
<?php if ( has_post_thumbnail() ) : ?>
<?php
the_post_thumbnail(
'cgt-card',
array(
'class' => 'cgt-news-card__thumb',
'loading' => 'lazy',
'alt' => esc_attr( get_the_title() ),
)
);
?>
<?php else : ?>
<img
src="<?php echo esc_url( $placeholder ); ?>"
alt="<?php echo esc_attr( get_the_title() ); ?>"
class="cgt-news-card__thumb"
loading="lazy"
decoding="async"
/>
<?php endif; ?>
</a>
<div class="cgt-news-card__body">
<div class="cgt-news-card__meta">
<span class="cgt-pill-cat"><?php echo esc_html( $cat_name ); ?></span>
<time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>" class="cgt-news-card__date">
<?php echo esc_html( get_the_date( 'd.m.Y' ) ); ?>
</time>
</div>
<h3 class="cgt-news-card__title">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h3>
<p class="cgt-news-card__excerpt">
<?php echo esc_html( wp_trim_words( get_the_excerpt(), 20, '…' ) ); ?>
</p>
</div>
</article>
<?php
}