Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/27/2024 09:11:27 PM (15 months 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-core/bp-core-rest-api.php

    r14013 r14026  
    1212
    1313/**
    14  * Is the BP REST plugin is active?
     14 * Is the BP REST plugin active?
    1515 *
    1616 * @since 5.0.0
     
    2323
    2424/**
    25  * Should we use the REST Endpoints of built BuddyPress?
     25 * Should we use BuddyPress core REST Endpoints?
    2626 *
    2727 * If the BP REST plugin is active, it overrides BuddyPress REST endpoints.
    28  * This allows us to carry on maintaining all the BP REST API endpoints from
    29  * the BP REST plugin on GitHub.
    30  *
    31  * @since 5.0.0
    32  *
    33  * @return bool Whether to use the REST Endpoints of built BuddyPress.
     28 *
     29 * @since 5.0.0
     30 *
     31 * @return bool Whether to use BuddyPress core REST endpoints.
    3432 */
    3533function bp_rest_in_buddypress() {
    36     return ! bp_is_running_from_src_subdirectory() && ! bp_rest_is_plugin_active();
     34    return ! bp_rest_is_plugin_active();
    3735}
    3836
     
    5553     * @param bool $api_is_available True if the BP REST API is available. False otherwise.
    5654     */
    57     return apply_filters( 'bp_rest_api_is_available', bp_rest_in_buddypress() ) || bp_rest_is_plugin_active();
    58 }
    59 
    60 /**
    61  * Register the jQuery.ajax wrapper for BP REST API requests.
    62  *
    63  * @since 5.0.0
    64  * @deprecated 10.0.0
    65  */
    66 function bp_rest_api_register_request_script() {
    67     if ( ! bp_rest_api_is_available() ) {
    68         return;
    69     }
    70 
    71     wp_register_script(
    72         'bp-api-request',
    73         sprintf( '%1$sbp-core/js/bp-api-request%2$s.js', buddypress()->plugin_url, bp_core_get_minified_asset_suffix() ),
    74         array( 'jquery', 'wp-api-request' ),
    75         bp_get_version(),
    76         true
    77     );
    78 
    79     wp_localize_script(
    80         'bp-api-request',
    81         'bpApiSettings',
    82         array(
    83             'unexpectedError'   => __( 'An unexpected error occured. Please try again.', 'buddypress' ),
    84             'deprecatedWarning' => __( 'The bp.apiRequest function is deprecated since BuddyPress 10.0.0, please use wp.apiRequest instead.', 'buddypress' ),
    85         )
    86     );
    87 }
    88 add_action( 'bp_init', 'bp_rest_api_register_request_script' );
     55    return apply_filters( 'bp_rest_api_is_available', ( bp_rest_in_buddypress() || bp_rest_is_plugin_active() ) );
     56}
    8957
    9058/**
     
    11179 *
    11280 * @since 5.0.0
     81 * @since 15.0.0 Version is now v2.
    11382 *
    11483 * @return string
     
    12089     *
    12190     * @since 5.0.0
    122      *
    123      * @param string $version BuddyPress core version.
     91     * @since 15.0.0 Default version is now v2.
     92     *
     93     * @param string $bp_version BuddyPress REST API version.
    12494     */
    125     return apply_filters( 'bp_rest_version', 'v1' );
     95    return apply_filters( 'bp_rest_version', 'v2' );
    12696}
    12797
     
    198168 *
    199169 * @param string $value Comma-separated list of group types.
    200  * @return array|null
     170 * @return array|null|string
    201171 */
    202172function bp_rest_sanitize_member_types( $value ) {
     
    210180    $valid_types        = array_intersect( $types, $registered_types );
    211181
    212     return ( ! empty( $valid_types ) ) ? $valid_types : null;
     182    return ! empty( $valid_types ) ? $valid_types : null;
    213183}
    214184
     
    219189 *
    220190 * @param  mixed $value Mixed value.
    221  * @return WP_Error|bool
     191 * @return WP_Error|true
    222192 */
    223193function bp_rest_validate_member_types( $value ) {
     
    244214        }
    245215    }
     216
     217    return true;
    246218}
    247219
     
    251223 * @since 5.0.0
    252224 *
    253  * @param string $value Comma-separated list of group types.
     225 * @param string $group_types Comma-separated list of group types.
    254226 * @return array|null
    255227 */
    256 function bp_rest_sanitize_group_types( $value ) {
    257     if ( empty( $value ) ) {
     228function bp_rest_sanitize_group_types( $group_types ) {
     229    if ( empty( $group_types ) ) {
    258230        return null;
    259231    }
    260232
    261     $types       = explode( ',', $value );
     233    $types       = explode( ',', $group_types );
    262234    $valid_types = array_intersect( $types, bp_groups_get_group_types() );
    263235
     
    270242 * @since 5.0.0
    271243 *
    272  * @param  mixed $value Mixed value.
     244 * @param  mixed $group_types Mixed value.
    273245 * @return WP_Error|bool
    274246 */
    275 function bp_rest_validate_group_types( $value ) {
    276     if ( empty( $value ) ) {
     247function bp_rest_validate_group_types( $group_types ) {
     248    if ( empty( $group_types ) ) {
    277249        return true;
    278250    }
    279251
    280     $types            = explode( ',', $value );
     252    $types            = explode( ',', $group_types );
    281253    $registered_types = bp_groups_get_group_types();
    282254    foreach ( $types as $type ) {
     
    293265        }
    294266    }
     267
     268    return true;
    295269}
    296270
     
    388362    return isset( $registered_fields[ $attribute ] );
    389363}
     364
     365/**
     366 * Filter the WP REST API response to return a 404 if the request is for the V1 of the BP REST API.
     367 *
     368 * @param mixed           $result Response to replace the requested version with. Can be anything
     369 *                                a normal endpoint can return, or null to not hijack the request.
     370 * @param WP_REST_Server  $server Server instance.
     371 * @param WP_REST_Request $request Request used to generate the response.
     372 *
     373 * @return mixed
     374 */
     375function bp_rest_api_v1_dispatch_error( $result, $server, $request ) {
     376
     377    // Bail early if the BP REST plugin is active.
     378    if ( bp_rest_is_plugin_active() ) {
     379        return $result;
     380    }
     381
     382    $route = $request->get_route();
     383
     384    if ( empty( $route ) || ! str_contains( $route, 'buddypress/v1' ) ) {
     385        return $result;
     386    }
     387
     388    return new WP_Error(
     389        'rest_no_route',
     390        __( 'The V1 of the BuddyPress REST API is no longer supported, use the V2 instead.', 'buddypress' ),
     391        array( 'status' => 404 )
     392    );
     393}
     394add_filter( 'rest_pre_dispatch', 'bp_rest_api_v1_dispatch_error', 10, 3 );
     395
     396/**
     397 * Register the jQuery.ajax wrapper for BP REST API requests.
     398 *
     399 * @since 5.0.0
     400 * @deprecated 10.0.0
     401 */
     402function bp_rest_api_register_request_script() {
     403    if ( ! bp_rest_api_is_available() ) {
     404        return;
     405    }
     406
     407    wp_register_script(
     408        'bp-api-request',
     409        sprintf( '%1$sbp-core/js/bp-api-request%2$s.js', buddypress()->plugin_url, bp_core_get_minified_asset_suffix() ),
     410        array( 'jquery', 'wp-api-request' ),
     411        bp_get_version(),
     412        true
     413    );
     414
     415    wp_localize_script(
     416        'bp-api-request',
     417        'bpApiSettings',
     418        array(
     419            'unexpectedError'   => __( 'An unexpected error occurred. Please try again.', 'buddypress' ),
     420            'deprecatedWarning' => __( 'The bp.apiRequest function is deprecated since BuddyPress 10.0.0, please use wp.apiRequest instead.', 'buddypress' ),
     421        )
     422    );
     423}
     424add_action( 'bp_init', 'bp_rest_api_register_request_script' );
Note: See TracChangeset for help on using the changeset viewer.