Skip to content

Commit 8125a8d

Browse files
Continued updates to comment stars
1 parent fa2858d commit 8125a8d

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

wire/modules/Fieldtype/FieldtypeComments/CommentArray.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,14 @@ public function stars($allowPartial = true, $getCount = false) {
262262
*/
263263
public function renderStars($showCount = false, $options = array()) {
264264
$defaults = array(
265-
'stars' => null,
266-
'count' => null,
267-
'blank' => true, // return blank if no ratings yet
265+
'stars' => null, // optionally override the combined stars value (stars and count must both be specified)
266+
'count' => null, // optionally override the combined count value (stars and count must both be specified)
267+
'blank' => true, // return blank string if no ratings yet?
268268
'partials' => true, // allow partial stars?
269+
'schema' => '', // may be 'rdfa', 'microdata' or blank. Used only if showCount=true.
269270
);
270271
$options = array_merge($defaults, $options);
271-
if(!is_null($options['stars'])) {
272+
if(!is_null($options['stars']) && !is_null($options['count'])) {
272273
$stars = $options['stars'];
273274
$count = (int) $options['count'];
274275
} else {
@@ -277,7 +278,7 @@ public function renderStars($showCount = false, $options = array()) {
277278
if(!$count && $options['blank']) return '';
278279
$commentStars = new CommentStars();
279280
$out = $commentStars->render($stars, false);
280-
if($showCount) $out .= $commentStars->renderCount((int) $count, $stars);
281+
if($showCount) $out .= $commentStars->renderCount((int) $count, $stars, $options['schema']);
281282
return $out;
282283
}
283284
}

wire/modules/Fieldtype/FieldtypeComments/CommentList.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class CommentList extends Wire implements CommentListInterface {
6262
'upvoteFormat' => '↑{cnt}',
6363
'downvoteFormat' => '↓{cnt}',
6464
'depth' => 0,
65+
'replyLabel' => 'Reply',
6566
);
6667

6768
/**
@@ -75,6 +76,7 @@ public function __construct(CommentArray $comments, $options = array()) {
7576

7677
$h3 = $this->_('h3'); // Headline tag
7778
$this->options['headline'] = "<$h3>" . $this->_('Comments') . "</$h3>"; // Header text
79+
$this->options['replyLabel'] = $this->_('Reply');
7880

7981
if(empty($options['commentHeader'])) {
8082
if(empty($options['dateFormat'])) {
@@ -208,7 +210,7 @@ public function renderItem(Comment $comment, $depth = 0) {
208210
$out .=
209211
"\n\t\t<div class='CommentFooter'>" .
210212
"\n\t\t\t<p class='CommentAction'>" .
211-
"\n\t\t\t\t<a class='CommentActionReply' data-comment-id='$comment->id' href='#Comment{$comment->id}'>" . $this->_('Reply') . "</a> " .
213+
"\n\t\t\t\t<a class='CommentActionReply' data-comment-id='$comment->id' href='#Comment{$comment->id}'>" . $this->options['replyLabel'] . "</a> " .
212214
($permalink ? "\n\t\t\t\t$permalink" : "") .
213215
"\n\t\t\t</p>" .
214216
"\n\t\t</div>";

wire/modules/Fieldtype/FieldtypeComments/CommentStars.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class CommentStars extends WireData {
2323
'wrapClass' => 'CommentStars', // required by JS and CSS
2424
'wrapClassInput' => 'CommentStarsInput', // required by JS and CSS
2525
'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
2929
'unratedLabel' => 'not yet rated',
3030
);
3131

@@ -39,8 +39,8 @@ public function __construct() {
3939
foreach(self::$defaults as $key => $value) {
4040
$this->set($key, $value);
4141
}
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)'));
4444
$this->set('unratedLabel', $this->_('not yet rated'));
4545
}
4646

@@ -117,10 +117,11 @@ public function render($stars = 0, $allowInput = false) {
117117
*
118118
* @param int $count
119119
* @param float|int $stars
120+
* @param string $schema May be "rdfa" or "microdata" or blank (default) to omit.
120121
* @return string
121122
*
122123
*/
123-
public function renderCount($count, $stars = 0.0) {
124+
public function renderCount($count, $stars = 0.0, $schema = '') {
124125
$count = (int) $count;
125126
if($stars > 0) {
126127
if(is_int($stars)) {
@@ -129,12 +130,31 @@ public function renderCount($count, $stars = 0.0) {
129130
if($stars > $this->numStars) $stars = 5.0;
130131
$stars = round($stars, 1);
131132
}
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);
134149
} else {
135150
$out = $this->unratedLabel;
136151
}
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+
}
138159
}
139-
140160
}

0 commit comments

Comments
 (0)