@@ -23,9 +23,9 @@ class CommentStars extends WireData {
23
23
'wrapClass ' => 'CommentStars ' , // required by JS and CSS
24
24
'wrapClassInput ' => 'CommentStarsInput ' , // required by JS and CSS
25
25
'countClass ' => 'CommentStarsCount ' , // class used for renderCount() method
26
- 'detailsLabel ' => '%g/%d ' , // i.e. 4.5/5
27
- 'countLabelSingular ' => '%s via %d rating ' , // i.e. 4/5 via 1 rating
28
- 'countLabelPlural ' => '%s via %d ratings ' , // i.e. 4.5/5 via 10 ratings
26
+ 'detailsLabel ' => '%s/%s ' , // i.e. 4.5/5
27
+ 'countLabelSingular ' => '%1$ s via %2$s rating ' , // i.e. 4/5 via 1 rating
28
+ 'countLabelPlural ' => '%1$ s via %2$s ratings ' , // i.e. 4.5/5 via 10 ratings
29
29
'unratedLabel ' => 'not yet rated ' ,
30
30
);
31
31
@@ -39,8 +39,8 @@ public function __construct() {
39
39
foreach (self ::$ defaults as $ key => $ value ) {
40
40
$ this ->set ($ key , $ value );
41
41
}
42
- $ this ->set ('countLabelSingular ' , $ this ->_ ('%s (%d rating) ' ));
43
- $ this ->set ('countLabelPlural ' , $ this ->_ ('%s (%d ratings) ' ));
42
+ $ this ->set ('countLabelSingular ' , $ this ->_ ('%1$ s (%2$s rating) ' ));
43
+ $ this ->set ('countLabelPlural ' , $ this ->_ ('%1$ s (%2$s ratings) ' ));
44
44
$ this ->set ('unratedLabel ' , $ this ->_ ('not yet rated ' ));
45
45
}
46
46
@@ -117,10 +117,11 @@ public function render($stars = 0, $allowInput = false) {
117
117
*
118
118
* @param int $count
119
119
* @param float|int $stars
120
+ * @param string $schema May be "rdfa" or "microdata" or blank (default) to omit.
120
121
* @return string
121
122
*
122
123
*/
123
- public function renderCount ($ count , $ stars = 0.0 ) {
124
+ public function renderCount ($ count , $ stars = 0.0 , $ schema = '' ) {
124
125
$ count = (int ) $ count ;
125
126
if ($ stars > 0 ) {
126
127
if (is_int ($ stars )) {
@@ -129,12 +130,31 @@ public function renderCount($count, $stars = 0.0) {
129
130
if ($ stars > $ this ->numStars ) $ stars = 5.0 ;
130
131
$ stars = round ($ stars , 1 );
131
132
}
132
- $ details = sprintf ($ this ->detailsLabel , $ stars , $ this ->numStars );
133
- $ out = sprintf ($ this ->_n ($ this ->countLabelSingular , $ this ->countLabelPlural , $ count ), $ details , $ count );
133
+ if ($ schema == 'rdfa ' ) {
134
+ $ stars = "<span property='ratingValue'> $ stars</span> " ;
135
+ $ numStars = "<span property='bestRating'> $ this ->numStars </span> " ;
136
+ $ countStr = "<span property='ratingCount'> $ count</span> " ;
137
+
138
+ } else if ($ schema == 'microdata ' ) {
139
+ $ stars = "<span itemprop='ratingValue'> $ stars</span> " ;
140
+ $ numStars = "<span itemprop='bestRating'> $ this ->numStars </span> " ;
141
+ $ countStr = "<span itemprop='ratingCount'> $ count</span> " ;
142
+ } else {
143
+ $ numStars = $ this ->numStars ;
144
+ $ countStr = (string ) $ count ;
145
+ }
146
+ $ details = sprintf ($ this ->detailsLabel , "$ stars " , "$ numStars " );
147
+ $ label = $ count === 1 ? $ this ->countLabelSingular : $ this ->countLabelPlural ;
148
+ $ out = sprintf ($ label , $ details , $ countStr );
134
149
} else {
135
150
$ out = $ this ->unratedLabel ;
136
151
}
137
- return "<span class=' $ this ->countClass '> $ out</span> " ;
152
+ if ($ schema == 'rdfa ' ) {
153
+ return "<span class=' $ this ->countClass ' property='aggregateRating' typeof='AggregateRating'> $ out</span> " ;
154
+ } else if ($ schema == 'microdata ' ) {
155
+ return "<span class=' $ this ->countClass ' itemprop='aggregateRating' itemscope itemtype='http://schema.org/AggregateRating'> $ out</span> " ;
156
+ } else {
157
+ return "<span class=' $ this ->countClass '> $ out</span> " ;
158
+ }
138
159
}
139
-
140
160
}
0 commit comments