Jake Vanderwerf
2026-05-12 16cb63b05910055c31dca821c86f2eb815da99e3
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
<?php
namespace JVBase\managers;
 
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
/**
 * Forum Manager
 * TODO
 * Handles a forum functionality
 */
class ForumManager
{
    protected CustomTable $relationships;
    protected CustomTable $forum;
    protected KarmaManager $karma;
 
    public function __construct()
    {
        $this->defineTables();
        $this->registerHooks();
    }
    public function registerHooks():void
    {
        add_action('jvbDefineRegistrar', [$this, 'registerForum']);
        add_action('jvbDefineRegistrarFields', [$this, 'registerForumFields']);
        add_action('plugins_loaded', [$this, 'registerForum'], 1);
        add_action('plugins_loaded', [$this, 'registerForum'], 2);
    }
        public function registerForum():void
        {
            Registrar::forPost('jvbforum', 'Post', 'Posts')
                ->setIcon('note')
                ->make([
                    'public'    => false,
                    'taxonomies' => [
                        'topic'
                    ]
                ]);
 
            Registrar::forTerm('jvbtopic', 'Topic', 'Topics')
                ->setIcon('folder')
                ->make([
                    'hierarchical' => true,
                    'for'   => ['jvbforum']
                ]);
        }
        public function registerForumFields():void
        {
            $forum = Registrar::getInstance('jvbforum');
            $fields = $forum->fields();
 
            $topic = Registrar::getInstance('jvbtopic');
            $topicFields = $topic->fields();
        }
 
 
    protected function defineTables():void
    {
        $this->definedRelationshipsTable();
        $this->karma = KarmaManager::for('forum', 'post');
    }
        protected function definedRelationshipsTable():void
        {
            $table = CustomTable::for('forum_relationship');
 
            $table->setColumns([
                'id'    => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT',
                'term_id'   => "{$table->getTermIDType()} NOT NULL",
                'user_id'   => "{$table->getUserIDType()} NOT NULL",
                'profile_id'=> "{$table->getPostIDType()} NOT NULL",
                'forum_count'   => 'int(10) unsigned NOT NULL',
                'last_post_date'=> 'datetime DEFAULT NULL',
                'updated_at'    => 'timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
            ]);
 
            $table->setKeys([
                ['key' => 'PRIMARY', 'value' => '(`id`)'],
                ['key' => 'UNIQUE', 'value' => '`term_user` (`term_id`, `user_id`)'],
                '`term_id` (`term_id`)',
                '`user_id` (`user_id`)',
                '`profile_id` (`profile_id`)',
            ]);
            $base = BASE;
            $table->setConstraints([
                "CONSTRAINT `{$base}fr_term_forum_term` FOREIGN KEY (`term_id`)
                REFERENCES `{$table->getTermTable()}` (`term_id`) ON DELETE CASCADE",
                "CONSTRAINT `{$base}fr_term_forum_user` FOREIGN KEY (`user_id`)
                REFERENCES `{$table->getUserTable()}` (`ID`) ON DELETE CASCADE",
                "CONSTRAINT `{$base}fr_term_news_profile` FOREIGN KEY (`profile_id`)
                REFERENCES `{$table->getPostTable()}` (`ID`) ON DELETE CASCADE",
            ]);
 
            $table->defineTable();
            $this->relationships = $table;
        }
}