Automattic\WooCommerce\Internal\Ucp
Serves the UCP discovery profile at /.well-known/ucp.
Хуков нет.
Использование
$UcpDiscovery = new UcpDiscovery();
// use class methods
Методы
- public add_query_var( $vars )
- public get_profile()
- public maybe_flush_rewrite_rules( string $feature_id )
- public maybe_render( \WP $wp )
- public register()
- public register_feature_hooks()
Код UcpDiscovery{} UcpDiscovery{}
WC 11.3.0
final class UcpDiscovery implements RegisterHooksInterface {
/**
* Query var set by the `/.well-known/ucp` rewrite rule.
*/
public const QUERY_VAR = 'wc_ucp_discovery';
/**
* Rewrite rule regex for `/.well-known/ucp`.
*/
public const RULE = '^\.well-known/ucp/?$';
/**
* Register hooks.
*/
public function register(): void {
add_action( FeaturesController::FEATURE_ENABLED_CHANGED_ACTION, array( $this, 'maybe_flush_rewrite_rules' ), 10, 1 );
add_action( 'init', array( $this, 'register_feature_hooks' ), 0, 0 );
}
/**
* Register the rewrite rule and renderer when UCP is enabled and pretty permalinks are on.
*/
public function register_feature_hooks(): void {
global $wp_rewrite;
if ( ! UcpUtils::is_enabled() || ! $wp_rewrite->using_permalinks() ) {
return;
}
add_rewrite_rule( self::RULE, 'index.php?' . self::QUERY_VAR . '=1', 'top' );
add_filter( 'query_vars', array( $this, 'add_query_var' ), 0 );
add_action( 'parse_request', array( $this, 'maybe_render' ), 0 );
}
/**
* Register the discovery query var.
*
* @param array $vars Public query vars.
* @return array
*/
public function add_query_var( $vars ): array {
$vars[] = self::QUERY_VAR;
return $vars;
}
/**
* Queue a rewrite rules flush when the UCP feature is toggled.
*
* @param string $feature_id Feature being toggled.
*/
public function maybe_flush_rewrite_rules( string $feature_id ): void {
if ( UcpUtils::FEATURE_ID === $feature_id ) {
update_option( 'woocommerce_queue_flush_rewrite_rules', 'yes' );
}
}
/**
* Send the discovery profile for `/.well-known/ucp` requests.
*
* @param \WP $wp Current WordPress environment instance.
*/
public function maybe_render( \WP $wp ): void {
// Check the matched rule, not only the query var: WP also fills public query vars from $_GET/$_POST.
if ( self::RULE !== $wp->matched_rule || '1' !== ( $wp->query_vars[ self::QUERY_VAR ] ?? '' ) ) {
return;
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
if ( ! in_array( $_SERVER['REQUEST_METHOD'] ?? '', array( 'GET', 'HEAD' ), true ) ) {
status_header( 405 );
exit;
}
// UCP requires a publicly cacheable profile with a max-age of at least 60 seconds.
if ( ! headers_sent() ) {
header( 'Cache-Control: public, max-age=300' );
}
wp_send_json( $this->get_profile() );
}
/**
* Build the discovery profile.
*
* @return array
*/
public function get_profile(): array {
return array(
'ucp' => array(
'version' => UcpUtils::VERSION,
'services' => array(
'dev.ucp.shopping' => array(
array(
'version' => UcpUtils::VERSION,
'transport' => 'rest',
'endpoint' => rest_url( 'wc/ucp/v1' ),
'spec' => 'https://ucp.dev/' . UcpUtils::VERSION . '/specification/overview',
'schema' => 'https://ucp.dev/' . UcpUtils::VERSION . '/services/shopping/rest.openapi.json',
),
),
),
'capabilities' => array_map(
static function ( array $declaration ): array {
return array( $declaration );
},
UcpUtils::capability_declarations()
),
),
'links' => array(
array(
'rel' => 'self',
'href' => home_url( '/.well-known/ucp' ),
),
),
);
}
}