Changeset 14026 for trunk/src/bp-core/bp-core-rest-api.php
- Timestamp:
- 09/27/2024 09:11:27 PM (15 months ago)
- File:
-
- 1 edited
-
trunk/src/bp-core/bp-core-rest-api.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-rest-api.php
r14013 r14026 12 12 13 13 /** 14 * Is the BP REST plugin isactive?14 * Is the BP REST plugin active? 15 15 * 16 16 * @since 5.0.0 … … 23 23 24 24 /** 25 * Should we use the REST Endpoints of built BuddyPress?25 * Should we use BuddyPress core REST Endpoints? 26 26 * 27 27 * 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. 34 32 */ 35 33 function bp_rest_in_buddypress() { 36 return ! bp_ is_running_from_src_subdirectory() && ! bp_rest_is_plugin_active();34 return ! bp_rest_is_plugin_active(); 37 35 } 38 36 … … 55 53 * @param bool $api_is_available True if the BP REST API is available. False otherwise. 56 54 */ 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 } 89 57 90 58 /** … … 111 79 * 112 80 * @since 5.0.0 81 * @since 15.0.0 Version is now v2. 113 82 * 114 83 * @return string … … 120 89 * 121 90 * @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. 124 94 */ 125 return apply_filters( 'bp_rest_version', 'v 1' );95 return apply_filters( 'bp_rest_version', 'v2' ); 126 96 } 127 97 … … 198 168 * 199 169 * @param string $value Comma-separated list of group types. 200 * @return array|null 170 * @return array|null|string 201 171 */ 202 172 function bp_rest_sanitize_member_types( $value ) { … … 210 180 $valid_types = array_intersect( $types, $registered_types ); 211 181 212 return ( ! empty( $valid_types )) ? $valid_types : null;182 return ! empty( $valid_types ) ? $valid_types : null; 213 183 } 214 184 … … 219 189 * 220 190 * @param mixed $value Mixed value. 221 * @return WP_Error| bool191 * @return WP_Error|true 222 192 */ 223 193 function bp_rest_validate_member_types( $value ) { … … 244 214 } 245 215 } 216 217 return true; 246 218 } 247 219 … … 251 223 * @since 5.0.0 252 224 * 253 * @param string $ valueComma-separated list of group types.225 * @param string $group_types Comma-separated list of group types. 254 226 * @return array|null 255 227 */ 256 function bp_rest_sanitize_group_types( $ value) {257 if ( empty( $ value) ) {228 function bp_rest_sanitize_group_types( $group_types ) { 229 if ( empty( $group_types ) ) { 258 230 return null; 259 231 } 260 232 261 $types = explode( ',', $ value);233 $types = explode( ',', $group_types ); 262 234 $valid_types = array_intersect( $types, bp_groups_get_group_types() ); 263 235 … … 270 242 * @since 5.0.0 271 243 * 272 * @param mixed $ valueMixed value.244 * @param mixed $group_types Mixed value. 273 245 * @return WP_Error|bool 274 246 */ 275 function bp_rest_validate_group_types( $ value) {276 if ( empty( $ value) ) {247 function bp_rest_validate_group_types( $group_types ) { 248 if ( empty( $group_types ) ) { 277 249 return true; 278 250 } 279 251 280 $types = explode( ',', $ value);252 $types = explode( ',', $group_types ); 281 253 $registered_types = bp_groups_get_group_types(); 282 254 foreach ( $types as $type ) { … … 293 265 } 294 266 } 267 268 return true; 295 269 } 296 270 … … 388 362 return isset( $registered_fields[ $attribute ] ); 389 363 } 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 */ 375 function 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 } 394 add_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 */ 402 function 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 } 424 add_action( 'bp_init', 'bp_rest_api_register_request_script' );
Note: See TracChangeset
for help on using the changeset viewer.