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' => '

' . esc_html__( 'Calendars, recurring events, and a Single Event template builder for Elementor.', 'burn-events' ) . '

', 'changelog' => '

' . esc_html__( 'Read the eventLabs release notes', 'burn-events' ) . '

', ], ]; } private static function latest(): ?array { // WordPress caches the update response. Reuse it within the request without delaying “Check again.” 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; } }