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
207 lines
7.4 KiB
PHP
207 lines
7.4 KiB
PHP
<?php
|
|
/**
|
|
* IFNEX Tracking Form Handler
|
|
* JavaScript و منطق فرم رهگیری
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
add_action('wp_footer', 'ifnex_tracking_form_scripts');
|
|
|
|
function ifnex_tracking_form_scripts() {
|
|
// فقط اگر شورتکد در صفحه وجود دارد، اسکریپت را لود کن
|
|
global $post;
|
|
if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'ifnex_tracking_form')) {
|
|
?>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
var form = document.getElementById('ifnex-tracking-form');
|
|
if (!form) return;
|
|
|
|
form.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
var input = document.getElementById('ifnex-awb-input');
|
|
var resultDiv = document.getElementById('ifnex-tracking-result');
|
|
var btn = document.getElementById('ifnex-track-btn');
|
|
var awb = input.value.trim();
|
|
|
|
if (!awb) {
|
|
resultDiv.innerHTML = '<p class="ifnex-error">لطفاً شماره بارنامه را وارد کنید.</p>';
|
|
resultDiv.style.display = 'block';
|
|
return;
|
|
}
|
|
|
|
btn.disabled = true;
|
|
btn.textContent = 'در حال جستجو...';
|
|
resultDiv.style.display = 'block';
|
|
resultDiv.innerHTML = '<p>در حال جستجو...</p>';
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.open('POST', '<?php echo esc_url(admin_url('admin-ajax.php')); ?>', true);
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
xhr.onload = function() {
|
|
btn.disabled = false;
|
|
btn.textContent = '<?php echo esc_js(__('رهگیری', 'ifnex-bridge')); ?>';
|
|
|
|
if (xhr.status === 200) {
|
|
var data = JSON.parse(xhr.responseText);
|
|
if (data.success) {
|
|
resultDiv.innerHTML = data.html;
|
|
} else {
|
|
resultDiv.innerHTML = '<p class="ifnex-error">' + data.message + '</p>';
|
|
}
|
|
} else {
|
|
resultDiv.innerHTML = '<p class="ifnex-error">خطا در ارتباط با سرور.</p>';
|
|
}
|
|
};
|
|
xhr.onerror = function() {
|
|
btn.disabled = false;
|
|
btn.textContent = '<?php echo esc_js(__('رهگیری', 'ifnex-bridge')); ?>';
|
|
resultDiv.innerHTML = '<p class="ifnex-error">خطا در ارتباط با سرور.</p>';
|
|
};
|
|
xhr.send('action=ifnex_track_shipment&awb=' + encodeURIComponent(awb));
|
|
});
|
|
});
|
|
</script>
|
|
<style>
|
|
.ifnex-tracking-container {
|
|
max-width: 600px;
|
|
margin: 20px auto;
|
|
padding: 20px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 8px;
|
|
font-family: inherit;
|
|
}
|
|
.ifnex-tracking-form {
|
|
margin-bottom: 20px;
|
|
}
|
|
.ifnex-form-group {
|
|
display: flex;
|
|
gap: 10px;
|
|
}
|
|
.ifnex-form-group input {
|
|
flex: 1;
|
|
padding: 10px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
font-size: 16px;
|
|
}
|
|
.ifnex-form-group button {
|
|
padding: 10px 20px;
|
|
background: #0073aa;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
}
|
|
.ifnex-form-group button:hover {
|
|
background: #005a87;
|
|
}
|
|
.ifnex-form-group button:disabled {
|
|
background: #ccc;
|
|
cursor: not-allowed;
|
|
}
|
|
.ifnex-tracking-result {
|
|
margin-top: 20px;
|
|
padding: 15px;
|
|
background: #f9f9f9;
|
|
border-radius: 4px;
|
|
}
|
|
.ifnex-error {
|
|
color: #dc3232;
|
|
}
|
|
.ifnex-tracking-status h3 {
|
|
margin-top: 0;
|
|
}
|
|
.ifnex-status-badge {
|
|
display: inline-block;
|
|
padding: 5px 10px;
|
|
background: #0073aa;
|
|
color: white;
|
|
border-radius: 4px;
|
|
margin-bottom: 10px;
|
|
}
|
|
.ifnex-timeline {
|
|
margin-top: 20px;
|
|
}
|
|
.ifnex-timeline ul {
|
|
list-style: none;
|
|
padding: 0;
|
|
}
|
|
.ifnex-timeline li {
|
|
padding: 10px;
|
|
border-bottom: 1px solid #eee;
|
|
}
|
|
.ifnex-timeline li:last-child {
|
|
border-bottom: none;
|
|
}
|
|
</style>
|
|
<?php
|
|
}
|
|
}
|
|
|
|
// AJAX Handler
|
|
add_action('wp_ajax_ifnex_track_shipment', 'ifnex_track_shipment_ajax');
|
|
add_action('wp_ajax_nopriv_ifnex_track_shipment', 'ifnex_track_shipment_ajax');
|
|
|
|
function ifnex_track_shipment_ajax() {
|
|
$awb = isset($_POST['awb']) ? sanitize_text_field($_POST['awb']) : '';
|
|
|
|
if (empty($awb)) {
|
|
wp_send_json(array('success' => false, 'message' => 'شماره بارنامه وارد نشده است.'));
|
|
return;
|
|
}
|
|
|
|
$client = new IFNEX_API_Client();
|
|
$result = $client->track_shipment($awb);
|
|
|
|
if (is_wp_error($result)) {
|
|
wp_send_json(array('success' => false, 'message' => $result->get_error_message()));
|
|
return;
|
|
}
|
|
|
|
// ساخت HTML نتیجه
|
|
ob_start();
|
|
?>
|
|
<div class="ifnex-tracking-status">
|
|
<h3>وضعیت مرسوله <?php echo esc_html($result['awb_no'] ?? ''); ?></h3>
|
|
<div class="ifnex-status-badge">
|
|
<?php echo esc_html($result['status'] ?? 'نامشخص'); ?>
|
|
</div>
|
|
<div class="ifnex-details">
|
|
<p><strong>نوع:</strong> <?php echo esc_html($result['type'] ?? ''); ?></p>
|
|
<p><strong>جهت:</strong> <?php echo esc_html($result['direction'] ?? ''); ?></p>
|
|
<?php if (!empty($result['sender']['name'])): ?>
|
|
<p><strong>فرستنده:</strong> <?php echo esc_html($result['sender']['name']); ?></p>
|
|
<?php endif; ?>
|
|
<?php if (!empty($result['receiver']['name'])): ?>
|
|
<p><strong>گیرنده:</strong> <?php echo esc_html($result['receiver']['name']); ?></p>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php if (!empty($result['timeline']) && is_array($result['timeline'])): ?>
|
|
<div class="ifnex-timeline">
|
|
<h4>تاریخچه رهگیری</h4>
|
|
<ul>
|
|
<?php foreach (array_reverse($result['timeline']) as $event): ?>
|
|
<li>
|
|
<strong><?php echo esc_html($event['event_date'] ?? ''); ?></strong>
|
|
<span><?php echo esc_html($event['event_description'] ?? ''); ?></span>
|
|
<?php if (!empty($event['location'])): ?>
|
|
<small><?php echo esc_html($event['location']); ?></small>
|
|
<?php endif; ?>
|
|
</li>
|
|
<?php endforeach; ?>
|
|
</ul>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php
|
|
$html = ob_get_clean();
|
|
|
|
wp_send_json(array('success' => true, 'html' => $html));
|
|
}
|