Jake Vanderwerf
2026-01-25 b38f03c0e7218762d90fa5092696b127f24f36db
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
<?php
 
namespace JVBase\rest\routes;
 
use JVBase\JVB;
use JVBase\rest\RestRouteManager;
use JVBase\managers\Cache;
use JVBase\utility\Features;
use WP_User;
use WP_REST_Request;
use WP_REST_Response;
use Exception;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
class ApprovalRoutes extends RestRouteManager
{
    protected array $userTypes;
    protected array $termTypes;
    protected array $allTypes;
    protected array $requestTables;
    protected array $voteTables;
 
    protected int $expiryDays = 7;
    protected bool $hasMemberApproval = false;
 
    public function __construct()
    {
        $this->cache_name = 'approvals';
        $this->hasMemberApproval = Features::forMembership()->has('member_verified');
        parent::__construct();
 
        $this->initTypes();
 
        if ($this->hasMemberApproval) {
            add_action('user_register', [$this, 'handleNewUserRegistration'], 10, 2);
        }
 
        add_action('jvb_cleanup_expired_approvals', [$this, 'cleanupExpiredApprovals']);
    }
 
    protected function initTypes():void
    {
        $approvals = jvbApprovalTypes();
        $this->userTypes = [];
        $this->termTypes = [];
        if ($this->hasMemberApproval) {
            $this->userTypes = array_filter(
                array_keys($approvals),
                function ($item) {
                    return $item !== 'term';
                }
            );
            $this->allTypes = $this->userTypes;
        }
        if (jvbSiteHasTermApproval()) {
            $this->termTypes = $approvals['term']??[];
            $this->allTypes[] = 'term';
        }
    }
 
    public function registerRoutes():void
    {
        register_rest_route($this->namespace, '/approvals', [
            [
                'methods'             => 'GET',
                'callback'            => [ $this, 'getApprovals' ],
                'permission_callback' => [ $this, 'checkPermission' ]
            ],
            [
                'methods'             => 'POST',
                'callback'            => [ $this, 'handleApprovalAction' ],
                'permission_callback' => [ $this, 'checkPermission' ]
            ]
        ]);
    }
 
    /**
     * @param WP_REST_Request $request The REST request
     *
     * @return bool
     */
    public function checkPermission(WP_REST_Request $request):bool
    {
        $userID = get_current_user_id();
        if (!user_can($userID, 'skip_moderation')) {
            return false;
        }
 
        return parent::checkPermission($request);
    }
 
 
    /**
     * Handler for user registration
     *
     * @param int $user_id New user ID
     * @param object $user the new user object
     *
     * @return void
     */
    public function handleNewUserRegistration(int $user_id, object $user):void
    {
        $intersect = array_intersect(
            array_map(
                function ($role) {
                    return BASE.$role;
                },
                $this->userTypes
            ),
            (array) $user->roles
        );
        if (!empty($intersect)) {
            // Mark as unverified initially
            $user->add_cap('skip_moderation', false);
            // Create approval request
            $this->createArtistApprovalRequest($user_id);
        }
    }
 
 
    /**
     * @param WP_REST_Request $request
     *
     * @return WP_REST_Response
     */
    public function handleApprovalAction(WP_REST_Request $request):WP_REST_Response
    {
        $data       = $request->get_params();
        $request_id = $data['request_id'] ?? 0;
        $user_id    = (array_key_exists('user', $data) &&
                       is_numeric($data['user'])) ?
                        (int) $data['user'] : get_current_user_id();
        $action     = (array_key_exists('action', $data) && in_array($data['action'], [
                'approve',
                'reject'
            ])) ? $data['action'] : false;
 
        $type = (array_key_exists('type', $data) &&
                 in_array($data['type'], $this->allTypes)) ?
            $data['type'] :
            false;
        $notes = (array_key_exists('notes', $data)) ? sanitize_text_field($data['notes']) : '';
 
        if ($action && $request_id !== 0 && $type) {
            $result = $this->handleVote($type, $action, $request_id, $user_id, $notes);
            return new WP_REST_Response([
                'success' => $result,
                'message' => $result ? 'Vote recorded successfully' : 'Failed to record vote'
            ], $result ? 200 : 500);
        }
        return new WP_REST_Response([
            'success' => false,
            'message' => 'Invalid action or request ID'
        ], 400);
    }
 
