mirror of
https://ghproxy.net/https://github.com/fairpm/fair-plugin.git
synced 2025-09-04 08:50:35 +08:00
Fetch local metadata as needed
Signed-off-by: Andy Fragen <andy@thefragens.com>
This commit is contained in:
parent
37c9285c28
commit
aab980a433
1 changed files with 30 additions and 0 deletions
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue