<?php | |
declare( strict_types=1 ); | |
new class( 'Rarst', [ 'laps' ] ) { | |
private $owner, $repos; | |
private $endpoint = 'download'; | |
public function __construct( string $owner, array $repos ) { | |
$this->owner = $owner; | |
$this->repos = $repos; | |
add_action( 'init', [ $this, 'init' ] ); | |
} | |
public function init() { | |
add_rewrite_endpoint( $this->endpoint, EP_ROOT ); | |
add_filter( 'posts_where', [ $this, 'posts_where' ], 10, 2 ); | |
add_filter( 'template_include', [ $this, 'template_include' ], 9 ); | |
} | |
public function posts_where( $where, $query ) { | |
if ( isset( $query->query[ $this->endpoint ] ) ) { | |
return ' AND 1=0'; | |
} | |
return $where; | |
} | |
public function template_include( $template ) { | |
global $wp_query; | |
$repo_name = $wp_query->query[ $this->endpoint ] ?? ''; | |
if ( ! in_array( $repo_name, $this->repos, true ) ) { | |
return $template; | |
} | |
$transient_key = "latest-release-{$this->owner}/{$repo_name}"; | |
$api_url = "https://api.github.com/repos/{$this->owner}/{$repo_name}/releases/latest"; | |
$release_url = "https://github.com/{$this->owner}/{$repo_name}/releases/latest"; | |
$response = get_transient( $transient_key ); | |
if ( empty( $response ) ) { | |
$response = wp_remote_get( $api_url ); | |
$response = wp_remote_retrieve_body( $response ); | |
if ( empty( $response ) ) { | |
wp_redirect( $release_url ); | |
die; | |
} | |
set_transient( $transient_key, $response, 15 * MINUTE_IN_SECONDS ); | |
} | |
$response = json_decode( $response ); | |
$redirect_url = $response->assets[0]->browser_download_url ?? $release_url; | |
wp_redirect( $redirect_url ); | |
die; | |
} | |
}; |
This comment has been minimized.
This comment has been minimized.
I don't quite follow the difference about what you are doing and how AWS is involved. What my code is doing:
Could you please elaborate what do you think could be simplified here? |
This comment has been minimized.
This comment has been minimized.
The URL that leads to for the zipfile is I was using that for updating from release assets but because of the If you only need to send a client the release asset URL and have them download from the browser you’re OK. I was downloading the zipfile in WP core for updating and needed the AWS link. My mistake in thinking we were doing the same thing. |
This comment has been minimized.
I recently found an action hook that will show the
$download_url
. This is how I use it in GitHub Updater.https://github.com/afragen/github-updater/blob/1a05595b0ed0dcb0a8c625b5c42162853fe42fcc/src/GitHub_Updater/API.php#L786