shoropio

wp-postratings.php

Jul 11th, 2018
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 65.59 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 site's content.
  6. Version:     1.85
  7. Author:      Lester 'GaMerZ' Chan
  8. Author URI:  http://lesterchan.net
  9. Text Domain: wp-postratings
  10. */
  11.  
  12.  
  13. /*
  14.     Copyright 2017 Lester Chan  (email : [email protected])
  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.  
  32. /**
  33.  * Security check
  34.  * Prevent direct access to the file.
  35.  */
  36. if ( ! defined( 'ABSPATH' ) ) {
  37.     exit;
  38. }
  39.  
  40. /**
  41.  * Plugin version
  42.  * Set wp-postratings plugin version.
  43.  */
  44. define( 'WP_POSTRATINGS_VERSION', 1.85 );
  45.  
  46. /**
  47.  * Rating logs table name
  48.  */
  49. global $wpdb;
  50. $wpdb->ratings = $wpdb->prefix.'ratings';
  51.  
  52. /**
  53.  * Load plugin files
  54.  * Require the plugin files in an alphabetical order.
  55.  */
  56. require_once( 'includes/postratings-activation.php' );
  57. require_once( 'includes/postratings-admin.php' );
  58. require_once( 'includes/postratings-i18n.php' );
  59. require_once( 'includes/postratings-scripts.php' );
  60. require_once( 'includes/postratings-shortcodes.php' );
  61. require_once( 'includes/postratings-stats.php' );
  62. require_once( 'includes/postratings-widgets.php' );
  63.  
  64. /**
  65.  * Register plugin activation hook
  66.  */
  67. register_activation_hook( __FILE__, 'ratings_activation' );
  68.  
  69. ### Define Image Extension
  70. add_action( 'init', 'postratings_init' );
  71. function postratings_init() {
  72.     if( ! defined( 'RATINGS_IMG_EXT' ) ) {
  73.         define( 'RATINGS_IMG_EXT', apply_filters( 'wp_postratings_image_extension', 'gif' ) );
  74.     }
  75. }
  76.  
  77. ### Function: Display The Rating For The Post
  78. function the_ratings($start_tag = 'div', $custom_id = 0, $display = true) {
  79.     global $id;
  80.     // Allow Custom ID
  81.     if(intval($custom_id) > 0) {
  82.         $ratings_id = $custom_id;
  83.     } else {
  84.         // If Global $id is 0, Get The Loop Post ID
  85.         if($id === 0) {
  86.             $ratings_id = get_the_ID();
  87.         } elseif (is_null($id)) {
  88.             global $post;
  89.             $ratings_id = $post->ID;
  90.         } else {
  91.             $ratings_id = $id;
  92.         }
  93.     }
  94.  
  95.     $ratings_id = (int) $ratings_id;
  96.  
  97.     // Loading Style
  98.     $postratings_ajax_style = get_option('postratings_ajax_style');
  99.     if(intval($postratings_ajax_style['loading']) == 1) {
  100.         $loading = '<' . $start_tag . ' id="post-ratings-' . $ratings_id . '-loading" class="post-ratings-loading">
  101.            <img src="' . plugins_url('wp-postratings/images/loading.gif') . '" width="16" height="16" class="post-ratings-image" />' . esc_html__( 'Loading...', 'wp-postratings' ) . '</' . $start_tag . '>';
  102.     } else {
  103.         $loading = '';
  104.     }
  105.     // Check To See Whether User Has Voted
  106.     $user_voted = check_rated($ratings_id);
  107.     // HTML Attributes
  108.     $ratings_options = get_option('postratings_options');
  109.     $ratings_options['richsnippet'] = isset( $ratings_options['richsnippet'] ) ? $ratings_options['richsnippet'] : 1;
  110.     if( is_singular() && $ratings_options['richsnippet'] ) {
  111.         $itemtype = apply_filters('wp_postratings_schema_itemtype', 'itemscope itemtype="http://schema.org/Article"');
  112.         $attributes = 'id="post-ratings-'.$ratings_id.'" class="post-ratings" '.$itemtype;
  113.     } else {
  114.         $attributes = 'id="post-ratings-'.$ratings_id.'" class="post-ratings"';
  115.     }
  116.     // If User Voted Or Is Not Allowed To Rate
  117.     if($user_voted) {
  118.         if(!$display) {
  119.             return "<$start_tag $attributes>".the_ratings_results($ratings_id).'</'.$start_tag.'>'.$loading;
  120.         } else {
  121.             echo "<$start_tag $attributes>".the_ratings_results($ratings_id).'</'.$start_tag.'>'.$loading;
  122.         }
  123.     // If User Is Not Allowed To Rate
  124.     } else if(!check_allowtorate()) {
  125.         if(!$display) {
  126.             return "<$start_tag $attributes>".the_ratings_results($ratings_id, 0, 0, 0, 1).'</'.$start_tag.'>'.$loading;
  127.         } else {
  128.             echo "<$start_tag $attributes>".the_ratings_results($ratings_id, 0, 0, 0, 1).'</'.$start_tag.'>'.$loading;
  129.         }
  130.     // If User Has Not Voted
  131.     } else {
  132.         if(!$display) {
  133.             return "<$start_tag $attributes data-nonce=\"".wp_create_nonce('postratings_'.$ratings_id.'-nonce')."\">".the_ratings_vote($ratings_id).'</'.$start_tag.'>'.$loading;
  134.         } else {
  135.             echo "<$start_tag $attributes data-nonce=\"".wp_create_nonce('postratings_'.$ratings_id.'-nonce')."\">".the_ratings_vote($ratings_id).'</'.$start_tag.'>'.$loading;
  136.         }
  137.     }
  138. }
  139.  
  140.  
  141. ### Function: Display Ratings Results
  142. function the_ratings_results( $post_id, $new_user = 0, $new_score = 0, $new_average = 0, $type = 0 ) {
  143.     if( $new_user === 0 && $new_score === 0 && $new_average === 0 ) {
  144.         $post_ratings_data = null;
  145.     } else {
  146.         $post_ratings_data = new stdClass();
  147.         $post_ratings_data->ratings_users = $new_user;
  148.         $post_ratings_data->ratings_score = $new_score;
  149.         $post_ratings_data->ratings_average = $new_average;
  150.     }
  151.     // Display The Contents
  152.     if( $type === 1 ) {
  153.         $template_postratings_text = stripslashes( get_option( 'postratings_template_permission' ) );
  154.     } else {
  155.         $template_postratings_text = stripslashes( get_option( 'postratings_template_text' ) );
  156.     }
  157.     // Return Post Ratings Template
  158.     return expand_ratings_template( $template_postratings_text, $post_id, $post_ratings_data );
  159. }
  160.  
  161.  
  162. ### Function: Display Ratings Vote
  163. function the_ratings_vote($post_id, $new_user = 0, $new_score = 0, $new_average = 0) {
  164.   if($new_user == 0 && $new_score == 0 && $new_average == 0) {
  165.     $post_ratings_data = null;
  166.   } else {
  167.     $post_ratings_data = new stdClass();
  168.     $post_ratings_data->ratings_users = $new_user;
  169.     $post_ratings_data->ratings_score = $new_score;
  170.     $post_ratings_data->ratings_average = $new_average;
  171.   }
  172.     // If No Ratings, Return No Ratings templae
  173.     if( intval( get_post_meta($post_id, 'ratings_users', true ) ) === 0 ) {
  174.         $template_postratings_none = stripslashes(get_option('postratings_template_none'));
  175.         // Return Post Ratings Template
  176.         return expand_ratings_template($template_postratings_none, $post_id, $post_ratings_data);
  177.     } else {
  178.         // Display The Contents
  179.         $template_postratings_vote = stripslashes(get_option('postratings_template_vote'));
  180.         // Return Post Ratings Voting Template
  181.         return expand_ratings_template($template_postratings_vote, $post_id, $post_ratings_data);
  182.     }
  183. }
  184.  
  185.  
  186. ### Function: Check Who Is Allow To Rate
  187. function check_allowtorate() {
  188.     $allow_to_vote = intval(get_option('postratings_allowtorate'));
  189.     switch($allow_to_vote) {
  190.         // Guests Only
  191.         case 0:
  192.             return ! is_user_logged_in();
  193.             break;
  194.         // Logged-in users only
  195.         case 1:
  196.             return is_user_logged_in();
  197.             break;
  198.         // Users registered on blog (for multisite)
  199.         case 3:
  200.             return is_user_member_of_blog();
  201.             break;
  202.         // Registered Users And Guests
  203.         case 2:
  204.         default:
  205.             return true;
  206.     }
  207. }
  208.  
  209.  
  210. ### Function: Check Whether User Have Rated For The Post
  211. function check_rated( $post_id ) {
  212.     $postratings_logging_method = (int) get_option( 'postratings_logging_method' );
  213.     $rated = false;
  214.     switch( $postratings_logging_method ) {
  215.         // Do Not Log
  216.         case 0:
  217.             $rated = false;
  218.             break;
  219.         // Logged By Cookie
  220.         case 1:
  221.             $rated = check_rated_cookie( $post_id );
  222.             break;
  223.         // Logged By IP
  224.         case 2:
  225.             $rated = check_rated_ip( $post_id );
  226.             break;
  227.         // Logged By Cookie And IP
  228.         case 3:
  229.             $rated_cookie = check_rated_cookie( $post_id );
  230.             if( $rated_cookie > 0 ) {
  231.                 $rated = true;
  232.             } else {
  233.                 $rated = check_rated_ip( $post_id );
  234.             }
  235.             break;
  236.         // Logged By Username
  237.         case 4:
  238.             $rated = check_rated_username( $post_id );
  239.             break;
  240.     }
  241.  
  242.     $rated = apply_filters( 'wp_postratings_check_rated', $rated, $post_id );
  243.  
  244.     return $rated;
  245. }
  246.  
  247.  
  248. ### Function: Check Rated By Cookie
  249. function check_rated_cookie($post_id) {
  250.     if(isset($_COOKIE["rated_$post_id"])) {
  251.         return true;
  252.     } else {
  253.         return false;
  254.     }
  255. }
  256.  
  257.  
  258. ### Function: Check Rated By IP
  259. function check_rated_ip($post_id) {
  260.     global $wpdb;
  261.     // Check IP From IP Logging Database
  262.     $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() ) );
  263.     // 0: False | > 0: True
  264.     return intval($get_rated);
  265. }
  266.  
  267.  
  268. ### Function: Check Rated By Username
  269. function check_rated_username($post_id) {
  270.     global $wpdb, $user_ID;
  271.     if ( !is_user_logged_in() ) {
  272.         return 0;
  273.     }
  274.     // Check User ID From IP Logging Database
  275.     $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 ) );
  276.     // 0: False | > 0: True
  277.     return (int) $get_rated;
  278. }
  279.  
  280.  
  281. ### Function: Get Comment Authors Ratings
  282. add_action ('loop_start', 'get_comment_authors_ratings' );
  283. function get_comment_authors_ratings() {
  284.     global $wpdb, $post, $comment_authors_ratings;
  285.     $comment_authors_ratings_results = null;
  286.     if ( !is_feed() && !is_admin() ) {
  287.         $comment_authors_ratings = array();
  288.         if ( $post && $post->ID ) {
  289.             $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 ) );
  290.         }
  291.         if ( $comment_authors_ratings_results ) {
  292.             foreach ( $comment_authors_ratings_results as $comment_authors_ratings_result ) {
  293.                 $comment_author = stripslashes( $comment_authors_ratings_result->rating_username );
  294.                 $comment_authors_ratings[ $comment_author ] = $comment_authors_ratings_result->rating_rating;
  295.                 $comment_authors_ratings[ $comment_authors_ratings_result->rating_ip ] = $comment_authors_ratings_result->rating_rating;
  296.             }
  297.         }
  298.     }
  299. }
  300.  
  301.  
  302. ### Function: Comment Author Ratings
  303. function comment_author_ratings( $comment_author_specific = '', $display = true ) {
  304.     global $comment_authors_ratings;
  305.     if ( get_comment_type() === 'comment' ) {
  306.         $post_ratings_images = '';
  307.         $ratings_image = get_option( 'postratings_image' );
  308.         $ratings_max = (int) get_option( 'postratings_max' );
  309.         $ratings_custom = (int) get_option( 'postratings_customrating' );
  310.         $postratings_logging_method = (int) get_option( 'postratings_logging_method' );
  311.         $comment_author = $comment_author_specific;
  312.         if ( empty( $comment_author ) ) {
  313.             $comment_author = get_comment_author();
  314.         }
  315.  
  316.         $comment_author_rating = (int) $comment_authors_ratings[ $comment_author ];
  317.         // If we logged by username, we don't want to use IP at all.
  318.         if ( $postratings_logging_method !== 4 && $comment_author_rating === 0 ) {
  319.             $comment_author_rating = (int) $comment_authors_ratings[ get_comment_author_IP() ];
  320.         }
  321.         if ( $comment_author_rating !== 0 ) {
  322.             // Display Rated Images
  323.             if ( $ratings_custom && $ratings_max === 2 ) {
  324.                 if ( $comment_author_rating > 0 ) {
  325.                     $comment_author_rating = '+' . $comment_author_rating;
  326.                 }
  327.             }
  328.             $image_alt = sprintf( __( '%s gives a rating of %s', 'wp-postratings' ), $comment_author, $comment_author_rating );
  329.             $post_ratings_images = get_ratings_images_comment_author( $ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt );
  330.         }
  331.         if($display) {
  332.             return $post_ratings_images;
  333.         }
  334.  
  335.         return $post_ratings_images;
  336.     }
  337. }
  338.  
  339.  
  340. ### Function:  Display Comment Author Ratings
  341. add_filter( 'comment_text', 'comment_author_ratings_filter' );
  342. function comment_author_ratings_filter( $comment_text ) {
  343.     global $comment, $comment_authors_ratings;
  344.  
  345.     $output = '';
  346.     $display_comment_author_ratings = apply_filters( 'wp_postratings_display_comment_author_ratings', false );
  347.  
  348.     if ( $display_comment_author_ratings ) {
  349.         if ( !is_feed() && !is_admin() ) {
  350.             if ( ! empty( $comment ) && get_comment_type() === 'comment' ) {
  351.                 $post_ratings_images = '';
  352.                 $ratings_image = get_option( 'postratings_image' );
  353.                 $ratings_max = (int) get_option( 'postratings_max' );
  354.                 $ratings_custom = (int) get_option( 'postratings_customrating' );
  355.                 $postratings_logging_method = (int) get_option( 'postratings_logging_method' );
  356.                 $comment_author = get_comment_author();
  357.                 $comment_author_rating = (int) $comment_authors_ratings[ $comment_author ];
  358.                 // If we logged by username, we don't want to use IP at all.
  359.                 if ( $postratings_logging_method !== 4 && $comment_author_rating === 0 ) {
  360.                     $comment_author_rating = (int) $comment_authors_ratings[ get_comment_author_IP() ];
  361.                 }
  362.                 if ( $comment_author_rating !== 0 ) {
  363.                     // Display Rated Images
  364.                     if ( $ratings_custom && $ratings_max === 2 ) {
  365.                         if ( $comment_author_rating > 0 ) {
  366.                             $comment_author_rating = '+' . $comment_author_rating;
  367.                         }
  368.                     }
  369.                     $image_alt = sprintf( __( '%s gives a rating of %s', 'wp-postratings' ), $comment_author, $comment_author_rating );
  370.                     $post_ratings_images = get_ratings_images_comment_author( $ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt );
  371.                 }
  372.                 $output .= '<div class="post-ratings-comment-author">';
  373.                 if ( $post_ratings_images !== '' ) {
  374.                     $output .= get_comment_author() . ' ratings for this post: ' . $post_ratings_images;
  375.                 } else {
  376.                     $output .= get_comment_author() . ' did not rate this post.';
  377.                 }
  378.                 $output .= '</div>';
  379.             }
  380.         }
  381.     }
  382.     return $comment_text.$output;
  383. }
  384.  
  385.  
  386. ### Function: Get IP Address
  387. if(!function_exists('get_ipaddress')) {
  388.     function get_ipaddress() {
  389.         if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  390.             $ip_address = $_SERVER["REMOTE_ADDR"];
  391.         } else {
  392.             $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
  393.         }
  394.         if(strpos($ip_address, ',') !== false) {
  395.             $ip_address = explode(',', $ip_address);
  396.             $ip_address = $ip_address[0];
  397.         }
  398.         return esc_attr($ip_address);
  399.     }
  400. }
  401.  
  402.  
  403. ### Function: Return All Images From A Rating Image Folder
  404. function ratings_images_folder($folder_name) {
  405.     $normal_images = array('rating_over.'.RATINGS_IMG_EXT, 'rating_on.'.RATINGS_IMG_EXT, 'rating_half.'.RATINGS_IMG_EXT, 'rating_off.'.RATINGS_IMG_EXT);
  406.     $postratings_path = WP_PLUGIN_DIR.'/wp-postratings/images/'.$folder_name;
  407.     $images_count = 1;
  408.     $count = 0;
  409.     $rating['max'] = 0;
  410.     $rating['custom'] = 0;
  411.     $rating['images'] = array();
  412.     if(is_dir($postratings_path)) {
  413.         if($handle = @opendir($postratings_path)) {
  414.             while (false !== ($filename = readdir($handle))) {
  415.                 if ($filename != '.' && $filename != '..' && substr($filename, -8) != '-rtl.'.RATINGS_IMG_EXT && strpos($filename, '.') !== 0) {
  416.                     if(in_array($filename, $normal_images)) {
  417.                         $count++;
  418.                     } elseif(intval(substr($filename,7, -7)) > $rating['max']) {
  419.                         $rating['max'] = intval(substr($filename,7, -7));
  420.                     }
  421.                     $rating['images'][] = $filename;
  422.                     $images_count++;
  423.                 }
  424.             }
  425.             closedir($handle);
  426.         }
  427.     }
  428.     if($count != sizeof($normal_images)) {
  429.         $rating['custom'] = 1;
  430.     }
  431.     if($rating['max'] == 0) {
  432.         $rating['max'] = intval(get_option('postratings_max'));
  433.     }
  434.     return $rating;
  435. }
  436.  
  437.  
  438. ### Function: Add PostRatings To Content Automatically
  439. //add_action('the_content', 'add_ratings_to_content');
  440. function add_ratings_to_content($content) {
  441.     if (!is_feed()) {
  442.         $content .= the_ratings('div', 0, false);
  443.     }
  444.     return $content;
  445. }
  446.  
  447.  
  448. ### Function: Snippet Text
  449. if(!function_exists('snippet_text')) {
  450.     function snippet_text($text, $length = 0) {
  451.         if (defined('MB_OVERLOAD_STRING')) {
  452.           $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
  453.              if (mb_strlen($text) > $length) {
  454.                 return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
  455.              } else {
  456.                 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
  457.              }
  458.         } else {
  459.             $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset'));
  460.              if (strlen($text) > $length) {
  461.                 return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...';
  462.              } else {
  463.                 return htmlentities($text, ENT_COMPAT, get_option('blog_charset'));
  464.              }
  465.         }
  466.     }
  467. }
  468.  
  469.  
  470. ### Function: Process Post Excerpt, For Some Reasons, The Default get_post_excerpt() Does Not Work As Expected
  471. function ratings_post_excerpt($post_id, $post_excerpt, $post_content) {
  472.     if( post_password_required( $post_id ) ) {
  473.         return esc_html__( 'There is no excerpt because this is a protected post.', 'wp-postratings' );
  474.     }
  475.     if(empty($post_excerpt)) {
  476.         return snippet_text( strip_tags( strip_shortcodes( $post_content ) ), 200 );
  477.     } else {
  478.         return strip_shortcodes( $post_excerpt );
  479.     }
  480. }
  481.  
  482.  
  483. ### Function: Add Rating Custom Fields
  484. add_action('publish_post', 'add_ratings_fields');
  485. add_action('publish_page', 'add_ratings_fields');
  486. function add_ratings_fields($post_ID) {
  487.     global $wpdb;
  488.     if(!wp_is_post_revision($post_ID)) {
  489.         add_post_meta($post_ID, 'ratings_users', 0, true);
  490.         add_post_meta($post_ID, 'ratings_score', 0, true);
  491.         add_post_meta($post_ID, 'ratings_average', 0, true);
  492.     }
  493. }
  494.  
  495.  
  496. ### Function:Delete Rating Custom Fields
  497. add_action('delete_post', 'delete_ratings_fields');
  498. function delete_ratings_fields($post_ID) {
  499.     global $wpdb;
  500.     if(!wp_is_post_revision($post_ID)) {
  501.         delete_post_meta($post_ID, 'ratings_users');
  502.         delete_post_meta($post_ID, 'ratings_score');
  503.         delete_post_meta($post_ID, 'ratings_average');
  504.     }
  505. }
  506.  
  507.  
  508. ### Function: Process Ratings
  509. add_action('wp_ajax_postratings', 'process_ratings');
  510. add_action('wp_ajax_nopriv_postratings', 'process_ratings');
  511. function process_ratings() {
  512.     global $wpdb, $user_identity, $user_ID;
  513.  
  514.     if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'postratings')
  515.     {
  516.         $rate = intval($_REQUEST['rate']);
  517.         $post_id = intval($_REQUEST['pid']);
  518.  
  519.         // Verify Referer
  520.         if(!check_ajax_referer('postratings_'.$post_id.'-nonce', 'postratings_'.$post_id.'_nonce', false))
  521.         {
  522.             esc_html_e('Failed To Verify Referrer', 'wp-postratings');
  523.             exit();
  524.         }
  525.  
  526.         if($rate > 0 && $post_id > 0 && check_allowtorate()) {
  527.             // Check For Bot
  528.             $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');
  529.             $useragent = $_SERVER['HTTP_USER_AGENT'];
  530.             foreach ($bots_useragent as $bot) {
  531.                 if (stristr($useragent, $bot) !== false) {
  532.                     return;
  533.                 }
  534.             }
  535.             header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
  536.             $rated = check_rated($post_id);
  537.             // Check Whether Post Has Been Rated By User
  538.             if(!$rated) {
  539.                 // Check Whether Is There A Valid Post
  540.                 $post = get_post($post_id);
  541.                 // If Valid Post Then We Rate It
  542.                 if($post && !wp_is_post_revision($post)) {
  543.                     $ratings_max = intval(get_option('postratings_max'));
  544.                     $ratings_custom = intval(get_option('postratings_customrating'));
  545.                     $ratings_value = get_option('postratings_ratingsvalue');
  546.                     $post_title = addslashes($post->post_title);
  547.                     $post_ratings = get_post_custom($post_id);
  548.                     $post_ratings_users = ! empty( $post_ratings['ratings_users'] ) ? intval($post_ratings['ratings_users'][0]) : 0;
  549.                     $post_ratings_score = ! empty( $post_ratings['ratings_score'] ) ? intval($post_ratings['ratings_score'][0]) : 0;
  550.                     // Check For Ratings Lesser Than 1 And Greater Than $ratings_max
  551.                     if($rate < 1 || $rate > $ratings_max) {
  552.                         $rate = 0;
  553.                     }
  554.                     $post_ratings_users = ($post_ratings_users+1);
  555.                     $post_ratings_score = ($post_ratings_score+intval($ratings_value[$rate-1]));
  556.                     $post_ratings_average = round($post_ratings_score/$post_ratings_users, 2);
  557.                     update_post_meta($post_id, 'ratings_users', $post_ratings_users);
  558.                     update_post_meta($post_id, 'ratings_score', $post_ratings_score);
  559.                     update_post_meta($post_id, 'ratings_average', $post_ratings_average);
  560.  
  561.                     // Add Log
  562.                     if(!empty($user_identity)) {
  563.                         $rate_user = addslashes($user_identity);
  564.                     } elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) {
  565.                         $rate_user = addslashes($_COOKIE['comment_author_'.COOKIEHASH]);
  566.                     } else {
  567.                         $rate_user = __('Guest', 'wp-postratings');
  568.                     }
  569.                     $rate_user = apply_filters( 'wp_postratings_process_ratings_user', $rate_user );
  570.                     $rate_userid = apply_filters( 'wp_postratings_process_ratings_userid', intval( $user_ID ) );
  571.  
  572.                     // Only Create Cookie If User Choose Logging Method 1 Or 3
  573.                     $postratings_logging_method = intval(get_option('postratings_logging_method'));
  574.                     if($postratings_logging_method == 1 || $postratings_logging_method == 3) {
  575.                         $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], apply_filters('wp_postratings_cookie_expiration', (time() + 30000000) ), apply_filters('wp_postratings_cookiepath', SITECOOKIEPATH));
  576.                     }
  577.                     // Log Ratings No Matter What
  578.                     $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 ) );
  579.                     // Allow Other Plugins To Hook When A Post Is Rated
  580.                     do_action('rate_post', $rate_userid, $post_id, $ratings_value[$rate-1]);
  581.                     // Output AJAX Result
  582.                     echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average);
  583.                     exit();
  584.                 } else {
  585.                     printf(esc_html__('Invalid Post ID (#%s).', 'wp-postratings'), $post_id);
  586.                     exit();
  587.                 } // End if($post)
  588.             } else {
  589.                 printf(esc_html__('You Had Already Rated This Post. Post ID #%s.', 'wp-postratings'), $post_id);
  590.                 exit();
  591.             }// End if(!$rated)
  592.         } // End if($rate && $post_id && check_allowtorate())
  593.     } // End if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'postratings')
  594. }
  595.  
  596.  
  597. ### Function: Process Ratings
  598. add_action('wp_ajax_postratings-admin', 'manage_ratings');
  599. function manage_ratings()
  600. {
  601.     ### Form Processing
  602.    if(isset($_GET['action']) && $_GET['action'] == 'postratings-admin')
  603.     {
  604.         check_ajax_referer('wp-postratings_option_update_individual_rating');
  605.  
  606.         //Variables
  607.         $postratings_url = plugins_url('wp-postratings/images');
  608.         $postratings_path = WP_PLUGIN_DIR.'/wp-postratings/images';
  609.         $postratings_ratingstext = get_option('postratings_ratingstext');
  610.         $postratings_ratingsvalue = get_option('postratings_ratingsvalue');
  611.  
  612.         // Form Processing
  613.         $postratings_customrating = intval($_GET['custom']);
  614.         $postratings_image = esc_attr( trim( $_GET['image'] ) );
  615.         $postratings_max = intval($_GET['max']);
  616.  
  617.         // If It Is A Up/Down Rating
  618.         if($postratings_customrating && $postratings_max == 2) {
  619.             $postratings_ratingsvalue[0] = -1;
  620.             $postratings_ratingsvalue[1] = 1;
  621.             $postratings_ratingstext[0] = __('Vote Down', 'wp-postratings');
  622.             $postratings_ratingstext[1] = __('Vote Up', 'wp-postratings');
  623.         } else {
  624.             for($i = 0; $i < $postratings_max; $i++) {
  625.                 if($i > 0) {
  626.                     $postratings_ratingstext[$i] = sprintf(esc_html__('%s Stars', 'wp-postratings'), number_format_i18n($i+1));
  627.                 } else {
  628.                     $postratings_ratingstext[$i] = sprintf(esc_html__('%s Star', 'wp-postratings'), number_format_i18n($i+1));
  629.                 }
  630.                 $postratings_ratingsvalue[$i] = $i+1;
  631.             }
  632.         }
  633. ?>
  634.         <table class="form-table">
  635.             <thead>
  636.                 <tr>
  637.                     <th><?php esc_html_e('Rating Image', 'wp-postratings'); ?></th>
  638.                     <th><?php esc_html_e('Rating Text', 'wp-postratings'); ?></th>
  639.                     <th><?php esc_html_e('Rating Value', 'wp-postratings'); ?></th>
  640.                 </tr>
  641.             </thead>
  642.             <tbody>
  643.                 <?php
  644.                     for($i = 1; $i <= $postratings_max; $i++) {
  645.                         $postratings_text = stripslashes($postratings_ratingstext[$i-1]);
  646.                         $postratings_value = $postratings_ratingsvalue[$i-1];
  647.                         if($postratings_value > 0) {
  648.                             $postratings_value = '+'.$postratings_value;
  649.                         }
  650.                         echo '<tr>'."\n";
  651.                         echo '<td>'."\n";
  652.                         if(is_rtl() && file_exists($postratings_path.'/'.$postratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  653.                             echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT.'" alt="rating_start-rtl.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  654.                         } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  655.                             echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_start.'.RATINGS_IMG_EXT.'" alt="rating_start.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  656.                         }
  657.                         if($postratings_customrating) {
  658.                             if($postratings_max == 2) {
  659.                                 echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT.'" alt="rating_'.$i.'_on.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  660.                             } else {
  661.                                 for($j = 1; $j < ($i+1); $j++) {
  662.                                     echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_'.$j.'_on.'.RATINGS_IMG_EXT.'" alt="rating_on.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  663.                                 }
  664.                             }
  665.                         } else {
  666.                             for($j = 1; $j < ($i+1); $j++) {
  667.                                 echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_on.'.RATINGS_IMG_EXT.'" alt="rating_on.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  668.                             }
  669.                         }
  670.                         if(is_rtl() && file_exists($postratings_path.'/'.$postratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  671.                             echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT.'" alt="rating_end-rtl.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  672.                         } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  673.                             echo '<img src="'.$postratings_url.'/'.$postratings_image.'/rating_end.'.RATINGS_IMG_EXT.'" alt="rating_end.'.RATINGS_IMG_EXT.'" class="post-ratings-image" />';
  674.                         }
  675.                         echo '</td>'."\n";
  676.                         echo '<td>'."\n";
  677.                         echo '<input type="text" id="postratings_ratingstext_'.$i.'" name="postratings_ratingstext[]" value="'.$postratings_text.'" size="20" maxlength="50" />'."\n";
  678.                         echo '</td>'."\n";
  679.                         echo '<td>'."\n";
  680.                         echo '<input type="text" id="postratings_ratingsvalue_'.$i.'" name="postratings_ratingsvalue[]" value="'.$postratings_value.'" size="2" maxlength="2" />'."\n";
  681.                         echo '</td>'."\n";
  682.                         echo '</tr>'."\n";
  683.                     }
  684.                 ?>
  685.             </tbody>
  686.         </table>
  687. <?php
  688.     }
  689.     exit();
  690. }
  691.  
  692.  
  693. ### Function: Modify Default WordPress Listing To Make It Sorted By Most Rated
  694. function ratings_most_fields($content) {
  695.     global $wpdb;
  696.     $content .= ", ($wpdb->postmeta.meta_value+0) AS ratings_votes";
  697.     return $content;
  698. }
  699. function ratings_most_join($content) {
  700.     global $wpdb;
  701.     $content .= " LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID AND $wpdb->postmeta.meta_key = 'ratings_users'";
  702.     return $content;
  703. }
  704. function ratings_most_orderby( $content ) {
  705.     $orderby = trim( addslashes( get_query_var( 'r_orderby' ) )) ;
  706.     if( empty( $orderby ) || ( $orderby !== 'asc' && $orderby !== 'desc' ) ) {
  707.         $orderby = 'desc';
  708.     }
  709.     $content = " ratings_votes $orderby";
  710.     return $content;
  711. }
  712.  
  713.  
  714. ### Function: Modify Default WordPress Listing To Make It Sorted By Highest Rated
  715. function ratings_highest_fields($content) {
  716.     $content .= ", (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users";
  717.     return $content;
  718. }
  719. function ratings_highest_join($content) {
  720.     global $wpdb;
  721.     $ratings_max = intval(get_option('postratings_max'));
  722.     $ratings_custom = intval(get_option('postratings_customrating'));
  723.  
  724.     $content .= " LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID";
  725.     if($ratings_custom && $ratings_max == 2) {
  726.         $content .= " AND t1.meta_key = 'ratings_score'";
  727.     } else {
  728.         $content .= " AND t1.meta_key = 'ratings_average'";
  729.     }
  730.     $content .= " LEFT JOIN $wpdb->postmeta As t2 ON t1.post_id = t2.post_id AND t2.meta_key = 'ratings_users'";
  731.     return $content;
  732. }
  733. function ratings_highest_orderby( $content ) {
  734.     $orderby = trim( addslashes( get_query_var( 'r_orderby' ) ) );
  735.     if( empty( $orderby ) || ( $orderby !== 'asc' && $orderby !== 'desc' ) ) {
  736.         $orderby = 'desc';
  737.     }
  738.     $content = " ratings_average $orderby, ratings_users $orderby";
  739.     return $content;
  740. }
  741.  
  742.  
  743. ### Function: Ratings Public Variables
  744. add_filter('query_vars', 'ratings_variables');
  745. function ratings_variables($public_query_vars) {
  746.     $public_query_vars[] = 'r_sortby';
  747.     $public_query_vars[] = 'r_orderby';
  748.     return $public_query_vars;
  749. }
  750.  
  751.  
  752. ### Function: Sort Ratings Posts
  753. add_action('pre_get_posts', 'ratings_sorting');
  754. function ratings_sorting($local_wp_query) {
  755.     if($local_wp_query->get('r_sortby') == 'most_rated') {
  756.         add_filter('posts_fields', 'ratings_most_fields');
  757.         add_filter('posts_join', 'ratings_most_join');
  758.         add_filter('posts_orderby', 'ratings_most_orderby');
  759.         remove_filter('posts_fields', 'ratings_highest_fields');
  760.         remove_filter('posts_join', 'ratings_highest_join');
  761.         remove_filter('posts_orderby', 'ratings_highest_orderby');
  762.     } elseif($local_wp_query->get('r_sortby') == 'highest_rated') {
  763.         add_filter('posts_fields', 'ratings_highest_fields');
  764.         add_filter('posts_join', 'ratings_highest_join');
  765.         add_filter('posts_orderby', 'ratings_highest_orderby');
  766.         remove_filter('posts_fields', 'ratings_most_fields');
  767.         remove_filter('posts_join', 'ratings_most_join');
  768.         remove_filter('posts_orderby', 'ratings_most_orderby');
  769.     } else {
  770.         remove_filter('posts_fields', 'ratings_highest_fields');
  771.         remove_filter('posts_join', 'ratings_highest_join');
  772.         remove_filter('posts_orderby', 'ratings_highest_orderby');
  773.         remove_filter('posts_fields', 'ratings_most_fields');
  774.         remove_filter('posts_join', 'ratings_most_join');
  775.         remove_filter('posts_orderby', 'ratings_most_orderby');
  776.     }
  777. }
  778.  
  779.  
  780. add_action('pre_get_posts', 'sort_postratings');
  781. function sort_postratings($query) {
  782.     if(!is_admin())
  783.         return;
  784.     $orderby = $query->get('orderby');
  785.     if('ratings' == $orderby) {
  786.         $query->set('meta_key', 'ratings_average');
  787.         $query->set('orderby', 'meta_value_num');
  788.     }
  789. }
  790.  
  791.  
  792. ### Function: Plug Into WP-Stats
  793. add_action( 'plugins_loaded','postratings_wp_stats' );
  794. function postratings_wp_stats() {
  795.     add_filter( 'wp_stats_page_admin_plugins', 'postratings_page_admin_general_stats' );
  796.     add_filter( 'wp_stats_page_admin_most', 'postratings_page_admin_most_stats' );
  797.     add_filter( 'wp_stats_page_plugins', 'postratings_page_general_stats' );
  798.     add_filter( 'wp_stats_page_most', 'postratings_page_most_stats' );
  799. }
  800.  
  801.  
  802. ### Function: Add WP-PostRatings General Stats To WP-Stats Page Options
  803. function postratings_page_admin_general_stats($content) {
  804.     $stats_display = get_option('stats_display');
  805.     if($stats_display['ratings'] == 1) {
  806.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_ratings" value="ratings" checked="checked" />&nbsp;&nbsp;<label for="wpstats_ratings">'.esc_html__('WP-PostRatings', 'wp-postratings').'</label><br />'."\n";
  807.     } else {
  808.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_ratings" value="ratings" />&nbsp;&nbsp;<label for="wpstats_ratings">'.esc_html__('WP-PostRatings', 'wp-postratings').'</label><br />'."\n";
  809.     }
  810.     return $content;
  811. }
  812.  
  813.  
  814. ### Function: Add WP-PostRatings Top Most/Highest Stats To WP-Stats Page Options
  815. function postratings_page_admin_most_stats($content) {
  816.     $stats_display = get_option('stats_display');
  817.     $stats_mostlimit = intval(get_option('stats_mostlimit'));
  818.     if($stats_display['rated_highest_post'] == 1) {
  819.         $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">'.esc_html(sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  820.     } else {
  821.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_post" value="rated_highest_post" />&nbsp;&nbsp;<label for="wpstats_rated_highest_post">'.esc_html(sprintf(_n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  822.     }
  823.     if($stats_display['rated_highest_page'] == 1) {
  824.         $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">'.esc_html(sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  825.     } else {
  826.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_highest_page" value="rated_highest_page" />&nbsp;&nbsp;<label for="wpstats_rated_highest_page">'.esc_html(sprintf(_n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  827.     }
  828.     if($stats_display['rated_most_post'] == 1) {
  829.         $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">'.esc_html(sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  830.     } else {
  831.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_post" value="rated_most_post" />&nbsp;&nbsp;<label for="wpstats_rated_most_post">'.esc_html(sprintf(_n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  832.     }
  833.     if($stats_display['rated_most_page'] == 1) {
  834.         $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">'.esc_html(sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  835.     } else {
  836.         $content .= '<input type="checkbox" name="stats_display[]" id="wpstats_rated_most_page" value="rated_most_page" />&nbsp;&nbsp;<label for="wpstats_rated_most_page">'.esc_html(sprintf(_n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings'), number_format_i18n($stats_mostlimit))).'</label><br />'."\n";
  837.     }
  838.     return $content;
  839. }
  840.  
  841.  
  842. ### Function: Add WP-PostRatings General Stats To WP-Stats Page
  843. function postratings_page_general_stats($content) {
  844.     $stats_display = get_option('stats_display');
  845.     if($stats_display['ratings'] == 1) {
  846.         $content .= '<p><strong>'.esc_html__('WP-PostRatings', 'wp-postratings').'</strong></p>'."\n";
  847.         $content .= '<ul>'."\n";
  848.         $content .= '<li>'.esc_html(sprintf(_n('%s user casted his vote.', '%s users casted their vote.', get_ratings_users(false), 'wp-postratings'), '<strong>'.number_format_i18n(get_ratings_users(false)).'</strong>')).'</li>'."\n";
  849.         $content .= '</ul>'."\n";
  850.     }
  851.     return $content;
  852. }
  853.  
  854.  
  855. ### Function: Add WP-PostRatings Top Most/Highest Stats To WP-Stats Page
  856. function postratings_page_most_stats($content) {
  857.     $stats_display = get_option('stats_display');
  858.     $stats_mostlimit = intval(get_option('stats_mostlimit'));
  859.     if($stats_display['rated_highest_post'] == 1) {
  860.         $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";
  861.         $content .= '<ul>'."\n";
  862.         $content .= get_highest_rated('post', 0, $stats_mostlimit, 0, false);
  863.         $content .= '</ul>'."\n";
  864.     }
  865.     if($stats_display['rated_highest_page'] == 1) {
  866.         $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";
  867.         $content .= '<ul>'."\n";
  868.         $content .= get_highest_rated('page', 0, $stats_mostlimit, 0, false);
  869.         $content .= '</ul>'."\n";
  870.     }
  871.     if($stats_display['rated_most_post'] == 1) {
  872.         $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";
  873.         $content .= '<ul>'."\n";
  874.         $content .= get_most_rated('post', 0, $stats_mostlimit, 0, false);
  875.         $content .= '</ul>'."\n";
  876.     }
  877.     if($stats_display['rated_most_page'] == 1) {
  878.         $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";
  879.         $content .= '<ul>'."\n";
  880.         $content .= get_most_rated('page', 0, $stats_mostlimit, 0, false);
  881.         $content .= '</ul>'."\n";
  882.     }
  883.     return $content;
  884. }
  885.  
  886.  
  887. ### Function: Gets HTML of rating images
  888. function get_ratings_images($ratings_custom, $ratings_max, $post_rating, $ratings_image, $image_alt, $insert_half) {
  889.     $ratings_images = '';
  890.     $image_alt = esc_attr( $image_alt );
  891.     $ratings_image = esc_attr( $ratings_image );
  892.     $image_alt = apply_filters( 'wp_postratings_ratings_image_alt', $image_alt );
  893.     if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  894.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  895.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  896.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  897.     }
  898.     if($ratings_custom) {
  899.         for($i=1; $i <= $ratings_max; $i++) {
  900.             if($i <= $post_rating) {
  901.                 $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" />';
  902.             } elseif($i == $insert_half) {
  903.                 if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT)) {
  904.                     $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" />';
  905.                 } else {
  906.                     $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" />';
  907.                 }
  908.             } else {
  909.                 $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" />';
  910.             }
  911.         }
  912.     } else {
  913.         for($i=1; $i <= $ratings_max; $i++) {
  914.             if($i <= $post_rating) {
  915.                 $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" />';
  916.             } elseif($i == $insert_half) {
  917.                 if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) {
  918.                     $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" />';
  919.                 } else {
  920.                     $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" />';
  921.                 }
  922.             } else {
  923.                 $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" />';
  924.             }
  925.         }
  926.     }
  927.     if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  928.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  929.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  930.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  931.     }
  932.     return $ratings_images;
  933. }
  934.  
  935.  
  936. ### Function: Gets HTML of rating images for voting
  937. function get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_rating, $ratings_image, $image_alt, $insert_half, $ratings_texts) {
  938.     $ratings_images = '';
  939.     $ratings_image = esc_attr( $ratings_image );
  940.     if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  941.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  942.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  943.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  944.     }
  945.     if($ratings_custom) {
  946.         for($i=1; $i <= $ratings_max; $i++) {
  947.             if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'half-rtl.'.RATINGS_IMG_EXT)) {
  948.                 $use_half_rtl = 1;
  949.             } else {
  950.                 $use_half_rtl = 0;
  951.             }
  952.             $ratings_text = esc_attr( stripslashes( $ratings_texts[$i-1] ) );
  953.             $ratings_text_js = esc_js( $ratings_text );
  954.             $image_alt = apply_filters( 'wp_postratings_ratings_image_alt', $ratings_text );
  955.             if($i <= $post_rating) {
  956.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  957.             } elseif($i == $insert_half) {
  958.                 if ($use_half_rtl) {
  959.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  960.                 } else {
  961.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  962.                 }
  963.             } else {
  964.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  965.             }
  966.         }
  967.     } else {
  968.         if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) {
  969.             $use_half_rtl = 1;
  970.         } else {
  971.             $use_half_rtl = 0;
  972.         }
  973.         for($i=1; $i <= $ratings_max; $i++) {
  974.             $ratings_text = esc_attr( stripslashes( $ratings_texts[$i-1] ) );
  975.             $ratings_text_js = esc_js( $ratings_text );
  976.             $image_alt = apply_filters( 'wp_postratings_ratings_image_alt', $ratings_text );
  977.             if($i <= $post_rating) {
  978.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_on.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  979.             } elseif($i == $insert_half) {
  980.                 if ($use_half_rtl) {
  981.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  982.                 } else {
  983.                     $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_half.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  984.                 }
  985.             } else {
  986.                 $ratings_images .= '<img id="rating_'.$post_id.'_'.$i.'" src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_off.'.RATINGS_IMG_EXT).'" alt="'.$image_alt.'" title="'.$image_alt.'" 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;" />';
  987.             }
  988.         }
  989.     }
  990.     if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) {
  991.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  992.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) {
  993.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  994.     }
  995.     return $ratings_images;
  996. }
  997.  
  998.  
  999. ### Function: Gets HTML of rating images for comment author
  1000. function get_ratings_images_comment_author($ratings_custom, $ratings_max, $comment_author_rating, $ratings_image, $image_alt) {
  1001.     $ratings_images = '';
  1002.     $image_alt = esc_attr( $image_alt );
  1003.     $ratings_image = esc_attr( $ratings_image );
  1004.     if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) {
  1005.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1006.     } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) {
  1007.         $ratings_images .= '<img src="'.plugins_url('/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT).'" alt="" class="post-ratings-image" />';
  1008.     }
  1009.     if($ratings_custom && $ratings_max == 2) {
  1010.         if($comment_author_rating > 0) {
  1011.             $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" />';
  1012.         } else {
  1013.             $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" />';
  1014.         }
  1015.     } elseif($ratings_custom) {
  1016.         for($i=1; $i <= $ratings_max; $i++) {
  1017.             if($i <= $comment_author_rating) {
  1018.                 $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" />';
  1019.             } else {
  1020.                 $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" />';
  1021.             }
  1022.         }
  1023.     } else {
  1024.         for($i=1; $i <= $ratings_max; $i++) {
  1025.             if($i <= $comment_author_rating) {
  1026.                 $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" />';
  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. ### Function: Replaces the template's variables with appropriate values
  1041. function expand_ratings_template($template, $post_data, $post_ratings_data = null, $max_post_title_chars = 0, $is_main_loop = true) {
  1042.     global $post;
  1043.  
  1044.     // Get global variables
  1045.     $ratings_image = get_option( 'postratings_image' );
  1046.     $ratings_max = (int) get_option( 'postratings_max' );
  1047.     $ratings_custom = (int) get_option( 'postratings_customrating' );
  1048.     $ratings_options = get_option( 'postratings_options' );
  1049.  
  1050.     if ( is_object( $post_data ) ) {
  1051.         $post_id = (int) $post_data->ID;
  1052.     } else {
  1053.         $post_id = (int) $post_data;
  1054.     }
  1055.  
  1056.     // Most likely from coming from Widget
  1057.     if ( isset( $post_data->ratings_users ) ) {
  1058.         $post_ratings_users = (int) $post_data->ratings_users;
  1059.         $post_ratings_score = (int) $post_data->ratings_score;
  1060.         $post_ratings_average = (float) $post_data->ratings_average;
  1061.     // Most Likely coming from the_ratings_vote or the_ratings_rate
  1062.     } else if ( isset( $post_ratings_data->ratings_users ) ) {
  1063.         $post_ratings_users = (int) $post_ratings_data->ratings_users;
  1064.         $post_ratings_score = (int) $post_ratings_data->ratings_score;
  1065.         $post_ratings_average = (float) $post_ratings_data->ratings_average;
  1066.     } else {
  1067.         if ( get_the_ID() !== $post_id ) {
  1068.             $post_ratings_data = get_post_custom( $post_id );
  1069.         } else {
  1070.             $post_ratings_data = get_post_custom();
  1071.         }
  1072.  
  1073.         $post_ratings_users = is_array( $post_ratings_data ) && array_key_exists( 'ratings_users', $post_ratings_data ) ? (int) $post_ratings_data['ratings_users'][0] : 0;
  1074.         $post_ratings_score = is_array( $post_ratings_data ) && array_key_exists( 'ratings_score', $post_ratings_data ) ? (int) $post_ratings_data['ratings_score'][0] : 0;
  1075.         $post_ratings_average = is_array( $post_ratings_data ) && array_key_exists( 'ratings_average', $post_ratings_data ) ? (float) $post_ratings_data['ratings_average'][0] : 0;
  1076.     }
  1077.  
  1078.     if($post_ratings_score === 0 || $post_ratings_users === 0) {
  1079.         $post_ratings = 0;
  1080.         $post_ratings_average = 0;
  1081.         $post_ratings_percentage = 0;
  1082.     } else {
  1083.         $post_ratings = round($post_ratings_average, 1);
  1084.         $post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);
  1085.     }
  1086.     $post_ratings_text = '<span class="post-ratings-text" id="ratings_'.$post_id.'_text"></span>';
  1087.     // Get the image's alt text
  1088.     if($ratings_custom && $ratings_max === 2) {
  1089.         if($post_ratings_score > 0) {
  1090.             $post_ratings_score = '+'.$post_ratings_score;
  1091.         }
  1092.         $post_ratings_alt_text = esc_html(sprintf(_n('%s rating', '%s ratings', $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)));
  1093.     } else {
  1094.         $post_ratings_score = number_format_i18n($post_ratings_score);
  1095.         $post_ratings_alt_text = esc_html(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));
  1096.     }
  1097.     // Check for half star
  1098.     $insert_half = 0;
  1099.     $average_diff = abs(floor($post_ratings_average)-$post_ratings);
  1100.     if($average_diff >= 0.25 && $average_diff <= 0.75) {
  1101.         $insert_half = ceil($post_ratings_average);
  1102.     } elseif($average_diff > 0.75) {
  1103.         $insert_half = ceil($post_ratings);
  1104.     }
  1105.     // Replace the variables
  1106.     $value = $template;
  1107.     if ( strpos( $template, '%RATINGS_IMAGES%') !== false ) {
  1108.         $get_ratings_images = get_ratings_images( $ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half );
  1109.         $post_ratings_images = apply_filters( 'wp_postratings_ratings_images', $get_ratings_images, $post_id, $post_ratings, $ratings_max );
  1110.         $value = str_replace( "%RATINGS_IMAGES%", $post_ratings_images, $value );
  1111.     }
  1112.     if ( strpos( $template, '%RATINGS_IMAGES_VOTE%' ) !== false ) {
  1113.         $ratings_texts = get_option('postratings_ratingstext');
  1114.         $get_ratings_images_vote = get_ratings_images_vote( $post_id, $ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half, $ratings_texts);
  1115.         $post_ratings_images = apply_filters( 'wp_postratings_ratings_images_vote', $get_ratings_images_vote, $post_id, $post_ratings, $ratings_max );
  1116.         $value = str_replace( "%RATINGS_IMAGES_VOTE%", $post_ratings_images, $value );
  1117.     }
  1118.     $value = str_replace("%RATINGS_ALT_TEXT%", $post_ratings_alt_text, $value);
  1119.     $value = str_replace("%RATINGS_TEXT%", $post_ratings_text, $value);
  1120.     $value = str_replace("%RATINGS_MAX%", number_format_i18n($ratings_max), $value);
  1121.     $value = str_replace("%RATINGS_SCORE%", $post_ratings_score, $value);
  1122.     $value = str_replace("%RATINGS_AVERAGE%", number_format_i18n($post_ratings_average, 2), $value);
  1123.     $value = str_replace("%RATINGS_PERCENTAGE%", number_format_i18n($post_ratings_percentage, 2), $value);
  1124.     $value = str_replace("%RATINGS_USERS%", number_format_i18n($post_ratings_users), $value);
  1125.  
  1126.     // Post Template Variables
  1127.     $post_link = get_permalink($post_data);
  1128.     $post_title = get_the_title($post_data);
  1129.     if ($max_post_title_chars > 0) {
  1130.         $post_title = snippet_text($post_title, $max_post_title_chars);
  1131.     }
  1132.     $value = str_replace("%POST_ID%", $post_id, $value);
  1133.     $value = str_replace("%POST_TITLE%", $post_title, $value);
  1134.     $value = str_replace("%POST_URL%", $post_link, $value);
  1135.  
  1136.     if (strpos($template, '%POST_EXCERPT%') !== false) {
  1137.         if (get_the_ID() != $post_id) {
  1138.             $post = &get_post($post_id);
  1139.         }
  1140.         $post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password);
  1141.         $value = str_replace("%POST_EXCERPT%", $post_excerpt, $value);
  1142.     }
  1143.     if (strpos($template, '%POST_CONTENT%') !== false) {
  1144.         if (get_the_ID() != $post_id) {
  1145.             $post = &get_post($post_id);
  1146.         }
  1147.         $value = str_replace("%POST_CONTENT%", get_the_content(), $value);
  1148.     }
  1149.     if (strpos($template, '%POST_THUMBNAIL%') !== false) {
  1150.         if (get_the_ID() != $post_id) {
  1151.             $post = &get_post($post_id);
  1152.         }
  1153.         $value = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( $post, 'thumbnail' ), $value );
  1154.     }
  1155.  
  1156.     // Google Rich Snippet
  1157.     $google_structured_data = '';
  1158.     $ratings_options['richsnippet'] = isset( $ratings_options['richsnippet'] ) ? $ratings_options['richsnippet'] : 1;
  1159.     if( $ratings_options['richsnippet'] && is_singular() && $is_main_loop ) {
  1160.         $itemtype = apply_filters( 'wp_postratings_schema_itemtype', 'itemscope itemtype="http://schema.org/Article"' );
  1161.  
  1162.         if( empty( $post_excerpt ) ) {
  1163.             $post_excerpt = ratings_post_excerpt( $post_id, $post->post_excerpt, $post->post_content, $post->post_password );
  1164.         }
  1165.         $post_meta = '<meta itemprop="headline" content="' . esc_attr( $post_title ) . '" />';
  1166.         $post_meta .= '<meta itemprop="description" content="' . wp_kses( $post_excerpt, array() ) . '" />';
  1167.         $post_meta .= '<meta itemprop="datePublished" content="' . mysql2date( 'c', $post->post_date, false ) . '" />';
  1168.         $post_meta .= '<meta itemprop="dateModified" content="' . mysql2date( 'c', $post->post_modified, false ) . '" />';
  1169.         $post_meta .= '<meta itemprop="url" content="' . $post_link . '" />';
  1170.         $post_meta .= '<meta itemprop="author" content="' . get_the_author() . '" />';
  1171.         $post_meta .= '<meta itemprop="mainEntityOfPage" content="' . get_permalink() . '" />';
  1172.         // Post Thumbnail
  1173.         if( has_post_thumbnail() ) {
  1174.             $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( null ) );
  1175.         }
  1176.         $thumbnail = apply_filters( 'wp_postratings_post_thumbnail', $thumbnail, $post_id );
  1177.         if( ! empty( $thumbnail ) ) {
  1178.             $post_meta .= '<div style="display: none;" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">';
  1179.             $post_meta .= '<meta itemprop="url" content="' . $thumbnail[0] . '" />';
  1180.             $post_meta .= '<meta itemprop="width" content="' . $thumbnail[1] . '" />';
  1181.             $post_meta .= '<meta itemprop="height" content="' . $thumbnail[2] . '" />';
  1182.             $post_meta .= '</div>';
  1183.         }
  1184.  
  1185.         // Publisher
  1186.         $site_logo = '';
  1187.         if ( function_exists( 'the_custom_logo' ) ) {
  1188.             $custom_logo_id = get_theme_mod( 'custom_logo' );
  1189.             if ( $custom_logo_id ) {
  1190.                 $custom_logo = wp_get_attachment_image_src( $custom_logo_id, 'full' );
  1191.                 $site_logo = $custom_logo[0];
  1192.             }
  1193.         }
  1194.         if( empty( $site_logo ) ) {
  1195.             if( has_header_image() ) {
  1196.                 $header_image = get_header_image();
  1197.                 if( ! empty( $header_image ) ) {
  1198.                     $site_logo = $header_image;
  1199.                 }
  1200.             }
  1201.         }
  1202.         $site_logo = apply_filters( 'wp_postratings_site_logo', $site_logo );
  1203.         $post_meta .= '<div style="display: none;" itemprop="publisher" itemscope itemtype="https://schema.org/Organization">';
  1204.         $post_meta .= '<meta itemprop="name" content="' . get_bloginfo( 'name' ) . '" />';
  1205.         $post_meta .= '<div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">';
  1206.         $post_meta .= '<meta itemprop="url" content="' . $site_logo . '" />';
  1207.         $post_meta .= '</div>';
  1208.         $post_meta .= '</div>';
  1209.  
  1210.         $ratings_meta = '';
  1211.         if( $post_ratings_average > 0 ) {
  1212.             $ratings_meta .= '<div style="display: none;" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">';
  1213.             $ratings_meta .= '<meta itemprop="bestRating" content="' . $ratings_max . '" />';
  1214.             $ratings_meta .= '<meta itemprop="worstRating" content="1" />';
  1215.             $ratings_meta .= '<meta itemprop="ratingValue" content="' . $post_ratings_average . '" />';
  1216.             $ratings_meta .= '<meta itemprop="ratingCount" content="' . $post_ratings_users . '" />';
  1217.             $ratings_meta .= '</div>';
  1218.         }
  1219.  
  1220.         $google_structured_data =  apply_filters( 'wp_postratings_google_structured_data', ( empty( $itemtype ) ? $ratings_meta : ( $post_meta . $ratings_meta ) ) );
  1221.     }
  1222.  
  1223.     return apply_filters( 'expand_ratings_template', ( $value . $google_structured_data ) );
  1224. }
Advertisement
Add Comment
Please, Sign In to add comment