Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Reviews Tag #3

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ Our customers give us a {{ $reviews['score'] }}/10 across {{ $reviews['count'] }

You can also use `$reviews['scores'][7]` to display how many reviews have a 7/10 rating.

## Tag

We integrated a Statamic Tag called `Reviews` which will allow you to get some basic data from the reviews like ratings and review content.

Blade example:

```php
Statamic::tag('reviews:get_rating_data')->fetch();
```

## Publishables

You can publish all of the publishables with:
Expand Down
5 changes: 5 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

use JustBetter\StatamicFeedbackCompany\Commands\HarvestReviewsCommand;
use JustBetter\StatamicFeedbackCompany\Commands\RemoveReviewsCommand;
use JustBetter\StatamicFeedbackCompany\Tags\Reviews;
use Statamic\Providers\AddonServiceProvider;

class ServiceProvider extends AddonServiceProvider
{
protected $tags = [
Reviews::class
];

public function bootAddon()
{
$this->bootCommands()
Expand Down
56 changes: 56 additions & 0 deletions src/Tags/Reviews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace JustBetter\StatamicFeedbackCompany\Tags;

use Illuminate\Support\Facades\Cache;
use Statamic\Eloquent\Entries\Entry;
use Statamic\Entries\EntryCollection;
use Statamic\Facades\Entry as EntryFacade;
use Statamic\Tags\Tags;

class Reviews extends Tags
{
public string $reviewTitleId = 'main_open';

public string $reviewTextId = 'open';

public function getReviews(int $count = 3): EntryCollection
{
/* @var EntryCollection $reviews */
$reviews = EntryFacade::query()
->where('collection', 'reviews')
->orderBy('review_date', 'DESC')
->limit($count)
->get(['name', 'product', 'review_date', 'questions', 'total_score']);

return $reviews;
}

public function getReviewData(): array
{
/** @var Entry $review */
$review = $this->params->get('review');
$questionsData = $review->questions ?? [];

return ['title' => $questionsData[$this->reviewTitleId] ?? '', 'text' => $questionsData[$this->reviewTextId] ?? ''];
}

public function getRatingData(): array
{
return Cache::remember('feedback-company-rating-data', now()->addHour(), function () {
$starCounts = [];

for ($star = 1; $star <= 5; $star++) {
$maxScore = $star * 2;
$minScore = $maxScore - 2;
$reviewsCount = EntryFacade::query()
->where('collection', 'reviews')
->whereBetween('total_score', [$minScore, $maxScore])
->count();
$starCounts[$star] = $reviewsCount;
}

return $starCounts;
});
}
}