Collaboration.php
3 weeks ago
CollaborationComment.php
3 weeks ago
CollaborationCommentSeen.php
3 weeks ago
CollaborationConnected.php
3 weeks ago
CollaborationSent.php
3 weeks ago
Collection.php
3 weeks ago
CollectionItem.php
3 weeks ago
Media.php
3 weeks ago
Page.php
3 weeks ago
Post.php
3 weeks ago
PostMeta.php
3 days ago
Reference.php
3 weeks ago
Term.php
3 weeks ago
TermMeta.php
3 weeks ago
TermTaxonomy.php
3 weeks ago
User.php
3 weeks ago
UserMeta.php
3 weeks ago
PostMeta.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Models; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\App\Casts\AsSerialize; |
| 8 | use Kirki\Framework\Database\Query\Model; |
| 9 | |
| 10 | class PostMeta extends Model |
| 11 | { |
| 12 | protected $table = 'postmeta'; |
| 13 | |
| 14 | protected $primary_key = 'meta_id'; |
| 15 | protected $timestamps = false; |
| 16 | |
| 17 | protected $fillable = [ |
| 18 | 'post_id', |
| 19 | 'meta_key', |
| 20 | 'meta_value', |
| 21 | ]; |
| 22 | |
| 23 | protected $casts = [ |
| 24 | 'post_id' => 'integer', |
| 25 | 'meta_value' => AsSerialize::class, |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * @param int $post_id |
| 30 | * @param string $meta_key |
| 31 | * @param mixed $default |
| 32 | * |
| 33 | * @return mixed |
| 34 | */ |
| 35 | public static function get_meta_value(int $post_id, string $meta_key, $default = null) |
| 36 | { |
| 37 | $meta = static::where('post_id', $post_id)->where('meta_key', $meta_key)->first(); |
| 38 | return $meta && $meta->meta_value !== null && $meta->meta_value !== '' ? $meta->meta_value : $default; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param int $post_id |
| 43 | * @param string $meta_key |
| 44 | * @param mixed $meta_value |
| 45 | * |
| 46 | * @return static |
| 47 | */ |
| 48 | public static function update_meta_value(int $post_id, string $meta_key, $meta_value) |
| 49 | { |
| 50 | return static::update_or_create(['post_id' => $post_id, 'meta_key' => $meta_key], ['meta_value' => $meta_value]); |
| 51 | } |
| 52 | |
| 53 | public function post() |
| 54 | { |
| 55 | return $this->belongs_to(Post::class, 'post_id', 'ID'); |
| 56 | } |
| 57 | } |
| 58 |