From db5972c2b7082c6671087d179c86b8d6204760da Mon Sep 17 00:00:00 2001 From: Antoine M Date: Tue, 24 Feb 2026 17:29:25 +0100 Subject: [PATCH] FEATURE Introducxing new utilities functions --- includes/utilities.php | 169 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) diff --git a/includes/utilities.php b/includes/utilities.php index bbaf116..c1304a7 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -104,3 +104,172 @@ function hasPostTypeNumerotation($post_type) return false; } } + + +/** + * Retourne une WP_Query avec la dernière analyse et la dernière étude (une de chaque type). + * Utilisable comme une query classique (->posts, ->post_count, etc.). + */ +function get_last_analyses_etudes_posts() +{ + $latest_analyse = get_posts(array( + 'posts_per_page' => 1, + 'post_status' => 'publish', + 'post_type' => 'analyses-etudes', + 'orderby' => 'date', + 'order' => 'DESC', + 'tax_query' => array( + array( + 'taxonomy' => 'type', + 'field' => 'slug', + 'terms' => 'analyse', + ), + ), + )); + $latest_etude = get_posts(array( + 'posts_per_page' => 1, + 'post_status' => 'publish', + 'post_type' => 'analyses-etudes', + 'orderby' => 'date', + 'order' => 'DESC', + 'tax_query' => array( + array( + 'taxonomy' => 'type', + 'field' => 'slug', + 'terms' => 'etude', + ), + ), + )); + + $post_ids = array_merge( + array_map(function ($p) { + return $p->ID; + }, $latest_analyse), + array_map(function ($p) { + return $p->ID; + }, $latest_etude) + ); + $post_ids = array_filter(array_unique($post_ids)); + + if (empty($post_ids)) { + return new WP_Query(array( + 'post_type' => 'analyses-etudes', + 'posts_per_page' => 0, + )); + } + + return new WP_Query(array( + 'post_type' => 'analyses-etudes', + 'post__in' => $post_ids, + 'orderby' => 'post__in', + 'posts_per_page' => count($post_ids), + )); +} + + +/** + * 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_post_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, + 'postUrl' => $postUrl + ); +} + + +function get_author_publications_amount($authorID) +{ + if (empty($authorID)) return 0; + if (get_current_blog_id() === 1) { + $posts = count_user_posts_by_author($authorID, array('expositions', 'outils-pedagogiques', 'analyses-etudes')); + return $posts; + } + if (get_current_blog_id() === 2) { + $posts = count_user_posts_by_author($authorID, 'articles'); + return $posts; + } + + return; +} + + + + +/** + * Compte le nombre d'articles d'un utilisateur pour un type de post donné + * + * Cette fonction compte combien d'articles d'un type spécifique + * sont associés à un utilisateur donné via le champ custom 'authors'. + * + * @param int $userID L'ID de l'utilisateur + * @param string $postType Le type de post à compter (ex: 'articles') + * @return int Le nombre d'articles trouvés + */ +function count_user_posts_by_author($userID, $postType) +{ + $args = array( + 'post_type' => $postType, + 'meta_query' => array( + array( + 'key' => 'authors', + 'value' => '"' . $userID . '"', + 'compare' => 'LIKE', + ), + ), + ); + $query = new WP_Query($args); + return $query->found_posts; +}