-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairducap-integration.php
More file actions
655 lines (568 loc) · 28.6 KB
/
airducap-integration.php
File metadata and controls
655 lines (568 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<?php
/**
* Plugin Name: Air Du Cap API Integration
* Plugin URI: https://airducap.com
* Description: Integration with Air Du Cap API for flight booking and airport search functionality
* Version: 1.0.3
* Author: Mr. Developer
* License: GPL v2 or later
* Text Domain: airducap-integration
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
// Plugin constants
define('AIRDUCAP_PLUGIN_URL', plugin_dir_url(__FILE__));
define('AIRDUCAP_PLUGIN_PATH', plugin_dir_path(__FILE__));
// API Configuration
// IMPORTANT: Using UAT environment because Production returns 404 errors
// Production URL: https://book.airducap.com (currently broken - returns 404)
// UAT URL: https://uat-book.airducap.com (working - returns 200 OK)
//
// Test results (Sept 2, 2025):
// - Production: 404 Not Found on all endpoints
// - UAT: Working correctly with flight data
//
// Contact client for correct Production API endpoints before switching
define('AIRDUCAP_API_BASE_URL', 'https://uat-book.airducap.com');
class AirDuCapIntegration {
private $api_username;
private $api_password;
private $api_base_url;
private $default_currency;
private $http_timeout;
private $ssl_verify;
public function __construct() {
// Get settings from database or use defaults
$this->api_username = get_option('airducap_api_username', 'dev101@dev101.com');
$this->api_password = get_option('airducap_api_password', 'QgdYlFgTvAQTcCC');
// Use UAT as the fallback default since production gives 404s
$this->api_base_url = get_option('airducap_api_base_url', 'https://uat-book.airducap.com');
$this->default_currency = get_option('airducap_default_currency', 'ZAR');
$this->http_timeout = intval(get_option('airducap_http_timeout', 45)) ?: 45; // Increased default timeout
$this->ssl_verify = get_option('airducap_ssl_verify', '1') === '1';
add_action('init', array($this, 'init'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_ajax_airducap_search_airports', array($this, 'ajax_search_airports'));
add_action('wp_ajax_nopriv_airducap_search_airports', array($this, 'ajax_search_airports'));
add_action('wp_ajax_airducap_search_flights', array($this, 'ajax_search_flights'));
add_action('wp_ajax_nopriv_airducap_search_flights', array($this, 'ajax_search_flights'));
add_action('wp_ajax_airducap_test_connection', array($this, 'ajax_test_connection'));
// Proxy for Google Static Map fallback
add_action('wp_ajax_airducap_proxy_map', array($this, 'ajax_proxy_map'));
add_action('wp_ajax_nopriv_airducap_proxy_map', array($this, 'ajax_proxy_map'));
// Network diagnostic tool
add_action('wp_ajax_airducap_network_test', array($this, 'ajax_network_test'));
add_shortcode('airducap_search_form', array($this, 'render_search_form'));
add_shortcode('airducap_flight_results', array($this, 'render_flight_results'));
// Include admin functionality
if (is_admin()) {
include_once AIRDUCAP_PLUGIN_PATH . 'admin/admin.php';
new AirDuCapAdmin();
include_once AIRDUCAP_PLUGIN_PATH . 'admin/api-test.php';
}
// Register activation hook
register_activation_hook(__FILE__, array($this, 'activate'));
}
public function activate() {
// Set default options
add_option('airducap_api_username', 'dev101@dev101.com');
add_option('airducap_api_password', 'QgdYlFgTvAQTcCC');
// Default to UAT API since production gives 404s
add_option('airducap_api_base_url', 'https://uat-book.airducap.com');
add_option('airducap_default_currency', 'ZAR');
add_option('airducap_enable_debug', '1'); // Enable debug by default to troubleshoot
add_option('airducap_http_timeout', 45); // Increased default
add_option('airducap_ssl_verify', '1');
}
public function init() {
// Initialize plugin
load_plugin_textdomain('airducap-integration', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
public function enqueue_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui-datepicker', 'https://code.jquery.com/ui/1.12.1/themes/ui-lightness/jquery-ui.css');
// Bootstrap CSS and JS for the flight cards design
wp_enqueue_style('bootstrap-css', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css', array(), '5.3.0');
wp_enqueue_script('bootstrap-js', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js', array('jquery'), '5.3.0', true);
// Font Awesome for icons
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css', array(), '6.4.0');
// Swiper for image carousels
wp_enqueue_style('swiper-css', 'https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.css', array(), '10.0.0');
wp_enqueue_script('swiper-js', 'https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.js', array(), '10.0.0', true);
// Plugin's custom scripts and styles
wp_enqueue_script('airducap-js', AIRDUCAP_PLUGIN_URL . 'assets/airducap.js', array('jquery', 'bootstrap-js', 'swiper-js'), '1.0.4', true);
wp_enqueue_style('airducap-css', AIRDUCAP_PLUGIN_URL . 'assets/airducap.css', array('bootstrap-css', 'font-awesome'), '1.0.3');
// Localize script for AJAX
wp_localize_script('airducap-js', 'airducap_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('airducap_nonce'),
// Expose defaults to the frontend so requests match live
'default_currency' => $this->default_currency,
'api_base_url' => $this->api_base_url,
));
}
/**
* Make API request with authentication with retry support
*/
private function make_api_request($endpoint, $params = array()) {
// Clean the base URL and ensure proper formatting
$base_url = rtrim($this->api_base_url, '/');
$url = $base_url . $endpoint;
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode($this->api_username . ':' . $this->api_password),
'Content-Type' => 'application/json',
'User-Agent' => 'AirDuCap-WordPress-Plugin/1.0',
),
'timeout' => $this->http_timeout,
'redirection' => 5,
'sslverify' => $this->ssl_verify,
'httpversion' => '1.1',
'decompress' => true,
);
// Debug logging
if (get_option('airducap_enable_debug', '0') === '1') {
error_log('AirDuCap API Request URL: ' . $url);
error_log('AirDuCap API Auth: ' . base64_encode($this->api_username . ':' . $this->api_password));
error_log('AirDuCap API Request Timeout: ' . $this->http_timeout . 's');
}
$attempts = 0;
$max_attempts = 2; // 1 retry on transient network timeouts
do {
$attempts++;
$response = wp_remote_get($url, $args);
if (is_wp_error($response)) {
$error = $response->get_error_message();
if (get_option('airducap_enable_debug', '0') === '1') {
error_log('AirDuCap API Error (attempt ' . $attempts . '): ' . $error);
}
// Retry only on timeouts
if ($attempts < $max_attempts && (stripos($error, 'timed out') !== false || stripos($error, 'cURL error 28') !== false)) {
// brief backoff
usleep(500000); // 500ms
continue;
}
return array('error' => $error);
}
$response_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
if (get_option('airducap_enable_debug', '0') === '1') {
error_log('AirDuCap API Response Code: ' . $response_code);
error_log('AirDuCap API Response Body (truncated): ' . substr($body, 0, 500));
}
if ($response_code !== 200) {
// Retry on 429/5xx once
if ($attempts < $max_attempts && in_array($response_code, array(429, 500, 502, 503, 504), true)) {
usleep(500000);
continue;
}
return array('error' => 'API returned status code: ' . $response_code . '. Response: ' . $body);
}
$data = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return array('error' => 'Invalid JSON response: ' . json_last_error_msg());
}
return $data;
} while ($attempts < $max_attempts);
return array('error' => 'Unknown error');
}
/**
* Normalize date to dd/mm/YYYY regardless of input format
*/
private function normalize_date($date) {
if (empty($date)) return '';
$date = trim($date);
// If already dd/mm/yyyy
if (preg_match('#^\d{2}/\d{2}/\d{4}$#', $date)) {
return $date;
}
// Try common formats
$formats = ['Y-m-d', 'm/d/Y', 'd-m-Y', 'Y/m/d'];
foreach ($formats as $fmt) {
$dt = DateTime::createFromFormat($fmt, $date);
if ($dt && $dt->format($fmt) === $date) {
return $dt->format('d/m/Y');
}
}
// Fallback: attempt strtotime
$ts = strtotime($date);
if ($ts) {
return date('d/m/Y', $ts);
}
return $date;
}
/**
* AJAX handler for API connection test
*/
public function ajax_test_connection() {
check_ajax_referer('airducap_nonce', 'nonce');
// Test with a simple API call - no hardcoded data
$test_params = array(
'q' => 'cape',
'field_name' => 'from_location'
);
$result = $this->make_api_request('/airports/api/list/', $test_params);
if (isset($result['error'])) {
wp_send_json_error('Connection failed: ' . $result['error']);
} else {
$count = is_array($result) ? count($result) : 0;
wp_send_json_success("✅ API connection successful! Found $count airports with test search. API is responding correctly.");
}
}
/**
* AJAX handler for airport search (with caching and better errors)
*/
public function ajax_search_airports() {
check_ajax_referer('airducap_nonce', 'nonce');
$search_term = isset($_POST['q']) ? sanitize_text_field(wp_unslash($_POST['q'])) : '';
$field_name = isset($_POST['field_name']) ? sanitize_text_field(wp_unslash($_POST['field_name'])) : '';
$from_location = isset($_POST['from_location']) ? intval($_POST['from_location']) : 0;
$to_location = isset($_POST['to_location']) ? intval($_POST['to_location']) : 0;
if (empty($search_term) || strlen($search_term) < 2) {
wp_send_json_error(['message' => 'Search term must be at least 2 characters long.']);
}
$cache_key = 'airducap_airports_' . md5(implode('|', array($this->api_base_url, $field_name, $from_location, $to_location, strtolower($search_term))));
$cached = get_transient($cache_key);
if ($cached) {
wp_send_json_success(['data' => $cached, 'cached' => true]);
}
// Use the documented endpoint
$params = array(
'q' => $search_term,
'field_name' => $field_name,
);
if ($from_location) { $params['from_location'] = $from_location; }
if ($to_location) { $params['to_location'] = $to_location; }
$response = $this->make_api_request('/airports/api/list/', $params);
// Force debug logging for this specific call
error_log('AIRPORT SEARCH DEBUG - Search Term: ' . $search_term);
error_log('AIRPORT SEARCH DEBUG - Timeout: ' . $this->http_timeout . 's');
error_log('AIRPORT SEARCH DEBUG - Raw API Response: ' . print_r($response, true));
if (isset($response['error'])) {
error_log('AIRPORT SEARCH DEBUG - API Error: ' . $response['error']);
$msg = $response['error'];
// Normalize common cURL timeout message for UX
if (stripos($msg, 'cURL error 28') !== false || stripos($msg, 'timed out') !== false) {
$msg = 'Connection to the API timed out. Please try again in a moment.';
}
wp_send_json_error(['message' => 'API request failed.', 'error' => $msg]);
}
// Handle the response - per API docs, it's an array of airports
if (empty($response)) {
error_log('AIRPORT SEARCH DEBUG - Empty response from API');
wp_send_json_error(['message' => 'No airports found - empty response.']);
}
if (!is_array($response)) {
error_log('AIRPORT SEARCH DEBUG - Response is not array, type: ' . gettype($response));
wp_send_json_error(['message' => 'Invalid response format - not an array.']);
}
if (count($response) === 0) {
error_log('AIRPORT SEARCH DEBUG - Response is empty array');
wp_send_json_error(['message' => 'No airports found - empty array.']);
}
// Cache for 30 minutes to improve UX and reduce API pressure
set_transient($cache_key, $response, 30 * MINUTE_IN_SECONDS);
error_log('AIRPORT SEARCH DEBUG - Success! Found ' . count($response) . ' airports');
error_log('AIRPORT SEARCH DEBUG - First airport: ' . print_r($response[0] ?? 'none', true));
wp_send_json_success(['data' => $response]);
}
/**
* AJAX handler for flight search
*/
public function ajax_search_flights() {
check_ajax_referer('airducap_nonce', 'nonce');
$from_location = intval($_POST['from_location']);
$to_location = intval($_POST['to_location']);
$raw_date_of_travel = isset($_POST['date_of_travel']) ? wp_unslash($_POST['date_of_travel']) : '';
$date_of_travel = sanitize_text_field($this->normalize_date($raw_date_of_travel));
$adults = intval($_POST['adults']);
if (empty($from_location) || empty($to_location) || empty($date_of_travel) || empty($adults)) {
wp_send_json_error('Missing required parameters: from_location, to_location, date_of_travel, and adults are required');
return;
}
$params = array(
'from_location' => $from_location,
'to_location' => $to_location,
'date_of_travel' => $date_of_travel,
'adults' => $adults,
'ip_address' => $_SERVER['REMOTE_ADDR']
);
// Optional parameters
if (!empty($_POST['date_of_return'])) {
$raw_return = wp_unslash($_POST['date_of_return']);
$params['date_of_return'] = sanitize_text_field($this->normalize_date($raw_return));
}
if (!empty($_POST['children'])) {
$params['children'] = intval($_POST['children']);
}
if (!empty($_POST['infants'])) {
$params['infants'] = intval($_POST['infants']);
}
// Always provide currency: posted value wins; otherwise use default from settings
if (!empty($_POST['currency'])) {
$params['currency'] = sanitize_text_field($_POST['currency']);
} else {
$params['currency'] = $this->default_currency ?: 'ZAR';
}
// Debug logging for final params
if (get_option('airducap_enable_debug', '0') === '1') {
error_log('AirDuCap Flights Search Params: ' . print_r($params, true));
}
// Always log the flight search for debugging purposes
error_log('FLIGHT SEARCH DEBUG - API Base URL: ' . $this->api_base_url);
error_log('FLIGHT SEARCH DEBUG - Final URL: ' . $this->api_base_url . '/flights/api/search/?' . http_build_query($params));
error_log('FLIGHT SEARCH DEBUG - Search Params: ' . print_r($params, true));
$flights = $this->make_api_request('/flights/api/search/', $params);
if (isset($flights['error'])) {
wp_send_json_error($flights['error']);
} else {
wp_send_json_success($flights);
}
}
/**
* Render search form shortcode
*/
public function render_search_form($atts) {
$atts = shortcode_atts(array(
'style' => 'default'
), $atts);
ob_start();
?>
<div class="airducap-search-form">
<!-- Tab Navigation matching screenshot -->
<div class="flight-search-tabs">
<button type="button" class="tab-button active" data-tab="flight-search">
<i class="fas fa-plane" aria-hidden="true"></i>
One Way
</button>
<button type="button" class="tab-button" data-tab="round-trip">
<i class="fas fa-exchange-alt" aria-hidden="true"></i>
Round Trip
</button>
</div>
<!-- Search Form matching screenshot layout -->
<form id="airducap-flight-search" class="airducap-form">
<?php wp_nonce_field('airducap_nonce', 'airducap_nonce'); ?>
<div class="form-container">
<!-- Origin Field -->
<div class="form-field origin-field">
<label for="origin">
<i class="fas fa-plane-departure" aria-hidden="true"></i>
Origin
</label>
<input type="text" id="origin" name="origin" class="airport-search form-input"
data-field="from_location" placeholder="Where from?" required>
<input type="hidden" id="from_location" name="from_location">
<div class="airport-suggestions" id="origin-suggestions"></div>
</div>
<!-- Destination Field -->
<div class="form-field destination-field">
<label for="destination">
<i class="fas fa-plane-arrival" aria-hidden="true"></i>
Destination
</label>
<input type="text" id="destination" name="destination" class="airport-search form-input"
data-field="to_location" placeholder="Where to?" required>
<input type="hidden" id="to_location" name="to_location">
<div class="airport-suggestions" id="destination-suggestions"></div>
</div>
<!-- Departure Date -->
<div class="form-field date-field">
<label for="depart_date">
<i class="fas fa-calendar-alt" aria-hidden="true"></i>
Departure
</label>
<input type="text" id="depart_date" name="depart_date" class="date-input form-input"
placeholder="Select date" required>
</div>
<!-- Return Date -->
<div class="form-field date-field return-field">
<label for="return_date">
<i class="fas fa-calendar-alt" aria-hidden="true"></i>
Return
</label>
<input type="text" id="return_date" name="return_date" class="date-input form-input"
placeholder="Select date">
</div>
<!-- Passengers & Class Dropdown -->
<div class="form-field passengers-field">
<label for="passengers-dropdown">
<i class="fas fa-user-friends" aria-hidden="true"></i>
Passengers & Class
</label>
<div class="passengers-dropdown-container">
<button type="button" class="passengers-dropdown-btn form-input" id="passengers-dropdown">
<span class="passenger-summary">1 Adult, Economy</span>
<i class="fas fa-chevron-down dropdown-arrow" aria-hidden="true"></i>
</button>
<div class="passengers-dropdown-menu" id="passengers-menu">
<div class="passenger-section">
<div class="passenger-row">
<span class="passenger-label">Adults</span>
<div class="passenger-controls">
<button type="button" class="passenger-btn minus" data-type="adults">-</button>
<span class="passenger-count" id="adults-count">1</span>
<button type="button" class="passenger-btn plus" data-type="adults">+</button>
</div>
</div>
<div class="passenger-row">
<span class="passenger-label">Children</span>
<div class="passenger-controls">
<button type="button" class="passenger-btn minus" data-type="children">-</button>
<span class="passenger-count" id="children-count">0</span>
<button type="button" class="passenger-btn plus" data-type="children">+</button>
</div>
</div>
<div class="passenger-row">
<span class="passenger-label">Infants</span>
<div class="passenger-controls">
<button type="button" class="passenger-btn minus" data-type="infants">-</button>
<span class="passenger-count" id="infants-count">0</span>
<button type="button" class="passenger-btn plus" data-type="infants">+</button>
</div>
</div>
</div>
<div class="class-section">
<div class="class-options">
<label class="class-option">
<input type="radio" name="travel_class" value="economy" checked>
<span>Economy</span>
</label>
<label class="class-option">
<input type="radio" name="travel_class" value="business">
<span>Business</span>
</label>
<label class="class-option">
<input type="radio" name="travel_class" value="first">
<span>First Class</span>
</label>
</div>
</div>
</div>
</div>
<!-- Hidden inputs for form submission -->
<input type="hidden" id="adults" name="adults" value="1">
<input type="hidden" id="children" name="children" value="0">
<input type="hidden" id="infants" name="infants" value="0">
<input type="hidden" id="class" name="class" value="economy">
</div>
<!-- Search Button -->
<div class="form-field search-field">
<button type="submit" class="search-btn">
<i class="fas fa-search" aria-hidden="true"></i>
<span class="btn-text">Search Flights</span>
</button>
</div>
</div>
</form>
<div id="flight-results"></div>
</div>
<?php
return ob_get_clean();
}
/**
* Render flight results shortcode
*/
public function render_flight_results($atts) {
return '<div id="airducap-flight-results-container"></div>';
}
/**
* Test API connection
*/
public function test_api_connection() {
$test_params = array(
'q' => 'new',
'field_name' => 'from_location'
);
$result = $this->make_api_request('/airports/api/list/', $test_params);
if (isset($result['error'])) {
return array('status' => 'error', 'message' => $result['error']);
} else {
return array('status' => 'success', 'message' => 'Connected successfully');
}
}
/**
* Proxy Google Static Map URL to bypass referrer restrictions when needed
*/
public function ajax_proxy_map() {
// nonce passed as GET param because images load via GET
check_ajax_referer('airducap_nonce', 'nonce');
$url = isset($_GET['url']) ? esc_url_raw(wp_unslash($_GET['url'])) : '';
if (!$url || stripos($url, 'https://maps.googleapis.com/maps/api/staticmap') !== 0) {
status_header(400);
echo 'Invalid URL';
wp_die();
}
$response = wp_remote_get($url, array(
'timeout' => min($this->http_timeout, 20),
'sslverify' => true,
));
if (is_wp_error($response)) {
status_header(502);
echo 'Map fetch error';
wp_die();
}
$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$ctype = wp_remote_retrieve_header($response, 'content-type');
if ($code !== 200 || empty($body)) {
status_header(502);
echo 'Map not available';
wp_die();
}
if (!$ctype) { $ctype = 'image/png'; }
header('Content-Type: ' . $ctype);
header('Cache-Control: max-age=3600');
echo $body;
wp_die();
}
/**
* Network diagnostic tool
*/
public function ajax_network_test() {
check_ajax_referer('airducap_nonce', 'nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error('Unauthorized');
}
$diagnostics = array();
// Test basic connectivity to API hosts
$hosts = array(
'Production' => 'https://book.airducap.com',
'UAT' => 'https://uat-book.airducap.com'
);
foreach ($hosts as $name => $host) {
$start_time = microtime(true);
$response = wp_remote_get($host . '/airports/api/list/?q=test&field_name=from_location', array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode($this->api_username . ':' . $this->api_password)
),
'timeout' => 15,
'sslverify' => $this->ssl_verify
));
$end_time = microtime(true);
$duration = round(($end_time - $start_time) * 1000, 2);
if (is_wp_error($response)) {
$diagnostics[$name] = array(
'status' => 'ERROR',
'error' => $response->get_error_message(),
'duration' => $duration . 'ms'
);
} else {
$code = wp_remote_retrieve_response_code($response);
$diagnostics[$name] = array(
'status' => $code === 200 ? 'OK' : 'HTTP ' . $code,
'duration' => $duration . 'ms'
);
}
}
wp_send_json_success($diagnostics);
}
}
// Initialize the plugin
new AirDuCapIntegration();