You've already forked wc-licensed-product
Implement version 0.0.1 - Licensed Product type for WooCommerce
Add complete plugin infrastructure for selling software with license keys: - New "Licensed Product" WooCommerce product type - License key generation (XXXX-XXXX-XXXX-XXXX format) on order completion - Domain-based license validation system - REST API endpoints (validate, status, activate, deactivate) - Customer My Account "Licenses" page - Admin license management under WooCommerce > Licenses - Checkout domain field for licensed products - Custom database tables for licenses and product versions - Twig template engine integration - Full i18n support with German (de_CH) translation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
187
src/Frontend/AccountController.php
Normal file
187
src/Frontend/AccountController.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
/**
|
||||
* Frontend Account Controller
|
||||
*
|
||||
* @package Jeremias\WcLicensedProduct\Frontend
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jeremias\WcLicensedProduct\Frontend;
|
||||
|
||||
use Jeremias\WcLicensedProduct\License\LicenseManager;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
* Handles customer account pages for viewing licenses
|
||||
*/
|
||||
final class AccountController
|
||||
{
|
||||
private Environment $twig;
|
||||
private LicenseManager $licenseManager;
|
||||
|
||||
public function __construct(Environment $twig, LicenseManager $licenseManager)
|
||||
{
|
||||
$this->twig = $twig;
|
||||
$this->licenseManager = $licenseManager;
|
||||
$this->registerHooks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register WordPress hooks
|
||||
*/
|
||||
private function registerHooks(): void
|
||||
{
|
||||
// Add licenses endpoint
|
||||
add_action('init', [$this, 'addLicensesEndpoint']);
|
||||
|
||||
// Add licenses menu item
|
||||
add_filter('woocommerce_account_menu_items', [$this, 'addLicensesMenuItem']);
|
||||
|
||||
// Add licenses endpoint content
|
||||
add_action('woocommerce_account_licenses_endpoint', [$this, 'displayLicensesContent']);
|
||||
|
||||
// Enqueue frontend styles
|
||||
add_action('wp_enqueue_scripts', [$this, 'enqueueStyles']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add licenses endpoint for My Account
|
||||
*/
|
||||
public function addLicensesEndpoint(): void
|
||||
{
|
||||
add_rewrite_endpoint('licenses', EP_ROOT | EP_PAGES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add licenses menu item to My Account navigation
|
||||
*/
|
||||
public function addLicensesMenuItem(array $items): array
|
||||
{
|
||||
// Insert licenses after orders
|
||||
$newItems = [];
|
||||
foreach ($items as $key => $value) {
|
||||
$newItems[$key] = $value;
|
||||
if ($key === 'orders') {
|
||||
$newItems['licenses'] = __('Licenses', 'wc-licensed-product');
|
||||
}
|
||||
}
|
||||
|
||||
return $newItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display licenses content in My Account
|
||||
*/
|
||||
public function displayLicensesContent(): void
|
||||
{
|
||||
$customerId = get_current_user_id();
|
||||
if (!$customerId) {
|
||||
echo '<p>' . esc_html__('Please log in to view your licenses.', 'wc-licensed-product') . '</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
$licenses = $this->licenseManager->getLicensesByCustomer($customerId);
|
||||
|
||||
// Enrich licenses with product data
|
||||
$enrichedLicenses = [];
|
||||
foreach ($licenses as $license) {
|
||||
$product = wc_get_product($license->getProductId());
|
||||
$order = wc_get_order($license->getOrderId());
|
||||
|
||||
$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() : '',
|
||||
];
|
||||
}
|
||||
|
||||
try {
|
||||
echo $this->twig->render('frontend/licenses.html.twig', [
|
||||
'licenses' => $enrichedLicenses,
|
||||
'has_licenses' => !empty($enrichedLicenses),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
// Fallback to PHP template if Twig fails
|
||||
$this->displayLicensesFallback($enrichedLicenses);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fallback display method if Twig is unavailable
|
||||
*/
|
||||
private function displayLicensesFallback(array $enrichedLicenses): void
|
||||
{
|
||||
if (empty($enrichedLicenses)) {
|
||||
echo '<p>' . esc_html__('You have no licenses yet.', 'wc-licensed-product') . '</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
?>
|
||||
<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'); ?>">
|
||||
<?php if ($item['product_url']): ?>
|
||||
<a href="<?php echo esc_url($item['product_url']); ?>">
|
||||
<?php echo esc_html($item['product_name']); ?>
|
||||
</a>
|
||||
<?php else: ?>
|
||||
<?php echo esc_html($item['product_name']); ?>
|
||||
<?php endif; ?>
|
||||
</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())); ?>
|
||||
</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>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue frontend styles
|
||||
*/
|
||||
public function enqueueStyles(): void
|
||||
{
|
||||
if (!is_account_page()) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_enqueue_style(
|
||||
'wc-licensed-product-frontend',
|
||||
WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/css/frontend.css',
|
||||
[],
|
||||
WC_LICENSED_PRODUCT_VERSION
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user