Files
404-enhancer/includes/class-fe-updater.php
T
2026-11-16 12:24:05 -05:00

217 lines
6.7 KiB
PHP

<?php
/**
* Self-hosted plugin updater for a Gitea instance (rudrastyh-style).
*
* Configuration, in order of precedence:
* 1. Constants: FE_GITEA_API_URL, FE_GITEA_OWNER, FE_GITEA_REPO, FE_GITEA_TOKEN.
* 2. The `fe_updater_config` filter returning an array with keys
* api_url, owner, repo, token.
*
* The updater is a no-op until api_url/owner/repo are set.
*
* @package 404_Enhancer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Checks a Gitea repository for releases and injects them into the
* WordPress update transients.
*/
final class FE_Updater {
const CACHE_KEY = 'fe_gitea_latest_release';
const CACHE_TTL = 6 * HOUR_IN_SECONDS;
/**
* Registers update hooks.
*/
public static function init() {
add_filter( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'check_for_updates' ) );
add_filter( 'plugins_api', array( __CLASS__, 'plugin_info' ), 10, 3 );
}
/**
* Returns the updater configuration.
*
* @return array{api_url: string, owner: string, repo: string, token: string}
*/
private static function config() {
$config = array(
'api_url' => defined( 'FE_GITEA_API_URL' ) ? (string) FE_GITEA_API_URL : '',
'owner' => defined( 'FE_GITEA_OWNER' ) ? (string) FE_GITEA_OWNER : '',
'repo' => defined( 'FE_GITEA_REPO' ) ? (string) FE_GITEA_REPO : '',
'token' => defined( 'FE_GITEA_TOKEN' ) ? (string) FE_GITEA_TOKEN : '',
);
/**
* Filters the Gitea updater configuration.
*
* @param array $config Keys: api_url, owner, repo, token.
*/
return wp_parse_args( apply_filters( 'fe_updater_config', $config ), $config );
}
/**
* Whether the updater has enough configuration to run.
*
* @return bool
*/
private static function is_configured() {
$config = self::config();
return '' !== $config['api_url'] && '' !== $config['owner'] && '' !== $config['repo'];
}
/**
* Fetches the latest usable release from the Gitea API (cached).
*
* @return array|null Keys: version, tag, name, body, html_url, package. Null on failure.
*/
private static function get_latest_release() {
if ( ! self::is_configured() ) {
return null;
}
$config = self::config();
$cache = get_transient( self::CACHE_KEY );
if ( false !== $cache && is_array( $cache ) ) {
return $cache;
}
$url = trailingslashit( $config['api_url'] ) . 'repos/' . rawurlencode( $config['owner'] ) . '/' . rawurlencode( $config['repo'] ) . '/releases?limit=30';
$args = array(
'timeout' => 10,
'headers' => array( 'Accept' => 'application/json' ),
);
if ( '' !== $config['token'] ) {
$args['headers']['Authorization'] = 'token ' . $config['token'];
}
$response = wp_remote_get( $url, $args );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
return null;
}
$releases = json_decode( (string) wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $releases ) ) {
return null;
}
$api_host = (string) wp_parse_url( $config['api_url'], PHP_URL_HOST );
$best = null;
foreach ( $releases as $release ) {
if ( ! empty( $release['draft'] ) || ! empty( $release['prerelease'] ) ) {
continue;
}
$version = ltrim( (string) ( $release['tag_name'] ?? '' ), 'vV' );
if ( '' === $version || ! preg_match( '/^\d+(\.\d+)*/', $version ) ) {
continue;
}
$package = '';
if ( ! empty( $release['assets'] ) && is_array( $release['assets'] ) ) {
foreach ( $release['assets'] as $asset ) {
$name = (string) ( $asset['name'] ?? '' );
$link = (string) ( $asset['browser_download_url'] ?? '' );
if ( '.zip' === substr( strtolower( $name ), -4 ) && self::same_host( $link, $api_host ) ) {
// Prefer an asset named after the plugin slug.
if ( FE_SLUG . '.zip' === strtolower( $name ) ) {
$package = $link;
break;
}
if ( '' === $package ) {
$package = $link;
}
}
}
}
if ( '' === $package ) {
continue;
}
$candidate = array(
'version' => $version,
'tag' => (string) $release['tag_name'],
'name' => (string) ( $release['name'] ?: $release['tag_name'] ),
'body' => (string) ( $release['body'] ?? '' ),
'html_url' => (string) ( $release['html_url'] ?? '' ),
'package' => $package,
);
if ( null === $best || version_compare( $candidate['version'], $best['version'], '>' ) ) {
$best = $candidate;
}
}
if ( null !== $best ) {
set_transient( self::CACHE_KEY, $best, self::CACHE_TTL );
}
return $best;
}
/**
* Checks whether a URL belongs to the configured Gitea host.
*
* @param string $url Candidate URL.
* @param string $api_host Expected host.
* @return bool
*/
private static function same_host( $url, $api_host ) {
$host = wp_parse_url( $url, PHP_URL_HOST );
return is_string( $host ) && strtolower( $host ) === strtolower( (string) $api_host );
}
/**
* Injects the remote version into the update_plugins transient.
*
* @param object $transient Update transient value.
* @return object
*/
public static function check_for_updates( $transient ) {
if ( ! is_object( $transient ) ) {
$transient = new stdClass();
}
$release = self::get_latest_release();
if ( null === $release ) {
return $transient;
}
if ( version_compare( $release['version'], FE_VERSION, '>' ) ) {
$transient->response[ FE_SLUG ] = (object) array(
'slug' => FE_SLUG,
'plugin' => plugin_basename( FE_PLUGIN_FILE ),
'new_version' => $release['version'],
'url' => $release['html_url'],
'package' => $release['package'],
'tested' => get_bloginfo( 'version' ),
);
}
return $transient;
}
/**
* Serves plugin information for the "View details" modal.
*
* @param mixed $result Result object or false.
* @param string $action plugins_api action.
* @param object $args Request args.
* @return mixed
*/
public static function plugin_info( $result, $action, $args ) {
if ( 'plugin_information' !== $action || ! isset( $args->slug ) || FE_SLUG !== $args->slug ) {
return $result;
}
$release = self::get_latest_release();
if ( null === $release ) {
return $result;
}
return (object) array(
'name' => __( '404 Enhancer', '404-enhancer' ),
'slug' => FE_SLUG,
'version' => $release['version'],
'author' => 'Your Name',
'homepage' => $release['html_url'],
'download_link' => $release['package'],
'tested' => get_bloginfo( 'version' ),
'sections' => array(
'description' => __( 'Enhances the 404 page with a custom message, featured image, hand-picked content, search and URL-based suggestions.', '404-enhancer' ),
'changelog' => $release['body'] ? wp_kses_post( nl2br( $release['body'] ) ) : __( 'No changelog provided.', '404-enhancer' ),
),
);
}
}