Last active
May 13, 2019 02:27
-
-
Save Rarst/d75dbd6d9c77e5f33940d1e5abc931d0 to your computer and use it in GitHub Desktop.
Revisions
-
Rarst revised this gist
Nov 12, 2018 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -35,30 +35,30 @@ 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; -
Rarst revised this gist
Nov 12, 2018 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,30 +37,30 @@ public function template_include( $template ) { $repo = $wp_query->query[ $this->endpoint ] ?? ''; if ( ! in_array( $repo, $this->repos, true ) ) { return $template; } $key = "latest-release-{$this->owner}/{$repo}"; $url = "https://api.github.com/repos/{$this->owner}/{$repo}/releases/latest"; $release_url = "https://github.com/{$this->owner}/{$repo}/releases/latest"; $response = get_transient( $key ); if ( empty( $response ) ) { $response = wp_remote_get( $url ); $response = wp_remote_retrieve_body( $response ); if ( empty( $response ) ) { wp_redirect( $release_url ); die; } set_transient( $key, $response, 15 * MINUTE_IN_SECONDS ); } $object = json_decode( $response ); $redirect_url = $object->assets[0]->browser_download_url ?? $release_url; wp_redirect( $redirect_url ); die; } }; -
Rarst revised this gist
Nov 10, 2018 . No changes.There are no files selected for viewing
-
Rarst created this gist
Nov 10, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ <?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 = $wp_query->query[ $this->endpoint ] ?? ''; if ( empty( $repo ) || ! in_array( $repo, $this->repos, true ) ) { return $template; } $key = "latest-release={$this->owner}/{$repo}"; $url = "https://api.github.com/repos/{$this->owner}/{$repo}/releases/latest"; $response = get_transient( $key ); if ( empty( $response ) ) { $response = wp_remote_get( $url ); $response = wp_remote_retrieve_body( $response ); if ( empty( $response ) ) { wp_redirect( "https://github.com/{$this->owner}/{$repo}/releases/latest" ); die; } set_transient( $key, $response, 15 * MINUTE_IN_SECONDS ); } $object = json_decode( $response ); $download_url = $object->assets[0]->browser_download_url; wp_redirect( $download_url ); die; } };