feat: implement new license validation and updater system with SitiWebUpdater2
This commit is contained in:
130
includes/SitiLicenseValidator.php
Normal file
130
includes/SitiLicenseValidator.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Siti License Validator
|
||||
*
|
||||
* Validates licenses against the Siti Plugin Repo API at plugins.robert.ooo
|
||||
*/
|
||||
class SitiLicenseValidator {
|
||||
|
||||
private $api_base_url = 'https://plugins.robert.ooo';
|
||||
private $license_key;
|
||||
private $hostname;
|
||||
|
||||
public function __construct( $license_key = null, $hostname = null ) {
|
||||
$this->license_key = $license_key ?: get_option( 'siti_license_key' );
|
||||
$this->hostname = $hostname ?: parse_url( home_url(), PHP_URL_HOST );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the license key
|
||||
*/
|
||||
public function set_license_key( $key ) {
|
||||
$this->license_key = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the API base URL
|
||||
*/
|
||||
public function set_api_base_url( $url ) {
|
||||
$this->api_base_url = rtrim( $url, '/' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hostname
|
||||
*/
|
||||
public function set_hostname( $hostname ) {
|
||||
$this->hostname = $hostname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the license
|
||||
*
|
||||
* @return array|WP_Error License data on success, WP_Error on failure
|
||||
*/
|
||||
public function verify() {
|
||||
if ( empty( $this->license_key ) ) {
|
||||
return new WP_Error( 'missing_license', 'Geen licentiecode ingesteld.' );
|
||||
}
|
||||
|
||||
if ( empty( $this->hostname ) ) {
|
||||
return new WP_Error( 'missing_hostname', 'Kon hostname niet bepalen.' );
|
||||
}
|
||||
|
||||
$response = wp_remote_post( $this->api_base_url . '/api/licenses/verify', array(
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
'body' => wp_json_encode( array(
|
||||
'key' => $this->license_key,
|
||||
'hostname' => $this->hostname,
|
||||
) ),
|
||||
'timeout' => 15,
|
||||
) );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$code = wp_remote_retrieve_response_code( $response );
|
||||
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
|
||||
if ( 200 !== $code ) {
|
||||
$error_message = isset( $body['error'] ) ? $body['error'] : 'Licentie verificatie mislukt.';
|
||||
return new WP_Error( 'verification_failed', $error_message );
|
||||
}
|
||||
|
||||
if ( empty( $body['valid'] ) ) {
|
||||
return new WP_Error( 'invalid_license', 'Licentie is ongeldig.' );
|
||||
}
|
||||
|
||||
// Update last check time
|
||||
update_option( 'siti_license_last_check', current_time( 'mysql' ) );
|
||||
|
||||
// Store license data
|
||||
if ( isset( $body['license'] ) ) {
|
||||
update_option( 'siti_license_data', $body['license'] );
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get stored license data
|
||||
*/
|
||||
public function get_license_data() {
|
||||
return get_option( 'siti_license_data', array() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if license is valid (cached)
|
||||
*/
|
||||
public function is_valid() {
|
||||
$data = $this->get_license_data();
|
||||
return ! empty( $data ) && isset( $data['key'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest version from license data
|
||||
*/
|
||||
public function get_latest_version() {
|
||||
$data = $this->get_license_data();
|
||||
return $data['pluginVersion'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Download a specific version
|
||||
*
|
||||
* @param string $version Version to download (e.g., 'v1.2.3' or 'latest')
|
||||
* @return string|WP_Error Download URL or error
|
||||
*/
|
||||
public function get_download_url( $version = 'latest' ) {
|
||||
if ( empty( $this->license_key ) || empty( $this->hostname ) ) {
|
||||
return new WP_Error( 'missing_credentials', 'Licentie of hostname ontbreekt.' );
|
||||
}
|
||||
|
||||
// For now, return the API download endpoint
|
||||
// In practice, you might want to proxy this through WordPress
|
||||
return $this->api_base_url . '/api/licenses/download?key=' . urlencode( $this->license_key ) . '&hostname=' . urlencode( $this->hostname ) . '&version=' . urlencode( $version );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user