    protected function getRequestTable(string $type, string $prefix):string
    {
        return match ($type) {
            'term' => $prefix . BASE . 'approval_term_requests',
            default => $prefix . BASE . 'approval_' . $type . '_requests',
        };
    }
    protected function getVoteTable(string $type, string $prefix):string
    {
        return match ($type) {
            'term' => $prefix . BASE . 'approval_term_votes',
            default => $prefix . BASE . 'approval_' . $type . '_votes',
        };
    }
    /**
     * Artist and Term Approvals
     */
    protected function handleVote(string $type, string $vote, int $request_id, int $user_id, string $notes = ''):bool
    {
        if (!in_array($vote, ['approve', 'reject'])) {
            return false;
        }
        global $wpdb;
        $table = $this->getRequestTable($type, $wpdb->prefix);
        $votes = $this->getVoteTable($type, $wpdb->prefix);
 
 
        try {
            $request = $wpdb->get_row($wpdb->prepare(
                "SELECT * FROM $table WHERE id = %d",
                $request_id
            ));
            if (!$request || $request->status !== 'pending') {
                throw new Exception("Invalid approval request");
            }
 
            $already_voted = $wpdb->get_row($wpdb->prepare(
                "SELECT * FROM $votes WHERE request_id = %d AND user_id = %d",
                $request_id,
                $user_id
            ));
 
            if ($already_voted && $already_voted->vote !== $vote) {
                $wpdb->update(
                    $votes,
                    [
                        'vote' => $vote,
                    ],
                    [
                        'id' => $already_voted->id
                    ]
                );
                return true;
            } elseif ($already_voted) {
                throw new Exception("User has already voted on this request");
            }
 
            $result = $wpdb->insert(
                $votes,
                [
                    'request_id' => $request_id,
                    'user_id'    => $user_id,
                    'vote'       => $vote,
                    'notes'      => $notes,
                    'created_at' => current_time('mysql')
                ]
            );
            if (!$result) {
                throw new Exception("Failed to record vote");
            }
 
            $user = get_userdata($user_id);
            if ($vote === 'approve') {
                $approvers = json_decode($request->approved_by, true)?:[];
 
                $approvers[$user_id] = [
                    'name'  => $user->display_name,
                    'voted' => current_time('mysql')
                ];
                $wpdb->update(
                    $table,
                    [
                        'current_approvals' => $request->current_approvals + 1,
                        'updated_at'        => current_time('mysql'),
                        'approved_by'       => $approvers,
                        'expires_at'       => $this->rebuildExpiryDate()
                    ],
                    [
                        'id' => $request_id
                    ]
                );
                if ($request->current_approvals + 1 >= $request->required_approvals) {
                    switch ($type) {
                        case 'user':
                        case 'artist':
                            $this->completeVerification($request_id);
                            break;
                        case 'term':
                            $this->makeTermLive($request);
                            break;
                    }
                }
            } elseif ($vote === 'reject') {
                $rejecters = json_decode($request->rejected_by, true)?:[];
 
                $rejecters[$user_id] = [
                    'name'  => $user->display_name,
                    'voted' => current_time('mysql')
                ];
                $wpdb->update(
                    $table,
                    [
                        'current_rejections'    => $request->current_rejections + 1,
                        'rejected_by'           => $rejecters,
                        'updated_at'            => current_time('mysql'),
                        'expires_at'            => $this->rebuildExpiryDate()
                    ],
                    [
                        'id'    => $request_id
                    ]
                );
                if ($request->current_rejections + 1 >= $request->required_approvals) {
                    switch ($type) {
                        case 'user':
                        case 'artist':
                            $this->denyVerification($request_id);
                            break;
                        case 'term':
                            $this->makeTermUnalive($request);
                            break;
                    }
                }
            }
 
            $wpdb->query('COMMIT');
 
            return true;
        } catch (Exception $e) {
            $wpdb->query('ROLLBACK');
 
            JVB()->error()
                ->log(
                    '[ApprovalRoutes]:handleVote',
                    "Error creating '.$type.' approval request: " . $e->getMessage(),
                    [
                        'user_id'   => $user_id,
                        'request_id' => $request_id,
                        'vote'       => $vote
                    ]
                );
            return false;
        }
    }
    protected function rebuildExpiryDate()
    {
        return date('Y-m-d H:i:s', strtotime("+{$this->expiryDays} days", time()));
    }
    /**
     * @param string $type user/artist or term
     * @param array $request
     *
     * @return bool|int
     */
    protected function createApprovalRequest(string $type, array $request):bool|int
    {
        global $wpdb;
 
        $table = $this->getRequestTable($type, $wpdb->prefix);
 
        $result = $wpdb->insert(
            $table,
            $request
        );
 
        if (!$result) {
            throw new Exception($wpdb->last_error);
        }
        return $wpdb->insert_id;
    }
 
