Files
argus-wp-defence/includes/class-argus-license.php
ARGUSandClaude Sonnet 5 c4db87af84 ARGUS WordPress Defence 7.23.2
- New: License section in Settings -- a 30-day trial with every
  feature active starts automatically, no registration required to
  start. Protection continues regardless of license status; the trial
  only affects what's shown, never what's enforced. Enter a license
  key to keep Premium features once the trial ends. This is the first
  release where the license system is actually visible anywhere --
  it existed in the codebase before but had no entry form and was
  excluded from every published build.
- Intelligence page: rewrote descriptions in plain language about what
  each thing does for your site's protection, not internal
  implementation details (was showing raw technical descriptions like
  a "signature corpus" and "RIR delegation database" range counts).

Verified end-to-end before publishing (not just code review): a real
signup and license created on the actual companion license service,
then submitted through the real Settings page form exactly as a
customer would -- status went from "TRIAL -- 30 DAYS LEFT" to
"LICENSED" on both the Settings and Overview pages. This exact ZIP was
also installed fresh and every admin page loaded with zero errors
before this commit.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-09 19:37:16 +00:00

128 lines
4.7 KiB
PHP

<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Premium-build-only, same as Argus_Update_Client: excluded from the
// WordPress.org release ZIP (bin/build-release.sh), guarded everywhere with
// class_exists() so the free build never references it. WP.org's guidelines
// prohibit trialware in a plugin submitted to the directory -- the free
// build stays genuinely, permanently functional, with no trial timer and no
// license requirement anywhere in it. This class exists only in a
// separately-distributed Premium build.
class Argus_License {
const TRIAL_DAYS = 30;
const TRIAL_STARTED_OPTION = 'argus_wpd_license_trial_started_at';
const LICENSE_KEY_OPTION = 'argus_wpd_license_key';
const LICENSE_STATUS_OPTION = 'argus_wpd_license_validation_status';
const LICENSE_CHECKED_OPTION = 'argus_wpd_license_last_checked';
public static function ensure_trial_started() {
if ( ! get_option( self::TRIAL_STARTED_OPTION ) ) {
update_option( self::TRIAL_STARTED_OPTION, time(), false );
}
}
public static function trial_started_at() {
return (int) get_option( self::TRIAL_STARTED_OPTION, time() );
}
public static function trial_ends_at() {
return self::trial_started_at() + ( self::TRIAL_DAYS * DAY_IN_SECONDS );
}
public static function days_remaining() {
$remaining_secs = self::trial_ends_at() - time();
return max( 0, (int) ceil( $remaining_secs / DAY_IN_SECONDS ) );
}
public static function license_key() {
if ( defined( 'ARGUS_WPD_LICENSE_KEY' ) && ARGUS_WPD_LICENSE_KEY ) {
return trim( ARGUS_WPD_LICENSE_KEY );
}
return trim( (string) get_option( self::LICENSE_KEY_OPTION, '' ) );
}
// Saving a submitted key never marks it valid by itself -- the caller
// still has to call validate_now() to actually check it against the
// configured validation server, same as entering any license key
// anywhere always requires a real check before it does anything.
public static function set_license_key( $key ) {
update_option( self::LICENSE_KEY_OPTION, trim( (string) $key ), false );
delete_option( self::LICENSE_STATUS_OPTION );
}
public static function validation_endpoint() {
return defined( 'ARGUS_WPD_LICENSE_VALIDATE_URL' ) ? ARGUS_WPD_LICENSE_VALIDATE_URL : '';
}
// Honest, not fabricated: a license key alone never counts as "licensed"
// unless there's a real validation endpoint configured to actually check
// it against. No key -> unlicensed. Key present but no validation
// endpoint configured -> still unlicensed, not a silent green light.
public static function is_licensed() {
if ( '' === self::license_key() || '' === self::validation_endpoint() ) {
return false;
}
return 'valid' === get_option( self::LICENSE_STATUS_OPTION, '' );
}
public static function validate_now() {
$key = self::license_key();
$url = self::validation_endpoint();
if ( '' === $key ) {
return array( 'success' => false, 'message' => __( 'Enter a license key first.', 'argus-wordpress-defence' ) );
}
if ( '' === $url ) {
return array( 'success' => false, 'message' => __( 'No license validation server is configured for this deployment.', 'argus-wordpress-defence' ) );
}
$response = wp_remote_post(
$url,
array(
'timeout' => 15,
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => wp_json_encode( array( 'license_key' => $key, 'domain' => wp_parse_url( home_url(), PHP_URL_HOST ) ) ),
)
);
update_option( self::LICENSE_CHECKED_OPTION, current_time( 'mysql', true ), false );
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
update_option( self::LICENSE_STATUS_OPTION, 'error', false );
return array( 'success' => false, 'message' => __( 'Could not reach the license server. Try again shortly.', 'argus-wordpress-defence' ) );
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
$valid = is_array( $data ) && ! empty( $data['valid'] );
update_option( self::LICENSE_STATUS_OPTION, $valid ? 'valid' : 'invalid', false );
return array(
'success' => $valid,
'message' => $valid
? __( 'License activated.', 'argus-wordpress-defence' )
: __( 'That license key is not valid or has expired.', 'argus-wordpress-defence' ),
);
}
public static function status() {
if ( self::is_licensed() ) {
return 'licensed';
}
return self::days_remaining() > 0 ? 'trial' : 'trial_expired';
}
public static function summary() {
return array(
'status' => self::status(),
'days_remaining' => self::days_remaining(),
'trial_ends_at' => gmdate( 'Y-m-d H:i:s', self::trial_ends_at() ),
'has_key' => '' !== self::license_key(),
'purchase_url' => defined( 'ARGUS_WPD_LICENSE_PURCHASE_URL' ) ? ARGUS_WPD_LICENSE_PURCHASE_URL : '',
);
}
}