You've already forked wc-licensed-product
Add live search to admin licenses overview
- Add AJAX handler for real-time license search - Create admin-licenses.js with debounced search and keyboard navigation - Display search results with highlighted matches - Support navigation with arrow keys and Enter to select - Add CSS for dropdown results styling Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,9 @@ final class AdminController
|
||||
|
||||
// Add to WooCommerce Reports
|
||||
add_filter('woocommerce_admin_reports', [$this, 'addLicenseReports']);
|
||||
|
||||
// AJAX handler for live search
|
||||
add_action('wp_ajax_wclp_live_search', [$this, 'handleLiveSearch']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,6 +111,67 @@ final class AdminController
|
||||
[],
|
||||
WC_LICENSED_PRODUCT_VERSION
|
||||
);
|
||||
|
||||
// Enqueue live search script on licenses page
|
||||
if ($isLicensePage) {
|
||||
wp_enqueue_script(
|
||||
'wc-licensed-product-admin',
|
||||
WC_LICENSED_PRODUCT_PLUGIN_URL . 'assets/js/admin-licenses.js',
|
||||
['jquery'],
|
||||
WC_LICENSED_PRODUCT_VERSION,
|
||||
true
|
||||
);
|
||||
|
||||
wp_localize_script('wc-licensed-product-admin', 'wclpAdmin', [
|
||||
'ajaxUrl' => admin_url('admin-ajax.php'),
|
||||
'nonce' => wp_create_nonce('wclp_live_search'),
|
||||
'strings' => [
|
||||
'noResults' => __('No licenses found', 'wc-licensed-product'),
|
||||
'searching' => __('Searching...', 'wc-licensed-product'),
|
||||
'error' => __('Search failed', 'wc-licensed-product'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle AJAX live search request
|
||||
*/
|
||||
public function handleLiveSearch(): void
|
||||
{
|
||||
check_ajax_referer('wclp_live_search', 'nonce');
|
||||
|
||||
if (!current_user_can('manage_woocommerce')) {
|
||||
wp_send_json_error(['message' => __('Permission denied.', 'wc-licensed-product')], 403);
|
||||
}
|
||||
|
||||
$search = isset($_GET['search']) ? sanitize_text_field($_GET['search']) : '';
|
||||
|
||||
if (strlen($search) < 2) {
|
||||
wp_send_json_success(['results' => []]);
|
||||
}
|
||||
|
||||
$filters = ['search' => $search];
|
||||
$licenses = $this->licenseManager->getAllLicenses(1, 10, $filters);
|
||||
|
||||
$results = [];
|
||||
foreach ($licenses as $license) {
|
||||
$product = wc_get_product($license->getProductId());
|
||||
$customer = get_userdata($license->getCustomerId());
|
||||
|
||||
$results[] = [
|
||||
'id' => $license->getId(),
|
||||
'license_key' => $license->getLicenseKey(),
|
||||
'domain' => $license->getDomain(),
|
||||
'status' => $license->getStatus(),
|
||||
'product_name' => $product ? $product->get_name() : __('Unknown', 'wc-licensed-product'),
|
||||
'customer_name' => $customer ? $customer->display_name : __('Guest', 'wc-licensed-product'),
|
||||
'customer_email' => $customer ? $customer->user_email : '',
|
||||
'view_url' => admin_url('admin.php?page=wc-licenses&s=' . urlencode($license->getLicenseKey())),
|
||||
];
|
||||
}
|
||||
|
||||
wp_send_json_success(['results' => $results]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user