-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasSEOAttributesTrait.php
99 lines (82 loc) · 3.25 KB
/
HasSEOAttributesTrait.php
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
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\Models\Concerns;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Str;
use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Statikbe\FilamentFlexibleContentBlocks\FilamentFlexibleBlocksConfig;
use Statikbe\FilamentFlexibleContentBlocks\Models\Enums\ImageFormat;
/**
* @property string|null $seo_title
* @property string|null $seo_description
* @property array|null $seo_keywords
*/
trait HasSEOAttributesTrait
{
use HasMediaAttributesTrait;
use InteractsWithMedia;
public function initializeHasSEOAttributesTrait(): void
{
$this->mergeFillable(['seo_title', 'seo_description', 'seo_keywords']);
$this->mergeCasts([
'seo_keywords' => 'array',
]);
$this->registerSEOImageMediaCollectionAndConversion();
}
public function SEOImage(): MorphMany
{
return $this->media()->where('collection_name', $this->getSEOImageCollection());
}
public function getSEOTitle(): ?string
{
if (! $this->seo_title && isset($this->title)) {
return $this->title;
}
return $this->seo_title;
}
public function getSEODescription(): ?string
{
if (! $this->seo_description && isset($this->intro)) {
return Str::squish(strip_tags($this->intro));
}
return Str::squish($this->seo_description);
}
protected function registerSEOImageMediaCollectionAndConversion()
{
$this->addMediaCollection($this->getSEOImageCollection())
->registerMediaConversions(function (?Media $media) {
$conversion = $this->addMediaConversion($this->getSEOImageConversionName())
->fit(Fit::Crop, 1200, 630)
->format(ImageFormat::WEBP->value);
FilamentFlexibleBlocksConfig::mergeConfiguredModelImageConversion(static::class, $this->getSEOImageCollection(), $this->getSEOImageConversionName(), $conversion);
FilamentFlexibleBlocksConfig::addExtraModelImageConversions(static::class, $this->getSEOImageCollection(), $this);
//for filament upload field
$this->addFilamentThumbnailMediaConversion();
});
}
public function addSEOImage(string $imagePath): void
{
$this->addMedia($imagePath)
->toMediaCollection($this->getSEOImageCollection());
}
public function getSEOImageConversionName(): string
{
return 'seo_image';
}
public function getSEOImageCollection(): string
{
return 'seo_image';
}
public function getSEOImageUrl(?string $conversion = null): ?string
{
if ($media = $this->getFallbackImageMedia($this->SEOImage()->first(), $this->getSEOImageCollection())) {
return $media->getUrl($conversion ?? $this->getSEOImageConversionName());
} elseif (method_exists($this, 'heroImage')) {
$heroMedia = $this->getFallbackImageMedia($this->heroImage()->first(), $this->getHeroImageCollection());
return $heroMedia?->getUrl($conversion ?? $this->getSEOImageConversionName());
} else {
return null;
}
}
}