|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Moox\Press\Models; |
| 6 | + |
| 7 | +use Illuminate\Database\Eloquent\Collection; |
| 8 | +use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 9 | +use Illuminate\Database\Eloquent\Model; |
| 10 | +use Illuminate\Database\Eloquent\Relations\HasMany; |
| 11 | +use Override; |
| 12 | + |
| 13 | +/** |
| 14 | + * @property int $id |
| 15 | + * @property string $domain |
| 16 | + * @property string $path |
| 17 | + */ |
| 18 | +class WpSite extends Model |
| 19 | +{ |
| 20 | + use HasFactory; |
| 21 | + |
| 22 | + /** @var list<string> */ |
| 23 | + protected $fillable = ['domain', 'path']; |
| 24 | + |
| 25 | + /** @var list<string> */ |
| 26 | + protected $searchableFields = ['*']; |
| 27 | + |
| 28 | + protected ?string $wpPrefix = null; |
| 29 | + |
| 30 | + protected $table; |
| 31 | + |
| 32 | + protected ?string $metatable = null; |
| 33 | + |
| 34 | + protected $primaryKey = 'id'; |
| 35 | + |
| 36 | + public $timestamps = false; |
| 37 | + |
| 38 | + protected bool $metaFieldsInitialized = false; |
| 39 | + |
| 40 | + public function __construct(array $attributes = []) |
| 41 | + { |
| 42 | + parent::__construct($attributes); |
| 43 | + $this->wpPrefix = config('press.wordpress_prefix'); |
| 44 | + $this->table = $this->wpPrefix.'site'; |
| 45 | + $this->metatable = $this->wpPrefix.'sitemeta'; |
| 46 | + $this->metaFieldsInitialized = true; |
| 47 | + } |
| 48 | + |
| 49 | + #[Override] |
| 50 | + protected static function boot(): void |
| 51 | + { |
| 52 | + parent::boot(); |
| 53 | + |
| 54 | + static::deleted(function (WpSite $model): void { |
| 55 | + $model->siteMeta()->delete(); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + public function siteMeta(): HasMany |
| 60 | + { |
| 61 | + return $this->hasMany(WpSiteMeta::class, 'site_id', 'id'); |
| 62 | + } |
| 63 | + |
| 64 | + #[Override] |
| 65 | + public function getAttribute($key) |
| 66 | + { |
| 67 | + $value = parent::getAttribute($key); |
| 68 | + |
| 69 | + if (is_null($value) && $this->metaFieldsInitialized && $this->isMetaField($key)) { |
| 70 | + return $this->getMeta($key); |
| 71 | + } |
| 72 | + |
| 73 | + return $value; |
| 74 | + } |
| 75 | + |
| 76 | + #[Override] |
| 77 | + public function setAttribute($key, $value) |
| 78 | + { |
| 79 | + if ($this->metaFieldsInitialized && $this->isMetaField($key)) { |
| 80 | + $this->addOrUpdateMeta($key, $value); |
| 81 | + |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + return parent::setAttribute($key, $value); |
| 86 | + } |
| 87 | + |
| 88 | + public function getMeta(string $key): mixed |
| 89 | + { |
| 90 | + if (! $this->relationLoaded('siteMeta')) { |
| 91 | + $this->load('siteMeta'); |
| 92 | + } |
| 93 | + |
| 94 | + /** @var Collection<int, WpSiteMeta> $siteMeta */ |
| 95 | + $siteMeta = $this->siteMeta; |
| 96 | + |
| 97 | + $meta = $siteMeta->where('meta_key', $key)->first(); |
| 98 | + |
| 99 | + return $meta instanceof WpSiteMeta ? $meta->meta_value : null; |
| 100 | + } |
| 101 | + |
| 102 | + public function addOrUpdateMeta(string $key, mixed $value): void |
| 103 | + { |
| 104 | + WpSiteMeta::updateOrCreate( |
| 105 | + ['site_id' => $this->id, 'meta_key' => $key], |
| 106 | + ['meta_value' => $value] |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return array<string, mixed> |
| 112 | + */ |
| 113 | + public function getAllMetaAttributes(): array |
| 114 | + { |
| 115 | + $metaFields = config('press.default_site_meta', []); |
| 116 | + $attributes = []; |
| 117 | + |
| 118 | + foreach (array_keys($metaFields) as $key) { |
| 119 | + $attributes[$key] = $this->getMeta($key); |
| 120 | + } |
| 121 | + |
| 122 | + return $attributes; |
| 123 | + } |
| 124 | + |
| 125 | + protected function isMetaField(string $key): bool |
| 126 | + { |
| 127 | + return array_key_exists($key, config('press.default_site_meta', [])); |
| 128 | + } |
| 129 | +} |
0 commit comments