Automattic\WooCommerce\Utilities

RestApiUtil::lazy_load_namespacepublicWC 1.0

Conditionally loads a REST API namespace based on the current route to improve performance.

This function implements lazy loading for WooCommerce REST API namespaces to prevent loading all controllers on every request. It checks if the current REST route matches the namespace in order for that namespace to be loaded. If the namespace does not match the current rest route, a callback will be registered to possibly load the namespace again on rest_pre_dispatch; this is done to allow the namespace to be loaded on the fly during rest_do_request()

Method of the class: RestApiUtil{}

Returns

null. Nothing (null).

Usage

$RestApiUtil = new RestApiUtil();
$RestApiUtil->lazy_load_namespace( $route_namespace, $callback );
$route_namespace(string) (required)
The namespace to check.
$callback(callable) (required)
The callback to execute if the namespace should be loaded.

RestApiUtil::lazy_load_namespace() code WC 10.4.3

public function lazy_load_namespace( string $route_namespace, callable $callback ) {
	/**
	 * Filter whether to lazy load the namespace.  When set to false, the namespace will be loaded immediately during initialization.
	 *
	 * @param bool   $should_lazy_load_namespace Whether to lazy load the namespace instead of loading immediately.
	 * @param string $route_namespace            The namespace.
	 *
	 * @since 10.3.0
	 */
	$should_lazy_load_namespace = apply_filters( 'woocommerce_rest_should_lazy_load_namespace', true, $route_namespace );
	if ( $should_lazy_load_namespace ) {
		$this->attach_lazy_loaded_namespace( $route_namespace, $callback );
	} else {
		call_user_func( $callback );
	}
}