dev-think-one /
nova-seo-entity
| 1 | <?php |
||
| 2 | |||
| 3 | namespace NovaSeoEntity\Models; |
||
| 4 | |||
| 5 | use Artesaos\SEOTools\Facades\SEOMeta; |
||
| 6 | use Artesaos\SEOTools\Facades\SEOTools; |
||
| 7 | use Illuminate\Database\Eloquent\Model; |
||
| 8 | use NovaSeoEntity\Contracts\WithSeoEntity; |
||
| 9 | use SimpleImageManager\Eloquent\HasThinkImage; |
||
| 10 | use SimpleImageManager\Eloquent\ThinkImage; |
||
| 11 | |||
| 12 | class SEOInfo extends Model |
||
| 13 | { |
||
| 14 | use HasThinkImage; |
||
| 15 | |||
| 16 | public static string $thinkSeoImgDriver = 'seo_image'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @inheritdoc |
||
| 20 | */ |
||
| 21 | protected $guarded = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @inheritdoc |
||
| 25 | */ |
||
| 26 | protected $casts = [ |
||
| 27 | 'meta' => 'json', |
||
| 28 | 'open_graph' => 'json', |
||
| 29 | 'twitter_card' => 'json', |
||
| 30 | 'json_ld' => 'json', |
||
| 31 | 'json_ld_multi' => 'json', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @inheritDoc |
||
| 36 | */ |
||
| 37 | 9 | public function getTable(): string |
|
| 38 | { |
||
| 39 | 9 | return config('nova-seo-entity.table', parent::getTable()); |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get model used token |
||
| 44 | * |
||
| 45 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 46 | */ |
||
| 47 | 5 | public function seoptimisable(): \Illuminate\Database\Eloquent\Relations\MorphTo |
|
| 48 | { |
||
| 49 | 5 | return $this->morphTo('seoptimisable'); |
|
| 50 | } |
||
| 51 | |||
| 52 | /** @inerhitDoc */ |
||
| 53 | 4 | public function thinkImagesMap(): array |
|
| 54 | { |
||
| 55 | 4 | return [ |
|
| 56 | 4 | 'image' => ( new ThinkImage(static::$thinkSeoImgDriver, $this->image) ), |
|
|
0 ignored issues
–
show
|
|||
| 57 | 4 | ]; |
|
| 58 | } |
||
| 59 | |||
| 60 | 4 | public function seoImage(): ThinkImage |
|
| 61 | { |
||
| 62 | 4 | return $this->thinkImage('image'); |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Prepare seo data for view. |
||
| 67 | * |
||
| 68 | * @return static |
||
| 69 | */ |
||
| 70 | 1 | public function seoPrepare(): static |
|
| 71 | { |
||
| 72 | /** @var $model WithSeoEntity|null */ |
||
| 73 | 1 | $model = ($this->seoptimisable instanceof WithSeoEntity) ? $this->seoptimisable : null; |
|
|
0 ignored issues
–
show
|
|||
| 74 | |||
| 75 | 1 | if ($value = $model?->getSEOFieldValue('robots', $this->robots) ?? $this->robots) { |
|
|
0 ignored issues
–
show
|
|||
| 76 | 1 | SEOMeta::setRobots($value); |
|
| 77 | } |
||
| 78 | |||
| 79 | 1 | if ($value = $model?->getSEOFieldValue('title', $this->title) ?? $this->title) { |
|
|
0 ignored issues
–
show
|
|||
| 80 | 1 | SEOTools::setTitle($value); |
|
| 81 | } |
||
| 82 | |||
| 83 | 1 | if ($value = $model?->getSEOFieldValue('description', $this->description) ?? $this->description) { |
|
|
0 ignored issues
–
show
|
|||
| 84 | 1 | SEOTools::setDescription($value); |
|
| 85 | } |
||
| 86 | |||
| 87 | 1 | if ($value = $model?->getSEOFieldValue('canonical', $this->canonical) ?? $this->canonical) { |
|
|
0 ignored issues
–
show
|
|||
| 88 | 1 | SEOTools::setCanonical( |
|
| 89 | 1 | preg_match('/^http(s)?:\/\//', $value) ? |
|
| 90 | 1 | $value : |
|
| 91 | 1 | (rtrim(config('nova-seo-entity.canonical_url'), '/') . '/' . ltrim($value, '/')) |
|
| 92 | 1 | ); |
|
| 93 | } |
||
| 94 | |||
| 95 | 1 | $twitterImage = $this->seoImage()->exists('2x1') ? $this->seoImage()->url('2x1'):null; |
|
| 96 | 1 | if ($value = $model?->getSEOFieldValue('img_twitter', $twitterImage) ?? $twitterImage) { |
|
| 97 | 1 | SEOTools::twitter()->setImage($value); |
|
| 98 | } |
||
| 99 | |||
| 100 | 1 | $opengraphImage = $this->seoImage()->exists('1_91x1') ? $this->seoImage()->url('1_91x1'):null; |
|
| 101 | 1 | if ($value = $model?->getSEOFieldValue('img_opengraph', $opengraphImage) ?? $opengraphImage) { |
|
| 102 | 1 | SEOTools::opengraph()->addImage($value); |
|
| 103 | } |
||
| 104 | |||
| 105 | 1 | $ldImages = array_filter([ |
|
| 106 | 1 | $this->seoImage()->exists('1x1') ? $this->seoImage()->url('1x1'):null, |
|
| 107 | 1 | $this->seoImage()->exists('4x3') ? $this->seoImage()->url('4x3'):null, |
|
| 108 | 1 | $this->seoImage()->exists('16x9') ? $this->seoImage()->url('16x9'):null, |
|
| 109 | 1 | ]); |
|
| 110 | 1 | if (($value = $model?->getSEOFieldValue('img_json_ld', $ldImages) ?? $ldImages) && !empty($value)) { |
|
| 111 | 1 | SEOTools::jsonLd()->setImages($value); |
|
| 112 | } |
||
| 113 | |||
| 114 | 1 | return $this; |
|
| 115 | } |
||
| 116 | } |
||
| 117 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.