fix: improve SearchableSelect with label resolution for pre-selected items

- Fetch post titles for pre-selected IDs on mount via include[] param
- Cache labels from search results to avoid re-fetching
- Filter already-selected items from dropdown results
- Add loading spinner and no-results message
- Update PHP autocomplete handler to support include[] param
- Fix CSS class conflict warning
This commit is contained in:
dwindown
2026-04-19 15:47:59 +07:00
parent 7ba92022d5
commit 1a10c18c31
6 changed files with 109 additions and 29 deletions

View File

@@ -1073,8 +1073,35 @@ class Coupon {
$post_type = isset($_REQUEST['post_type']) ? sanitize_text_field(wp_unslash($_REQUEST['post_type'])) : '';
$search = isset($_REQUEST['search']) ? sanitize_text_field(wp_unslash($_REQUEST['search'])) : '';
$include = isset($_REQUEST['include']) ? array_map('intval', (array) $_REQUEST['include']) : [];
if (empty($post_type) || strlen($search) < 2) {
if (empty($post_type)) {
wp_send_json_error( [ 'message' => 'Invalid request' ] );
}
// Resolve labels for specific IDs (pre-selected items)
if (!empty($include)) {
$query = get_posts([
'post_type' => $post_type,
'post__in' => $include,
'posts_per_page' => -1,
'post_status' => 'any',
]);
$results = [];
if (!empty($query)) {
foreach ($query as $post) {
$results[] = [
'value' => $post->ID,
'label' => $post->post_title,
];
}
}
wp_send_json_success($results);
return;
}
// Search by keyword
if (strlen($search) < 2) {
wp_send_json_error( [ 'message' => 'Invalid request' ] );
}