You've already forked wc-licensed-product
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:
@@ -61,12 +61,6 @@ final class VersionAdminController
|
||||
*/
|
||||
public function renderVersionsMetaBox(\WP_Post $post): void
|
||||
{
|
||||
$product = wc_get_product($post->ID);
|
||||
if (!$product || !$product->is_type('licensed')) {
|
||||
echo '<p>' . esc_html__('This meta box is only available for Licensed Products.', 'wc-licensed-product') . '</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
$versions = $this->versionManager->getVersionsByProduct($post->ID);
|
||||
wp_nonce_field('wc_licensed_product_versions', 'wc_licensed_product_versions_nonce');
|
||||
?>
|
||||
@@ -82,10 +76,24 @@ final class VersionAdminController
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label for="new_download_url"><?php esc_html_e('Download URL', 'wc-licensed-product'); ?></label></th>
|
||||
<th><label for="new_attachment_id"><?php esc_html_e('Download File', 'wc-licensed-product'); ?></label></th>
|
||||
<td>
|
||||
<input type="hidden" id="new_attachment_id" name="new_attachment_id" value="" />
|
||||
<span id="selected_file_name" class="selected-file-name"></span>
|
||||
<button type="button" class="button" id="upload-version-file-btn">
|
||||
<?php esc_html_e('Select File', 'wc-licensed-product'); ?>
|
||||
</button>
|
||||
<button type="button" class="button" id="remove-version-file-btn" style="display: none;">
|
||||
<?php esc_html_e('Remove', 'wc-licensed-product'); ?>
|
||||
</button>
|
||||
<p class="description"><?php esc_html_e('Upload or select a file from the media library. Version will be auto-detected from filename (e.g., plugin-v1.2.3.zip).', 'wc-licensed-product'); ?></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label for="new_download_url"><?php esc_html_e('Or External URL', 'wc-licensed-product'); ?></label></th>
|
||||
<td>
|
||||
<input type="url" id="new_download_url" name="new_download_url" class="large-text" placeholder="https://" />
|
||||
<p class="description"><?php esc_html_e('URL to the downloadable file for this version.', 'wc-licensed-product'); ?></p>
|
||||
<p class="description"><?php esc_html_e('Alternative: Enter an external download URL instead of uploading a file.', 'wc-licensed-product'); ?></p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
@@ -110,7 +118,7 @@ final class VersionAdminController
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php esc_html_e('Version', 'wc-licensed-product'); ?></th>
|
||||
<th><?php esc_html_e('Download URL', 'wc-licensed-product'); ?></th>
|
||||
<th><?php esc_html_e('Download File', 'wc-licensed-product'); ?></th>
|
||||
<th><?php esc_html_e('Release Notes', 'wc-licensed-product'); ?></th>
|
||||
<th><?php esc_html_e('Status', 'wc-licensed-product'); ?></th>
|
||||
<th><?php esc_html_e('Released', 'wc-licensed-product'); ?></th>
|
||||
@@ -127,12 +135,19 @@ final class VersionAdminController
|
||||
<tr data-version-id="<?php echo esc_attr($version->getId()); ?>">
|
||||
<td><strong><?php echo esc_html($version->getVersion()); ?></strong></td>
|
||||
<td>
|
||||
<?php if ($version->getDownloadUrl()): ?>
|
||||
<a href="<?php echo esc_url($version->getDownloadUrl()); ?>" target="_blank">
|
||||
<?php echo esc_html(wp_basename($version->getDownloadUrl())); ?>
|
||||
<?php
|
||||
$effectiveUrl = $version->getEffectiveDownloadUrl();
|
||||
$filename = $version->getDownloadFilename();
|
||||
if ($effectiveUrl):
|
||||
?>
|
||||
<a href="<?php echo esc_url($effectiveUrl); ?>" target="_blank">
|
||||
<?php echo esc_html($filename ?: wp_basename($effectiveUrl)); ?>
|
||||
</a>
|
||||
<?php if ($version->getAttachmentId()): ?>
|
||||
<span class="dashicons dashicons-media-archive" title="<?php esc_attr_e('Uploaded file', 'wc-licensed-product'); ?>"></span>
|
||||
<?php endif; ?>
|
||||
<?php else: ?>
|
||||
<em><?php esc_html_e('No download URL', 'wc-licensed-product'); ?></em>
|
||||
<em><?php esc_html_e('No download file', 'wc-licensed-product'); ?></em>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo esc_html($version->getReleaseNotes() ? wp_trim_words($version->getReleaseNotes(), 10) : '—'); ?></td>
|
||||
@@ -173,6 +188,9 @@ final class VersionAdminController
|
||||
return;
|
||||
}
|
||||
|
||||
// Enqueue WordPress media uploader
|
||||
wp_enqueue_media();
|
||||
|
||||
wp_enqueue_script(
|
||||
'wc-licensed-product-versions',
|
||||
WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/js/versions.js',
|
||||
@@ -189,6 +207,8 @@ final class VersionAdminController
|
||||
'versionRequired' => __('Please enter a version number.', 'wc-licensed-product'),
|
||||
'versionInvalid' => __('Please enter a valid version number (e.g., 1.0.0).', 'wc-licensed-product'),
|
||||
'error' => __('An error occurred. Please try again.', 'wc-licensed-product'),
|
||||
'selectFile' => __('Select Download File', 'wc-licensed-product'),
|
||||
'useThisFile' => __('Use this file', 'wc-licensed-product'),
|
||||
],
|
||||
]);
|
||||
|
||||
@@ -215,6 +235,7 @@ final class VersionAdminController
|
||||
$version = sanitize_text_field($_POST['version'] ?? '');
|
||||
$downloadUrl = esc_url_raw($_POST['download_url'] ?? '');
|
||||
$releaseNotes = sanitize_textarea_field($_POST['release_notes'] ?? '');
|
||||
$attachmentId = absint($_POST['attachment_id'] ?? 0);
|
||||
|
||||
if (!$productId || !$version) {
|
||||
wp_send_json_error(['message' => __('Product ID and version are required.', 'wc-licensed-product')]);
|
||||
@@ -234,7 +255,8 @@ final class VersionAdminController
|
||||
$productId,
|
||||
$version,
|
||||
$releaseNotes ?: null,
|
||||
$downloadUrl ?: null
|
||||
$downloadUrl ?: null,
|
||||
$attachmentId ?: null
|
||||
);
|
||||
|
||||
if (!$newVersion) {
|
||||
@@ -317,12 +339,19 @@ final class VersionAdminController
|
||||
<tr data-version-id="<?php echo esc_attr($version->getId()); ?>">
|
||||
<td><strong><?php echo esc_html($version->getVersion()); ?></strong></td>
|
||||
<td>
|
||||
<?php if ($version->getDownloadUrl()): ?>
|
||||
<a href="<?php echo esc_url($version->getDownloadUrl()); ?>" target="_blank">
|
||||
<?php echo esc_html(wp_basename($version->getDownloadUrl())); ?>
|
||||
<?php
|
||||
$effectiveUrl = $version->getEffectiveDownloadUrl();
|
||||
$filename = $version->getDownloadFilename();
|
||||
if ($effectiveUrl):
|
||||
?>
|
||||
<a href="<?php echo esc_url($effectiveUrl); ?>" target="_blank">
|
||||
<?php echo esc_html($filename ?: wp_basename($effectiveUrl)); ?>
|
||||
</a>
|
||||
<?php if ($version->getAttachmentId()): ?>
|
||||
<span class="dashicons dashicons-media-archive" title="<?php esc_attr_e('Uploaded file', 'wc-licensed-product'); ?>"></span>
|
||||
<?php endif; ?>
|
||||
<?php else: ?>
|
||||
<em><?php esc_html_e('No download URL', 'wc-licensed-product'); ?></em>
|
||||
<em><?php esc_html_e('No download file', 'wc-licensed-product'); ?></em>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo esc_html($version->getReleaseNotes() ? wp_trim_words($version->getReleaseNotes(), 10) : '—'); ?></td>
|
||||
|
||||
@@ -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'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
237
src/Frontend/DownloadController.php
Normal file
237
src/Frontend/DownloadController.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,7 @@ final class Installer
|
||||
patch_version INT UNSIGNED NOT NULL,
|
||||
release_notes TEXT DEFAULT NULL,
|
||||
download_url VARCHAR(512) DEFAULT NULL,
|
||||
attachment_id BIGINT UNSIGNED DEFAULT NULL,
|
||||
is_active TINYINT(1) NOT NULL DEFAULT 1,
|
||||
released_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
@@ -15,6 +15,7 @@ use Jeremias\WcLicensedProduct\Api\RestApiController;
|
||||
use Jeremias\WcLicensedProduct\Checkout\CheckoutController;
|
||||
use Jeremias\WcLicensedProduct\Email\LicenseEmailController;
|
||||
use Jeremias\WcLicensedProduct\Frontend\AccountController;
|
||||
use Jeremias\WcLicensedProduct\Frontend\DownloadController;
|
||||
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
||||
use Jeremias\WcLicensedProduct\Product\LicensedProductType;
|
||||
use Jeremias\WcLicensedProduct\Product\VersionManager;
|
||||
@@ -46,6 +47,11 @@ final class Plugin
|
||||
*/
|
||||
private VersionManager $versionManager;
|
||||
|
||||
/**
|
||||
* Download controller
|
||||
*/
|
||||
private DownloadController $downloadController;
|
||||
|
||||
/**
|
||||
* Get singleton instance
|
||||
*/
|
||||
@@ -103,7 +109,8 @@ final class Plugin
|
||||
// Initialize controllers
|
||||
new LicensedProductType();
|
||||
new CheckoutController($this->licenseManager);
|
||||
new AccountController($this->twig, $this->licenseManager);
|
||||
$this->downloadController = new DownloadController($this->licenseManager, $this->versionManager);
|
||||
new AccountController($this->twig, $this->licenseManager, $this->versionManager, $this->downloadController);
|
||||
new RestApiController($this->licenseManager);
|
||||
new LicenseEmailController($this->licenseManager);
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ class ProductVersion
|
||||
private int $patchVersion;
|
||||
private ?string $releaseNotes;
|
||||
private ?string $downloadUrl;
|
||||
private ?int $attachmentId;
|
||||
private bool $isActive;
|
||||
private \DateTimeInterface $releasedAt;
|
||||
private \DateTimeInterface $createdAt;
|
||||
@@ -40,6 +41,7 @@ class ProductVersion
|
||||
$version->patchVersion = (int) $data['patch_version'];
|
||||
$version->releaseNotes = $data['release_notes'] ?: null;
|
||||
$version->downloadUrl = $data['download_url'] ?: null;
|
||||
$version->attachmentId = !empty($data['attachment_id']) ? (int) $data['attachment_id'] : null;
|
||||
$version->isActive = (bool) $data['is_active'];
|
||||
$version->releasedAt = new \DateTimeImmutable($data['released_at']);
|
||||
$version->createdAt = new \DateTimeImmutable($data['created_at']);
|
||||
@@ -60,6 +62,36 @@ class ProductVersion
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract version from filename
|
||||
*
|
||||
* Supports patterns like:
|
||||
* - product-name-v1.2.3.zip
|
||||
* - product-name-1.2.3.zip
|
||||
* - product_v1.2.3.zip
|
||||
* - v1.2.3.zip
|
||||
*/
|
||||
public static function extractVersionFromFilename(string $filename): ?string
|
||||
{
|
||||
// Remove extension
|
||||
$basename = pathinfo($filename, PATHINFO_FILENAME);
|
||||
|
||||
// Try various patterns
|
||||
$patterns = [
|
||||
'/[_-]v?(\d+\.\d+\.\d+)$/i', // ends with -v1.2.3 or -1.2.3
|
||||
'/[_-]v?(\d+\.\d+\.\d+)[_-]/i', // contains -v1.2.3- or -1.2.3-
|
||||
'/^v?(\d+\.\d+\.\d+)$/i', // just version number
|
||||
];
|
||||
|
||||
foreach ($patterns as $pattern) {
|
||||
if (preg_match($pattern, $basename, $matches)) {
|
||||
return $matches[1];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
@@ -100,6 +132,36 @@ class ProductVersion
|
||||
return $this->downloadUrl;
|
||||
}
|
||||
|
||||
public function getAttachmentId(): ?int
|
||||
{
|
||||
return $this->attachmentId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the effective download URL (from attachment or direct URL)
|
||||
*/
|
||||
public function getEffectiveDownloadUrl(): ?string
|
||||
{
|
||||
if ($this->attachmentId) {
|
||||
return wp_get_attachment_url($this->attachmentId) ?: null;
|
||||
}
|
||||
return $this->downloadUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filename for download
|
||||
*/
|
||||
public function getDownloadFilename(): ?string
|
||||
{
|
||||
if ($this->attachmentId) {
|
||||
return wp_basename(get_attached_file($this->attachmentId) ?: '');
|
||||
}
|
||||
if ($this->downloadUrl) {
|
||||
return wp_basename($this->downloadUrl);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->isActive;
|
||||
@@ -129,6 +191,7 @@ class ProductVersion
|
||||
'patch_version' => $this->patchVersion,
|
||||
'release_notes' => $this->releaseNotes,
|
||||
'download_url' => $this->downloadUrl,
|
||||
'attachment_id' => $this->attachmentId,
|
||||
'is_active' => $this->isActive,
|
||||
'released_at' => $this->releasedAt->format('Y-m-d H:i:s'),
|
||||
'created_at' => $this->createdAt->format('Y-m-d H:i:s'),
|
||||
|
||||
@@ -97,7 +97,8 @@ class VersionManager
|
||||
int $productId,
|
||||
string $version,
|
||||
?string $releaseNotes = null,
|
||||
?string $downloadUrl = null
|
||||
?string $downloadUrl = null,
|
||||
?int $attachmentId = null
|
||||
): ?ProductVersion {
|
||||
global $wpdb;
|
||||
|
||||
@@ -114,9 +115,10 @@ class VersionManager
|
||||
'patch_version' => $parsed['patch'],
|
||||
'release_notes' => $releaseNotes,
|
||||
'download_url' => $downloadUrl,
|
||||
'attachment_id' => $attachmentId,
|
||||
'is_active' => 1,
|
||||
],
|
||||
['%d', '%s', '%d', '%d', '%d', '%s', '%s', '%d']
|
||||
['%d', '%s', '%d', '%d', '%d', '%s', '%s', '%d', '%d']
|
||||
);
|
||||
|
||||
if ($result === false) {
|
||||
@@ -133,7 +135,8 @@ class VersionManager
|
||||
int $versionId,
|
||||
?string $releaseNotes = null,
|
||||
?string $downloadUrl = null,
|
||||
?bool $isActive = null
|
||||
?bool $isActive = null,
|
||||
?int $attachmentId = null
|
||||
): bool {
|
||||
global $wpdb;
|
||||
|
||||
@@ -155,6 +158,11 @@ class VersionManager
|
||||
$formats[] = '%d';
|
||||
}
|
||||
|
||||
if ($attachmentId !== null) {
|
||||
$data['attachment_id'] = $attachmentId > 0 ? $attachmentId : null;
|
||||
$formats[] = $attachmentId > 0 ? '%d' : null;
|
||||
}
|
||||
|
||||
if (empty($data)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user