Jake Vanderwerf
2026-05-03 9bbeea742424837fb58207d88e10dbca0b2cae04
inc/managers/SEO/render/Traits/_Properties/aggregateRatingTrait.php
@@ -3,6 +3,8 @@
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;
@@ -29,32 +31,94 @@
      $this->aggregateRating = $aggregateRating;
   }
   public function getAggregateRatingFieldConfig():array
   public function setAggregateRatingField(Fields $fields):void
   {
      return [
         'type'   => 'group',
         'label'  => 'Aggregate/Average Rating',
         'wrap'   => 'details',
         'fields' => [
            'ratingCount'  => [
               'type'   => 'number',
               'label'  => 'The total number of ratings (without text)',
            ],
            'reviewCount'  => [
               'type'   => 'number',
               'label'  => 'The total number of reviews (with text)'
            ],
            'bestRating'   => [
               'type'   => 'number',
               'label'  => 'The best rating',
               'default'=> 5,
            ],
            'worstRating'  => [
               'type'   => 'number',
               'label'  => 'The worst rating',
               'default'=> 1,
            ]
      $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);
      }
   }
}