Skip to content

Commit

Permalink
v 0.3.0
Browse files Browse the repository at this point in the history
- Save Post rating count.
  • Loading branch information
Darklg committed Dec 10, 2024
1 parent 43881a0 commit 5817a76
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lang/wpu_comments_rating-fr_FR.po
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
41 changes: 31 additions & 10 deletions wpu_comments_rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5817a76

Please sign in to comment.