You've already forked wc-licensed-product
Implement multi-domain licensing for v0.5.0
- Add multi-domain checkout support for WooCommerce Blocks - Fix domain field rendering using ExperimentalOrderMeta slot - Add DOM injection fallback for checkout field rendering - Update translations with new multi-domain strings (de_CH) - Update email templates for grouped license display - Refactor account page to group licenses by product/order Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -94,8 +94,10 @@ final class OrderLicenseController
|
||||
return;
|
||||
}
|
||||
|
||||
// Get order domain
|
||||
$orderDomain = $order->get_meta('_licensed_product_domain');
|
||||
// Check for multi-domain format first, then fall back to legacy single domain
|
||||
$multiDomainData = $order->get_meta('_licensed_product_domains');
|
||||
$legacyDomain = $order->get_meta('_licensed_product_domain');
|
||||
$hasMultiDomain = !empty($multiDomainData) && is_array($multiDomainData);
|
||||
|
||||
// Get licenses for this order
|
||||
$licenses = $this->licenseManager->getLicensesByOrder($order->get_id());
|
||||
@@ -104,23 +106,42 @@ final class OrderLicenseController
|
||||
?>
|
||||
<div class="wclp-order-licenses">
|
||||
<div class="wclp-order-domain-section">
|
||||
<h4><?php esc_html_e('Order Domain', 'wc-licensed-product'); ?></h4>
|
||||
<p class="description">
|
||||
<?php esc_html_e('The domain specified during checkout. Changing this will not automatically update existing license domains.', 'wc-licensed-product'); ?>
|
||||
</p>
|
||||
<div class="wclp-inline-edit">
|
||||
<input type="text"
|
||||
id="wclp-order-domain"
|
||||
class="regular-text"
|
||||
value="<?php echo esc_attr($orderDomain); ?>"
|
||||
data-order-id="<?php echo esc_attr($order->get_id()); ?>"
|
||||
placeholder="<?php esc_attr_e('example.com', 'wc-licensed-product'); ?>" />
|
||||
<button type="button" class="button" id="wclp-save-order-domain">
|
||||
<?php esc_html_e('Save', 'wc-licensed-product'); ?>
|
||||
</button>
|
||||
<span class="spinner"></span>
|
||||
<span class="wclp-status-message"></span>
|
||||
</div>
|
||||
<h4><?php esc_html_e('Order Domains', 'wc-licensed-product'); ?></h4>
|
||||
|
||||
<?php if ($hasMultiDomain): ?>
|
||||
<p class="description">
|
||||
<?php esc_html_e('Domains specified during checkout (multi-domain order).', 'wc-licensed-product'); ?>
|
||||
</p>
|
||||
<div class="wclp-multi-domain-display" style="margin-top: 10px;">
|
||||
<?php foreach ($multiDomainData as $item): ?>
|
||||
<?php
|
||||
$product = wc_get_product($item['product_id']);
|
||||
$productName = $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product');
|
||||
?>
|
||||
<div class="wclp-product-domains-item" style="margin-bottom: 10px; padding: 10px; background: #f8f8f8; border-radius: 4px;">
|
||||
<strong><?php echo esc_html($productName); ?>:</strong><br>
|
||||
<code><?php echo esc_html(implode(', ', $item['domains'])); ?></code>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<p class="description">
|
||||
<?php esc_html_e('The domain specified during checkout. Changing this will not automatically update existing license domains.', 'wc-licensed-product'); ?>
|
||||
</p>
|
||||
<div class="wclp-inline-edit">
|
||||
<input type="text"
|
||||
id="wclp-order-domain"
|
||||
class="regular-text"
|
||||
value="<?php echo esc_attr($legacyDomain); ?>"
|
||||
data-order-id="<?php echo esc_attr($order->get_id()); ?>"
|
||||
placeholder="<?php esc_attr_e('example.com', 'wc-licensed-product'); ?>" />
|
||||
<button type="button" class="button" id="wclp-save-order-domain">
|
||||
<?php esc_html_e('Save', 'wc-licensed-product'); ?>
|
||||
</button>
|
||||
<span class="spinner"></span>
|
||||
<span class="wclp-status-message"></span>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<hr />
|
||||
@@ -128,15 +149,26 @@ final class OrderLicenseController
|
||||
<h4><?php esc_html_e('Licenses', 'wc-licensed-product'); ?></h4>
|
||||
|
||||
<?php
|
||||
// Count licensed products to check if all have licenses
|
||||
$licensedProductCount = 0;
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
$licensedProductCount++;
|
||||
// Count expected licenses based on domain data
|
||||
$expectedLicenses = 0;
|
||||
if ($hasMultiDomain) {
|
||||
// Multi-domain: count total domains across all products
|
||||
foreach ($multiDomainData as $item) {
|
||||
if (isset($item['domains']) && is_array($item['domains'])) {
|
||||
$expectedLicenses += count($item['domains']);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Legacy: one license per licensed product
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
$expectedLicenses++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$missingLicenses = $licensedProductCount - count($licenses);
|
||||
$missingLicenses = $expectedLicenses - count($licenses);
|
||||
$hasDomainData = $hasMultiDomain || !empty($legacyDomain);
|
||||
?>
|
||||
|
||||
<?php if (empty($licenses)): ?>
|
||||
@@ -150,7 +182,7 @@ final class OrderLicenseController
|
||||
<em><?php esc_html_e('Licenses will be generated when the order is marked as paid/completed.', 'wc-licensed-product'); ?></em>
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
<?php if ($orderDomain && $order->is_paid()): ?>
|
||||
<?php if ($hasDomainData && $order->is_paid()): ?>
|
||||
<p style="margin-top: 10px;">
|
||||
<button type="button" class="button button-primary" id="wclp-generate-licenses" data-order-id="<?php echo esc_attr($order->get_id()); ?>">
|
||||
<?php esc_html_e('Generate Licenses', 'wc-licensed-product'); ?>
|
||||
@@ -158,7 +190,7 @@ final class OrderLicenseController
|
||||
<span class="spinner" style="float: none; margin-top: 4px;"></span>
|
||||
<span class="wclp-generate-status"></span>
|
||||
</p>
|
||||
<?php elseif (!$orderDomain): ?>
|
||||
<?php elseif (!$hasDomainData): ?>
|
||||
<p class="description" style="margin-top: 10px; color: #d63638;">
|
||||
<span class="dashicons dashicons-warning"></span>
|
||||
<?php esc_html_e('Please set the order domain above before generating licenses.', 'wc-licensed-product'); ?>
|
||||
@@ -251,7 +283,7 @@ final class OrderLicenseController
|
||||
?>
|
||||
</p>
|
||||
|
||||
<?php if ($missingLicenses > 0 && $orderDomain && $order->is_paid()): ?>
|
||||
<?php if ($missingLicenses > 0 && $hasDomainData && $order->is_paid()): ?>
|
||||
<p style="margin-top: 10px;">
|
||||
<span class="dashicons dashicons-warning" style="color: #dba617;"></span>
|
||||
<?php
|
||||
@@ -474,68 +506,138 @@ final class OrderLicenseController
|
||||
wp_send_json_error(['message' => __('Order must be paid before licenses can be generated.', 'wc-licensed-product')]);
|
||||
}
|
||||
|
||||
// Get domain
|
||||
$domain = $order->get_meta('_licensed_product_domain');
|
||||
if (empty($domain)) {
|
||||
// Check for multi-domain format first
|
||||
$multiDomainData = $order->get_meta('_licensed_product_domains');
|
||||
$legacyDomain = $order->get_meta('_licensed_product_domain');
|
||||
|
||||
if (!empty($multiDomainData) && is_array($multiDomainData)) {
|
||||
// Multi-domain format
|
||||
$result = $this->generateMultiDomainLicenses($order, $multiDomainData);
|
||||
} elseif (!empty($legacyDomain)) {
|
||||
// Legacy single domain format
|
||||
$result = $this->generateLegacyLicenses($order, $legacyDomain);
|
||||
} else {
|
||||
wp_send_json_error(['message' => __('Please set the order domain before generating licenses.', 'wc-licensed-product')]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate licenses for each licensed product
|
||||
$generated = 0;
|
||||
$skipped = 0;
|
||||
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
$license = $this->licenseManager->generateLicense(
|
||||
$orderId,
|
||||
$product->get_id(),
|
||||
$order->get_customer_id(),
|
||||
$domain
|
||||
);
|
||||
|
||||
if ($license) {
|
||||
// Check if this is a new license or existing
|
||||
$existingLicenses = $this->licenseManager->getLicensesByOrder($orderId);
|
||||
$isNew = true;
|
||||
foreach ($existingLicenses as $existing) {
|
||||
if ($existing->getProductId() === $product->get_id() && $existing->getId() !== $license->getId()) {
|
||||
$isNew = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($isNew) {
|
||||
$generated++;
|
||||
} else {
|
||||
$skipped++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($generated > 0) {
|
||||
if ($result['generated'] > 0) {
|
||||
wp_send_json_success([
|
||||
'message' => sprintf(
|
||||
/* translators: %d: Number of licenses generated */
|
||||
_n(
|
||||
'%d license generated successfully.',
|
||||
'%d licenses generated successfully.',
|
||||
$generated,
|
||||
$result['generated'],
|
||||
'wc-licensed-product'
|
||||
),
|
||||
$generated
|
||||
$result['generated']
|
||||
),
|
||||
'generated' => $generated,
|
||||
'skipped' => $skipped,
|
||||
'generated' => $result['generated'],
|
||||
'skipped' => $result['skipped'],
|
||||
'reload' => true,
|
||||
]);
|
||||
} else {
|
||||
wp_send_json_success([
|
||||
'message' => __('All licenses already exist for this order.', 'wc-licensed-product'),
|
||||
'generated' => 0,
|
||||
'skipped' => $skipped,
|
||||
'skipped' => $result['skipped'],
|
||||
'reload' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate licenses for multi-domain format
|
||||
*/
|
||||
private function generateMultiDomainLicenses(\WC_Order $order, array $domainData): array
|
||||
{
|
||||
$generated = 0;
|
||||
$skipped = 0;
|
||||
$orderId = $order->get_id();
|
||||
$customerId = $order->get_customer_id();
|
||||
|
||||
// Index domains by product ID
|
||||
$domainsByProduct = [];
|
||||
foreach ($domainData as $item) {
|
||||
if (isset($item['product_id']) && isset($item['domains']) && is_array($item['domains'])) {
|
||||
$domainsByProduct[(int) $item['product_id']] = $item['domains'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if (!$product || !$product->is_type('licensed')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$productId = $product->get_id();
|
||||
$domains = $domainsByProduct[$productId] ?? [];
|
||||
|
||||
// Get existing licenses for this product
|
||||
$existingLicenses = $this->licenseManager->getLicensesByOrderAndProduct($orderId, $productId);
|
||||
$existingDomains = array_map(fn($l) => $l->getDomain(), $existingLicenses);
|
||||
|
||||
foreach ($domains as $domain) {
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
|
||||
// Skip if license already exists for this domain
|
||||
if (in_array($normalizedDomain, $existingDomains, true)) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$license = $this->licenseManager->generateLicense(
|
||||
$orderId,
|
||||
$productId,
|
||||
$customerId,
|
||||
$normalizedDomain
|
||||
);
|
||||
|
||||
if ($license) {
|
||||
$generated++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ['generated' => $generated, 'skipped' => $skipped];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate licenses for legacy single domain format
|
||||
*/
|
||||
private function generateLegacyLicenses(\WC_Order $order, string $domain): array
|
||||
{
|
||||
$generated = 0;
|
||||
$skipped = 0;
|
||||
$orderId = $order->get_id();
|
||||
$customerId = $order->get_customer_id();
|
||||
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if (!$product || !$product->is_type('licensed')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if license already exists
|
||||
$existing = $this->licenseManager->getLicenseByOrderAndProduct($orderId, $product->get_id());
|
||||
if ($existing) {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$license = $this->licenseManager->generateLicense(
|
||||
$orderId,
|
||||
$product->get_id(),
|
||||
$customerId,
|
||||
$domain
|
||||
);
|
||||
|
||||
if ($license) {
|
||||
$generated++;
|
||||
}
|
||||
}
|
||||
|
||||
return ['generated' => $generated, 'skipped' => $skipped];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,6 +202,13 @@ final class SettingsController
|
||||
'id' => 'wc_licensed_product_default_bind_to_version',
|
||||
'default' => 'no',
|
||||
],
|
||||
'enable_multi_domain' => [
|
||||
'name' => __('Enable Multi-Domain Licensing', 'wc-licensed-product'),
|
||||
'type' => 'checkbox',
|
||||
'desc' => __('Allow customers to purchase multiple licenses for different domains at once. Each unit in cart quantity requires a unique domain.', 'wc-licensed-product'),
|
||||
'id' => 'wc_licensed_product_enable_multi_domain',
|
||||
'default' => 'no',
|
||||
],
|
||||
'section_end' => [
|
||||
'type' => 'sectionend',
|
||||
'id' => 'wc_licensed_product_section_defaults_end',
|
||||
@@ -387,6 +394,14 @@ final class SettingsController
|
||||
return get_option('wc_licensed_product_default_bind_to_version', 'no') === 'yes';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if multi-domain licensing is enabled
|
||||
*/
|
||||
public static function isMultiDomainEnabled(): bool
|
||||
{
|
||||
return get_option('wc_licensed_product_enable_multi_domain', 'no') === 'yes';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if expiration warning emails are enabled
|
||||
* This checks both the WooCommerce email setting and the old setting for backwards compatibility
|
||||
|
||||
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||
namespace Jeremias\WcLicensedProduct\Checkout;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
|
||||
use Jeremias\WcLicensedProduct\Admin\SettingsController;
|
||||
|
||||
/**
|
||||
* Integration with WooCommerce Checkout Blocks
|
||||
@@ -30,7 +31,7 @@ final class CheckoutBlocksIntegration implements IntegrationInterface
|
||||
public function initialize(): void
|
||||
{
|
||||
$this->registerScripts();
|
||||
$this->registerBlockExtensionData();
|
||||
$this->registerAdditionalCheckoutFields();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,7 +46,7 @@ final class CheckoutBlocksIntegration implements IntegrationInterface
|
||||
wp_register_script(
|
||||
'wc-licensed-product-checkout-blocks',
|
||||
$scriptUrl,
|
||||
['wc-blocks-checkout', 'wp-element', 'wp-components', 'wp-i18n'],
|
||||
['wc-blocks-checkout', 'wp-element', 'wp-components', 'wp-i18n', 'wp-plugins', 'wp-data'],
|
||||
WC_LICENSED_PRODUCT_VERSION,
|
||||
true
|
||||
);
|
||||
@@ -59,20 +60,33 @@ final class CheckoutBlocksIntegration implements IntegrationInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Register block extension data
|
||||
* Register additional checkout fields using WooCommerce Blocks API
|
||||
*/
|
||||
private function registerBlockExtensionData(): void
|
||||
private function registerAdditionalCheckoutFields(): void
|
||||
{
|
||||
// Pass data to the checkout block script
|
||||
add_filter(
|
||||
'woocommerce_blocks_checkout_block_registration_data',
|
||||
function (array $data): array {
|
||||
$data['wc-licensed-product'] = [
|
||||
'hasLicensedProducts' => $this->cartHasLicensedProducts(),
|
||||
];
|
||||
return $data;
|
||||
add_action('woocommerce_blocks_loaded', function (): void {
|
||||
// Check if the function exists (WooCommerce 8.9+)
|
||||
if (!function_exists('woocommerce_register_additional_checkout_field')) {
|
||||
return;
|
||||
}
|
||||
);
|
||||
|
||||
// Register the domain field using WooCommerce's checkout fields API
|
||||
// For single domain mode only (multi-domain uses custom JS component)
|
||||
if (!SettingsController::isMultiDomainEnabled()) {
|
||||
woocommerce_register_additional_checkout_field([
|
||||
'id' => 'wc-licensed-product/domain',
|
||||
'label' => __('License Domain', 'wc-licensed-product'),
|
||||
'location' => 'order',
|
||||
'type' => 'text',
|
||||
'required' => false,
|
||||
'attributes' => [
|
||||
'placeholder' => __('example.com', 'wc-licensed-product'),
|
||||
'pattern' => '^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$',
|
||||
'title' => __('Enter a valid domain (without http:// or www)', 'wc-licensed-product'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,13 +110,23 @@ final class CheckoutBlocksIntegration implements IntegrationInterface
|
||||
*/
|
||||
public function get_script_data(): array
|
||||
{
|
||||
$isMultiDomain = SettingsController::isMultiDomainEnabled();
|
||||
|
||||
return [
|
||||
'hasLicensedProducts' => $this->cartHasLicensedProducts(),
|
||||
'fieldLabel' => __('Domain for License Activation', 'wc-licensed-product'),
|
||||
'licensedProducts' => $this->getLicensedProductsFromCart(),
|
||||
'isMultiDomainEnabled' => $isMultiDomain,
|
||||
'fieldPlaceholder' => __('example.com', 'wc-licensed-product'),
|
||||
'fieldDescription' => __('Enter the domain where you will use this license (without http:// or www).', 'wc-licensed-product'),
|
||||
'sectionTitle' => __('License Domain', 'wc-licensed-product'),
|
||||
'validationError' => __('Please enter a valid domain for your license activation.', 'wc-licensed-product'),
|
||||
'fieldDescription' => $isMultiDomain
|
||||
? __('Enter a unique domain for each license (without http:// or www).', 'wc-licensed-product')
|
||||
: __('Enter the domain where you will use the license (without http:// or www).', 'wc-licensed-product'),
|
||||
'sectionTitle' => $isMultiDomain
|
||||
? __('License Domains', 'wc-licensed-product')
|
||||
: __('License Domain', 'wc-licensed-product'),
|
||||
'validationError' => __('Please enter a valid domain.', 'wc-licensed-product'),
|
||||
'duplicateError' => __('Each license requires a unique domain.', 'wc-licensed-product'),
|
||||
'licenseLabel' => __('License %d:', 'wc-licensed-product'),
|
||||
'singleDomainLabel' => __('Domain', 'wc-licensed-product'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -110,18 +134,34 @@ final class CheckoutBlocksIntegration implements IntegrationInterface
|
||||
* Check if cart contains licensed products
|
||||
*/
|
||||
private function cartHasLicensedProducts(): bool
|
||||
{
|
||||
return !empty($this->getLicensedProductsFromCart());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get licensed products from cart with quantities
|
||||
*
|
||||
* @return array<int, array{product_id: int, name: string, quantity: int}>
|
||||
*/
|
||||
private function getLicensedProductsFromCart(): array
|
||||
{
|
||||
if (!WC()->cart) {
|
||||
return false;
|
||||
return [];
|
||||
}
|
||||
|
||||
$licensedProducts = [];
|
||||
foreach (WC()->cart->get_cart() as $cartItem) {
|
||||
$product = $cartItem['data'];
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
return true;
|
||||
$productId = $product->get_id();
|
||||
$licensedProducts[] = [
|
||||
'product_id' => $productId,
|
||||
'name' => $product->get_name(),
|
||||
'quantity' => (int) $cartItem['quantity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return $licensedProducts;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
||||
namespace Jeremias\WcLicensedProduct\Checkout;
|
||||
|
||||
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
||||
use Jeremias\WcLicensedProduct\Admin\SettingsController;
|
||||
|
||||
/**
|
||||
* Handles checkout modifications for licensed products
|
||||
@@ -50,35 +51,75 @@ final class CheckoutController
|
||||
*/
|
||||
private function cartHasLicensedProducts(): bool
|
||||
{
|
||||
if (!WC()->cart) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (WC()->cart->get_cart() as $cartItem) {
|
||||
$product = $cartItem['data'];
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return !empty($this->getLicensedProductsFromCart());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add domain field to checkout form
|
||||
* Get licensed products from cart with quantities
|
||||
*
|
||||
* @return array<int, array{product_id: int, name: string, quantity: int}>
|
||||
*/
|
||||
private function getLicensedProductsFromCart(): array
|
||||
{
|
||||
if (!WC()->cart) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$licensedProducts = [];
|
||||
foreach (WC()->cart->get_cart() as $cartItem) {
|
||||
$product = $cartItem['data'];
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
$productId = $product->get_id();
|
||||
$licensedProducts[$productId] = [
|
||||
'product_id' => $productId,
|
||||
'name' => $product->get_name(),
|
||||
'quantity' => (int) $cartItem['quantity'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $licensedProducts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add domain fields to checkout form
|
||||
* Shows multiple domain fields if multi-domain is enabled, otherwise single field
|
||||
*/
|
||||
public function addDomainField(): void
|
||||
{
|
||||
if (!$this->cartHasLicensedProducts()) {
|
||||
$licensedProducts = $this->getLicensedProductsFromCart();
|
||||
if (empty($licensedProducts)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if multi-domain licensing is enabled
|
||||
if (SettingsController::isMultiDomainEnabled()) {
|
||||
$this->renderMultiDomainFields($licensedProducts);
|
||||
} else {
|
||||
$this->renderSingleDomainField();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render single domain field (legacy mode)
|
||||
*/
|
||||
private function renderSingleDomainField(): void
|
||||
{
|
||||
$savedValue = '';
|
||||
|
||||
// Check POST data first (validation failure case)
|
||||
if (isset($_POST['licensed_product_domain'])) {
|
||||
$savedValue = sanitize_text_field($_POST['licensed_product_domain']);
|
||||
} elseif (WC()->session) {
|
||||
$savedValue = WC()->session->get('licensed_product_domain', '');
|
||||
}
|
||||
|
||||
?>
|
||||
<div id="licensed-product-domain-field">
|
||||
<h3><?php esc_html_e('License Domain', 'wc-licensed-product'); ?></h3>
|
||||
<p class="form-row form-row-wide">
|
||||
<label for="licensed_product_domain">
|
||||
<?php esc_html_e('Domain for License Activation', 'wc-licensed-product'); ?>
|
||||
<?php esc_html_e('Domain', 'wc-licensed-product'); ?>
|
||||
<abbr class="required" title="<?php esc_attr_e('required', 'wc-licensed-product'); ?>">*</abbr>
|
||||
</label>
|
||||
<input
|
||||
@@ -87,10 +128,10 @@ final class CheckoutController
|
||||
name="licensed_product_domain"
|
||||
id="licensed_product_domain"
|
||||
placeholder="<?php esc_attr_e('example.com', 'wc-licensed-product'); ?>"
|
||||
value="<?php echo esc_attr(WC()->checkout->get_value('licensed_product_domain')); ?>"
|
||||
value="<?php echo esc_attr($savedValue); ?>"
|
||||
/>
|
||||
<span class="description">
|
||||
<?php esc_html_e('Enter the domain where you will use this license (without http:// or www).', 'wc-licensed-product'); ?>
|
||||
<?php esc_html_e('Enter the domain where you will use the license (without http:// or www).', 'wc-licensed-product'); ?>
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
@@ -98,62 +139,276 @@ final class CheckoutController
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate domain field during checkout
|
||||
* Render multi-domain fields (one per quantity)
|
||||
*/
|
||||
private function renderMultiDomainFields(array $licensedProducts): void
|
||||
{
|
||||
?>
|
||||
<div id="licensed-product-domain-fields">
|
||||
<h3><?php esc_html_e('License Domains', 'wc-licensed-product'); ?></h3>
|
||||
<p class="wclp-domain-description">
|
||||
<?php esc_html_e('Enter a unique domain for each license (without http:// or www).', 'wc-licensed-product'); ?>
|
||||
</p>
|
||||
|
||||
<?php foreach ($licensedProducts as $productId => $productData): ?>
|
||||
<div class="wclp-product-domains" data-product-id="<?php echo esc_attr($productId); ?>">
|
||||
<h4>
|
||||
<?php
|
||||
echo esc_html($productData['name']);
|
||||
if ($productData['quantity'] > 1) {
|
||||
printf(' (×%d)', $productData['quantity']);
|
||||
}
|
||||
?>
|
||||
</h4>
|
||||
|
||||
<?php for ($i = 0; $i < $productData['quantity']; $i++): ?>
|
||||
<?php
|
||||
$fieldName = sprintf('licensed_domains[%d][%d]', $productId, $i);
|
||||
$fieldId = sprintf('licensed_domain_%d_%d', $productId, $i);
|
||||
$savedValue = $this->getSavedDomainValue($productId, $i);
|
||||
?>
|
||||
<p class="form-row form-row-wide wclp-domain-row">
|
||||
<label for="<?php echo esc_attr($fieldId); ?>">
|
||||
<?php
|
||||
printf(
|
||||
/* translators: %d: license number */
|
||||
esc_html__('License %d:', 'wc-licensed-product'),
|
||||
$i + 1
|
||||
);
|
||||
?>
|
||||
<abbr class="required" title="<?php esc_attr_e('required', 'wc-licensed-product'); ?>">*</abbr>
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
class="input-text wclp-domain-input"
|
||||
name="<?php echo esc_attr($fieldName); ?>"
|
||||
id="<?php echo esc_attr($fieldId); ?>"
|
||||
placeholder="<?php esc_attr_e('example.com', 'wc-licensed-product'); ?>"
|
||||
value="<?php echo esc_attr($savedValue); ?>"
|
||||
/>
|
||||
</p>
|
||||
<?php endfor; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<style>
|
||||
#licensed-product-domain-fields { margin-bottom: 20px; }
|
||||
#licensed-product-domain-fields h3 { margin-bottom: 10px; }
|
||||
.wclp-domain-description { margin-bottom: 15px; color: #666; }
|
||||
.wclp-product-domains { margin-bottom: 20px; padding: 15px; background: #f8f8f8; border-radius: 4px; }
|
||||
.wclp-product-domains h4 { margin: 0 0 10px 0; font-size: 1em; }
|
||||
.wclp-domain-row { margin-bottom: 10px; }
|
||||
.wclp-domain-row:last-child { margin-bottom: 0; }
|
||||
.wclp-domain-row label { display: block; margin-bottom: 5px; }
|
||||
</style>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Get saved domain value from session/POST
|
||||
*/
|
||||
private function getSavedDomainValue(int $productId, int $index): string
|
||||
{
|
||||
// Check POST data first (validation failure case)
|
||||
if (isset($_POST['licensed_domains'][$productId][$index])) {
|
||||
return sanitize_text_field($_POST['licensed_domains'][$productId][$index]);
|
||||
}
|
||||
|
||||
// Check session for blocks checkout
|
||||
if (WC()->session) {
|
||||
$sessionDomains = WC()->session->get('licensed_product_domains', []);
|
||||
foreach ($sessionDomains as $item) {
|
||||
if (isset($item['product_id']) && (int) $item['product_id'] === $productId) {
|
||||
if (isset($item['domains'][$index])) {
|
||||
return $item['domains'][$index];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate domain fields during checkout
|
||||
*/
|
||||
public function validateDomainField(): void
|
||||
{
|
||||
if (!$this->cartHasLicensedProducts()) {
|
||||
$licensedProducts = $this->getLicensedProductsFromCart();
|
||||
if (empty($licensedProducts)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$domain = isset($_POST['licensed_product_domain'])
|
||||
? sanitize_text_field($_POST['licensed_product_domain'])
|
||||
: '';
|
||||
|
||||
if (empty($domain)) {
|
||||
wc_add_notice(
|
||||
__('Please enter a domain for your license activation.', 'wc-licensed-product'),
|
||||
'error'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate domain format
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
if (!$this->isValidDomain($normalizedDomain)) {
|
||||
wc_add_notice(
|
||||
__('Please enter a valid domain name.', 'wc-licensed-product'),
|
||||
'error'
|
||||
);
|
||||
// Check if multi-domain licensing is enabled
|
||||
if (SettingsController::isMultiDomainEnabled()) {
|
||||
$this->validateMultiDomainFields($licensedProducts);
|
||||
} else {
|
||||
$this->validateSingleDomainField();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save domain field to order meta
|
||||
* Validate single domain field
|
||||
*/
|
||||
public function saveDomainField(int $orderId): void
|
||||
private function validateSingleDomainField(): void
|
||||
{
|
||||
if (!$this->cartHasLicensedProducts()) {
|
||||
$domain = isset($_POST['licensed_product_domain']) ? sanitize_text_field($_POST['licensed_product_domain']) : '';
|
||||
|
||||
if (empty($domain)) {
|
||||
wc_add_notice(__('Please enter a domain for your license.', 'wc-licensed-product'), 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($_POST['licensed_product_domain']) && !empty($_POST['licensed_product_domain'])) {
|
||||
$domain = sanitize_text_field($_POST['licensed_product_domain']);
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
if (!$this->isValidDomain($normalizedDomain)) {
|
||||
wc_add_notice(__('Please enter a valid domain for your license.', 'wc-licensed-product'), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
$order = wc_get_order($orderId);
|
||||
if ($order) {
|
||||
$order->update_meta_data('_licensed_product_domain', $normalizedDomain);
|
||||
$order->save();
|
||||
/**
|
||||
* Validate multi-domain fields
|
||||
*/
|
||||
private function validateMultiDomainFields(array $licensedProducts): void
|
||||
{
|
||||
$licensedDomains = $_POST['licensed_domains'] ?? [];
|
||||
|
||||
foreach ($licensedProducts as $productId => $productData) {
|
||||
$productDomains = $licensedDomains[$productId] ?? [];
|
||||
$normalizedDomains = [];
|
||||
|
||||
for ($i = 0; $i < $productData['quantity']; $i++) {
|
||||
$domain = isset($productDomains[$i]) ? sanitize_text_field($productDomains[$i]) : '';
|
||||
|
||||
// Check if domain is empty
|
||||
if (empty($domain)) {
|
||||
wc_add_notice(
|
||||
sprintf(
|
||||
/* translators: 1: product name, 2: license number */
|
||||
__('Please enter a domain for %1$s (License %2$d).', 'wc-licensed-product'),
|
||||
$productData['name'],
|
||||
$i + 1
|
||||
),
|
||||
'error'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate domain format
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
if (!$this->isValidDomain($normalizedDomain)) {
|
||||
wc_add_notice(
|
||||
sprintf(
|
||||
/* translators: 1: product name, 2: license number */
|
||||
__('Please enter a valid domain for %1$s (License %2$d).', 'wc-licensed-product'),
|
||||
$productData['name'],
|
||||
$i + 1
|
||||
),
|
||||
'error'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for duplicate domains within same product
|
||||
if (in_array($normalizedDomain, $normalizedDomains, true)) {
|
||||
wc_add_notice(
|
||||
sprintf(
|
||||
/* translators: 1: domain name, 2: product name */
|
||||
__('The domain "%1$s" is used multiple times for %2$s. Each license requires a unique domain.', 'wc-licensed-product'),
|
||||
$normalizedDomain,
|
||||
$productData['name']
|
||||
),
|
||||
'error'
|
||||
);
|
||||
} else {
|
||||
$normalizedDomains[] = $normalizedDomain;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display domain in admin order view
|
||||
* Save domain fields to order meta
|
||||
*/
|
||||
public function saveDomainField(int $orderId): void
|
||||
{
|
||||
$licensedProducts = $this->getLicensedProductsFromCart();
|
||||
if (empty($licensedProducts)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$order = wc_get_order($orderId);
|
||||
if (!$order) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if multi-domain licensing is enabled
|
||||
if (SettingsController::isMultiDomainEnabled()) {
|
||||
$this->saveMultiDomainFields($order, $licensedProducts);
|
||||
} else {
|
||||
$this->saveSingleDomainField($order);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save single domain field to order meta (legacy format)
|
||||
*/
|
||||
private function saveSingleDomainField(\WC_Order $order): void
|
||||
{
|
||||
$domain = isset($_POST['licensed_product_domain']) ? sanitize_text_field($_POST['licensed_product_domain']) : '';
|
||||
|
||||
if (!empty($domain)) {
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
$order->update_meta_data('_licensed_product_domain', $normalizedDomain);
|
||||
$order->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save multi-domain fields to order meta
|
||||
*/
|
||||
private function saveMultiDomainFields(\WC_Order $order, array $licensedProducts): void
|
||||
{
|
||||
$licensedDomains = $_POST['licensed_domains'] ?? [];
|
||||
$domainData = [];
|
||||
|
||||
foreach ($licensedProducts as $productId => $productData) {
|
||||
$productDomains = $licensedDomains[$productId] ?? [];
|
||||
$normalizedDomains = [];
|
||||
|
||||
for ($i = 0; $i < $productData['quantity']; $i++) {
|
||||
$domain = isset($productDomains[$i]) ? sanitize_text_field($productDomains[$i]) : '';
|
||||
if (!empty($domain)) {
|
||||
$normalizedDomains[] = $this->licenseManager->normalizeDomain($domain);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($normalizedDomains)) {
|
||||
$domainData[] = [
|
||||
'product_id' => $productId,
|
||||
'domains' => $normalizedDomains,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($domainData)) {
|
||||
$order->update_meta_data('_licensed_product_domains', $domainData);
|
||||
$order->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display domains in admin order view
|
||||
*/
|
||||
public function displayDomainInAdmin(\WC_Order $order): void
|
||||
{
|
||||
// Try new multi-domain format first
|
||||
$domainData = $order->get_meta('_licensed_product_domains');
|
||||
if (!empty($domainData) && is_array($domainData)) {
|
||||
$this->displayMultiDomainsInAdmin($domainData);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fall back to legacy single domain
|
||||
$domain = $order->get_meta('_licensed_product_domain');
|
||||
if (!$domain) {
|
||||
return;
|
||||
@@ -168,10 +423,40 @@ final class CheckoutController
|
||||
}
|
||||
|
||||
/**
|
||||
* Display domain in order emails
|
||||
* Display multi-domain data in admin
|
||||
*/
|
||||
private function displayMultiDomainsInAdmin(array $domainData): void
|
||||
{
|
||||
?>
|
||||
<div class="wclp-order-domains">
|
||||
<strong><?php esc_html_e('License Domains:', 'wc-licensed-product'); ?></strong>
|
||||
<?php foreach ($domainData as $item): ?>
|
||||
<?php
|
||||
$product = wc_get_product($item['product_id']);
|
||||
$productName = $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product');
|
||||
?>
|
||||
<p style="margin: 5px 0 5px 15px;">
|
||||
<em><?php echo esc_html($productName); ?>:</em><br>
|
||||
<?php echo esc_html(implode(', ', $item['domains'])); ?>
|
||||
</p>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Display domains in order emails
|
||||
*/
|
||||
public function displayDomainInEmail(\WC_Order $order, bool $sentToAdmin, bool $plainText): void
|
||||
{
|
||||
// Try new multi-domain format first
|
||||
$domainData = $order->get_meta('_licensed_product_domains');
|
||||
if (!empty($domainData) && is_array($domainData)) {
|
||||
$this->displayMultiDomainsInEmail($domainData, $plainText);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fall back to legacy single domain
|
||||
$domain = $order->get_meta('_licensed_product_domain');
|
||||
if (!$domain) {
|
||||
return;
|
||||
@@ -189,6 +474,37 @@ final class CheckoutController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display multi-domain data in email
|
||||
*/
|
||||
private function displayMultiDomainsInEmail(array $domainData, bool $plainText): void
|
||||
{
|
||||
if ($plainText) {
|
||||
echo "\n" . esc_html__('License Domains:', 'wc-licensed-product') . "\n";
|
||||
foreach ($domainData as $item) {
|
||||
$product = wc_get_product($item['product_id']);
|
||||
$productName = $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product');
|
||||
echo ' ' . esc_html($productName) . ': ' . esc_html(implode(', ', $item['domains'])) . "\n";
|
||||
}
|
||||
} else {
|
||||
?>
|
||||
<div style="margin-bottom: 15px;">
|
||||
<strong><?php esc_html_e('License Domains:', 'wc-licensed-product'); ?></strong>
|
||||
<?php foreach ($domainData as $item): ?>
|
||||
<?php
|
||||
$product = wc_get_product($item['product_id']);
|
||||
$productName = $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product');
|
||||
?>
|
||||
<p style="margin: 5px 0 5px 15px;">
|
||||
<em><?php echo esc_html($productName); ?>:</em><br>
|
||||
<?php echo esc_html(implode(', ', $item['domains'])); ?>
|
||||
</p>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate domain format
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Jeremias\WcLicensedProduct\Checkout;
|
||||
use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema;
|
||||
use Automattic\WooCommerce\StoreApi\StoreApi;
|
||||
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
|
||||
use Jeremias\WcLicensedProduct\Admin\SettingsController;
|
||||
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
||||
|
||||
/**
|
||||
@@ -70,6 +71,12 @@ final class StoreApiExtension
|
||||
*/
|
||||
public function getExtensionData(): array
|
||||
{
|
||||
if (SettingsController::isMultiDomainEnabled()) {
|
||||
return [
|
||||
'licensed_product_domains' => WC()->session ? WC()->session->get('licensed_product_domains', []) : [],
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'licensed_product_domain' => WC()->session ? WC()->session->get('licensed_product_domain', '') : '',
|
||||
];
|
||||
@@ -80,6 +87,31 @@ final class StoreApiExtension
|
||||
*/
|
||||
public function getExtensionSchema(): array
|
||||
{
|
||||
if (SettingsController::isMultiDomainEnabled()) {
|
||||
return [
|
||||
'licensed_product_domains' => [
|
||||
'description' => __('Domains for license activation by product', 'wc-licensed-product'),
|
||||
'type' => 'array',
|
||||
'context' => ['view', 'edit'],
|
||||
'readonly' => false,
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'product_id' => [
|
||||
'type' => 'integer',
|
||||
],
|
||||
'domains' => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'string',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'licensed_product_domain' => [
|
||||
'description' => __('Domain for license activation', 'wc-licensed-product'),
|
||||
@@ -95,30 +127,103 @@ final class StoreApiExtension
|
||||
*/
|
||||
public function handleExtensionUpdate(array $data): void
|
||||
{
|
||||
if (isset($data['licensed_product_domain'])) {
|
||||
$domain = sanitize_text_field($data['licensed_product_domain']);
|
||||
$normalizedDomain = $this->licenseManager->normalizeDomain($domain);
|
||||
if (SettingsController::isMultiDomainEnabled()) {
|
||||
// Multi-domain mode
|
||||
if (isset($data['licensed_product_domains']) && is_array($data['licensed_product_domains'])) {
|
||||
$normalizedData = $this->normalizeDomainsData($data['licensed_product_domains']);
|
||||
|
||||
if (WC()->session) {
|
||||
WC()->session->set('licensed_product_domain', $normalizedDomain);
|
||||
if (WC()->session) {
|
||||
WC()->session->set('licensed_product_domains', $normalizedData);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Single domain mode
|
||||
if (isset($data['licensed_product_domain'])) {
|
||||
$sanitized = sanitize_text_field($data['licensed_product_domain']);
|
||||
$normalized = $this->licenseManager->normalizeDomain($sanitized);
|
||||
|
||||
if (WC()->session) {
|
||||
WC()->session->set('licensed_product_domain', $normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the checkout order - save domain to order meta
|
||||
* Normalize domains data from frontend
|
||||
*/
|
||||
private function normalizeDomainsData(array $domainsData): array
|
||||
{
|
||||
$normalized = [];
|
||||
|
||||
foreach ($domainsData as $item) {
|
||||
if (!isset($item['product_id']) || !isset($item['domains']) || !is_array($item['domains'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$productId = (int) $item['product_id'];
|
||||
$domains = [];
|
||||
|
||||
foreach ($item['domains'] as $domain) {
|
||||
$sanitized = sanitize_text_field($domain);
|
||||
if (!empty($sanitized)) {
|
||||
$domains[] = $this->licenseManager->normalizeDomain($sanitized);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($domains)) {
|
||||
$normalized[] = [
|
||||
'product_id' => $productId,
|
||||
'domains' => $domains,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the checkout order - save domains to order meta
|
||||
*/
|
||||
public function processCheckoutOrder(\WC_Order $order): void
|
||||
{
|
||||
$domain = WC()->session ? WC()->session->get('licensed_product_domain', '') : '';
|
||||
$requestData = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
// Also check in the request data for block checkout
|
||||
if (empty($domain)) {
|
||||
$requestData = json_decode(file_get_contents('php://input'), true);
|
||||
if (isset($requestData['extensions'][self::IDENTIFIER]['licensed_product_domain'])) {
|
||||
$domain = sanitize_text_field($requestData['extensions'][self::IDENTIFIER]['licensed_product_domain']);
|
||||
$domain = $this->licenseManager->normalizeDomain($domain);
|
||||
}
|
||||
if (SettingsController::isMultiDomainEnabled()) {
|
||||
$this->processMultiDomainOrder($order, $requestData);
|
||||
} else {
|
||||
$this->processSingleDomainOrder($order, $requestData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process order in single domain mode (legacy)
|
||||
*/
|
||||
private function processSingleDomainOrder(\WC_Order $order, ?array $requestData): void
|
||||
{
|
||||
$domain = '';
|
||||
|
||||
// Check session first
|
||||
if (WC()->session) {
|
||||
$domain = WC()->session->get('licensed_product_domain', '');
|
||||
}
|
||||
|
||||
// Check in the request data for block checkout (extension data)
|
||||
if (empty($domain) && isset($requestData['extensions'][self::IDENTIFIER]['licensed_product_domain'])) {
|
||||
$sanitized = sanitize_text_field($requestData['extensions'][self::IDENTIFIER]['licensed_product_domain']);
|
||||
$domain = $this->licenseManager->normalizeDomain($sanitized);
|
||||
}
|
||||
|
||||
// Check for wclp_license_domain (from our hidden input)
|
||||
if (empty($domain) && isset($requestData['wclp_license_domain'])) {
|
||||
$sanitized = sanitize_text_field($requestData['wclp_license_domain']);
|
||||
$domain = $this->licenseManager->normalizeDomain($sanitized);
|
||||
}
|
||||
|
||||
// Check for additional_fields (WC Blocks API)
|
||||
if (empty($domain) && isset($requestData['additional_fields']['wc-licensed-product/domain'])) {
|
||||
$sanitized = sanitize_text_field($requestData['additional_fields']['wc-licensed-product/domain']);
|
||||
$domain = $this->licenseManager->normalizeDomain($sanitized);
|
||||
}
|
||||
|
||||
if (!empty($domain)) {
|
||||
@@ -131,4 +236,65 @@ final class StoreApiExtension
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Process order in multi-domain mode
|
||||
*/
|
||||
private function processMultiDomainOrder(\WC_Order $order, ?array $requestData): void
|
||||
{
|
||||
$domainData = [];
|
||||
|
||||
// Check session first
|
||||
if (WC()->session) {
|
||||
$domainData = WC()->session->get('licensed_product_domains', []);
|
||||
}
|
||||
|
||||
// Check in the request data for block checkout (extension data)
|
||||
if (empty($domainData) && isset($requestData['extensions'][self::IDENTIFIER]['licensed_product_domains'])) {
|
||||
$domainData = $this->normalizeDomainsData(
|
||||
$requestData['extensions'][self::IDENTIFIER]['licensed_product_domains']
|
||||
);
|
||||
}
|
||||
|
||||
// Check for wclp_license_domains (from our hidden input - JSON string)
|
||||
if (empty($domainData) && isset($requestData['wclp_license_domains'])) {
|
||||
$parsed = json_decode($requestData['wclp_license_domains'], true);
|
||||
if (is_array($parsed)) {
|
||||
$domainData = $this->normalizeDomainsData($parsed);
|
||||
}
|
||||
}
|
||||
|
||||
// Check for licensed_domains in classic format (from DOM injection)
|
||||
if (empty($domainData) && isset($requestData['licensed_domains']) && is_array($requestData['licensed_domains'])) {
|
||||
$domainData = [];
|
||||
foreach ($requestData['licensed_domains'] as $productId => $domains) {
|
||||
if (!is_array($domains)) {
|
||||
continue;
|
||||
}
|
||||
$normalizedDomains = [];
|
||||
foreach ($domains as $domain) {
|
||||
$sanitized = sanitize_text_field($domain);
|
||||
if (!empty($sanitized)) {
|
||||
$normalizedDomains[] = $this->licenseManager->normalizeDomain($sanitized);
|
||||
}
|
||||
}
|
||||
if (!empty($normalizedDomains)) {
|
||||
$domainData[] = [
|
||||
'product_id' => (int) $productId,
|
||||
'domains' => $normalizedDomains,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($domainData)) {
|
||||
$order->update_meta_data('_licensed_product_domains', $domainData);
|
||||
$order->save();
|
||||
|
||||
// Clear session data
|
||||
if (WC()->session) {
|
||||
WC()->session->set('licensed_product_domains', []);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ final class LicenseEmailController
|
||||
}
|
||||
|
||||
/**
|
||||
* Add license key to order item in email
|
||||
* Add license key(s) to order item in email
|
||||
*/
|
||||
public function addLicenseToOrderItem(int $itemId, \WC_Order_Item $item, \WC_Order $order, bool $plainText): void
|
||||
{
|
||||
@@ -203,94 +203,117 @@ final class LicenseEmailController
|
||||
return;
|
||||
}
|
||||
|
||||
$license = $this->licenseManager->getLicenseByOrderAndProduct($order->get_id(), $product->get_id());
|
||||
if (!$license) {
|
||||
$licenses = $this->licenseManager->getLicensesByOrderAndProduct($order->get_id(), $product->get_id());
|
||||
if (empty($licenses)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($plainText) {
|
||||
echo "\n" . esc_html__('License Key:', 'wc-licensed-product') . ' ' . esc_html($license->getLicenseKey()) . "\n";
|
||||
echo "\n" . esc_html__('License Keys:', 'wc-licensed-product') . "\n";
|
||||
foreach ($licenses as $license) {
|
||||
echo ' - ' . esc_html($license->getLicenseKey());
|
||||
echo ' (' . esc_html($license->getDomain()) . ')' . "\n";
|
||||
}
|
||||
} else {
|
||||
?>
|
||||
<div style="margin-top: 10px; padding: 10px; background-color: #f8f9fa; border-left: 3px solid #7f54b3;">
|
||||
<strong><?php esc_html_e('License Key:', 'wc-licensed-product'); ?></strong>
|
||||
<code style="display: block; margin-top: 5px; padding: 5px; background: #fff; font-family: monospace;">
|
||||
<?php echo esc_html($license->getLicenseKey()); ?>
|
||||
</code>
|
||||
<strong><?php esc_html_e('License Keys:', 'wc-licensed-product'); ?></strong>
|
||||
<?php foreach ($licenses as $license) : ?>
|
||||
<div style="margin-top: 5px; padding: 5px; background: #fff;">
|
||||
<code style="font-family: monospace;">
|
||||
<?php echo esc_html($license->getLicenseKey()); ?>
|
||||
</code>
|
||||
<span style="color: #666; margin-left: 10px;">
|
||||
<?php echo esc_html($license->getDomain()); ?>
|
||||
</span>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all licenses for an order
|
||||
* Get all licenses for an order grouped by product
|
||||
*
|
||||
* @return array Array of products with their licenses
|
||||
*/
|
||||
private function getLicensesForOrder(\WC_Order $order): array
|
||||
{
|
||||
$licenses = [];
|
||||
$products = [];
|
||||
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
$license = $this->licenseManager->getLicenseByOrderAndProduct($order->get_id(), $product->get_id());
|
||||
if ($license) {
|
||||
$licenses[] = [
|
||||
'license' => $license,
|
||||
$licenses = $this->licenseManager->getLicensesByOrderAndProduct($order->get_id(), $product->get_id());
|
||||
if (!empty($licenses)) {
|
||||
$products[] = [
|
||||
'product_name' => $product->get_name(),
|
||||
'licenses' => $licenses,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $licenses;
|
||||
return $products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render license info in HTML format
|
||||
*/
|
||||
private function renderHtmlLicenseInfo(array $licenses, \WC_Order $order): void
|
||||
private function renderHtmlLicenseInfo(array $products, \WC_Order $order): void
|
||||
{
|
||||
$domain = $order->get_meta('_licensed_product_domain');
|
||||
?>
|
||||
<div style="margin: 20px 0; padding: 20px; background-color: #f8f9fa; border: 1px solid #e5e5e5; border-radius: 4px;">
|
||||
<h2 style="margin-top: 0; color: #333;"><?php esc_html_e('Your License Keys', 'wc-licensed-product'); ?></h2>
|
||||
|
||||
<?php if ($domain) : ?>
|
||||
<p style="margin-bottom: 15px;">
|
||||
<strong><?php esc_html_e('Licensed Domain:', 'wc-licensed-product'); ?></strong>
|
||||
<?php echo esc_html($domain); ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
<?php foreach ($products as $product) : ?>
|
||||
<div style="margin-bottom: 20px;">
|
||||
<h3 style="margin: 0 0 10px 0; font-size: 1.1em; color: #333;">
|
||||
<?php echo esc_html($product['product_name']); ?>
|
||||
<span style="font-weight: normal; color: #666; font-size: 0.9em;">
|
||||
(<?php
|
||||
printf(
|
||||
esc_html(_n('%d license', '%d licenses', count($product['licenses']), 'wc-licensed-product')),
|
||||
count($product['licenses'])
|
||||
);
|
||||
?>)
|
||||
</span>
|
||||
</h3>
|
||||
|
||||
<table style="width: 100%; border-collapse: collapse;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="text-align: left; padding: 10px; border-bottom: 2px solid #ddd;"><?php esc_html_e('Product', 'wc-licensed-product'); ?></th>
|
||||
<th style="text-align: left; padding: 10px; border-bottom: 2px solid #ddd;"><?php esc_html_e('License Key', 'wc-licensed-product'); ?></th>
|
||||
<th style="text-align: left; padding: 10px; border-bottom: 2px solid #ddd;"><?php esc_html_e('Expires', 'wc-licensed-product'); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($licenses as $item) : ?>
|
||||
<tr>
|
||||
<td style="padding: 10px; border-bottom: 1px solid #eee;"><?php echo esc_html($item['product_name']); ?></td>
|
||||
<td style="padding: 10px; border-bottom: 1px solid #eee;">
|
||||
<code style="background: #fff; padding: 3px 6px; font-family: monospace;">
|
||||
<?php echo esc_html($item['license']->getLicenseKey()); ?>
|
||||
</code>
|
||||
</td>
|
||||
<td style="padding: 10px; border-bottom: 1px solid #eee;">
|
||||
<?php
|
||||
$expiresAt = $item['license']->getExpiresAt();
|
||||
echo $expiresAt
|
||||
? esc_html($expiresAt->format(get_option('date_format')))
|
||||
: esc_html__('Never', 'wc-licensed-product');
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<table style="width: 100%; border-collapse: collapse; background: #fff;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="text-align: left; padding: 8px 10px; border-bottom: 2px solid #ddd; font-size: 0.9em;"><?php esc_html_e('License Key', 'wc-licensed-product'); ?></th>
|
||||
<th style="text-align: left; padding: 8px 10px; border-bottom: 2px solid #ddd; font-size: 0.9em;"><?php esc_html_e('Domain', 'wc-licensed-product'); ?></th>
|
||||
<th style="text-align: left; padding: 8px 10px; border-bottom: 2px solid #ddd; font-size: 0.9em;"><?php esc_html_e('Expires', 'wc-licensed-product'); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($product['licenses'] as $license) : ?>
|
||||
<tr>
|
||||
<td style="padding: 8px 10px; border-bottom: 1px solid #eee;">
|
||||
<code style="background: #f5f5f5; padding: 3px 6px; font-family: monospace; font-size: 0.9em;">
|
||||
<?php echo esc_html($license->getLicenseKey()); ?>
|
||||
</code>
|
||||
</td>
|
||||
<td style="padding: 8px 10px; border-bottom: 1px solid #eee;">
|
||||
<?php echo esc_html($license->getDomain()); ?>
|
||||
</td>
|
||||
<td style="padding: 8px 10px; border-bottom: 1px solid #eee;">
|
||||
<?php
|
||||
$expiresAt = $license->getExpiresAt();
|
||||
echo $expiresAt
|
||||
? esc_html($expiresAt->format(get_option('date_format')))
|
||||
: esc_html__('Never', 'wc-licensed-product');
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
|
||||
<p style="margin-top: 15px; margin-bottom: 0; font-size: 0.9em; color: #666;">
|
||||
<?php esc_html_e('You can also view your licenses in your account under "Licenses".', 'wc-licensed-product'); ?>
|
||||
@@ -302,29 +325,33 @@ final class LicenseEmailController
|
||||
/**
|
||||
* Render license info in plain text format
|
||||
*/
|
||||
private function renderPlainTextLicenseInfo(array $licenses, \WC_Order $order): void
|
||||
private function renderPlainTextLicenseInfo(array $products, \WC_Order $order): void
|
||||
{
|
||||
$domain = $order->get_meta('_licensed_product_domain');
|
||||
|
||||
echo "\n\n";
|
||||
echo "==========================================================\n";
|
||||
echo esc_html__('YOUR LICENSE KEYS', 'wc-licensed-product') . "\n";
|
||||
echo "==========================================================\n\n";
|
||||
|
||||
if ($domain) {
|
||||
echo esc_html__('Licensed Domain:', 'wc-licensed-product') . ' ' . esc_html($domain) . "\n\n";
|
||||
}
|
||||
foreach ($products as $product) {
|
||||
echo esc_html($product['product_name']);
|
||||
echo ' (' . count($product['licenses']) . ' ' .
|
||||
_n('license', 'licenses', count($product['licenses']), 'wc-licensed-product') . ')';
|
||||
echo "\n";
|
||||
echo "-----------------------------------------------------------\n";
|
||||
|
||||
foreach ($licenses as $item) {
|
||||
echo esc_html($item['product_name']) . "\n";
|
||||
echo esc_html__('License Key:', 'wc-licensed-product') . ' ' . esc_html($item['license']->getLicenseKey()) . "\n";
|
||||
foreach ($product['licenses'] as $license) {
|
||||
echo esc_html__('License Key:', 'wc-licensed-product') . ' ';
|
||||
echo esc_html($license->getLicenseKey()) . "\n";
|
||||
echo esc_html__('Domain:', 'wc-licensed-product') . ' ';
|
||||
echo esc_html($license->getDomain()) . "\n";
|
||||
echo esc_html__('Expires:', 'wc-licensed-product') . ' ';
|
||||
|
||||
$expiresAt = $item['license']->getExpiresAt();
|
||||
echo esc_html__('Expires:', 'wc-licensed-product') . ' ';
|
||||
echo $expiresAt
|
||||
? esc_html($expiresAt->format(get_option('date_format')))
|
||||
: esc_html__('Never', 'wc-licensed-product');
|
||||
echo "\n\n";
|
||||
$expiresAt = $license->getExpiresAt();
|
||||
echo $expiresAt
|
||||
? esc_html($expiresAt->format(get_option('date_format')))
|
||||
: esc_html__('Never', 'wc-licensed-product');
|
||||
echo "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
echo esc_html__('You can also view your licenses in your account under "Licenses".', 'wc-licensed-product') . "\n";
|
||||
|
||||
@@ -107,135 +107,248 @@ final class AccountController
|
||||
|
||||
$licenses = $this->licenseManager->getLicensesByCustomer($customerId);
|
||||
|
||||
// Enrich licenses with product data and downloads
|
||||
$enrichedLicenses = [];
|
||||
foreach ($licenses as $license) {
|
||||
$product = wc_get_product($license->getProductId());
|
||||
$order = wc_get_order($license->getOrderId());
|
||||
|
||||
// Get available downloads for this license
|
||||
$downloads = [];
|
||||
if ($license->getStatus() === 'active') {
|
||||
$versions = $this->versionManager->getVersionsByProduct($license->getProductId());
|
||||
foreach ($versions as $version) {
|
||||
if ($version->isActive() && ($version->getAttachmentId() || $version->getDownloadUrl())) {
|
||||
$downloads[] = [
|
||||
'version' => $version->getVersion(),
|
||||
'version_id' => $version->getId(),
|
||||
'filename' => $version->getDownloadFilename(),
|
||||
'download_url' => $this->downloadController->generateDownloadUrl(
|
||||
$license->getId(),
|
||||
$version->getId()
|
||||
),
|
||||
'release_notes' => $version->getReleaseNotes(),
|
||||
'released_at' => $version->getReleasedAt()->format(get_option('date_format')),
|
||||
'file_hash' => $version->getFileHash(),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$enrichedLicenses[] = [
|
||||
'license' => $license,
|
||||
'product_name' => $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product'),
|
||||
'product_url' => $product ? $product->get_permalink() : '',
|
||||
'order_number' => $order ? $order->get_order_number() : '',
|
||||
'order_url' => $order ? $order->get_view_order_url() : '',
|
||||
'downloads' => $downloads,
|
||||
];
|
||||
}
|
||||
// Group licenses by product+order into "packages"
|
||||
$packages = $this->groupLicensesIntoPackages($licenses);
|
||||
|
||||
try {
|
||||
echo $this->twig->render('frontend/licenses.html.twig', [
|
||||
'licenses' => $enrichedLicenses,
|
||||
'has_licenses' => !empty($enrichedLicenses),
|
||||
'packages' => $packages,
|
||||
'has_packages' => !empty($packages),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
// Fallback to PHP template if Twig fails
|
||||
$this->displayLicensesFallback($enrichedLicenses);
|
||||
$this->displayLicensesFallback($packages);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Group licenses into packages by product+order
|
||||
*
|
||||
* @param array $licenses Array of License objects
|
||||
* @return array Array of package data
|
||||
*/
|
||||
private function groupLicensesIntoPackages(array $licenses): array
|
||||
{
|
||||
$grouped = [];
|
||||
|
||||
foreach ($licenses as $license) {
|
||||
$productId = $license->getProductId();
|
||||
$orderId = $license->getOrderId();
|
||||
$key = $productId . '_' . $orderId;
|
||||
|
||||
if (!isset($grouped[$key])) {
|
||||
$product = wc_get_product($productId);
|
||||
$order = wc_get_order($orderId);
|
||||
|
||||
$grouped[$key] = [
|
||||
'product_id' => $productId,
|
||||
'order_id' => $orderId,
|
||||
'product_name' => $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product'),
|
||||
'product_url' => $product ? $product->get_permalink() : '',
|
||||
'order_number' => $order ? $order->get_order_number() : '',
|
||||
'order_url' => $order ? $order->get_view_order_url() : '',
|
||||
'licenses' => [],
|
||||
'downloads' => [],
|
||||
'has_active_license' => false,
|
||||
];
|
||||
}
|
||||
|
||||
// Add license to package
|
||||
$grouped[$key]['licenses'][] = [
|
||||
'id' => $license->getId(),
|
||||
'license_key' => $license->getLicenseKey(),
|
||||
'domain' => $license->getDomain(),
|
||||
'status' => $license->getStatus(),
|
||||
'expires_at' => $license->getExpiresAt(),
|
||||
'is_transferable' => in_array($license->getStatus(), ['active', 'inactive'], true),
|
||||
];
|
||||
|
||||
// Track if package has at least one active license
|
||||
if ($license->getStatus() === 'active') {
|
||||
$grouped[$key]['has_active_license'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Add downloads for packages with active licenses
|
||||
foreach ($grouped as $key => &$package) {
|
||||
if ($package['has_active_license']) {
|
||||
$package['downloads'] = $this->getDownloadsForProduct(
|
||||
$package['product_id'],
|
||||
$package['licenses'][0]['id'] // Use first license for download URL
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by order date (newest first) - re-index array
|
||||
return array_values($grouped);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get downloads for a product
|
||||
*/
|
||||
private function getDownloadsForProduct(int $productId, int $licenseId): array
|
||||
{
|
||||
$downloads = [];
|
||||
$versions = $this->versionManager->getVersionsByProduct($productId);
|
||||
|
||||
foreach ($versions as $version) {
|
||||
if ($version->isActive() && ($version->getAttachmentId() || $version->getDownloadUrl())) {
|
||||
$downloads[] = [
|
||||
'version' => $version->getVersion(),
|
||||
'version_id' => $version->getId(),
|
||||
'filename' => $version->getDownloadFilename(),
|
||||
'download_url' => $this->downloadController->generateDownloadUrl(
|
||||
$licenseId,
|
||||
$version->getId()
|
||||
),
|
||||
'release_notes' => $version->getReleaseNotes(),
|
||||
'released_at' => $version->getReleasedAt()->format(get_option('date_format')),
|
||||
'file_hash' => $version->getFileHash(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $downloads;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback display method if Twig is unavailable
|
||||
*/
|
||||
private function displayLicensesFallback(array $enrichedLicenses): void
|
||||
private function displayLicensesFallback(array $packages): void
|
||||
{
|
||||
if (empty($enrichedLicenses)) {
|
||||
if (empty($packages)) {
|
||||
echo '<p>' . esc_html__('You have no licenses yet.', 'wc-licensed-product') . '</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="woocommerce-licenses">
|
||||
<?php foreach ($enrichedLicenses as $item): ?>
|
||||
<div class="license-card">
|
||||
<div class="license-header">
|
||||
<?php foreach ($packages as $package): ?>
|
||||
<div class="license-package">
|
||||
<div class="package-header">
|
||||
<h3>
|
||||
<?php if ($item['product_url']): ?>
|
||||
<a href="<?php echo esc_url($item['product_url']); ?>">
|
||||
<?php echo esc_html($item['product_name']); ?>
|
||||
<?php if ($package['product_url']): ?>
|
||||
<a href="<?php echo esc_url($package['product_url']); ?>">
|
||||
<?php echo esc_html($package['product_name']); ?>
|
||||
</a>
|
||||
<?php else: ?>
|
||||
<?php echo esc_html($item['product_name']); ?>
|
||||
<?php echo esc_html($package['product_name']); ?>
|
||||
<?php endif; ?>
|
||||
</h3>
|
||||
<span class="license-status license-status-<?php echo esc_attr($item['license']->getStatus()); ?>">
|
||||
<?php echo esc_html(ucfirst($item['license']->getStatus())); ?>
|
||||
<span class="package-order">
|
||||
<?php
|
||||
printf(
|
||||
/* translators: %s: order number */
|
||||
esc_html__('Order #%s', 'wc-licensed-product'),
|
||||
esc_html($package['order_number'])
|
||||
);
|
||||
?>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="license-details">
|
||||
<div class="license-key-row">
|
||||
<label><?php esc_html_e('License Key:', 'wc-licensed-product'); ?></label>
|
||||
<code class="license-key" data-license-key="<?php echo esc_attr($item['license']->getLicenseKey()); ?>">
|
||||
<?php echo esc_html($item['license']->getLicenseKey()); ?>
|
||||
</code>
|
||||
<button type="button" class="copy-license-btn" data-license-key="<?php echo esc_attr($item['license']->getLicenseKey()); ?>" title="<?php esc_attr_e('Copy to clipboard', 'wc-licensed-product'); ?>">
|
||||
<span class="dashicons dashicons-clipboard"></span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="license-info-row">
|
||||
<span class="license-domain-display" data-license-id="<?php echo esc_attr($item['license']->getId()); ?>">
|
||||
<strong><?php esc_html_e('Domain:', 'wc-licensed-product'); ?></strong>
|
||||
<span class="domain-value"><?php echo esc_html($item['license']->getDomain()); ?></span>
|
||||
<?php if (in_array($item['license']->getStatus(), ['active', 'inactive'], true)): ?>
|
||||
<button type="button" class="wclp-transfer-btn"
|
||||
data-license-id="<?php echo esc_attr($item['license']->getId()); ?>"
|
||||
data-current-domain="<?php echo esc_attr($item['license']->getDomain()); ?>"
|
||||
title="<?php esc_attr_e('Transfer to new domain', 'wc-licensed-product'); ?>">
|
||||
<span class="dashicons dashicons-randomize"></span>
|
||||
<?php esc_html_e('Transfer', 'wc-licensed-product'); ?>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
</span>
|
||||
<span><strong><?php esc_html_e('Expires:', 'wc-licensed-product'); ?></strong>
|
||||
<?php
|
||||
$expiresAt = $item['license']->getExpiresAt();
|
||||
echo $expiresAt
|
||||
? esc_html($expiresAt->format(get_option('date_format')))
|
||||
: esc_html__('Never', 'wc-licensed-product');
|
||||
?>
|
||||
</span>
|
||||
</div>
|
||||
<div class="package-licenses">
|
||||
<?php foreach ($package['licenses'] as $license): ?>
|
||||
<div class="license-entry license-entry-<?php echo esc_attr($license['status']); ?>">
|
||||
<div class="license-row-primary">
|
||||
<div class="license-key-group">
|
||||
<code class="license-key"><?php echo esc_html($license['license_key']); ?></code>
|
||||
<span class="license-status license-status-<?php echo esc_attr($license['status']); ?>">
|
||||
<?php echo esc_html(ucfirst($license['status'])); ?>
|
||||
</span>
|
||||
</div>
|
||||
<div class="license-actions">
|
||||
<button type="button" class="copy-license-btn" data-license-key="<?php echo esc_attr($license['license_key']); ?>" title="<?php esc_attr_e('Copy to clipboard', 'wc-licensed-product'); ?>">
|
||||
<span class="dashicons dashicons-clipboard"></span>
|
||||
</button>
|
||||
<?php if ($license['is_transferable']): ?>
|
||||
<button type="button" class="wclp-transfer-btn"
|
||||
data-license-id="<?php echo esc_attr($license['id']); ?>"
|
||||
data-current-domain="<?php echo esc_attr($license['domain']); ?>"
|
||||
title="<?php esc_attr_e('Transfer to new domain', 'wc-licensed-product'); ?>">
|
||||
<span class="dashicons dashicons-randomize"></span>
|
||||
</button>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<div class="license-row-secondary">
|
||||
<span class="license-meta-item license-domain">
|
||||
<span class="dashicons dashicons-admin-site-alt3"></span>
|
||||
<?php echo esc_html($license['domain']); ?>
|
||||
</span>
|
||||
<span class="license-meta-item license-expiry">
|
||||
<span class="dashicons dashicons-calendar-alt"></span>
|
||||
<?php
|
||||
echo $license['expires_at']
|
||||
? esc_html($license['expires_at']->format('Y-m-d'))
|
||||
: '<span class="lifetime">' . esc_html__('Lifetime', 'wc-licensed-product') . '</span>';
|
||||
?>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
|
||||
<?php if (!empty($item['downloads'])): ?>
|
||||
<div class="license-downloads">
|
||||
<?php if (!empty($package['downloads'])): ?>
|
||||
<div class="package-downloads">
|
||||
<h4><?php esc_html_e('Available Downloads', 'wc-licensed-product'); ?></h4>
|
||||
<ul class="download-list">
|
||||
<?php foreach ($item['downloads'] as $download): ?>
|
||||
<li>
|
||||
<a href="<?php echo esc_url($download['download_url']); ?>" class="download-link">
|
||||
<?php
|
||||
$latest = $package['downloads'][0];
|
||||
?>
|
||||
<li class="download-item download-item-latest">
|
||||
<div class="download-row-file">
|
||||
<a href="<?php echo esc_url($latest['download_url']); ?>" class="download-link">
|
||||
<span class="dashicons dashicons-download"></span>
|
||||
<?php echo esc_html($download['filename'] ?: sprintf(__('Version %s', 'wc-licensed-product'), $download['version'])); ?>
|
||||
<?php echo esc_html($latest['filename'] ?: sprintf(__('Version %s', 'wc-licensed-product'), $latest['version'])); ?>
|
||||
</a>
|
||||
<span class="download-version">v<?php echo esc_html($download['version']); ?></span>
|
||||
<span class="download-date"><?php echo esc_html($download['released_at']); ?></span>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
<span class="download-version-badge"><?php esc_html_e('Latest', 'wc-licensed-product'); ?></span>
|
||||
</div>
|
||||
<div class="download-row-meta">
|
||||
<span class="download-date"><?php echo esc_html($latest['released_at']); ?></span>
|
||||
<?php if (!empty($latest['file_hash'])): ?>
|
||||
<span class="download-hash" title="<?php echo esc_attr($latest['file_hash']); ?>">
|
||||
<span class="dashicons dashicons-shield"></span>
|
||||
<code><?php echo esc_html(substr($latest['file_hash'], 0, 12)); ?>...</code>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<?php if (count($package['downloads']) > 1): ?>
|
||||
<div class="older-versions-section">
|
||||
<button type="button" class="older-versions-toggle" aria-expanded="false">
|
||||
<span class="dashicons dashicons-arrow-down-alt2"></span>
|
||||
<?php
|
||||
printf(
|
||||
esc_html__('Older versions (%d)', 'wc-licensed-product'),
|
||||
count($package['downloads']) - 1
|
||||
);
|
||||
?>
|
||||
</button>
|
||||
<ul class="download-list older-versions-list" style="display: none;">
|
||||
<?php foreach (array_slice($package['downloads'], 1) as $download): ?>
|
||||
<li class="download-item">
|
||||
<div class="download-row-file">
|
||||
<a href="<?php echo esc_url($download['download_url']); ?>" class="download-link">
|
||||
<span class="dashicons dashicons-download"></span>
|
||||
<?php echo esc_html($download['filename'] ?: sprintf(__('Version %s', 'wc-licensed-product'), $download['version'])); ?>
|
||||
</a>
|
||||
</div>
|
||||
<div class="download-row-meta">
|
||||
<span class="download-date"><?php echo esc_html($download['released_at']); ?></span>
|
||||
<?php if (!empty($download['file_hash'])): ?>
|
||||
<span class="download-hash" title="<?php echo esc_attr($download['file_hash']); ?>">
|
||||
<span class="dashicons dashicons-shield"></span>
|
||||
<code><?php echo esc_html(substr($download['file_hash'], 0, 12)); ?>...</code>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
@@ -49,8 +49,11 @@ class LicenseManager
|
||||
): ?License {
|
||||
global $wpdb;
|
||||
|
||||
// Check if license already exists for this order and product
|
||||
$existing = $this->getLicenseByOrderAndProduct($orderId, $productId);
|
||||
// Normalize domain first for duplicate detection
|
||||
$normalizedDomain = $this->normalizeDomain($domain);
|
||||
|
||||
// Check if license already exists for this order, product, and domain
|
||||
$existing = $this->getLicenseByOrderProductAndDomain($orderId, $productId, $normalizedDomain);
|
||||
if ($existing) {
|
||||
return $existing;
|
||||
}
|
||||
@@ -161,6 +164,49 @@ class LicenseManager
|
||||
return $row ? License::fromArray($row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all licenses for an order and product
|
||||
*
|
||||
* @return License[]
|
||||
*/
|
||||
public function getLicensesByOrderAndProduct(int $orderId, int $productId): array
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
$tableName = Installer::getLicensesTable();
|
||||
$rows = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT * FROM {$tableName} WHERE order_id = %d AND product_id = %d ORDER BY created_at ASC",
|
||||
$orderId,
|
||||
$productId
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
return array_map(fn(array $row) => License::fromArray($row), $rows ?: []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get license by order, product, and domain
|
||||
*/
|
||||
public function getLicenseByOrderProductAndDomain(int $orderId, int $productId, string $domain): ?License
|
||||
{
|
||||
global $wpdb;
|
||||
|
||||
$tableName = Installer::getLicensesTable();
|
||||
$row = $wpdb->get_row(
|
||||
$wpdb->prepare(
|
||||
"SELECT * FROM {$tableName} WHERE order_id = %d AND product_id = %d AND domain = %s",
|
||||
$orderId,
|
||||
$productId,
|
||||
$domain
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
return $row ? License::fromArray($row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all licenses for an order
|
||||
*/
|
||||
|
||||
@@ -208,15 +208,50 @@ final class Plugin
|
||||
return;
|
||||
}
|
||||
|
||||
// Try new multi-domain format first
|
||||
$domainData = $order->get_meta('_licensed_product_domains');
|
||||
if (!empty($domainData) && is_array($domainData)) {
|
||||
$this->generateLicensesMultiDomain($order, $domainData);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fall back to legacy single domain format
|
||||
$this->generateLicensesSingleDomain($order);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate licenses for new multi-domain format
|
||||
*/
|
||||
private function generateLicensesMultiDomain(\WC_Order $order, array $domainData): void
|
||||
{
|
||||
$orderId = $order->get_id();
|
||||
$customerId = $order->get_customer_id();
|
||||
|
||||
// Index domains by product ID for quick lookup
|
||||
$domainsByProduct = [];
|
||||
foreach ($domainData as $item) {
|
||||
if (isset($item['product_id']) && isset($item['domains']) && is_array($item['domains'])) {
|
||||
$domainsByProduct[(int) $item['product_id']] = $item['domains'];
|
||||
}
|
||||
}
|
||||
|
||||
// Generate licenses for each licensed product
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
$domain = $order->get_meta('_licensed_product_domain');
|
||||
if ($domain) {
|
||||
if (!$product || !$product->is_type('licensed')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$productId = $product->get_id();
|
||||
$domains = $domainsByProduct[$productId] ?? [];
|
||||
|
||||
// Generate a license for each domain
|
||||
foreach ($domains as $domain) {
|
||||
if (!empty($domain)) {
|
||||
$this->licenseManager->generateLicense(
|
||||
$orderId,
|
||||
$product->get_id(),
|
||||
$order->get_customer_id(),
|
||||
$productId,
|
||||
$customerId,
|
||||
$domain
|
||||
);
|
||||
}
|
||||
@@ -224,6 +259,29 @@ final class Plugin
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate licenses for legacy single domain format
|
||||
*/
|
||||
private function generateLicensesSingleDomain(\WC_Order $order): void
|
||||
{
|
||||
$domain = $order->get_meta('_licensed_product_domain');
|
||||
if (empty($domain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($order->get_items() as $item) {
|
||||
$product = $item->get_product();
|
||||
if ($product && $product->is_type('licensed')) {
|
||||
$this->licenseManager->generateLicense(
|
||||
$order->get_id(),
|
||||
$product->get_id(),
|
||||
$order->get_customer_id(),
|
||||
$domain
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Twig environment
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user