Implement version 0.0.3 features

- Add file attachment support for product versions (Media Library)
- Add version auto-detection from uploaded filenames
- Implement secure customer downloads with hash verification
- Add license key copy-to-clipboard functionality
- Redesign customer licenses page with card-based UI
- Fix product versions meta box visibility for non-licensed types
- Add DownloadController for secure file delivery
- Update CLAUDE.md roadmap and session history

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-21 19:46:50 +01:00
parent 41e5f8d467
commit 78e43b9aea
15 changed files with 1036 additions and 133 deletions

View File

@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Jeremias\WcLicensedProduct\Frontend;
use Jeremias\WcLicensedProduct\License\LicenseManager;
use Jeremias\WcLicensedProduct\Product\VersionManager;
use Twig\Environment;
/**
@@ -19,11 +20,19 @@ final class AccountController
{
private Environment $twig;
private LicenseManager $licenseManager;
private VersionManager $versionManager;
private DownloadController $downloadController;
public function __construct(Environment $twig, LicenseManager $licenseManager)
{
public function __construct(
Environment $twig,
LicenseManager $licenseManager,
VersionManager $versionManager,
DownloadController $downloadController
) {
$this->twig = $twig;
$this->licenseManager = $licenseManager;
$this->versionManager = $versionManager;
$this->downloadController = $downloadController;
$this->registerHooks();
}
@@ -41,8 +50,8 @@ final class AccountController
// Add licenses endpoint content
add_action('woocommerce_account_licenses_endpoint', [$this, 'displayLicensesContent']);
// Enqueue frontend styles
add_action('wp_enqueue_scripts', [$this, 'enqueueStyles']);
// Enqueue frontend styles and scripts
add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']);
}
/**
@@ -83,18 +92,40 @@ final class AccountController
$licenses = $this->licenseManager->getLicensesByCustomer($customerId);
// Enrich licenses with product data
// 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')),
];
}
}
}
$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,
];
}
@@ -120,23 +151,11 @@ final class AccountController
}
?>
<table class="woocommerce-licenses-table shop_table shop_table_responsive">
<thead>
<tr>
<th><?php esc_html_e('License Key', 'wc-licensed-product'); ?></th>
<th><?php esc_html_e('Product', 'wc-licensed-product'); ?></th>
<th><?php esc_html_e('Domain', 'wc-licensed-product'); ?></th>
<th><?php esc_html_e('Status', 'wc-licensed-product'); ?></th>
<th><?php esc_html_e('Expires', 'wc-licensed-product'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($enrichedLicenses as $item): ?>
<tr>
<td data-title="<?php esc_attr_e('License Key', 'wc-licensed-product'); ?>">
<code><?php echo esc_html($item['license']->getLicenseKey()); ?></code>
</td>
<td data-title="<?php esc_attr_e('Product', 'wc-licensed-product'); ?>">
<div class="woocommerce-licenses">
<?php foreach ($enrichedLicenses as $item): ?>
<div class="license-card">
<div class="license-header">
<h3>
<?php if ($item['product_url']): ?>
<a href="<?php echo esc_url($item['product_url']); ?>">
<?php echo esc_html($item['product_name']); ?>
@@ -144,34 +163,63 @@ final class AccountController
<?php else: ?>
<?php echo esc_html($item['product_name']); ?>
<?php endif; ?>
</td>
<td data-title="<?php esc_attr_e('Domain', 'wc-licensed-product'); ?>">
<?php echo esc_html($item['license']->getDomain()); ?>
</td>
<td data-title="<?php esc_attr_e('Status', 'wc-licensed-product'); ?>">
<span class="license-status license-status-<?php echo esc_attr($item['license']->getStatus()); ?>">
<?php echo esc_html(ucfirst($item['license']->getStatus())); ?>
</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');
?>
</span>
</td>
<td data-title="<?php esc_attr_e('Expires', 'wc-licensed-product'); ?>">
<?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>
</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>
<?php
}
/**
* Enqueue frontend styles
* Enqueue frontend styles and scripts
*/
public function enqueueStyles(): void
public function enqueueAssets(): void
{
if (!is_account_page()) {
return;
@@ -183,5 +231,20 @@ final class AccountController
[],
WC_LICENSED_PRODUCT_VERSION
);
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'),
],
]);
}
}

View File

