Fetch local metadata as needed

Signed-off-by: Andy Fragen <andy@thefragens.com>
This commit is contained in:
Andy Fragen 2025-08-15 08:43:22 -07:00
parent 37c9285c28
commit aab980a433

View file

@ -144,6 +144,7 @@ function fetch_package_metadata( string $id ) {
function fetch_metadata_doc( string $url ) {
$cache_key = md5( $url );
$response = get_site_transient( $cache_key );
$response = fetch_metadata_from_local( $response, $url );
if ( ! $response ) {
$response = wp_remote_get( $url, [
@ -164,6 +165,35 @@ function fetch_metadata_doc( string $url ) {
return MetadataDocument::from_response( $response );
}
/**
* Fetch Metadata from local source.
*
* Solves issue where Metadata source is from same
* site. Mini-FAIR REST endpoint times out under these circumstances.
*
* @param bool|array $response Response from cache.
* @param string $url URI for Metadata.
* @return bool|array
*/
function fetch_metadata_from_local( $response, $url ) {
if ( ! $response ) {
if ( str_contains( $url, home_url() ) ) {
$did = explode( '/', parse_url( $url, PHP_URL_PATH ) );
$did = array_pop( $did );
$body = wp_cache_get( 'rest-endpoint-' . $did, 'metadata-endpoints' );
$response = [];
$response['headers'] = [];
$response['body'] = json_encode( $body );
$response = ! $body ? false : $response;
if ( $response ) {
wp_cache_set( CACHE_KEY . md5( $url ), $response, 'metadata-docs', CACHE_LIFETIME );
}
}
}
return $response;
}
/**
* Select the best release from a list of releases.
*