mirror of
https://ghproxy.net/https://github.com/fairpm/fair-plugin.git
synced 2025-09-04 10:32:01 +08:00
addressing Ryan's comments
Signed-off-by: Andy Fragen <andy@thefragens.com>
This commit is contained in:
parent
8c9506eb66
commit
e1fddb4d6f
5 changed files with 54 additions and 55 deletions
|
@ -11,8 +11,7 @@ use FAIR\Packages;
|
|||
use FAIR\Packages\Admin;
|
||||
use FAIR\Packages\MetadataDocument;
|
||||
use FAIR\Packages\ReleaseDocument;
|
||||
|
||||
use function FAIR\Updater\get_packages;
|
||||
use FAIR\Updater;
|
||||
|
||||
/**
|
||||
* Sanitize HTML content for plugin information.
|
||||
|
@ -448,7 +447,7 @@ function get_action_button( MetadataDocument $doc, ReleaseDocument $release ) {
|
|||
);
|
||||
}
|
||||
|
||||
$file = get_packages()[ "{$type}s" ][ $doc->id ];
|
||||
$file = Updater\get_packages()[ "{$type}s" ][ $doc->id ];
|
||||
$file = $type === 'plugin' ? plugin_basename( $file ) : basename( dirname( $file ) );
|
||||
$slug = $type === 'plugin' ? dirname( $file ) : $file;
|
||||
|
||||
|
|
|
@ -70,8 +70,8 @@ function handle_did_during_ajax( $result, $action, $args ) {
|
|||
}
|
||||
|
||||
wp_cache_set( ACTION_INSTALL_DID, $did );
|
||||
Updater\add_package_to_release_cache( $did );
|
||||
add_filter( 'http_request_args', 'FAIR\\Updater\\maybe_add_accept_header', 20, 2 );
|
||||
Packages\add_package_to_release_cache( $did );
|
||||
add_filter( 'http_request_args', 'FAIR\\Packages\\maybe_add_accept_header', 20, 2 );
|
||||
|
||||
return (object) Packages\get_update_data( $did );
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ use WP_Upgrader;
|
|||
const SERVICE_ID = 'FairPackageManagementRepo';
|
||||
const CONTENT_TYPE = 'application/json+fair';
|
||||
const CACHE_LIFETIME = 12 * HOUR_IN_SECONDS;
|
||||
const RELEASE_PACKAGES_CACHE_KEY = 'fair-release-packages';
|
||||
|
||||
// phpcs:disable WordPress.NamingConventions.ValidVariableName
|
||||
|
||||
|
@ -624,4 +625,51 @@ function rename_source_selection( string $source, string $remote_source, WP_Upgr
|
|||
|
||||
return trailingslashit( $new_source );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add FAIR ReleaseDocument data to cache.
|
||||
*
|
||||
* @param string $did DID.
|
||||
* @return void
|
||||
*/
|
||||
function add_package_to_release_cache( string $did ) : void {
|
||||
if ( empty( $did ) ) {
|
||||
return;
|
||||
}
|
||||
$releases = wp_cache_get( RELEASE_PACKAGES_CACHE_KEY ) ?: [];
|
||||
$releases[ $did ] = get_latest_release_from_did( $did );
|
||||
wp_cache_set( RELEASE_PACKAGES_CACHE_KEY, $releases );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe add accept header for release asset package binary.
|
||||
*
|
||||
* ReleaseDocument artifact package content-type will be application/octet-stream.
|
||||
* Only for GitHub release assets.
|
||||
*
|
||||
* @param array $args Array of http args.
|
||||
* @param string $url Download URL.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function maybe_add_accept_header( $args, $url ) : array {
|
||||
$releases = wp_cache_get( RELEASE_PACKAGES_CACHE_KEY ) ?: [];
|
||||
|
||||
if ( ! str_contains( $url, 'api.github.com' ) ) {
|
||||
return $args;
|
||||
}
|
||||
|
||||
foreach ( $releases as $release ) {
|
||||
if ( $url === $release->artifacts->package[0]->url ) {
|
||||
$content_type = $release->artifacts->package[0]->{'content-type'};
|
||||
if ( $content_type === 'application/octet-stream' ) {
|
||||
$args = array_merge( $args, [ 'headers' => [ 'Accept' => $content_type ] ] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
// phpcs:enable
|
||||
|
|
|
@ -129,7 +129,7 @@ class Updater {
|
|||
add_filter( 'wp_prepare_themes_for_js', [ $this, 'customize_theme_update_html' ] );
|
||||
}
|
||||
|
||||
add_package_to_release_cache( $this->did );
|
||||
Packages\add_package_to_release_cache( $this->did );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,8 +9,6 @@ namespace FAIR\Updater;
|
|||
|
||||
use FAIR\Packages;
|
||||
|
||||
const RELEASE_PACKAGES_CACHE_KEY = 'fair-release-packages';
|
||||
|
||||
/**
|
||||
* Bootstrap.
|
||||
*/
|
||||
|
@ -19,21 +17,6 @@ function bootstrap() {
|
|||
add_filter( 'upgrader_pre_download', __NAMESPACE__ . '\\upgrader_pre_download', 10, 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add FAIR ReleaseDocument data to cache.
|
||||
*
|
||||
* @param string $did DID.
|
||||
* @return void
|
||||
*/
|
||||
function add_package_to_release_cache( string $did ) : void {
|
||||
if ( empty( $did ) ) {
|
||||
return;
|
||||
}
|
||||
$releases = wp_cache_get( RELEASE_PACKAGES_CACHE_KEY ) ?: [];
|
||||
$releases[ $did ] = Packages\get_latest_release_from_did( $did );
|
||||
wp_cache_set( RELEASE_PACKAGES_CACHE_KEY, $releases );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send upgrader_pre_download filter to maybe_add_accept_header().
|
||||
*
|
||||
|
@ -42,41 +25,10 @@ function add_package_to_release_cache( string $did ) : void {
|
|||
* @return bool
|
||||
*/
|
||||
function upgrader_pre_download( $false ) : bool {
|
||||
add_filter( 'http_request_args', __NAMESPACE__ . '\\maybe_add_accept_header', 20, 2 );
|
||||
add_filter( 'http_request_args', 'FAIR\\Packages\\maybe_add_accept_header', 20, 2 );
|
||||
return $false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe add accept header for release asset package binary.
|
||||
*
|
||||
* ReleaseDocument artifact package content-type will be application/octet-stream.
|
||||
* Only for GitHub release assets.
|
||||
*
|
||||
* @param array $args Array of http args.
|
||||
* @param string $url Download URL.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function maybe_add_accept_header( $args, $url ) : array {
|
||||
$releases = wp_cache_get( RELEASE_PACKAGES_CACHE_KEY ) ?: [];
|
||||
|
||||
if ( ! str_contains( $url, 'api.github.com' ) ) {
|
||||
return $args;
|
||||
}
|
||||
|
||||
foreach ( $releases as $release ) {
|
||||
if ( $url === $release->artifacts->package[0]->url ) {
|
||||
$content_type = $release->artifacts->package[0]->{'content-type'};
|
||||
if ( $content_type === 'application/octet-stream' ) {
|
||||
$args = array_merge( $args, [ 'headers' => [ 'Accept' => $content_type ] ] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather all plugins/themes with data in Update URI and DID header.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue