Enhance Subscriptions, Affiliates, and Software Distribution modules

This commit is contained in:
Dwindi Ramadhana
2026-06-03 21:24:03 +07:00
parent f8c733832e
commit 21ece27b9b
9 changed files with 803 additions and 96 deletions

View File

@@ -608,7 +608,7 @@ class LicenseManager
/**
* Validate license (check if valid without activating)
*/
public static function validate($license_key)
public static function validate($license_key, $domain = null)
{
$license = self::get_license_by_key($license_key);
@@ -626,8 +626,31 @@ class LicenseManager
$subscription_status = self::get_order_subscription_status($license['order_id']);
$is_subscription_valid = $subscription_status === null || in_array($subscription_status, ['active', 'pending-cancel']);
// Check domain activation if domain is provided
$is_domain_active = true;
if ($domain) {
global $wpdb;
$table = $wpdb->prefix . self::$activations_table;
$activation = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM $table WHERE license_id = %d AND domain = %s AND status = 'active' LIMIT 1",
$license['id'],
$domain
));
if (!$activation) {
$is_domain_active = false;
}
}
if ($domain && !$is_domain_active) {
return [
'valid' => false,
'error' => 'domain_not_activated',
'message' => __('License is not activated for this domain', 'woonoow'),
];
}
return [
'valid' => $license['status'] === 'active' && !$is_expired && $is_subscription_valid,
'valid' => $license['status'] === 'active' && !$is_expired && $is_subscription_valid && $is_domain_active,
'license_key' => $license['license_key'],
'status' => $license['status'],
'activation_limit' => (int) $license['activation_limit'],
@@ -639,6 +662,7 @@ class LicenseManager
'is_expired' => $is_expired,
'subscription_status' => $subscription_status,
'subscription_active' => $is_subscription_valid,
'domain_active' => $is_domain_active,
];
}

View File

@@ -127,10 +127,10 @@ class SoftwareManager
/**
* Check for updates
*/
public static function check_update($license_key, $slug, $current_version)
public static function check_update($license_key, $slug, $current_version, $site_url = null)
{
// Validate license
$license_validation = LicenseManager::validate($license_key);
$license_validation = LicenseManager::validate($license_key, $site_url);
if (!$license_validation['valid']) {
return [
@@ -255,10 +255,21 @@ class SoftwareManager
global $wpdb;
$table = $wpdb->prefix . self::$versions_table;
return $wpdb->get_results($wpdb->prepare(
$results = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table WHERE product_id = %d ORDER BY release_date DESC",
$product_id
), ARRAY_A);
if ($results) {
foreach ($results as &$row) {
$decoded_changelog = json_decode($row['changelog'], true);
if (json_last_error() === JSON_ERROR_NONE) {
$row['changelog'] = $decoded_changelog;
}
}
}
return $results;
}
/**
@@ -308,6 +319,39 @@ class SoftwareManager
return $wpdb->insert_id;
}
/**
* Update a version
*/
public static function update_version($version_id, $product_id, $version, $changelog = '', $set_current = false)
{
global $wpdb;
$table = $wpdb->prefix . self::$versions_table;
// Reset other current versions if this one is set to current
if ($set_current) {
$wpdb->update($table, ['is_current' => 0], ['product_id' => $product_id]);
}
// Update version
$wpdb->update($table, [
'version' => $version,
'changelog' => $changelog,
'is_current' => $set_current ? 1 : 0,
], [
'id' => $version_id,
'product_id' => $product_id
]);
// Update product meta
if ($set_current) {
update_post_meta($product_id, '_woonoow_software_current_version', $version);
}
do_action('woonoow/software/version_updated', $version_id, $product_id, $version);
return true;
}
/**
* Generate secure download token
*/

View File

@@ -48,6 +48,9 @@ class SubscriptionModule
add_action('deleted_post', [__CLASS__, 'on_post_deleted']);
add_action('delete_user', [__CLASS__, 'on_user_deleted']);
// Prevent guest checkout for subscriptions
add_filter('woocommerce_add_to_cart_validation', [__CLASS__, 'validate_subscription_add_to_cart'], 10, 3);
// Modify add to cart button text for subscription products
add_filter('woocommerce_product_single_add_to_cart_text', [__CLASS__, 'subscription_add_to_cart_text'], 10, 2);
add_filter('woocommerce_product_add_to_cart_text', [__CLASS__, 'subscription_add_to_cart_text'], 10, 2);
@@ -271,6 +274,24 @@ class SubscriptionModule
$order->save();
}
/**
* Prevent guest checkout for subscriptions
*/
public static function validate_subscription_add_to_cart($passed, $product_id, $quantity) {
if (!ModuleRegistry::is_enabled('subscription')) {
return $passed;
}
if (get_post_meta($product_id, '_woonoow_subscription_enabled', true) === 'yes') {
if (!is_user_logged_in()) {
wc_add_notice(__('You must be logged in to purchase a subscription.', 'woonoow'), 'error');
return false;
}
}
return $passed;
}
/**
* Modify add to cart button text for subscription products
*/