Skip to content

Commit

Permalink
v 0.2.0
Browse files Browse the repository at this point in the history
Edit rating in comment page
  • Loading branch information
Darklg committed Dec 8, 2024
1 parent f486dff commit 43881a0
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 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.1.2
Version: 0.2.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.1.2';
private $plugin_version = '0.2.0';
private $plugin_settings = array(
'id' => 'wpu_comments_rating',
'name' => 'WPU Comments Rating'
Expand Down Expand Up @@ -69,6 +69,15 @@ public function __construct() {

# Display rating in comments
add_filter('comment_text', array($this, 'comment_text'), 10, 3);

/* Edit rating in comment page */
add_action('add_meta_boxes_comment', array(&$this, 'add_meta_boxes_comment'));
add_action('edit_comment', function ($comment_id) {
if (!isset($_POST['rating']) || !ctype_digit($_POST['rating']) || $_POST['rating'] < 1 || $_POST['rating'] > $this->max_rating) {
return;
}
update_comment_meta($comment_id, 'wpu_comment_rating', intval($_POST['rating']));
}, 10, 2);
}

public function plugins_loaded() {
Expand Down Expand Up @@ -271,6 +280,24 @@ public function comments_get_rating_html($note) {
return $html;
}

public function add_meta_boxes_comment($comment) {
add_meta_box('wpu_comment_rating', __('Rating', 'wpu_comments_rating'), array(&$this, 'wpu_comment_rating'), null, 'normal', 'high');
}

public function wpu_comment_rating($comment) {
$rating = get_comment_meta($comment->comment_ID, 'wpu_comment_rating', true);
echo '<fieldset class="comments-rating">';
echo '<span class="rating-container">';
for ($i = 1; $i <= $this->max_rating; $i++):
echo '<span style="margin-right:0.5em;" class="rating-item-' . $i . '">';
echo '<span><input type="radio" id="rating-' . esc_attr($i) . '" name="rating" value="' . esc_attr($i) . '" ' . checked($rating, $i, false) . ' /></span>';
echo '<label for="rating-' . esc_attr($i) . '">' . $this->star_icon_vote . '</label>';
echo '</span>';
endfor;
echo '</span>';
echo '</fieldset>';
}

}

$WPUCommentsRating = new WPUCommentsRating();
Expand Down

0 comments on commit 43881a0

Please sign in to comment.