Jake Vanderwerf
5 days ago 0dfe1d8afafc59c4a5559c498342668d5a58d6ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace JVBase\managers\Dashboard;
 
use JVBase\managers\Cache;
use JVBase\registrar\Registrar;
use WP_Query;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class DashboardPage {
    protected string $title;
    protected string $slug;
    protected string $ogSlug;
    protected string $icon;
    protected string $URL;
    protected int $parent = 0;
    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');
        if (JVB_TESTING) {
            $this->cache->flush();
        }
        $this->title = $title;
        $this->setSlug(empty($slug) ? $title : $slug);
        $this->icon = $icon;
        $this->parent = $parent;
        $this->setID($parent);
        $this->setURL();
    }
 
    public function setID(int $parentID):void
    {
        if ($this->slug === 'dash') {
            //We do not need to create the page for the main dash
            $this->ID = 0;
            return;
        }
        $this->ID = $this->cache->remember(
            $this->slug.'_ID',
            function() use ($parentID) {
                $existing = new WP_Query([
                    'post_type' => DashboardManager::$based,
                    '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'     => DashboardManager::$based,
                    'post_status'   => 'publish'
                ];
                if ($parentID > 0) {
                    $args['post_parent'] = $parentID;
                }
                return wp_insert_post($args);
            }
        );
    }
    public function getID():int
    {
        return $this->ID;
    }
 
    public function getParent():int
    {
        return $this->parent;
    }
 
    public function setURL():void
    {
 
        if ($this->slug === 'dash') {
            $this->URL = get_post_type_archive_link(DashboardManager::$based);
            return;
        }
        $this->URL = get_the_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->ogSlug = $slug;
        $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
    {
        $check = str_replace('-','_',$this->slug);
        ob_start();
        do_action(BASE.'dashboard_page_'.$check, $this->slug, $this->ogSlug);
        $out = ob_get_clean();
 
        if (empty($out)) {
            $out = sprintf(
                '<h1>%s</h1><p>It doesn\'t look like this page is configured yet: %s.</p>',
                $this->title,
                BASE.'dashboard_page_'.$check
            );
        }
        return $out;
    }
    /*********************************************************
     * UTILITY
     ********************************************************/
    public static function sanitizeString(string $string):string
    {
        return str_replace('_', '-', strtolower(sanitize_title($string)));
    }
 
    public function setScripts(array $scripts):void
    {
        $this->scripts = $scripts;
    }
    public function getScripts():array
    {
        return $this->scripts;
    }
}