پیشنمایش طراحی و ترکیب سه کلاس در افزونه W
1. کلاس Loader
مسئول مدیریت و ثبت هوکهای اکشن و فیلتر وردپرس به صورت متمرکز
بارگذاری اجزای افزونه و مدیریت lifecycle آنها در افزونه
2. کلاس API Client
انجام ارتباطات HTTP امن با سرور SaaS برای فراخوانی APIها
مدیریت درخواستها، پاسخها، خطاها، و احراز هویت توکنی
3. کلاس AuthService
مدیریت فرایند احراز هویت در افزونه
نگهداری، تجدید و اعتبارسنجی توکنها (JWT یا OAuth token)
ارائه متدهای ورود، خروج و بررسی وضعیت نشست کاربر
پیادهسازی نمونه تلفیقی (نمونه کلی و سادهشده)
php
// Saas_Loader.php
class Saas_Loader {
protected $actions = [];
protected $filters = [];
public function add_action($hook, $component, $callback, $priority = 10, $accepted_args = 1) {
$this->actions[] = compact('hook', 'component', 'callback', 'priority', 'accepted_args');
}
public function add_filter($hook, $component, $callback, $priority = 10, $accepted_args = 1) {
$this->filters[] = compact('hook', 'component', 'callback', 'priority', 'accepted_args');
}
public function run() {
foreach ($this->filters as $filter) {
add_filter($filter['hook'], [$filter['component'], $filter['callback']], $filter['priority'], $filter['accepted_args']);
}
foreach ($this->actions as $action) {
add_action($action['hook'], [$action['component'], $action['callback']], $action['priority'], $action['accepted_args']);
}
}
}
// Saas_Api_Client.php
class Saas_Api_Client {
protected $base_url;
protected $token;
public function __construct($base_url, $token = null) {
$this->base_url = rtrim($base_url, '/');
$this->token = $token;
}
protected function request($method, $endpoint, $data = []) {
$url = $this->base_url . $endpoint;
$args = [
'method' => strtoupper($method),
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => $this->token ? "Bearer {$this->token}" : '',
],
'body' => $data ? json_encode($data) : null,
'timeout' => 15,
];
$response = wp_remote_request($url, $args);
if (is_wp_error($response)) {
throw new Exception('API request failed: ' . $response->get_error_message());
}
$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$result = json_decode($body, true);
if ($code !== 200) {
throw new Exception("API returned error: HTTP $code - " . ($result['message'] ?? $body));
}
return $result;
}
public function get($endpoint, $params = []) {
if ($params) {
$endpoint .= '?' . http_build_query($params);
}
return $this->request('GET', $endpoint);
}
public function post($endpoint, $data = []) {
return $this->request('POST', $endpoint, $data);
}
}
// Saas_Auth_Service.php
class Saas_Auth_Service {
protected $api_client;
protected $token_option_key = 'saas_auth_token';
public function __construct(Saas_Api_Client $api_client) {
$this->api_client = $api_client;
}
public function login($username, $password) {
$response = $this->api_client->post('/auth/login', ['username' => $username, 'password' => $password]);
if (isset($response['token'])) {
update_option($this->token_option_key, $response['token']);
$this->api_client->token = $response['token'];
return true;
}
return false;
}
public function logout() {
delete_option($this->token_option_key);
$this->api_client->token = null;
}
public function get_token() {
return get_option($this->token_option_key);
}
public function is_authenticated() {
return !empty($this->get_token());
}
public function refresh_token() {
$response = $this->api_client->post('/auth/refresh', ['token' => $this->get_token()]);
if (isset($response['token'])) {
update_option($this->token_option_key, $response['token']);
$this->api_client->token = $response['token'];
return true;
}
return false;
}
}
نکات کلیدی معماری پیشنهادی
Saas_Loader متمرکز روی رجیستر کردن هوکها و گردش کار افزونه
Saas_Api_Client وظیفه جامع تماسهای REST API و مدیریت خطاها را دارد
Saas_Auth_Service لایه احراز هویت حرفهای و ذخیرهسازی امن توکنها در دیتابیس وردپرس
امکان گسترش آسان و استفاده مشترک در کل افزونه با تزریق وابستگیها (DI)
مهندس، اگر بخواهید میتوانم همین کد را با توجه به ساختار دقیق پکیج شما به فایلهای مجزا تبدیل کرده، مستندسازی و حتی توسعه بدهم.
منتظر دستورات بعدی شما هستم.