mirror of
https://ghproxy.net/https://github.com/fairpm/fair-plugin.git
synced 2025-09-04 10:32:01 +08:00
Merge e8f58a6c48
into 6ca4e6e73b
This commit is contained in:
commit
6882ebb96b
1 changed files with 30 additions and 1 deletions
|
@ -145,13 +145,13 @@ function fetch_package_metadata( string $id ) {
|
|||
function fetch_metadata_doc( string $url ) {
|
||||
$cache_key = CACHE_KEY . md5( $url );
|
||||
$response = wp_cache_get( $cache_key, 'metadata-docs' );
|
||||
$response = fetch_metadata_from_local( $response, $url );
|
||||
|
||||
if ( ! $response ) {
|
||||
$response = wp_remote_get( $url, [
|
||||
'headers' => [
|
||||
'Accept' => sprintf( '%s;q=1.0, application/json;q=0.8', CONTENT_TYPE ),
|
||||
],
|
||||
'timeout' => 7,
|
||||
] );
|
||||
$code = wp_remote_retrieve_response_code( $response );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
|
@ -165,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 may time 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 && str_contains( $url, home_url() ) ) {
|
||||
$did = explode( '/', parse_url( $url, PHP_URL_PATH ) );
|
||||
$did = array_pop( $did );
|
||||
$body = wp_cache_get( 'fair-metadata-endpoint-' . $did, 'metadata-endpoints' );
|
||||
$response = [];
|
||||
$response = [
|
||||
'headers' => [],
|
||||
'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