Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Sep 26, 2024
1 parent d94abc6 commit b7634f7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
101 changes: 97 additions & 4 deletions bp-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,110 @@ function bp_rest() {
*
* @return string
*/
function bp_rest_current_rest_namespace() {
function bp_filter_v1_rest_current_rest_namespace() {
return 'buddypress';
}
add_filter( 'bp_rest_namespace', 'bp_rest_current_rest_namespace' );
add_filter( 'bp_rest_namespace', 'bp_filter_v1_rest_current_rest_namespace' );

/**
* Set the current BP REST version.
*
* @return string
*/
function bp_rest_current_rest_version() {
function bp_filter_v1_rest_current_rest_version() {
return 'v1';
}
add_filter( 'bp_rest_version', 'bp_rest_current_rest_version' );
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 );
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const WP_TESTS_PHPUNIT_POLYFILLS_PATH = __DIR__ . '/../vendor/yoast/phpunit-polyfills';

// Define constants.
require( dirname( __FILE__ ) . '/define-constants.php' );
require __DIR__ . '/define-constants.php';

if ( ! file_exists( WP_TESTS_DIR . '/includes/functions.php' ) ) {
die( "The WordPress PHPUnit test suite could not be found.\n" );
Expand All @@ -31,7 +31,7 @@ function _manually_load_plugins() {
require_once BP_TESTS_DIR . '/includes/loader.php';

// Load our plugin.
require_once dirname( __FILE__ ) . '/../bp-rest.php';
require_once __DIR__ . '/../bp-rest.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugins' );

Expand Down

0 comments on commit b7634f7

Please sign in to comment.