<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Koselig\Support\Wordpress;
class UserMeta extends Model
{
protected $table = DB_PREFIX . 'usermeta';
protected $primaryKey = 'umeta_id';
public $timestamps = false;
private static $cache = [];
public static function get($user = null, $name = null)
{
if (!ctype_digit((string) $user) && $name === null) {
$name = $user;
$page = null;
}
if ($user === null) {
$user = auth()->id();
}
if (!isset(self::$cache[$user])) {
self::$cache[$user] = self::where('user_id', $user)->get();
}
if ($name === null) {
return self::$cache[$user]->mapWithKeys(function ($item) {
return [$item->meta_key => $item->meta_value];
})->all();
}
return self::$cache[$user]->where('meta_key', $name)->first()->meta_value;
}
public function user()
{
return $this->belongsTo(User::class);
}
}