Implement a new WordPress plugin to bridge the frontend with the Laravel logistics API. This includes: - API client for secure shipment tracking via Bearer token - Admin settings page for configuring API URL and Key - Shortcodes for embedding tracking forms and status displays - AJAX-based tracking form with real-time results and timeline view
55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
/**
|
|
* IFNEX API Client
|
|
* اتصال به API لاراول برای استعلام ترکینگ
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
class IFNEX_API_Client {
|
|
private $api_url;
|
|
private $api_key;
|
|
|
|
public function __construct() {
|
|
$this->api_url = rtrim(get_option('ifnex_api_url', 'http://localhost:8000/api/v1'), '/');
|
|
$this->api_key = get_option('ifnex_api_key', '');
|
|
}
|
|
|
|
public function track_shipment($awb_no) {
|
|
if (empty($this->api_key)) {
|
|
return new WP_Error('no_api_key', 'API Key تنظیم نشده است.');
|
|
}
|
|
|
|
$url = $this->api_url . '/track/' . urlencode($awb_no);
|
|
|
|
$args = array(
|
|
'headers' => array(
|
|
'Authorization' => 'Bearer ' . $this->api_key,
|
|
'Accept' => 'application/json',
|
|
),
|
|
'timeout' => 30,
|
|
);
|
|
|
|
$response = wp_remote_get($url, $args);
|
|
|
|
if (is_wp_error($response)) {
|
|
return $response;
|
|
}
|
|
|
|
$body = wp_remote_retrieve_body($response);
|
|
$data = json_decode($body, true);
|
|
|
|
if ($response['response']['code'] === 404) {
|
|
return new WP_Error('not_found', 'مرسولهای با این کد پیدا نشد.');
|
|
}
|
|
|
|
if ($response['response']['code'] !== 200) {
|
|
return new WP_Error('api_error', 'خطا در ارتباط با API: ' . ($data['message'] ?? 'Unknown error'));
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|