Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 74.77 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP-PostRatings
  4. Plugin URI: http://lesterchan.net/portfolio/programming/php/
  5. Description: Adds an AJAX rating system for your WordPress blog's post/page.
  6. Version: 1.80
  7. Author: Lester 'GaMerZ' Chan
  8. Author URI: http://lesterchan.net
  9. Text Domain: wp-postratings
  10. */
  11.  
  12.  
  13. /*
  14. Copyright 2014 Lester Chan (email : lesterchan@gmail.com)
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30.  
  31. // Exit if accessed directly
  32. if ( ! defined( 'ABSPATH' ) ) exit;
  33.  
  34. ### Version
  35. define( 'WP_POSTRATINGS_VERSION', 1.80 );
  36.  
  37. ### Define Image Extension
  38. define('RATINGS_IMG_EXT', 'gif');
  39. //define('RATINGS_IMG_EXT', 'png');
  40.  
  41. ### Create Text Domain For Translations
  42. add_action( 'plugins_loaded', 'postratings_textdomain' );
  43. function postratings_textdomain() {
  44. load_plugin_textdomain( 'wp-postratings', false, dirname( plugin_basename( __FILE__ ) ) );
  45. }
  46.  
  47.  
  48. ### Rating Logs Table Name
  49. global $wpdb;
  50. $wpdb->ratings = $wpdb->prefix.'ratings';
  51.  
  52.  
  53. ### Function: Ratings Administration Menu
  54. add_action('admin_menu', 'ratings_menu');
  55. function ratings_menu() {
  56. add_menu_page(__('Ratings', 'wp-postratings'), __('Ratings', 'wp-postratings'), 'manage_ratings', 'wp-postratings/postratings-manager.php', '', 'dashicons-star-filled');
  57.  
  58. add_submenu_page('wp-postratings/postratings-manager.php', __('Manage Ratings', 'wp-postratings'), __('Manage Ratings', 'wp-postratings'), 'manage_ratings', 'wp-postratings/postratings-manager.php');
  59. add_submenu_page('wp-postratings/postratings-manager.php', __('Ratings Options', 'wp-postratings'), __('Ratings Options', 'wp-postratings'), 'manage_ratings', 'wp-postratings/postratings-options.php');
  60. add_submenu_page('wp-postratings/postratings-manager.php', __('Ratings Templates', 'wp-postratings'), __('Ratings Templates', 'wp-postratings'), 'manage_ratings', 'wp-postratings/postratings-templates.php');
  61. }
  62.  
  63.  
  64. ### Function: Display The Rating For The Post
  65. function the_ratings($start_tag = 'div', $custom_id = 0, $display = true) {
  66. global $id;
  67. // Allow Custom ID
  68. if(intval($custom_id) > 0) {
  69. $ratings_id = $custom_id;
  70. } else {
  71. // If Global $id is 0, Get The Loop Post ID
  72. if($id === 0) {
  73. $ratings_id = get_the_ID();
  74. } elseif (is_null($id)) {
  75. global $post;
  76. $ratings_id = $post->ID;
  77. } else {
  78. $ratings_id = $id;
  79. }
  80. }
  81.  
  82. // Loading Style
  83. $postratings_ajax_style = get_option('postratings_ajax_style');
  84. if(intval($postratings_ajax_style['loading']) == 1) {
  85. $loading = '<' . $start_tag . ' id="post-ratings-' . $ratings_id . '-loading" class="post-ratings-loading">
  86. <img src="' . plugins_url('wp-postratings/images/loading.gif') . '" width="16" height="16" alt="' . __( 'Loading...', 'wp-postratings' ) . '" title="' . __( 'Loading...', 'wp-postratings' ) . '" class="post-ratings-image" />' . __( 'Loading...', 'wp-postratings' ) . '</' . $start_tag . '>';
  87. } else {
  88. $loading = '';
  89. }
  90. // Check To See Whether User Has Voted
  91. $user_voted = check_rated($ratings_id);
  92. // HTML Attributes
  93. if( is_single() || is_page() ) {
  94. $itemtype = apply_filters('wp_postratings_schema_itemtype', 'itemscope itemtype="http://schema.org/Article"');
  95. $attributes = 'id="post-ratings-'.$ratings_id.'" class="post-ratings" '.$itemtype;
  96. } else {
  97. $attributes = 'id="post-ratings-'.$ratings_id.'" class="post-ratings"';
  98. }
  99. // If User Voted Or Is Not Allowed To Rate
  100. if($user_voted) {
  101. if(!$display) {
  102. return "<$start_tag $attributes>".the_ratings_results($ratings_id).'</'.$start_tag.'>'.$loading;
  103. } else {
  104. echo "<$start_tag $attributes>".the_ratings_results($ratings_id).'</'.$start_tag.'>'.$loading;
  105. return;
  106. }
  107. // If User Is Not Allowed To Rate
  108. } else if(!check_allowtorate()) {
  109. if(!$display) {
  110. return "<$start_tag $attributes>".the_ratings_results($ratings_id, 0, 0, 0, 1).'</'.$start_tag.'>'.$loading;
  111. } else {
  112. echo "<$start_tag $attributes>".the_ratings_results($ratings_id, 0, 0, 0, 1).'</'.$start_tag.'>'.$loading;
  113. return;
  114. }
  115. // If User Has Not Voted
  116. } else {
  117. if(!$display) {
  118. return "<$start_tag $attributes data-nonce=\"".wp_create_nonce('postratings_'.$ratings_id.'-nonce')."\">".the_ratings_vote($ratings_id).'</'.$start_tag.'>'.$loading;
  119. } else {
  120. echo "<$start_tag $attributes data-nonce=\"".wp_create_nonce('postratings_'.$ratings_id.'-nonce')."\">".the_ratings_vote($ratings_id).'</'.$start_tag.'>'.$loading;
  121. return;
  122. }
  123. }
  124. }
  125.  
  126.  
  127. ### Function: Print Out jQuery Script At The Top
  128. add_action('wp_head', 'ratings_javascripts_header');
  129. function ratings_javascripts_header() {
  130. wp_print_scripts('jquery');
  131. }
  132.  
  133.  
  134. ### Function: Enqueue Ratings JavaScripts/CSS
  135. add_action('wp_enqueue_scripts', 'ratings_scripts');
  136. function ratings_scripts() {
  137. if(@file_exists(TEMPLATEPATH.'/postratings-css.css')) {
  138. wp_enqueue_style('wp-postratings', get_stylesheet_directory_uri().'/postratings-css.css', false, WP_POSTRATINGS_VERSION, 'all');
  139. } else {
  140. wp_enqueue_style('wp-postratings', plugins_url('wp-postratings/postratings-css.css'), false, WP_POSTRATINGS_VERSION, 'all');
  141. }
  142. if(is_rtl()) {
  143. if(@file_exists(TEMPLATEPATH.'/postratings-css-rtl.css')) {
  144. wp_enqueue_style('wp-postratings-rtl', get_stylesheet_directory_uri().'/postratings-css-rtl.css', false, WP_POSTRATINGS_VERSION, 'all');
  145. } else {
  146. wp_enqueue_style('wp-postratings-rtl', plugins_url('wp-postratings/postratings-css-rtl.css'), false, WP_POSTRATINGS_VERSION, 'all');
  147. }
  148. }
  149. $postratings_max = intval(get_option('postratings_max'));
  150. $postratings_custom = intval(get_option('postratings_customrating'));
  151. $postratings_ajax_style = get_option('postratings_ajax_style');
  152. $postratings_javascript = '';
  153. if($postratings_custom) {
  154. for($i = 1; $i <= $postratings_max; $i++) {
  155. $postratings_javascript .= 'var ratings_'.$i.'_mouseover_image=new Image();ratings_'.$i.'_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_'.$i.'_over."+ratingsL10n.image_ext;';
  156. }
  157. } else {
  158. $postratings_javascript = 'var ratings_mouseover_image=new Image();ratings_mouseover_image.src=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_over."+ratingsL10n.image_ext;';
  159. }
  160. wp_enqueue_script('wp-postratings', plugins_url('wp-postratings/postratings-js.js'), array('jquery'), WP_POSTRATINGS_VERSION, true);
  161. wp_localize_script('wp-postratings', 'ratingsL10n', array(
  162. 'plugin_url' => plugins_url('wp-postratings'),
  163. 'ajax_url' => admin_url('admin-ajax.php'),
  164. 'text_wait' => __('Please rate only 1 post at a time.', 'wp-postratings'),
  165. 'image' => get_option('postratings_image'),
  166. 'image_ext' => RATINGS_IMG_EXT,
  167. 'max' => $postratings_max,
  168. 'show_loading' => intval($postratings_ajax_style['loading']),
  169. 'show_fading' => intval($postratings_ajax_style['fading']),
  170. 'custom' => $postratings_custom,
  171. 'l10n_print_after' => $postratings_javascript
  172. ));
  173. }
  174.  
  175.  
  176. ### Function: Enqueue Ratings Stylesheets/JavaScripts In WP-Admin
  177. add_action('admin_enqueue_scripts', 'ratings_scripts_admin');
  178. function ratings_scripts_admin($hook_suffix) {
  179. $postratings_admin_pages = array('wp-postratings/postratings-manager.php', 'wp-postratings/postratings-options.php', 'wp-postratings/postratings-templates.php', 'wp-postratings/postratings-uninstall.php');
  180. if(in_array($hook_suffix, $postratings_admin_pages)) {
  181. wp_enqueue_style('wp-postratings-admin', plugins_url('wp-postratings/postratings-admin-css.css'), false, WP_POSTRATINGS_VERSION, 'all');
  182. wp_enqueue_script('wp-postratings-admin', plugins_url('wp-postratings/postratings-admin-js.js'), array('jquery'), WP_POSTRATINGS_VERSION, true);
  183. wp_localize_script('wp-postratings-admin', 'ratingsAdminL10n', array(
  184. 'admin_ajax_url' => admin_url('admin-ajax.php')
  185. ));
  186. }
  187. }
  188.  
  189.  
  190. ### Function: Display Ratings Results
  191. function the_ratings_results($post_id, $new_user = 0, $new_score = 0, $new_average = 0, $type = 0) {
  192. if($new_user == 0 && $new_score == 0 && $new_average == 0) {
  193. $post_ratings_data = null;
  194. } else {
  195. $post_ratings_data = new stdClass();
  196. $post_ratings_data->ratings_users = $new_user;
  197. $post_ratings_data->ratings_score = $new_score;
  198. $post_ratings_data->ratings_average = $new_average;
  199. }
  200. // Display The Contents
  201. if($type == 1) {
  202. $template_postratings_text = stripslashes(get_option('postratings_template_permission'));
  203. } else {
  204. $template_postratings_text = stripslashes(get_option('postratings_template_text'));
  205. }
  206. // Return Post Ratings Template
  207. return expand_ratings_template($template_postratings_text, $post_id, $post_ratings_data);
  208. }
  209.  
  210.  
  211. ### Function: Display Ratings Vote
  212. function the_ratings_vote($post_id, $new_user = 0, $new_score = 0, $new_average = 0) {
  213. if($new_user == 0 && $new_score == 0 && $new_average == 0) {
  214. $post_ratings_data = null;
  215. } else {
  216. $post_ratings_data = new stdClass();
  217. $post_ratings_data->ratings_users = $new_user;
  218. $post_ratings_data->ratings_score = $new_score;
  219. $post_ratings_data->ratings_average = $new_average;
  220. }
  221. // If No Ratings, Return No Ratings templae
  222. if(get_post_meta($post_id, 'ratings_users', true) == 0) {
  223. $template_postratings_none = stripslashes(get_option('postratings_template_none'));
  224. // Return Post Ratings Template
  225. return expand_ratings_template($template_postratings_none, $post_id, $post_ratings_data);
  226. } else {
  227. // Display The Contents
  228. $template_postratings_vote = stripslashes(get_option('postratings_template_vote'));
  229. // Return Post Ratings Voting Template
  230. return expand_ratings_template($template_postratings_vote, $post_id, $post_ratings_data);
  231. }
  232. }
  233.  
  234.  
  235. ### Function: Check Who Is Allow To Rate
  236. function check_allowtorate() {
  237. global $user_ID;
  238. $user_ID = intval($user_ID);
  239. $allow_to_vote = intval(get_option('postratings_allowtorate'));
  240. switch($allow_to_vote) {
  241. // Guests Only
  242. case 0:
  243. if($user_ID > 0) {
  244. return false;
  245. }
  246. return true;
  247. break;
  248. // Registered Users Only
  249. case 1:
  250. if($user_ID == 0) {
  251. return false;
  252. }
  253. return true;
  254. break;
  255. // Registered Users And Guests
  256. case 2:
  257. default:
  258. return true;
  259. }
  260. }
  261.  
  262.  
  263. ### Function: Check Whether User Have Rated For The Post
  264. function check_rated( $post_id ) {
  265. $postratings_logging_method = intval( get_option( 'postratings_logging_method' ) );
  266. $rated = false;
  267. switch( $postratings_logging_method ) {
  268. // Do Not Log
  269. case 0:
  270. $rated = false;
  271. break;
  272. // Logged By Cookie
  273. case 1:
  274. $rated = check_rated_cookie( $post_id );
  275. break;
  276. // Logged By IP
  277. case 2:
  278. $rated = check_rated_ip( $post_id );
  279. break;
  280. // Logged By Cookie And IP
  281. case 3:
  282. $rated_cookie = check_rated_cookie( $post_id );
  283. if( $rated_cookie > 0 ) {
  284. $rated = true;
  285. } else {
  286. $rated = check_rated_ip( $post_id );
  287. }
  288. break;
  289. // Logged By Username
  290. case 4:
  291. $rated = check_rated_username( $post_id );
  292. break;
  293. }
  294.  
  295. $rated = apply_filters( 'wp_postratings_check_rated', $rated );
  296.  
  297. return $rated;
  298. }
  299.  
  300.  
  301. ### Function: Check Rated By Cookie
  302. function check_rated_cookie($post_id) {
  303. if(isset($_COOKIE["rated_$post_id"])) {
  304. return true;
  305. } else {
  306. return false;
  307. }
  308. }
  309.  
  310.  
  311. ### Function: Check Rated By IP
  312. function check_rated_ip($post_id) {
  313. global $wpdb;
  314. // Check IP From IP Logging Database
  315. $get_rated = $wpdb->get_var( $wpdb->prepare( "SELECT rating_ip FROM {$wpdb->ratings} WHERE rating_postid = %d AND rating_ip = %s", $post_id, get_ipaddress() ) );
  316. // 0: False | > 0: True
  317. return intval($get_rated);
  318. }
  319.  
  320.  
  321. ### Function: Check Rated By Username
  322. function check_rated_username($post_id) {
  323. global $wpdb, $user_ID;
  324. if(!is_user_logged_in()) {
  325. return 0;
  326. }
  327. // Check User ID From IP Logging Database
  328. $get_rated = $wpdb->get_var( $wpdb->prepare( "SELECT rating_userid FROM {$wpdb->ratings} WHERE rating_postid = %d AND rating_userid = %d", $post_id, $user_ID ) );
  329. // 0: False | > 0: True
  330. return intval( $get_rated);
  331. }
  332.  
  333.  
  334. ### Function: Get Comment Authors Ratings
  335. add_action('loop_start', 'get_comment_authors_ratings');
  336. function get_comment_authors_ratings() {
  337. global $wpdb, $post, $comment_authors_ratings;
  338. $comment_authors_ratings_results = null;
  339. if(!is_feed() && !is_admin()) {
  340. $comment_authors_ratings = array();
  341. if($post && $post->ID) {
  342. $comment_authors_ratings_results = $wpdb->get_results( $wpdb->prepare( "SELECT rating_username, rating_rating, rating_ip FROM {$wpdb->ratings} WHERE rating_postid = %d", $post->ID ) );
  343. }
  344. if($comment_authors_ratings_results) {
  345. foreach($comment_authors_ratings_results as $comment_authors_ratings_result) {
  346. $comment_author = stripslashes($comment_authors_ratings_result->rating_username);
  347. $comment_authors_ratings[$comment_author] = $comment_authors_ratings_result->rating_rating;
  348. $comment_authors_ratings[$comment_authors_ratings_result->rating_ip] = $comment_authors_ratings_result->rating_rating;
  349. }
  350. }
  351. }
  352. }
  353.  
  354.  
  355. ### Function: Comment Author Ratings
  356. function comment_author_ratings($comment_author_specific = '', $display = true) {
  357. global $comment_authors_ratings;
  358. if(get_comment_type() == 'comment') {
  359. $post_ratings_images = '';
  360. $ratings_image = get_option('postratings_image');
  361. $ratings_max = intval(get_option('postratings_max'));
  362. $ratings_custom = intval(get_option('postratings_customrating'));
  363. if(empty($comment_author_specific)) {
  364. $comment_author = get_comment_author();
  365. } else {
  366. $comment_author = $comment_author_specific;
  367. }
  368. $comment_author_rating = intval($comment_authors_ratings[$comment_author]);
  369. if($comment_author_rating == 0) {
  370. $comment_author_rating = intval($comment_authors_ratings[get_comment_author_IP()]);
  371. }
  372. if($comment_author_rating != 0) {
  373. // Display Rated Images
  374. if($ratings_custom && $ratings_max == 2) {
  375. if($comment_author_rating > 0) {
  376. $comment_author_rating = '+'.$comment_author_rating;
  377. }
  378. }
  379. $image_alt = sprintf(__('%s gives a rating of %s', 'wp-postratings'), $comment_author, $comment_author_rating);
  380. $post_ratings_images = get_ratings_images_comment_author($ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt);
  381. }
  382. if($display) {
  383. return $post_ratings_images;
  384. } else {
  385. return $post_ratings_images;
  386. }
  387. }
  388. }
  389.  
  390.  
  391. ### Function: Display Comment Author Ratings
  392. //add_filter('comment_text', 'comment_author_ratings_filter');
  393. function comment_author_ratings_filter($comment_text) {
  394. global $comment, $comment_authors_ratings;
  395. $output = '';
  396. if(!is_feed() && !is_admin()) {
  397. if(get_comment_type() == 'comment') {
  398. $post_ratings_images = '';
  399. $ratings_image = get_option('postratings_image');
  400. $ratings_max = intval(get_option('postratings_max'));
  401. $ratings_custom = intval(get_option('postratings_customrating'));
  402. $comment_author = get_comment_author();
  403. $comment_author_rating = intval($comment_authors_ratings[$comment_author]);
  404. if($comment_author_rating == 0) {
  405. $comment_author_rating = intval($comment_authors_ratings[get_comment_author_IP()]);
  406. }
  407. if($comment_author_rating != 0) {
  408. // Display Rated Images
  409. if($ratings_custom && $ratings_max == 2) {
  410. if($comment_author_rating > 0) {
  411. $comment_author_rating = '+'.$comment_author_rating;
  412. }
  413. }
  414. $image_alt = sprintf(__('%s gives a rating of %s', 'wp-postratings'), $comment_author, $comment_author_rating);
  415. $post_ratings_images = get_ratings_images_comment_author($ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt);
  416. }
  417. $output .= '<div class="post-ratings-comment-author">';
  418. if($post_ratings_images != '') {
  419. $output .= get_comment_author().' ratings for this post: '.$post_ratings_images;
  420. } else {
  421. $output .= get_comment_author().' did not rate this post.';
  422. }
  423. $output .= '</div>';
  424. }
  425. }
  426. return $comment_text.$output;
  427. }
  428.  
  429.  
  430. ### Function: Get IP Address
  431. if(!function_exists('get_ipaddress')) {
  432. function get_ipaddress() {
  433. if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  434. $ip_address = $_SERVER["REMOTE_ADDR"];
  435. } else {
  436. $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
  437. }
  438. if(strpos($ip_address, ',') !== false) {
  439. $ip_address = explode(',', $ip_address);
  440. $ip_address = $ip_address[0];
  441. }
  442. return esc_attr($ip_address);
  443. }
  444. }
  445.  
  446.  
  447. ### Function: Return All Images From A Rating Image Folder
  448. function ratings_images_folder($folder_name) {
  449. $normal_images = array('rating_over.'.RATINGS_IMG_EXT, 'rating_on.'.RATINGS_IMG_EXT, 'rating_half.'.RATINGS_IMG_EXT, 'rating_off.'.RATINGS_IMG_EXT);
  450. $postratings_path = WP_PLUGIN_DIR.'/wp-postratings/images/'.$folder_name;
  451. $images_count_temp = 1;
  452. $images_count = 1;
  453. $count = 0;
  454. $rating['max'] = 0;
  455. $rating['custom'] = 0;
  456. $rating['images'] = array();
  457. if(is_dir($postratings_path)) {
  458. if($handle = @opendir($postratings_path)) {
  459. while (false !== ($filename = readdir($handle))) {
  460. if ($filename != '.' && $filename != '..' && substr($filename, -8) != '-rtl.'.RATINGS_IMG_EXT && strpos($filename, '.') !== 0) {
  461. if(in_array($filename, $normal_images)) {
  462. $count++;
  463. } elseif(intval(substr($filename,7, -7)) > $rating['max']) {
  464. $rating['max'] = intval(substr($filename,7, -7));
  465. }
  466. $rating['images'][] = $filename;
  467. $images_count++;
  468. }
  469. }
  470. closedir($handle);
  471. }
  472. }
  473. if($count != sizeof($normal_images)) {
  474. $rating['custom'] = 1;
  475. }
  476. if($rating['max'] == 0) {
  477. $rating['max'] = intval(get_option('postratings_max'));
  478. }
  479. return $rating;
  480. }
  481.  
  482.  
  483. ### Function: Add PostRatings To Post/Page Automatically
  484. //add_action('the_content', 'add_ratings_to_content');
  485. function add_ratings_to_content($content) {
  486. if (!is_feed()) {
  487. $content .= the_ratings('div', 0, false);
  488. }
  489. return $content;
  490. }
  491.  
  492.  
  493. ### Function: Short Code For Inserting Ratings Into Posts
  494. add_shortcode( 'ratings', 'ratings_shortcode' );
  495. function ratings_shortcode( $atts ) {
  496. $attributes = shortcode_atts( array( 'id' => 0, 'results' => false ), $atts );
  497. if( ! is_feed() ) {
  498. $id = intval( $attributes['id'] );
  499. if( $attributes['results'] ) {
  500. return the_ratings_results( $id );
  501. } else {
  502. return the_ratings( 'span', $id, false );
  503. }
  504. } else {
  505. return __( 'Note: There is a rating embedded within this post, please visit this post to rate it.', 'wp-postratings' );
  506. }
  507. }
  508.  
  509.  
  510. ### Function: Snippet Text
  511. if(!function_exists('snippet_text')) {
  512. function snippet_text($text, $length = 0) {
  513. if (defined('MB_OVERLOAD_STRING')) {
  514. $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
  515. if (mb_strlen($text) > $length) {
  516. return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
  517. } else {
  518. return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
  519. }
  520. } else {
  521. $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
  522. if (strlen($text) > $length) {
  523. return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
  524. } else {
  525. return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
  526. }
  527. }
  528. }
  529. }
  530.  
  531.  
  532. ### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected
  533. function ratings_post_excerpt($post_id, $post_excerpt, $post_content) {
  534. if(post_password_required($post_id)) {
  535. return __('There is no excerpt because this is a protected post.', 'wp-postratings');
  536. }
  537. if(empty($post_excerpt)) {
  538. return snippet_text(strip_tags($post_content), 200);
  539. } else {
  540. return $post_excerpt;
  541. }
  542. }
  543.  
  544.  
  545. ### Function: Add Rating Custom Fields
  546. add_action('publish_post', 'add_ratings_fields');
  547. add_action('publish_page', 'add_ratings_fields');
  548. function add_ratings_fields($post_ID) {
  549. global $wpdb;
  550. if(!wp_is_post_revision($post_ID)) {
  551. add_post_meta($post_ID, 'ratings_users', 0, true);
  552. add_post_meta($post_ID, 'ratings_score', 0, true);
  553. add_post_meta($post_ID, 'ratings_average', 0, true);
  554. }
  555. }
  556.  
  557.  
  558. ### Function:Delete Rating Custom Fields
  559. add_action('delete_post', 'delete_ratings_fields');
  560. function delete_ratings_fields($post_ID) {
  561. global $wpdb;
  562. if(!wp_is_post_revision($post_ID)) {
  563. delete_post_meta($post_ID, 'ratings_users');
  564. delete_post_meta($post_ID, 'ratings_score');
  565. delete_post_meta($post_ID, 'ratings_average');
  566. }
  567. }
  568.  
  569.  
  570. ### Function: Process Ratings
  571. add_action('wp_ajax_postratings', 'process_ratings');
  572. add_action('wp_ajax_nopriv_postratings', 'process_ratings');
  573. function process_ratings() {
  574. global $wpdb, $user_identity, $user_ID;
  575.  
  576. if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'postratings')
  577. {
  578. $rate = intval($_REQUEST['rate']);
  579. $post_id = intval($_REQUEST['pid']);
  580.  
  581. // Verify Referer
  582. if(!check_ajax_referer('postratings_'.$post_id.'-nonce', 'postratings_'.$post_id.'_nonce', false))
  583. {
  584. _e('Failed To Verify Referrer', 'wp-postratings');
  585. exit();
  586. }
  587.  
  588. if($rate > 0 && $post_id > 0 && check_allowtorate()) {
  589. // Check For Bot
  590. $bots_useragent = array('googlebot', 'google', 'msnbot', 'ia_archiver', 'lycos', 'jeeves', 'scooter', 'fast-webcrawler', 'slurp@inktomi', 'turnitinbot', 'technorati', 'yahoo', 'findexa', 'findlinks', 'gaisbo', 'zyborg', 'surveybot', 'bloglines', 'blogsearch', 'ubsub', 'syndic8', 'userland', 'gigabot', 'become.com');
  591. $useragent = $_SERVER['HTTP_USER_AGENT'];
  592. foreach ($bots_useragent as $bot) {
  593. if (stristr($useragent, $bot) !== false) {
  594. return;
  595. }
  596. }
  597. header('Content-Type: text/html; charset='.get_option('blog_charset').'');
  598. postratings_textdomain();
  599. $rated = check_rated($post_id);
  600. // Check Whether Post Has Been Rated By User
  601. if(!$rated) {
  602. // Check Whether Is There A Valid Post
  603. $post = get_post($post_id);
  604. // If Valid Post Then We Rate It
  605. if($post && !wp_is_post_revision($post)) {
  606. $ratings_max = intval(get_option('postratings_max'));
  607. $ratings_custom = intval(get_option('postratings_customrating'));
  608. $ratings_value = get_option('postratings_ratingsvalue');
  609. $post_title = addslashes($post->post_title);
  610. $post_ratings = get_post_custom($post_id);
  611. $post_ratings_users = ! empty( $post_ratings['ratings_users'] ) ? intval($post_ratings['ratings_users'][0]) : 0;
  612. $post_ratings_score = ! empty( $post_ratings['ratings_score'] ) ? intval($post_ratings['ratings_score'][0]) : 0;
  613. // Check For Ratings Lesser Than 1 And Greater Than $ratings_max
  614. if($rate < 1 || $rate > $ratings_max) {
  615. $rate = 0;
  616. }
  617. $post_ratings_users = ($post_ratings_users+1);
  618. $post_ratings_score = ($post_ratings_score+intval($ratings_value[$rate-1]));
  619. $post_ratings_average = round($post_ratings_score/$post_ratings_users, 2);
  620. update_post_meta($post_id, 'ratings_users', $post_ratings_users);
  621. update_post_meta($post_id, 'ratings_score', $post_ratings_score);
  622. update_post_meta($post_id, 'ratings_average', $post_ratings_average);
  623.  
  624. // Add Log
  625. if(!empty($user_identity)) {
  626. $rate_user = addslashes($user_identity);
  627. } elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) {
  628. $rate_user = addslashes($_COOKIE['comment_author_'.COOKIEHASH]);
  629. } else {
  630. $rate_user = __('Guest', 'wp-postratings');
  631. }
  632. $rate_user = apply_filters( 'wp_postratings_process_ratings_user', $rate_user );
  633. $rate_userid = apply_filters( 'wp_postratings_process_ratings_userid', intval( $user_ID ) );
  634.  
  635. // Only Create Cookie If User Choose Logging Method 1 Or 3
  636. $postratings_logging_method = intval(get_option('postratings_logging_method'));
  637. if($postratings_logging_method == 1 || $postratings_logging_method == 3) {
  638. $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], time() + 30000000, apply_filters('wp_postratings_cookiepath', SITECOOKIEPATH));
  639. }
  640. // Log Ratings No Matter What
  641. $rate_log = $wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->ratings} VALUES (%d, %d, %s, %d, %d, %s, %s, %s, %d )", 0, $post_id, $post_title, $ratings_value[$rate-1], current_time('timestamp'), get_ipaddress(), @gethostbyaddr( get_ipaddress() ), $rate_user, $rate_userid ) );
  642. // Allow Other Plugins To Hook When A Post Is Rated
  643. do_action('rate_post', $rate_userid, $post_id, $ratings_value[$rate-1]);
  644. // Output AJAX Result
  645. echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);
  646. exit();
  647. } else {
  648. printf(__('Invalid Post ID. Post ID #%s.', 'wp-postratings'), $post_id);
  649. exit();
  650. } // End if($post)
  651. } else {
  652. printf(__('You Had Already Rated This Post. Post ID #%s.', 'wp-postratings'), $post_id);
  653. exit();
  654. }// End if(!$rated)
  655. } // End if($rate && $post_id && check_allowtorate())
  656. } // End if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'postratings')
  657. }
  658.  
  659.  
  660. ### Function: Process Ratings
  661. add_action('wp_ajax_postratings-admin', 'manage_ratings');
  662. function manage_ratings()
  663. {
  664. ### Form Processing
  665. if(isset($_GET['action']) && $_GET['action'] == 'postratings-admin')
  666. {
  667. check_ajax_referer('wp-postratings_option_update_individual_rating');
  668.  
  669. //Variables
  670. $postratings_url = plugins_url('wp-postratings/images');
  671. $postratings_path = WP_PLUGIN_DIR.'/wp-postratings/images';
  672. $postratings_ratingstext = get_option('postratings_ratingstext');
  673. $postratings_ratingsvalue = get_option('postratings_ratingsvalue');
  674.  
  675. // Form Processing
  676. $postratings_customrating = intval($_GET['custom']);
  677. $postratings_image = trim($_GET['image']);
  678. $postratings_max = intval($_GET['max']);
  679.  
  680. // If It Is A Up/Down Rating
  681. if($postratings_customrating && $postratings_max == 2) {
  682. $postratings_ratingsvalue[0] = -1;
  683. $postratings_ratingsvalue[1] = 1;
  684. $postratings_ratingstext[0] = __('Vote This Post Down', 'wp-postratings');
  685. $postratings_ratingstext[1] = __('Vote This Post Up', 'wp-postratings');
  686. } else {
  687. for($i = 0; $i < $postratings_max; $i++) {
  688. if($i > 0) {
  689. $postratings_ratingstext[$i] = sprintf(__('%s Stars', 'wp-postratings'), number_format_i18n($i+1));
  690. } else {
  691. $postratings_ratingstext[$i] = sprintf(__('%s Star', 'wp-postratings'), number_format_i18n($i+1));
  692. }
  693. $postratings_ratingsvalue[$i] = $i+1;
  694. }
  695. }
  696. ?>
  697. <table class="form-table">
  698. <thead>
  699. <tr>
  700. <th><?php _e('Rating Image', 'wp-postratings'); ?></th>
  701. <th><?php _e('Rating Text', 'wp-postratings'); ?></th>
  702. <th><?php _e('Rating Value', 'wp-postratings'); ?></th>
  703. </tr>
  704. </thead>
  705. <tbody>
  706. <?php
  707. for($i = 1; $i <= $postratings_max; $i++) {
  708. $postratings_text = stripslashes($postratings_ratingstext[$i-1]);
  709. $postratings_value = $postratings_ratingsvalue[$i-1];
  710. if($postratings_value > 0) {
  711. $postratings_value = '+'.$postratings_value;
  712. }
  713. echo '<tr>'."\n";
  714. echo '<td>'."\n";
  715. if(is_rtl() && file_exists($postratings_path.'/'.$postratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  716. echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT.'" alt="rating_start-rtl.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  717. } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  718. echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_start.'.RATINGS_IMG_EXT.'" alt="rating_start.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  719. }
  720. if($postratings_customrating) {
  721. if($postratings_max == 2) {
  722. echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT.'" alt="rating_'.$i.'_on.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  723. } else {
  724. for($j = 1; $j < ($i+1); $j++) {
  725. echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_'.$j.'_on.'.RATINGS_IMG_EXT.'" alt="rating_on.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  726. }
  727. }
  728. } else {
  729. for($j = 1; $j < ($i+1); $j++) {
  730. echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_on.'.RATINGS_IMG_EXT.'" alt="rating_on.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  731. }
  732. }
  733. if(is_rtl() && file_exists($postratings_path.'/'.$postratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  734. echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT.'" alt="rating_end-rtl.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  735. } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  736. echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_end.'.RATINGS_IMG_EXT.'" alt="rating_end.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  737. }
  738. echo '</td>'."\n";
  739. echo '<td>'."\n";
  740. echo '<input type="text" id="postratings_ratingstext_'.$i.'" name="postratings_ratingstext[]" value="'.$postratings_text.'" size="20" maxlength="50" />'."\n";
  741. echo '</td>'."\n";
  742. echo '<td>'."\n";
  743. echo '<input type="text" id="postratings_ratingsvalue_'.$i.'" name="postratings_ratingsvalue[]" value="'.$postratings_value.'" size="2" maxlength="2" />'."\n";
  744. echo '</td>'."\n";
  745. echo '</tr>'."\n";
  746. }
  747. ?>
  748. </tbody>
  749. </table>
  750. <?php
  751. }
  752. exit();
  753. }
  754.  
  755.  
  756. ### Function: Modify Default WordPress Listing To Make It Sorted By Most Rated
  757. function ratings_most_fields($content) {
  758. global $wpdb;
  759. $content .= ", ($wpdb->postmeta.meta_value+0) AS ratings_votes";
  760. return $content;
  761. }
  762. function ratings_most_join($content) {
  763. global $wpdb;
  764. $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID";
  765. return $content;
  766. }
  767. function ratings_most_where($content) {
  768. global $wpdb;
  769. $content .= " AND $wpdb->postmeta.meta_key = 'ratings_users'";
  770. return $content;
  771. }
  772. function ratings_most_orderby($content) {
  773. $orderby = trim(addslashes(get_query_var('r_orderby')));
  774. if(empty($orderby) && ($orderby != 'asc' || $orderby != 'desc')) {
  775. $orderby = 'desc';
  776. }
  777. $content = " ratings_votes $orderby";
  778. return $content;
  779. }
  780.  
  781.  
  782. ### Function: Modify Default WordPress Listing To Make It Sorted By Highest Rated
  783. function ratings_highest_fields($content) {
  784. $content .= ", (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users";
  785. return $content;
  786. }
  787. function ratings_highest_join($content) {
  788. global $wpdb;
  789. $content .= " LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta As t2 ON t1.post_id = t2.post_id";
  790. return $content;
  791. }
  792. function ratings_highest_where($content) {
  793. $ratings_max = intval(get_option('postratings_max'));
  794. $ratings_custom = intval(get_option('postratings_customrating'));
  795. if($ratings_custom && $ratings_max == 2) {
  796. $content .= " AND t1.meta_key = 'ratings_score' AND t2.meta_key = 'ratings_users'";
  797. } else {
  798. $content .= " AND t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users'";
  799. }
  800. return $content;
  801. }
  802. function ratings_highest_orderby($content) {
  803. $orderby = trim(addslashes(get_query_var('r_orderby')));
  804. if(empty($orderby) || ($orderby != 'asc' && $orderby != 'desc')) {
  805. $orderby = 'desc';
  806. }
  807. $content = " ratings_average $orderby, ratings_users $orderby";
  808. return $content;
  809. }
  810.  
  811.  
  812. ### Function: Ratings Public Variables
  813. add_filter('query_vars', 'ratings_variables');
  814. function ratings_variables($public_query_vars) {
  815. $public_query_vars[] = 'r_sortby';
  816. $public_query_vars[] = 'r_orderby';
  817. return $public_query_vars;
  818. }
  819.  
  820.  
  821. ### Function: Sort Ratings Posts
  822. add_action('pre_get_posts', 'ratings_sorting');
  823. function ratings_sorting($local_wp_query) {
  824. if($local_wp_query->get('r_sortby') == 'most_rated') {
  825. add_filter('posts_fields', 'ratings_most_fields');
  826. add_filter('posts_join', 'ratings_most_join');
  827. add_filter('posts_where', 'ratings_most_where');
  828. add_filter('posts_orderby', 'ratings_most_orderby');
  829. remove_filter('posts_fields', 'ratings_highest_fields');
  830. remove_filter('posts_join', 'ratings_highest_join');
  831. remove_filter('posts_where', 'ratings_highest_where');
  832. remove_filter('posts_orderby', 'ratings_highest_orderby');
  833. } elseif($local_wp_query->get('r_sortby') == 'highest_rated') {
  834. add_filter('posts_fields', 'ratings_highest_fields');
  835. add_filter('posts_join', 'ratings_highest_join');
  836. add_filter('posts_where', 'ratings_highest_where');
  837. add_filter('posts_orderby', 'ratings_highest_orderby');
  838. remove_filter('posts_fields', 'ratings_most_fields');
  839. remove_filter('posts_join', 'ratings_most_join');
  840. remove_filter('posts_where', 'ratings_most_where');
  841. remove_filter('posts_orderby', 'ratings_most_orderby');
  842. } else {
  843. remove_filter('posts_fields', 'ratings_highest_fields');
  844. remove_filter('posts_join', 'ratings_highest_join');
  845. remove_filter('posts_where', 'ratings_highest_where');
  846. remove_filter('posts_orderby', 'ratings_highest_orderby');
  847. remove_filter('posts_fields', 'ratings_most_fields');
  848. remove_filter('posts_join', 'ratings_most_join');
  849. remove_filter('posts_where', 'ratings_most_where');
  850. remove_filter('posts_orderby', 'ratings_most_orderby');
  851. }
  852. }
  853.  
  854.  
  855. ### Function Show Ratings Column in WP-Admin
  856. add_action('manage_posts_custom_column', 'add_postratings_column_content');
  857. add_filter('manage_posts_columns', 'add_postratings_column');
  858. add_action('manage_pages_custom_column', 'add_postratings_column_content');
  859. add_filter('manage_pages_columns', 'add_postratings_column');
  860. function add_postratings_column($defaults) {
  861. $defaults['ratings'] = 'Ratings';
  862. return $defaults;
  863. }
  864.  
  865.  
  866. ### Functions Fill In The Ratings
  867. function add_postratings_column_content($column_name) {
  868. global $post;
  869. if($column_name == 'ratings') {
  870. if(function_exists('the_ratings')) {
  871. $template = str_replace('%RATINGS_IMAGES_VOTE%', '%RATINGS_IMAGES%<br />', stripslashes(get_option('postratings_template_vote')));
  872. echo expand_ratings_template($template, $post, null, 0, false);
  873. }
  874. }
  875. }
  876.  
  877.  
  878. ### Function Sort Columns
  879. add_filter('manage_edit-post_sortable_columns', 'sort_postratings_column');
  880. add_filter('manage_edit-page_sortable_columns', 'sort_postratings_column');
  881. function sort_postratings_column($defaults)
  882. {
  883. $defaults['ratings'] = 'ratings';
  884. return $defaults;
  885. }
  886. add_action('pre_get_posts', 'sort_postratings');
  887. function sort_postratings($query) {
  888. if(!is_admin())
  889. return;
  890. $orderby = $query->get('orderby');
  891. if('ratings' == $orderby) {
  892. $query->set('meta_key', 'ratings_average');
  893. $query->set('orderby', 'meta_value_num');
  894. }
  895. }
  896.  
  897.  
  898. ### Function: Plug Into WP-Stats
  899. add_action('wp','postratings_wp_stats');
  900. function postratings_wp_stats() {
  901. if(function_exists('stats_page')) {
  902. add_filter('wp_stats_page_admin_plugins', 'postratings_page_admin_general_stats');
  903. add_filter('wp_stats_page_admin_most', 'postratings_page_admin_most_stats');
  904. add_filter('wp_stats_page_plugins', 'postratings_page_general_stats');
  905. add_filter('wp_stats_page_most', 'postratings_page_most_stats');
  906. }
  907. }
  908.  
  909.  
  910. ### Function: Add WP-PostRatings General Stats To WP-Stats Page Options
  911. function postratings_page_admin_general_stats($content) {
  912. $stats_display = get_option('stats_display');
  913. if($stats_display['ratings'] == 1) {
  914. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_ratings" value="ratings" checked="checked" />&nbsp;&nbsp;<label for="wpstats_ratings">'.__('WP-PostRatings', 'wp-postratings').'</label><br />'."\n";
  915. } else {
  916. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_ratings" value="ratings" />&nbsp;&nbsp;<label for="wpstats_ratings">'.__('WP-PostRatings', 'wp-postratings').'</label><br />'."\n";
  917. }
  918. return $content;
  919. }
  920.  
  921.  
  922. ### Function: Add WP-PostRatings Top Most/Highest Stats To WP-Stats Page Options
  923. function postratings_page_admin_most_stats($content) {
  924. $stats_display = get_option('stats_display');
  925. $stats_mostlimit = intval(get_option('stats_mostlimit'));
  926. if($stats_display['rated_highest_post'] == 1) {
  927. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_post" value="rated_highest_post" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_highest_post">'.sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  928. } else {
  929. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_post" value="rated_highest_post" />&nbsp;&nbsp;<label for="wpstats_rated_highest_post">'.sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  930. }
  931. if($stats_display['rated_highest_page'] == 1) {
  932. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_page" value="rated_highest_page" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_highest_page">'.sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  933. } else {
  934. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_page" value="rated_highest_page" />&nbsp;&nbsp;<label for="wpstats_rated_highest_page">'.sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  935. }
  936. if($stats_display['rated_most_post'] == 1) {
  937. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_post" value="rated_most_post" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_most_post">'.sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  938. } else {
  939. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_post" value="rated_most_post" />&nbsp;&nbsp;<label for="wpstats_rated_most_post">'.sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  940. }
  941. if($stats_display['rated_most_page'] == 1) {
  942. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_page" value="rated_most_page" checked="checked" />&nbsp;&nbsp;<label for="wpstats_rated_most_page">'.sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  943. } else {
  944. $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_page" value="rated_most_page" />&nbsp;&nbsp;<label for="wpstats_rated_most_page">'.sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</label><br />'."\n";
  945. }
  946. return $content;
  947. }
  948.  
  949.  
  950. ### Function: Add WP-PostRatings General Stats To WP-Stats Page
  951. function postratings_page_general_stats($content) {
  952. $stats_display = get_option('stats_display');
  953. if($stats_display['ratings'] == 1) {
  954. $content .= '<p><strong>'.__('WP-PostRatings', 'wp-postratings').'</strong></p>'."\n";
  955. $content .= '<ul>'."\n";
  956. $content .= '<li>'.sprintf(_n('<strong>%s</strong> user casted his vote.', '<strong>%s</strong> users casted their vote.', get_ratings_users(false), 'wp-postratings'), number_format_i18n(get_ratings_users(false))).'</li>'."\n";
  957. $content .= '</ul>'."\n";
  958. }
  959. return $content;
  960. }
  961.  
  962.  
  963. ### Function: Add WP-PostRatings Top Most/Highest Stats To WP-Stats Page
  964. function postratings_page_most_stats($content) {
  965. $stats_display = get_option('stats_display');
  966. $stats_mostlimit = intval(get_option('stats_mostlimit'));
  967. if($stats_display['rated_highest_post'] == 1) {
  968. $content .= '<p><strong>'.sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  969. $content .= '<ul>'."\n";
  970. $content .= get_highest_rated('post', 0, $stats_mostlimit, 0, false);
  971. $content .= '</ul>'."\n";
  972. }
  973. if($stats_display['rated_highest_page'] == 1) {
  974. $content .= '<p><strong>'.sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  975. $content .= '<ul>'."\n";
  976. $content .= get_highest_rated('page', 0, $stats_mostlimit, 0, false);
  977. $content .= '</ul>'."\n";
  978. }
  979. if($stats_display['rated_most_post'] == 1) {
  980. $content .= '<p><strong>'.sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  981. $content .= '<ul>'."\n";
  982. $content .= get_most_rated('post', 0, $stats_mostlimit, 0, false);
  983. $content .= '</ul>'."\n";
  984. }
  985. if($stats_display['rated_most_page'] == 1) {
  986. $content .= '<p><strong>'.sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit)).'</strong></p>'."\n";
  987. $content .= '<ul>'."\n";
  988. $content .= get_most_rated('page', 0, $stats_mostlimit, 0, false);
  989. $content .= '</ul>'."\n";
  990. }
  991. return $content;
  992. }
  993.  
  994.  
  995. ### Function: Gets HTML of rating images
  996. function get_ratings_images($ratings_custom, $ratings_max, $post_rating, $ratings_image, $image_alt, $insert_half) {
  997. $ratings_images = '';
  998. if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  999. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1000. } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  1001. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1002. }
  1003. if($ratings_custom) {
  1004. for($i=1; $i <= $ratings_max; $i++) {
  1005. if($i <= $post_rating) {
  1006. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1007. } elseif($i == $insert_half) {
  1008. if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT)) {
  1009. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1010. } else {
  1011. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1012. }
  1013. } else {
  1014. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1015. }
  1016. }
  1017. } else {
  1018. for($i=1; $i <= $ratings_max; $i++) {
  1019. if($i <= $post_rating) {
  1020. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1021. } elseif($i == $insert_half) {
  1022. if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) {
  1023. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1024. } else {
  1025. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1026. }
  1027. } else {
  1028. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1029. }
  1030. }
  1031. }
  1032. if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  1033. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1034. } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  1035. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1036. }
  1037. return $ratings_images;
  1038. }
  1039.  
  1040.  
  1041. ### Function: Gets HTML of rating images for voting
  1042. function get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_rating, $ratings_image, $image_alt, $insert_half, $ratings_texts) {
  1043. $ratings_images = '';
  1044. if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  1045. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1046. } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  1047. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1048. }
  1049. if($ratings_custom) {
  1050. for($i=1; $i <= $ratings_max; $i++) {
  1051. if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'half-rtl.'.RATINGS_IMG_EXT)) {
  1052. $use_half_rtl = 1;
  1053. } else {
  1054. $use_half_rtl = 0;
  1055. }
  1056. $ratings_text = esc_attr( stripslashes( $ratings_texts[$i-1] ) );
  1057. $ratings_text_js = esc_js( $ratings_text );
  1058. if($i <= $post_rating) {
  1059. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1060. } elseif($i == $insert_half) {
  1061. if ($use_half_rtl) {
  1062. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1063. } else {
  1064. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1065. }
  1066. } else {
  1067. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1068. }
  1069. }
  1070. } else {
  1071. if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) {
  1072. $use_half_rtl = 1;
  1073. } else {
  1074. $use_half_rtl = 0;
  1075. }
  1076. for($i=1; $i <= $ratings_max; $i++) {
  1077. $ratings_text = esc_attr( stripslashes( $ratings_texts[$i-1] ) );
  1078. $ratings_text_js = esc_js( $ratings_text );
  1079. if($i <= $post_rating) {
  1080. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1081. } elseif($i == $insert_half) {
  1082. if ($use_half_rtl) {
  1083. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1084. } else {
  1085. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1086. }
  1087. } else {
  1088. $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" alt="'.$ratings_text.'" title="'.$ratings_text.'" onmouseover="current_rating('.$post_id.', '.$i.', \''.$ratings_text_js.'\');" onmouseout="ratings_off('.$post_rating.', '.$insert_half.', '.$use_half_rtl.');" onclick="rate_post();" onkeypress="rate_post();" style="cursor: pointer; border: 0px;" />';
  1089. }
  1090. }
  1091. }
  1092. if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  1093. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1094. } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  1095. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1096. }
  1097. return $ratings_images;
  1098. }
  1099.  
  1100.  
  1101. ### Function: Gets HTML of rating images for comment author
  1102. function get_ratings_images_comment_author($ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt) {
  1103. $ratings_images = '';
  1104. if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  1105. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1106. } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  1107. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1108. }
  1109. if($ratings_custom && $ratings_max == 2) {
  1110. if($comment_author_rating > 0) {
  1111. $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_2_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1112. } else {
  1113. $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_1_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1114. }
  1115. } elseif($ratings_custom) {
  1116. for($i=1; $i <= $ratings_max; $i++) {
  1117. if($i <= $comment_author_rating) {
  1118. $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1119. } else {
  1120. $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1121. }
  1122. }
  1123. } else {
  1124. for($i=1; $i <= $ratings_max; $i++) {
  1125. if($i <= $comment_author_rating) {
  1126. $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1127. } else {
  1128. $ratings_images .= '<img src="'.plugins_url('wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
  1129. }
  1130. }
  1131. }
  1132. if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  1133. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1134. } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  1135. $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1136. }
  1137. return $ratings_images;
  1138. }
  1139.  
  1140. ### Function: Replaces the template's variables with appropriate values
  1141. function expand_ratings_template($template, $post_data, $post_ratings_data = null, $max_post_title_chars = 0, $is_main_loop = true) {
  1142. global $post;
  1143.  
  1144. // Get global variables
  1145. $ratings_image = get_option('postratings_image');
  1146. $ratings_max = intval(get_option('postratings_max'));
  1147. $ratings_custom = intval(get_option('postratings_customrating'));
  1148. $ratings_options = get_option('postratings_options');
  1149.  
  1150. if(is_object($post_data)) {
  1151. $post_id = $post_data->ID;
  1152. } else {
  1153. $post_id = $post_data;
  1154. }
  1155.  
  1156. // Most likely from coming from Widget
  1157. if(isset($post_data->ratings_users)) {
  1158. $post_ratings_users = intval($post_data->ratings_users);
  1159. $post_ratings_score = intval($post_data->ratings_score);
  1160. $post_ratings_average = floatval($post_data->ratings_average);
  1161. // Most Likely coming from the_ratings_vote or the_ratings_rate
  1162. } else if(isset($post_ratings_data->ratings_users)) {
  1163. $post_ratings_users = intval($post_ratings_data->ratings_users);
  1164. $post_ratings_score = intval($post_ratings_data->ratings_score);
  1165. $post_ratings_average = floatval($post_ratings_data->ratings_average);
  1166. } else {
  1167. if(get_the_ID() != $post_id) {
  1168. $post_ratings_data = get_post_custom($post_id);
  1169. } else {
  1170. $post_ratings_data = get_post_custom();
  1171. }
  1172.  
  1173. $post_ratings_users = is_array($post_ratings_data) && array_key_exists('ratings_users', $post_ratings_data) ? intval($post_ratings_data['ratings_users'][0]) : 0;
  1174. $post_ratings_score = is_array($post_ratings_data) && array_key_exists('ratings_score', $post_ratings_data) ? intval($post_ratings_data['ratings_score'][0]) : 0;
  1175. $post_ratings_average = is_array($post_ratings_data) && array_key_exists('ratings_average', $post_ratings_data) ? floatval($post_ratings_data['ratings_average'][0]) : 0;
  1176. }
  1177.  
  1178. if($post_ratings_score == 0 || $post_ratings_users == 0) {
  1179. $post_ratings = 0;
  1180. $post_ratings_average = 0;
  1181. $post_ratings_percentage = 0;
  1182. } else {
  1183. $post_ratings = round($post_ratings_average, 1);
  1184. $post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);
  1185. }
  1186. $post_ratings_text = '<span class="post-ratings-text" id="ratings_'.$post_id.'_text"></span>';
  1187. // Get the image's alt text
  1188. if($ratings_custom && $ratings_max == 2) {
  1189. if($post_ratings_score > 0) {
  1190. $post_ratings_score = '+'.$post_ratings_score;
  1191. }
  1192. $post_ratings_alt_text = sprintf(_n('%s rating', '%s rating', $post_ratings_score, 'wp-postratings'), number_format_i18n($post_ratings_score)).__(',', 'wp-postratings').' '.sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users));
  1193. } else {
  1194. $post_ratings_score = number_format_i18n($post_ratings_score);
  1195. $post_ratings_alt_text = sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users)).__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': '.number_format_i18n($post_ratings_average, 2).' '.__('out of', 'wp-postratings').' '.number_format_i18n($ratings_max);
  1196. }
  1197. // Check for half star
  1198. $insert_half = 0;
  1199. $average_diff = abs(floor($post_ratings_average)-$post_ratings);
  1200. if($average_diff >= 0.25 && $average_diff <= 0.75) {
  1201. $insert_half = ceil($post_ratings_average);
  1202. } elseif($average_diff > 0.75) {
  1203. $insert_half = ceil($post_ratings);
  1204. }
  1205. // Replace the variables
  1206. $value = $template;
  1207. if (strpos($template, '%RATINGS_IMAGES%') !== false) {
  1208. $post_ratings_images = get_ratings_images($ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half);
  1209. $value = str_replace("%RATINGS_IMAGES%", $post_ratings_images, $value);
  1210. }
  1211. if (strpos($template, '%RATINGS_IMAGES_VOTE%') !== false) {
  1212. $ratings_texts = get_option('postratings_ratingstext');
  1213. $post_ratings_images = get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half, $ratings_texts);
  1214. $value = str_replace("%RATINGS_IMAGES_VOTE%", $post_ratings_images, $value);
  1215. }
  1216. $value = str_replace("%RATINGS_ALT_TEXT%", $post_ratings_alt_text, $value);
  1217. $value = str_replace("%RATINGS_TEXT%", $post_ratings_text, $value);
  1218. $value = str_replace("%RATINGS_MAX%", number_format_i18n($ratings_max), $value);
  1219. $value = str_replace("%RATINGS_SCORE%", $post_ratings_score, $value);
  1220. $value = str_replace("%RATINGS_AVERAGE%", number_format_i18n($post_ratings_average, 2), $value);
  1221. $value = str_replace("%RATINGS_PERCENTAGE%", number_format_i18n($post_ratings_percentage, 2), $value);
  1222. $value = str_replace("%RATINGS_USERS%", number_format_i18n($post_ratings_users), $value);
  1223.  
  1224. // Post Template Variables
  1225. $post_link = get_permalink($post_data);
  1226. $post_title = get_the_title($post_data);
  1227. if ($max_post_title_chars > 0) {
  1228. $post_title = snippet_text($post_title, $max_post_title_chars);
  1229. }
  1230. $value = str_replace("%POST_ID%", $post_id, $value);
  1231. $value = str_replace("%POST_TITLE%", $post_title, $value);
  1232. $value = str_replace("%POST_URL%", $post_link, $value);
  1233.  
  1234. if (strpos($template, '%POST_EXCERPT%') !== false) {
  1235. if (get_the_ID() != $post_id) {
  1236. $post = &get_post($post_id);
  1237. }
  1238. $post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password);
  1239. $value = str_replace("%POST_EXCERPT%", $post_excerpt, $value);
  1240. }
  1241. if (strpos($template, '%POST_CONTENT%') !== false) {
  1242. if (get_the_ID() != $post_id) {
  1243. $post = &get_post($post_id);
  1244. }
  1245. $value = str_replace("%POST_CONTENT%", get_the_content(), $value);
  1246. }
  1247.  
  1248. // Google Rich Snippet
  1249. $ratings_options['richsnippet'] = isset($ratings_options['richsnippet']) ? $ratings_options['richsnippet'] : 1;
  1250. if($ratings_options['richsnippet'] && (is_single() || is_page()) && $is_main_loop && $post_ratings_average > 0)
  1251. {
  1252. if(!isset($post_excerpt))
  1253. $post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password);
  1254.  
  1255. $post_meta = '<meta itemprop="name" content="'.esc_attr($post_title).'" /><meta itemprop="description" content="'.wp_kses($post_excerpt, array()).'" /><meta itemprop="url" content="'.$post_link.'" />';
  1256. $ratings_meta = '<div style="display: none;" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
  1257. $ratings_meta .= '<meta itemprop="bestRating" content="'.$ratings_max.'" />';
  1258. $ratings_meta .= '<meta itemprop="ratingValue" content="'.$post_ratings_average.'" />';
  1259. $ratings_meta .= '<meta itemprop="ratingCount" content="'.$post_ratings_users.'" />';
  1260. $ratings_meta .= '</div>';
  1261.  
  1262. $value = $value.$post_meta.$ratings_meta;
  1263. }
  1264.  
  1265. return apply_filters('expand_ratings_template', $value);
  1266. }
  1267.  
  1268.  
  1269. ### Class: WP-PostRatings Widget
  1270. class WP_Widget_PostRatings extends WP_Widget {
  1271. // Constructor
  1272. function WP_Widget_PostRatings() {
  1273. $widget_ops = array('description' => __('WP-PostRatings ratings statistics', 'wp-postratings'));
  1274. $this->WP_Widget('ratings-widget', __('Ratings', 'wp-postratings'), $widget_ops);
  1275. }
  1276.  
  1277. // Display Widget
  1278. function widget($args, $instance) {
  1279. extract($args);
  1280. $title = apply_filters('widget_title', esc_attr($instance['title']));
  1281. $type = esc_attr($instance['type']);
  1282. $mode = esc_attr($instance['mode']);
  1283. $limit = intval($instance['limit']);
  1284. $min_votes = intval($instance['min_votes']);
  1285. $chars = intval($instance['chars']);
  1286. $cat_ids = explode(',', esc_attr($instance['cat_ids']));
  1287. $time_range = esc_attr($instance['time_range']);
  1288. echo $before_widget.$before_title.$title.$after_title;
  1289. echo '<ul>'."\n";
  1290. switch($type) {
  1291. case 'most_rated':
  1292. get_most_rated($mode, $min_votes, $limit, $chars);
  1293. break;
  1294. case 'most_rated_category':
  1295. get_most_rated($cat_ids, $mode, $min_votes, $limit, $chars);
  1296. break;
  1297. case 'most_rated_range':
  1298. get_most_rated_range($time_range, $mode, $limit, $chars);
  1299. break;
  1300. case 'most_rated_range_category':
  1301. get_most_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
  1302. break;
  1303. case 'highest_rated':
  1304. get_highest_rated($mode, $min_votes, $limit, $chars);
  1305. break;
  1306. case 'highest_rated_category':
  1307. get_highest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
  1308. break;
  1309. case 'highest_rated_range':
  1310. get_highest_rated_range($time_range, $mode, $limit, $chars);
  1311. break;
  1312. case 'highest_rated_range_category':
  1313. get_highest_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);
  1314. break;
  1315. case 'lowest_rated':
  1316. get_lowest_rated($mode, $min_votes, $limit, $chars);
  1317. break;
  1318. case 'lowest_rated_category':
  1319. get_lowest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);
  1320. break;
  1321. case 'lowest_rated_range':
  1322. get_lowest_rated_range($time_range, $mode, $limit, $chars);
  1323. break;
  1324. case 'highest_score':
  1325. get_highest_score($mode, $min_votes, $limit, $chars);
  1326. break;
  1327. case 'highest_score_category':
  1328. get_highest_score_category($cat_ids, $mode, $min_votes, $limit, $chars);
  1329. break;
  1330. case 'highest_score_range':
  1331. get_highest_score_range($time_range, $mode, $limit, $chars);
  1332. break;
  1333. case 'highest_score_range_category':
  1334. get_highest_score_range_category($time_range, $cat_ids, $mode, $limit, $chars);
  1335. break;
  1336. }
  1337. echo '</ul>'."\n";
  1338. echo $after_widget;
  1339. }
  1340.  
  1341. // When Widget Control Form Is Posted
  1342. function update($new_instance, $old_instance) {
  1343. if (!isset($new_instance['submit'])) {
  1344. return false;
  1345. }
  1346. $instance = $old_instance;
  1347. $instance['title'] = strip_tags($new_instance['title']);
  1348. $instance['type'] = strip_tags($new_instance['type']);
  1349. $instance['mode'] = strip_tags($new_instance['mode']);
  1350. $instance['limit'] = intval($new_instance['limit']);
  1351. $instance['min_votes'] = intval($new_instance['min_votes']);
  1352. $instance['chars'] = intval($new_instance['chars']);
  1353. $instance['cat_ids'] = strip_tags($new_instance['cat_ids']);
  1354. $instance['time_range'] = strip_tags($new_instance['time_range']);
  1355. return $instance;
  1356. }
  1357.  
  1358. // DIsplay Widget Control Form
  1359. function form($instance) {
  1360. global $wpdb;
  1361. $instance = wp_parse_args((array) $instance, array('title' => __('Ratings', 'wp-postratings'), 'type' => 'highest_rated', 'mode' => '', 'limit' => 10, 'min_votes' => 0, 'chars' => 200, 'cat_ids' => '0', 'time_range' => '1 day'));
  1362. $title = esc_attr($instance['title']);
  1363. $type = esc_attr($instance['type']);
  1364. $mode = trim( esc_attr( $instance['mode'] ) );
  1365. $limit = intval($instance['limit']);
  1366. $min_votes = intval($instance['min_votes']);
  1367. $chars = intval($instance['chars']);
  1368. $cat_ids = esc_attr($instance['cat_ids']);
  1369. $time_range = esc_attr($instance['time_range']);
  1370. $post_types = get_post_types( array(
  1371. 'public' => true
  1372. ) );
  1373. ?>
  1374. <p>
  1375. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-postratings'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label>
  1376. </p>
  1377. <p>
  1378. <label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Statistics Type:', 'wp-postratings'); ?>
  1379. <select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
  1380. <option value="most_rated"<?php selected('most_rated', $type); ?>><?php _e('Most Rated', 'wp-postratings'); ?></option>
  1381. <option value="most_rated_category"<?php selected('most_rated_category', $type); ?>><?php _e('Most Rated By Category', 'wp-postratings'); ?></option>
  1382. <option value="most_rated_range"<?php selected('most_rated_range', $type); ?>><?php _e('Most Rated By Time Range', 'wp-postratings'); ?></option>
  1383. <option value="most_rated_range_category"<?php selected('most_rated_range_category', $type); ?>><?php _e('Most Rated By Time Range And Category', 'wp-postratings'); ?></option>
  1384. <optgroup>&nbsp;</optgroup>
  1385. <option value="highest_rated"<?php selected('highest_rated', $type); ?>><?php _e('Highest Rated', 'wp-postratings'); ?></option>
  1386. <option value="highest_rated_category"<?php selected('highest_rated_category', $type); ?>><?php _e('Highest Rated By Category', 'wp-postratings'); ?></option>
  1387. <option value="highest_rated_range"<?php selected('highest_rated_range', $type); ?>><?php _e('Highest Rated By Time Range', 'wp-postratings'); ?></option>
  1388. <option value="highest_rated_range_category"<?php selected('highest_rated_range_category', $type); ?>><?php _e('Highest Rated By Time Range And Category', 'wp-postratings'); ?></option>
  1389. <optgroup>&nbsp;</optgroup>
  1390. <option value="lowest_rated"<?php selected('lowest_rated', $type); ?>><?php _e('Lowest Rated', 'wp-postratings'); ?></option>
  1391. <option value="lowest_rated_category"<?php selected('lowest_rated_category', $type); ?>><?php _e('Lowest Rated By Category', 'wp-postratings'); ?></option>
  1392. <option value="lowest_rated_range"<?php selected('lowest_rated_range', $type); ?>><?php _e('Lowest Rated By Time Range', 'wp-postratings'); ?></option>
  1393. <optgroup>&nbsp;</optgroup>
  1394. <option value="highest_score"<?php selected('highest_score', $type); ?>><?php _e('Highest Score', 'wp-postratings'); ?></option>
  1395. <option value="highest_score_category"<?php selected('highest_score_category', $type); ?>><?php _e('Highest Score By Category', 'wp-postratings'); ?></option>
  1396. <option value="highest_score_range"<?php selected('highest_score_range', $type); ?>><?php _e('Highest Score By Time Range', 'wp-postratings'); ?></option>
  1397. <option value="highest_score_range_category"<?php selected('highest_score_range_category', $type); ?>><?php _e('Highest Score By Time Range And Category', 'wp-postratings'); ?></option>
  1398. </select>
  1399. </label>
  1400. </p>
  1401. <p>
  1402. <label for="<?php echo $this->get_field_id('mode'); ?>"><?php _e('Include Ratings From:', 'wp-postratings'); ?>
  1403. <select name="<?php echo $this->get_field_name('mode'); ?>" id="<?php echo $this->get_field_id('mode'); ?>" class="widefat">
  1404. <option value=""<?php selected( '', $mode ); ?>><?php _e( 'All', 'wp-postratings' ); ?></option>
  1405. <?php if( $post_types > 0 ): ?>
  1406. <?php foreach( $post_types as $post_type ): ?>
  1407. <option value="<?php echo $post_type; ?>"<?php selected( $post_type, $mode ); ?>><?php printf( __( '%s Only', 'wp-postratings' ), ucfirst( $post_type ) ); ?></option>
  1408. <?php endforeach; ?>
  1409. <?php endif; ?>
  1410. </select>
  1411. </label>
  1412. </p>
  1413. <p>
  1414. <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('No. Of Records To Show:', 'wp-postratings'); ?> <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit; ?>" /></label>
  1415. </p>
  1416. <p>
  1417. <label for="<?php echo $this->get_field_id('min_votes'); ?>"><?php _e('Minimum Votes:', 'wp-postratings'); ?> <span style="color: red;">*</span> <input class="widefat" id="<?php echo $this->get_field_id('min_votes'); ?>" name="<?php echo $this->get_field_name('min_votes'); ?>" type="text" value="<?php echo $min_votes; ?>" size="4" /></label><br />
  1418. <small><?php _e('You can set the minimum votes that a post or page must have before it gets displayed.', 'wp-postratings'); ?></small>
  1419. </p>
  1420. <p>
  1421. <label for="<?php echo $this->get_field_id('chars'); ?>"><?php _e('Maximum Post Title Length (Characters):', 'wp-postratings'); ?> <input class="widefat" id="<?php echo $this->get_field_id('chars'); ?>" name="<?php echo $this->get_field_name('chars'); ?>" type="text" value="<?php echo $chars; ?>" /></label><br />
  1422. <small><?php _e('<strong>0</strong> to disable.', 'wp-postratings'); ?></small>
  1423. </p>
  1424. <p>
  1425. <label for="<?php echo $this->get_field_id('cat_ids'); ?>"><?php _e('Category IDs:', 'wp-postratings'); ?> <span style="color: red;">**</span> <input class="widefat" id="<?php echo $this->get_field_id('cat_ids'); ?>" name="<?php echo $this->get_field_name('cat_ids'); ?>" type="text" value="<?php echo $cat_ids; ?>" /></label><br />
  1426. <small><?php _e('Seperate mutiple categories with commas.', 'wp-postratings'); ?></small>
  1427. </p>
  1428. <p>
  1429. <label for="<?php echo $this->get_field_id('time_range'); ?>"><?php _e('Time Range:', 'wp-postratings'); ?> <span style="color: red;">**</span> <input class="widefat" id="<?php echo $this->get_field_id('time_range'); ?>" name="<?php echo $this->get_field_name('time_range'); ?>" type="text" value="<?php echo $time_range; ?>" /></label><br />
  1430. <small><?php _e('Use values like <strong>1 day</strong>, <strong>2 weeks</strong>, <strong>1 month</strong>.', 'wp-postratings'); ?></small>
  1431. </p>
  1432. <p style="color: red;">
  1433. <small><?php _e('* Time range statistics does not support Minimum Votes field, you can ignore that it.', 'wp-postratings'); ?></small><br />
  1434. <small><?php _e('** If you are not using any category or time range statistics, you can ignore it.', 'wp-postratings'); ?></small>
  1435. <p>
  1436. <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
  1437. <?php
  1438. }
  1439. }
  1440.  
  1441.  
  1442. ### Function: Init WP-PostRatings Widget
  1443. add_action('widgets_init', 'widget_ratings_init');
  1444. function widget_ratings_init() {
  1445. postratings_textdomain();
  1446. register_widget('WP_Widget_PostRatings');
  1447. }
  1448.  
  1449.  
  1450. ### Function: Activate Plugin
  1451. register_activation_hook( __FILE__, 'ratings_activation' );
  1452. function ratings_activation( $network_wide )
  1453. {
  1454. if ( is_multisite() && $network_wide )
  1455. {
  1456. $ms_sites = wp_get_sites();
  1457.  
  1458. if( 0 < sizeof( $ms_sites ) )
  1459. {
  1460. foreach ( $ms_sites as $ms_site )
  1461. {
  1462. switch_to_blog( $ms_site['blog_id'] );
  1463. ratings_activate();
  1464. }
  1465. }
  1466.  
  1467. restore_current_blog();
  1468. }
  1469. else
  1470. {
  1471. ratings_activate();
  1472. }
  1473. }
  1474.  
  1475. function ratings_activate() {
  1476. global $wpdb;
  1477.  
  1478. // Create Post Ratings Table
  1479. $create_sql = "CREATE TABLE $wpdb->ratings (".
  1480. "rating_id INT(11) NOT NULL auto_increment,".
  1481. "rating_postid INT(11) NOT NULL ,".
  1482. "rating_posttitle TEXT NOT NULL,".
  1483. "rating_rating INT(2) NOT NULL ,".
  1484. "rating_timestamp VARCHAR(15) NOT NULL ,".
  1485. "rating_ip VARCHAR(40) NOT NULL ,".
  1486. "rating_host VARCHAR(200) NOT NULL,".
  1487. "rating_username VARCHAR(50) NOT NULL,".
  1488. "rating_userid int(10) NOT NULL default '0',".
  1489. "PRIMARY KEY (rating_id));";
  1490.  
  1491. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1492. dbDelta( $create_sql );
  1493.  
  1494. // Add In Options (4 Records)
  1495. add_option('postratings_image', 'stars' );
  1496. add_option('postratings_max', '5' );
  1497. add_option('postratings_template_vote', '%RATINGS_IMAGES_VOTE% (<strong>%RATINGS_USERS%</strong> '.__('votes', 'wp-postratings').__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': <strong>%RATINGS_AVERAGE%</strong> '.__('out of', 'wp-postratings').' %RATINGS_MAX%)<br />%RATINGS_TEXT%' );
  1498. add_option('postratings_template_text', '%RATINGS_IMAGES% (<em><strong>%RATINGS_USERS%</strong> '.__('votes', 'wp-postratings').__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': <strong>%RATINGS_AVERAGE%</strong> '.__('out of', 'wp-postratings').' %RATINGS_MAX%'.__(',', 'wp-postratings').' <strong>'.__('rated', 'wp-postratings').'</strong></em>)' );
  1499. add_option('postratings_template_none', '%RATINGS_IMAGES_VOTE% ('.__('No Ratings Yet', 'wp-postratings').')<br />%RATINGS_TEXT%' );
  1500. // Database Upgrade For WP-PostRatings 1.02
  1501. add_option('postratings_logging_method', '3' );
  1502. add_option('postratings_allowtorate', '2' );
  1503. // Database Uprade For WP-PostRatings 1.04
  1504. maybe_add_column($wpdb->ratings, 'rating_userid', "ALTER TABLE $wpdb->ratings ADD rating_userid INT( 10 ) NOT NULL DEFAULT '0';");
  1505. // Database Uprade For WP-PostRatings 1.05
  1506. add_option('postratings_ratingstext', array(__('1 Star', 'wp-postratings'), __('2 Stars', 'wp-postratings'), __('3 Stars', 'wp-postratings'), __('4 Stars', 'wp-postratings'), __('5 Stars', 'wp-postratings')) );
  1507. add_option('postratings_template_highestrated', '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> %RATINGS_IMAGES% (%RATINGS_AVERAGE% '.__('out of', 'wp-postratings').' %RATINGS_MAX%)</li>' );
  1508. // Database Upgrade For WP-PostRatings 1.11
  1509. add_option('postratings_ajax_style', array('loading' => 1, 'fading' => 1) );
  1510. // Database Upgrade For WP-PostRatings 1.20
  1511. add_option('postratings_ratingsvalue', array(1,2,3,4,5) );
  1512. add_option('postratings_customrating', 0 );
  1513. add_option('postratings_template_permission', '%RATINGS_IMAGES% (<em><strong>%RATINGS_USERS%</strong> '.__('votes', 'wp-postratings').__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': <strong>%RATINGS_AVERAGE%</strong> '.__('out of', 'wp-postratings').' %RATINGS_MAX%</em>)<br /><em>'.__('You need to be a registered member to rate this post.', 'wp-postratings').'</em>' );
  1514. // Database Upgrade For WP-PostRatings 1.30
  1515. add_option('postratings_template_mostrated', '<li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> - %RATINGS_USERS% '.__('votes', 'wp-postratings').'</li>' );
  1516. // Database Upgrade For WP-PostRatings 1.50
  1517. delete_option('widget_ratings_highest_rated');
  1518. delete_option('widget_ratings_most_rated');
  1519.  
  1520. // Set 'manage_ratings' Capabilities To Administrator
  1521. $role = get_role( 'administrator' );
  1522. $role->add_cap( 'manage_ratings' );
  1523. }
  1524.  
  1525.  
  1526. ### Seperate PostRatings Stats For Readability
  1527. require_once('postratings-stats.php');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement