<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Koselig\Support\Wordpress;
class Meta extends Model
{
public $table = DB_PREFIX . 'postmeta';
public static $cache = [];
public static function get($page = null, $name = null)
{
if (!ctype_digit((string) $page) && $name === null) {
$name = $page;
$page = null;
}
if ($page === null) {
$page = Wordpress::id();
}
if (!isset(self::$cache[$page])) {
self::$cache[$page] = self::where('post_id', $page)->get();
}
if ($name === null) {
return self::$cache[$page]->mapWithKeys(function ($item) {
return [$item->meta_key => $item->meta_value];
})->all();
}
$value = self::$cache[$page]->where('meta_key', $name)->first();
return empty($value) ? null : $value->meta_value;
}
public function post()
{
return $this->belongsTo(Post::class);
}
}