* @copyright 2026 webhead GmbH */ declare( strict_types=1 ); namespace Webhead\WP\SearchCloud\Frontend; use Webhead\WP\SearchCloud\Dependencies\Webhead\WP\Framework\Attributes\Filter; defined( 'ABSPATH' ) || exit; /** * @since 1.4.0 */ final class Resource_Hints { /** * @param string $handle The storefront script's handle. * @param string $service Searcio's address, whose origin the storefront asks. */ public function __construct( private string $handle, private string $service ) {} /** * @param mixed $urls The hints of `$relation`. * @param mixed $relation `preconnect`, `dns-prefetch` and so on. */ #[Filter( 'wp_resource_hints', accepted_args: 2 )] public function preconnect( mixed $urls, mixed $relation = '' ): mixed { if ( 'preconnect' !== $relation || ! is_array( $urls ) || ! wp_script_is( $this->handle, 'enqueued' ) ) { return $urls; } $origin = self::origin( $this->service ); if ( '' === $origin || self::origin( home_url( '/' ) ) === $origin ) { return $urls; } $urls[] = [ 'href' => $origin, 'crossorigin' => 'anonymous', ]; return $urls; } /** * `scheme://host[:port]` of `$url`, or '' when it has none. */ public static function origin( string $url ): string { $parts = wp_parse_url( $url ); if ( ! is_array( $parts ) || empty( $parts['scheme'] ) || empty( $parts['host'] ) ) { return ''; } return strtolower( $parts['scheme'] . '://' . $parts['host'] ) . ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ); } }