ifnex/03_WordPress/wp-content/plugins/ifnex-bridge/includes/user-bridge.php
Kazem Alghasi b605b689fd feat(wp-plugin): expand IFNEX Bridge with wallet and transaction support
Extend the WordPress bridge plugin to provide seamless integration with
the backend wallet system. This update introduces user-specific
functionality via shortcodes and AJAX.

- feat(bridge): implement `IFNEX_User_Bridge` for Sanctum token management
- feat(bridge): add `[ifnex_wallet_balance]` shortcode for balance display
- feat(bridge): add `[ifnex_transactions]` shortcode for transaction history
- feat(bridge): implement AJAX handlers for real-time balance and transaction updates
- feat(bridge): add plugin-specific CSS assets and enqueueing logic
- docs: update project status with new bridge capabilities
- chore: add plugin testing utility and asset directory structure
2026-08-08 03:49:46 +03:30

222 lines
6.3 KiB
PHP

<?php
/**
* IFNEX User Bridge
* مدیریت ارتباط کاربران وردپرس با لاراول
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class IFNEX_User_Bridge {
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', '');
}
/**
* دریافت توکن Sanctum برای کاربر وردپرس
*/
public function get_user_token($user_id) {
$token = get_user_meta($user_id, 'ifnex_laravel_token', true);
$token_expiry = get_user_meta($user_id, 'ifnex_laravel_token_expiry', true);
if ($token && $token_expiry && strtotime($token_expiry) > time()) {
return $token;
}
$user = get_userdata($user_id);
if (!$user) {
return false;
}
$token = $this->request_token($user->user_email, $this->generate_temp_password($user_id));
if ($token) {
update_user_meta($user_id, 'ifnex_laravel_token', $token);
update_user_meta($user_id, 'ifnex_laravel_token_expiry', date('Y-m-d H:i:s', strtotime('+30 days')));
}
return $token;
}
/**
* درخواست توکن از لاراول
*/
private function request_token($email, $password) {
$url = $this->api_url . '/sanctum/token';
$args = array(
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
),
'body' => json_encode(array(
'email' => $email,
'password' => $password,
'token_name' => 'wordpress-bridge-' . get_current_blog_id(),
)),
'timeout' => 30,
);
$response = wp_remote_post($url, $args);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['token'])) {
return $data['token'];
}
return false;
}
/**
* تولید رمز عبور موقت برای کاربر وردپرس
*/
private function generate_temp_password($user_id) {
$temp_pass = get_user_meta($user_id, 'ifnex_temp_password', true);
if (!$temp_pass) {
$temp_pass = wp_generate_password(32, true, true);
update_user_meta($user_id, 'ifnex_temp_password', $temp_pass);
}
return $temp_pass;
}
/**
* ارسال درخواست احراز هویت به API لاراول
*/
public function authenticated_request($user_id, $endpoint, $method = 'GET', $data = array()) {
$token = $this->get_user_token($user_id);
if (!$token) {
return new WP_Error('no_token', 'توکن احراز هویت یافت نشد.');
}
$url = $this->api_url . $endpoint;
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
),
'timeout' => 30,
);
if ($method === 'POST' && !empty($data)) {
$args['body'] = json_encode($data);
$args['method'] = 'POST';
} else {
$args['method'] = 'GET';
if (!empty($data)) {
$url = add_query_arg($data, $url);
}
}
$response = wp_remote_request($url, $args);
if (is_wp_error($response)) {
return $response;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if ($response['response']['code'] === 401) {
$this->invalidate_token(get_current_user_id());
return new WP_Error('token_expired', 'توکن منقضی شده است. لطفاً دوباره وارد شوید.');
}
return $data;
}
/**
* باطل کردن توکن
*/
public function invalidate_token($user_id) {
delete_user_meta($user_id, 'ifnex_laravel_token');
delete_user_meta($user_id, 'ifnex_laravel_token_expiry');
}
/**
* دریافت موجودی کیف پول کاربر
*/
public function get_wallet_balance($user_id) {
$result = $this->authenticated_request($user_id, '/wallet/balance');
if (is_wp_error($result)) {
return $result;
}
return array(
'balance' => $result['balance'] ?? 0,
'total_deposited' => $result['total_deposited'] ?? 0,
'total_withdrawn' => $result['total_withdrawn'] ?? 0,
'is_frozen' => $result['is_frozen'] ?? false,
);
}
/**
* دریافت تراکنش‌های کاربر
*/
public function get_transactions($user_id, $per_page = 15) {
$result = $this->authenticated_request($user_id, '/wallet/transactions', 'GET', array(
'per_page' => $per_page,
));
if (is_wp_error($result)) {
return $result;
}
return $result;
}
/**
* رهگیری مرسوله
*/
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;
}
}