<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Watson\Rememberable\Rememberable;
class UserMeta extends Model
{
use Rememberable;
public $timestamps = false;
protected $table = DB_PREFIX . 'usermeta';
protected $primaryKey = 'umeta_id';
protected $rememberFor;
private static $cache = [];
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
if (config('wordpress.caching')) {
$this->rememberFor = config('wordpress.caching');
} else {
unset($this->rememberFor);
}
}
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);
}
}