Skip to content

Commit

Permalink
Setting the plugin to V1
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Aug 24, 2024
1 parent 2dec281 commit 93680e9
Showing 1 changed file with 10 additions and 83 deletions.
93 changes: 10 additions & 83 deletions bp-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,94 +169,21 @@ function bp_rest() {
add_action( 'bp_rest_api_init', 'bp_rest', 5 );

/**
* Filter the Blog url in the WP_REST_Request::from_url().
* Set the current BP REST namespace.
*
* @param WP_REST_Request $request Request used to generate the response.
* @param string $url URL being requested.
* @return WP_REST_Request
* @return string
*/
function bp_filter_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;
function bp_rest_current_rest_namespace() {
return 'buddypress';
}
add_filter( 'rest_request_from_url', 'bp_filter_rest_request_blog_url', 10, 2 );
add_filter( 'bp_rest_namespace', 'bp_rest_current_rest_namespace' );

/**
* Output BuddyPress blog response.
* Set the current BP REST version.
*
* @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
* @return string
*/
function bp_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 successfull or not.
return $response;
function bp_rest_current_rest_version() {
return 'v1';
}
add_filter( 'rest_post_dispatch', 'bp_rest_post_dispatch', 10, 3 );
add_filter( 'bp_rest_version', 'bp_rest_current_rest_version' );

0 comments on commit 93680e9

Please sign in to comment.