    /*************
     * Artist Approvals
     ************/
    /**
     * Create artist approval request
     *
     * @param int $user_id User ID to be approved
     *
     * @return int|false Request ID or false on failure
     */
    public function createArtistApprovalRequest(int $user_id):int|false
    {
        global $wpdb;
        $wpdb->query('START TRANSACTION');
 
        try {
            //Check for existing first
            $table = $this->getRequestTable(jvbUserRole($user_id), $wpdb->prefix);
 
            // Verify this is not a duplicate request
            $existing = $wpdb->get_var($wpdb->prepare(
                "SELECT id FROM $table
         WHERE user_id = %d",
                $user_id
            ));
 
            if ($existing) {
                return $existing;
            }
 
            $user_data = get_userdata($user_id);
            $request = [
                'user_id'            => $user_id,
                'status'             => 'pending',
                'expires_at'         => date('Y-m-d H:i:s', strtotime('+30 days')),
                'created_at'         => current_time('mysql'),
                'updated_at'         => current_time('mysql'),
                'name'               => $user_data->display_name,
                'email'              => $user_data->user_email,
            ];
 
            $result = $this->createApprovalRequest('user', $request);
 
            if (!$result) {
                throw new Exception($wpdb->last_error);
            }
 
            $wpdb->query('COMMIT');
 
            return $result;
        } catch (Exception $e) {
            $wpdb->query('ROLLBACK');
            JVB()->error()
                ->log(
                    '[ApprovalRoutes]:createArtistApprovalRequest',
                    "Error creating artist approval request: " . $e->getMessage(),
                    [
                        'user_id'   => $user_id,
                    ]
                );
 
            return false;
        }
    }
 
    /**
     * Mark an artist as verified
     *
     * @param int $user_id The user to verify
     * @param int $verified_by ID of user who verified them (optional)
     *
     * @return bool Success status
     */
    public function verifyArtist(int $user_id, int $verified_by = 0):bool
    {
        $user = get_userdata($user_id);
 
        // Check if user has the artist role
        if (!array_intersect(array_map(function ($role) { return BASE.$role; }, $this->userTypes), $user->roles)) {
            return false;
        }
 
        // Add the capability
        $user->add_cap('skip_moderation', true);
 
        // Store verification metadata
        update_user_meta($user_id, BASE . 'verification_date', current_time('mysql'));
        if ($verified_by) {
            update_user_meta($user_id, BASE . 'verified_by', $verified_by);
        }
 
        return true;
    }
 
    /**
     * Mark an artist as verified
     *
     * @param int $user_id The user to verify
     * @param int $verified_by ID of user who verified them (optional)
     *
     * @return bool Success status
     */
    public function unverifyArtist(int $user_id, int $verified_by = 0):bool
    {
        $user = get_userdata($user_id);
 
        // Check if user has the artist role
        if (!array_intersect(array_map(function ($role) { return BASE.$role; }, $this->userTypes), $user->roles)) {
            return false;
        }
 
        // Add the capability
        $user->add_cap('skip_moderation', false);
 
        // Store verification metadata
        update_user_meta($user_id, BASE . 'unverification_date', current_time('mysql'));
        if ($verified_by) {
            update_user_meta($user_id, BASE . 'unverified_by', $verified_by);
        }
 
        return true;
    }
 
    /**
     * Record an approval vote for an artist
     *
     * @param int $user_id User casting the approval vote
     * @param int $request_id The approval request ID
     * @param string $vote 'approve' or 'reject'
     * @param string $notes Optional notes for the vote
     *
     * @return bool Success status
     */
    public function voteForArtist(int $user_id, int $request_id, string $vote, string $notes = ''):bool
    {
        return $this->handleVote(jvbUserRole($user_id), $vote, $request_id, $user_id, $notes);
    }
 
