Advertisement
Guest User

Untitled

a guest
May 12th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.77 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: Comment Rating Field Plugin
  4. * Plugin URI: http://www.wpcube.co.uk/crfp
  5. * Version: 1.5
  6. * Author: <a href="http://www.wpcube.co.uk/">WP Cube</a>
  7. * Description: Adds a 5 star rating field to the comments form in WordPress. Requires WordPress 3.0+
  8. */
  9.  
  10. /**
  11. * Comment Rating Field Plugin Class
  12. *
  13. * @package WordPress
  14. * @subpackage Comment Rating Field Plugin
  15. * @author Tim Carr
  16. * @version 1.5
  17. * @copyright WP Cube
  18. */
  19. class CommentRatingFieldPlugin {
  20. /**
  21. * Constructor.
  22. */
  23. function CommentRatingFieldPlugin() {
  24. // Plugin Details
  25. $this->plugin->name = 'comment-rating-field-plugin';
  26. $this->plugin->displayName = 'Comment Rating Field Plugin';
  27. $this->plugin->url = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
  28.  
  29. // Settings
  30. $this->settings = get_option($this->plugin->name);
  31.  
  32. add_action('comment_post', array(&$this, 'SaveRating')); // Save Rating Field on Comment Post
  33. add_action('comment_text', array(&$this, 'DisplayRating')); // Displays Rating on Comments
  34. add_filter('the_content', array(&$this, 'DisplayAverageRating')); // Displays Average Rating below Content
  35.  
  36. if (is_admin()) {
  37. add_action('admin_menu', array(&$this, 'AddAdminMenu'));
  38. add_action('wp_set_comment_status', array(&$this, 'UpdatePostRatingByCommentID')); // Recalculate average rating on comment approval / hold / spam
  39. add_action('deleted_comment', array(&$this, 'UpdatePostRatingByCommentID')); // Recalculate average rating on comment delete
  40. } else {
  41. // Register and load CSS
  42. wp_register_style('crfp-rating-css', $this->plugin->url.'css/rating.css');
  43. wp_enqueue_style('crfp-rating-css');
  44.  
  45. // Register and load JS
  46. wp_enqueue_script('jquery');
  47. wp_enqueue_script('crfp-jquery-rating-pack', $this->plugin->url.'js/jquery.rating.pack.js', 'jquery', false, true);
  48.  
  49. // Action and Filter Hooks
  50. add_action('wp_footer', array(&$this, 'DisplayRatingField')); // Displays Rating Field on Comments Form
  51. }
  52. }
  53.  
  54. /**
  55. * Adds a single option panel to Wordpress Administration
  56. */
  57. function AddAdminMenu() {
  58. add_menu_page($this->plugin->displayName, $this->plugin->displayName, 9, $this->plugin->name, array(&$this, 'AdminPanel'), $this->plugin->url.'images/icons/crfp-small.png');
  59. }
  60.  
  61. /**
  62. * Outputs the plugin Admin Panel in Wordpress Admin
  63. */
  64. function AdminPanel() {
  65. // Save Settings
  66. if (isset($_POST['submit'])) {
  67. update_option($this->plugin->name, $_POST[$this->plugin->name]);
  68. $this->message = __('Settings Updated.');
  69. }
  70.  
  71. // Load form
  72. $this->settings = get_option($this->plugin->name);
  73. include_once(WP_PLUGIN_DIR.'/'.$this->plugin->name.'/admin/settings.php');
  74. }
  75.  
  76. /**
  77. * Saves the POSTed rating for the given comment ID to the comment meta table,
  78. * as well as storing the total ratings and average on the post itself.
  79. *
  80. * @param int $commentID
  81. */
  82. function SaveRating($commentID) {
  83. // Save rating against comment
  84. add_comment_meta($commentID, 'crfp-rating', $_POST['crfp-rating'], true);
  85.  
  86. // Get post ID from comment and store total and average ratings against post
  87. // Run here in case comments are set to always be approved
  88. $this->UpdatePostRatingByCommentID($commentID);
  89. }
  90.  
  91. /**
  92. * Calculates the average rating and total number of ratings
  93. * for the given post ID, storing it in the post meta.
  94. *
  95. * @param int @postID Post ID
  96. * @return bool Rating Updated
  97. */
  98. function UpdatePostRatingByPostID($postID) {
  99. global $wpdb;
  100.  
  101. // Calculate average rating and total ratings
  102. $results = $wpdb->get_results(" SELECT ".$wpdb->prefix."commentmeta.meta_value
  103. FROM ".$wpdb->prefix."comments
  104. LEFT JOIN ".$wpdb->prefix."commentmeta
  105. ON ".$wpdb->prefix."comments.comment_ID = ".$wpdb->prefix."commentmeta.comment_id
  106. WHERE ".$wpdb->prefix."comments.comment_post_ID = '".mysql_real_escape_string($postID)."'
  107. AND ".$wpdb->prefix."comments.comment_approved = '1'
  108. AND ".$wpdb->prefix."commentmeta.meta_key = 'crfp-rating'
  109. AND ".$wpdb->prefix."commentmeta.meta_value != 0
  110. GROUP BY ".$wpdb->prefix."commentmeta.comment_id");
  111.  
  112. if (count($results) == 0) {
  113. $totalRatings = 0;
  114. $averageRating = 0;
  115. } else {
  116. $totalRatings = count($results);
  117. foreach ($results as $key=>$result) $totalRating += $result->meta_value;
  118. $averageRating = (($totalRatings == 0 OR $totalRating == 0) ? 0 : round(($totalRating / $totalRatings), 0));
  119. }
  120.  
  121. update_post_meta($postID, 'crfp-total-ratings', $totalRatings);
  122. update_post_meta($postID, 'crfp-average-rating', $averageRating);
  123.  
  124. return true;
  125. }
  126.  
  127. /**
  128. * Called by WP action, passes function call to UpdatePostRatingByPostID
  129. *
  130. * @param int $commentID Comment ID
  131. * @return int Comment ID
  132. */
  133. function UpdatePostRatingByCommentID($commentID) {
  134. $comment = get_comment($commentID);
  135. $this->UpdatePostRatingByPostID($comment->comment_post_ID);
  136. return true;
  137. }
  138.  
  139. /**
  140. * Checks if the post can have a rating
  141. *
  142. * @return bool Post can have rating
  143. */
  144. function PostCanHaveRating() {
  145. global $post;
  146.  
  147. $displayRatingField = false; // Don't display rating field by default
  148. wp_reset_query(); // Reset to default loop query so we can test if a single Page or Post
  149.  
  150. if (!is_array($this->settings)) return; // No settings defined
  151. if ($post->comment_status != 'open') return; // Comments are no longer open
  152. if (!is_singular()) return; // Not a single Post
  153.  
  154. // Check if post type is enabled
  155. $type = get_post_type($post->ID);
  156. if (is_array($this->settings['enabled']) AND $this->settings['enabled'][$type]) {
  157. // Post type enabled, regardless of taxonomies
  158. $displayRatingField = true;
  159. } elseif (is_array($this->settings['taxonomies'])) {
  160. // Get all terms assigned to this Post
  161. // Check if we need to display ratings here
  162. $taxonomies = get_taxonomies();
  163. $ignoreTaxonomies = array('post_tag', 'nav_menu', 'link_category', 'post_format');
  164. foreach ($taxonomies as $key=>$taxonomyProgName) {
  165. if (in_array($taxonomyProgName, $ignoreTaxonomies)) continue; // Skip ignored taxonomies
  166. if (!is_array($this->settings['taxonomies'][$taxonomyProgName])) continue; // Skip this taxonomy
  167.  
  168. // Get terms and build array of term IDs
  169. unset($terms, $termIDs);
  170. $terms = wp_get_post_terms($post->ID, $taxonomyProgName);
  171. foreach ($terms as $key=>$term) $termIDs[] = $term->term_id;
  172.  
  173. // Check if any of the post term IDs have been selected within the plugin
  174. if ($termIDs) {
  175. foreach ($this->settings['taxonomies'][$taxonomyProgName] as $termID=>$intVal) {
  176. if (in_array($termID, $termIDs)) {
  177. $displayRatingField = true;
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. }
  184.  
  185. return $displayRatingField;
  186. }
  187.  
  188. /**
  189. * Displays the Average Rating below the Content, if required
  190. *
  191. * @param string $content Post Content
  192. * @return string Post Content w/ Ratings HTML
  193. */
  194. function DisplayAverageRating($content) {
  195. global $post;
  196.  
  197. if (!$this->settings['enabled']['average']) return $content; // Don't display average
  198. $averageRating = get_post_meta($post->ID, 'crfp-average-rating', true); // Get average rating
  199.  
  200. // Check if the meta key exists; if not go and run the calculation
  201. if ($averageRating == '') {
  202. $this->UpdatePostRatingByPostID($post->ID);
  203. $averageRating = get_post_meta($post->ID, 'crfp-average-rating', true); // Get average rating
  204. }
  205.  
  206. // If still no rating, a rating has never been left, so don't display one
  207. if ($averageRating == '' OR $averageRating == 0) return $content;
  208.  
  209. // Build rating HTML
  210. $ratingHTML = '<div class="crfp-average-rating">'.$this->settings['averageRatingText'].'<div class="crfp-rating crfp-rating-'.$averageRating.'"></div></div>';
  211.  
  212. // Return rating widget with content
  213. return $content.$ratingHTML;
  214. }
  215.  
  216. /**
  217. * Appends the rating to the end of the comment text for the given comment ID
  218. *
  219. * @param text $comment
  220. */
  221. function DisplayRating($comment) {
  222. global $post;
  223.  
  224. $commentID = get_comment_ID();
  225.  
  226. // Check whether we need to display ratings
  227. if (!$this->display) { // Prevents checking for every comment in a single Post
  228. $this->display = $this->PostCanHaveRating();
  229. }
  230.  
  231. // Display rating?
  232. if ($this->display) {
  233. $rating = get_comment_meta($commentID, 'crfp-rating', true);
  234. if ($rating == '') $rating = 0;
  235. return $comment.'<div class="rating'.($this->settings['displayStyle'] == 'grey' ? ' rating-always-on' : '').'"><div class="crfp-rating crfp-rating-'.$rating.'">'.$rating.'</div></div>';
  236. }
  237.  
  238. // Just return comment without rating
  239. return $comment;
  240. }
  241.  
  242. /**
  243. * Appends the rating field to the end of the comment form, if required
  244. */
  245. function DisplayRatingField() {
  246. global $post;
  247.  
  248. if (!$this->PostCanHaveRating()) return;
  249.  
  250. // If here, output rating field
  251. $label = (($this->settings['ratingFieldLabel'] != '') ? '<label for="rating-star">'.$this->settings['ratingFieldLabel'].'</label>' : '');
  252. $field = $label.'<input name="rating-star" type="radio" class="star" value="1" /><input name="rating-star" type="radio" class="star" value="2" /><input name="rating-star" type="radio" class="star" value="3" /><input name="rating-star" type="radio" class="star" value="4" /><input name="rating-star" type="radio" class="star" value="5" /><input type="hidden" name="crfp-rating" value="0" />';
  253. ?>
  254. <script type="text/javascript">
  255. jQuery(document).ready(function($) {
  256. if ($('form#commentform textarea[name=comment]').length > 0) {
  257. // If parent tag is a container for the comment field, append rating after the parent
  258. var commentField = $('form#commentform textarea[name=comment]');
  259. var parentTagName = $(commentField).parent().get(0).tagName;
  260. if (parentTagName == 'P' || parentTagName == 'DIV' || parentTagName == 'LI') {
  261. // Append rating field as a new element
  262. $(commentField).parent().after('<'+parentTagName+' class="crfp-field"><?php echo $field; ?></'+parentTagName+'>');
  263. } else {
  264. // Append rating field straight after comment field
  265. $(commentField).after('<?php echo $field; ?>');
  266. }
  267.  
  268. $('input.star').rating(); // Invoke rating plugin
  269. $('div.star-rating a').bind('click', function(e) { $('input[name=crfp-rating]').val($(this).html()); }); // Stores rating in hidden field ready for POST
  270. $('div.rating-cancel a').bind('click', function(e) { $('input[name=crfp-rating]').val('0'); }); // Stores rating in hidden field ready for POST
  271. }
  272. });
  273. </script>
  274. <?php
  275. }
  276. }
  277. $crfp = new CommentRatingFieldPlugin(); // Invoke class
  278. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement