wc_rest_should_load_namespace()
Check if a REST namespace should be loaded. Useful to maintain site performance even when lots of REST namespaces are registered.
Хуки из функции
Возвращает
true|false. True if the namespace should be loaded, false otherwise.
Использование
wc_rest_should_load_namespace( $ns, $rest_route ): bool;
- $ns(строка) (обязательный)
- The namespace to check.
- $rest_route(строка)
- (Optional) The REST route being checked.
По умолчанию: ''
Список изменений
| С версии 9.2.0. | Введена. |
Код wc_rest_should_load_namespace() wc rest should load namespace WC 10.4.2
function wc_rest_should_load_namespace( string $ns, string $rest_route = '' ): bool {
if ( '' === $rest_route ) {
$rest_route = $GLOBALS['wp']->query_vars['rest_route'] ?? '';
}
if ( '' === $rest_route ) {
return true;
}
$rest_route = trailingslashit( ltrim( $rest_route, '/' ) );
$ns = trailingslashit( $ns );
/**
* Known namespaces that we know are safe to not load if the request is not for them. Namespaces not in this namespace should always be loaded, because we don't know if they won't be making another internal REST request to an unloaded namespace.
*/
$known_namespaces = array(
'wc/v1',
'wc/v2',
'wc/v3',
'wc/v4',
'wc-telemetry',
'wc-admin',
'wc-analytics',
'wc/store',
'wc/private',
);
$known_namespace_request = false;
foreach ( $known_namespaces as $known_namespace ) {
if ( str_starts_with( $rest_route, $known_namespace ) ) {
$known_namespace_request = true;
break;
}
}
if ( ! $known_namespace_request ) {
return true;
}
/**
* Filters whether a namespace should be loaded.
*
* @param bool $should_load True if the namespace should be loaded, false otherwise.
* @param string $ns The namespace to check.
* @param string $rest_route The REST route being checked.
* @param array $known_namespaces Known namespaces that we know are safe to not load if the request is not for them.
*
* @since 9.4
*/
return apply_filters( 'wc_rest_should_load_namespace', str_starts_with( $rest_route, $ns ), $ns, $rest_route, $known_namespaces );
}