| @@ -5,33 +5,73 @@ | ||
| 5 | 5 | use SyncBasalam\Queue\Tasks\Debug; |
| 6 | 6 | use SyncBasalam\Actions\Controller\ProductActions\CancelDebug; |
| 7 | 7 | use SyncBasalam\Services\WebhookService; |
| 8 | 8 | use SyncBasalam\Services\VendorInfoService; |
| 9 | +use SyncBasalam\Services\ScheduledProductSync; | |
| 9 | 10 | |
| 10 | 11 | defined('ABSPATH') || exit; |
| 11 | 12 | |
| 12 | 13 | class SettingsPageHandler |
| 13 | 14 | { |
| 15 | + private const VALIDATION_ERROR_TRANSIENT_PREFIX = 'sync_basalam_settings_validation_error_'; | |
| 16 | + | |
| 14 | 17 | public static function saveSettings() |
| 15 | 18 | { |
| 16 | 19 | $data = isset($_POST['sync_basalam_settings']) ? array_map('sanitize_text_field', wp_unslash($_POST['sync_basalam_settings'])) : []; |
| 17 | 20 | |
| 18 | 21 | if ($data) { |
| 22 | + if (!SettingsManager::isProductUpdateSelectionValid($data)) { | |
| 23 | + self::pushValidationError(SettingsConfig::CUSTOM_PRODUCT_UPDATE_REQUIRED_MESSAGE); | |
| 24 | + return false; | |
| 25 | + } | |
| 26 | + | |
| 19 | 27 | SettingsManager::updateSettings($data); |
| 20 | 28 | |
| 21 | - if (!empty($data[SettingsConfig::DEVELOPER_MODE]) && $data[SettingsConfig::DEVELOPER_MODE] === 'true') { | |
| 22 | - $debugTask = new Debug(); | |
| 23 | - $debugTask->schedule(); | |
| 24 | - } else { | |
| 25 | - (new CancelDebug())(); | |
| 29 | + if (array_key_exists(SettingsConfig::AUTO_FULL_UPDATE_INTERVAL, $data)) { | |
| 30 | + (new ScheduledProductSync())->ensureScheduled(); | |
| 26 | 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 | + } | |
| 27 | 41 | } |
| 28 | 42 | |
| 29 | 43 | if (isset($_POST['get_token']) && $_POST['get_token'] == 1) { |
| 30 | 44 | self::redirectToOAuth(); |
| 31 | 45 | } |
| 46 | + | |
| 47 | + return true; | |
| 32 | 48 | } |
| 33 | 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 | + | |
| 34 | 74 | private static function redirectToOAuth() |
| 35 | 75 | { |
| 36 | 76 | $OAuthManger = new OAuthManager(); |
| 37 | 77 | $oauthUrls = $OAuthManger->getOAuthUrls(); |
| @@ -38,9 +78,15 @@ | ||
| 38 | 78 | |
| 39 | 79 | // Mark this authorization as started by the current (authenticated) admin |
| 40 | 80 | // so the OAuth callback can reject forged requests. This runs only after |
| 41 | 81 | // the nonce-protected settings POST, so it cannot be triggered cross-site. |
| 42 | - OAuthManager::issueOauthState(); | |
| 82 | + if (! OAuthManager::issueOauthState()) { | |
| 83 | + wp_die( | |
| 84 | + esc_html__('امکان شروع فرایند دریافت دسترسی وجود ندارد. لطفاً دوباره تلاش کنید.', 'sync-basalam'), | |
| 85 | + esc_html__('خطای دریافت دسترسی', 'sync-basalam'), | |
| 86 | + ['response' => 500] | |
| 87 | + ); | |
| 88 | + } | |
| 43 | 89 | |
| 44 | 90 | wp_redirect($oauthUrls['url_req_token']); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Intentional external/user-provided redirect. |
| 45 | 91 | exit(); |
| 46 | 92 | } |