| @@ -5,33 +5,90 @@ | ||
| 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'])) : []; |
| 20 | + $toggleProductSync = !empty($_POST['sync_basalam_toggle_product']); | |
| 17 | 21 | |
| 22 | + if ($toggleProductSync) { | |
| 23 | + $nextStatus = !SettingsManager::getSettings(SettingsConfig::SYNC_STATUS_PRODUCT); | |
| 24 | + $nextValue = $nextStatus ? '1' : '0'; | |
| 25 | + $data[SettingsConfig::SYNC_STATUS_PRODUCT] = $nextValue; | |
| 26 | + $data[SettingsConfig::SYNC_STATUS_PRODUCT_CREATE] = $nextValue; | |
| 27 | + $data[SettingsConfig::SYNC_STATUS_PRODUCT_UPDATE] = $nextValue; | |
| 28 | + } | |
| 29 | + | |
| 18 | 30 | if ($data) { |
| 31 | + $syncToggleSubmitted = array_key_exists(SettingsConfig::SYNC_STATUS_PRODUCT, $data) | |
| 32 | + || array_key_exists(SettingsConfig::SYNC_STATUS_PRODUCT_CREATE, $data) | |
| 33 | + || array_key_exists(SettingsConfig::SYNC_STATUS_PRODUCT_UPDATE, $data); | |
| 34 | + $candidateSettings = SettingsManager::sanitizeSettings($data); | |
| 35 | + | |
| 36 | + if ( | |
| 37 | + (!$syncToggleSubmitted || $candidateSettings[SettingsConfig::SYNC_STATUS_PRODUCT_UPDATE]) | |
| 38 | + && !SettingsManager::isProductUpdateSelectionValid($data) | |
| 39 | + ) { | |
| 40 | + self::pushValidationError(SettingsConfig::CUSTOM_PRODUCT_UPDATE_REQUIRED_MESSAGE); | |
| 41 | + return false; | |
| 42 | + } | |
| 43 | + | |
| 19 | 44 | SettingsManager::updateSettings($data); |
| 20 | 45 | |
| 21 | - if (!empty($data[SettingsConfig::DEVELOPER_MODE]) && $data[SettingsConfig::DEVELOPER_MODE] === 'true') { | |
| 22 | - $debugTask = new Debug(); | |
| 23 | - $debugTask->schedule(); | |
| 24 | - } else { | |
| 25 | - (new CancelDebug())(); | |
| 46 | + if (array_key_exists(SettingsConfig::AUTO_FULL_UPDATE_INTERVAL, $data)) { | |
| 47 | + (new ScheduledProductSync())->ensureScheduled(); | |
| 26 | 48 | } |
| 49 | + | |
| 50 | + if (array_key_exists(SettingsConfig::DEVELOPER_MODE, $data)) { | |
| 51 | + if ($data[SettingsConfig::DEVELOPER_MODE] === 'true') { | |
| 52 | + $debugTask = new Debug(); | |
| 53 | + $debugTask->schedule(); | |
| 54 | + } else { | |
| 55 | + (new CancelDebug())(); | |
| 56 | + } | |
| 57 | + } | |
| 27 | 58 | } |
| 28 | 59 | |
| 29 | 60 | if (isset($_POST['get_token']) && $_POST['get_token'] == 1) { |
| 30 | 61 | self::redirectToOAuth(); |
| 31 | 62 | } |
| 63 | + | |
| 64 | + return true; | |
| 32 | 65 | } |
| 33 | 66 | |
| 67 | + public static function pullValidationError(): string | |
| 68 | + { | |
| 69 | + $userId = get_current_user_id(); | |
| 70 | + if ($userId <= 0) return ''; | |
| 71 | + | |
| 72 | + $key = self::VALIDATION_ERROR_TRANSIENT_PREFIX . $userId; | |
| 73 | + $message = get_transient($key); | |
| 74 | + delete_transient($key); | |
| 75 | + | |
| 76 | + return is_string($message) ? $message : ''; | |
| 77 | + } | |
| 78 | + | |
| 79 | + private static function pushValidationError(string $message): void | |
| 80 | + { | |
| 81 | + $userId = get_current_user_id(); | |
| 82 | + if ($userId <= 0) return; | |
| 83 | + | |
| 84 | + set_transient( | |
| 85 | + self::VALIDATION_ERROR_TRANSIENT_PREFIX . $userId, | |
| 86 | + wp_strip_all_tags($message), | |
| 87 | + 2 * MINUTE_IN_SECONDS | |
| 88 | + ); | |
| 89 | + } | |
| 90 | + | |
| 34 | 91 | private static function redirectToOAuth() |
| 35 | 92 | { |
| 36 | 93 | $OAuthManger = new OAuthManager(); |
| 37 | 94 | $oauthUrls = $OAuthManger->getOAuthUrls(); |
| @@ -38,9 +95,15 @@ | ||
| 38 | 95 | |
| 39 | 96 | // Mark this authorization as started by the current (authenticated) admin |
| 40 | 97 | // so the OAuth callback can reject forged requests. This runs only after |
| 41 | 98 | // the nonce-protected settings POST, so it cannot be triggered cross-site. |
| 42 | - OAuthManager::issueOauthState(); | |
| 99 | + if (! OAuthManager::issueOauthState()) { | |
| 100 | + wp_die( | |
| 101 | + esc_html__('امکان شروع فرایند دریافت دسترسی وجود ندارد. لطفاً دوباره تلاش کنید.', 'sync-basalam'), | |
| 102 | + esc_html__('خطای دریافت دسترسی', 'sync-basalam'), | |
| 103 | + ['response' => 500] | |
| 104 | + ); | |
| 105 | + } | |
| 43 | 106 | |
| 44 | 107 | wp_redirect($oauthUrls['url_req_token']); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Intentional external/user-provided redirect. |
| 45 | 108 | exit(); |
| 46 | 109 | } |