Skip to content

Commit

Permalink
added mutable to WpPost (#694)
Browse files Browse the repository at this point in the history
Co-authored-by: KimSpeer <[email protected]>
Co-authored-by: Kim-the-Diamond <[email protected]>
  • Loading branch information
3 people authored Aug 22, 2024
1 parent 4b2ad2a commit bb70081
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 55 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"moox/notifications": "*",
"moox/flags": "*",
"moox/locate": "*",
"moox/moox-press-wiki": "*",
"wikimedia/composer-merge-plugin": "^2.1"
},
"require-dev": {
Expand Down
19 changes: 19 additions & 0 deletions config/press.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
],

/*
|--------------------------------------------------------------------------
Expand Down
176 changes: 122 additions & 54 deletions packages/press/src/Models/WpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +16,7 @@
*/
class WpPost extends Model
{
use HasFactory;
use HasFactory, Mutable;

protected $fillable = [
'post_author',
Expand All @@ -42,28 +43,26 @@ class WpPost extends Model
'comment_count',
];

public function comment()
{
return $this->hasMany(WpComment::class, 'comment_post_ID');
}

protected $searchableFields = ['*'];
protected $appends;

protected $wpPrefix;

protected $table;

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';
Expand All @@ -74,6 +73,13 @@ public function __construct(array $attributes = [])
'turnus',
'fruhwarnung',
];

$this->initializeMetaField();
}

protected static function boot()
{
parent::boot();
}

protected $casts = [
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class WpPostMetaRelationManager extends RelationManager
{
protected static string $relationship = 'meta';
protected static string $relationship = 'postMeta';

protected static ?string $recordTitleAttribute = 'title';

Expand Down

0 comments on commit bb70081

Please sign in to comment.