PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.22
ووسلام – همگام سازی ووکامرس و باسلام v1.10.22
1.10.22 1.10.21 1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 All 55 releases
sync-basalam / includes / Admin / Settings / SettingsPageHandler.php

SettingsPageHandler.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.22, at includes/Admin/Settings/SettingsPageHandler.php

116 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Admin\Settings;
4
5 use SyncBasalam\Queue\Tasks\Debug;
6 use SyncBasalam\Actions\Controller\ProductActions\CancelDebug;
7 use SyncBasalam\Services\WebhookService;
8 use SyncBasalam\Services\VendorInfoService;
9 use SyncBasalam\Services\ScheduledProductSync;
10
11 defined('ABSPATH') || exit;
12
13 class SettingsPageHandler
14 {
15 private const VALIDATION_ERROR_TRANSIENT_PREFIX = 'sync_basalam_settings_validation_error_';
16
17 public static function saveSettings()
18 {
19 $data = isset($_POST['sync_basalam_settings']) ? array_map('sanitize_text_field', wp_unslash($_POST['sync_basalam_settings'])) : [];
20
21 if ($data) {
22 if (!SettingsManager::isProductUpdateSelectionValid($data)) {
23 self::pushValidationError(SettingsConfig::CUSTOM_PRODUCT_UPDATE_REQUIRED_MESSAGE);
24 return false;
25 }
26
27 SettingsManager::updateSettings($data);
28
29 if (array_key_exists(SettingsConfig::AUTO_FULL_UPDATE_INTERVAL, $data)) {
30 (new ScheduledProductSync())->ensureScheduled();
31 }
32
33 if (array_key_exists(SettingsConfig::DEVELOPER_MODE, $data)) {
34 if ($data[SettingsConfig::DEVELOPER_MODE] === 'true') {
35 $debugTask = new Debug();
36 $debugTask->schedule();
37 } else {
38 (new CancelDebug())();
39 }
40 }
41 }
42
43 if (isset($_POST['get_token']) && $_POST['get_token'] == 1) {
44 self::redirectToOAuth();
45 }
46
47 return true;
48 }
49
50 public static function pullValidationError(): string
51 {
52 $userId = get_current_user_id();
53 if ($userId <= 0) return '';
54
55 $key = self::VALIDATION_ERROR_TRANSIENT_PREFIX . $userId;
56 $message = get_transient($key);
57 delete_transient($key);
58
59 return is_string($message) ? $message : '';
60 }
61
62 private static function pushValidationError(string $message): void
63 {
64 $userId = get_current_user_id();
65 if ($userId <= 0) return;
66
67 set_transient(
68 self::VALIDATION_ERROR_TRANSIENT_PREFIX . $userId,
69 wp_strip_all_tags($message),
70 2 * MINUTE_IN_SECONDS
71 );
72 }
73
74 private static function redirectToOAuth()
75 {
76 $OAuthManger = new OAuthManager();
77 $oauthUrls = $OAuthManger->getOAuthUrls();
78
79 // Mark this authorization as started by the current (authenticated) admin
80 // so the OAuth callback can reject forged requests. This runs only after
81 // the nonce-protected settings POST, so it cannot be triggered cross-site.
82 if (! OAuthManager::issueOauthState()) {
83 wp_die(
84 esc_html__('ا�
85 کان شروع فرایند دریافت دسترسی وجود ندارد. لطفاً دوباره تلاش کنید.', 'sync-basalam'),
86 esc_html__('خطای دریافت دسترسی', 'sync-basalam'),
87 ['response' => 500]
88 );
89 }
90
91 wp_redirect($oauthUrls['url_req_token']); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Intentional external/user-provided redirect.
92 exit();
93 }
94
95 public static function handleOauthCallback()
96 {
97 $oauthSaved = OAuthManager::saveOauthData();
98
99 if ($oauthSaved) {
100 $webhookService = new WebhookService();
101 $webhookService->setupWebhook();
102 $vendorInfoService = new VendorInfoService();
103 $vendorInfoService->FetchVendorInfo();
104 }
105
106 $onboardingCompleted = get_option('sync_basalam_onboarding_completed');
107
108 if (!$onboardingCompleted) {
109 wp_safe_redirect(admin_url('admin.php?page=basalam-onboarding&step=3'));
110 } else {
111 wp_safe_redirect(admin_url('admin.php?page=sync_basalam'));
112 }
113 exit();
114 }
115 }
116