<?php
namespace JordanDoyle\Larapress\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use JordanDoyle\Larapress\Support\Wordpress;
class UserMeta extends Model
{
public $table = DB_PREFIX . 'usermeta';
public 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 = Wordpress::currentUser()->ID;
}
if (!isset(self::$cache[$user])) {
self::$cache[$user] = UserMeta::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);
}
}