@@ -0,0 +1,237 @@
<?php
/**
* Download Controller
*
* @package Jeremias\WcLicensedProduct\Frontend
*/
declare(strict_types=1);
namespace Jeremias\WcLicensedProduct\Frontend;
use Jeremias\WcLicensedProduct\License\LicenseManager;
use Jeremias\WcLicensedProduct\Product\VersionManager;
/**
* Handles secure file downloads for licensed customers
*/
final class DownloadController
{
private LicenseManager $licenseManager;
private VersionManager $versionManager;
public function __construct(LicenseManager $licenseManager, VersionManager $versionManager)
{
$this->licenseManager = $licenseManager;
$this->versionManager = $versionManager;
$this->registerHooks();
}
/**
* Register WordPress hooks
*/
private function registerHooks(): void
{
// Add download endpoint
add_action('init', [$this, 'addDownloadEndpoint']);
// Handle download requests
add_action('template_redirect', [$this, 'handleDownloadRequest']);
}
/**
* Add download endpoint
*/
public function addDownloadEndpoint(): void
{
add_rewrite_endpoint('license-download', EP_ROOT | EP_PAGES);
}
/**
* Handle download request
*/
public function handleDownloadRequest(): void
{
global $wp_query;
if (!isset($wp_query->query_vars['license-download'])) {
return;
}
$downloadKey = sanitize_text_field($wp_query->query_vars['license-download']);
if (empty($downloadKey)) {
wp_die(
__('Invalid download link.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 403]
);
}
// Parse download key: format is "license_id-version_id-hash"
$parts = explode('-', $downloadKey);
if (count($parts) < 3) {
wp_die(
__('Invalid download link format.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 403]
);
}
$licenseId = absint($parts[0]);
$versionId = absint($parts[1]);
$hash = $parts[2];
// Verify hash
$expectedHash = $this->generateDownloadHash($licenseId, $versionId);
if (!hash_equals($expectedHash, $hash)) {
wp_die(
__('Invalid download link.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 403]
);
}
// Check user authentication
if (!is_user_logged_in()) {
wp_redirect(wp_login_url(home_url('license-download/' . $downloadKey)));
exit;
}
// Get license
$license = $this->licenseManager->getLicenseById($licenseId);
if (!$license) {
wp_die(
__('License not found.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 404]
);
}
// Verify user owns the license
$currentUserId = get_current_user_id();
if ($license->getCustomerId() !== $currentUserId) {
wp_die(
__('You do not have permission to download this file.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 403]
);
}
// Check license status
if ($license->getStatus() !== 'active') {
wp_die(
__('Your license is not active. Please contact support.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 403]
);
}
// Get version
$version = $this->versionManager->getVersionById($versionId);
if (!$version) {
wp_die(
__('Version not found.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 404]
);
}
// Verify version belongs to licensed product
if ($version->getProductId() !== $license->getProductId()) {
wp_die(
__('Version does not match your licensed product.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 403]
);
}
// Check if version is active
if (!$version->isActive()) {
wp_die(
__('This version is no longer available for download.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 403]
);
}
// Get download file
$attachmentId = $version->getAttachmentId();
$downloadUrl = $version->getDownloadUrl();
if ($attachmentId) {
$this->serveAttachment($attachmentId, $version->getVersion());
} elseif ($downloadUrl) {
// Redirect to external URL
wp_redirect($downloadUrl);
exit;
} else {
wp_die(
__('No download file available for this version.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 404]
);
}
}
/**
* Serve attachment file for download
*/
private function serveAttachment(int $attachmentId, string $version): void
{
$filePath = get_attached_file($attachmentId);
if (!$filePath || !file_exists($filePath)) {
wp_die(
__('Download file not found.', 'wc-licensed-product'),
__('Download Error', 'wc-licensed-product'),
['response' => 404]
);
}
$filename = wp_basename($filePath);
$mimeType = mime_content_type($filePath) ?: 'application/octet-stream';
$fileSize = filesize($filePath);
// Prevent caching
nocache_headers();
// Set headers for download
header('Content-Type: ' . $mimeType);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . $fileSize);
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
// Clear output buffer
if (ob_get_level()) {
ob_end_clean();
}
// Read file and output
readfile($filePath);
exit;
}
/**
* Generate download hash for security
*/
public function generateDownloadHash(int $licenseId, int $versionId): string
{
$data = $licenseId . '-' . $versionId . '-' . wp_salt('auth');
return substr(hash('sha256', $data), 0, 16);
}
/**
* Generate download URL for a license and version
*/
public function generateDownloadUrl(int $licenseId, int $versionId): string
{
$hash = $this->generateDownloadHash($licenseId, $versionId);
$downloadKey = $licenseId . '-' . $versionId . '-' . $hash;
return home_url('license-download/' . $downloadKey);
}
}