Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Utilitie...
File: RestApiUtil.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Automattic\WooCommerce\Utilities;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Utility methods related to the REST API.
[5] Fix | Delete
*/
[6] Fix | Delete
class RestApiUtil {
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Get data from a WooCommerce API endpoint.
[10] Fix | Delete
* This method used to be part of the WooCommerce Legacy REST API.
[11] Fix | Delete
*
[12] Fix | Delete
* @since 9.0.0
[13] Fix | Delete
*
[14] Fix | Delete
* @param string $endpoint Endpoint.
[15] Fix | Delete
* @param array $params Params to pass with request.
[16] Fix | Delete
* @return array|\WP_Error
[17] Fix | Delete
*/
[18] Fix | Delete
public function get_endpoint_data( $endpoint, $params = array() ) {
[19] Fix | Delete
$request = new \WP_REST_Request( 'GET', $endpoint );
[20] Fix | Delete
if ( $params ) {
[21] Fix | Delete
$request->set_query_params( $params );
[22] Fix | Delete
}
[23] Fix | Delete
$response = rest_do_request( $request );
[24] Fix | Delete
$server = rest_get_server();
[25] Fix | Delete
$json = wp_json_encode( $server->response_to_data( $response, false ) );
[26] Fix | Delete
return json_decode( $json, true );
[27] Fix | Delete
}
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* Conditionally loads a REST API namespace based on the current route to improve performance.
[31] Fix | Delete
*
[32] Fix | Delete
* This function implements lazy loading for WooCommerce REST API namespaces to prevent loading
[33] Fix | Delete
* all controllers on every request. It checks if the current REST route matches the namespace
[34] Fix | Delete
* in order for that namespace to be loaded. If the namespace does not match the current rest
[35] Fix | Delete
* route, a callback will be registered to possibly load the namespace again on `rest_pre_dispatch`;
[36] Fix | Delete
* this is done to allow the namespace to be loaded on the fly during `rest_do_request()` calls.
[37] Fix | Delete
*
[38] Fix | Delete
* @param string $route_namespace The namespace to check.
[39] Fix | Delete
* @param callable $callback The callback to execute if the namespace should be loaded.
[40] Fix | Delete
*
[41] Fix | Delete
* @return void
[42] Fix | Delete
*
[43] Fix | Delete
* @internal Do not call this function directly. Backward compatibility is not guaranteed.
[44] Fix | Delete
*/
[45] Fix | Delete
public function lazy_load_namespace( string $route_namespace, callable $callback ) {
[46] Fix | Delete
/**
[47] Fix | Delete
* Filter whether to lazy load the namespace. When set to false, the namespace will be loaded immediately during initialization.
[48] Fix | Delete
*
[49] Fix | Delete
* @param bool $should_lazy_load_namespace Whether to lazy load the namespace instead of loading immediately.
[50] Fix | Delete
* @param string $route_namespace The namespace.
[51] Fix | Delete
*
[52] Fix | Delete
* @since 10.3.0
[53] Fix | Delete
*/
[54] Fix | Delete
$should_lazy_load_namespace = apply_filters( 'woocommerce_rest_should_lazy_load_namespace', true, $route_namespace );
[55] Fix | Delete
if ( $should_lazy_load_namespace ) {
[56] Fix | Delete
$this->attach_lazy_loaded_namespace( $route_namespace, $callback );
[57] Fix | Delete
} else {
[58] Fix | Delete
call_user_func( $callback );
[59] Fix | Delete
}
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* This is the internal function that implements the logic of self::lazy_load_namespace(). Its interface
[64] Fix | Delete
* and behavior is not guaranteed. It solely exists so that $callback_filter_id does not need to be part of the
[65] Fix | Delete
* public interface to `self::lazy_load_namespace()`. Do not call it directly.
[66] Fix | Delete
*
[67] Fix | Delete
* @param string $route_namespace The namespace to check.
[68] Fix | Delete
* @param callable $callback The callback to execute if the namespace should be loaded.
[69] Fix | Delete
* @param string $rest_route (Optional) The REST route to check against.
[70] Fix | Delete
* @param string $callback_filter_id (Internal) Used to prevent recursive filter registration.
[71] Fix | Delete
*
[72] Fix | Delete
* @return void
[73] Fix | Delete
*
[74] Fix | Delete
* @see self::lazy_load_namespace()
[75] Fix | Delete
* @internal Do not call this function directly. Backward compatibility is not guaranteed.
[76] Fix | Delete
*/
[77] Fix | Delete
public function attach_lazy_loaded_namespace( string $route_namespace, callable $callback, string $rest_route = '', string $callback_filter_id = '' ) {
[78] Fix | Delete
if ( '' === $rest_route && isset( $GLOBALS['wp'] ) && is_object( $GLOBALS['wp'] ) ) {
[79] Fix | Delete
$rest_route = $GLOBALS['wp']->query_vars['rest_route'] ?? '';
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
if ( '' !== $rest_route ) {
[83] Fix | Delete
$rest_route = trailingslashit( ltrim( $rest_route, '/' ) );
[84] Fix | Delete
$route_namespace = trailingslashit( $route_namespace );
[85] Fix | Delete
if ( '/' === $rest_route || str_starts_with( $rest_route, $route_namespace ) ) {
[86] Fix | Delete
// Load all namespaces for root requests (/wp-json/) to maintain API discovery functionality.
[87] Fix | Delete
if ( '' !== $callback_filter_id ) {
[88] Fix | Delete
// Remove the current filter prior to the callback, to prevent recursive callback issues.
[89] Fix | Delete
// This is crucial for APIs like wc-analytics that may callback to their own namespace when loading.
[90] Fix | Delete
remove_filter( 'rest_pre_dispatch', $callback_filter_id, 0 );
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
call_user_func( $callback );
[94] Fix | Delete
[95] Fix | Delete
return;
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
// Register a filter to check again on rest_pre_dispatch for dynamic loading.
[100] Fix | Delete
if ( '' === $callback_filter_id ) {
[101] Fix | Delete
$callback_filter = function ( $filter_result, $server, $request ) use ( $route_namespace, $callback, &$callback_filter_id ) {
[102] Fix | Delete
if ( is_callable( array( $request, 'get_route' ) ) ) {
[103] Fix | Delete
$this->attach_lazy_loaded_namespace( $route_namespace, $callback, $request->get_route(), $callback_filter_id );
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
return $filter_result;
[107] Fix | Delete
};
[108] Fix | Delete
$callback_filter_id = _wp_filter_build_unique_id( 'rest_pre_dispatch', $callback_filter, 0 );
[109] Fix | Delete
/**
[110] Fix | Delete
* The `rest_handle_options_request()` function only works correctly if all REST API routes are registered. To ensure
[111] Fix | Delete
* our routes are available in time, we must load the namespace before `rest_handle_options_request()` runs
[112] Fix | Delete
* (which happens at priority 10).
[113] Fix | Delete
*/
[114] Fix | Delete
add_filter( 'rest_pre_dispatch', $callback_filter, 0, 3 );
[115] Fix | Delete
[116] Fix | Delete
}
[117] Fix | Delete
}
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function