2026-01-21 18:55:18 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Frontend Account Controller
|
|
|
|
|
*
|
|
|
|
|
* @package Jeremias\WcLicensedProduct\Frontend
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Jeremias\WcLicensedProduct\Frontend;
|
|
|
|
|
|
|
|
|
|
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
2026-01-21 19:46:50 +01:00
|
|
|
use Jeremias\WcLicensedProduct\Product\VersionManager;
|
2026-01-21 18:55:18 +01:00
|
|
|
use Twig\Environment;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles customer account pages for viewing licenses
|
|
|
|
|
*/
|
|
|
|
|
final class AccountController
|
|
|
|
|
{
|
|
|
|
|
private Environment $twig;
|
|
|
|
|
private LicenseManager $licenseManager;
|
2026-01-21 19:46:50 +01:00
|
|
|
private VersionManager $versionManager;
|
|
|
|
|
private DownloadController $downloadController;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
Environment $twig,
|
|
|
|
|
LicenseManager $licenseManager,
|
|
|
|
|
VersionManager $versionManager,
|
|
|
|
|
DownloadController $downloadController
|
|
|
|
|
) {
|
2026-01-21 18:55:18 +01:00
|
|
|
$this->twig = $twig;
|
|
|
|
|
$this->licenseManager = $licenseManager;
|
2026-01-21 19:46:50 +01:00
|
|
|
$this->versionManager = $versionManager;
|
|
|
|
|
$this->downloadController = $downloadController;
|
2026-01-21 18:55:18 +01:00
|
|
|
$this->registerHooks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register WordPress hooks
|
|
|
|
|
*/
|
|
|
|
|
private function registerHooks(): void
|
|
|
|
|
{
|
|
|
|
|
// Add licenses endpoint
|
|
|
|
|
add_action('init', [$this, 'addLicensesEndpoint']);
|
|
|
|
|
|
|
|
|
|
// Add licenses menu item
|
|
|
|
|
add_filter('woocommerce_account_menu_items', [$this, 'addLicensesMenuItem']);
|
|
|
|
|
|
|
|
|
|
// Add licenses endpoint content
|
|
|
|
|
add_action('woocommerce_account_licenses_endpoint', [$this, 'displayLicensesContent']);
|
|
|
|
|
|
2026-01-21 19:46:50 +01:00
|
|
|
// Enqueue frontend styles and scripts
|
|
|
|
|
add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']);
|
2026-01-21 18:55:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add licenses endpoint for My Account
|
|
|
|
|
*/
|
|
|
|
|
public function addLicensesEndpoint(): void
|
|
|
|
|
{
|
|
|
|
|
add_rewrite_endpoint('licenses', EP_ROOT | EP_PAGES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add licenses menu item to My Account navigation
|
|
|
|
|
*/
|
|
|
|
|
public function addLicensesMenuItem(array $items): array
|
|
|
|
|
{
|
|
|
|
|
// Insert licenses after orders
|
|
|
|
|
$newItems = [];
|
|
|
|
|
foreach ($items as $key => $value) {
|
|
|
|
|
$newItems[$key] = $value;
|
|
|
|
|
if ($key === 'orders') {
|
|
|
|
|
$newItems['licenses'] = __('Licenses', 'wc-licensed-product');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $newItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display licenses content in My Account
|
|
|
|
|
*/
|
|
|
|
|
public function displayLicensesContent(): void
|
|
|
|
|
{
|
|
|
|
|
$customerId = get_current_user_id();
|
|
|
|
|
if (!$customerId) {
|
|
|
|
|
echo '<p>' . esc_html__('Please log in to view your licenses.', 'wc-licensed-product') . '</p>';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$licenses = $this->licenseManager->getLicensesByCustomer($customerId);
|
|
|
|
|
|
2026-01-21 19:46:50 +01:00
|
|
|
// Enrich licenses with product data and downloads
|
2026-01-21 18:55:18 +01:00
|
|
|
$enrichedLicenses = [];
|
|
|
|
|
foreach ($licenses as $license) {
|
|
|
|
|
$product = wc_get_product($license->getProductId());
|
|
|
|
|
$order = wc_get_order($license->getOrderId());
|
|
|
|
|
|
2026-01-21 19:46:50 +01:00
|
|
|
// 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')),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 18:55:18 +01:00
|
|
|
$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() : '',
|
2026-01-21 19:46:50 +01:00
|
|
|
'downloads' => $downloads,
|
2026-01-21 18:55:18 +01:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
echo $this->twig->render('frontend/licenses.html.twig', [
|
|
|
|
|
'licenses' => $enrichedLicenses,
|
|
|
|
|
'has_licenses' => !empty($enrichedLicenses),
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
// Fallback to PHP template if Twig fails
|
|
|
|
|
$this->displayLicensesFallback($enrichedLicenses);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fallback display method if Twig is unavailable
|
|
|
|
|
*/
|
|
|
|
|
private function displayLicensesFallback(array $enrichedLicenses): void
|
|
|
|
|
{
|
|
|
|
|
if (empty($enrichedLicenses)) {
|
|
|
|
|
echo '<p>' . esc_html__('You have no licenses yet.', 'wc-licensed-product') . '</p>';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|
2026-01-21 19:46:50 +01:00
|
|
|
<div class="woocommerce-licenses">
|
|
|
|
|
<?php foreach ($enrichedLicenses as $item): ?>
|
|
|
|
|
<div class="license-card">
|
|
|
|
|
<div class="license-header">
|
|
|
|
|
<h3>
|
2026-01-21 18:55:18 +01:00
|
|
|
<?php if ($item['product_url']): ?>
|
|
|
|
|
<a href="<?php echo esc_url($item['product_url']); ?>">
|
|
|
|
|
<?php echo esc_html($item['product_name']); ?>
|
|
|
|
|
</a>
|
|
|
|
|
<?php else: ?>
|
|
|
|
|
<?php echo esc_html($item['product_name']); ?>
|
|
|
|
|
<?php endif; ?>
|
2026-01-21 19:46:50 +01:00
|
|
|
</h3>
|
|
|
|
|
<span class="license-status license-status-<?php echo esc_attr($item['license']->getStatus()); ?>">
|
|
|
|
|
<?php echo esc_html(ucfirst($item['license']->getStatus())); ?>
|
|
|
|
|
</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><strong><?php esc_html_e('Domain:', 'wc-licensed-product'); ?></strong> <?php echo esc_html($item['license']->getDomain()); ?></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');
|
|
|
|
|
?>
|
2026-01-21 18:55:18 +01:00
|
|
|
</span>
|
2026-01-21 19:46:50 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<?php if (!empty($item['downloads'])): ?>
|
|
|
|
|
<div class="license-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">
|
|
|
|
|
<span class="dashicons dashicons-download"></span>
|
|
|
|
|
<?php echo esc_html($download['filename'] ?: sprintf(__('Version %s', 'wc-licensed-product'), $download['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; ?>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
<?php endif; ?>
|
|
|
|
|
</div>
|
|
|
|
|
<?php endforeach; ?>
|
|
|
|
|
</div>
|
2026-01-21 18:55:18 +01:00
|
|
|
<?php
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 19:46:50 +01:00
|
|
|
* Enqueue frontend styles and scripts
|
2026-01-21 18:55:18 +01:00
|
|
|
*/
|
2026-01-21 19:46:50 +01:00
|
|
|
public function enqueueAssets(): void
|
2026-01-21 18:55:18 +01:00
|
|
|
{
|
|
|
|
|
if (!is_account_page()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wp_enqueue_style(
|
|
|
|
|
'wc-licensed-product-frontend',
|
|
|
|
|
WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/css/frontend.css',
|
|
|
|
|
[],
|
|
|
|
|
WC_LICENSED_PRODUCT_VERSION
|
|
|
|
|
);
|
2026-01-21 19:46:50 +01:00
|
|
|
|
|
|
|
|
wp_enqueue_script(
|
|
|
|
|
'wc-licensed-product-frontend',
|
|
|
|
|
WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/js/frontend.js',
|
|
|
|
|
['jquery'],
|
|
|
|
|
WC_LICENSED_PRODUCT_VERSION,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
wp_localize_script('wc-licensed-product-frontend', 'wcLicensedProduct', [
|
|
|
|
|
'strings' => [
|
|
|
|
|
'copied' => __('Copied!', 'wc-licensed-product'),
|
|
|
|
|
'copyFailed' => __('Copy failed', 'wc-licensed-product'),
|
|
|
|
|
],
|
|
|
|
|
]);
|
2026-01-21 18:55:18 +01:00
|
|
|
}
|
|
|
|
|
}
|