diff --git a/README.md b/README.md index 607264f..d1b5f91 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,9 @@ To localize the button, add a lang file in `/resources/lang` with the name match } ``` -**Two ways to get that URL:** +**Getting the Review URL:** -1. The entry listing via the entry's Open Review action: ![screenshot of review action](https://github.com/transformstudios/statamic-review/raw/main/img/Open%20Review%20Action.png "Review Action") -2. The Review fieldtype; I'd recommend adding to the top of the sidebar of any entry blueprint: ![screenshot of copy review url button](https://github.com/transformstudios/statamic-review/raw/main/img/Copy%20URL%20Button.png "Copy Review URL Button") +Review fieldtype: Recommend adding it to the top of the sidebar of any entry blueprint: ![screenshot of copy review url button](https://github.com/transformstudios/statamic-review/raw/main/img/Copy%20URL%20Button.png "Copy Review URL Button") **Please note**: diff --git a/composer.json b/composer.json index 1e90a18..68af46c 100644 --- a/composer.json +++ b/composer.json @@ -23,10 +23,7 @@ "autoload": { "psr-4": { "TransformStudios\\Review\\": "src" - }, - "files": [ - "src/helpers.php" - ] + } }, "config": { "allow-plugins": { diff --git a/src/Actions/OpenReview.php b/src/Actions/OpenReview.php deleted file mode 100644 index f32f4e8..0000000 --- a/src/Actions/OpenReview.php +++ /dev/null @@ -1,42 +0,0 @@ -first()); - } - - public function visibleTo($item) - { - if (! $item instanceof Entry) { - return false; - } - - $fields = $item->blueprint()->fields()->items(); - - if ($fields->doesntContain(fn (array $value) => Arr::get($value, 'field.type') === 'review')) { - return false; - } - - return ! $item->published() || - $item->hasWorkingCopy() || - ($item->collection()->dated() && $item->date()->isFuture()); - } - - public function visibleToBulk($items) - { - return false; - } -} diff --git a/src/Fieldtypes/Review.php b/src/Fieldtypes/Review.php index ab7eaf8..29d140d 100644 --- a/src/Fieldtypes/Review.php +++ b/src/Fieldtypes/Review.php @@ -3,8 +3,11 @@ namespace TransformStudios\Review\Fieldtypes; use Statamic\Entries\Collection; +use Statamic\Entries\Entry; +use Statamic\Facades\Token as TokenFacade; use Statamic\Fields\Fieldtype; -use TransformStudios\Review\Support\URL; +use Statamic\Tokens\Token; +use TransformStudios\Review\TokenHandler; class Review extends Fieldtype { @@ -33,6 +36,19 @@ public function preload() return []; } - return ['site_url' => review_url($entry)]; + return ['site_url' => $this->url($entry)]; + } + + private function url(Entry $entry): string + { + /** @var \Statamic\Tokens\Token */ + if (! $token = TokenFacade::find($entry->id())) { + $token = tap( + TokenFacade::make(token: $entry->id(), handler: TokenHandler::class), + fn (Token $token) => $token->expireAt(now()->addMonths(6))->save() + ); + } + + return $entry->absoluteUrl().'?token='.$token->token(); } } diff --git a/src/helpers.php b/src/helpers.php deleted file mode 100644 index 892ec46..0000000 --- a/src/helpers.php +++ /dev/null @@ -1,21 +0,0 @@ -id())) { - $token = tap( - TokenFacade::make(token: $entry->id(), handler: TokenHandler::class), - fn (Token $token) => $token->expireAt(now()->addMonths(6))->save() - ); - } - - return $entry->absoluteUrl().'?token='.$token->token(); - } -}