Skip to:
Content

BuddyPress.org

Changeset 12454


Ignore:
Timestamp:
09/07/2019 05:26:24 PM (5 years ago)
Author:
imath
Message:

Improve the bp.apiRequest() JS function and some PHP code indentation

bp.apiRequest() will make sure to return the JSON response object in case of an error. In case this JSON response object is not available a default error object will be used.

When WordPress version is lower than 4.9, the same function will make sure to override the url option with the REST API root URL only if this option is not already set. Moreover the Nonce header is now only added for REST API requests made on the current site domain.

The PHP code indentation of bp-core/bp-core-rest-api.php file has been improved.

Fixes #8131

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-rest-api.php

    r12451 r12454  
    1919 */
    2020function bp_rest_is_plugin_active() {
    21     return (bool) has_action( 'bp_rest_api_init', 'bp_rest', 5 );
     21    return (bool) has_action( 'bp_rest_api_init', 'bp_rest', 5 );
    2222}
    2323
     
    3434 */
    3535function bp_rest_in_buddypress() {
    36     $is_src = defined( 'BP_SOURCE_SUBDIRECTORY' ) && BP_SOURCE_SUBDIRECTORY === 'src';
    37 
    38     return ! $is_src && ! bp_rest_is_plugin_active();
     36    $is_src = defined( 'BP_SOURCE_SUBDIRECTORY' ) && BP_SOURCE_SUBDIRECTORY === 'src';
     37
     38    return ! $is_src && ! bp_rest_is_plugin_active();
    3939}
    4040
     
    4848function bp_rest_api_is_available() {
    4949
    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();
     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();
    6060}
    6161
     
    6666 */
    6767function 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    );
    9496}
    9597add_action( 'bp_init', 'bp_rest_api_register_request_script' );
  • trunk/src/bp-core/js/bp-api-request.js

    r12419 r12454  
    1818    // Polyfill wp.apiRequest if WordPress < 4.9
    1919    bp.apiRequest = function( options ) {
     20        var bpRequest;
     21
    2022        if ( ! options.dataType ) {
    2123            options.dataType = 'json';
     
    2426        // WordPress is >= 4.9.0.
    2527        if ( wp.apiRequest ) {
    26             return wp.apiRequest( options );
     28            bpRequest = wp.apiRequest( options );
    2729
    2830        // WordPress is < 4.9.0.
     
    3436            }
    3537
    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                }
    3959            };
    4060
    41             return $.ajax( options );
    42         }
     61            if ( result && result.responseJSON ) {
     62                errorObject = result.responseJSON;
     63            }
     64
     65            return errorObject;
     66        } );
    4367    };
    4468
Note: See TracChangeset for help on using the changeset viewer.