Changeset 12454
- Timestamp:
- 09/07/2019 05:26:24 PM (5 years ago)
- Location:
- trunk/src/bp-core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-rest-api.php
r12451 r12454 19 19 */ 20 20 function bp_rest_is_plugin_active() { 21 21 return (bool) has_action( 'bp_rest_api_init', 'bp_rest', 5 ); 22 22 } 23 23 … … 34 34 */ 35 35 function bp_rest_in_buddypress() { 36 37 38 36 $is_src = defined( 'BP_SOURCE_SUBDIRECTORY' ) && BP_SOURCE_SUBDIRECTORY === 'src'; 37 38 return ! $is_src && ! bp_rest_is_plugin_active(); 39 39 } 40 40 … … 48 48 function bp_rest_api_is_available() { 49 49 50 51 52 53 54 55 56 57 58 59 50 /** 51 * Filter here to disable the BP REST API. 52 * 53 * The BP REST API requires at least WordPress 4.7.0 54 * 55 * @since 5.0.0 56 * 57 * @param boolean $value True if the BP REST API is available. False otherwise. 58 */ 59 return apply_filters( 'bp_rest_api_is_available', function_exists( 'create_initial_rest_routes' ) && bp_rest_in_buddypress() ) || bp_rest_is_plugin_active(); 60 60 } 61 61 … … 66 66 */ 67 67 function bp_rest_api_register_request_script() { 68 if ( ! bp_rest_api_is_available() ) { 69 return; 70 } 71 72 $dependencies = array( 'jquery' ); 73 74 // The wrapper for WP REST API requests was introduced in WordPress 4.9.0 75 if ( wp_script_is( 'wp-api-request', 'registered' ) ) { 76 $dependencies = array( 'wp-api-request' ); 77 } 78 79 wp_register_script( 80 'bp-api-request', 81 sprintf( '%1$sbp-core/js/bp-api-request%2$s.js', buddypress()->plugin_url, bp_core_get_minified_asset_suffix() ), 82 $dependencies, 83 bp_get_version(), 84 true 85 ); 86 wp_localize_script( 87 'bp-api-request', 88 'bpApiSettings', 89 array( 90 'root' => esc_url_raw( get_rest_url() ), 91 'nonce' => wp_create_nonce( 'wp_rest' ), 92 ) 93 ); 68 if ( ! bp_rest_api_is_available() ) { 69 return; 70 } 71 72 $dependencies = array( 'jquery' ); 73 74 // The wrapper for WP REST API requests was introduced in WordPress 4.9.0. 75 if ( wp_script_is( 'wp-api-request', 'registered' ) ) { 76 $dependencies = array( 'wp-api-request' ); 77 } 78 79 wp_register_script( 80 'bp-api-request', 81 sprintf( '%1$sbp-core/js/bp-api-request%2$s.js', buddypress()->plugin_url, bp_core_get_minified_asset_suffix() ), 82 $dependencies, 83 bp_get_version(), 84 true 85 ); 86 87 wp_localize_script( 88 'bp-api-request', 89 'bpApiSettings', 90 array( 91 'root' => esc_url_raw( get_rest_url() ), 92 'nonce' => wp_create_nonce( 'wp_rest' ), 93 'unexpectedError' => __( 'An unexpected error occured. Please try again.', 'buddypress' ), 94 ) 95 ); 94 96 } 95 97 add_action( 'bp_init', 'bp_rest_api_register_request_script' ); -
trunk/src/bp-core/js/bp-api-request.js
r12419 r12454 18 18 // Polyfill wp.apiRequest if WordPress < 4.9 19 19 bp.apiRequest = function( options ) { 20 var bpRequest; 21 20 22 if ( ! options.dataType ) { 21 23 options.dataType = 'json'; … … 24 26 // WordPress is >= 4.9.0. 25 27 if ( wp.apiRequest ) { 26 returnwp.apiRequest( options );28 bpRequest = wp.apiRequest( options ); 27 29 28 30 // WordPress is < 4.9.0. … … 34 36 } 35 37 36 options.url = url; 37 options.beforeSend = function( xhr ) { 38 xhr.setRequestHeader( 'X-WP-Nonce', bpApiSettings.nonce ); 38 if ( ! options.url ) { 39 options.url = url; 40 } 41 42 // Add The nonce only when needed. 43 if ( -1 !== options.url.indexOf( url ) ) { 44 options.beforeSend = function( xhr ) { 45 xhr.setRequestHeader( 'X-WP-Nonce', bpApiSettings.nonce ); 46 }; 47 } 48 49 bpRequest = $.ajax( options ); 50 } 51 52 return bpRequest.then( null, function( result ) { 53 var errorObject = { 54 code: 'unexpected_error', 55 message: bpApiSettings.unexpectedError, 56 data: { 57 status: 404 58 } 39 59 }; 40 60 41 return $.ajax( options ); 42 } 61 if ( result && result.responseJSON ) { 62 errorObject = result.responseJSON; 63 } 64 65 return errorObject; 66 } ); 43 67 }; 44 68
Note: See TracChangeset
for help on using the changeset viewer.