Add in-WordPress updater: checks Gitea releases, one-click and auto updates
- Update\ReleaseParser (pure, host-pinned, https only) and Update\Updater (WP hooks, cached, failure-safe) - Settings: optional access token for a private repo, 'Check for updates now' Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
27646c1001
commit
4cec93067c
7 files changed
+453
No files matched your search
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Update;
|
||||
|
||||
/**
|
||||
* Pure (WordPress-free) parsing of a Gitea release payload.
|
||||
*/
|
||||
final class ReleaseParser {
|
||||
|
||||
public const ASSET_NAME = 'streamgoals-donate.zip';
|
||||
|
||||
private const DATE_TAG = '/^\d{4}\.\d{2}\.\d{2}-\d{4}$/';
|
||||
private const SEMVER = '/^\d+\.\d+\.\d+$/';
|
||||
|
||||
/**
|
||||
* @param array $release Decoded Gitea release JSON.
|
||||
* @param string $host The only host a package URL may point to (e.g. repo.anhonesthost.net).
|
||||
* @return array{version:string,package:string,url:string,notes:string,published:string}|null
|
||||
*/
|
||||
public static function parse( array $release, string $host ): ?array {
|
||||
if ( ! empty( $release['draft'] ) || ! empty( $release['prerelease'] ) ) {
|
||||
return null;
|
||||
}
|
||||
$version = self::normalizeVersion( $release['tag_name'] ?? null );
|
||||
if ( null === $version ) {
|
||||
return null;
|
||||
}
|
||||
$assets = isset( $release['assets'] ) && is_array( $release['assets'] ) ? $release['assets'] : array();
|
||||
$package = self::pickPackage( $assets, $host );
|
||||
if ( null === $package ) {
|
||||
return null;
|
||||
}
|
||||
$url = $release['html_url'] ?? '';
|
||||
return array(
|
||||
'version' => $version,
|
||||
'package' => $package,
|
||||
'url' => is_string( $url ) && self::isAllowedUrl( $url, $host ) ? $url : '',
|
||||
'notes' => is_string( $release['body'] ?? null ) ? $release['body'] : '',
|
||||
'published' => is_string( $release['published_at'] ?? null ) ? $release['published_at'] : '',
|
||||
);
|
||||
}
|
||||
|
||||
public static function normalizeVersion( mixed $tag ): ?string {
|
||||
if ( ! is_string( $tag ) ) {
|
||||
return null;
|
||||
}
|
||||
$t = trim( $tag );
|
||||
if ( '' !== $t && ( 'v' === $t[0] || 'V' === $t[0] ) ) {
|
||||
$t = substr( $t, 1 );
|
||||
}
|
||||
return 1 === preg_match( self::DATE_TAG, $t ) || 1 === preg_match( self::SEMVER, $t ) ? $t : null;
|
||||
}
|
||||
|
||||
public static function isNewer( string $remote, string $current ): bool {
|
||||
$r = self::normalizeVersion( $remote );
|
||||
if ( null === $r ) {
|
||||
return false;
|
||||
}
|
||||
return version_compare( $r, ltrim( $current, 'vV' ), '>' );
|
||||
}
|
||||
|
||||
/** https URL on exactly the configured host (no userinfo tricks). */
|
||||
public static function isAllowedUrl( string $url, string $host ): bool {
|
||||
$p = parse_url( $url );
|
||||
if ( ! is_array( $p ) || ! isset( $p['scheme'], $p['host'] ) || isset( $p['user'] ) || isset( $p['pass'] ) ) {
|
||||
return false;
|
||||
}
|
||||
return 'https' === strtolower( $p['scheme'] ) && '' !== $host && strtolower( $p['host'] ) === strtolower( $host );
|
||||
}
|
||||
|
||||
private static function pickPackage( array $assets, string $host ): ?string {
|
||||
$exact = null;
|
||||
$first = null;
|
||||
foreach ( $assets as $a ) {
|
||||
if ( ! is_array( $a ) || ! is_string( $a['name'] ?? null ) || ! is_string( $a['browser_download_url'] ?? null ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( ! self::isAllowedUrl( $a['browser_download_url'], $host ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( self::ASSET_NAME === $a['name'] ) {
|
||||
$exact = $exact ?? $a['browser_download_url'];
|
||||
} elseif ( null === $first && str_ends_with( strtolower( $a['name'] ), '.zip' ) ) {
|
||||
$first = $a['browser_download_url'];
|
||||
}
|
||||
}
|
||||
return $exact ?? $first;
|
||||
}
|
||||
|
||||
/** Minimal markdown to safe HTML: everything is escaped first; only paragraphs, headings (as <h4>) and lists. */
|
||||
public static function changelogHtml( string $markdown ): string {
|
||||
$lines = preg_split( '/\r\n|\r|\n/', $markdown ) ?: array();
|
||||
$out = '';
|
||||
$list = false;
|
||||
$para = array();
|
||||
$flush = static function () use ( &$out, &$para ): void {
|
||||
if ( $para ) {
|
||||
$out .= '<p>' . implode( ' ', $para ) . '</p>';
|
||||
$para = array();
|
||||
}
|
||||
};
|
||||
foreach ( $lines as $line ) {
|
||||
$t = trim( $line );
|
||||
if ( 1 === preg_match( '/^[*\-+]\s+(.*)$/', $t, $m ) ) {
|
||||
$flush();
|
||||
if ( ! $list ) {
|
||||
$out .= '<ul>';
|
||||
$list = true;
|
||||
}
|
||||
$out .= '<li>' . self::esc( $m[1] ) . '</li>';
|
||||
continue;
|
||||
}
|
||||
if ( $list ) {
|
||||
$out .= '</ul>';
|
||||
$list = false;
|
||||
}
|
||||
if ( '' === $t ) {
|
||||
$flush();
|
||||
} elseif ( 1 === preg_match( '/^#{1,6}\s+(.*)$/', $t, $m ) ) {
|
||||
$flush();
|
||||
$out .= '<h4>' . self::esc( $m[1] ) . '</h4>';
|
||||
} else {
|
||||
$para[] = self::esc( $t );
|
||||
}
|
||||
}
|
||||
$flush();
|
||||
if ( $list ) {
|
||||
$out .= '</ul>';
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
private static function esc( string $s ): string {
|
||||
return htmlspecialchars( $s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Update;
|
||||
|
||||
use StreamGoals\Admin\Admin;
|
||||
use StreamGoals\Core\Settings;
|
||||
|
||||
/**
|
||||
* Self-updater: checks the Gitea repo's latest release and feeds WordPress' plugin update machinery.
|
||||
* Every network/parse failure degrades to "no update known"; nothing here throws into WP screens.
|
||||
*/
|
||||
final class Updater {
|
||||
|
||||
public const SLUG = 'streamgoals-donate';
|
||||
public const BASE_URL = 'https://repo.anhonesthost.net';
|
||||
public const OWNER = 'wp-plugins';
|
||||
public const REPO = 'wp-donations';
|
||||
public const CACHE_KEY = 'sg_update_release';
|
||||
public const CACHE_OK = HOUR_IN_SECONDS;
|
||||
public const CACHE_FAIL = 10 * MINUTE_IN_SECONDS;
|
||||
public const TIMEOUT = 8;
|
||||
public const ACTION_CHECK = 'sg_update_check';
|
||||
|
||||
public static function register(): void {
|
||||
if ( ! self::enabled() ) {
|
||||
return;
|
||||
}
|
||||
add_filter( 'pre_set_site_transient_update_plugins', array( self::class, 'inject' ) );
|
||||
add_filter( 'plugins_api', array( self::class, 'pluginInfo' ), 10, 3 );
|
||||
add_filter( 'http_request_args', array( self::class, 'authorize' ), 10, 2 );
|
||||
add_action( 'upgrader_process_complete', array( self::class, 'afterUpgrade' ), 10, 2 );
|
||||
add_action( 'admin_post_' . self::ACTION_CHECK, array( self::class, 'handleCheck' ) );
|
||||
}
|
||||
|
||||
public static function enabled(): bool {
|
||||
return (bool) apply_filters( 'streamgoals_updates_enabled', true );
|
||||
}
|
||||
|
||||
/** @return array{base:string,owner:string,repo:string,host:string} */
|
||||
public static function repo(): array {
|
||||
$r = apply_filters( 'streamgoals_update_repo', array( 'base' => self::BASE_URL, 'owner' => self::OWNER, 'repo' => self::REPO ) );
|
||||
$r = is_array( $r ) ? $r : array();
|
||||
$base = rtrim( is_string( $r['base'] ?? null ) ? $r['base'] : self::BASE_URL, '/' );
|
||||
$host = parse_url( $base, PHP_URL_HOST );
|
||||
return array(
|
||||
'base' => $base,
|
||||
'owner' => is_string( $r['owner'] ?? null ) ? $r['owner'] : self::OWNER,
|
||||
'repo' => is_string( $r['repo'] ?? null ) ? $r['repo'] : self::REPO,
|
||||
'host' => is_string( $host ) ? strtolower( $host ) : '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cached latest release (parsed) or null. Failures are cached briefly so WP never re-hits a broken endpoint.
|
||||
*
|
||||
* @return array{version:string,package:string,url:string,notes:string,published:string}|null
|
||||
*/
|
||||
public static function latest( bool $force = false ): ?array {
|
||||
try {
|
||||
if ( ! $force ) {
|
||||
$c = get_site_transient( self::CACHE_KEY );
|
||||
if ( is_array( $c ) && array_key_exists( 'release', $c ) ) {
|
||||
return is_array( $c['release'] ) ? $c['release'] : null;
|
||||
}
|
||||
}
|
||||
$release = self::fetch();
|
||||
set_site_transient( self::CACHE_KEY, array( 'release' => $release ), null === $release ? self::CACHE_FAIL : self::CACHE_OK );
|
||||
return $release;
|
||||
} catch ( \Throwable $e ) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static function clearCache(): void {
|
||||
delete_site_transient( self::CACHE_KEY );
|
||||
}
|
||||
|
||||
private static function fetch(): ?array {
|
||||
$repo = self::repo();
|
||||
if ( '' === $repo['host'] || 'https' !== strtolower( (string) parse_url( $repo['base'], PHP_URL_SCHEME ) ) ) {
|
||||
return null;
|
||||
}
|
||||
$url = $repo['base'] . '/api/v1/repos/' . rawurlencode( $repo['owner'] ) . '/' . rawurlencode( $repo['repo'] ) . '/releases/latest';
|
||||
$resp = wp_remote_get( $url, array( 'timeout' => self::TIMEOUT, 'redirection' => 2, 'headers' => array( 'Accept' => 'application/json' ) ) );
|
||||
if ( is_wp_error( $resp ) || 200 !== (int) wp_remote_retrieve_response_code( $resp ) ) {
|
||||
return null;
|
||||
}
|
||||
$data = json_decode( (string) wp_remote_retrieve_body( $resp ), true );
|
||||
return is_array( $data ) ? ReleaseParser::parse( $data, $repo['host'] ) : null;
|
||||
}
|
||||
|
||||
public static function pluginFile(): string {
|
||||
return plugin_basename( STREAMGOALS_FILE );
|
||||
}
|
||||
|
||||
private static function entry( array $rel ): object {
|
||||
$e = new \stdClass();
|
||||
$e->id = $rel['url'] ?: self::BASE_URL;
|
||||
$e->slug = self::SLUG;
|
||||
$e->plugin = self::pluginFile();
|
||||
$e->new_version = $rel['version'];
|
||||
$e->url = $rel['url'];
|
||||
$e->package = $rel['package'];
|
||||
$e->requires = '6.8';
|
||||
$e->requires_php = '8.1';
|
||||
return $e;
|
||||
}
|
||||
|
||||
public static function inject( mixed $transient ): mixed {
|
||||
try {
|
||||
if ( ! is_object( $transient ) ) {
|
||||
return $transient;
|
||||
}
|
||||
$rel = self::latest();
|
||||
$file = self::pluginFile();
|
||||
unset( $transient->response[ $file ], $transient->no_update[ $file ] );
|
||||
if ( null === $rel ) {
|
||||
return $transient;
|
||||
}
|
||||
$entry = self::entry( $rel );
|
||||
if ( ReleaseParser::isNewer( $rel['version'], STREAMGOALS_VERSION ) ) {
|
||||
$transient->response = is_array( $transient->response ?? null ) ? $transient->response : array();
|
||||
$transient->response[ $file ] = $entry;
|
||||
} else {
|
||||
$entry->new_version = STREAMGOALS_VERSION;
|
||||
$transient->no_update = is_array( $transient->no_update ?? null ) ? $transient->no_update : array();
|
||||
$transient->no_update[ $file ] = $entry;
|
||||
}
|
||||
} catch ( \Throwable $e ) {
|
||||
// Never break the Updates screen.
|
||||
}
|
||||
return $transient;
|
||||
}
|
||||
|
||||
public static function pluginInfo( mixed $result, mixed $action = '', mixed $args = null ): mixed {
|
||||
try {
|
||||
if ( 'plugin_information' !== $action || ! is_object( $args ) || ( $args->slug ?? '' ) !== self::SLUG ) {
|
||||
return $result;
|
||||
}
|
||||
$rel = self::latest();
|
||||
if ( null === $rel ) {
|
||||
return $result;
|
||||
}
|
||||
$i = new \stdClass();
|
||||
$i->name = 'StreamGoals Donate';
|
||||
$i->slug = self::SLUG;
|
||||
$i->version = $rel['version'];
|
||||
$i->author = 'StreamGoals';
|
||||
$i->homepage = $rel['url'];
|
||||
$i->requires = '6.8';
|
||||
$i->requires_php = '8.1';
|
||||
$i->last_updated = $rel['published'];
|
||||
$i->download_link = $rel['package'];
|
||||
$i->sections = array(
|
||||
'description' => 'PayPal donations with multiple goals, fee coverage, and OBS stream overlays.',
|
||||
'changelog' => '' !== $rel['notes'] ? ReleaseParser::changelogHtml( $rel['notes'] ) : '<p>No release notes.</p>',
|
||||
);
|
||||
return $i;
|
||||
} catch ( \Throwable $e ) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/** Adds the token ONLY for https requests to the configured Gitea host. */
|
||||
public static function authorize( mixed $args, mixed $url ): mixed {
|
||||
try {
|
||||
if ( ! is_array( $args ) || ! is_string( $url ) ) {
|
||||
return $args;
|
||||
}
|
||||
$token = trim( (string) Settings::get( 'update_token', '' ) );
|
||||
$host = self::repo()['host'];
|
||||
if ( '' === $token || ! ReleaseParser::isAllowedUrl( $url, $host ) ) {
|
||||
return $args;
|
||||
}
|
||||
$args['headers'] = is_array( $args['headers'] ?? null ) ? $args['headers'] : array();
|
||||
$args['headers']['Authorization'] = 'token ' . $token;
|
||||
} catch ( \Throwable $e ) {
|
||||
// Fall through without credentials.
|
||||
}
|
||||
return $args;
|
||||
}
|
||||
|
||||
public static function afterUpgrade( mixed $upgrader, mixed $options ): void {
|
||||
if ( is_array( $options ) && ( $options['type'] ?? '' ) === 'plugin' ) {
|
||||
self::clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
public static function handleCheck(): void {
|
||||
Admin::guard( self::ACTION_CHECK );
|
||||
self::clearCache();
|
||||
delete_site_transient( 'update_plugins' );
|
||||
$rel = self::latest( true );
|
||||
$back = Admin::url( 'streamgoals-settings' );
|
||||
if ( null === $rel ) {
|
||||
Admin::redirect( $back, 'warning', 'Could not read the latest release. Check your connection and, if the update repository is private, the access token.' );
|
||||
}
|
||||
Admin::redirect( $back, 'success', ReleaseParser::isNewer( $rel['version'], STREAMGOALS_VERSION ) ? 'Version ' . $rel['version'] . ' is available. See Dashboard > Updates.' : 'You are up to date.' );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user