-
Notifications
You must be signed in to change notification settings - Fork 6
/
bbp-api.php
184 lines (171 loc) · 5.11 KB
/
bbp-api.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
<?php
/*
Plugin Name: bbPress API
Description: A bbPress REST API.
Plugin URI: https://wordpress.org/plugins/bbp-api/
Author: Pascal Casier
Author URI: http://casier.eu/wp-dev/
Text Domain: bbp-api
Version: 1.0.15
License: GPL2
*/
// No direct access
if ( !defined( 'ABSPATH' ) ) exit;
define ('BBPAPI_VERSION' , '1.0.15');
if(!defined('BBPAPI_PLUGIN_DIR'))
define('BBPAPI_PLUGIN_DIR', dirname(__FILE__));
if(!defined('BBPAPI_URL_PATH'))
define('BBPAPI_URL_PATH', plugin_dir_url(__FILE__));
foreach ( glob( BBPAPI_PLUGIN_DIR . "/inc/*.php" ) as $endpoint) {
include $endpoint;
}
/*
* Register all routes
*/
add_action( 'rest_api_init', function() {
// FORUMS LIST
$args = array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_forums',
);
// register both forum slug as /forums
register_rest_route( 'bbp-api/v1', '/' . bbp_get_forum_slug() . '/', $args );
if ( bbp_get_forum_slug() != 'forums' )
register_rest_route( 'bbp-api/v1', '/forums/', $args );
// FORUM One specific forum with meta data and topics
$args = array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_forums_one',
),
array(
'args' => array(
'id' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
}
),
'content' => array(
'required' => True,
'description' => 'Content for the initial post in the new topic.',
'type' => 'string',
),
'title' => array(
'required' => True,
'description' => 'Title for the new topic.',
'type' => 'string',
),
'email' => array(
'required' => True,
'description' => 'Email address of the thread author.',
'type' => 'string',
),
),
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'bbp_api_newtopic_post',
),
);
// register both forum-slug as well as /forums
register_rest_route( 'bbp-api/v1', '/' . bbp_get_forum_slug() . '/(?P<id>\d+)', $args );
if ( bbp_get_forum_slug() != 'forums' )
register_rest_route( 'bbp-api/v1', '/forums/(?P<id>\d+)', $args );
// TOPICS
$args = array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_topics',
),
);
// register both topic-slug as well as /topics
register_rest_route( 'bbp-api/v1', '/' . bbp_get_topic_slug() . '/', $args );
if ( bbp_get_topic_slug() != 'topics' )
register_rest_route( 'bbp-api/v1', '/topics/', $args );
// TOPIC One specific topic with meta data and replies
// The POST request is for a reply to this topic
$args = array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_topics_one',
),
array(
'args' => array(
'id' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
}
),
'content' => array(
'required' => True,
'description' => 'Content for the reply.',
'type' => 'string',
),
'email' => array(
'required' => True,
'description' => 'Email address of the reply author.',
'type' => 'string',
),
),
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'bbp_api_replytotopic_post',
),
);
register_rest_route( 'bbp-api/v1', '/' . bbp_get_topic_slug() . '/(?P<id>\d+)', $args );
if ( bbp_get_topic_slug() != 'topics' )
register_rest_route( 'bbp-api/v1', '/topics/(?P<id>\d+)', $args );
// REPLIES
$args = array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_replies',
);
// register both reply-slug as well as /replies
register_rest_route( 'bbp-api/v1', '/' . bbp_get_reply_slug() . '/', $args );
if ( bbp_get_reply_slug() != 'replies' )
register_rest_route( 'bbp-api/v1', '/replies/', $args );
// REPLIES One specific reply with meta data
// The POST request is for a reply to this reply
$args = array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_replies_one',
),
array(
'args' => array(
'id' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
}
),
'content' => array(
'required' => True,
'description' => 'Content for the reply.',
'type' => 'string',
),
'email' => array(
'required' => True,
'description' => 'Email address of the reply author.',
'type' => 'string',
),
),
'methods' => WP_REST_Server::CREATABLE,
'callback' => 'bbp_api_replytoreply_post',
),
);
// register both reply-slug as well as /replies
register_rest_route( 'bbp-api/v1', '/' . bbp_get_reply_slug() . '/(?P<id>\d+)', $args );
if ( bbp_get_reply_slug() != 'replies' )
register_rest_route( 'bbp-api/v1', '/replies/(?P<id>\d+)', $args );
// TOPICTAGS
$args = array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_topic_tags',
);
// register both reply-slug as well as /replies
register_rest_route( 'bbp-api/v1', '/' . bbp_get_topic_tag_tax_slug() . '/', $args );
if ( bbp_get_reply_slug() != 'topic-tags' )
register_rest_route( 'bbp-api/v1', '/topic-tags/', $args );
// STATS
register_rest_route( 'bbp-api/v1', '/stats/', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'bbp_api_stats',
) );
} );