diff --git a/composer.json b/composer.json index 8b8c08cc0..3453f4673 100644 --- a/composer.json +++ b/composer.json @@ -45,6 +45,7 @@ "moox/notifications": "*", "moox/flags": "*", "moox/locate": "*", + "moox/moox-press-wiki": "*", "wikimedia/composer-merge-plugin": "^2.1" }, "require-dev": { diff --git a/config/press.php b/config/press.php index eedf012d3..33e7ce7f7 100644 --- a/config/press.php +++ b/config/press.php @@ -1122,6 +1122,25 @@ // syntax_highlighting ], + 'default_post_meta' => [ + '_wp_page_template' => '', + '_edit_lock' => '', + '_edit_last' => '', + '_thumbnail_id' => '', + '_wp_attached_file' => '', + '_wp_attachment_metadata' => '', + '_wp_old_slug' => '', + '_wp_trash_meta_status' => '', + '_wp_trash_meta_time' => '', + '_pingme' => '', + '_encloseme' => '', + '_menu_order' => '', + '_wp_post_lock' => '', + '_wp_post_revision' => '', + '_wp_post_type' => '', + '_wp_old_date' => '', + '_wp_old_status' => '', + ], /* |-------------------------------------------------------------------------- diff --git a/packages/press/src/Models/WpPost.php b/packages/press/src/Models/WpPost.php index c2f117129..94fede82d 100644 --- a/packages/press/src/Models/WpPost.php +++ b/packages/press/src/Models/WpPost.php @@ -2,6 +2,7 @@ namespace Moox\Press\Models; +use Awobaz\Mutator\Mutable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; @@ -15,7 +16,7 @@ */ class WpPost extends Model { - use HasFactory; + use HasFactory, Mutable; protected $fillable = [ 'post_author', @@ -42,12 +43,7 @@ class WpPost extends Model 'comment_count', ]; - public function comment() - { - return $this->hasMany(WpComment::class, 'comment_post_ID'); - } - - protected $searchableFields = ['*']; + protected $appends; protected $wpPrefix; @@ -55,15 +51,18 @@ public function comment() protected $metatable; + protected $primaryKey = 'ID'; + public $timestamps = false; - protected $appends; + protected $searchableFields = ['*']; - protected $primaryKey = 'ID'; + protected $metaFieldsInitialized = false; public function __construct(array $attributes = []) { parent::__construct($attributes); + $this->wpPrefix = config('press.wordpress_prefix'); $this->table = $this->wpPrefix.'posts'; $this->metatable = $this->wpPrefix.'postmeta'; @@ -74,6 +73,13 @@ public function __construct(array $attributes = []) 'turnus', 'fruhwarnung', ]; + + $this->initializeMetaField(); + } + + protected static function boot() + { + parent::boot(); } protected $casts = [ @@ -83,104 +89,166 @@ public function __construct(array $attributes = []) 'post_modified_gmt' => 'datetime', ]; - public function getVerantwortlicherAttribute() + protected function initializeMetaField() { - return $this->getMeta('verantwortlicher') ?? null; + if ($this->metaFieldsInitialized) { + return; + } + + $this->metaFieldsInitialized = true; } - public function setVerantwortlicherAttribute($value) + public function metaKey($key) { - $this->addOrUpdateMeta('verantwortlicher', $value); + if (! Str::startsWith($key, $this->wpPrefix)) { + $key = "{$this->wpPrefix}{$key}"; + } + + return $this->getMeta($key); } - public function getGultigBisAttribute() + protected function getMeta($key) { - return $this->getMeta('gultig_bis') ?? null; + if (! $this->relationLoaded('postMeta')) { + $this->load('postMeta'); + } + + $meta = $this->postMeta->where('meta_key', $key)->first(); + + return $meta ? $meta->meta_value : null; } - public function setGultigBisAttribute($value) + public function getAttribute($key) { - $this->addOrUpdateMeta('gultig_bis', $value); + // First, check if the key exists as a native attribute or relationship + $value = parent::getAttribute($key); + + // If the native attribute is not found, look for the meta field + if (is_null($value) && $this->metaFieldsInitialized && $this->isMetaField($key)) { + return $this->getMeta($key); + } + + return $value; } - public function getTurnusAttribute() + public function setAttribute($key, $value) { - return $this->getMeta('turnus') ?? null; + // Check if the key is a meta field first + if ($this->metaFieldsInitialized && $this->isMetaField($key)) { + $this->addOrUpdateMeta($key, $value); + } else { + parent::setAttribute($key, $value); + } + + return $this; } - public function setTurnusAttribute($value) + public function toArray() { - $this->addOrUpdateMeta('turnus', $value); + $attributes = parent::toArray(); + + // Include meta fields in the array representation + $metaFields = config('press.default_post_meta', []); + foreach ($metaFields as $key => $defaultValue) { + $attributes[$key] = $this->getMeta($key) ?? $defaultValue; + } + + return $attributes; } - public function getFruhwarnungAttribute() + public function toJson($options = 0) { - return $this->getMeta('fruhwarnung') ?? null; + return json_encode($this->toArray(), $options); } - public function setFruhwarnungAttribute($value) + protected function addOrUpdateMeta($key, $value) { - $this->addOrUpdateMeta('fruhwarnung', $value); + /** @disregard */ + WpPostMeta::updateOrCreate( + ['post_id' => $this->ID, 'meta_key' => $key], + ['meta_value' => $value] + ); + } + + protected function isMetaField($key) + { + return array_key_exists($key, config('press.default_post_meta', [])); } + /* + * Relations + * + */ public function postMeta() { return $this->hasMany(WpPostMeta::class, 'post_id', 'ID'); } - public function meta() + public function author() { - return $this->hasMany(WpPostMeta::class, 'post_id', 'ID'); + return $this->belongsTo(WpUser::class, 'post_author', 'ID'); } - public function metaKey($key) + public function taxonomies() { - if (! Str::startsWith($key, $this->wpPrefix)) { - $key = "{$this->wpPrefix}{$key}"; - } + return $this->belongsToMany(WpTermTaxonomy::class, config('press.wordpress_prefix').'term_relationships', 'object_id', 'term_taxonomy_id'); + } - return $this->getMeta($key); + public function categories() + { + return $this->taxonomies()->where('taxonomy', 'category'); } - protected function getMeta($key) + public function tags() { - $meta = $this->postMeta()->where('meta_key', $key)->first(); + return $this->taxonomies()->where('taxonomy', 'post_tag'); + } - return $meta ? $meta->meta_value : null; + public function comment() + { + return $this->hasMany(WpComment::class, 'comment_post_ID'); } - protected function addOrUpdateMeta($key, $value) + /* + * ACF- Fields Getter and Setter + */ + public function getVerantwortlicherAttribute() { - $meta = $this->postMeta()->where('meta_key', $key)->first(); + return $this->getMeta('verantwortlicher') ?? null; + } - if ($meta) { - $meta->meta_value = $value; - $meta->save(); - } else { - $this->postMeta()->create([ - 'meta_key' => $key, - 'meta_value' => $value, - ]); - } + public function setVerantwortlicherAttribute($value) + { + $this->addOrUpdateMeta('verantwortlicher', $value); } - public function author() + public function getGultigBisAttribute() { - return $this->belongsTo(WpUser::class, 'post_author', 'ID'); + return $this->getMeta('gultig_bis') ?? null; } - public function taxonomies() + public function setGultigBisAttribute($value) { - return $this->belongsToMany(WpTermTaxonomy::class, config('press.wordpress_prefix').'term_relationships', 'object_id', 'term_taxonomy_id'); + $this->addOrUpdateMeta('gultig_bis', $value); } - public function categories() + public function getTurnusAttribute() { - return $this->taxonomies()->where('taxonomy', 'category'); + return $this->getMeta('turnus') ?? null; } - public function tags() + public function setTurnusAttribute($value) { - return $this->taxonomies()->where('taxonomy', 'post_tag'); + $this->addOrUpdateMeta('turnus', $value); + } + + public function getFruhwarnungAttribute() + { + return $this->getMeta('fruhwarnung') ?? null; + } + + public function setFruhwarnungAttribute($value) + { + $this->addOrUpdateMeta('fruhwarnung', $value); } } diff --git a/packages/press/src/Resources/WpPostResource/RelationManagers/WpPostMetaRelationManager.php b/packages/press/src/Resources/WpPostResource/RelationManagers/WpPostMetaRelationManager.php index 957e9aebe..536159341 100644 --- a/packages/press/src/Resources/WpPostResource/RelationManagers/WpPostMetaRelationManager.php +++ b/packages/press/src/Resources/WpPostResource/RelationManagers/WpPostMetaRelationManager.php @@ -16,7 +16,7 @@ class WpPostMetaRelationManager extends RelationManager { - protected static string $relationship = 'meta'; + protected static string $relationship = 'postMeta'; protected static ?string $recordTitleAttribute = 'title';