Jake Vanderwerf
2026-05-03 9bbeea742424837fb58207d88e10dbca0b2cae04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace JVBase\managers\SEO\render\Traits\_Properties;
 
use JVBase\base\SchemaHelper;
use JVBase\managers\SEO\render\Thing\Intangible\Rating\AggregateRating;
use JVBase\meta\Meta;
use JVBase\registrar\Fields;
 
if (!defined('ABSPATH')) {
    exit;
}
trait aggregateRatingTrait {
    /**
     * @var AggregateRating The overall rating, based on a collection of reviews or ratings, of the item.
     */
    protected AggregateRating $aggregateRating;
 
    public function getAggregateRating():?AggregateRating
    {
        return $this->aggregateRating??null;
    }
 
    public function setAggregateRating(array|AggregateRating $aggregateRating):void
    {
        if (is_array($aggregateRating)){
            if (!array_key_exists('type', $aggregateRating)) {
                $aggregateRating['type'] = 'JVBase\managers\SEO\render\Thing\Intangible\Rating\AggregateRating';
            }
            $aggregateRating = SchemaHelper::classFromConfig($aggregateRating);
        }
        $this->aggregateRating = $aggregateRating;
    }
 
    public function setAggregateRatingField(Fields $fields):void
    {
        $fields->addField('average_rating', [
            'type'  => 'select',
            'label' => __('Average Rating', 'jvb'),
            'options'   => [
                'none'  => 'None',
                '0.5'   => '0.5',
                '1'     => '1',
                '1.5'   => '1.5',
                '2'     => '2',
                '2.5'   => '2.5',
                '3'     => '3',
                '3.5'   => '3.5',
                '4'     => '4',
                '4.5'   => '4.5',
                '5'     => '5',
            ],
            'default'   => 'none',
        ]);
        $fields->addField('ratingCount', [
            'type'  => 'number',
            'label' => __('The total number of ratings', 'jvb'),
            'condition' => [
                'field'     => 'average_rating',
                'operator'  => '!=',
                'value'     => 'none',
            ]
        ]);
 
        $fields->addField('reviewCount', [
            'type'  => 'number',
            'label' => __('The total number of reviews (with text)', 'jvb'),
            'condition' => [
                'field'     => 'average_rating',
                'operator'  => '!=',
                'value'     => 'none',
            ]
        ]);
 
        $fields->addField('bestRating', [
            'type'  => 'number',
            'label' => __('The best possible rating value (top of scale)', 'jvb'),
            'default' => 5,
            'condition' => [
                'field'     => 'average_rating',
                'operator'  => '!=',
                'value'     => 'none',
            ]
        ]);
        $fields->addField('worstRating', [
            'type'  => 'number',
            'label' => __('The worst possible rating value (bottom of scale)', 'jvb'),
            'default' => 1,
            'condition' => [
                'field'     => 'average_rating',
                'operator'  => '!=',
                'value'     => 'none',
            ]
        ]);
    }
 
    public function formatAggregateRatingField(Meta $meta):void
    {
        [$average, $ratingCount, $reviewCount, $bestRating, $worstRating] = $meta->getAll([
            'average_rating',
            'ratingCount',
            'reviewCount',
            'bestRating',
            'worstRating'
        ]);
 
        if (!empty($average)) {
            $rating = new AggregateRating();
            $rating->setRatingValue((float)$average);
            if (!empty($ratingCount)) {
                $rating->setRatingCount($ratingCount);
            }
            if (!empty($reviewCount)){
                $rating->setReviewCount($reviewCount);
            }
            if ($bestRating !== 5) {
                $rating->setBestRating($bestRating);
            }
            if ($worstRating !== 1) {
                $rating->setWorstRating($worstRating);
            }
            $this->setAggregateRating($rating);
        }
    }
}