diff --git a/lang/wpu_comments_rating-fr_FR.po b/lang/wpu_comments_rating-fr_FR.po index f3d3d7d..4bd7fe2 100644 --- a/lang/wpu_comments_rating-fr_FR.po +++ b/lang/wpu_comments_rating-fr_FR.po @@ -1,7 +1,7 @@ msgid "" msgstr "" "Project-Id-Version: WPU Comments Rating\n" -"POT-Creation-Date: 2024-12-04 22:33+0100\n" +"POT-Creation-Date: 2024-12-10 18:51+0100\n" "PO-Revision-Date: \n" "Last-Translator: \n" "Language-Team: \n" @@ -25,24 +25,24 @@ msgstr "" "X-Poedit-SearchPathExcluded-8: inc/WPUBaseToolbox\n" "X-Poedit-SearchPathExcluded-9: inc/WPUBaseUpdate\n" -#: .././wpu_comments_rating.php:92 +#: .././wpu_comments_rating.php:102 msgid "Allow users to rate in comments." msgstr "Permettez aux utilisateurs de noter dans les commentaires." -#: .././wpu_comments_rating.php:119 +#: .././wpu_comments_rating.php:130 msgid "Global note" msgstr "Note globale" -#: .././wpu_comments_rating.php:215 +#: .././wpu_comments_rating.php:234 .././wpu_comments_rating.php:305 msgid "Rating" msgstr "Note" -#: .././wpu_comments_rating.php:228 +#: .././wpu_comments_rating.php:247 #, php-format msgid "Rating : %s" msgstr "Note : %s" -#: .././wpu_comments_rating.php:236 +#: .././wpu_comments_rating.php:251 #, php-format msgid "Number of ratings : %d" msgstr "Nombre de notes : %d" diff --git a/wpu_comments_rating.php b/wpu_comments_rating.php index 24d749e..e59c884 100644 --- a/wpu_comments_rating.php +++ b/wpu_comments_rating.php @@ -4,7 +4,7 @@ Plugin URI: https://github.com/WordPressUtilities/wpu_comments_rating Update URI: https://github.com/WordPressUtilities/wpu_comments_rating Description: Allow users to rate in comments. -Version: 0.2.0 +Version: 0.3.0 Author: Darklg Author URI: https://darklg.me/ Text Domain: wpu_comments_rating @@ -22,7 +22,7 @@ } class WPUCommentsRating { - private $plugin_version = '0.2.0'; + private $plugin_version = '0.3.0'; private $plugin_settings = array( 'id' => 'wpu_comments_rating', 'name' => 'WPU Comments Rating' @@ -176,13 +176,21 @@ public function save_rating($comment_id, $comment_approved) { public function update_post_rating($post_id) { global $wpdb; + + /* Average rating */ $q = $wpdb->prepare("SELECT AVG(meta_value) FROM $wpdb->commentmeta WHERE meta_key = 'wpu_comment_rating' AND comment_id IN(SELECT comment_ID FROM $wpdb->comments WHERE comment_approved='1' && comment_post_ID=%d)", $post_id); $median = $wpdb->get_var($q); if (!is_numeric($median)) { $median = 0; } - $median = round($median, 2); - update_post_meta($post_id, 'wpu_post_rating', $median); + update_post_meta($post_id, 'wpu_post_rating', round($median, 2)); + + /* Number of ratings */ + $q = $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->commentmeta WHERE meta_key = 'wpu_comment_rating' AND comment_id IN(SELECT comment_ID FROM $wpdb->comments WHERE comment_approved='1' && comment_post_ID=%d)", $post_id); + $count = $wpdb->get_var($q); + if (is_numeric($count)) { + update_post_meta($post_id, 'wpu_post_rating_count', $count); + } } public function get_comment_parent($comment_id) { @@ -239,15 +247,28 @@ public function display_metabox($post) { echo wpautop(sprintf(__('Rating : %s', 'wpu_comments_rating'), $rating)); /* Display number of ratings */ - $comments = get_comments(array( - 'post_id' => $post->ID, - 'status' => 'approve', - 'meta_key' => 'wpu_comment_rating' - )); - echo wpautop(sprintf(__('Number of ratings : %d', 'wpu_comments_rating'), count($comments))); + $rating_count = $this->get_post_rating_count($post->ID); + echo wpautop(sprintf(__('Number of ratings : %d', 'wpu_comments_rating'), $rating_count)); } + public function get_post_rating_count($post_id) { + $rating_count = get_post_meta($post_id, 'wpu_post_rating_count', true); + if (!$rating_count) { + $comments = get_comments(array( + 'post_id' => $post_id, + 'status' => 'approve', + 'meta_key' => 'wpu_comment_rating' + )); + $rating_count = count($comments); + update_post_meta($post_id, 'wpu_post_rating_count', $rating_count); + } + if (!$rating_count) { + $rating_count = 0; + } + return $rating_count; + } + public function comments_get_post_rating_html($post_id) { $note = get_post_meta($post_id, 'wpu_post_rating', 1); if (!$note) {