Yasr – Yet Another Stars Rating - Version 1.0.4

Version Description

  • FIXED: yasr_visitor_votes didn't work if used the postid attribute
  • Code cleanup
Download this release

Release Info

Developer Dudo
Plugin Icon 128x128 Yasr – Yet Another Stars Rating
Version 1.0.4
Comparing to
See all releases

Code changes from version 1.0.3 to 1.0.4

js/yasr-front.js CHANGED
@@ -1,9 +1,9 @@
1
 
2
  /*** Constant used by yasr
3
 
4
- yasrCommonData (postid, ajaxurl, loggedUser)
5
 
6
- yasrVisitorsVotesData (tooltipValues, starSize, voteIfUserAlredyRated, loaderHtml, nonceVisitor, visitorStatsEnabled )
7
 
8
  yasrMultiSetData (setType, nonce)
9
 
@@ -20,7 +20,7 @@ document.addEventListener('DOMContentLoaded', function(event) {
20
 
21
  yasrVisitorsVotes ();
22
 
23
- if (yasrVisitorsVotesData.visitorStatsEnabled == 'yes') {
24
 
25
  yasrDrawTipsProgress ();
26
 
@@ -42,26 +42,29 @@ document.addEventListener('DOMContentLoaded', function(event) {
42
 
43
  function yasrVisitorsVotes () {
44
 
45
- jQuery('#yasr_rateit_visitor_votes_' + yasrCommonData.postid).bind('over', function (event, value) { jQuery(this).attr('title', yasrVisitorsVotesData.tooltipValues[value-1]); });
46
 
47
  //Should be useless from version 0.7.9, just to be safe
48
  if (yasrVisitorsVotesData.voteIfUserAlredyRated == "0" ) {
49
  yasrVisitorsVotesData.voteIfUserAlredyRated = false;
50
  }
51
 
52
- jQuery('#yasr_rateit_visitor_votes_' + yasrCommonData.postid).on('rated', function() {
53
 
54
  var el = jQuery(this);
55
  var value = el.rateit('value');
56
  var value = value.toFixed(1); //
57
 
 
 
 
58
  if (value < 1) {
59
- jQuery('#yasr_visitor_votes_' + yasrCommonData.postid).html('You can\'t vote 0');
60
  }
61
 
62
  else {
63
 
64
- jQuery('#yasr_visitor_votes_' + yasrCommonData.postid).html(yasrVisitorsVotesData.loaderHtml);
65
 
66
  //If logged in user and has already rated for a post/page update the vote
67
  if (yasrCommonData.loggedUser && yasrVisitorsVotesData.voteIfUserAlredyRated) {
@@ -69,8 +72,8 @@ document.addEventListener('DOMContentLoaded', function(event) {
69
  var data = {
70
  action: 'yasr_update_visitor_rating',
71
  rating: value,
72
- post_id: yasrCommonData.postid,
73
- size: yasrVisitorsVotesData.starSize,
74
  nonce_visitor: yasrVisitorsVotesData.nonceVisitor
75
  };
76
 
@@ -82,8 +85,8 @@ document.addEventListener('DOMContentLoaded', function(event) {
82
  var data = {
83
  action: 'yasr_send_visitor_rating',
84
  rating: value,
85
- post_id: yasrCommonData.postid,
86
- size: yasrVisitorsVotesData.starSize,
87
  nonce_visitor: yasrVisitorsVotesData.nonceVisitor
88
  };
89
 
@@ -92,7 +95,7 @@ document.addEventListener('DOMContentLoaded', function(event) {
92
  //Send value to the Server
93
  jQuery.post(yasrCommonData.ajaxurl, data, function(response) {
94
  //response
95
- jQuery('#yasr_visitor_votes_' + yasrCommonData.postid).html(response);
96
  jQuery('.rateit').rateit();
97
 
98
  }) ;
@@ -101,6 +104,7 @@ document.addEventListener('DOMContentLoaded', function(event) {
101
 
102
  });//End function insert/update vote
103
 
 
104
  }
105
  /*** End Yasr Visitor Votes ***/
