<?php
|
namespace JVBase\managers\Dashboard;
|
|
use JVBase\managers\Cache;
|
use WP_Query;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
class DashboardPage {
|
protected string $title;
|
protected string $slug;
|
protected string $icon;
|
protected string $URL;
|
protected int $ID;
|
protected ?string $permission = null;
|
protected ?string $section = null;
|
protected array $scripts = [];
|
protected int $order = 0;
|
protected Cache $cache;
|
|
public function __construct(string $title, string $slug = '', string $icon ='', int $parent = 0) {
|
$this->cache = Cache::for('dashboard');
|
$this->title = $title;
|
if (empty($slug)) {
|
$this->setSlug($title);
|
}else {
|
$this->setSlug($slug);
|
}
|
$this->icon = $icon;
|
$this->setID($parent);
|
$this->setURL();
|
}
|
|
public function setID(int $parentID):void
|
{
|
$this->ID = $this->cache->remember(
|
$this->slug.'_ID',
|
function() use ($parentID) {
|
$existing = new WP_Query([
|
'post_type' => BASE.'dash',
|
'name' => $this->slug,
|
'fields' => 'ids',
|
'posts_per_page'=> 1,
|
]);
|
if ($existing->have_posts()) {
|
return $existing->posts[0];
|
}
|
|
$args = [
|
'post_title' => $this->title,
|
'post_name' => $this->slug,
|
'post_type' => BASE.'dash',
|
'post_status' => 'publish'
|
];
|
if ($parentID > 0) {
|
$args['post_parent'] = $parentID;
|
}
|
return wp_insert_post($args);
|
}
|
);
|
}
|
public function getID():int
|
{
|
return $this->ID;
|
}
|
|
public function setURL():void
|
{
|
$this->URL = $this->cache->remember(
|
$this->slug.'_url',
|
function () {
|
return get_permalink($this->ID);
|
}
|
);
|
}
|
|
public function getURL():string
|
{
|
return $this->URL;
|
}
|
|
|
public function setTitle(string $title):void
|
{
|
$this->title = $title;
|
}
|
public function getTitle():string
|
{
|
return $this->title;
|
}
|
|
public function setSlug(string $slug):void
|
{
|
$this->slug = self::sanitizeString($slug);
|
}
|
public function getSlug():string
|
{
|
return $this->slug;
|
}
|
|
public function setIcon(string $icon):void
|
{
|
$this->icon = $icon;
|
}
|
public function getIcon():string
|
{
|
return $this->icon;
|
}
|
|
public function setPermission(string $permission):void
|
{
|
$this->permission = $permission;
|
}
|
public function getPermission():?string
|
{
|
return $this->permission;
|
}
|
|
public function setSection(string $section):void
|
{
|
$this->section = self::sanitizeString($section);
|
}
|
public function getSection():?string
|
{
|
return $this->section;
|
}
|
|
public function setOrder(int $order):void
|
{
|
$this->order = $order;
|
}
|
public function getOrder():int
|
{
|
return $this->order;
|
}
|
|
public function setRenderCallback(string $callback):void
|
{
|
|
}
|
public function render():string
|
{
|
return $this->cache->remember(
|
$this->ID,
|
function () {
|
return apply_filters(BASE.'render_'.$this->slug, '<h2>'.$this->title.' is not configured yet.</h2><p>Add a filter to '.BASE.'render_'.$this->slug.'</p>');
|
}
|
);
|
}
|
/*********************************************************
|
* UTILITY
|
********************************************************/
|
public static function sanitizeString(string $string):string
|
{
|
return str_replace('_', '-', strtolower(sanitize_title($string)));
|
}
|
}
|