Jake Vanderwerf
2026-02-11 427efb903c2f23c5ddc05ce75d869fdb187d771d
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
<?php
 
use JVBase\integrations\Umami;
use JVBase\managers\Cache;
use JVBase\managers\ReferralManager;
use JVBase\managers\SEO\SEOAdminPage;
use JVBase\utility\Features;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
function jvbActivatePlugin():void
{
    $validator = new JVBase\utility\Validator();
    $validation = $validator->validateAll();
    error_log('Validation result: '.print_r($validation, true));
    $checker = JVBase\utility\Checker::getInstance();
    error_log('Doing our activation action...');
    // Get ContentRegistry instance early
    $registry = new JVBase\registry\ContentRegistry();
    // Initialize content types
    $registry->onActivation();
    $checker->invalidateContentCache();
    $checker->invalidateTaxonomyCache();
    do_action(BASE.'activation');
    error_log('Action done!');
    error_log('Checking custom tables...');
    (new JVBase\registry\CheckCustomTables())->maybeCreateTables();
    error_log('Tables created!');
 
    // Store schema version
    update_option('jvb_db_version', '1.0.0');
    error_log('Starting schedules...');
    jvbSchedules();
    error_log('Schedules done!');
    error_log('checking Admin capabilities...');
    jvbAddAdminCaps();
    error_log('Admin caps done!');
    error_log('Removing unneeded roles...');
    remove_role('contributor');
    remove_role('author');
    remove_role('editor');
    error_log('Roles removed!');
    error_log('New Roles done!');
    jvbRegisterAdminPages();
    if (array_key_exists('integrations', JVB_SITE) && array_key_exists('umami', JVB_SITE['integrations']) && JVB_SITE['integrations']['umami']=== true) {
        error_log('Adding Umami tables');
        Umami::createTables();
    }
 
    JVB()->directories()->activate();
    error_log('Activation done! Huzzah!');
}
 
function jvbAddAdminCaps()
{
    $roleManager = new \JVBase\managers\RoleManager();
 
    $role = get_role('administrator');
    $users = get_users(['role' => 'administrator']);
    foreach (JVB_CONTENT as $slug => $config) {
 
        $plural = strtolower($config['plural']);
        $capabilities = [
            'edit_' . $plural,
            'edit_published_' . $plural,
            'publish_' . $plural,
            'delete_' . $plural,
            'delete_published_' . $plural,
            'edit_others_' . $plural,
            'delete_others_' . $plural,
            'read_private_' . $plural,
            'edit_private_' . $plural,
            'delete_private_' . $plural,
        ];
 
        foreach ($capabilities as $cap) {
            $role->add_cap($cap, true);
        }
 
        foreach ($users as $user) {
            $roleManager->grantContent($user, $slug);
            $roleManager->grantOthersContent($user, $slug);
        }
    }
    add_action('init', 'jvbGrantAdminUserCaps', 99);
}
/**
 * Grant capabilities to admin users after post types are registered
 * This runs on init with high priority to ensure post types exist
 */
function jvbGrantAdminUserCaps()
{
    // Only run once after activation
    if (get_option('jvb_admin_caps_granted') === '1') {
        return;
    }
 
    $roleManager = new \JVBase\managers\RoleManager();
    $users = get_users(['role' => 'administrator']);
 
    foreach (JVB_CONTENT as $slug => $config) {
        foreach ($users as $user) {
            // These methods should check if post type exists before adding caps
            $roleManager->grantContent($user, $slug);
            $roleManager->grantOthersContent($user, $slug);
        }
    }
 
    // Mark as complete to prevent running again
    update_option('jvb_admin_caps_granted', '1');
    remove_action('init', 'jvbGrantAdminUserCaps', 99);
}
function jvbScheduledHooks()
{
    return [
        'jvb_daily_reset'           => [
            'time'          => '00:01',
            'recurrence'    => 'daily'
        ],
        'jvb_process_queue'          =>
            [
                'recurrence' => 'every-minute',
            ],
        'jvb_queue_maintenance' => [
            'time'  => '1:05am',
            'recurrence'    => 'hourly'
        ],
        'jvbEmailDailyMetricsReport' =>
            [
                'time'         => '4:15am tomorrow'
            ],
        'jvb_generate_daily_report'  =>
            [
                'time'          => '1:20am tomorrow',
            ],
        'jvb_daily_error_summary'    =>
            [
                'time'          => '7:01am tomorrow',
            ],
        'jvb_cleanup_expired_approvals' => [],
        'jvb_cleanupOrphanedFavourites' => [],
        'jvb_daily_maintenance'         => [
            'time'              => '12:03am tomorrow',
        ],
        //NotificationManager.php
        'jvb_notification_digest_daily' =>
            [
                'time'  => '8:08am tomorrow',
            ],
        'jvb_notification_digest_weekly' =>
            [
                'time'       => 'monday 6:07am',
                'recurrence' => 'weekly',
            ],
        'jvb_notification_digest_monthly' =>
            [
                'time'       => '2025-05-05 9:00am',
                'recurrence' => 'monthly',
            ],
 
    ];
}
 
