157 lines
6.0 KiB
PHP
157 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace BurnEvents;
|
||
|
||
defined( 'ABSPATH' ) || exit;
|
||
|
||
final class Updates {
|
||
private const REPO = 'https://git.labs.casa/josh/eventLabs';
|
||
private const LATEST = 'https://git.labs.casa/api/v1/repos/josh/eventLabs/releases/latest';
|
||
private const MANIFEST = 'eventlabs-update.json';
|
||
private const SLUG = 'burn-events';
|
||
private const VERSION = '(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)\.(?:0|[1-9][0-9]*)';
|
||
private const MAX_ZIP_BYTES = 32 * 1024 * 1024;
|
||
|
||
public static function register(): void {
|
||
add_filter( 'update_plugins_git.labs.casa', [ self::class, 'update' ], 10, 3 );
|
||
add_filter( 'plugins_api', [ self::class, 'details' ], 10, 3 );
|
||
add_filter( 'upgrader_pre_download', [ self::class, 'download' ], 10, 2 );
|
||
}
|
||
|
||
public static function update( $update, array $headers, string $plugin_file ) {
|
||
if ( false !== $update || plugin_basename( BURN_EVENTS_FILE ) !== $plugin_file || self::REPO !== ( $headers['UpdateURI'] ?? '' ) ) {
|
||
return $update;
|
||
}
|
||
return self::latest() ?? $update;
|
||
}
|
||
|
||
public static function details( $result, string $action, object $args ) {
|
||
if ( false !== $result || 'plugin_information' !== $action || self::SLUG !== ( $args->slug ?? '' ) ) {
|
||
return $result;
|
||
}
|
||
$release = self::latest();
|
||
if ( ! $release ) {
|
||
return $result;
|
||
}
|
||
return (object) [
|
||
'name' => 'eventLabs',
|
||
'slug' => self::SLUG,
|
||
'author' => 'hostLabs',
|
||
'version' => $release['version'],
|
||
'requires' => $release['requires'],
|
||
'requires_php' => $release['requires_php'],
|
||
'homepage' => self::REPO,
|
||
'download_link' => $release['package'],
|
||
'sections' => [
|
||
'description' => '<p>' . esc_html__( 'Calendars, recurring events, and a Single Event template builder for Elementor.', 'burn-events' ) . '</p>',
|
||
'changelog' => '<p><a href="' . esc_url( $release['url'] ) . '">' . esc_html__( 'Read the eventLabs release notes', 'burn-events' ) . '</a></p>',
|
||
],
|
||
];
|
||
}
|
||
|
||
private static function latest(): ?array {
|
||
// WordPress caches the update response. Reuse it within the request without delaying “Check again.â€<C3A2>
|
||
static $loaded = false;
|
||
static $update = null;
|
||
if ( $loaded ) {
|
||
return $update;
|
||
}
|
||
$loaded = true;
|
||
$release = self::json( self::LATEST );
|
||
$tag = $release['tag_name'] ?? null;
|
||
if ( ! is_string( $tag ) || ! preg_match( '/^v' . self::VERSION . '$/D', $tag ) || false !== ( $release['draft'] ?? null ) || false !== ( $release['prerelease'] ?? null ) || ! is_array( $release['assets'] ?? null ) ) {
|
||
return null;
|
||
}
|
||
$names = array_column( $release['assets'], 'name' );
|
||
if ( ! in_array( self::archive_name( $tag ), $names, true ) || ! in_array( self::MANIFEST, $names, true ) ) {
|
||
return null;
|
||
}
|
||
$manifest = self::manifest( $tag );
|
||
if ( ! $manifest ) {
|
||
return null;
|
||
}
|
||
$update = [
|
||
'slug' => self::SLUG,
|
||
'version' => $manifest['version'],
|
||
'requires' => $manifest['requires'],
|
||
'requires_php' => $manifest['requires_php'],
|
||
'url' => self::REPO . '/releases/tag/' . $tag,
|
||
'package' => self::asset_url( $tag, self::archive_name( $tag ) ),
|
||
];
|
||
return $update;
|
||
}
|
||
|
||
public static function download( $reply, $package ) {
|
||
$base = preg_quote( self::REPO . '/releases/download/', '~' );
|
||
if ( false !== $reply || ! is_string( $package ) || ! preg_match( '~^' . $base . '(v' . self::VERSION . ')/eventlabs-\1\.zip$~D', $package, $match ) ) {
|
||
return $reply;
|
||
}
|
||
$manifest = self::manifest( $match[1] );
|
||
if ( ! $manifest ) {
|
||
return new \WP_Error( 'eventlabs_update_manifest', __( 'The eventLabs release manifest is unavailable or invalid.', 'burn-events' ) );
|
||
}
|
||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||
$file = wp_tempnam( self::archive_name( $match[1] ) );
|
||
if ( ! $file ) {
|
||
return new \WP_Error( 'eventlabs_update_file', __( 'WordPress could not create a temporary file for the eventLabs update.', 'burn-events' ) );
|
||
}
|
||
$response = wp_safe_remote_get( $package, [
|
||
'timeout' => 60,
|
||
'redirection' => 2,
|
||
'user-agent' => 'eventLabs/' . BURN_EVENTS_VERSION,
|
||
'stream' => true,
|
||
'filename' => $file,
|
||
'limit_response_size' => $manifest['size'] + 1,
|
||
] );
|
||
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
|
||
wp_delete_file( $file );
|
||
return new \WP_Error( 'eventlabs_update_download', __( 'WordPress could not download the eventLabs release ZIP.', 'burn-events' ) );
|
||
}
|
||
clearstatcache( true, $file );
|
||
if ( $manifest['size'] !== filesize( $file ) || $manifest['sha256'] !== hash_file( 'sha256', $file ) ) {
|
||
wp_delete_file( $file );
|
||
return new \WP_Error( 'eventlabs_update_checksum', __( 'The eventLabs download did not match the release checksum.', 'burn-events' ) );
|
||
}
|
||
return $file;
|
||
}
|
||
|
||
private static function manifest( string $tag ): ?array {
|
||
$manifest = self::json( self::asset_url( $tag, self::MANIFEST ) );
|
||
if ( ! $manifest || substr( $tag, 1 ) !== ( $manifest['version'] ?? null ) || self::archive_name( $tag ) !== ( $manifest['package'] ?? null ) ) {
|
||
return null;
|
||
}
|
||
foreach ( [ 'requires', 'requires_php' ] as $key ) {
|
||
if ( ! is_string( $manifest[ $key ] ?? null ) || ! preg_match( '/^[0-9]+\.[0-9]+(?:\.[0-9]+)?$/D', $manifest[ $key ] ) ) {
|
||
return null;
|
||
}
|
||
}
|
||
if ( ! is_string( $manifest['sha256'] ?? null ) || ! preg_match( '/^[a-f0-9]{64}$/D', $manifest['sha256'] ) || ! is_int( $manifest['size'] ?? null ) || $manifest['size'] < 1 || $manifest['size'] > self::MAX_ZIP_BYTES ) {
|
||
return null;
|
||
}
|
||
return $manifest;
|
||
}
|
||
|
||
private static function json( string $url ): ?array {
|
||
$response = wp_safe_remote_get( $url, [
|
||
'timeout' => 8,
|
||
'redirection' => 2,
|
||
'user-agent' => 'eventLabs/' . BURN_EVENTS_VERSION,
|
||
'headers' => [ 'Accept' => 'application/json' ],
|
||
'limit_response_size' => 128 * 1024,
|
||
] );
|
||
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
|
||
return null;
|
||
}
|
||
$data = json_decode( wp_remote_retrieve_body( $response ), true );
|
||
return is_array( $data ) ? $data : null;
|
||
}
|
||
|
||
private static function archive_name( string $tag ): string {
|
||
return 'eventlabs-' . $tag . '.zip';
|
||
}
|
||
|
||
private static function asset_url( string $tag, string $name ): string {
|
||
return self::REPO . '/releases/download/' . $tag . '/' . $name;
|
||
}
|
||
}
|