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