Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/27/2024 09:11:27 PM (2 years ago)
Author:
espellcaste
Message:

Include the V2 of the BP REST API in BuddyPress core.

We are officially deprecating the V1 of the BP REST API. And bundling the new, default, V2 of the BP REST API inside BuddyPress core. Previously, the V1 was developed as a plugin in a separate repo (​https://github.com/buddypress/BP-REST).

  • One of the main differences between the V1 and V2 is how objects are returned. Single items are no longer returned as an array;
  • We have a new BP_Test_REST_Controller_Testcase for testing the new API endpoints;
  • We changed the names of our controllers to follow our autoloader rules;
  • Removed the BP-REST plugin from wp-env and from our release script;
  • And we added notices for the deprecated V1 API (endpoints and files).

Props imath & I. ;)

Fixes #8200
See #9145
Closes ​https://github.com/buddypress/buddypress/pull/337

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-filters.php

    r13476 r14026  
    190190        return $retval;
    191191}
     192
     193/**
     194 * Filter the Blog url in the WP_REST_Request::from_url().
     195 *
     196 * First part in embedding the latest blog post.
     197 *
     198 * @link https://github.com/buddypress/BP-REST/pull/395
     199 *
     200 * @param WP_REST_Request $request Request used to generate the response.
     201 * @param string          $url     URL being requested.
     202 * @return WP_REST_Request
     203 */
     204function bp_filter_rest_request_blog_url( $request, $url ) {
     205
     206        if ( ! bp_is_active( 'blogs' ) || empty( $url ) ) {
     207                return $request;
     208        }
     209
     210        // Get url info.
     211        $bits      = wp_parse_url( $url );
     212        $home_bits = wp_parse_url( get_home_url() );
     213
     214        if ( empty( $bits['host'] ) || empty( $home_bits['host'] ) ) {
     215                return $request;
     216        }
     217
     218        // Bail early if the request URL is the same as the current site.
     219        if ( $bits['host'] === $home_bits['host'] ) {
     220                return $request;
     221        }
     222
     223        // Create a fake request to bypass the current logic.
     224        $request = new WP_REST_Request( 'GET', $bits['path'] );
     225        $request->set_query_params( array( 'bp_blogs_url' => $url ) );
     226
     227        return $request;
     228}
     229add_filter( 'rest_request_from_url', 'bp_filter_rest_request_blog_url', 10, 2 );
     230
     231/**
     232 * Output BuddyPress blog response.
     233 *
     234 * Second part in embedding the latest blog post.
     235 *
     236 * @link https://github.com/buddypress/BP-REST/pull/395
     237 *
     238 * @param WP_REST_Response $response Response generated by the request.
     239 * @param WP_REST_Server   $instance Server instance.
     240 * @param WP_REST_Request  $request  Request used to generate the response.
     241 * @return WP_REST_Response
     242 */
     243function bp_rest_post_dispatch( $response, $instance, $request ) {
     244        if (
     245                ! bp_is_active( 'blogs' )
     246                || 404 !== $response->get_status()
     247                || 'embed' !== $request->get_param( 'context' )
     248                || empty( $request->get_param( 'bp_blogs_url' ) )
     249                || empty( $request->get_route() )
     250        ) {
     251                return $response;
     252        }
     253
     254        // Get domain from url.
     255        $bits = wp_parse_url( $request->get_param( 'bp_blogs_url' ) );
     256
     257        // We need those two to proceed.
     258        if ( empty( $bits['host'] ) || empty( $bits['path'] ) ) {
     259                return $response;
     260        }
     261
     262        // Request route and requested URL path should match.
     263        if ( $request->get_route() !== $bits['path'] ) {
     264                return $response;
     265        }
     266
     267        // Get site using the domain.
     268        $site = get_site_by_path( $bits['host'], $bits['path'] );
     269
     270        if ( ! $site instanceof WP_Site || empty( $site->blog_id ) ) {
     271                return $response;
     272        }
     273
     274        switch_to_blog( absint( $site->blog_id ) );
     275
     276        $response = rest_do_request(
     277                new WP_REST_Request(
     278                        'GET',
     279                        str_replace(
     280                                '/wp-json',
     281                                '',
     282                                $request->get_route()
     283                        )
     284                )
     285        );
     286
     287        restore_current_blog();
     288
     289        // Return it, regardless if it was successful.
     290        return $response;
     291}
     292add_filter( 'rest_post_dispatch', 'bp_rest_post_dispatch', 10, 3 );
Note: See TracChangeset for help on using the changeset viewer.