diff --git a/README.md b/README.md index 1e8bbf9..17796ff 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,6 @@ Adds an AJAX rating system for your WordPress site's content. ### Development [https://github.com/lesterchan/wp-postratings](https://github.com/lesterchan/wp-postratings "https://github.com/lesterchan/wp-postratings") -### Translations -[http://dev.wp-plugins.org/browser/wp-postratings/i18n/](http://dev.wp-plugins.org/browser/wp-postratings/i18n/ "http://dev.wp-plugins.org/browser/wp-postratings/i18n/") - ### Credits * Plugin icon by [Freepik](http://www.freepik.com) from [Flaticon](http://www.flaticon.com) * Icons courtesy of [FamFamFam](http://www.famfamfam.com/ "FamFamFam") and [Everaldo](http://www.everaldo.com "Everaldo") diff --git a/includes/postratings-activation.php b/includes/postratings-activation.php index 0411b3b..e3f5527 100644 --- a/includes/postratings-activation.php +++ b/includes/postratings-activation.php @@ -37,20 +37,22 @@ function ratings_activate() { $charset_collate = $wpdb->get_charset_collate(); // Create Post Ratings Table - $create_sql = "CREATE TABLE $wpdb->ratings (". - "rating_id INT(11) NOT NULL auto_increment,". - "rating_postid INT(11) NOT NULL ,". - "rating_posttitle TEXT NOT NULL,". - "rating_rating INT(2) NOT NULL ,". - "rating_timestamp VARCHAR(15) NOT NULL ,". - "rating_ip VARCHAR(40) NOT NULL ,". - "rating_host VARCHAR(200) NOT NULL,". - "rating_username VARCHAR(50) NOT NULL,". - "rating_userid int(10) NOT NULL default '0',". - "PRIMARY KEY (rating_id),". - "KEY rating_userid (rating_userid),". - "KEY rating_postid_ip (rating_postid, rating_ip)) ". - "$charset_collate;"; + $create_sql = <<ratings} ( + rating_id INT(11) NOT NULL auto_increment, + rating_postid INT(11) NOT NULL, + rating_posttitle TEXT NOT NULL, + rating_rating INT(2) NOT NULL, + rating_timestamp VARCHAR(15) NOT NULL, + rating_ip VARCHAR(40) NOT NULL, + rating_host VARCHAR(200) NOT NULL, + rating_username VARCHAR(50) NOT NULL, + rating_userid int(10) NOT NULL default '0', + PRIMARY KEY (rating_id), + KEY rating_userid (rating_userid), + KEY rating_postid_ip (rating_postid, rating_ip)) + {$charset_collate}; +EOF; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $create_sql ); diff --git a/includes/postratings-bayesian-score.php b/includes/postratings-bayesian-score.php new file mode 100644 index 0000000..bfbcd42 --- /dev/null +++ b/includes/postratings-bayesian-score.php @@ -0,0 +1,83 @@ + $nk) { + $asum[] = $sk * ($nk+1); + } + return array_sum($asum) / ($N+$K); + } + } + + $fsns = f($s, $ns); + return $fsns - $z * sqrt( ( f($s2, $ns) - pow($fsns, 2)) / ($N+$K+1) ); +} + +function get_post_score_data($post_id) { + global $wpdb; + + $def = []; + $f = $wpdb->get_results( $wpdb->prepare( "SELECT rating_rating as r, count(1) as c FROM {$wpdb->ratings} WHERE rating_postid = %d GROUP BY rating_rating", $post_id )); + foreach($f as $data) $def[$data->r] = (int)$data->c; + $def += array_fill( 1, intval( get_option( 'postratings_max', 5 ) ), 0 ); + ksort( $def ); + + return $def; +} + +function get_bayesian_score($post_id, $confidence) { + return bayesian_score( get_post_score_data( $post_id ), $confidence ); +} + + +/* ToDo: how to produce a dynamically generated field (with no stored data at all?) + If it's not possible, we may consider (optionnally) storing the alternative average + inside the DB too */ +/* +function get_postrating_bayesian_score($metadata, $object_id, $meta_key, $single) { + if ($meta_key && $meta_key == 'bayesian_score') { + return get_bayesian_score($object_id, floatval(constant('NUM_VOTES_IMPACT_' . intval(get_option('bayesian_votes_impact', 90)))) ? : NUM_VOTES_IMPACT_90); + } +} +add_filter('get_post_metadata', 'get_postrating_bayesian_score', 10, 4); +*/ diff --git a/includes/postratings-captcha.php b/includes/postratings-captcha.php new file mode 100644 index 0000000..8bd603d --- /dev/null +++ b/includes/postratings-captcha.php @@ -0,0 +1,43 @@ +is_active() ) { + return false; + } + + return $recaptcha->get_sitekey(); +} + +function recaptcha_is_enabled() { + if (! ($opt = get_option('postratings_options')) ) { + return false; + } + if (! isset($opt['recaptcha']) || ! $opt['recaptcha']) { + return false; + } + + return true; +} + +function is_human() { + $recaptcha = WPCF7_RECAPTCHA::get_instance(); + $response_token = wpcf7_recaptcha_response(); + // return true for mutants and humans + return $recaptcha->verify( $response_token ); +} + + +add_action( 'wp_enqueue_scripts', 'google_recaptcha' ); +function google_recaptcha() { + if ( ! recaptcha_is_enabled() ) return; + if ( ! recaptcha_is_op() ) return; + wp_register_script( 'google-recaptcha', + add_query_arg( [ 'onload' => 'recaptchaCallback', 'render' => 'explicit' ], + 'https://www.google.com/recaptcha/api.js' ), + [], '2.0', true ); +} diff --git a/includes/postratings-comment.php b/includes/postratings-comment.php new file mode 100644 index 0000000..44589dc --- /dev/null +++ b/includes/postratings-comment.php @@ -0,0 +1,97 @@ +comment_ID, 'postratings_id', true); + if (intval($rate_id)) { + $rate = $wpdb->get_var( $wpdb->prepare( "SELECT rating_rating FROM {$wpdb->ratings} WHERE rating_id = %d", intval($rate_id)) ); + if ($rate) { + $comment->postrating_rate = $rate; + } + } + } + + return $comments; +} + +add_filter( 'manage_edit-comments_columns', 'comment_has_vote' ); +function comment_has_vote( $columns ) { + $columns['comment-vote'] = __( 'Vote', 'wp-postratings' ); + return $columns; +} + + +add_filter( 'manage_comments_custom_column', 'recent_comment_has_vote', 20, 2 ); +function recent_comment_has_vote( $column_name, $comment_id ) { + if( 'comment-vote' != strtolower( $column_name ) ) return; + if ( ( $rate_id = get_comment_meta( $comment_id, 'postratings_id', true ) ) ) { + if (intval($rate_id)) { + global $wpdb; + $rate = $wpdb->get_var( $wpdb->prepare( "SELECT rating_rating FROM {$wpdb->ratings} WHERE rating_id = %d", intval($rate_id)) ); + if ($rate) { + printf(__('Rated at %d', 'wp-postratings'), $rate); + } + } + } +} + + +// REST API specific +// to be called from the "update_callback" of register_rest_field() +function process_ratings_from_rest_API( WP_Comment $comment, $rate ) { + // see update_additional_fields_for_object() + if (! $comment->comment_post_ID || ! $rate) { + return new WP_Error( 'rest_comment_vote_invalid', 'Voted content not found.', array( 'status' => 500 ) ); + } + + $allow_to_vote_with_comment = (int)get_option('postratings_onlyifcomment'); + if (! $allow_to_vote_with_comment) { + return new WP_Error( 'rest_comment_vote_invalid', 'Vote bound to comment are not allowed.', array( 'status' => 400 ) ); + } + + $rate_id = 0; $last_error = ''; + process_ratings($comment->comment_post_ID, $rate, $rate_id, $last_error); + if ( $rate_id ) { + $updated = update_comment_meta( $comment->comment_ID, 'postratings_id', $rate_id ); + return $updated; + } + + if ( $last_error ) { + return new WP_Error( 'rest_comment_vote_invalid', $last_error, array( 'status' => 403 ) ); + } + return new WP_Error( 'rest_comment_vote_invalid', 'Unknown error.', array( 'status' => 500 ) ); +} diff --git a/includes/postratings-scripts.php b/includes/postratings-scripts.php index 04e7c35..97178b3 100644 --- a/includes/postratings-scripts.php +++ b/includes/postratings-scripts.php @@ -42,31 +42,23 @@ function ratings_scripts() { wp_enqueue_style( 'wp-postratings-rtl', plugins_url( 'wp-postratings/css/postratings-css-rtl.css' ), false, WP_POSTRATINGS_VERSION, 'all' ); } } - $postratings_max = intval( get_option( 'postratings_max' ) ); - $postratings_custom = intval( get_option( 'postratings_customrating' ) ); + + wp_enqueue_script('wp-postratings', plugins_url('wp-postratings/js/postratings-js.dev.js'), array('jquery'), WP_POSTRATINGS_VERSION); + + // these are static JS parameters $postratings_ajax_style = get_option( 'postratings_ajax_style' ); - $postratings_image = get_option( 'postratings_image' ); - $postratings_plugins_url = plugins_url( 'wp-postratings' ); - $postratings_javascript = ''; - if($postratings_custom) { - for($i = 1; $i <= $postratings_max; $i++) { - $postratings_javascript .= 'var ratings_' . $i . '_mouseover_image=new Image();ratings_' . $i . '_mouseover_image.src="' . $postratings_plugins_url . '/images/' . $postratings_image . '/rating_' . $i . '_over.' . RATINGS_IMG_EXT . '";'; - } - } else { - $postratings_javascript = 'var ratings_mouseover_image=new Image();ratings_mouseover_image.src="' . $postratings_plugins_url . '/images/' . $postratings_image . '/rating_over.' . RATINGS_IMG_EXT . '";'; - } - wp_enqueue_script('wp-postratings', plugins_url('wp-postratings/js/postratings-js.js'), array('jquery'), WP_POSTRATINGS_VERSION, true); wp_localize_script('wp-postratings', 'ratingsL10n', array( - 'plugin_url' => $postratings_plugins_url, + 'plugin_url' => plugins_url( 'wp-postratings' ), 'ajax_url' => admin_url('admin-ajax.php'), 'text_wait' => __('Please rate only 1 item at a time.', 'wp-postratings'), - 'image' => $postratings_image, + 'image' => get_option( 'postratings_image' ), 'image_ext' => RATINGS_IMG_EXT, - 'max' => $postratings_max, + 'max' => intval( get_option( 'postratings_max' ) ), 'show_loading' => intval($postratings_ajax_style['loading']), 'show_fading' => intval($postratings_ajax_style['fading']), - 'custom' => $postratings_custom, - 'l10n_print_after' => $postratings_javascript + 'custom' => boolval( get_option( 'postratings_customrating', false ) ), + 'captcha_sitekey' => recaptcha_is_enabled() ? recaptcha_is_op() : false, + 'rtl' => intval( is_rtl() ) )); } diff --git a/includes/postratings-tests.php b/includes/postratings-tests.php new file mode 100644 index 0000000..c49d72c --- /dev/null +++ b/includes/postratings-tests.php @@ -0,0 +1,44 @@ + 0 ? "\t " : ($d < 0 ? "\t" : '')), + $d != 0 ? sprintf("%.2f", $d) : ""); + } +} + +function get_post_rating_detail($stats) { + global $wpdb; + + $post_stats = []; + foreach($wpdb->get_results("SELECT distinct rating_postid as p FROM {$wpdb->ratings}") as $i) { + $post_stats[] = get_post_score_data($i->p); + } + + postratings_score_tests(NUM_VOTES_IMPACT_90, $post_stats); +} diff --git a/includes/validation.php b/includes/validation.php new file mode 100644 index 0000000..2951b25 --- /dev/null +++ b/includes/validation.php @@ -0,0 +1,135 @@ + 0; +} + +// Check Rated By Cookie +function wpr_check_rated_cookie( $err, $post_id ) { + $rated = isset($_COOKIE["rated_$post_id"]); + if ( $rated ) { + $err[] = sprintf(esc_html__('You already rated post #%d (cookie).', 'wp-postratings'), $post_id); + } + return $err; +} + + +// Check Rated By IP +function wpr_check_rated_ip( $err, $post_id ) { + global $wpdb; + // Check IP From IP Logging Database + if ($wpdb->get_var( $wpdb->prepare( "SELECT rating_ip FROM {$wpdb->ratings} WHERE rating_postid = %d AND rating_ip = %s", $post_id, get_ipaddress() ) ) ) { + $err[] = sprintf(esc_html__('You already rated post #%d (ip).', 'wp-postratings'), $post_id); + } + return $err; +} + + +// Check Rated By Username +function wpr_check_rated_username( $err, $post_id ) { + global $wpdb, $user_ID; + if( !is_user_logged_in() ) { + $err[] = sprintf(esc_html__('You already rated post #%d (anonymous userid).', 'wp-postratings'), $post_id); + return $err; + } + + // Check User ID From IP Logging Database + if ( $wpdb->get_var( $wpdb->prepare( "SELECT rating_userid FROM {$wpdb->ratings} WHERE rating_postid = %d AND rating_userid = %d", $post_id, $user_ID ) ) ) { + $err[] = sprintf(esc_html__('You already rated post #%d (userid).', 'wp-postratings'), $post_id); + } + return $err; +} diff --git a/js/postratings-js.dev.js b/js/postratings-js.dev.js index d4c5976..8c7743f 100644 --- a/js/postratings-js.dev.js +++ b/js/postratings-js.dev.js @@ -1,127 +1,221 @@ /* -+----------------------------------------------------------------+ -| | -| WordPress Plugin: WP-PostRatings | -| Copyright (c) 2012 Lester "GaMerZ" Chan | -| | -| File Written By: | -| - Lester "GaMerZ" Chan | -| - http://lesterchan.net | -| | -| File Information: | -| - Post Ratings Javascript File | -| - wp-content/plugins/wp-postratings/postratings-js.php | -| | -+----------------------------------------------------------------+ -*/ + +----------------------------------------------------------------+ + | | + | WordPress Plugin: WP-PostRatings | + | Copyright (c) 2012 Lester "GaMerZ" Chan | + | Copyright (c) 2017, 2018 Raphaël Droz | + | | + | File Information: | + | - Post Ratings Javascript File | + | - wp-content/plugins/wp-postratings/postratings-js.php | + | | + +----------------------------------------------------------------+ + */ // Variables -var post_id = 0; -var post_rating = 0; +var $j = jQuery.noConflict(); var is_being_rated = false; -ratingsL10n.custom = parseInt(ratingsL10n.custom); -ratingsL10n.max = parseInt(ratingsL10n.max); -ratingsL10n.show_loading = parseInt(ratingsL10n.show_loading); -ratingsL10n.show_fading = parseInt(ratingsL10n.show_fading); - -// When User Mouse Over Ratings -function current_rating(id, rating, rating_text) { - if(!is_being_rated) { - post_id = id; - post_rating = rating; - if(ratingsL10n.custom && ratingsL10n.max == 2) { - jQuery('#rating_' + post_id + '_' + rating).attr('src', eval('ratings_' + rating + '_mouseover_image.src')); - } else { - for(i = 1; i <= rating; i++) { - if(ratingsL10n.custom) { - jQuery('#rating_' + post_id + '_' + i).attr('src', eval('ratings_' + i + '_mouseover_image.src')); - } else { - jQuery('#rating_' + post_id + '_' + i).attr('src', ratings_mouseover_image.src); - } - } - } - if(jQuery('#ratings_' + post_id + '_text').length) { - jQuery('#ratings_' + post_id + '_text').show(); - jQuery('#ratings_' + post_id + '_text').html(rating_text); +var postratings_captcha = null; +var ratings_mouseover_image; + +jQuery(function() { + postratings_init_vars(); + postratings_setup_evt_listeners(); +}); + +function postratings_init_vars() { + ratingsL10n.custom = parseInt(ratingsL10n.custom); + ratingsL10n.max = parseInt(ratingsL10n.max); + ratingsL10n.show_loading = parseInt(ratingsL10n.show_loading); + ratingsL10n.show_fading = parseInt(ratingsL10n.show_fading); + ratingsL10n.baseimg = ratingsL10n.plugin_url + '/images/' + ratingsL10n.image + '/rating' ; + ratingsL10n.rtl = parseInt(ratingsL10n.rtl); + + if(ratingsL10n.custom) { + ratings_mouseover_image = []; + for(var i = 1; i <= ratingsL10n.max; i++) { + ratings_mouseover_image[i] = new Image(); + ratings_mouseover_image[i].src = ratingsL10n.baseimg + "_" + i + "_over." + ratingsL10n.image_ext ; } + } else { + ratings_mouseover_image = new Image(); + ratings_mouseover_image.src = ratingsL10n.baseimg + "_over." + ratingsL10n.image_ext ; + } +}; + +function postratings_setup_evt_listeners() { + jQuery('img[data-votes]').on('mouseover mouseout', current_rating).on('click keypress', rate_post); +}; + +// intermediary functions: wrap RTL complexity (invert on/off) +function getRtlI(i) { return (!ratingsL10n.rtl) ? i : (ratingsL10n.max - i + 1); } +function getOver(i) { return ratingsL10n.custom ? ratings_mouseover_image[ getRtlI(i) ].src : ratings_mouseover_image.src; } +function getOn(i) { return ratingsL10n.baseimg + (ratingsL10n.custom ? '_' + getRtlI(i) : '') + '_' + getRtlDir('on') + '.' + ratingsL10n.image_ext; } +function getOff(i) { return ratingsL10n.baseimg + (ratingsL10n.custom ? '_' + getRtlI(i) : '') + '_' + getRtlDir('off') + '.' + ratingsL10n.image_ext; } +function getHalf(i) { return ratingsL10n.baseimg + (ratingsL10n.custom ? '_' + getRtlI(i) : '') + '_' + getRtlDir('half') + '.' + ratingsL10n.image_ext; } + +function getRtlDir(name) { + if (!ratingsL10n.rtl) return name; + switch(name) { + case "on": return "off"; + case "off": return "on"; + case "half": return "half-rtl"; + default: return ""; } } +/* DOM function: let help knowing whether we are in Ajax mode or not + * Ajax mode: submit async on click + * Non-ajax mode: click stores in an hidden field (value can be changed with further click) and + and it's not wp-postraings responsibility to post the value */ +function is_using_ajax(post_id) { + return Boolean( $j('#post-ratings-' + post_id).data('ajax') ); +} -// When User Mouse Out Ratings -function ratings_off(rating_score, insert_half, half_rtl) { - if(!is_being_rated) { - for(i = 1; i <= ratingsL10n.max; i++) { - if(i <= rating_score) { - if(ratingsL10n.custom) { - jQuery('#rating_' + post_id + '_' + i).attr('src', ratingsL10n.plugin_url + '/images/' + ratingsL10n.image + '/rating_' + i + '_on.' + ratingsL10n.image_ext); - } else { - jQuery('#rating_' + post_id + '_' + i).attr('src', ratingsL10n.plugin_url + '/images/' + ratingsL10n.image + '/rating_on.' + ratingsL10n.image_ext); - } - } else if(i == insert_half) { - if(ratingsL10n.custom) { - jQuery('#rating_' + post_id + '_' + i).attr('src', ratingsL10n.plugin_url + '/images/' + ratingsL10n.image + '/rating_' + i + '_half' + (half_rtl ? '-rtl' : '') + '.' + ratingsL10n.image_ext); - } else { - jQuery('#rating_' + post_id + '_' + i).attr('src', ratingsL10n.plugin_url + '/images/' + ratingsL10n.image + '/rating_half' + (half_rtl ? '-rtl' : '') + '.' + ratingsL10n.image_ext); - } - } else { - if(ratingsL10n.custom) { - jQuery('#rating_' + post_id + '_' + i).attr('src', ratingsL10n.plugin_url + '/images/' + ratingsL10n.image + '/rating_' + i + '_off.' + ratingsL10n.image_ext); - } else { - jQuery('#rating_' + post_id + '_' + i).attr('src', ratingsL10n.plugin_url + '/images/' + ratingsL10n.image + '/rating_off.' + ratingsL10n.image_ext); - } - } - } - if(jQuery('#ratings_' + post_id + '_text').length) { - jQuery('#ratings_' + post_id + '_text').hide(); - jQuery('#ratings_' + post_id + '_text').empty(); - } +function non_ajax_hidden_parent(post_id) { + var parent = $j('input[name="wp_postrating_form_value_' + post_id + '"]'); + if (parent.length == 1) return parent; + return false; +} + +// mouseover/out handler +function current_rating(event) { + var post_ratings_el = $j(event.target); + var post_id = $j(event.target).data('id'); + var current_rating = $j(event.target).data('currentRating'); + var rating_score = $j(event.target).data('votes'); + var insert_half = $j(event.target).data('half'); + + if (is_being_rated) return; + + var curval = NaN; // possible stored value: disabled + if (! is_using_ajax(post_id)) { + var $parent = non_ajax_hidden_parent(post_id); + curval = parseInt($parent.val()); } + + /* This could be: + 1) first set all OFF + 2) then set to ON those that need + 3) halve it if necessary + 4) then highlight if it's a non-mouseover selection (non-ajax mode) + (currently stored value)... but prioritize mouseover => Math.min(mouseover,selected value) + 5) then highlight up to the score of the current star (if mouseover) + + Make all these passes filling up the array of images URL */ + + var next_images = []; + function setOn(i) { next_images[i] = getOn(i); } + function setOff(i) { next_images[i] = getOff(i); } + function setHalf(i) { next_images[i] = getHalf(i); } + function setOver(i) { next_images[i] = getOver(i); } + var max = ratingsL10n.max; + + var i; + // 1) off them all + for(i = 1; i <= max; i++) setOff(i); + // 2) on up to current score (always applies except for unvoted items) + for(i = 1; i <= current_rating; i++) setOn(i); + // 3) set the half-star (if it applies) + if (insert_half > rating_score) setHalf(insert_half); + // 4) on up to currently voted score (if non-ajax, non-default mode) + if (! isNaN(curval)) { + // ToDo: find another color + if (event.type == "mouseover") for(i = 1; i <= Math.min(curval, rating_score); i++) setOver(i); + else for(i = 1; i <= curval; i++) setOver(i); + } + // 5) mouseover + if (event.type == "mouseover") for(i = 1; i <= rating_score; i++) setOver(i); + + // Now apply all these images. + // NB: reversing the array, may be an even simpler way to do RTL + for(i = 1; i <= max; i++) document.querySelector('img#rating_' + post_id + '_' + i).setAttribute('src', next_images[i]); + + updateText($j('#ratings_' + post_id + '_text'), post_ratings_el, event.type == "mouseout"); } -// Set is_being_rated Status -function set_is_being_rated(rated_status) { - is_being_rated = rated_status; +function updateText($text_el, $element, mouseout) { + if ($text_el.length) { + if (mouseout) $text_el.hide().empty(); + else $text_el.html($element.data('ratingsText')).show(); + } } + // Process Post Ratings Success -function rate_post_success(data) { - jQuery('#post-ratings-' + post_id).html(data); +function rate_post_success(post_id, data) { + $j('#post-ratings-' + post_id).html(data); if(ratingsL10n.show_loading) { - jQuery('#post-ratings-' + post_id + '-loading').hide(); + $j('#post-ratings-' + post_id + '-loading').hide(); } if(ratingsL10n.show_fading) { - jQuery('#post-ratings-' + post_id).fadeTo('def', 1, function () { - set_is_being_rated(false); - }); - } else { - set_is_being_rated(false); + $j('#post-ratings-' + post_id).fadeTo('def', 1); } } // Process Post Ratings -function rate_post() { - post_ratings_el = jQuery('#post-ratings-' + post_id); - if(!is_being_rated) { - post_ratings_nonce = jQuery(post_ratings_el).data('nonce'); - if(typeof post_ratings_nonce == 'undefined' || post_ratings_nonce == null) - post_ratings_nonce = jQuery(post_ratings_el).attr('data-nonce'); - set_is_being_rated(true); +function rate_post(event) { + var post_ratings_el = $j(event.target); + var post_id = $j(event.target).data('id'); + var post_rating = $j(event.target).data('votes'); + + var captcha_response = ''; + + if (! is_using_ajax(post_id)) { + var value_holder = non_ajax_hidden_parent(post_id); + var curval = $j(value_holder).val(); + $j(value_holder).val(null); + $j('#rating_' + post_id + '_' + curval).trigger('mouseout'); + if (curval != post_rating) { + $j('#rating_' + post_id + '_' + post_rating).trigger('mouseover'); + $j(value_holder).val(post_rating); + } + return; + } + + if(ratingsL10n.captcha_sitekey && ratingsL10n.captcha_sitekey.length) { + if (postratings_captcha === null) { + postratings_captcha = grecaptcha.render("g-recaptcha-response", {"sitekey":ratingsL10n.captcha_sitekey}); + return; + } else { + captcha_response = grecaptcha.getResponse(postratings_captcha); + if (! grecaptcha.getResponse(postratings_captcha)) { + // grecaptcha.reset(postratings_captcha); + return; + } + else { + // ok, let's submit + $j('#g-recaptcha-response').remove(); + } + } + } + + if(! is_being_rated) { + var post_ratings_nonce = $j(post_ratings_el).parent('.post-ratings').data('nonce'); + is_being_rated = true; if(ratingsL10n.show_fading) { - jQuery(post_ratings_el).fadeTo('def', 0, function () { + $j(post_ratings_el).fadeTo('def', 0, function () { if(ratingsL10n.show_loading) { - jQuery('#post-ratings-' + post_id + '-loading').show(); + $j('#post-ratings-' + post_id + '-loading').show(); } - jQuery.ajax({type: 'POST', xhrFields: {withCredentials: true}, dataType: 'html', url: ratingsL10n.ajax_url, data: 'action=postratings&pid=' + post_id + '&rate=' + post_rating + '&postratings_' + post_id + '_nonce=' + post_ratings_nonce, cache: false, success: rate_post_success}); }); } else { if(ratingsL10n.show_loading) { - jQuery('#post-ratings-' + post_id + '-loading').show(); + $j('#post-ratings-' + post_id + '-loading').show(); } - jQuery.ajax({type: 'POST', xhrFields: {withCredentials: true}, dataType: 'html', url: ratingsL10n.ajax_url, data: 'action=postratings&pid=' + post_id + '&rate=' + post_rating + '&postratings_' + post_id + '_nonce=' + post_ratings_nonce, cache: false, success: rate_post_success}); } - } else { + + $j.post({xhrFields: {withCredentials: true}, + dataType: 'html', + url: ratingsL10n.ajax_url, + data: 'action=postratings&pid=' + post_id + '&rate=' + post_rating + '&postratings_' + post_id + '_nonce=' + post_ratings_nonce + '&g-recaptcha-response=' + captcha_response, + cache: false}) + .done(function(data) { rate_post_success(post_id, data); }) + .always(function() { is_being_rated = false; }); + } + + else { alert(ratingsL10n.text_wait); } -} \ No newline at end of file +} diff --git a/js/postratings-js.js b/js/postratings-js.js index ceb23ef..9cb5695 100644 --- a/js/postratings-js.js +++ b/js/postratings-js.js @@ -1,9 +1 @@ -var post_id=0,post_rating=0,is_being_rated=!1;ratingsL10n.custom=parseInt(ratingsL10n.custom);ratingsL10n.max=parseInt(ratingsL10n.max);ratingsL10n.show_loading=parseInt(ratingsL10n.show_loading);ratingsL10n.show_fading=parseInt(ratingsL10n.show_fading); -function current_rating(a,b,c){if(!is_being_rated){post_id=a;post_rating=b;if(ratingsL10n.custom&&2==ratingsL10n.max)jQuery("#rating_"+post_id+"_"+b).attr("src",eval("ratings_"+b+"_mouseover_image.src"));else for(i=1;i<=b;i++)ratingsL10n.custom?jQuery("#rating_"+post_id+"_"+i).attr("src",eval("ratings_"+i+"_mouseover_image.src")):jQuery("#rating_"+post_id+"_"+i).attr("src",ratings_mouseover_image.src);jQuery("#ratings_"+post_id+"_text").length&&(jQuery("#ratings_"+post_id+"_text").show(),jQuery("#ratings_"+ - post_id+"_text").html(c))}} -function ratings_off(a,b,c){if(!is_being_rated){for(i=1;i<=ratingsL10n.max;i++)i<=a?ratingsL10n.custom?jQuery("#rating_"+post_id+"_"+i).attr("src",ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_"+i+"_on."+ratingsL10n.image_ext):jQuery("#rating_"+post_id+"_"+i).attr("src",ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_on."+ratingsL10n.image_ext):i==b?ratingsL10n.custom?jQuery("#rating_"+post_id+"_"+i).attr("src",ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_"+ - i+"_half"+(c?"-rtl":"")+"."+ratingsL10n.image_ext):jQuery("#rating_"+post_id+"_"+i).attr("src",ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_half"+(c?"-rtl":"")+"."+ratingsL10n.image_ext):ratingsL10n.custom?jQuery("#rating_"+post_id+"_"+i).attr("src",ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_"+i+"_off."+ratingsL10n.image_ext):jQuery("#rating_"+post_id+"_"+i).attr("src",ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating_off."+ratingsL10n.image_ext);jQuery("#ratings_"+ - post_id+"_text").length&&(jQuery("#ratings_"+post_id+"_text").hide(),jQuery("#ratings_"+post_id+"_text").empty())}}function set_is_being_rated(a){is_being_rated=a}function rate_post_success(a){jQuery("#post-ratings-"+post_id).html(a);ratingsL10n.show_loading&&jQuery("#post-ratings-"+post_id+"-loading").hide();ratingsL10n.show_fading?jQuery("#post-ratings-"+post_id).fadeTo("def",1,function(){set_is_being_rated(!1)}):set_is_being_rated(!1)} -function rate_post(){post_ratings_el=jQuery("#post-ratings-"+post_id);if(is_being_rated)alert(ratingsL10n.text_wait);else{post_ratings_nonce=jQuery(post_ratings_el).data("nonce");if("undefined"==typeof post_ratings_nonce||null==post_ratings_nonce)post_ratings_nonce=jQuery(post_ratings_el).attr("data-nonce");set_is_being_rated(!0);ratingsL10n.show_fading?jQuery(post_ratings_el).fadeTo("def",0,function(){ratingsL10n.show_loading&&jQuery("#post-ratings-"+post_id+"-loading").show();jQuery.ajax({type:"POST", - xhrFields:{withCredentials:!0},dataType:"html",url:ratingsL10n.ajax_url,data:"action=postratings&pid="+post_id+"&rate="+post_rating+"&postratings_"+post_id+"_nonce="+post_ratings_nonce,cache:!1,success:rate_post_success})}):(ratingsL10n.show_loading&&jQuery("#post-ratings-"+post_id+"-loading").show(),jQuery.ajax({type:"POST",xhrFields:{withCredentials:!0},dataType:"html",url:ratingsL10n.ajax_url,data:"action=postratings&pid="+post_id+"&rate="+post_rating+"&postratings_"+post_id+"_nonce="+post_ratings_nonce, - cache:!1,success:rate_post_success}))}}; \ No newline at end of file +var ratings_mouseover_image,$j=jQuery.noConflict(),is_being_rated=!1,postratings_captcha=null;function postratings_init_vars(){if(ratingsL10n.custom=parseInt(ratingsL10n.custom),ratingsL10n.max=parseInt(ratingsL10n.max),ratingsL10n.show_loading=parseInt(ratingsL10n.show_loading),ratingsL10n.show_fading=parseInt(ratingsL10n.show_fading),ratingsL10n.baseimg=ratingsL10n.plugin_url+"/images/"+ratingsL10n.image+"/rating",ratingsL10n.rtl=parseInt(ratingsL10n.rtl),ratingsL10n.custom){ratings_mouseover_image=[];for(var t=1;t<=ratingsL10n.max;t++)ratings_mouseover_image[t]=new Image,ratings_mouseover_image[t].src=ratingsL10n.baseimg+"_"+t+"_over."+ratingsL10n.image_ext}else(ratings_mouseover_image=new Image).src=ratingsL10n.baseimg+"_over."+ratingsL10n.image_ext}function postratings_setup_evt_listeners(){jQuery("img[data-votes]").on("mouseover mouseout",current_rating).on("click keypress",rate_post)}function getRtlI(t){return ratingsL10n.rtl?ratingsL10n.max-t+1:t}function getOver(t){return ratingsL10n.custom?ratings_mouseover_image[getRtlI(t)].src:ratings_mouseover_image.src}function getOn(t){return ratingsL10n.baseimg+(ratingsL10n.custom?"_"+getRtlI(t):"")+"_"+getRtlDir("on")+"."+ratingsL10n.image_ext}function getOff(t){return ratingsL10n.baseimg+(ratingsL10n.custom?"_"+getRtlI(t):"")+"_"+getRtlDir("off")+"."+ratingsL10n.image_ext}function getHalf(t){return ratingsL10n.baseimg+(ratingsL10n.custom?"_"+getRtlI(t):"")+"_"+getRtlDir("half")+"."+ratingsL10n.image_ext}function getRtlDir(t){if(!ratingsL10n.rtl)return t;switch(t){case"on":return"off";case"off":return"on";case"half":return"half-rtl";default:return""}}function is_using_ajax(t){return Boolean($j("#post-ratings-"+t).data("ajax"))}function non_ajax_hidden_parent(t){var n=$j('input[name="wp_postrating_form_value_'+t+'"]');return 1==n.length&&n}function current_rating(t){var n=$j(t.target),a=$j(t.target).data("id"),e=$j(t.target).data("currentRating"),r=$j(t.target).data("votes"),i=$j(t.target).data("half");if(!is_being_rated){var s=NaN;if(!is_using_ajax(a)){var g=non_ajax_hidden_parent(a);s=parseInt(g.val())}var o,_=[],u=ratingsL10n.max;for(o=1;o<=u;o++)f(o);for(o=1;o<=e;o++)c(o);if(i>r&&function(t){_[t]=getHalf(t)}(i),!isNaN(s))if("mouseover"==t.type)for(o=1;o<=Math.min(s,r);o++)p(o);else for(o=1;o<=s;o++)p(o);if("mouseover"==t.type)for(o=1;o<=r;o++)p(o);for(o=1;o<=u;o++)document.querySelector("img#rating_"+a+"_"+o).setAttribute("src",_[o]);updateText($j("#ratings_"+a+"_text"),n,"mouseout"==t.type)}function c(t){_[t]=getOn(t)}function f(t){_[t]=getOff(t)}function p(t){_[t]=getOver(t)}}function updateText(t,n,a){t.length&&(a?t.hide().empty():t.html(n.data("ratingsText")).show())}function rate_post_success(t,n){$j("#post-ratings-"+t).html(n),ratingsL10n.show_loading&&$j("#post-ratings-"+t+"-loading").hide(),ratingsL10n.show_fading&&$j("#post-ratings-"+t).fadeTo("def",1)}function rate_post(t){var n=$j(t.target),a=$j(t.target).data("id"),e=$j(t.target).data("votes"),r="";if(!is_using_ajax(a)){var i=non_ajax_hidden_parent(a),s=$j(i).val();return $j(i).val(null),$j("#rating_"+a+"_"+s).trigger("mouseout"),void(s!=e&&($j("#rating_"+a+"_"+e).trigger("mouseover"),$j(i).val(e)))}if(ratingsL10n.captcha_sitekey&&ratingsL10n.captcha_sitekey.length){if(null===postratings_captcha)return void(postratings_captcha=grecaptcha.render("g-recaptcha-response",{sitekey:ratingsL10n.captcha_sitekey}));if(r=grecaptcha.getResponse(postratings_captcha),!grecaptcha.getResponse(postratings_captcha))return;$j("#g-recaptcha-response").remove()}if(is_being_rated)alert(ratingsL10n.text_wait);else{var g=$j(n).parent(".post-ratings").data("nonce");is_being_rated=!0,ratingsL10n.show_fading?$j(n).fadeTo("def",0,function(){ratingsL10n.show_loading&&$j("#post-ratings-"+a+"-loading").show()}):ratingsL10n.show_loading&&$j("#post-ratings-"+a+"-loading").show(),$j.post({xhrFields:{withCredentials:!0},dataType:"html",url:ratingsL10n.ajax_url,data:"action=postratings&pid="+a+"&rate="+e+"&postratings_"+a+"_nonce="+g+"&g-recaptcha-response="+r,cache:!1}).done(function(t){rate_post_success(a,t)}).always(function(){is_being_rated=!1})}}jQuery(function(){postratings_init_vars(),postratings_setup_evt_listeners()}); diff --git a/js/postratings-render-js.dev.js b/js/postratings-render-js.dev.js index e6555b9..e9e6db1 100644 --- a/js/postratings-render-js.dev.js +++ b/js/postratings-render-js.dev.js @@ -28,34 +28,16 @@ function expand_ratings_template(rating, max_rate, images_dir) { var img_dir = images_dir || wp_postratings.images_dir; var img; - var ratings_images = ''; var vote_text = rating.usr > 1 ? 'votes' : 'vote'; var alt_text = rating.usr + ' ' + vote_text + ', average: ' + rating.avg; - for (var i=1; i<= max_rate; i++) { - if (i <= Math.round(rating.avg, 1)) { - img = "on"; - } else if(i == _get_voting_half_star(rating.avg)) { - img = "half"; - } else { - img = "off"; - } - ratings_images += '' + alt_text + ''; - } + var tpl = `${alt_text}\n`; + // templating + var ratings_images = '', i = 1; + var has_half = Math.abs(rating.avg - Math.floor(rating.avg)).toFixed(3) >= 0.25; + for (i=1; i<= Math.floor(rating.avg); i++) ratings_images += tpl.replace(/%s/g,"on"); + if (i < max_rate) ratings_images += tpl.replace(/%s/g, has_half ? 'half' : 'off'); + for (++i; i <= max_rate; i++) ratings_images += tpl.replace(/%s/g,"off"); + // return return ratings_images + ' (' + rating.usr + ' ' + vote_text + ')'; } - -// helper for expand_ratings_template() -function _get_voting_half_star(avg) { - var post_ratings = Math.round(avg, 1); - var post_ratings_average = Math.abs(Math.floor(avg)); - var average_diff = post_ratings_average - post_ratings; - var insert_half = 0; - if (average_diff >= 0.25 && average_diff <= 0.75) { - insert_half = Math.ceil(post_ratings_average); - } - else if (average_diff > 0.75) { - insert_half = Math.ceil(post_ratings); - } - return insert_half; -} diff --git a/postratings-options.php b/postratings-options.php index 07db9c4..ed1e3db 100644 --- a/postratings-options.php +++ b/postratings-options.php @@ -43,6 +43,7 @@ $postratings_image = sanitize_text_field( trim( $_POST['postratings_image'] ) ); $postratings_max = intval($_POST['postratings_max']); $postratings_richsnippet = intval($_POST['postratings_richsnippet']); + $postratings_recaptcha = is_plugin_active('contact-form-7/wp-contact-form-7.php') && recaptcha_is_op() ? boolval($_POST['postratings_recaptcha']) : false; $postratings_ratingstext_array = $_POST['postratings_ratingstext']; $postratings_ratingstext = array(); if( ! empty( $postratings_ratingstext_array ) && is_array( $postratings_ratingstext_array ) ) { @@ -60,10 +61,11 @@ $postratings_ajax_style = array('loading' => intval($_POST['postratings_ajax_style_loading']), 'fading' => intval($_POST['postratings_ajax_style_fading'])); $postratings_logging_method = intval($_POST['postratings_logging_method']); + $postratings_onlyifcomment = intval($_POST['postratings_onlyifcomment']); $postratings_allowtorate = intval($_POST['postratings_allowtorate']); $update_ratings_queries = array(); $update_ratings_text = array(); - $postratings_options = array('richsnippet' => $postratings_richsnippet); + $postratings_options = array('richsnippet' => $postratings_richsnippet, 'recaptcha' => $postratings_recaptcha); $update_ratings_queries[] = update_option('postratings_customrating', $postratings_customrating); $update_ratings_queries[] = update_option('postratings_template_vote', $postratings_template_vote); $update_ratings_queries[] = update_option('postratings_template_text', $postratings_template_text); @@ -77,6 +79,7 @@ $update_ratings_queries[] = update_option('postratings_ratingsvalue', $postratings_ratingsvalue); $update_ratings_queries[] = update_option('postratings_ajax_style', $postratings_ajax_style); $update_ratings_queries[] = update_option('postratings_logging_method', $postratings_logging_method); + $update_ratings_queries[] = update_option('postratings_onlyifcomment', $postratings_onlyifcomment); $update_ratings_queries[] = update_option('postratings_allowtorate', $postratings_allowtorate); $update_ratings_queries[] = update_option('postratings_options', $postratings_options); $update_ratings_text[] = __('Custom Rating', 'wp-postratings'); @@ -304,6 +307,32 @@ function set_custom(custom, max) { />  + + + + + false); // default value if it has never been saved before +?> + + />  +    + + + />  + Configure Google's Recaptcha sitekeys inside 'wpcf7-integration','service'=>'recaptcha','action'=>'setup'], menu_page_url('wpcf7', false))) . "\">contact-form-7 in order to enable wp-postratings recaptcha

"; + } + else if(! $recaptcha_possible) { + echo "

Install Contact Form 7 in order to use Google's Recaptcha with wp-postratings

"; + } + ?> + + +
@@ -384,6 +413,22 @@ function set_custom(custom, max) { + +

+ + + + + + + +
+ +
+

@@ -420,4 +465,4 @@ function set_custom(custom, max) {

- \ No newline at end of file + diff --git a/uninstall.php b/uninstall.php index 88d30fc..2b3cbca 100644 --- a/uninstall.php +++ b/uninstall.php @@ -12,6 +12,7 @@ , 'postratings_template_text' , 'postratings_template_none' , 'postratings_logging_method' + , 'postratings_onlyifcomment' , 'postratings_allowtorate' , 'postratings_ratingstext' , 'postratings_template_highestrated' diff --git a/wp-postratings.php b/wp-postratings.php index 0e8375c..35c2ce7 100644 --- a/wp-postratings.php +++ b/wp-postratings.php @@ -56,10 +56,14 @@ require_once( 'includes/postratings-activation.php' ); require_once( 'includes/postratings-admin.php' ); require_once( 'includes/postratings-i18n.php' ); +require_once( 'includes/postratings-captcha.php' ); require_once( 'includes/postratings-scripts.php' ); require_once( 'includes/postratings-shortcodes.php' ); require_once( 'includes/postratings-stats.php' ); require_once( 'includes/postratings-widgets.php' ); +require_once( 'includes/postratings-comment.php' ); +require_once( 'includes/postratings-bayesian-score.php' ); +require_once( 'includes/validation.php' ); /** * Register plugin activation hook @@ -72,10 +76,17 @@ function postratings_init() { if( ! defined( 'RATINGS_IMG_EXT' ) ) { define( 'RATINGS_IMG_EXT', apply_filters( 'wp_postratings_image_extension', 'gif' ) ); } + wpr_setup_validation(); } ### Function: Display The Rating For The Post -function the_ratings($start_tag = 'div', $custom_id = 0, $display = true) { +function the_ratings($start_tag = 'div', $custom_id = 0, $display = true /* obsolete */, $ajax = true) { + if ($display) echo get_the_ratings($start_tag, $custom_id, $ajax); + else return get_the_ratings($start_tag, $custom_id, $ajax); +} + +### Function: Get The Rating For The Post +function get_the_ratings($start_tag = 'div', $custom_id = 0, $ajax) { global $id; // Allow Custom ID if(intval($custom_id) > 0) { @@ -95,45 +106,62 @@ function the_ratings($start_tag = 'div', $custom_id = 0, $display = true) { $ratings_id = (int) $ratings_id; // Loading Style - $postratings_ajax_style = get_option('postratings_ajax_style'); - if(intval($postratings_ajax_style['loading']) == 1) { - $loading = '<' . $start_tag . ' id="post-ratings-' . $ratings_id . '-loading" class="post-ratings-loading"> - ' . esc_html__( 'Loading...', 'wp-postratings' ) . ''; - } else { - $loading = ''; + $loading = ''; + if(intval(get_option('postratings_ajax_style')) == 1) { + $loading = sprintf(<<<'EOF' +<%1$s id="post-ratings-%2$d-loading" class="post-ratings-loading"> + + %4$s + +EOF + , + $start_tag, + $ratings_id, + plugins_url('wp-postratings/images/loading.gif'), + esc_html__( 'Loading...', 'wp-postratings' ) + ); } - // Check To See Whether User Has Voted - $user_voted = check_rated($ratings_id); + // HTML Attributes - $ratings_options = get_option('postratings_options'); - $ratings_options['richsnippet'] = isset( $ratings_options['richsnippet'] ) ? $ratings_options['richsnippet'] : 1; - if( is_singular() && $ratings_options['richsnippet'] ) { + $richsnippet = get_option('postratings_options', array('richsnippet' => 1)); + $itemtype = ''; + if( is_singular() && $richsnippet ) { $itemtype = apply_filters('wp_postratings_schema_itemtype', 'itemscope itemtype="http://schema.org/Article"'); - $attributes = 'id="post-ratings-'.$ratings_id.'" class="post-ratings" '.$itemtype; - } else { - $attributes = 'id="post-ratings-'.$ratings_id.'" class="post-ratings"'; } + // If User Voted Or Is Not Allowed To Rate - if($user_voted) { - if(!$display) { - return "<$start_tag $attributes>".the_ratings_results($ratings_id).''.$loading; - } else { - echo "<$start_tag $attributes>".the_ratings_results($ratings_id).''.$loading; - } + $template = '<%1$s id="post-ratings-%2$d" class="post-ratings" %3$s> %4$s %5$s'; + + // Check To See Whether User Has Voted + if ( wpr_has_already_rated( $ratings_id ) ) { + return sprintf($template, $start_tag, $ratings_id, $itemtype, the_ratings_results($ratings_id), $loading); // If User Is Not Allowed To Rate - } else if(!check_allowtorate()) { - if(!$display) { - return "<$start_tag $attributes>".the_ratings_results($ratings_id, 0, 0, 0, 1).''.$loading; - } else { - echo "<$start_tag $attributes>".the_ratings_results($ratings_id, 0, 0, 0, 1).''.$loading; - } + } else if( !wpr_is_allowed_to_rate() ) { + return sprintf($template, $start_tag, $ratings_id, $itemtype, the_ratings_results($ratings_id, 0, 0, 0, 1), $loading); // If User Has Not Voted } else { - if(!$display) { - return "<$start_tag $attributes data-nonce=\"".wp_create_nonce('postratings_'.$ratings_id.'-nonce')."\">".the_ratings_vote($ratings_id).''.$loading; - } else { - echo "<$start_tag $attributes data-nonce=\"".wp_create_nonce('postratings_'.$ratings_id.'-nonce')."\">".the_ratings_vote($ratings_id).''.$loading; - } + /* ATM, the presence of this input#[name="wp_postrating_form_value_' + ratings_id] is the only way + we check whether the value must be submit immediatly through Ajax or not. + In the later case, serves as a value holder of the selected value. + See non_ajax_hidden_parent() and is_using_ajax() inside postratings-js.dev.js */ + + return sprintf(<<<'EOF' +
+<%1$s id="post-ratings-%2$d" class="post-ratings" %3$s data-nonce="%4$s" data-ajax="%5$d"> + + %6$s + +%7$s +EOF + , + $start_tag, + $ratings_id, + $itemtype, // $3 + wp_create_nonce('postratings_'.$ratings_id.'-nonce'), + $ajax, // $5: here if we want to avoid looking to parent's sibling (from PoV, cf JS) + the_ratings_vote($ratings_id, array('ajax' => $ajax)), + $loading // $7 + ); } } @@ -160,7 +188,18 @@ function the_ratings_results( $post_id, $new_user = 0, $new_score = 0, $new_aver ### Function: Display Ratings Vote -function the_ratings_vote($post_id, $new_user = 0, $new_score = 0, $new_average = 0) { +/** + * @parameter options: an array of templating/rendering option. Known options are: + * - new_user: default 0 + * - new_score: default 0 + * - new_average: default 0 + * - ajax: default: true, whether or not vote should be submited directly (albeigh async) via Ajax or not + * (futur use, not implemented yet) + */ +function the_ratings_vote($post_id, $options) { + $options += array('new_user' => 0, 'new_score' => 0, 'new_average' => 0, 'ajax' => true); + extract($options); // import elements as variable in function scope + if($new_user == 0 && $new_score == 0 && $new_average == 0) { $post_ratings_data = null; } else { @@ -183,101 +222,6 @@ function the_ratings_vote($post_id, $new_user = 0, $new_score = 0, $new_average } -### Function: Check Who Is Allow To Rate -function check_allowtorate() { - $allow_to_vote = intval(get_option('postratings_allowtorate')); - switch($allow_to_vote) { - // Guests Only - case 0: - return ! is_user_logged_in(); - break; - // Logged-in users only - case 1: - return is_user_logged_in(); - break; - // Users registered on blog (for multisite) - case 3: - return is_user_member_of_blog(); - break; - // Registered Users And Guests - case 2: - default: - return true; - } -} - - -### Function: Check Whether User Have Rated For The Post -function check_rated( $post_id ) { - $postratings_logging_method = intval( get_option( 'postratings_logging_method' ) ); - $rated = false; - switch( $postratings_logging_method ) { - // Do Not Log - case 0: - $rated = false; - break; - // Logged By Cookie - case 1: - $rated = check_rated_cookie( $post_id ); - break; - // Logged By IP - case 2: - $rated = check_rated_ip( $post_id ); - break; - // Logged By Cookie And IP - case 3: - $rated_cookie = check_rated_cookie( $post_id ); - if( $rated_cookie > 0 ) { - $rated = true; - } else { - $rated = check_rated_ip( $post_id ); - } - break; - // Logged By Username - case 4: - $rated = check_rated_username( $post_id ); - break; - } - - $rated = apply_filters( 'wp_postratings_check_rated', $rated, $post_id ); - - return $rated; -} - - -### Function: Check Rated By Cookie -function check_rated_cookie($post_id) { - if(isset($_COOKIE["rated_$post_id"])) { - return true; - } else { - return false; - } -} - - -### Function: Check Rated By IP -function check_rated_ip($post_id) { - global $wpdb; - // Check IP From IP Logging Database - $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() ) ); - // 0: False | > 0: True - return intval($get_rated); -} - - -### Function: Check Rated By Username -function check_rated_username($post_id) { - global $wpdb, $user_ID; - if(!is_user_logged_in()) { - return 0; - } - // Check User ID From IP Logging Database - $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 ) ); - // 0: False | > 0: True - return intval( $get_rated); -} - - ### Function: Get Comment Authors Ratings add_action('loop_start', 'get_comment_authors_ratings'); function get_comment_authors_ratings() { @@ -391,7 +335,7 @@ function get_ipaddress() { $ip_address = explode(',', $ip_address); $ip_address = $ip_address[0]; } - return esc_attr($ip_address); + return $ip_address; } } @@ -501,94 +445,131 @@ function delete_ratings_fields($post_ID) { } -### Function: Process Ratings -add_action('wp_ajax_postratings', 'process_ratings'); -add_action('wp_ajax_nopriv_postratings', 'process_ratings'); -function process_ratings() { - global $wpdb, $user_identity, $user_ID; +// Check For Bot +function is_bot($useragent) { + $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'); + foreach ($bots_useragent as $bot) { + if (stristr($useragent, $bot) !== false) { + return true; + } + } + return false; +} - if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'postratings') - { + +### Function: Process Ratings +add_action('wp_ajax_postratings', 'process_ratings_from_ajax'); +add_action('wp_ajax_nopriv_postratings', 'process_ratings_from_ajax'); +function process_ratings_from_ajax() { + if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'postratings') { $rate = intval($_REQUEST['rate']); $post_id = intval($_REQUEST['pid']); // Verify Referer - if(!check_ajax_referer('postratings_'.$post_id.'-nonce', 'postratings_'.$post_id.'_nonce', false)) - { + if(!check_ajax_referer('postratings_'.$post_id.'-nonce', 'postratings_'.$post_id.'_nonce', false)) { esc_html_e('Failed To Verify Referrer', 'wp-postratings'); exit(); } - if($rate > 0 && $post_id > 0 && check_allowtorate()) { - // Check For Bot - $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'); - $useragent = $_SERVER['HTTP_USER_AGENT']; - foreach ($bots_useragent as $bot) { - if (stristr($useragent, $bot) !== false) { - return; - } - } - header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); - $rated = check_rated($post_id); - // Check Whether Post Has Been Rated By User - if(!$rated) { - // Check Whether Is There A Valid Post - $post = get_post($post_id); - // If Valid Post Then We Rate It - if($post && !wp_is_post_revision($post)) { - $ratings_max = intval(get_option('postratings_max')); - $ratings_custom = intval(get_option('postratings_customrating')); - $ratings_value = get_option('postratings_ratingsvalue'); - $post_title = addslashes($post->post_title); - $post_ratings = get_post_custom($post_id); - $post_ratings_users = ! empty( $post_ratings['ratings_users'] ) ? intval($post_ratings['ratings_users'][0]) : 0; - $post_ratings_score = ! empty( $post_ratings['ratings_score'] ) ? intval($post_ratings['ratings_score'][0]) : 0; - // Check For Ratings Lesser Than 1 And Greater Than $ratings_max - if($rate < 1 || $rate > $ratings_max) { - $rate = 0; - } - $post_ratings_users = ($post_ratings_users+1); - $post_ratings_score = ($post_ratings_score+intval($ratings_value[$rate-1])); - $post_ratings_average = round($post_ratings_score/$post_ratings_users, 2); - update_post_meta($post_id, 'ratings_users', $post_ratings_users); - update_post_meta($post_id, 'ratings_score', $post_ratings_score); - update_post_meta($post_id, 'ratings_average', $post_ratings_average); - - // Add Log - if(!empty($user_identity)) { - $rate_user = addslashes($user_identity); - } elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) { - $rate_user = addslashes($_COOKIE['comment_author_'.COOKIEHASH]); - } else { - $rate_user = __('Guest', 'wp-postratings'); - } - $rate_user = apply_filters( 'wp_postratings_process_ratings_user', $rate_user ); - $rate_userid = apply_filters( 'wp_postratings_process_ratings_userid', intval( $user_ID ) ); + if (is_bot($_SERVER['HTTP_USER_AGENT'])) { + esc_html_e('Bots refused', 'wp-postratings'); + exit(); + } - // Only Create Cookie If User Choose Logging Method 1 Or 3 - $postratings_logging_method = intval(get_option('postratings_logging_method')); - if($postratings_logging_method == 1 || $postratings_logging_method == 3) { - $rate_cookie = setcookie("rated_".$post_id, $ratings_value[$rate-1], apply_filters('wp_postratings_cookie_expiration', (time() + 30000000) ), apply_filters('wp_postratings_cookiepath', SITECOOKIEPATH)); - } - // Log Ratings No Matter What - $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 ) ); - // Allow Other Plugins To Hook When A Post Is Rated - do_action('rate_post', $rate_userid, $post_id, $ratings_value[$rate-1]); - // Output AJAX Result - echo the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average); - exit(); - } else { - printf(esc_html__('Invalid Post ID (#%s).', 'wp-postratings'), $post_id); - exit(); - } // End if($post) - } else { - printf(esc_html__('You Had Already Rated This Post. Post ID #%s.', 'wp-postratings'), $post_id); - exit(); - }// End if(!$rated) - } // End if($rate && $post_id && check_allowtorate()) + + header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); + + $last_id = 0; $last_error = ''; + $ret = process_ratings($post_id, $rate, $last_id, $last_error); + if (! $ret) { + printf($last_error); + exit(); + } + + // defines $post_ratings_users, $post_ratings_score and $post_ratings_average, $post_ratings_rating + extract($ret); + process_ratings_setcookie($post_id, $post_ratings_rating); + // Output AJAX Result + print ( the_ratings_results($post_id, $post_ratings_users, $post_ratings_score, $post_ratings_average) ); + exit(); } // End if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'postratings') } +function process_ratings_setcookie($post_id, $post_ratings_rating) { + // Only Create Cookie If User Choose Logging Method 1 Or 3 + $postratings_logging_method = (int)get_option('postratings_logging_method'); + if( $postratings_logging_method == 1 || $postratings_logging_method == 3 ) { + return setcookie("rated_" . $post_id, + $post_ratings_rating, + apply_filters('wp_postratings_cookie_expiration', (time() + 30000000) ), + apply_filters('wp_postratings_cookiepath', SITECOOKIEPATH)); + } + return TRUE; +} + +// integer $last_id: in/out, if given, fill it with the rate ID inserted inside the DB +function process_ratings( $post_id, $rate, &$last_id = NULL, &$last_error = array() ) { + global $wpdb, $user_identity, $user_ID; + + $errors = array(); + $can_rate = apply_filters( 'wp_postratings_can_rate', $errors, $post_id, $rate); + if (! empty($can_rate)) { + $last_error = $can_rate; + return FALSE; + } + + $post = get_post($post_id); + // If Valid Post Then We Rate It + $ratings_max = intval(get_option('postratings_max')); + $ratings_custom = intval(get_option('postratings_customrating')); + $ratings_value = get_option('postratings_ratingsvalue'); + $post_ratings = get_post_custom($post_id); + $post_ratings_users = ! empty( $post_ratings['ratings_users'] ) ? intval($post_ratings['ratings_users'][0]) : 0; + $post_ratings_score = ! empty( $post_ratings['ratings_score'] ) ? intval($post_ratings['ratings_score'][0]) : 0; + // Check For Ratings Lesser Than 1 And Greater Than $ratings_max + if($rate < 1 || $rate > $ratings_max) { + $rate = 0; + } + $post_ratings_rating = (int)$ratings_value[$rate-1]; + $post_ratings_users = ($post_ratings_users+1); + $post_ratings_score = ($post_ratings_score+intval($ratings_value[$rate-1])); + $post_ratings_average = round($post_ratings_score/$post_ratings_users, 2); + update_post_meta($post_id, 'ratings_users', $post_ratings_users); + update_post_meta($post_id, 'ratings_score', $post_ratings_score); + update_post_meta($post_id, 'ratings_average', $post_ratings_average); + + // Add Log + if(!empty($user_identity)) { + $rate_user = $user_identity; + } elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) { + $rate_user = $_COOKIE['comment_author_'.COOKIEHASH]; + } else { + $rate_user = __('Guest', 'wp-postratings'); + } + $rate_user = apply_filters( 'wp_postratings_process_ratings_user', $rate_user ); + $rate_userid = apply_filters( 'wp_postratings_process_ratings_userid', intval( $user_ID ) ); + + // Log Ratings No Matter What + $rate_log = $wpdb->insert( $wpdb->prefix . 'ratings', + array(// 'rating_id' => 0, autoinc + 'rating_postid' => $post_id, + 'rating_posttitle' => $post->post_title, + 'rating_rating' => $ratings_value[$rate-1], + 'rating_timestamp' => current_time('timestamp'), + 'rating_ip' => get_ipaddress(), + 'rating_host' => @gethostbyaddr( get_ipaddress() ), + 'rating_username' => $rate_user, + 'rating_userid' => $rate_userid), + array('%d', '%s', '%d', '%d', '%s', '%s', '%s', '%d') ); + + $last_id = $wpdb->insert_id; + // Allow Other Plugins To Hook When A Post Is Rated + do_action('rate_post', $rate_userid, $post_id, $ratings_value[$rate-1]); + return compact( 'post_ratings_users', 'post_ratings_score', 'post_ratings_average', 'post_ratings_rating'); +} + ### Function: Process Ratings add_action('wp_ajax_postratings-admin', 'manage_ratings'); @@ -618,11 +599,7 @@ function manage_ratings() $postratings_ratingstext[1] = __('Vote Up', 'wp-postratings'); } else { for($i = 0; $i < $postratings_max; $i++) { - if($i > 0) { - $postratings_ratingstext[$i] = sprintf(esc_html__('%s Stars', 'wp-postratings'), number_format_i18n($i+1)); - } else { - $postratings_ratingstext[$i] = sprintf(esc_html__('%s Star', 'wp-postratings'), number_format_i18n($i+1)); - } + $postratings_ratingstext[$i] = esc_html__(sprintf(_n('%s Star', '%s Stars', $i, 'wp-postratings'), number_format_i18n($i+1))); $postratings_ratingsvalue[$i] = $i+1; } } @@ -637,19 +614,27 @@ function manage_ratings() '; + } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_start.'.RATINGS_IMG_EXT)) { + $image_start = 'rating_start.'.RATINGS_IMG_EXT.''; + } + + if(is_rtl() && file_exists($postratings_path.'/'.$postratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) { + $image_end = 'rating_end-rtl.'.RATINGS_IMG_EXT.''; + } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_end.'.RATINGS_IMG_EXT)) { + $image_end = 'rating_end.'.RATINGS_IMG_EXT.''; + } + for($i = 1; $i <= $postratings_max; $i++) { $postratings_text = stripslashes($postratings_ratingstext[$i-1]); $postratings_value = $postratings_ratingsvalue[$i-1]; if($postratings_value > 0) { $postratings_value = '+'.$postratings_value; } - echo ''."\n"; - echo '\n'."\n"; - echo ''."\n"; - echo ''."\n"; - echo ''."\n"; + echo $image_end; + echo << + + + +EOF; } ?> @@ -809,29 +788,22 @@ function postratings_page_admin_general_stats($content) { ### Function: Add WP-PostRatings Top Most/Highest Stats To WP-Stats Page Options function postratings_page_admin_most_stats($content) { + $content = array(); $stats_display = get_option('stats_display'); $stats_mostlimit = intval(get_option('stats_mostlimit')); - if($stats_display['rated_highest_post'] == 1) { - $content .= '  
'."\n"; - } else { - $content .= '  
'."\n"; - } - if($stats_display['rated_highest_page'] == 1) { - $content .= '  
'."\n"; - } else { - $content .= '  
'."\n"; - } - if($stats_display['rated_most_post'] == 1) { - $content .= '  
'."\n"; - } else { - $content .= '  
'."\n"; - } - if($stats_display['rated_most_page'] == 1) { - $content .= '  
'."\n"; - } else { - $content .= '  
'."\n"; + + foreach(array('rated_highest_post' => _n('%s Highest Rated Post', '%s Highest Rated Posts', $stats_mostlimit, 'wp-postratings'), + 'rated_highest_page' => _n('%s Highest Rated Page', '%s Highest Rated Pages', $stats_mostlimit, 'wp-postratings'), + 'rated_most_post' => _n('%s Most Rated Post', '%s Most Rated Posts', $stats_mostlimit, 'wp-postratings'), + 'rated_most_page' => _n('%s Most Rated Page', '%s Most Rated Pages', $stats_mostlimit, 'wp-postratings')) as $k => $v) { + if($stats_display[$k] == 1) { + $content[] = '' + . '  ' + . '
'."\n"; + } } - return $content; + + return implode('', $content); } @@ -929,66 +901,73 @@ function get_ratings_images($ratings_custom, $ratings_max, $post_rating, $rating } +// $custom: true|false +// $type: one of [on, off, half, half-rtl] +function get_rating_image_url($ratings_image, $type, $i = null /* if custom */) { + if ($i) { + return plugins_url(sprintf("/wp-postratings/images/%s/rating_%d_%s.%s", $ratings_image, $i, $type, RATINGS_IMG_EXT)); + } else { + return plugins_url(sprintf("/wp-postratings/images/%s/rating_%s.%s", $ratings_image, $type, RATINGS_IMG_EXT)); + } +} + ### Function: Gets HTML of rating images for voting function get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_rating, $ratings_image, $image_alt, $insert_half, $ratings_texts) { - $ratings_images = ''; + $ratings_images = array(); + $ratings_image = esc_attr( $ratings_image ); if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) { - $ratings_images .= ''; + $ratings_images[] = ''; } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_start.'.RATINGS_IMG_EXT)) { - $ratings_images .= ''; + $ratings_images[] = ''; } - if($ratings_custom) { - for($i=1; $i <= $ratings_max; $i++) { - if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'half-rtl.'.RATINGS_IMG_EXT)) { - $use_half_rtl = 1; - } else { - $use_half_rtl = 0; - } - $ratings_text = esc_attr( stripslashes( $ratings_texts[$i-1] ) ); - $ratings_text_js = esc_js( $ratings_text ); - $image_alt = apply_filters( 'wp_postratings_ratings_image_alt', $ratings_text ); - if($i <= $post_rating) { - $ratings_images .= ''.$image_alt.''; - } elseif($i == $insert_half) { - if ($use_half_rtl) { - $ratings_images .= ''.$image_alt.''; - } else { - $ratings_images .= ''.$image_alt.''; - } - } else { - $ratings_images .= ''.$image_alt.''; - } - } + + if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'half-rtl.'.RATINGS_IMG_EXT)) { + $use_custom_half_rtl = 1; } else { - if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) { - $use_half_rtl = 1; - } else { - $use_half_rtl = 0; - } - for($i=1; $i <= $ratings_max; $i++) { - $ratings_text = esc_attr( stripslashes( $ratings_texts[$i-1] ) ); - $ratings_text_js = esc_js( $ratings_text ); - $image_alt = apply_filters( 'wp_postratings_ratings_image_alt', $ratings_text ); - if($i <= $post_rating) { - $ratings_images .= ''.$image_alt.''; - } elseif($i == $insert_half) { - if ($use_half_rtl) { - $ratings_images .= ''.$image_alt.''; - } else { - $ratings_images .= ''.$image_alt.''; - } - } else { - $ratings_images .= ''.$image_alt.''; - } - } + $use_custom_half_rtl = 0; + } + if (is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_half-rtl.'.RATINGS_IMG_EXT)) { + $use_half_rtl = 1; + } else { + $use_half_rtl = 0; } + + + for($i=1; $i <= $ratings_max; $i++) { + $ratings_text = esc_attr( stripslashes( $ratings_texts[$i-1] ) ) ; + $image_alt = esc_attr( apply_filters( 'wp_postratings_ratings_image_alt', $ratings_text ) ); + + $rating_attr = array( + 'id' => "rating_" . $post_id . "_" . $i, + 'alt' => $image_alt, + 'title' => $image_alt, + 'data-id' => $post_id, + 'data-votes' => $i, + 'data-ratings-text' => $ratings_text, + 'data-current-rating' => $post_rating, + 'data-half' => $insert_half, + 'style' => "cursor:pointer; border:0px;" + ); + + if($ratings_custom) { + $rating_attr['src'] = get_rating_image_url($ratings_image, $i <= $post_rating ? 'on' : ( $i == $insert_half ? ( $use_custom_half_rtl ? 'half-rtl' : 'half' ) : 'off' ), $i); + } else { + $rating_attr['src'] = get_rating_image_url($ratings_image, $i <= $post_rating ? 'on' : ( $i == $insert_half ? ( $use_half_rtl ? 'half-rtl' : 'half' ) : 'off' ), NULL); + } + + $ratings_images[] = ''; + } + + if(is_rtl() && file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) { - $ratings_images .= ''; - } elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) { - $ratings_images .= ''; + $ratings_images[] = ''; } - return $ratings_images; + elseif(file_exists(WP_PLUGIN_DIR.'/wp-postratings/images/'.$ratings_image.'/rating_end.'.RATINGS_IMG_EXT)) { + $ratings_images[] = ''; + } + + return implode('', $ratings_images); } @@ -1111,13 +1090,14 @@ function expand_ratings_template($template, $post_data, $post_ratings_data = nul $post_ratings_images = apply_filters( 'wp_postratings_ratings_images_vote', $get_ratings_images_vote, $post_id, $post_ratings, $ratings_max ); $value = str_replace( "%RATINGS_IMAGES_VOTE%", $post_ratings_images, $value ); } - $value = str_replace("%RATINGS_ALT_TEXT%", $post_ratings_alt_text, $value); - $value = str_replace("%RATINGS_TEXT%", $post_ratings_text, $value); - $value = str_replace("%RATINGS_MAX%", number_format_i18n($ratings_max), $value); - $value = str_replace("%RATINGS_SCORE%", $post_ratings_score, $value); - $value = str_replace("%RATINGS_AVERAGE%", number_format_i18n($post_ratings_average, 2), $value); - $value = str_replace("%RATINGS_PERCENTAGE%", number_format_i18n($post_ratings_percentage, 2), $value); - $value = str_replace("%RATINGS_USERS%", number_format_i18n($post_ratings_users), $value); + + $search1 = array("%RATINGS_ALT_TEXT%", "%RATINGS_TEXT%", "%RATINGS_MAX%", "%RATINGS_SCORE%"); + $replac1 = array($post_ratings_alt_text, $post_ratings_text, number_format_i18n($ratings_max), $post_ratings_score); + $value = str_replace($search1, $replac1, $value); + + $search2 = array("%RATINGS_AVERAGE%", "%RATINGS_PERCENTAGE%", "%RATINGS_USERS%"); + $replac2 = array(number_format_i18n($post_ratings_average, 2), number_format_i18n($post_ratings_percentage, 2), number_format_i18n($post_ratings_users)); + $value = str_replace($search2, $replac2, $value); // Post Template Variables $post_link = get_permalink($post_data); @@ -1125,32 +1105,29 @@ function expand_ratings_template($template, $post_data, $post_ratings_data = nul if ($max_post_title_chars > 0) { $post_title = snippet_text($post_title, $max_post_title_chars); } - $value = str_replace("%POST_ID%", $post_id, $value); - $value = str_replace("%POST_TITLE%", $post_title, $value); - $value = str_replace("%POST_URL%", $post_link, $value); + $value = str_replace(array("%POST_ID%", "%POST_TITLE%", "%POST_URL%"), + array($post_id, $post_title, $post_link), + $value); - if (strpos($template, '%POST_EXCERPT%') !== false) { + if (preg_match('/%POST_(EXCERPT|CONTENT|THUMBNAIL)%/', $template)) { if (get_the_ID() != $post_id) { $post = &get_post($post_id); } - $post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password); - $value = str_replace("%POST_EXCERPT%", $post_excerpt, $value); - } - if (strpos($template, '%POST_CONTENT%') !== false) { - if (get_the_ID() != $post_id) { - $post = &get_post($post_id); + + if (strpos($template, '%POST_EXCERPT%') !== false) { + $post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password); + $value = str_replace("%POST_EXCERPT%", $post_excerpt, $value); } - $value = str_replace("%POST_CONTENT%", get_the_content(), $value); - } - if (strpos($template, '%POST_THUMBNAIL%') !== false) { - if (get_the_ID() != $post_id) { - $post = &get_post($post_id); + if (strpos($template, '%POST_CONTENT%') !== false) { + $value = str_replace("%POST_CONTENT%", get_the_content(), $value); + } + if (strpos($template, '%POST_THUMBNAIL%') !== false) { + $value = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( $post, 'thumbnail' ), $value ); } - $value = str_replace( '%POST_THUMBNAIL%', get_the_post_thumbnail( $post, 'thumbnail' ), $value ); } // Google Rich Snippet - $google_structured_data = ''; + $google_structured_data = ''; $ratings_options['richsnippet'] = isset( $ratings_options['richsnippet'] ) ? $ratings_options['richsnippet'] : 1; if( $ratings_options['richsnippet'] && is_singular() && $is_main_loop ) { $itemtype = apply_filters( 'wp_postratings_schema_itemtype', 'itemscope itemtype="http://schema.org/Article"' ); @@ -1158,24 +1135,25 @@ function expand_ratings_template($template, $post_data, $post_ratings_data = nul if( empty( $post_excerpt ) ) { $post_excerpt = ratings_post_excerpt( $post_id, $post->post_excerpt, $post->post_content, $post->post_password ); } - $post_meta = ''; - $post_meta .= ''; - $post_meta .= ''; - $post_meta .= ''; - $post_meta .= ''; - $post_meta .= ''; - $post_meta .= ''; + $post_meta = '' + . '' + . '' + . '' + . '' + . '' + . ''; + // Image - if( has_post_thumbnail() ) { - $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( null ) ); - if( ! empty( $thumbnail ) ) { - $post_meta .= '
'; - $post_meta .= ''; - $post_meta .= ''; - $post_meta .= ''; - $post_meta .= '
'; - } + if( has_post_thumbnail() && ( $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( null ) ) ) ) { + $post_meta .= << + + + + +EOF; } + // Publisher $site_logo = ''; if ( function_exists( 'the_custom_logo' ) ) { @@ -1185,7 +1163,7 @@ function expand_ratings_template($template, $post_data, $post_ratings_data = nul $site_logo = $custom_logo[0]; } } - if( empty( $site_logo ) ) { + if( ! $site_logo ) { if( has_header_image() ) { $header_image = get_header_image(); if( ! empty( $header_image ) ) { @@ -1193,22 +1171,28 @@ function expand_ratings_template($template, $post_data, $post_ratings_data = nul } } } + $site_logo = apply_filters( 'wp_postratings_site_logo', $site_logo ); - $post_meta .= '
'; - $post_meta .= ''; - $post_meta .= '
'; - $post_meta .= ''; - $post_meta .= '
'; - $post_meta .= '
'; + $site_name = get_bloginfo( 'name' ); + $post_meta .= << + +
+ +
+ +EOF; $ratings_meta = ''; if( $post_ratings_average > 0 ) { - $ratings_meta .= '
'; - $ratings_meta .= ''; - $ratings_meta .= ''; - $ratings_meta .= ''; - $ratings_meta .= ''; - $ratings_meta .= '
'; + $ratings_meta = << + + + + + +EOF; } $google_structured_data = apply_filters( 'wp_postratings_google_structured_data', ( empty( $itemtype ) ? $ratings_meta : ( $post_meta . $ratings_meta ) ) );
'."\n"; - if(is_rtl() && file_exists($postratings_path.'/'.$postratings_image.'/rating_start-rtl.'.RATINGS_IMG_EXT)) { - echo 'rating_start-rtl.'.RATINGS_IMG_EXT.''; - } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_start.'.RATINGS_IMG_EXT)) { - echo 'rating_start.'.RATINGS_IMG_EXT.''; - } + echo "
\n"; + echo $image_start; if($postratings_customrating) { if($postratings_max == 2) { echo 'rating_'.$i.'_on.'.RATINGS_IMG_EXT.''; @@ -663,19 +648,13 @@ function manage_ratings() echo 'rating_on.'.RATINGS_IMG_EXT.''; } } - if(is_rtl() && file_exists($postratings_path.'/'.$postratings_image.'/rating_end-rtl.'.RATINGS_IMG_EXT)) { - echo 'rating_end-rtl.'.RATINGS_IMG_EXT.''; - } elseif(file_exists($postratings_path.'/'.$postratings_image.'/rating_end.'.RATINGS_IMG_EXT)) { - echo 'rating_end.'.RATINGS_IMG_EXT.''; - } - echo ''."\n"; - echo ''."\n"; - echo ''."\n"; - echo ''."\n"; - echo '