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'] ) : '

No release notes.

', ); 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 || ! RedirectPolicy::tokenAllowed( $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; } private static bool $following = false; /** * Token-bearing requests never let HTTP redirect handling carry the Authorization header anywhere: we send the * request with redirection disabled and follow a 3xx ourselves ONLY while it stays on the pinned host (max 3 hops). * A redirect to any other host is not followed at all, so the token cannot leak and the update fails closed. */ public static function followSafely( mixed $pre, mixed $args, mixed $url ): mixed { if ( false !== $pre || self::$following || ! is_array( $args ) || ! is_string( $url ) ) { return $pre; } $headers = is_array( $args['headers'] ?? null ) ? $args['headers'] : array(); $has = false; foreach ( array_keys( $headers ) as $k ) { $has = $has || 'authorization' === strtolower( (string) $k ); } $host = self::repo()['host']; if ( ! $has || ! RedirectPolicy::tokenAllowed( $url, $host ) ) { return $pre; } self::$following = true; try { $current = $url; $resp = null; for ( $hop = 0; $hop <= RedirectPolicy::MAX_HOPS; $hop++ ) { $a = $args; $a['redirection'] = 0; $resp = wp_remote_request( $current, $a ); $code = is_wp_error( $resp ) ? 0 : (int) wp_remote_retrieve_response_code( $resp ); if ( $code < 300 || $code >= 400 || is_wp_error( $resp ) ) { return $resp; } $next = RedirectPolicy::nextUrl( $current, (string) wp_remote_retrieve_header( $resp, 'location' ), $host, $hop ); if ( null === $next ) { return $resp; // not followed: the caller sees the 3xx and fails } $current = $next; } return $resp; } finally { self::$following = false; } } 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.' ); } }