    /**
     * Mark an artist as verified after receiving required approvals
     *
     * @param int $request_id The approval request ID
     *
     * @return bool Success status
     */
    public function completeVerification(int $request_id):bool
    {
        global $wpdb;
        $approval_table = $wpdb->prefix . $this->userRequests;
 
        // Get the request details
        $request = $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM $approval_table WHERE id = %d",
            $request_id
        ));
 
        if (!$request || $request->status !== 'pending') {
            return false;
        }
 
        // Check if enough approvals have been collected
        if ($request->current_approvals < $request->required_approvals) {
            return false;
        }
 
        // Start a transaction
        $wpdb->query('START TRANSACTION');
 
        try {
            // Get the user ID from the request
            $user_id = $request->user_id;
 
            $this->verifyArtist($user_id, $request->current_approvals);
 
            // Update the request status
            $updated = $wpdb->update(
                $approval_table,
                [
                    'status'     => 'approved',
                    'updated_at' => current_time('mysql')
                ],
                [ 'id' => $request_id ]
            );
 
            if ($updated === false) {
                throw new Exception("Failed to update approval request status");
            }
 
            // Notify the user they've been verified
            JVB()->notification()->addNotification(
                $user_id,
                'artist_approved',
                [
                    'request_id'     => $request_id,
                    'approval_date'  => current_time('mysql')
                ]
            );
 
            $wpdb->query('COMMIT');
 
            return true;
        } catch (Exception $e) {
            $wpdb->query('ROLLBACK');
            JVB()->error()
                ->log(
                    '[ApprovalRoutes]:completeVerification',
                    "Error verifying user: " . $e->getMessage(),
                    [
                        'user_id'   => $user_id,
                    ]
                );
 
            return false;
        }
    }
 
    public function denyVerification(int $request_id):bool
    {
        global $wpdb;
        $approval_table = $wpdb->prefix . $this->userRequests;
 
        // Get the request details
        $request = $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM $approval_table WHERE id = %d",
            $request_id
        ));
 
        if (!$request || $request->status !== 'pending') {
            return false;
        }
 
        // Check if enough approvals have been collected
        if ($request->current_rejections < $request->required_approvals) {
            return false;
        }
 
        // Start a transaction
        $wpdb->query('START TRANSACTION');
 
        try {
            // Get the user ID from the request
            $user_id = $request->user_id;
 
            $this->unverifyArtist($user_id, $request->rejected_by);
 
            // Update the request status
            $updated = $wpdb->update(
                $approval_table,
                [
                    'status'     => 'rejected',
                    'updated_at' => current_time('mysql')
                ],
                [ 'id' => $request_id ]
            );
 
            if ($updated === false) {
                throw new Exception("Failed to update approval request status");
            }
 
            // Notify the user they've been verified
            JVB()->notification()->addNotification(
                $user_id,
                'artist_rejected',
                [
                    'request_id'     => $request_id,
                    'approval_date'  => current_time('mysql')
                ]
            );
 
            $wpdb->query('COMMIT');
 
            return true;
        } catch (Exception $e) {
            $wpdb->query('ROLLBACK');
            JVB()->error()
                ->log(
                    '[ApprovalRoutes]:denyVerification',
                    "Error removing artist verification status: " . $e->getMessage(),
                    [
                        'user_id'   => $user_id
                    ]
                );
 
            return false;
        }
    }
 
    /**
     * Get verification details for a request
     *
     * @param int $requestID the request ID
     * @param string $type Type
     *
     * @return array|false Verification details or false if not verified
     */
    public function getVerificationDetails(int $requestID, string $type):array|false
    {
        global $wpdb;
 
        $approval_table = $this->getRequestTable($type, $wpdb->prefix);
        $votes_table = $this->getVoteTable($type, $wpdb->prefix);
 
        // Get the approval request
        $request = $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM $approval_table
         WHERE id = %d
         ORDER BY updated_at DESC",
            $requestID
        ), ARRAY_A);
 
        if (!$request) {
            return false;
        }
 
        // Get the votes for this request
        $votes = $wpdb->get_results($wpdb->prepare(
            "SELECT v.*, u.display_name as approver_name
         FROM $votes_table v
         WHERE v.request_id = %d
         ORDER BY v.created_at",
            $request['id']
        ), ARRAY_A);
 
        return [
            'request'           => $request,
            'votes'             => $votes,
            'verification_date' => $request['updated_at'],
        ];
    }
 
    /*************
     * Term Approvals
     ************/
    public function voteForTerm(int $user_id, int $request_id, string $vote, string $notes = ''):bool
    {
        return $this->handleVote('term', $vote, $user_id, $request_id, $notes);
    }
 
    /**
     * Publish an approved term
     *
     * @param object $request Approval request object
     *
     * @return boolean Success or failure
     */
    protected function makeTermLive(object $request):bool
    {
        global $wpdb;
 
        try {
            // Get term data from request
            $taxonomy         = $request->taxonomy;
            $term_name        = $request->name;
            $parent           = $request->parent;
 
            $result = wp_insert_term($term_name, $taxonomy, [
                'parent'    => $parent
            ]);
 
            if (is_wp_error($result)) {
                throw new Exception($result->get_error_message());
            }
            $term_id = $result['term_id'];
 
            $table = $this->getRequestTable('term', $wpdb);
            // Update request status
            $wpdb->update(
                $table,
                [
                    'status'     => 'approved',
                    'updated_at' => current_time('mysql'),
                    'created_term' => $term_id
                ],
                [ 'id' => $request->id ]
            );
 
            $userIDs = [];
            $approvedBy = [];
            $approvors = json_decode($request->approved_by, true) ?: [];
            $requesters = json_decode($request->requested_by, true) ?: [];
            $rejectors = json_decode($request->rejected_by, true) ?: [];
            foreach (array_merge($requesters, $approvors, $rejectors) as $user_id => $info) {
                $userIDs[] = $user_id;
            }
            foreach ($approvors as $user_id => $info) {
                $approvedBy[] = $info['name'];
            }
 
            $approvedBy = jvbCommaList($approvedBy);
 
            // Notify the requester
            JVB()->notification()->addNotification(
                $userIDs,
                'term_approved',
                [
                    'term_id'     => $term_id,
                    'term_name'   => $term_name,
                    'taxonomy'    => $taxonomy,
                    'approved_by' => $approvedBy
                ]
            );
 
            return true;
        } catch (Exception $e) {
            JVB()->error()
                ->log(
                    '[ApprovalRoutes]:makeTermLive',
                    "Error making term live: " . $e->getMessage(),
                    [
                        'request_id'    => $request->id,
                        'requester'     => $request->requested_by,
                        'term_name'     => $term_name,
                        'taxonomy'      => $taxonomy
                    ]
                );
 
            return false;
        }
    }
    /**
     * Reject a proposed term
     *
     * @param object $request request object
     *
     * @return boolean Success or failure
     */
    protected function makeTermUnalive(object $request):bool
    {
        global $wpdb;
 
        try {
            // Update request status
            $wpdb->update(
                $this->getRequestTable('term', $wpdb),
                [
                    'status'     => 'rejected',
                    'updated_at' => current_time('mysql'),
                ],
                [ 'id' => $request->id ]
            );
 
            $userIDs = [];
            $rejectedBy = [];
 
            $approvors = json_decode($request->approved_by, true) ?: [];
            $requesters = json_decode($request->requested_by, true) ?: [];
            $rejectors = json_decode($request->rejected_by, true) ?: [];
            foreach (array_merge($requesters, $approvors, $rejectors) as $user_id => $info) {
                $userIDs[] = $user_id;
            }
            foreach ($rejectors as $user_id => $info) {
                $rejectedBy[] = $info['name'];
            }
 
            $rejectedBy = jvbCommaList($rejectedBy);
 
            // Notify the requester
            JVB()->notification()->addNotification(
                $userIDs,
                'term_rejected',
                [
                    'term_name'   => $request->name,
                    'taxonomy'    => $request->taxonomy,
                    'rejected_by' => $rejectedBy
                ]
            );
 
            return true;
        } catch (Exception $e) {
            JVB()->error()
                ->log(
                    '[ApprovalRoutes]:makeTermUnalive',
                    "Error rejecting term: " . $e->getMessage(),
                    [
                        'request_id'    => $request->id,
                        'requester'     => $request->requested_by,
                        'term_name'     => $request->name,
                        'taxonomy'      => $request->taxonomy
                    ]
                );
 
            return false;
        }
    }
 
    /**
     * Create a new term approval request
     *
     * @param int $user_id User requesting approval
     * @param string $taxonomy Taxonomy
     * @param string $name New Term Name
     * @param int $parent Parent Term ID
     * @param int $required_approvals Number of approvals required
     *
     * @return int|false Request ID or false on failure
     */
    public function createTermApprovalRequest(
        int $user_id,
        string $taxonomy,
        string $name,
        int $parent = 0,
        int $required_approvals = 3
    ):int|false {
        global $wpdb;
        $table = $this->getRequestTable('term', $wpdb);
 
        try {
            $wpdb->query('START TRANSACTION');
            // Step 1: Check if user already has a pending request for this term
            $existing = $wpdb->get_row($wpdb->prepare(
                "SELECT id, requested_by FROM $table
            WHERE name = %s
            AND taxonomy = %s
            AND parent = %d
            AND status = 'pending'",
                $name,
                $taxonomy,
                $parent
            ));
 
            if ($existing) {
                // Decode the requested_by JSON field
                $requestedBy = json_decode($existing->requested_by, true) ?: [];
 
                // Check if this user has already requested this term
                if (isset($requestedBy[$user_id])) {
                    $wpdb->query('COMMIT');
                    return (int)$existing->id;
                }
 
                // Add this user to the requesters
                $requestedBy[$user_id] = get_userdata($user_id)->display_name;
 
                // Update the request with the new requester
                $updated = $wpdb->update(
                    $table,
                    ['requested_by' => json_encode($requestedBy)],
                    ['id' => $existing->id]
                );
 
                if (!$updated) {
                    throw new Exception($wpdb->last_error);
                }
 
                $wpdb->query('COMMIT');
                return (int)$existing->id;
            }
 
            $request = [
                'taxonomy'  => $taxonomy,
                'name'      => $name,
                'parent'    => $parent ?: null,
                'status' => 'pending',
                'required_approvals' => $required_approvals,
                'current_approvals' => 0,
                'current_rejections' => 0,
                'requested_by' => json_encode([$user_id => get_userdata($user_id)->display_name]),
                'expires_at' => date('Y-m-d H:i:s', strtotime('+30 days')),
                'created_at' => current_time('mysql'),
                'updated_at' => current_time('mysql')
            ];
            $result = $this->createApprovalRequest('term', $request);
 
            if (!$result) {
                throw new Exception($wpdb->last_error);
            }
 
            $request_id = $wpdb->insert_id;
            $wpdb->query('COMMIT');
            return $request_id;
        } catch (Exception $e) {
            $wpdb->query('ROLLBACK');
            JVB()->error()
                ->log(
                    '[ApprovalRoutes]:createTermApprovalRequest',
                    "Error creating term approval request: " . $e->getMessage(),
                    [
                        'user_id'   => $user_id,
                        'taxonomy' => $taxonomy,
                        'name'       => $name
                    ]
                );
 
            return false;
        }
    }
    /**
     * Clean up expired approval requests and notify admin
     *
     * @return void
     */
    public function cleanupExpiredApprovals(): void
    {
        global $wpdb;
        $tables = array_map(function ($table) use ($wpdb){
            return $wpdb->prefix . BASE . 'approval_'.$table.'_requests';
        }, $this->allTypes);
 
 
        foreach ($tables as $table) {
            $wpdb->query($wpdb->prepare(
                "UPDATE $table SET status = 'expired', updated_at = %s
        WHERE status = 'pending' AND expires_at < %s",
                current_time('mysql'),
                current_time('mysql')
            ));
        }
 
        // Clear caches
        $this->cache->flush();
    }
 
    public function getApprovals(WP_REST_Request $request)
    {
        $user_id = get_current_user_id();
        $params = $request->get_params();
        $type = $params['type'] ?? 'all';
        $status = $params['status'] ?? 'pending';
 
        // Get appropriate approvals based on type
        if ($type === 'user' || $type === 'all') {
            $user_approvals = $this->getUserApprovals($status);
        } else {
            $user_approvals = [];
        }
 
        if ($type === 'term' || $type === 'all') {
            $term_approvals = $this->getTermApprovals($status);
        } else {
            $term_approvals = [];
        }
 
        return new WP_REST_Response([
            'user_approvals' => $user_approvals,
            'term_approvals' => $term_approvals
        ]);
    }
 
    private function getUserApprovals(string $status = 'pending'): array
    {
        global $wpdb;
        $table = $wpdb->prefix . $this->userRequests;
 
        // Build the status condition
        $status_condition = ($status === 'all') ?
            "status IN ('pending', 'approved', 'rejected', 'expired')" :
            $wpdb->prepare("status = %s", $status);
 
        return $wpdb->get_results(
            "SELECT * FROM $table
        WHERE $status_condition
        ORDER BY created_at DESC"
        );
    }
 
    private function getTermApprovals(string $status = 'pending'): array
    {
        global $wpdb;
        $table = $wpdb->prefix . $this->termRequests;
 
        // Build the status condition
        $status_condition = ($status === 'all') ?
            "status IN ('pending', 'approved', 'rejected', 'expired')" :
            $wpdb->prepare("status = %s", $status);
 
        return $wpdb->get_results(
            "SELECT * FROM $table
        WHERE $status_condition
        ORDER BY created_at DESC"
        );
    }
}