-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbpress-api-server.php
1262 lines (1110 loc) · 43 KB
/
bbpress-api-server.php
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
<?php
/**
* Plugin Name: bbPress API Server
* Plugin URI: https://cyberforums.com/document/bbpress-api-server
* Description: Provides comprehensive API endpoints for bbPress forums, topics, and replies
* Version: 1.0.0
* Author: Cyberforums
* Author URI: https://cyberforums.com
* Text Domain: bbpress-api-server
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('BBPAS_VERSION', '1.0.0');
define('BBPAS_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('BBPAS_PLUGIN_URL', plugin_dir_url(__FILE__));
// API namespace and version
define('BBPAS_API_NAMESPACE', 'bbpas/v1');
// Check if bbPress is active
function bbpas_check_required_plugins() {
if (!class_exists('bbPress')) {
add_action('admin_notices', 'bbpas_admin_notice');
return false;
}
return true;
}
// Admin notice if required plugins are not active
function bbpas_admin_notice() {
?>
<div class="notice notice-error">
<p><?php _e('bbPress API Server requires bbPress to be installed and activated.', 'bbpress-api-server'); ?></p>
</div>
<?php
}
// Register REST API routes
function bbpas_register_routes() {
// === Test endpoint ===
register_rest_route(BBPAS_API_NAMESPACE, '/test', array(
'methods' => 'GET',
'callback' => 'bbpas_test_connection',
'permission_callback' => '__return_true',
));
// === Forum endpoints ===
// Get all forums
register_rest_route(BBPAS_API_NAMESPACE, '/forums', array(
'methods' => 'GET',
'callback' => 'bbpas_get_forums',
'permission_callback' => '__return_true',
'args' => array(
'parent' => array(
'validate_callback' => function($param) {
return is_numeric($param) || $param === '0';
},
'default' => null,
'description' => 'Filter forums by parent ID. Use 0 for root forums.',
),
'per_page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && ($param == -1 || ($param > 0 && $param <= 100));
},
'default' => -1,
'description' => 'Number of forums to return per page. Use -1 to return all.',
),
'page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0;
},
'default' => 1,
'description' => 'Current page of forum results.',
),
),
));
// Get single forum details
register_rest_route(BBPAS_API_NAMESPACE, '/forum/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'bbpas_get_forum_details',
'permission_callback' => '__return_true',
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param) {
return is_numeric($param);
},
'description' => 'Forum ID',
),
),
));
// === Topic endpoints ===
// Get topics (with optional forum filter)
register_rest_route(BBPAS_API_NAMESPACE, '/topics', array(
'methods' => 'GET',
'callback' => 'bbpas_get_topics',
'permission_callback' => '__return_true',
'args' => array(
'forum_id' => array(
'validate_callback' => function($param) {
return is_numeric($param);
},
'description' => 'Filter topics by forum ID',
),
'per_page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0 && $param <= 100;
},
'default' => 10,
'description' => 'Number of topics to return per page',
),
'page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0;
},
'default' => 1,
'description' => 'Current page of topic results',
),
'orderby' => array(
'validate_callback' => function($param) {
return in_array($param, ['date', 'title', 'activity', 'popularity']);
},
'default' => 'activity',
'description' => 'Order topics by parameter: date, title, activity, popularity',
),
'order' => array(
'validate_callback' => function($param) {
return in_array(strtoupper($param), ['ASC', 'DESC']);
},
'default' => 'DESC',
'description' => 'Order direction: ASC or DESC',
),
),
));
// Get single topic details
register_rest_route(BBPAS_API_NAMESPACE, '/topic/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'bbpas_get_topic_details',
'permission_callback' => '__return_true',
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param) {
return is_numeric($param);
},
'description' => 'Topic ID',
),
'with_replies' => array(
'validate_callback' => function($param) {
return rest_is_boolean($param);
},
'default' => false,
'description' => 'Include topic replies',
),
),
));
// === Reply endpoints ===
// Get replies for a topic
register_rest_route(BBPAS_API_NAMESPACE, '/replies', array(
'methods' => 'GET',
'callback' => 'bbpas_get_replies',
'permission_callback' => '__return_true',
'args' => array(
'topic_id' => array(
'required' => true,
'validate_callback' => function($param) {
return is_numeric($param);
},
'description' => 'Topic ID to get replies from',
),
'per_page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0 && $param <= 100;
},
'default' => 20,
'description' => 'Number of replies to return per page',
),
'page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0;
},
'default' => 1,
'description' => 'Current page of reply results',
),
'order' => array(
'validate_callback' => function($param) {
return in_array(strtoupper($param), ['ASC', 'DESC']);
},
'default' => 'ASC',
'description' => 'Order direction: ASC or DESC',
),
),
));
// Get single reply details
register_rest_route(BBPAS_API_NAMESPACE, '/reply/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'bbpas_get_reply_details',
'permission_callback' => '__return_true',
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param) {
return is_numeric($param);
},
'description' => 'Reply ID',
),
),
));
// === User endpoints ===
// Get user forum activity
register_rest_route(BBPAS_API_NAMESPACE, '/user/(?P<id>\d+)/activity', array(
'methods' => 'GET',
'callback' => 'bbpas_get_user_activity',
'permission_callback' => '__return_true',
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function($param) {
return is_numeric($param);
},
'description' => 'User ID',
),
'type' => array(
'validate_callback' => function($param) {
return in_array($param, ['topics', 'replies', 'all']);
},
'default' => 'all',
'description' => 'Activity type: topics, replies, or all',
),
'per_page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0 && $param <= 50;
},
'default' => 10,
'description' => 'Number of activities to return',
),
),
));
// === Search endpoint ===
register_rest_route(BBPAS_API_NAMESPACE, '/search', array(
'methods' => 'GET',
'callback' => 'bbpas_search',
'permission_callback' => '__return_true',
'args' => array(
'query' => array(
'required' => true,
'description' => 'Search query',
),
'type' => array(
'validate_callback' => function($param) {
return in_array($param, ['forum', 'topic', 'reply', 'all']);
},
'default' => 'all',
'description' => 'Type of content to search: forum, topic, reply, all',
),
'per_page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0 && $param <= 50;
},
'default' => 10,
'description' => 'Number of results to return',
),
'page' => array(
'validate_callback' => function($param) {
return is_numeric($param) && $param > 0;
},
'default' => 1,
'description' => 'Current page of search results',
),
),
));
// === Stats endpoint ===
register_rest_route(BBPAS_API_NAMESPACE, '/stats', array(
'methods' => 'GET',
'callback' => 'bbpas_get_stats',
'permission_callback' => '__return_true',
));
}
// === Test endpoint function ===
function bbpas_test_connection() {
if (!function_exists('bbp_get_forum_post_type')) {
return new WP_Error('bbpress_not_active', __('bbPress is not active', 'bbpress-api-server'), array('status' => 500));
}
return array(
'success' => true,
'message' => __('Connection successful', 'bbpress-api-server'),
'version' => BBPAS_VERSION,
'bbpress_version' => bbp_get_version(),
'api_namespace' => BBPAS_API_NAMESPACE,
);
}
// === Forum endpoint functions ===
function bbpas_get_forums($request) {
if (!function_exists('bbp_get_forum_post_type')) {
return new WP_Error('bbpress_not_active', __('bbPress is not active', 'bbpress-api-server'), array('status' => 500));
}
$parent = $request->get_param('parent');
$per_page = $request->get_param('per_page');
$page = $request->get_param('page');
$args = array(
'post_type' => bbp_get_forum_post_type(),
'post_status' => 'publish',
'posts_per_page' => $per_page,
'paged' => $page,
'orderby' => 'menu_order',
'order' => 'ASC',
);
// If parent parameter is set, filter by parent forum
if ($parent !== null) {
$args['post_parent'] = $parent;
}
$forums_query = new WP_Query($args);
$forums = array();
if ($forums_query->have_posts()) {
while ($forums_query->have_posts()) {
$forums_query->the_post();
$forum_id = get_the_ID();
// Skip hidden forums or forums user doesn't have access to
if (!bbp_is_forum_public($forum_id)) {
continue;
}
$forums[] = bbpas_prepare_forum_data($forum_id);
}
wp_reset_postdata();
}
// Add pagination info to response
$response = rest_ensure_response($forums);
$response->header('X-WP-Total', $forums_query->found_posts);
$response->header('X-WP-TotalPages', $forums_query->max_num_pages);
return $response;
}
function bbpas_get_forum_details($request) {
$forum_id = $request->get_param('id');
if (!bbp_is_forum($forum_id)) {
return new WP_Error('invalid_forum', __('Invalid forum ID', 'bbpress-api-server'), array('status' => 404));
}
if (!bbp_is_forum_public($forum_id)) {
return new WP_Error('private_forum', __('This forum is not public', 'bbpress-api-server'), array('status' => 403));
}
return bbpas_prepare_forum_data($forum_id, true);
}
function bbpas_prepare_forum_data($forum_id, $with_subforums = false) {
$forum_data = array(
'id' => $forum_id,
'title' => bbp_get_forum_title($forum_id),
'permalink' => bbp_get_forum_permalink($forum_id),
'description' => bbp_get_forum_content($forum_id),
'topic_count' => bbp_get_forum_topic_count($forum_id),
'reply_count' => bbp_get_forum_reply_count($forum_id),
'total_topic_count' => bbp_get_forum_topic_count($forum_id, true),
'total_reply_count' => bbp_get_forum_reply_count($forum_id, true),
'last_active' => bbp_get_forum_last_active_time($forum_id),
'last_active_time' => get_post_meta($forum_id, '_bbp_last_active_time', true),
'parent_forum' => bbp_get_forum_parent_id($forum_id),
'status' => bbp_get_forum_status($forum_id),
'is_category' => bbp_is_forum_category($forum_id),
'forum_type' => bbp_get_forum_type($forum_id),
'visibility' => bbp_get_forum_visibility($forum_id),
);
// Add last topic info if available
$last_topic_id = bbp_get_forum_last_topic_id($forum_id);
if ($last_topic_id) {
$forum_data['last_topic'] = array(
'id' => $last_topic_id,
'title' => bbp_get_forum_last_topic_title($forum_id),
'permalink' => bbp_get_topic_permalink($last_topic_id),
'author_id' => bbp_get_topic_author_id($last_topic_id),
'author_name' => bbp_get_topic_author_display_name($last_topic_id),
);
}
// Include subforums if requested
if ($with_subforums) {
$subforums = bbp_forum_get_subforums($forum_id);
if ($subforums) {
$forum_data['subforums'] = array();
foreach ($subforums as $subforum) {
if (bbp_is_forum_public($subforum->ID)) {
$forum_data['subforums'][] = bbpas_prepare_forum_data($subforum->ID);
}
}
}
}
return $forum_data;
}
// === Topic endpoint functions ===
function bbpas_get_topics($request) {
if (!function_exists('bbp_get_topic_post_type')) {
return new WP_Error('bbpress_not_active', __('bbPress is not active', 'bbpress-api-server'), array('status' => 500));
}
$forum_id = $request->get_param('forum_id');
$per_page = $request->get_param('per_page');
$page = $request->get_param('page');
$orderby = $request->get_param('orderby');
$order = $request->get_param('order');
// Map API orderby parameter to WP_Query parameters
$orderby_map = array(
'date' => 'date',
'title' => 'title',
'activity' => 'meta_value',
'popularity' => 'meta_value_num'
);
$wp_orderby = isset($orderby_map[$orderby]) ? $orderby_map[$orderby] : 'meta_value';
$meta_key = ($orderby === 'popularity') ? '_bbp_voice_count' : '_bbp_last_active_time';
$args = array(
'post_type' => bbp_get_topic_post_type(),
'post_status' => bbp_get_public_status_id(),
'posts_per_page' => $per_page,
'paged' => $page,
'orderby' => $wp_orderby,
'order' => $order,
);
// Add meta key for ordering if needed
if (in_array($orderby, ['activity', 'popularity'])) {
$args['meta_key'] = $meta_key;
}
// If forum ID is provided, filter by forum
if ($forum_id) {
// Check if forum exists and is public
if (!bbp_is_forum($forum_id) || !bbp_is_forum_public($forum_id)) {
return new WP_Error('invalid_forum', __('Invalid or inaccessible forum', 'bbpress-api-server'), array('status' => 404));
}
$args['meta_query'] = array(
array(
'key' => '_bbp_forum_id',
'value' => $forum_id,
),
);
}
// Handle sticky topics
$sticky_topics = array();
if ($forum_id) {
$sticky_topics = bbp_get_stickies($forum_id);
}
$topics_query = new WP_Query($args);
$topics = array();
// Add sticky topics first if forum_id is provided and we're on the first page
if ($page == 1 && !empty($sticky_topics) && $forum_id) {
foreach ($sticky_topics as $sticky_id) {
if (bbp_is_topic($sticky_id) && bbp_is_topic_published($sticky_id)) {
$topics[] = bbpas_prepare_topic_data($sticky_id);
}
}
}
// Add regular topics
if ($topics_query->have_posts()) {
while ($topics_query->have_posts()) {
$topics_query->the_post();
$topic_id = get_the_ID();
// Skip sticky topics as they've already been added
if ($page == 1 && !empty($sticky_topics) && in_array($topic_id, $sticky_topics)) {
continue;
}
$topics[] = bbpas_prepare_topic_data($topic_id);
}
wp_reset_postdata();
}
// Add pagination info to response
$response = rest_ensure_response($topics);
$response->header('X-WP-Total', $topics_query->found_posts);
$response->header('X-WP-TotalPages', $topics_query->max_num_pages);
return $response;
}
function bbpas_get_topic_details($request) {
$topic_id = $request->get_param('id');
$with_replies = $request->get_param('with_replies');
if (!bbp_is_topic($topic_id)) {
return new WP_Error('invalid_topic', __('Invalid topic ID', 'bbpress-api-server'), array('status' => 404));
}
$topic_data = bbpas_prepare_topic_data($topic_id, true);
// Include replies if requested
if ($with_replies) {
$replies_data = bbpas_get_replies(new WP_REST_Request('GET', '/bbpas/v1/replies'));
$topic_data['replies'] = $replies_data;
}
return $topic_data;
}
function bbpas_prepare_topic_data($topic_id, $with_content = false) {
$last_active = get_post_meta($topic_id, '_bbp_last_active_time', true);
$forum_id = bbp_get_topic_forum_id($topic_id);
$topic_data = array(
'id' => $topic_id,
'title' => bbp_get_topic_title($topic_id),
'permalink' => bbp_get_topic_permalink($topic_id),
'forum_id' => $forum_id,
'forum_title' => bbp_get_forum_title($forum_id),
'forum_permalink' => bbp_get_forum_permalink($forum_id),
'created' => get_post_time('c', true, $topic_id),
'last_active' => $last_active,
'last_active_gmt' => get_gmt_from_date($last_active),
'last_active_relative' => bbp_get_topic_last_active_time($topic_id),
'author_id' => bbp_get_topic_author_id($topic_id),
'author_name' => bbp_get_topic_author_display_name($topic_id),
'author_avatar' => bbpas_get_author_avatar_url(bbp_get_topic_author_id($topic_id)),
'reply_count' => bbp_get_topic_reply_count($topic_id),
'voice_count' => bbp_get_topic_voice_count($topic_id),
'is_sticky' => bbp_is_topic_sticky($topic_id),
'is_super_sticky' => bbp_is_topic_super_sticky($topic_id),
'status' => bbp_get_topic_status($topic_id),
'excerpt' => bbp_get_topic_excerpt($topic_id, 100),
);
// Include full content if requested
if ($with_content) {
$topic_data['content'] = bbp_get_topic_content($topic_id);
}
// Get tags if any
$topic_data['tags'] = array();
$tags = wp_get_object_terms($topic_id, bbp_get_topic_tag_tax_id());
if (!empty($tags) && !is_wp_error($tags)) {
foreach ($tags as $tag) {
$topic_data['tags'][] = array(
'id' => $tag->term_id,
'name' => $tag->name,
'slug' => $tag->slug,
'permalink' => bbp_get_topic_tag_link($tag->term_id)
);
}
}
// Add last reply info if available
$last_reply_id = bbp_get_topic_last_reply_id($topic_id);
if ($last_reply_id && $last_reply_id != $topic_id) {
$topic_data['last_reply'] = array(
'id' => $last_reply_id,
'excerpt' => bbp_get_reply_excerpt($last_reply_id, 55),
'author_id' => bbp_get_reply_author_id($last_reply_id),
'author_name' => bbp_get_reply_author_display_name($last_reply_id),
'author_avatar' => bbpas_get_author_avatar_url(bbp_get_reply_author_id($last_reply_id)),
'created' => get_post_time('c', true, $last_reply_id),
);
}
return $topic_data;
}
// === Reply endpoint functions ===
function bbpas_get_replies($request) {
if (!function_exists('bbp_get_reply_post_type')) {
return new WP_Error('bbpress_not_active', __('bbPress is not active', 'bbpress-api-server'), array('status' => 500));
}
$topic_id = $request->get_param('topic_id');
$per_page = $request->get_param('per_page');
$page = $request->get_param('page');
$order = $request->get_param('order');
if (!bbp_is_topic($topic_id)) {
return new WP_Error('invalid_topic', __('Invalid topic ID', 'bbpress-api-server'), array('status' => 404));
}
$args = array(
'post_type' => bbp_get_reply_post_type(),
'post_status' => bbp_get_public_status_id(),
'post_parent' => $topic_id,
'posts_per_page' => $per_page,
'paged' => $page,
'orderby' => 'date',
'order' => $order,
);
$replies_query = new WP_Query($args);
$replies = array();
if ($replies_query->have_posts()) {
while ($replies_query->have_posts()) {
$replies_query->the_post();
$reply_id = get_the_ID();
$replies[] = bbpas_prepare_reply_data($reply_id);
}
wp_reset_postdata();
}
// Add pagination info to response
$response = rest_ensure_response($replies);
$response->header('X-WP-Total', $replies_query->found_posts);
$response->header('X-WP-TotalPages', $replies_query->max_num_pages);
return $response;
}
function bbpas_get_reply_details($request) {
$reply_id = $request->get_param('id');
if (!bbp_is_reply($reply_id)) {
return new WP_Error('invalid_reply', __('Invalid reply ID', 'bbpress-api-server'), array('status' => 404));
}
return bbpas_prepare_reply_data($reply_id, true);
}
function bbpas_prepare_reply_data($reply_id, $with_content = false) {
$reply_data = array(
'id' => $reply_id,
'permalink' => bbp_get_reply_permalink($reply_id),
'topic_id' => bbp_get_reply_topic_id($reply_id),
'topic_title' => bbp_get_topic_title(bbp_get_reply_topic_id($reply_id)),
'forum_id' => bbp_get_reply_forum_id($reply_id),
'created' => get_post_time('c', true, $reply_id),
'author_id' => bbp_get_reply_author_id($reply_id),
'author_name' => bbp_get_reply_author_display_name($reply_id),
'author_avatar' => bbpas_get_author_avatar_url(bbp_get_reply_author_id($reply_id)),
'excerpt' => bbp_get_reply_excerpt($reply_id, 100),
);
// Include full content if requested
if ($with_content) {
$reply_data['content'] = bbp_get_reply_content($reply_id);
}
return $reply_data;
}
// === User endpoint functions ===
function bbpas_get_user_activity($request) {
$user_id = $request->get_param('id');
$type = $request->get_param('type');
$per_page = $request->get_param('per_page');
if (!get_userdata($user_id)) {
return new WP_Error('invalid_user', __('Invalid user ID', 'bbpress-api-server'), array('status' => 404));
}
$activity = array();
// Get user's topics if requested
if ($type === 'topics' || $type === 'all') {
$topics = bbpas_get_user_topics($user_id, $per_page);
if ($type === 'topics') {
return $topics;
} else {
$activity['topics'] = $topics;
}
}
// Get user's replies if requested
if ($type === 'replies' || $type === 'all') {
$replies = bbpas_get_user_replies($user_id, $per_page);
if ($type === 'replies') {
return $replies;
} else {
$activity['replies'] = $replies;
}
}
// If type is 'all', we're returning both
return $activity;
}
function bbpas_get_user_topics($user_id, $per_page = 10) {
$args = array(
'author' => $user_id,
'post_type' => bbp_get_topic_post_type(),
'post_status' => array(bbp_get_public_status_id()),
'posts_per_page' => $per_page,
'orderby' => 'date',
'order' => 'DESC',
);
$query = new WP_Query($args);
$topics = array();
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$topics[] = bbpas_prepare_topic_data(get_the_ID());
}
wp_reset_postdata();
}
return $topics;
}
function bbpas_get_user_replies($user_id, $per_page = 10) {
$args = array(
'author' => $user_id,
'post_type' => bbp_get_reply_post_type(),
'post_status' => array(bbp_get_public_status_id()),
'posts_per_page' => $per_page,
'orderby' => 'date',
'order' => 'DESC',
);
$query = new WP_Query($args);
$replies = array();
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$replies[] = bbpas_prepare_reply_data(get_the_ID());
}
wp_reset_postdata();
}
return $replies;
}
// === Search endpoint function ===
function bbpas_search($request) {
$query = $request->get_param('query');
$type = $request->get_param('type');
$per_page = $request->get_param('per_page');
$page = $request->get_param('page');
if (empty($query)) {
return new WP_Error('empty_search', __('Search query cannot be empty', 'bbpress-api-server'), array('status' => 400));
}
// Determine post types to search
$post_types = array();
switch ($type) {
case 'forum':
$post_types[] = bbp_get_forum_post_type();
break;
case 'topic':
$post_types[] = bbp_get_topic_post_type();
break;
case 'reply':
$post_types[] = bbp_get_reply_post_type();
break;
case 'all':
default:
$post_types = array(
bbp_get_forum_post_type(),
bbp_get_topic_post_type(),
bbp_get_reply_post_type()
);
break;
}
$args = array(
'post_type' => $post_types,
'post_status' => 'publish',
's' => $query,
'posts_per_page' => $per_page,
'paged' => $page,
'orderby' => 'relevance',
);
$search_query = new WP_Query($args);
$results = array();
if ($search_query->have_posts()) {
while ($search_query->have_posts()) {
$search_query->the_post();
$post_id = get_the_ID();
$post_type = get_post_type($post_id);
$result = array(
'id' => $post_id,
'type' => $post_type,
'title' => get_the_title($post_id),
'permalink' => get_permalink($post_id),
'date' => get_post_time('c', true, $post_id),
'excerpt' => get_the_excerpt($post_id),
);
// Add type-specific data
if ($post_type === bbp_get_forum_post_type()) {
$result['topic_count'] = bbp_get_forum_topic_count($post_id);
$result['reply_count'] = bbp_get_forum_reply_count($post_id);
} elseif ($post_type === bbp_get_topic_post_type()) {
$result['forum_id'] = bbp_get_topic_forum_id($post_id);
$result['forum_title'] = bbp_get_forum_title($result['forum_id']);
$result['reply_count'] = bbp_get_topic_reply_count($post_id);
} elseif ($post_type === bbp_get_reply_post_type()) {
$result['topic_id'] = bbp_get_reply_topic_id($post_id);
$result['topic_title'] = bbp_get_topic_title($result['topic_id']);
$result['forum_id'] = bbp_get_reply_forum_id($post_id);
}
$results[] = $result;
}
wp_reset_postdata();
}
// Add pagination info to response
$response = rest_ensure_response($results);
$response->header('X-WP-Total', $search_query->found_posts);
$response->header('X-WP-TotalPages', $search_query->max_num_pages);
return $response;
}
// === Stats endpoint function ===
function bbpas_get_stats() {
return array(
'forums' => array(
'total' => bbpas_count_total_forums(),
'active' => bbpas_count_active_forums(),
),
'topics' => array(
'total' => bbpas_count_total_topics(),
'open' => bbpas_count_open_topics(),
'closed' => bbpas_count_closed_topics(),
),
'replies' => array(
'total' => bbpas_count_total_replies(),
),
'users' => array(
'total' => bbpas_count_total_users(),
),
'last_24_hours' => array(
'topics' => bbpas_count_topics_last_24_hours(),
'replies' => bbpas_count_replies_last_24_hours(),
),
'last_7_days' => array(
'topics' => bbpas_count_topics_last_7_days(),
'replies' => bbpas_count_replies_last_7_days(),
),
);
}
// Helper functions for stats
function bbpas_count_total_forums() {
$args = array(
'post_type' => bbp_get_forum_post_type(),
'post_status' => 'publish',
'posts_per_page' => 1,
'fields' => 'ids',
);
$query = new WP_Query($args);
return $query->found_posts;
}
function bbpas_count_active_forums() {
// Active forums have at least one topic
$args = array(
'post_type' => bbp_get_forum_post_type(),
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_bbp_topic_count',
'value' => 0,
'compare' => '>',
),
),
'posts_per_page' => 1,
'fields' => 'ids',
);
$query = new WP_Query($args);
return $query->found_posts;
}
function bbpas_count_total_topics() {
$args = array(
'post_type' => bbp_get_topic_post_type(),
'post_status' => 'publish',
'posts_per_page' => 1,
'fields' => 'ids',
);
$query = new WP_Query($args);
return $query->found_posts;
}
function bbpas_count_open_topics() {
$args = array(
'post_type' => bbp_get_topic_post_type(),
'post_status' => bbp_get_public_status_id(),
'meta_query' => array(
array(
'key' => '_bbp_status',
'value' => 'closed',
'compare' => '!=',
),
),
'posts_per_page' => 1,
'fields' => 'ids',
);
$query = new WP_Query($args);
return $query->found_posts;
}
function bbpas_count_closed_topics() {
$args = array(
'post_type' => bbp_get_topic_post_type(),
'post_status' => bbp_get_public_status_id(),
'meta_query' => array(
array(
'key' => '_bbp_status',
'value' => 'closed',
),
),
'posts_per_page' => 1,
'fields' => 'ids',
);
$query = new WP_Query($args);
return $query->found_posts;
}
function bbpas_count_total_replies() {
$args = array(
'post_type' => bbp_get_reply_post_type(),
'post_status' => 'publish',
'posts_per_page' => 1,
'fields' => 'ids',
);
$query = new WP_Query($args);
return $query->found_posts;
}
function bbpas_count_total_users() {
$count = count_users();
return $count['total_users'];
}
function bbpas_count_topics_last_24_hours() {
$args = array(
'post_type' => bbp_get_topic_post_type(),
'post_status' => 'publish',
'date_query' => array(
array(
'after' => '1 day ago',
),
),
'posts_per_page' => 1,
'fields' => 'ids',
);
$query = new WP_Query($args);
return $query->found_posts;
}
function bbpas_count_replies_last_24_hours() {
$args = array(
'post_type' => bbp_get_reply_post_type(),
'post_status' => 'publish',
'date_query' => array(
array(
'after' => '1 day ago',
),
),
'posts_per_page' => 1,
'fields' => 'ids',