181 lines
5.8 KiB
PHP
181 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* SEO local, OpenGraph, Alerte Indexation Admin & Optimisation Performance.
|
|
*
|
|
* @package CGT_FHA
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Titre SEO personnalisé.
|
|
*/
|
|
add_filter( 'pre_get_document_title', 'cgt_fha_document_title', 20 );
|
|
function cgt_fha_document_title( string $title ): string {
|
|
$site_brand = 'Union Locale CGT Foix Haute-Ariège';
|
|
|
|
if ( is_front_page() ) {
|
|
return $site_brand . ' | Défense des salariés et action syndicale';
|
|
}
|
|
|
|
if ( is_singular() ) {
|
|
return wp_strip_all_tags( get_the_title() ) . ' | ' . $site_brand;
|
|
}
|
|
|
|
if ( is_home() ) {
|
|
return 'Actualités locales | ' . $site_brand;
|
|
}
|
|
|
|
if ( is_archive() ) {
|
|
return wp_strip_all_tags( get_the_archive_title() ) . ' | ' . $site_brand;
|
|
}
|
|
|
|
if ( is_404() ) {
|
|
return 'Page introuvable | ' . $site_brand;
|
|
}
|
|
|
|
return $title ?: $site_brand;
|
|
}
|
|
|
|
/**
|
|
* Calcule la meta description adaptée au contexte courant.
|
|
*/
|
|
function cgt_fha_get_meta_description(): string {
|
|
$opts = cgt_fha_get_options();
|
|
|
|
if ( is_front_page() ) {
|
|
return wp_strip_all_tags(
|
|
"Union Locale CGT de Foix et de la Haute-Ariège : union des syndicats CGT du territoire, rattachement des salarié·e·s isolé·e·s via le syndicat Multipros et permanences à l'Espace Sabart."
|
|
);
|
|
}
|
|
|
|
if ( is_singular() ) {
|
|
$post = get_queried_object();
|
|
if ( $post instanceof WP_Post ) {
|
|
$raw = has_excerpt( $post ) ? $post->post_excerpt : $post->post_content;
|
|
$txt = trim( (string) preg_replace( '/\s+/u', ' ', wp_strip_all_tags( strip_shortcodes( $raw ) ) ) );
|
|
if ( '' !== $txt ) {
|
|
return wp_html_excerpt( $txt, 158, '…' );
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( is_category() || is_tag() ) {
|
|
$desc = trim( wp_strip_all_tags( term_description() ) );
|
|
if ( '' !== $desc ) {
|
|
return wp_html_excerpt( $desc, 158, '…' );
|
|
}
|
|
}
|
|
|
|
return wp_html_excerpt( (string) $opts['footer_desc'], 158, '…' );
|
|
}
|
|
|
|
/**
|
|
* Injection automatique des balises <meta name="description"> et OpenGraph.
|
|
*/
|
|
add_action( 'wp_head', 'cgt_fha_render_meta_and_og', 2 );
|
|
function cgt_fha_render_meta_and_og(): void {
|
|
$opts = cgt_fha_get_options();
|
|
$title = cgt_fha_document_title( wp_get_document_title() );
|
|
$description = cgt_fha_get_meta_description();
|
|
$og_type = is_single() ? 'article' : 'website';
|
|
|
|
if ( is_front_page() ) {
|
|
$url = home_url( '/' );
|
|
} elseif ( is_singular() ) {
|
|
$url = get_permalink() ?: home_url( '/' );
|
|
} else {
|
|
global $wp;
|
|
$url = home_url( add_query_arg( array(), $wp->request ?? '' ) );
|
|
}
|
|
|
|
$og_image = (string) $opts['hero_bg_url'];
|
|
if ( is_singular() && has_post_thumbnail() ) {
|
|
$thumb_url = get_the_post_thumbnail_url( get_queried_object_id(), 'cgt-featured' );
|
|
if ( $thumb_url ) {
|
|
$og_image = $thumb_url;
|
|
}
|
|
}
|
|
?>
|
|
<meta name="description" content="<?php echo esc_attr( $description ); ?>" />
|
|
<meta property="og:locale" content="fr_FR" />
|
|
<meta property="og:site_name" content="Union Locale CGT Foix Haute-Ariège" />
|
|
<meta property="og:type" content="<?php echo esc_attr( $og_type ); ?>" />
|
|
<meta property="og:title" content="<?php echo esc_attr( $title ); ?>" />
|
|
<meta property="og:description" content="<?php echo esc_attr( $description ); ?>" />
|
|
<meta property="og:url" content="<?php echo esc_url( $url ); ?>" />
|
|
<?php if ( '' !== $og_image ) : ?>
|
|
<meta property="og:image" content="<?php echo esc_url( $og_image ); ?>" />
|
|
<?php endif; ?>
|
|
<meta name="twitter:card" content="summary_large_image" />
|
|
<?php
|
|
}
|
|
|
|
/**
|
|
* Alerte rouge dans le tableau de bord si l'indexation des moteurs de recherche est bloquée.
|
|
*/
|
|
add_action( 'admin_notices', 'cgt_fha_indexation_warning_notice' );
|
|
function cgt_fha_indexation_warning_notice(): void {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return;
|
|
}
|
|
|
|
if ( '0' === (string) get_option( 'blog_public' ) ) {
|
|
$reading_url = admin_url( 'options-reading.php' );
|
|
?>
|
|
<div class="notice notice-error" style="border-left-color:#d20012; background:#fff5f5;">
|
|
<p>
|
|
<strong style="color:#d20012;">Alerte SEO — Indexation bloquée :</strong>
|
|
Votre site demande actuellement aux moteurs de recherche de ne pas l'indexer.
|
|
<a href="<?php echo esc_url( $reading_url ); ?>">Rendez-vous dans Réglages > Lecture</a>
|
|
et décochez « Demander aux moteurs de recherche de ne pas indexer ce site ».
|
|
</p>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Nettoyage du head WordPress et suppression de wp-emoji / wp-embed pour un TTFB et LCP optimaux.
|
|
*/
|
|
add_action( 'init', 'cgt_fha_cleanup_wp_bloat' );
|
|
function cgt_fha_cleanup_wp_bloat(): void {
|
|
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
|
|
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
|
|
remove_action( 'wp_print_styles', 'print_emoji_styles' );
|
|
remove_action( 'admin_print_styles', 'print_emoji_styles' );
|
|
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
|
|
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
|
|
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
|
|
|
|
remove_action( 'wp_head', 'rsd_link' );
|
|
remove_action( 'wp_head', 'wlwmanifest_link' );
|
|
remove_action( 'wp_head', 'wp_generator' );
|
|
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
|
|
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
|
|
}
|
|
|
|
add_action( 'wp_footer', 'cgt_fha_disable_wp_embed' );
|
|
function cgt_fha_disable_wp_embed(): void {
|
|
wp_deregister_script( 'wp-embed' );
|
|
}
|
|
|
|
/**
|
|
* Ajoute loading="lazy" et decoding="async" sur les images hors ligne de flottaison.
|
|
*
|
|
* @param array<string, string> $attr Attributs de la balise img.
|
|
* @return array<string, string>
|
|
*/
|
|
add_filter( 'wp_get_attachment_image_attributes', 'cgt_fha_lazy_image_attributes', 10, 1 );
|
|
function cgt_fha_lazy_image_attributes( array $attr ): array {
|
|
if ( empty( $attr['fetchpriority'] ) && empty( $attr['loading'] ) ) {
|
|
$attr['loading'] = 'lazy';
|
|
}
|
|
if ( empty( $attr['decoding'] ) ) {
|
|
$attr['decoding'] = 'async';
|
|
}
|
|
return $attr;
|
|
}
|