-
Notifications
You must be signed in to change notification settings - Fork 38
/
bp-rest.php
282 lines (238 loc) · 9.28 KB
/
bp-rest.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
<?php
/**
* Plugin Name: BuddyPress RESTful API
* Plugin URI: https://buddypress.org
* Description: Access your BuddyPress site's data through an easy-to-use HTTP REST API.
* Author: The BuddyPress Community
* Author URI: https: //buddypress.org/
* Version: 0.9.0
* Text Domain: buddypress
* Domain Path: /languages/
* Requires at least: 6.1
* Tested up to: 6.4
* Requires PHP: 5.6
* Requires Plugins: buddypress
* License: GNU General Public License v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package BuddyPress
* @since 0.1.0
*/
/**
* Copyright (c) 2020 BuddyPress (email: [email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
defined( 'ABSPATH' ) || exit;
/**
* Register BuddyPress endpoints.
*
* @since 0.1.0
*/
function bp_rest() {
// Bail early if no core rest support.
if ( ! class_exists( 'WP_REST_Controller' ) ) {
return;
}
require_once __DIR__ . '/includes/bp-components/classes/class-bp-rest-components-endpoint.php';
$controller = new BP_REST_Components_Endpoint();
$controller->register_routes();
if ( bp_is_active( 'members' ) ) {
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-members-endpoint.php';
$controller = new BP_REST_Members_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-attachments/classes/trait-attachments.php';
// Support Member's Avatar.
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-attachments-member-avatar-endpoint.php';
$controller = new BP_REST_Attachments_Member_Avatar_Endpoint();
$controller->register_routes();
// Support Member's Cover.
if ( bp_is_active( 'members', 'cover_image' ) ) {
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-attachments-member-cover-endpoint.php';
$controller = new BP_REST_Attachments_Member_Cover_Endpoint();
$controller->register_routes();
}
if ( bp_get_signup_allowed() ) {
require_once __DIR__ . '/includes/bp-members/classes/class-bp-rest-signup-endpoint.php';
$controller = new BP_REST_Signup_Endpoint();
$controller->register_routes();
}
}
if ( bp_is_active( 'activity' ) ) {
require_once __DIR__ . '/includes/bp-activity/classes/class-bp-rest-activity-endpoint.php';
$controller = new BP_REST_Activity_Endpoint();
$controller->register_routes();
}
if ( is_multisite() && bp_is_active( 'blogs' ) ) {
require_once __DIR__ . '/includes/bp-blogs/classes/class-bp-rest-blogs-endpoint.php';
$controller = new BP_REST_Blogs_Endpoint();
$controller->register_routes();
// Support to Blog Avatar.
if ( bp_is_active( 'blogs', 'site-icon' ) ) {
require_once __DIR__ . '/includes/bp-attachments/classes/trait-attachments.php';
require_once __DIR__ . '/includes/bp-blogs/classes/class-bp-rest-attachments-blog-avatar-endpoint.php';
$controller = new BP_REST_Attachments_Blog_Avatar_Endpoint();
$controller->register_routes();
}
}
if ( bp_is_active( 'xprofile' ) ) {
require_once __DIR__ . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-fields-endpoint.php';
$controller = new BP_REST_XProfile_Fields_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-field-groups-endpoint.php';
$controller = new BP_REST_XProfile_Field_Groups_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-xprofile/classes/class-bp-rest-xprofile-data-endpoint.php';
$controller = new BP_REST_XProfile_Data_Endpoint();
$controller->register_routes();
}
if ( bp_is_active( 'groups' ) ) {
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-groups-endpoint.php';
$controller = new BP_REST_Groups_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-group-membership-endpoint.php';
$controller = new BP_REST_Group_Membership_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-group-invites-endpoint.php';
$controller = new BP_REST_Group_Invites_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-group-membership-request-endpoint.php';
$controller = new BP_REST_Group_Membership_Request_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-attachments/classes/trait-attachments.php';
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-attachments-group-avatar-endpoint.php';
$controller = new BP_REST_Attachments_Group_Avatar_Endpoint();
$controller->register_routes();
// Support to Group Cover.
if ( bp_is_active( 'groups', 'cover_image' ) ) {
require_once __DIR__ . '/includes/bp-groups/classes/class-bp-rest-attachments-group-cover-endpoint.php';
$controller = new BP_REST_Attachments_Group_Cover_Endpoint();
$controller->register_routes();
}
}
if ( bp_is_active( 'messages' ) ) {
require_once __DIR__ . '/includes/bp-messages/classes/class-bp-rest-messages-endpoint.php';
$controller = new BP_REST_Messages_Endpoint();
$controller->register_routes();
require_once __DIR__ . '/includes/bp-messages/classes/class-bp-rest-sitewide-notices-endpoint.php';
$controller = new BP_REST_Sitewide_Notices_Endpoint();
$controller->register_routes();
}
if ( bp_is_active( 'notifications' ) ) {
require_once __DIR__ . '/includes/bp-notifications/classes/class-bp-rest-notifications-endpoint.php';
$controller = new BP_REST_Notifications_Endpoint();
$controller->register_routes();
}
if ( bp_is_active( 'friends' ) ) {
require_once __DIR__ . '/includes/bp-friends/classes/class-bp-rest-friends-endpoint.php';
$controller = new BP_REST_Friends_Endpoint();
$controller->register_routes();
}
}
add_action( 'bp_rest_api_init', 'bp_rest', 5 );
/**
* Set the current BP REST namespace.
*
* @return string
*/
function bp_filter_v1_rest_current_rest_namespace() {
return 'buddypress';
}
add_filter( 'bp_rest_namespace', 'bp_filter_v1_rest_current_rest_namespace' );
/**
* Set the current BP REST version.
*
* @return string
*/
function bp_filter_v1_rest_current_rest_version() {
return 'v1';
}
add_filter( 'bp_rest_version', 'bp_filter_v1_rest_current_rest_version' );
/**
* Filter the Blog url in the WP_REST_Request::from_url().
*
* @param WP_REST_Request $request Request used to generate the response.
* @param string $url URL being requested.
* @return WP_REST_Request
*/
function bp_filter_v1_rest_request_blog_url( $request, $url ) {
if ( ! bp_is_active( 'blogs' ) || empty( $url ) ) {
return $request;
}
// Get url info.
$bits = wp_parse_url( $url );
$home_bits = wp_parse_url( get_home_url() );
if ( empty( $bits['host'] ) || empty( $home_bits['host'] ) ) {
return $request;
}
// Bail early if the request URL is the same as the current site.
if ( $bits['host'] === $home_bits['host'] ) {
return $request;
}
// Create a fake request to bypass the current logic.
$request = new WP_REST_Request( 'GET', $bits['path'] );
$request->set_query_params( array( 'bp_blogs_url' => $url ) );
return $request;
}
add_filter( 'rest_request_from_url', 'bp_filter_v1_rest_request_blog_url', 10, 2 );
/**
* Output BuddyPress blog response.
*
* @param WP_REST_Response $response Response generated by the request.
* @param WP_REST_Server $instance Server instance.
* @param WP_REST_Request $request Request used to generate the response.
* @return WP_REST_Response
*/
function bp_filter_v1_rest_post_dispatch( $response, $instance, $request ) {
if (
! bp_is_active( 'blogs' )
|| 404 !== $response->get_status()
|| 'embed' !== $request->get_param( 'context' )
|| empty( $request->get_param( 'bp_blogs_url' ) )
|| empty( $request->get_route() )
) {
return $response;
}
// Get domain from url.
$bits = wp_parse_url( $request->get_param( 'bp_blogs_url' ) );
// We need those two to proceed.
if ( empty( $bits['host'] ) || empty( $bits['path'] ) ) {
return $response;
}
// Request route and requested URL path should match.
if ( $request->get_route() !== $bits['path'] ) {
return $response;
}
// Get site using the domain.
$site = get_site_by_path( $bits['host'], $bits['path'] );
if ( ! $site instanceof WP_Site || empty( $site->blog_id ) ) {
return $response;
}
switch_to_blog( absint( $site->blog_id ) );
$response = rest_do_request(
new WP_REST_Request(
'GET',
str_replace(
'/wp-json',
'',
$request->get_route()
)
)
);
restore_current_blog();
// Return it, regardless if it was successful or not.
return $response;
}
add_filter( 'rest_post_dispatch', 'bp_filter_v1_rest_post_dispatch', 10, 3 );