Jake Vanderwerf
2026-04-15 c4aa5cdb5e90ad4b420e22772797d16980232a2b
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
namespace JVBase;
 
use JVBase\blocks\CustomBlocks;
use JVBase\integrations\BlueSky;
use JVBase\managers\Cache;
use JVBase\managers\EmailManager;
use JVBase\managers\ErrorHandler;
use JVBase\managers\InvitationsManager;
use JVBase\managers\LoginManager;
use JVBase\managers\MagicLinkManager;
use JVBase\managers\queue\Queue;
use JVBase\managers\DashboardManager;
use JVBase\managers\DirectoryManager;
use JVBase\managers\ReferralManager;
use JVBase\managers\RoleManager;
//use JVBase\managers\SchemaManager;
use JVBase\managers\SEO\render\SchemaOutput;
use JVBase\managers\SEO\SchemaOutputManager;
use JVBase\admin\SEOAdmin;
use JVBase\managers\AdminPages;
use JVBase\managers\NotificationManager;
use JVBase\managers\TaxonomyRelationships;
use JVBase\managers\UserTermsManager;
use JVBase\registrar\Registrar;
use JVBase\rest\routes\FeedRoutes;
use JVBase\rest\routes\FavouritesRoutes;
use JVBase\rest\routes\IntegrationsSquareRoutes;
use JVBase\rest\routes\IntegrationsHelcimRoutes;
use JVBase\rest\routes\NotificationsRoutes;
use JVBase\rest\routes\ContentRoutes;
use JVBase\rest\routes\TermRoutes;
use JVBase\rest\routes\UploadRoutes;
//use JVBase\rest\routes\BioRoutes;
use JVBase\rest\routes\SettingsRoutes;
//use JVBase\rest\routes\ShopRoutes;
use JVBase\rest\routes\ContentTermsRoutes;
use JVBase\rest\routes\SEORoutes;
use JVBase\rest\routes\QueueRoutes;
use JVBase\rest\routes\ErrorRoutes;
use JVBase\rest\routes\FormRoutes;
use JVBase\rest\routes\LoginRoutes;
use JVBase\rest\routes\NewsRoutes;
use JVBase\rest\routes\ReferralRoutes;
//use JVBase\rest\routes\MagicLinkRoutes;
use JVBase\rest\routes\ResponseRoutes;
use JVBase\rest\routes\OptionsRoutes;
use JVBase\rest\routes\VoteRoutes;
use JVBase\rest\routes\Invitations;
use JVBase\rest\routes\ApprovalRoutes;
use JVBase\rest\routes\AdminRoutes;
use JVBase\rest\routes\IntegrationsRoutes;
use JVBase\utility\Features;
use JVBase\base\SchemaHelper;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class JVB
{
    protected static JVB|null $instance = null;
    protected array $managers = [];
    protected array $content = [];
    protected array $taxonomies = [];
    protected array $integrations = [];
    protected array $blocks = [];
    protected array $routes = [];
    protected CustomBlocks $customBlocks;
 
    protected array $serviceMap = [
        'maps' => 'JVBase\integrations\GoogleMaps',
        'square' => 'JVBase\integrations\Square',
        'facebook' => 'JVBase\integrations\Facebook',
        'helcim' => 'JVBase\integrations\Helcim',
        'instagram' => 'JVBase\integrations\Instagram',
        'bluesky' => 'JVBase\integrations\BlueSky',
        'gmb' => 'JVBase\integrations\GoogleMyBusiness',
        'cloudflare' => 'JVBase\integrations\Cloudflare',
        'umami' => 'JVBase\integrations\Umami',
        'postmark' => 'JVBase\integrations\PostMark',
    ];
 
    public static function getInstance(): JVB
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
 
 
    public function __construct()
    {
        $this->customBlocks = new CustomBlocks();
        $this->managers = [
            'errors'    => new ErrorHandler(),
            'queue'     => new Queue(),
//            'dash'    => new DashboardManager(),
            'roles'     => new RoleManager(),
//            'forms'   => new FormManager(),
//          'schema'    => new SchemaOutputManager(),
            'admin'     => new AdminPages(),
            'seoAdmin'  => new SEOAdmin(),
            'seo'       => new SchemaOutput(),
            'schemaHelper' => new SchemaHelper(),
//          'uploads'   => new UploadManager(),
            'userTerms' => new UserTermsManager(),
            'email'     => new EmailManager(),
            'terms'     => new TaxonomyRelationships(),
        ];
 
        $this->routes = [
            'login' => new LoginRoutes(),
            'integrations' => new IntegrationsRoutes(),
            'seo' => new SEORoutes(),
            'queue' => new QueueRoutes(),
            'settings' => new SettingsRoutes(),
            'upload' => new UploadRoutes(),
            'forms' => new FormRoutes()
        ];
 
        if (Features::forSite()->has('magicLink')) {
//          $this->routes['magicLink'] = new MagicLinkRoutes();
            $this->managers['magicLink'] = new MagicLinkManager();
        }
        if (Features::forSite()->has('referrals')) {
            $this->managers['referral'] = new ReferralManager();
            $this->routes['referral'] = new ReferralRoutes();
        }
 
        if (Features::forSite()->has('dashboard')) {
            $this->managers['dash'] = new DashboardManager();
        }
 
        if (Features::hasIntegration('square')) {
            $this->routes['square'] = new IntegrationsSquareRoutes();
        }
        if (Features::hasIntegration('helcim')) {
            $this->routes['helcim'] = new IntegrationsHelcimRoutes();
        }
 
        if (Features::forSite()->has('feed_block')) {
            $this->routes['feed'] = new FeedRoutes();
        }
        if (Features::forMembership()->has('notifications')) {
            $this->managers['notifications'] = new NotificationManager();
            $this->routes['notifications'] = new NotificationsRoutes();
        }
        if (Features::forSite()->has('feed_block') || Features::forSite()->has('dashboard')) {
            $this->routes['term'] = new TermRoutes();
        }
 
        if (Features::forSite()->has('is_directory')) {
            $this->managers['directory'] = new DirectoryManager();
        }
 
        if (Features::forSite()->has('dashboard')) {
            $this->routes['error'] = new ErrorRoutes();
            $this->routes['admin']  = new AdminRoutes();
            $this->routes['content'] = new ContentRoutes();
//            $this->routes['bio']    = new BioRoutes();
//          $this->routes['shop'] = new ShopRoutes();
            $this->routes['contentTax'] = new ContentTermsRoutes();
            $this->routes['options'] = new OptionsRoutes();
        }
 
        if (Features::forSite()->has('favourites')) {
            $this->routes['favourites'] = new FavouritesRoutes();
        }
 
        if (Features::forMembership()->has('forum')) {
            $this->routes['news'] = new NewsRoutes();
        }
        if (Features::forMembership()->has('invitable')) {
            $this->managers['invitations'] = new InvitationsManager();
        }
        if (!empty(Registrar::getFeatured('has_responses'))) {
            $this->routes['comments'] = new ResponseRoutes();
        }
        if (!empty(Registrar::getFeatured('karma'))) {
            $this->routes['vote'] = new VoteRoutes();
        }
        if (!empty(Registrar::getFeatured('karma'))
            || Features::forMembership()->has('member_verified') ||
            Features::forMembership()->has('term_approval')) {
            $this->routes['approvals'] = new ApprovalRoutes();
        }
        if (Features::forMembership()->has('can_invite')) {
            $this->routes['invites'] = new Invitations();
        }
 
        $this->setupIntegrations();
 
        add_action('wp_footer', [$this, 'additionalActions']);
//        $this->managers['notifications'] = new NotificationManager();
        // Register activation hook
        register_activation_hook(JVB_DIR . '/jvb.php', [$this, 'activate']);
    }
 
 
    protected function setupIntegrations(): void
    {
        if (array_key_exists('integrations', JVB_SITE)) {
            foreach (JVB_SITE['integrations'] as $service => $use) {
                if (!$use) {
                    continue;
                }
                if (array_key_exists($service, $this->serviceMap)) {
                    $this->integrations[$service] = new $this->serviceMap[$service]();
                }
 
            }
        }
    }
 
    public function registeredContent(): array
    {
        return array_merge(array_keys($this->content), array_keys($this->taxonomies));
    }
 
    public function dashboard(): DashboardManager|false
    {
        return $this->managers['dash'] ?? false;
    }
 
    public function directories(): DirectoryManager|false
    {
        return $this->managers['directory'] ?? false;
    }
 
    public function error(): ErrorHandler
    {
        return $this->managers['errors'];
    }
 
    public function file()
    {
        return $this->managers['file'];
    }
 
    public function queue(): Queue
    {
        return $this->managers['queue'];
    }
//    public function forms()
//    {
//        return $this->managers['forms'];
//    }
    public function notification(): NotificationManager|false
    {
        return $this->managers['notifications'] ?? false;
    }
 
    public function routes($route): mixed
    {
        if (array_key_exists($route, $this->routes)) {
            return $this->routes[$route];
        }
        return false;
    }
 
    public function roles(): RoleManager
    {
        return $this->managers['roles'];
    }
 
    public function admin()
    {
        return $this->managers['admin'];
    }
 
    public function seoAdmin()
    {
        return $this->managers['seoAdmin'];
    }
 
    public function getFields($type): array
    {
        return Registrar::getFieldsFor($type)??[];
    }
 
    public function getContent($type): mixed
    {
        return $this->content[$type] ?? $this->taxonomies[$type] ?? $this->blocks[$type] ?? null;
    }
 
    public function connect(string $service, ?int $userID = null): mixed
    {
        if ($userID) {
            if (!$this->userCanConnect($service, $userID)) {
                return null;
            }
 
            if (!array_key_exists($service, $this->integrations)) {
                return null;
            }
            return new $this->serviceMap[$service]($userID);
        }
        return (array_key_exists($service, $this->integrations)) ? $this->integrations[$service] : null;
    }
 
    public function userCanConnect(string $service, int $userID): bool
    {
        $allowed = JVB_USER[jvbUserRole($userID)]['integrations'] ?? [];
        return user_can($userID, 'manage_options') || in_array($service, $allowed);
    }
 
    public function getAvailableServices(bool $keys = true): array
    {
 
        return ($keys) ? array_keys($this->integrations) : $this->integrations;
    }
 
    public function activate(): void
    {
        // Activate roles - will be properly initialized after post types are registered
        $this->roles()->activate();
    }
 
    public function addRoute($slug, $class): void
    {
        $this->routes[$slug] = $class;
    }
 
    public function email(): EmailManager
    {
        return $this->managers['email'];
    }
 
    public function referrals(): ReferralManager|false
    {
        return $this->managers['referral'] ?? false;
    }
 
    public function magicLink(): MagicLinkManager|false
    {
        return $this->managers['magicLink'] ?? false;
    }
 
    public function invitations(): InvitationsManager|false
    {
        return $this->managers['invitations'] ?? false;
    }
 
    public function additionalActions():void
    {
        if (LoginManager::isLogin()) {
            return;
        }
        $extras = apply_filters('jvbAdditionalActions', []);
        $extras = array_filter($extras, function ($extra) {
            return is_array($extra) && array_key_exists('button', $extra) && array_key_exists('content', $extra);
        });
        if (empty ($extras)) {
            return;
        }
        $buttons = array_map(function ($extra) {
            return $extra['button'];
        }, $extras);
        $contents = array_map(function($extra) {
            return $extra['content'];
        }, $extras);
 
        if (!empty ($buttons)) {
            ?>
            <section class="additional-actions">
                <div class="buttons col">
                    <?= implode($buttons); ?>
                </div>
                <div class="actions">
                    <?= implode($contents); ?>
                </div>
            </section>
            <?php
        }
    }
 
    public function seo():SchemaOutput
    {
        return $this->managers['seo'];
    }
 
    public function blocks():CustomBlocks|bool
    {
        return $this->customBlocks??false;
    }
    public function schemaHelper():SchemaHelper
    {
        return $this->managers['schemaHelper'];
    }
 
    public function termRelationships():TaxonomyRelationships
    {
        return $this->managers['terms'];
    }
}