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:
27
CHANGELOG.md
27
CHANGELOG.md
@@ -7,6 +7,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [0.0.3] - 2026-01-21
|
||||
|
||||
### Added
|
||||
|
||||
- File attachment support for product versions (WordPress Media Library integration)
|
||||
- Version auto-detection from uploaded filenames (e.g., `plugin-v1.2.3.zip`)
|
||||
- Customer download page for purchased licenses with secure authenticated downloads
|
||||
- License key copy-to-clipboard functionality on account page and emails
|
||||
- New card-based UI for customer licenses page with download section
|
||||
- DownloadController for secure file delivery with hash-based URL verification
|
||||
|
||||
### Changed
|
||||
|
||||
- Product versions meta box now hidden for non-licensed product types (dynamic show/hide)
|
||||
- Redesigned customer licenses page with modern card layout
|
||||
- Frontend JavaScript and CSS enhancements for better UX
|
||||
|
||||
### Technical Details
|
||||
|
||||
- New class: DownloadController for secure file downloads
|
||||
- Database schema updated: `attachment_id` column added to versions table
|
||||
- ProductVersion model extended with `getEffectiveDownloadUrl()` and `getDownloadFilename()`
|
||||
- Secure download URLs use hash verification (license_id-version_id-hash format)
|
||||
|
||||
## [0.0.2] - 2026-01-21
|
||||
|
||||
### Added
|
||||
@@ -68,6 +92,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- WordPress REST API integration
|
||||
- Custom WooCommerce product type extending WC_Product
|
||||
|
||||
[Unreleased]: https://src.bundespruefstelle.ch/magdev/wc-licensed-product/compare/v0.0.2...HEAD
|
||||
[Unreleased]: https://src.bundespruefstelle.ch/magdev/wc-licensed-product/compare/v0.0.3...HEAD
|
||||
[0.0.3]: https://src.bundespruefstelle.ch/magdev/wc-licensed-product/compare/v0.0.2...v0.0.3
|
||||
[0.0.2]: https://src.bundespruefstelle.ch/magdev/wc-licensed-product/compare/v0.0.1...v0.0.2
|
||||
[0.0.1]: https://src.bundespruefstelle.ch/magdev/wc-licensed-product/releases/tag/v0.0.1
|
||||
|
||||
35
CLAUDE.md
35
CLAUDE.md
@@ -36,12 +36,11 @@ This project is proudly **"vibe-coded"** using Claude.AI - the entire codebase w
|
||||
|
||||
_No known bugs at this time._
|
||||
|
||||
### Version 0.0.3 (Next)
|
||||
### Version 0.0.4 (Next)
|
||||
|
||||
- Add download file attachment support for product versions
|
||||
- Implement customer download page for purchased licenses
|
||||
- Add license key copy-to-clipboard functionality in emails and account page
|
||||
- Consider adding license usage statistics/analytics
|
||||
- Consider adding bulk license operations in admin
|
||||
- Consider adding license renewal/extension functionality
|
||||
|
||||
## Technical Stack
|
||||
|
||||
@@ -289,3 +288,31 @@ Base: `/wp-json/wc-licensed-product/v1/`
|
||||
- Rate limiting uses WordPress transients
|
||||
- IP detection supports Cloudflare and proxies
|
||||
- Version management uses AJAX for smooth UX
|
||||
|
||||
### 2026-01-21 - Version 0.0.3 Features
|
||||
|
||||
**Implemented:**
|
||||
|
||||
- File attachment support for product versions (WordPress Media Library integration)
|
||||
- Version auto-detection from uploaded filenames (e.g., `plugin-v1.2.3.zip`)
|
||||
- Customer download page for purchased licenses with secure authenticated downloads
|
||||
- License key copy-to-clipboard functionality on account page
|
||||
- New card-based UI for customer licenses page with download section
|
||||
- Product versions meta box visibility fix (now hidden for non-licensed product types)
|
||||
|
||||
**New classes:**
|
||||
|
||||
- `DownloadController` - Secure file delivery with hash-based URL verification
|
||||
|
||||
**Technical notes:**
|
||||
|
||||
- Secure download URLs use hash verification (license_id-version_id-hash format)
|
||||
- Database schema updated: `attachment_id` column added to versions table
|
||||
- ProductVersion model extended with `getEffectiveDownloadUrl()` and `getDownloadFilename()`
|
||||
- Media uploader filters for zip files only
|
||||
- Clipboard API with fallback for older browsers
|
||||
- Card-based responsive UI design for licenses page
|
||||
|
||||
**Bug fixes:**
|
||||
|
||||
- Fixed product versions meta box visibility for non-licensed product types (targets `#wc_licensed_product_versions` container)
|
||||
|
||||
@@ -145,3 +145,23 @@
|
||||
font-style: italic;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
/* File Upload UI */
|
||||
.selected-file-name {
|
||||
display: inline-block;
|
||||
margin-right: 10px;
|
||||
padding: 5px 10px;
|
||||
background: #e7f3ff;
|
||||
border-radius: 3px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.selected-file-name:empty {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#versions-table .dashicons-media-archive {
|
||||
color: #2271b1;
|
||||
vertical-align: middle;
|
||||
margin-left: 5px;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,206 @@
|
||||
color: #383d41;
|
||||
}
|
||||
|
||||
/* License Table */
|
||||
/* License Cards */
|
||||
.woocommerce-licenses {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.5em;
|
||||
}
|
||||
|
||||
.license-card {
|
||||
border: 1px solid #e5e5e5;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.license-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 1em 1.5em;
|
||||
background: #f8f9fa;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
.license-header h3 {
|
||||
margin: 0;
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
.license-header h3 a {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.license-header h3 a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.license-details {
|
||||
padding: 1.5em;
|
||||
}
|
||||
|
||||
.license-key-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75em;
|
||||
margin-bottom: 1em;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.license-key-row label {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.license-key-row code {
|
||||
font-family: 'SF Mono', Monaco, Consolas, monospace;
|
||||
background-color: #f5f5f5;
|
||||
padding: 0.4em 0.8em;
|
||||
border-radius: 4px;
|
||||
font-size: 1em;
|
||||
letter-spacing: 0.05em;
|
||||
}
|
||||
|
||||
.copy-license-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
padding: 0;
|
||||
background: #f0f0f0;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.copy-license-btn:hover {
|
||||
background: #e5e5e5;
|
||||
border-color: #ccc;
|
||||
}
|
||||
|
||||
.copy-license-btn .dashicons {
|
||||
font-size: 18px;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
}
|
||||
|
||||
.copy-feedback {
|
||||
display: inline-block;
|
||||
margin-left: 0.5em;
|
||||
padding: 0.25em 0.5em;
|
||||
font-size: 0.85em;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.copy-feedback.success {
|
||||
background: #d4edda;
|
||||
color: #155724;
|
||||
}
|
||||
|
||||
.copy-feedback.error {
|
||||
background: #f8d7da;
|
||||
color: #721c24;
|
||||
}
|
||||
|
||||
.license-info-row {
|
||||
display: flex;
|
||||
gap: 2em;
|
||||
color: #666;
|
||||
font-size: 0.95em;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
/* Download Section */
|
||||
.license-downloads {
|
||||
padding: 1em 1.5em;
|
||||
background: #f8f9fa;
|
||||
border-top: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
.license-downloads h4 {
|
||||
margin: 0 0 0.75em 0;
|
||||
font-size: 0.95em;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.download-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.download-list li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1em;
|
||||
padding: 0.5em 0;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
|
||||
.download-list li:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.download-link {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.5em;
|
||||
color: #2271b1;
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.download-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.download-link .dashicons {
|
||||
font-size: 16px;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
|
||||
.download-version {
|
||||
background: #e7f3ff;
|
||||
padding: 0.2em 0.5em;
|
||||
border-radius: 3px;
|
||||
font-size: 0.85em;
|
||||
color: #2271b1;
|
||||
}
|
||||
|
||||
.download-date {
|
||||
color: #999;
|
||||
font-size: 0.85em;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
/* Domain Field */
|
||||
#licensed-product-domain-field {
|
||||
margin-top: 2em;
|
||||
padding: 1.5em;
|
||||
background-color: #f8f9fa;
|
||||
border: 1px solid #e5e5e5;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
#licensed-product-domain-field h3 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1em;
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
#licensed-product-domain-field .description {
|
||||
display: block;
|
||||
margin-top: 0.5em;
|
||||
font-size: 0.9em;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
/* Legacy License Table (kept for backwards compatibility) */
|
||||
.woocommerce-licenses-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
@@ -64,30 +263,33 @@
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
/* Domain Field */
|
||||
#licensed-product-domain-field {
|
||||
margin-top: 2em;
|
||||
padding: 1.5em;
|
||||
background-color: #f8f9fa;
|
||||
border: 1px solid #e5e5e5;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
#licensed-product-domain-field h3 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 1em;
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
#licensed-product-domain-field .description {
|
||||
display: block;
|
||||
margin-top: 0.5em;
|
||||
font-size: 0.9em;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
/* Responsive */
|
||||
@media screen and (max-width: 768px) {
|
||||
.license-header {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 0.5em;
|
||||
}
|
||||
|
||||
.license-key-row {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.license-info-row {
|
||||
flex-direction: column;
|
||||
gap: 0.5em;
|
||||
}
|
||||
|
||||
.download-list li {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.download-date {
|
||||
margin-left: 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.woocommerce-licenses-table,
|
||||
.woocommerce-licenses-table thead,
|
||||
.woocommerce-licenses-table tbody,
|
||||
|
||||
90
assets/js/frontend.js
Normal file
90
assets/js/frontend.js
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* WC Licensed Product - Frontend Scripts
|
||||
*
|
||||
* @package Jeremias\WcLicensedProduct
|
||||
*/
|
||||
|
||||
(function($) {
|
||||
'use strict';
|
||||
|
||||
var WCLicensedProductFrontend = {
|
||||
init: function() {
|
||||
this.bindEvents();
|
||||
},
|
||||
|
||||
bindEvents: function() {
|
||||
$(document).on('click', '.copy-license-btn', this.copyLicenseKey);
|
||||
},
|
||||
|
||||
/**
|
||||
* Copy license key to clipboard
|
||||
*/
|
||||
copyLicenseKey: function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
var $btn = $(this);
|
||||
var licenseKey = $btn.data('license-key');
|
||||
|
||||
if (!licenseKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Use modern clipboard API if available
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
navigator.clipboard.writeText(licenseKey)
|
||||
.then(function() {
|
||||
WCLicensedProductFrontend.showCopyFeedback($btn, true);
|
||||
})
|
||||
.catch(function() {
|
||||
WCLicensedProductFrontend.fallbackCopy(licenseKey, $btn);
|
||||
});
|
||||
} else {
|
||||
WCLicensedProductFrontend.fallbackCopy(licenseKey, $btn);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Fallback copy method for older browsers
|
||||
*/
|
||||
fallbackCopy: function(text, $btn) {
|
||||
var $temp = $('<textarea>');
|
||||
$('body').append($temp);
|
||||
$temp.val(text).select();
|
||||
|
||||
try {
|
||||
var success = document.execCommand('copy');
|
||||
WCLicensedProductFrontend.showCopyFeedback($btn, success);
|
||||
} catch (err) {
|
||||
WCLicensedProductFrontend.showCopyFeedback($btn, false);
|
||||
}
|
||||
|
||||
$temp.remove();
|
||||
},
|
||||
|
||||
/**
|
||||
* Show feedback after copy attempt
|
||||
*/
|
||||
showCopyFeedback: function($btn, success) {
|
||||
var message = success
|
||||
? wcLicensedProduct.strings.copied
|
||||
: wcLicensedProduct.strings.copyFailed;
|
||||
|
||||
var $feedback = $('<span class="copy-feedback"></span>')
|
||||
.text(message)
|
||||
.addClass(success ? 'success' : 'error');
|
||||
|
||||
$btn.after($feedback);
|
||||
|
||||
setTimeout(function() {
|
||||
$feedback.fadeOut(300, function() {
|
||||
$(this).remove();
|
||||
});
|
||||
}, 1500);
|
||||
}
|
||||
};
|
||||
|
||||
$(document).ready(function() {
|
||||
WCLicensedProductFrontend.init();
|
||||
});
|
||||
|
||||
})(jQuery);
|
||||
@@ -8,6 +8,8 @@
|
||||
'use strict';
|
||||
|
||||
var WCLicensedProductVersions = {
|
||||
mediaFrame: null,
|
||||
|
||||
init: function() {
|
||||
this.bindEvents();
|
||||
},
|
||||
@@ -16,6 +18,113 @@
|
||||
$('#add-version-btn').on('click', this.addVersion);
|
||||
$(document).on('click', '.delete-version-btn', this.deleteVersion);
|
||||
$(document).on('click', '.toggle-version-btn', this.toggleVersion);
|
||||
|
||||
// Media uploader events
|
||||
$('#upload-version-file-btn').on('click', this.openMediaUploader.bind(this));
|
||||
$('#remove-version-file-btn').on('click', this.removeSelectedFile);
|
||||
|
||||
// Listen for product type changes
|
||||
$('#product-type').on('change', this.onProductTypeChange);
|
||||
|
||||
// Initial check for product type (handles page load for new products)
|
||||
this.onProductTypeChange();
|
||||
},
|
||||
|
||||
onProductTypeChange: function() {
|
||||
var productType = $('#product-type').val();
|
||||
var $metaBox = $('#wc_licensed_product_versions');
|
||||
|
||||
if (productType === 'licensed') {
|
||||
$metaBox.show();
|
||||
} else {
|
||||
$metaBox.hide();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Open WordPress media uploader
|
||||
*/
|
||||
openMediaUploader: function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
var self = this;
|
||||
|
||||
// If frame already exists, open it
|
||||
if (this.mediaFrame) {
|
||||
this.mediaFrame.open();
|
||||
return;
|
||||
}
|
||||
|
||||
// Create media frame
|
||||
this.mediaFrame = wp.media({
|
||||
title: wcLicensedProductVersions.strings.selectFile,
|
||||
button: {
|
||||
text: wcLicensedProductVersions.strings.useThisFile
|
||||
},
|
||||
multiple: false,
|
||||
library: {
|
||||
type: ['application/zip', 'application/x-zip-compressed', 'application/octet-stream']
|
||||
}
|
||||
});
|
||||
|
||||
// When file is selected
|
||||
this.mediaFrame.on('select', function() {
|
||||
var attachment = self.mediaFrame.state().get('selection').first().toJSON();
|
||||
|
||||
// Set attachment ID
|
||||
$('#new_attachment_id').val(attachment.id);
|
||||
|
||||
// Show filename
|
||||
$('#selected_file_name').text(attachment.filename);
|
||||
$('#remove-version-file-btn').show();
|
||||
|
||||
// Try to extract version from filename
|
||||
var extractedVersion = self.extractVersionFromFilename(attachment.filename);
|
||||
if (extractedVersion && !$('#new_version').val().trim()) {
|
||||
$('#new_version').val(extractedVersion);
|
||||
}
|
||||
|
||||
// Clear external URL when file is selected
|
||||
$('#new_download_url').val('');
|
||||
});
|
||||
|
||||
this.mediaFrame.open();
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove selected file
|
||||
*/
|
||||
removeSelectedFile: function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
$('#new_attachment_id').val('');
|
||||
$('#selected_file_name').text('');
|
||||
$('#remove-version-file-btn').hide();
|
||||
},
|
||||
|
||||
/**
|
||||
* Extract version from filename
|
||||
* Supports patterns like: plugin-v1.2.3.zip, plugin-1.2.3.zip, v1.2.3.zip
|
||||
*/
|
||||
extractVersionFromFilename: function(filename) {
|
||||
// Remove extension
|
||||
var basename = filename.replace(/\.[^/.]+$/, '');
|
||||
|
||||
// Try various patterns
|
||||
var 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
|
||||
];
|
||||
|
||||
for (var i = 0; i < patterns.length; i++) {
|
||||
var match = basename.match(patterns[i]);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
addVersion: function(e) {
|
||||
@@ -27,6 +136,7 @@
|
||||
var version = $('#new_version').val().trim();
|
||||
var downloadUrl = $('#new_download_url').val().trim();
|
||||
var releaseNotes = $('#new_release_notes').val().trim();
|
||||
var attachmentId = $('#new_attachment_id').val();
|
||||
|
||||
// Validate version
|
||||
if (!version) {
|
||||
@@ -51,7 +161,8 @@
|
||||
product_id: productId,
|
||||
version: version,
|
||||
download_url: downloadUrl,
|
||||
release_notes: releaseNotes
|
||||
release_notes: releaseNotes,
|
||||
attachment_id: attachmentId
|
||||
},
|
||||
success: function(response) {
|
||||
if (response.success) {
|
||||
@@ -65,6 +176,9 @@
|
||||
$('#new_version').val('');
|
||||
$('#new_download_url').val('');
|
||||
$('#new_release_notes').val('');
|
||||
$('#new_attachment_id').val('');
|
||||
$('#selected_file_name').text('');
|
||||
$('#remove-version-file-btn').hide();
|
||||
} else {
|
||||
alert(response.data.message || wcLicensedProductVersions.strings.error);
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
<div class="woocommerce-licenses">
|
||||
<?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="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'); ?>">
|
||||
</h3>
|
||||
<span class="license-status license-status-<?php echo esc_attr($item['license']->getStatus()); ?>">
|
||||
<?php echo esc_html(ucfirst($item['license']->getStatus())); ?>
|
||||
</span>
|
||||
</td>
|
||||
<td data-title="<?php esc_attr_e('Expires', 'wc-licensed-product'); ?>">
|
||||
</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');
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
</span>
|
||||
</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; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</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;
|
||||
}
|
||||
|
||||
@@ -1,46 +1,63 @@
|
||||
{% if not has_licenses %}
|
||||
<p>{{ __('You have no licenses yet.') }}</p>
|
||||
{% else %}
|
||||
<table class="woocommerce-licenses-table shop_table shop_table_responsive">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ __('License Key') }}</th>
|
||||
<th>{{ __('Product') }}</th>
|
||||
<th>{{ __('Domain') }}</th>
|
||||
<th>{{ __('Status') }}</th>
|
||||
<th>{{ __('Expires') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<div class="woocommerce-licenses">
|
||||
{% for item in licenses %}
|
||||
<tr>
|
||||
<td data-title="{{ __('License Key') }}">
|
||||
<code>{{ item.license.licenseKey }}</code>
|
||||
</td>
|
||||
<td data-title="{{ __('Product') }}">
|
||||
<div class="license-card">
|
||||
<div class="license-header">
|
||||
<h3>
|
||||
{% if item.product_url %}
|
||||
<a href="{{ esc_url(item.product_url) }}">{{ esc_html(item.product_name) }}</a>
|
||||
{% else %}
|
||||
{{ esc_html(item.product_name) }}
|
||||
{% endif %}
|
||||
</td>
|
||||
<td data-title="{{ __('Domain') }}">
|
||||
{{ esc_html(item.license.domain) }}
|
||||
</td>
|
||||
<td data-title="{{ __('Status') }}">
|
||||
</h3>
|
||||
<span class="license-status license-status-{{ item.license.status }}">
|
||||
{{ item.license.status|capitalize }}
|
||||
</span>
|
||||
</td>
|
||||
<td data-title="{{ __('Expires') }}">
|
||||
</div>
|
||||
|
||||
<div class="license-details">
|
||||
<div class="license-key-row">
|
||||
<label>{{ __('License Key:') }}</label>
|
||||
<code class="license-key" data-license-key="{{ esc_attr(item.license.licenseKey) }}">
|
||||
{{ esc_html(item.license.licenseKey) }}
|
||||
</code>
|
||||
<button type="button" class="copy-license-btn" data-license-key="{{ esc_attr(item.license.licenseKey) }}" title="{{ __('Copy to clipboard') }}">
|
||||
<span class="dashicons dashicons-clipboard"></span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="license-info-row">
|
||||
<span><strong>{{ __('Domain:') }}</strong> {{ esc_html(item.license.domain) }}</span>
|
||||
<span><strong>{{ __('Expires:') }}</strong>
|
||||
{% if item.license.expiresAt %}
|
||||
{{ item.license.expiresAt|date('Y-m-d') }}
|
||||
{% else %}
|
||||
{{ __('Never') }}
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if item.downloads is defined and item.downloads is not empty %}
|
||||
<div class="license-downloads">
|
||||
<h4>{{ __('Available Downloads') }}</h4>
|
||||
<ul class="download-list">
|
||||
{% for download in item.downloads %}
|
||||
<li>
|
||||
<a href="{{ esc_url(download.download_url) }}" class="download-link">
|
||||
<span class="dashicons dashicons-download"></span>
|
||||
{{ esc_html(download.filename ?: 'Version ' ~ download.version) }}
|
||||
</a>
|
||||
<span class="download-version">v{{ esc_html(download.version) }}</span>
|
||||
<span class="download-date">{{ esc_html(download.released_at) }}</span>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* Plugin Name: WooCommerce Licensed Product
|
||||
* Plugin URI: https://src.bundespruefstelle.ch/magdev/wc-licensed-product
|
||||
* Description: WooCommerce plugin to sell software products using license keys with domain-based validation.
|
||||
* Version: 0.0.2
|
||||
* Version: 0.0.3
|
||||
* Author: Marco Graetsch
|
||||
* Author URI: https://src.bundespruefstelle.ch/magdev
|
||||
* License: GPL-2.0-or-later
|
||||
@@ -28,7 +28,7 @@ if (!defined('ABSPATH')) {
|
||||
}
|
||||
|
||||
// Plugin constants
|
||||
define('WC_LICENSED_PRODUCT_VERSION', '0.0.2');
|
||||
define('WC_LICENSED_PRODUCT_VERSION', '0.0.3');
|
||||
define('WC_LICENSED_PRODUCT_PLUGIN_FILE', __FILE__);
|
||||
define('WC_LICENSED_PRODUCT_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
||||
define('WC_LICENSED_PRODUCT_PLUGIN_URL', plugin_dir_url(__FILE__));
|
||||
|
||||
Reference in New Issue
Block a user