| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Ajax; |
| 6 |
|
| 7 |
use Yatra\Repositories\AttributeRepository; |
| 8 |
|
| 9 |
/** |
| 10 |
* Attribute Query AJAX Handler (admin fallback when REST cache shape differs). |
| 11 |
*/ |
| 12 |
class DirectAttributeQuery |
| 13 |
{ |
| 14 |
private AttributeRepository $attributeRepository; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
$this->attributeRepository = new AttributeRepository(); |
| 19 |
add_action('wp_ajax_yatra_get_attribute_direct', [$this, 'handle']); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Handle AJAX request for attribute data |
| 24 |
*/ |
| 25 |
public function handle(): void |
| 26 |
{ |
| 27 |
if (!wp_verify_nonce($_POST['nonce'] ?? '', 'yatra_admin_nonce')) { |
| 28 |
wp_send_json_error(__('Your session has expired. Please refresh the page and try again.', 'yatra')); |
| 29 |
} |
| 30 |
|
| 31 |
$attributeId = (int) ($_POST['attribute_id'] ?? 0); |
| 32 |
|
| 33 |
if ($attributeId <= 0) { |
| 34 |
wp_send_json_error('Invalid attribute ID'); |
| 35 |
} |
| 36 |
|
| 37 |
try { |
| 38 |
$attribute = $this->attributeRepository->find($attributeId); |
| 39 |
|
| 40 |
if (!$attribute) { |
| 41 |
wp_send_json_error('Attribute not found'); |
| 42 |
} |
| 43 |
|
| 44 |
$row = [ |
| 45 |
'id' => (int) $attribute->id, |
| 46 |
'name' => (string) $attribute->name, |
| 47 |
'slug' => (string) $attribute->slug, |
| 48 |
'description' => (string) ($attribute->description ?? ''), |
| 49 |
'status' => (string) ($attribute->status ?? 'draft'), |
| 50 |
'icon' => null, |
| 51 |
'created_at' => $attribute->created_at ?? null, |
| 52 |
'updated_at' => $attribute->updated_at ?? null, |
| 53 |
]; |
| 54 |
|
| 55 |
if (!empty($attribute->icon)) { |
| 56 |
$iconData = maybe_unserialize($attribute->icon); |
| 57 |
if (is_array($iconData)) { |
| 58 |
$row['icon'] = $iconData; |
| 59 |
} else { |
| 60 |
$row['icon'] = [ |
| 61 |
'type' => 'icon', |
| 62 |
'value' => (string) $attribute->icon, |
| 63 |
]; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
if (!empty($attribute->metadata)) { |
| 68 |
$metadata = json_decode((string) $attribute->metadata, true); |
| 69 |
if (is_array($metadata)) { |
| 70 |
$row['field_type'] = $metadata['field_type'] ?? 'text_field'; |
| 71 |
$row['field_options'] = $metadata['field_options'] ?? ''; |
| 72 |
$row['default_value'] = $metadata['default_value'] ?? ''; |
| 73 |
$row['placeholder'] = $metadata['placeholder'] ?? ''; |
| 74 |
$row['required'] = $metadata['required'] ?? false; |
| 75 |
$row['validation_rules'] = $metadata['validation_rules'] ?? ''; |
| 76 |
$row['display_order'] = isset($metadata['display_order']) ? (int) $metadata['display_order'] : 0; |
| 77 |
$row['show_on_frontend'] = $metadata['show_on_frontend'] ?? false; |
| 78 |
$row['show_in_filters'] = $metadata['show_in_filters'] ?? false; |
| 79 |
$row['filter_type'] = $metadata['filter_type'] ?? 'exact'; |
| 80 |
$row['searchable'] = $metadata['searchable'] ?? false; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
wp_send_json_success($row); |
| 85 |
} catch (\Exception $e) { |
| 86 |
wp_send_json_error('Database query failed: ' . $e->getMessage()); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|