<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Koselig\Support\Wordpress;
* Table containing all metadata about a post.
*
* @author Jordan Doyle <jordan@doyle.wf>
*/
class Meta extends Model
{
public $table = DB_PREFIX . 'postmeta';
* Cache for all meta values.
*
* @var array
*/
public static $cache = [];
* Get metadata for a page (or the current page)
*
* <code>Meta::get('my_meta_key');</code>
* or
* <code>Meta::get(7, 'my_meta_key');</code>
*
* @param int|string|null $page page to get meta for (or name of the meta item to get
* if you want to get the current page's meta)
* @param string|null $name
* @return mixed
*/
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] = Meta::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;
}
* Get the post that this meta value belongs to.
*
* @return BelongsTo
*/
public function post()
{
return $this->belongsTo(Post::class);
}
}