function jvbSchedules()
{
    $hooks = jvbScheduledHooks();
    //TODO: ensure we actually need these
    foreach ($hooks as $hook => $options) {
        $time = (array_key_exists('time', $options)) ? strtotime($options['time']) : time();
 
        $recurrence = (array_key_exists('recurrence', $options)) ? $options['recurrence'] : 'daily';
 
        if (!wp_next_scheduled($hook)) {
            $result = wp_schedule_event($time, $recurrence, $hook);
            if (!$result) {
                error_log('Did not schedule '.$hook.' successfully...'.print_r($result, true));
                error_log('Time: '.print_r($time, true));
                error_log('Recurrence: '.print_r($recurrence, true));
            }
        }
    }
}
 
function jvbDeactivatePlugin()
{
    jvbClearSchedules();
    jvbDeleteOptions();
    jvbDeleteDashboard();
    jvbDeleteDirectories();
    Cache::flushAll();
    do_action('jvbDeactivate');
}
 
function jvbDeleteOptions():void
{
    $options = array_filter(
        wp_load_alloptions(),
        fn ($key) => str_starts_with($key, BASE) ||
                     str_starts_with($key, '_transient_'.BASE) ||
                     str_starts_with($key, '_transient_timeout_'.BASE),
        ARRAY_FILTER_USE_KEY
    );
    foreach ($options as $key => $value) {
        delete_option($key);
    }
}
function jvbDeleteDashboard()
{
    $pages = new WP_Query([
        'post_type' => BASE.'dash',
        'posts_per_page'    => -1,
        'post_status'   => 'any',
        'fields'    => 'ids'
    ]);
    if ($pages->have_posts()) {
        foreach ($pages->posts as $ID) {
            wp_delete_post($ID, true);
        }
    }
}
function jvbDeleteDirectories()
{
    $pages = new WP_Query([
        'post_type' => BASE.'directory',
        'posts_per_page'    => -1,
        'post_status'   => ['publish', 'draft','trash'],
        'fields'    => 'ids'
    ]);
    foreach ($pages->posts as $ID) {
        wp_delete_post($ID, true);
    }
 
}
function jvbClearSchedules()
{
    $hooks = array_keys(jvbScheduledHooks());
    foreach ($hooks as $hook) {
        wp_clear_scheduled_hook($hook);
    }
}
 
// Optional: Add upgrade routine
function jvbMaybeUpgrade():void
{
    $current_version = get_option('jvb_db_version', '0');
    if (version_compare($current_version, '1.0.0', '<')) {
        jvbActivatePlugin();
    }
}
add_action('plugins_loaded', 'jvbMaybeUpgrade');
 
 
add_action('jvb_daily_reset', 'jvbDailyResetAction');
function jvbDailyResetAction(): void
{
    do_action('jvbDailyReset');
    error_log('Daily options reset completed at ' . current_time('Y-m-d H:i:s'));
}
 
 
function jvbRegisterAdminPages():void
{
    if (Features::forSite()->has('referrals')){
        ReferralManager::addSubpage();
    }
    SEOAdminPage::addSubpage();
}