You've already forked wc-licensed-product
Release v0.6.1 - UI improvements and bug fixes
- Fix admin license test popup showing empty product field - Display product name in bold in test license modal - Split auto-update settings into notification and auto-install options - Add filter functionality to customer account licenses page - Update translations (402 strings) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -106,23 +106,104 @@ final class AccountController
|
||||
return;
|
||||
}
|
||||
|
||||
// Get filter parameters from URL
|
||||
$filterProductId = isset($_GET['filter_product']) ? absint($_GET['filter_product']) : 0;
|
||||
$filterDomain = isset($_GET['filter_domain']) ? sanitize_text_field(wp_unslash($_GET['filter_domain'])) : '';
|
||||
|
||||
$licenses = $this->licenseManager->getLicensesByCustomer($customerId);
|
||||
|
||||
// Apply filters
|
||||
$filteredLicenses = $this->applyLicenseFilters($licenses, $filterProductId, $filterDomain);
|
||||
|
||||
// Group licenses by product+order into "packages"
|
||||
$packages = $this->groupLicensesIntoPackages($licenses);
|
||||
$packages = $this->groupLicensesIntoPackages($filteredLicenses);
|
||||
|
||||
// Get unique products and domains for filter dropdowns
|
||||
$filterOptions = $this->getFilterOptions($licenses);
|
||||
|
||||
try {
|
||||
echo $this->twig->render('frontend/licenses.html.twig', [
|
||||
'packages' => $packages,
|
||||
'has_packages' => !empty($packages),
|
||||
'signing_enabled' => ResponseSigner::isSigningEnabled(),
|
||||
'filter_products' => $filterOptions['products'],
|
||||
'filter_domains' => $filterOptions['domains'],
|
||||
'current_filter_product' => $filterProductId,
|
||||
'current_filter_domain' => $filterDomain,
|
||||
'is_filtered' => $filterProductId > 0 || !empty($filterDomain),
|
||||
'licenses_url' => wc_get_account_endpoint_url('licenses'),
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
// Fallback to PHP template if Twig fails
|
||||
$this->displayLicensesFallback($packages);
|
||||
$this->displayLicensesFallback($packages, $filterOptions, $filterProductId, $filterDomain);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply filters to licenses
|
||||
*
|
||||
* @param array $licenses Array of License objects
|
||||
* @param int $productId Filter by product ID (0 for all)
|
||||
* @param string $domain Filter by domain (empty for all)
|
||||
* @return array Filtered array of License objects
|
||||
*/
|
||||
private function applyLicenseFilters(array $licenses, int $productId, string $domain): array
|
||||
{
|
||||
if ($productId === 0 && empty($domain)) {
|
||||
return $licenses;
|
||||
}
|
||||
|
||||
return array_filter($licenses, function ($license) use ($productId, $domain) {
|
||||
// Filter by product
|
||||
if ($productId > 0 && $license->getProductId() !== $productId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Filter by domain (partial match)
|
||||
if (!empty($domain) && stripos($license->getDomain(), $domain) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unique filter options from licenses
|
||||
*
|
||||
* @param array $licenses Array of License objects
|
||||
* @return array Array with 'products' and 'domains' keys
|
||||
*/
|
||||
private function getFilterOptions(array $licenses): array
|
||||
{
|
||||
$products = [];
|
||||
$domains = [];
|
||||
|
||||
foreach ($licenses as $license) {
|
||||
// Collect unique products
|
||||
$productId = $license->getProductId();
|
||||
if (!isset($products[$productId])) {
|
||||
$product = wc_get_product($productId);
|
||||
$products[$productId] = $product ? $product->get_name() : __('Unknown Product', 'wc-licensed-product');
|
||||
}
|
||||
|
||||
// Collect unique domains
|
||||
$domain = $license->getDomain();
|
||||
if (!in_array($domain, $domains, true)) {
|
||||
$domains[] = $domain;
|
||||
}
|
||||
}
|
||||
|
||||
// Sort products by name, domains alphabetically
|
||||
asort($products);
|
||||
sort($domains);
|
||||
|
||||
return [
|
||||
'products' => $products,
|
||||
'domains' => $domains,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Group licenses into packages by product+order
|
||||
*
|
||||
@@ -217,10 +298,67 @@ final class AccountController
|
||||
/**
|
||||
* Fallback display method if Twig is unavailable
|
||||
*/
|
||||
private function displayLicensesFallback(array $packages): void
|
||||
{
|
||||
private function displayLicensesFallback(
|
||||
array $packages,
|
||||
array $filterOptions = [],
|
||||
int $currentFilterProduct = 0,
|
||||
string $currentFilterDomain = ''
|
||||
): void {
|
||||
$isFiltered = $currentFilterProduct > 0 || !empty($currentFilterDomain);
|
||||
$licensesUrl = wc_get_account_endpoint_url('licenses');
|
||||
|
||||
// Display filter form if we have filter options
|
||||
if (!empty($filterOptions['products']) || !empty($filterOptions['domains'])) {
|
||||
?>
|
||||
<div class="wclp-filter-form">
|
||||
<form method="get" action="<?php echo esc_url($licensesUrl); ?>">
|
||||
<div class="wclp-filter-row">
|
||||
<?php if (!empty($filterOptions['products'])): ?>
|
||||
<div class="wclp-filter-field">
|
||||
<label for="filter_product"><?php esc_html_e('Product', 'wc-licensed-product'); ?></label>
|
||||
<select name="filter_product" id="filter_product">
|
||||
<option value=""><?php esc_html_e('All Products', 'wc-licensed-product'); ?></option>
|
||||
<?php foreach ($filterOptions['products'] as $id => $name): ?>
|
||||
<option value="<?php echo esc_attr($id); ?>" <?php selected($currentFilterProduct, $id); ?>>
|
||||
<?php echo esc_html($name); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($filterOptions['domains'])): ?>
|
||||
<div class="wclp-filter-field">
|
||||
<label for="filter_domain"><?php esc_html_e('Domain', 'wc-licensed-product'); ?></label>
|
||||
<select name="filter_domain" id="filter_domain">
|
||||
<option value=""><?php esc_html_e('All Domains', 'wc-licensed-product'); ?></option>
|
||||
<?php foreach ($filterOptions['domains'] as $domain): ?>
|
||||
<option value="<?php echo esc_attr($domain); ?>" <?php selected($currentFilterDomain, $domain); ?>>
|
||||
<?php echo esc_html($domain); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="wclp-filter-actions">
|
||||
<button type="submit" class="button"><?php esc_html_e('Filter', 'wc-licensed-product'); ?></button>
|
||||
<?php if ($isFiltered): ?>
|
||||
<a href="<?php echo esc_url($licensesUrl); ?>" class="button"><?php esc_html_e('Clear', 'wc-licensed-product'); ?></a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
if (empty($packages)) {
|
||||
echo '<p>' . esc_html__('You have no licenses yet.', 'wc-licensed-product') . '</p>';
|
||||
if ($isFiltered) {
|
||||
echo '<p>' . esc_html__('No licenses found matching your filters.', 'wc-licensed-product') . '</p>';
|
||||
} else {
|
||||
echo '<p>' . esc_html__('You have no licenses yet.', 'wc-licensed-product') . '</p>';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user