FEATURE Introducing postLikes features & og meta tags
This commit is contained in:
parent
028f6bf871
commit
1c74c7a095
|
|
@ -250,3 +250,243 @@ function apply_footnotes_urls_to_content($content)
|
|||
return $content;
|
||||
}
|
||||
add_filter('the_content', 'apply_footnotes_urls_to_content', 10);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Récupère le nombre de likes d'un post
|
||||
*
|
||||
* Cette fonction utilitaire récupère le compteur de likes stocké
|
||||
* dans les métadonnées d'un post. Retourne 0 si aucun like n'existe.
|
||||
*
|
||||
* @param int $post_id L'ID du post
|
||||
* @return int Le nombre de likes (0 si aucun)
|
||||
*/
|
||||
function get_post_likes_count($post_id)
|
||||
{
|
||||
$likes_count = get_post_meta($post_id, 'likes_count', true);
|
||||
return $likes_count ? intval($likes_count) : 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Affiche le nombre de likes d'un post avec formatage
|
||||
*
|
||||
* Cette fonction utilitaire formate l'affichage du compteur de likes
|
||||
* avec une icône optionnelle et la gestion du pluriel.
|
||||
*
|
||||
* @param int $post_id L'ID du post
|
||||
* @param bool $show_icon Afficher l'icône cœur (défaut: true)
|
||||
* @return string Le texte formaté (ex: "❤️ 5 likes" ou "3 like")
|
||||
*/
|
||||
function display_likes_count($post_id, $show_icon = true)
|
||||
{
|
||||
$likes_count = get_likes_count($post_id);
|
||||
$icon = $show_icon ? '❤️ ' : '';
|
||||
|
||||
return $icon . $likes_count . ' like' . ($likes_count > 1 ? 's' : '');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Construit les URL de partage pour un post
|
||||
*
|
||||
* Cette fonction génère les URL de partage pour un post spécifique.
|
||||
* Elle retourne un tableau associatif contenant les URL de partage pour Facebook, Twitter-X et LinkedIn.
|
||||
*
|
||||
* @return array Tableau associatif contenant les URL de partage
|
||||
*/
|
||||
function build_share_urls()
|
||||
{
|
||||
$post_id = get_the_ID();
|
||||
|
||||
$postUrl = get_permalink($post_id);
|
||||
$postTitle = get_the_title($post_id);
|
||||
$facebookUrl = 'https://www.facebook.com/sharer.php?u=' . $postUrl;
|
||||
$twitterUrl = 'https://twitter.com/intent/tweet?text=' . $postTitle . '&url=' . get_the_permalink(get_the_id());
|
||||
$linkedInUrl = 'https://www.linkedin.com/feed/?shareActive=true&text=' . $postTitle . ' ' . $postUrl;
|
||||
|
||||
return array(
|
||||
'Facebook' => $facebookUrl,
|
||||
'Twitter-X' => $twitterUrl,
|
||||
'Linkedin' => $linkedInUrl
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Génère les métadonnées Open Graph pour le partage sur les réseaux sociaux
|
||||
*/
|
||||
function generate_og_meta_tags()
|
||||
{
|
||||
global $post;
|
||||
|
||||
// URL canonique
|
||||
$og_url = is_home() ? home_url() : get_permalink();
|
||||
|
||||
// Titre
|
||||
if (is_home() || is_front_page()) {
|
||||
$og_title = get_bloginfo('name');
|
||||
$og_description = get_bloginfo('description');
|
||||
} elseif (is_single() || is_page()) {
|
||||
$og_title = get_the_title();
|
||||
$og_description = get_the_excerpt();
|
||||
|
||||
// Pour les articles, améliorer la description avec les métadonnées
|
||||
if (empty($og_description) && get_post_type() === 'articles') {
|
||||
// Essayer d'utiliser le champ de référence de citation si disponible
|
||||
$cite_reference = get_field('cite_reference');
|
||||
if (!empty($cite_reference)) {
|
||||
$og_description = strip_tags($cite_reference);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback vers le contenu si toujours vide
|
||||
if (empty($og_description)) {
|
||||
$og_description = wp_trim_words(strip_tags(get_the_content()), 30);
|
||||
}
|
||||
} elseif (is_category()) {
|
||||
$og_title = single_cat_title('', false) . ' - ' . get_bloginfo('name');
|
||||
$og_description = category_description();
|
||||
} elseif (is_tag()) {
|
||||
$og_title = single_tag_title('', false) . ' - ' . get_bloginfo('name');
|
||||
$og_description = tag_description();
|
||||
} elseif (is_archive()) {
|
||||
$og_title = get_the_archive_title() . ' - ' . get_bloginfo('name');
|
||||
$og_description = get_the_archive_description();
|
||||
} else {
|
||||
$og_title = wp_get_document_title();
|
||||
$og_description = get_bloginfo('description');
|
||||
}
|
||||
|
||||
// Nettoyer la description
|
||||
$og_description = wp_strip_all_tags($og_description);
|
||||
$og_description = str_replace(array("\r", "\n", "\t"), ' ', $og_description);
|
||||
$og_description = wp_trim_words($og_description, 30);
|
||||
|
||||
// Image
|
||||
$og_image = '';
|
||||
if (is_single() || is_page()) {
|
||||
if (has_post_thumbnail()) {
|
||||
$og_image = get_the_post_thumbnail_url(null, 'large');
|
||||
}
|
||||
}
|
||||
|
||||
// Image par défaut si aucune image n'est trouvée
|
||||
if (empty($og_image)) {
|
||||
// Essayer d'utiliser le logo personnalisé du site
|
||||
$custom_logo = wp_get_attachment_image_src(get_theme_mod('custom_logo'), 'large');
|
||||
if ($custom_logo) {
|
||||
$og_image = $custom_logo[0];
|
||||
} else {
|
||||
// Utiliser l'icône du site
|
||||
$og_image = get_site_icon_url(512);
|
||||
// Si pas d'icône de site, utiliser une image par défaut du thème
|
||||
if (empty($og_image)) {
|
||||
$og_image = get_template_directory_uri() . '/resources/img/covers/carhop-articles-page-cover.svg';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Type de contenu
|
||||
$og_type = is_single() ? 'article' : 'website';
|
||||
|
||||
// Nom du site
|
||||
$site_name = get_bloginfo('name');
|
||||
|
||||
// Auteur (pour les articles)
|
||||
$og_author = '';
|
||||
$article_authors = array();
|
||||
if (is_single()) {
|
||||
$post_type = get_post_type();
|
||||
if ($post_type === 'articles') {
|
||||
// Pour les articles, utiliser le champ ACF 'authors'
|
||||
$authors = get_field('authors');
|
||||
if ($authors && is_array($authors)) {
|
||||
foreach ($authors as $author) {
|
||||
$first_name = get_field('first_name', $author->ID) ?: '';
|
||||
$last_name = get_field('last_name', $author->ID) ?: '';
|
||||
$author_name = trim($first_name . ' ' . $last_name);
|
||||
if (empty($author_name)) {
|
||||
$author_name = $author->post_title;
|
||||
}
|
||||
$article_authors[] = $author_name;
|
||||
}
|
||||
$og_author = implode(', ', $article_authors);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback vers l'auteur WordPress standard
|
||||
if (empty($og_author)) {
|
||||
$og_author = get_the_author_meta('display_name');
|
||||
}
|
||||
}
|
||||
|
||||
// Date de publication (pour les articles)
|
||||
$published_time = '';
|
||||
$modified_time = '';
|
||||
if (is_single()) {
|
||||
$published_time = get_the_date('c');
|
||||
$modified_time = get_the_modified_date('c');
|
||||
}
|
||||
|
||||
// Générer les balises meta
|
||||
$meta_tags = array();
|
||||
|
||||
// Open Graph
|
||||
$meta_tags[] = '<meta property="og:title" content="' . esc_attr($og_title) . '" />';
|
||||
$meta_tags[] = '<meta property="og:description" content="' . esc_attr($og_description) . '" />';
|
||||
$meta_tags[] = '<meta property="og:type" content="' . esc_attr($og_type) . '" />';
|
||||
$meta_tags[] = '<meta property="og:url" content="' . esc_url($og_url) . '" />';
|
||||
$meta_tags[] = '<meta property="og:site_name" content="' . esc_attr($site_name) . '" />';
|
||||
|
||||
if (!empty($og_image)) {
|
||||
$meta_tags[] = '<meta property="og:image" content="' . esc_url($og_image) . '" />';
|
||||
$meta_tags[] = '<meta property="og:image:alt" content="' . esc_attr($og_title) . '" />';
|
||||
}
|
||||
|
||||
if (!empty($og_author)) {
|
||||
$meta_tags[] = '<meta property="article:author" content="' . esc_attr($og_author) . '" />';
|
||||
}
|
||||
|
||||
if (!empty($published_time)) {
|
||||
$meta_tags[] = '<meta property="article:published_time" content="' . esc_attr($published_time) . '" />';
|
||||
}
|
||||
|
||||
if (!empty($modified_time)) {
|
||||
$meta_tags[] = '<meta property="article:modified_time" content="' . esc_attr($modified_time) . '" />';
|
||||
}
|
||||
|
||||
// Ajouter les tags/sections pour les articles
|
||||
if (is_single() && get_post_type() === 'articles') {
|
||||
$article_tags = get_the_tags();
|
||||
if ($article_tags) {
|
||||
foreach ($article_tags as $tag) {
|
||||
$meta_tags[] = '<meta property="article:tag" content="' . esc_attr($tag->name) . '" />';
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter la section/revue si disponible
|
||||
$related_revue = get_field('related_revue');
|
||||
if ($related_revue) {
|
||||
$meta_tags[] = '<meta property="article:section" content="' . esc_attr($related_revue->post_title) . '" />';
|
||||
}
|
||||
}
|
||||
|
||||
// Twitter Cards
|
||||
$meta_tags[] = '<meta name="twitter:card" content="summary_large_image" />';
|
||||
$meta_tags[] = '<meta name="twitter:title" content="' . esc_attr($og_title) . '" />';
|
||||
$meta_tags[] = '<meta name="twitter:description" content="' . esc_attr($og_description) . '" />';
|
||||
|
||||
if (!empty($og_image)) {
|
||||
$meta_tags[] = '<meta name="twitter:image" content="' . esc_url($og_image) . '" />';
|
||||
}
|
||||
|
||||
// Meta description standard
|
||||
$meta_tags[] = '<meta name="description" content="' . esc_attr($og_description) . '" />';
|
||||
|
||||
// Canonical URL
|
||||
$meta_tags[] = '<link rel="canonical" href="' . esc_url($og_url) . '" />';
|
||||
|
||||
return implode("\n\t", $meta_tags);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user