106
 
@@ -135,7 +139,6 @@ document.addEventListener('DOMContentLoaded', function(event) {
135
 
136
  jQuery('#yasr-send-visitor-multiset-'+yasrCommonData.postid+'-'+yasrMultiSetData.setType).hide();
137
 
138
- var cookiename = "yasr_multi_visitor_vote_" +yasrCommonData.postid+'_'+yasrMultiSetData.setType;
139
 
140
  jQuery('#yasr-loader-multiset-visitor-'+yasrCommonData.postid+'-'+yasrMultiSetData.setType).show();
141
 
1
 
2
  /*** Constant used by yasr
3
 
4
+ yasrCommonData (postid, ajaxurl, loggedUser, visitorStatsEnabled, loaderHtml, tooltipValues)
5
 
6
+ yasrVisitorsVotesData (, postid, starSize, voteIfUserAlredyRated, nonceVisitor, )
7
 
8
  yasrMultiSetData (setType, nonce)
9
 
20
 
21
  yasrVisitorsVotes ();
22
 
23
+ if (yasrCommonData.visitorStatsEnabled == 'yes') {
24
 
25
  yasrDrawTipsProgress ();
26
 
42
 
43
  function yasrVisitorsVotes () {
44
 
45
+ jQuery('.rateit').bind('over', function (event, value) { jQuery(this).attr('title', yasrCommonData.tooltipValues[value-1]); });
46
 
47
  //Should be useless from version 0.7.9, just to be safe
48
  if (yasrVisitorsVotesData.voteIfUserAlredyRated == "0" ) {
49
  yasrVisitorsVotesData.voteIfUserAlredyRated = false;
50
  }
51
 
52
+ jQuery('.rateit').on('rated', function() {
53
 
54
  var el = jQuery(this);
55
  var value = el.rateit('value');
56
  var value = value.toFixed(1); //
57
 
58
+ var postid = jQuery( this ).data('postid');
59
+ var classSize = jQuery( this ).attr('class');
60
+
61
  if (value < 1) {
62
+ jQuery('#yasr_visitor_votes_' + postid).html('You can\'t vote 0');
63
  }
64
 
65
  else {
66
 
67
+ jQuery('#yasr_visitor_votes_' + postid).html(yasrCommonData.loaderHtml);
68
 
69
  //If logged in user and has already rated for a post/page update the vote
70
  if (yasrCommonData.loggedUser && yasrVisitorsVotesData.voteIfUserAlredyRated) {
72
  var data = {
73
  action: 'yasr_update_visitor_rating',
74
  rating: value,
75
+ post_id: postid,
76
+ size: classSize,
77
  nonce_visitor: yasrVisitorsVotesData.nonceVisitor
78
  };
79
 
85
  var data = {
86
  action: 'yasr_send_visitor_rating',
87
  rating: value,
88
+ post_id: postid,
89
+ size: classSize,
90
  nonce_visitor: yasrVisitorsVotesData.nonceVisitor
91
  };
92
 
95
  //Send value to the Server
96
  jQuery.post(yasrCommonData.ajaxurl, data, function(response) {
97
  //response
98
+ jQuery('#yasr_visitor_votes_' + postid).html(response);
99
  jQuery('.rateit').rateit();
100
 
101
  }) ;
104
 
105
  });//End function insert/update vote
106
 
107
+
108
  }
109
  /*** End Yasr Visitor Votes ***/
110
 
139
 
140
  jQuery('#yasr-send-visitor-multiset-'+yasrCommonData.postid+'-'+yasrMultiSetData.setType).hide();
141
 
 
142
 
143
  jQuery('#yasr-loader-multiset-visitor-'+yasrCommonData.postid+'-'+yasrMultiSetData.setType).show();
144
 
lib/yasr-ajax-functions.php CHANGED
@@ -988,7 +988,7 @@ if ( ! defined( 'ABSPATH' ) ) exit('You\'re not allowed to see this page'); // E
988
  if(isset($_POST['rating']) && isset($_POST['post_id']) && isset($_POST['size']) && isset($_POST['nonce_visitor'])) {
989
  $rating = $_POST['rating'];
990
  $post_id = $_POST['post_id'];
991
- $size = $_POST['size'];
992
  $nonce_visitor = $_POST['nonce_visitor'];
993
 
994
  if($post_id == '') {
@@ -1023,19 +1023,16 @@ if ( ! defined( 'ABSPATH' ) ) exit('You\'re not allowed to see this page'); // E
1023
  $rating = 5;
1024
  }
1025
 
1026
- if ($size == 'small') {
1027
- $rateit_class='rateit';
1028
  $px_size = '16';
1029
  }
1030
 
1031
- elseif ($size == 'medium') {
1032
- $rateit_class = 'rateit medium';
1033
  $px_size = '24';
1034
  }
1035
 
1036
  //default values
1037
  else {
1038
- $rateit_class = 'rateit bigstars';
1039
  $px_size = '32';
1040
  }
1041
 
@@ -1147,7 +1144,7 @@ if ( ! defined( 'ABSPATH' ) ) exit('You\'re not allowed to see this page'); // E
1147
  if(isset($_POST['rating']) && isset($_POST['post_id']) && isset($_POST['size']) && isset($_POST['nonce_visitor']) ) {
1148
  $new_rating = $_POST['rating'];
1149
  $post_id = $_POST['post_id'];
1150
- $size = $_POST['size'];
1151
  $nonce_visitor = $_POST['nonce_visitor'];
1152
 
1153
  if($post_id == '') {
@@ -1179,19 +1176,16 @@ if ( ! defined( 'ABSPATH' ) ) exit('You\'re not allowed to see this page'); // E
1179
  $rating = 5;
1180
  }
1181
 
1182
- if ($size == 'small') {
1183
- $rateit_class = 'rateit';
1184
  $px_size = '16';
1185
  }
1186
 
1187
- elseif ($size == 'medium') {
1188
- $rateit_class = 'rateit medium';
1189
  $px_size = '24';
1190
  }
1191
 
1192
  //default values
1193
  else {
1194
- $rateit_class = 'rateit bigstars';
1195
  $px_size = '32';
1196
  }
1197
 
988
  if(isset($_POST['rating']) && isset($_POST['post_id']) && isset($_POST['size']) && isset($_POST['nonce_visitor'])) {
989
  $rating = $_POST['rating'];
990
  $post_id = $_POST['post_id'];
991
+ $rateit_class = $_POST['size'];
992
  $nonce_visitor = $_POST['nonce_visitor'];
993
 
994
  if($post_id == '') {
1023
  $rating = 5;
1024
  }
1025
 
1026
+ if ($rateit_class == 'rateit') {
 
1027
  $px_size = '16';
1028
  }
1029
 
1030
+ elseif ($rateit_class == 'rateit medium') {
 
1031
  $px_size = '24';
1032
  }
1033
 
1034
  //default values
1035
  else {
 
1036
  $px_size = '32';
1037
  }
1038
 
1144
  if(isset($_POST['rating']) && isset($_POST['post_id']) && isset($_POST['size']) && isset($_POST['nonce_visitor']) ) {
1145
  $new_rating = $_POST['rating'];
1146
  $post_id = $_POST['post_id'];
1147
+ $rateit_class = $_POST['size'];
1148
  $nonce_visitor = $_POST['nonce_visitor'];
1149
 
1150
  if($post_id == '') {
1176
  $rating = 5;
1177
  }
1178
 
1179
+ if ($rateit_class == 'rateit') {
 
1180
  $px_size = '16';
1181
  }
1182
 
1183
+ elseif ($rateit_class == 'rateit medium') {
 
1184
  $px_size = '24';
1185
  }
1186
 
1187
  //default values
1188
  else {
 
1189
  $px_size = '32';
1190
  }
1191
 
lib/yasr-db-functions.php CHANGED
@@ -487,7 +487,7 @@ add_action ('admin_init', 'admin_init_delete_data_on_post_callback');
487
 
488
  /****** Check if a logged in user has already rated. Return user vote for a post if exists ******/
489
 
490
- function yasr_check_if_user_already_voted() {
491
 
492
  global $wpdb;
493
 
@@ -496,7 +496,12 @@ function yasr_check_if_user_already_voted() {
496
 
497
  $user_id = $current_user->ID;
498
 
499
- $post_id = get_the_ID();
 
 
 
 
 
500
 
501
  if (!$post_id || !$user_id) {
502
 
487
 
488
  /****** Check if a logged in user has already rated. Return user vote for a post if exists ******/
489
 
490
+ function yasr_check_if_user_already_voted($post_id=FALSE) {
491
 
492
  global $wpdb;
493
 
496
 
497
  $user_id = $current_user->ID;
498
 
499
+ //just to be safe
500
+ if(!$post_id) {
501
+
502
+ $post_id = get_the_ID();
503
+
504
+ }
505
 
506
  if (!$post_id || !$user_id) {
507
 
lib/yasr-functions.php CHANGED
@@ -61,12 +61,19 @@ if ( ! defined( 'ABSPATH' ) ) exit('You\'re not allowed to see this page'); // E
61
 
62
  wp_enqueue_script( 'yasrfront', YASR_JS_DIR . 'yasr-front.js' , array('jquery', 'rateit'), '1.0.0', TRUE );
63
 
 
 
 
 
 
64
  wp_localize_script ('yasrfront', 'yasrCommonData', array(
65
 
66
  'postid' => get_the_ID(),
67
  'ajaxurl' => admin_url('admin-ajax.php'),
68
  'loggedUser' => is_user_logged_in(),
69
-
 
 
70
 
71
  )
72
 
61
 
62
  wp_enqueue_script( 'yasrfront', YASR_JS_DIR . 'yasr-front.js' , array('jquery', 'rateit'), '1.0.0', TRUE );
63
 
64
+ $yasr_visitor_votes_loader = '<div id="loader-visitor-rating" >&nbsp; ' . __("Loading, please wait",'yet-another-stars-rating') . ' <img src=' . YASR_IMG_DIR . '/loader.gif title="yasr-loader" alt="yasr-loader"></div>';
65
+
66
+ $tooltip_values = __("bad, poor, ok, good, super", 'yet-another-stars-rating');
67
+ $tooltip_values_exploded = explode(", ", $tooltip_values);
68
+
69
  wp_localize_script ('yasrfront', 'yasrCommonData', array(
70
 
71
  'postid' => get_the_ID(),
72
  'ajaxurl' => admin_url('admin-ajax.php'),
73
  'loggedUser' => is_user_logged_in(),
74
+ 'visitorStatsEnabled' => YASR_VISITORS_STATS,
75
+ 'tooltipValues' => $tooltip_values_exploded,
76
+ 'loaderHtml' => $yasr_visitor_votes_loader
77
 
78
  )
79
 
lib/yasr-shortcode-functions.php CHANGED
@@ -142,10 +142,6 @@ function shortcode_visitor_votes_callback ($atts) {
142
  }
143
  }
144
 
145
- $image = YASR_IMG_DIR . "/loader.gif";
146
-
147
- $loader_html = '<div id="loader-visitor-rating" >&nbsp; ' . __("Loading, please wait",'yet-another-stars-rating') . ' <img src=' . "$image" .' title="yasr-loader" alt="yasr-loader"></div>';
148
-
149
  $medium_rating=round($medium_rating, 1);
150
 
151
  $stars_attribute = yasr_stars_size($size);
@@ -192,7 +188,7 @@ function shortcode_visitor_votes_callback ($atts) {
192
  $readonly = 'false';
193
 
194
  //Chek if a logged in user has already rated for this post
195
- $vote_if_user_already_rated = yasr_check_if_user_already_voted();
196
 
197
  //If user has already rated
198
  if ($vote_if_user_already_rated) {
@@ -244,7 +240,7 @@ function shortcode_visitor_votes_callback ($atts) {
244
  $readonly = 'false'; //REadonly is false if user is logged
245
 
246
  //Chek if a logged in user has already rated for this post
247
- $vote_if_user_already_rated = yasr_check_if_user_already_voted();
248
 
249
  if ($vote_if_user_already_rated) {
250
 
@@ -306,7 +302,7 @@ function shortcode_visitor_votes_callback ($atts) {
306
 
307
  }
308
 
309
- $shortcode_html .= "<div class=\"$stars_attribute[class]\" id=\"yasr_rateit_visitor_votes_$post_id\" data-rateit-starwidth=\"$stars_attribute[px_size]\" data-rateit-starheight=\"$stars_attribute[px_size]\" data-rateit-value=\"$medium_rating\" data-rateit-step=\"1\" data-rateit-resetable=\"false\" data-rateit-readonly=\"$readonly\"></div>";
310
 
311
  $shortcode_html .= $span_dashicon;
312
 
@@ -316,39 +312,30 @@ function shortcode_visitor_votes_callback ($atts) {
316
 
317
  $shortcode_html .= "</div>";
318
 
319
-
320
- $tooltip_values = __("bad, poor, ok, good, super", 'yet-another-stars-rating');
321
- $tooltip_values_exploded = explode(", ", $tooltip_values);
322
-
323
- $ajax_url = admin_url('admin-ajax.php');
324
-
325
- wp_localize_script( 'yasrfront', 'yasrVisitorsVotesData', array(
326
- 'tooltipValues' => $tooltip_values_exploded,
327
- 'starSize' => $size,
328
  'voteIfUserAlredyRated' => $vote_if_user_already_rated,
329
- 'loaderHtml' => $loader_html,
330
- 'nonceVisitor' => $ajax_nonce_visitor,
331
- 'visitorStatsEnabled' => YASR_VISITORS_STATS
332
  )
333
  );
334
 
335
- //IF show visitor votes in loop is disabled use is_singular && is_main query
336
- if ( YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'disabled' ) {
337
 
338
- if( is_singular() && is_main_query() ) {
 
339
 
340
- return $shortcode_html;
341
 
342
- }
343
 
344
- } // End if YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'disabled') {
345
 
346
- //If overall rating in loop is enabled don't use is_singular && is main_query
347
- elseif ( YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'enabled' ) {
348
 
349
- return $shortcode_html;
 
350
 
351
- }
 
 
352
 
353
  } //End function shortcode_visitor_votes_callback
354
 
142
  }
143
  }
144
 
 
 
 
 
145
  $medium_rating=round($medium_rating, 1);
146
 
147
  $stars_attribute = yasr_stars_size($size);
188
  $readonly = 'false';
189
 
190
  //Chek if a logged in user has already rated for this post
191
+ $vote_if_user_already_rated = yasr_check_if_user_already_voted($post_id);
192
 
193
  //If user has already rated
194
  if ($vote_if_user_already_rated) {
240
  $readonly = 'false'; //REadonly is false if user is logged
241
 
242
  //Chek if a logged in user has already rated for this post
243
+ $vote_if_user_already_rated = yasr_check_if_user_already_voted($post_id);
244
 
245
  if ($vote_if_user_already_rated) {
246
 
302
 
303
  }
304
 
305
+ $shortcode_html .= "<div class=\"$stars_attribute[class]\" id=\"yasr_rateit_visitor_votes_$post_id\" data-postid=\"$post_id\" data-rateit-starwidth=\"$stars_attribute[px_size]\" data-rateit-starheight=\"$stars_attribute[px_size]\" data-rateit-value=\"$medium_rating\" data-rateit-step=\"1\" data-rateit-resetable=\"false\" data-rateit-readonly=\"$readonly\"></div>";
306
 
307
  $shortcode_html .= $span_dashicon;
308
 
312
 
313
  $shortcode_html .= "</div>";
314
 
315
+ wp_localize_script( 'yasrfront', "yasrVisitorsVotesData", array(
 
 
 
 
 
 
 
 
316
  'voteIfUserAlredyRated' => $vote_if_user_already_rated,
317
+ 'nonceVisitor' => $ajax_nonce_visitor
 
 
318
  )
319
  );
320
 
 
 
321
 
322
+ //IF show visitor votes in loop is disabled use is_singular && is_main query
323
+ if ( YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'disabled' ) {
324
 
325
+ if( is_singular() && is_main_query() ) {
326
 
327
+ return $shortcode_html;
328
 
329
+ }
330
 
331
+ } // End if YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'disabled') {
 
332
 
333
+ //If overall rating in loop is enabled don't use is_singular && is main_query
334
+ elseif ( YASR_SHOW_VISITOR_VOTES_IN_LOOP === 'enabled' ) {
335
 
336
+ return $shortcode_html;
337
+
338
+ }
339
 
340
  } //End function shortcode_visitor_votes_callback
341
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: 5 star, admin, administrator, AJAX, five-star, javascript, jquery, post r
4
  Requires at least: 3.5
5
  Contributors: Dudo
6
  Tested up to: 4.3.1
7
- Stable tag: 1.0.3
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
  Yet Another Stars Rating is a simple plugin which allows you and / or your visitor to rate a post or element. Ideal for review's website
@@ -121,6 +121,10 @@ Of course not: you can easily add it on the visual editor just by clicking on th
121
 
122
  == Changelog ==
123
 
 
 
 
 
124
  = 1.0.3 =
125
  * TWEAKED: shortcodes doesn't echo javascrfipt anymore. wp_localize_script is used instead
126
  * Minor changes on settings api
4
  Requires at least: 3.5
5
  Contributors: Dudo
6
  Tested up to: 4.3.1
7
+ Stable tag: 1.0.4
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
10
  Yet Another Stars Rating is a simple plugin which allows you and / or your visitor to rate a post or element. Ideal for review's website
121
 
122
  == Changelog ==
123
 
124
+ = 1.0.4 =
125
+ * FIXED: yasr_visitor_votes didn't work if used the postid attribute
126
+ * Code cleanup
127
+
128
  = 1.0.3 =
129
  * TWEAKED: shortcodes doesn't echo javascrfipt anymore. wp_localize_script is used instead
130
  * Minor changes on settings api
yet-another-stars-rating.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Yet Another Stars Rating
4
  * Plugin URI: http://wordpress.org/plugins/yet-another-stars-rating/
5
  * Description: Rating system with rich snippets
6
- * Version: 1.0.3
7
  * Author: Dario Curvino
8
  * Author URI: https://yetanotherstarsrating.com/
9
  * Text Domain: yet-another-stars-rating
@@ -30,7 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
30
  */
31
 
32
 
33
- define('YASR_VERSION_NUM', '1.0.3');
34
 
35
  //Plugin relative path
36
  define( "YASR_RELATIVE_PATH", dirname(__FILE__) );
3
  * Plugin Name: Yet Another Stars Rating
4
  * Plugin URI: http://wordpress.org/plugins/yet-another-stars-rating/
5
  * Description: Rating system with rich snippets
6
+ * Version: 1.0.4
7
  * Author: Dario Curvino
8
  * Author URI: https://yetanotherstarsrating.com/
9
  * Text Domain: yet-another-stars-rating
30
  */
31
 
32
 
33
+ define('YASR_VERSION_NUM', '1.0.4');
34
 
35
  //Plugin relative path
36
  define( "YASR_RELATIVE_PATH", dirname(__FILE__) );