Skip to content

Commit

Permalink
Only add preview targets when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed May 28, 2024
1 parent 40d2ec6 commit 6a16657
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/Subscribers/SocialImagesGeneratorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

namespace Aerni\AdvancedSeo\Subscribers;

use Aerni\AdvancedSeo\Actions\ShouldGenerateSocialImages;
use Aerni\AdvancedSeo\Concerns\GetsEventData;
use Aerni\AdvancedSeo\Facades\SocialImage;
use Aerni\AdvancedSeo\Features\SocialImagesGenerator;
use Aerni\AdvancedSeo\Jobs\DeleteSocialImagesJob;
use Aerni\AdvancedSeo\Jobs\GenerateSocialImagesJob;
use Illuminate\Events\Dispatcher;
use Statamic\Events;
use Statamic\Statamic;
use Statamic\Events\Event;
use Statamic\Facades\Site;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Statamic\Facades\CP\Toast;
use Illuminate\Events\Dispatcher;
use Aerni\AdvancedSeo\Facades\SocialImage;
use Aerni\AdvancedSeo\Concerns\GetsEventData;
use Aerni\AdvancedSeo\Jobs\DeleteSocialImagesJob;
use Aerni\AdvancedSeo\Jobs\GenerateSocialImagesJob;
use Aerni\AdvancedSeo\Features\SocialImagesGenerator;
use Aerni\AdvancedSeo\Actions\ShouldGenerateSocialImages;

class SocialImagesGeneratorSubscriber
{
Expand Down Expand Up @@ -50,13 +54,26 @@ public function generateSocialImages(Event $event): void

public function addPreviewTargets(Event $event): void
{
$data = $this->getDataFromEvent($event);

if (! SocialImagesGenerator::enabled($data)) {
if (! $this->shouldAddPreviewTargets($event)) {
return;
}

// TODO: This has to change when implementing for taxonomies.
$this->getProperty($event)?->collection()->addPreviewTargets(SocialImage::previewTargets($event->entry));
}

protected function shouldAddPreviewTargets(Event $event): bool
{
// Only add preview targets in the CP.
if (! Statamic::isCpRoute()) {
return false;
}

// Only add preview targets when editing an existing entry.
if (! Str::containsAll(request()->path(), ['collections', $event->entry?->id()])) {
return false;
}

// Only add preview targets when the generator is enabled.
return SocialImagesGenerator::enabled($this->getDataFromEvent($event));
}
}

0 comments on commit 6a16657

Please sign in to comment.