| 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 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class SettingsPageHandler |
| 13 |
{ |
| 14 |
private const VALIDATION_ERROR_TRANSIENT_PREFIX = 'sync_basalam_settings_validation_error_'; |
| 15 |
|
| 16 |
public static function saveSettings() |
| 17 |
{ |
| 18 |
$data = isset($_POST['sync_basalam_settings']) ? array_map('sanitize_text_field', wp_unslash($_POST['sync_basalam_settings'])) : []; |
| 19 |
|
| 20 |
if ($data) { |
| 21 |
if (!SettingsManager::isProductUpdateSelectionValid($data)) { |
| 22 |
self::pushValidationError(SettingsConfig::CUSTOM_PRODUCT_UPDATE_REQUIRED_MESSAGE); |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
SettingsManager::updateSettings($data); |
| 27 |
|
| 28 |
if (!empty($data[SettingsConfig::DEVELOPER_MODE]) && $data[SettingsConfig::DEVELOPER_MODE] === 'true') { |
| 29 |
$debugTask = new Debug(); |
| 30 |
$debugTask->schedule(); |
| 31 |
} else { |
| 32 |
(new CancelDebug())(); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if (isset($_POST['get_token']) && $_POST['get_token'] == 1) { |
| 37 |
self::redirectToOAuth(); |
| 38 |
} |
| 39 |
|
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
public static function pullValidationError(): string |
| 44 |
{ |
| 45 |
$userId = get_current_user_id(); |
| 46 |
if ($userId <= 0) return ''; |
| 47 |
|
| 48 |
$key = self::VALIDATION_ERROR_TRANSIENT_PREFIX . $userId; |
| 49 |
$message = get_transient($key); |
| 50 |
delete_transient($key); |
| 51 |
|
| 52 |
return is_string($message) ? $message : ''; |
| 53 |
} |
| 54 |
|
| 55 |
private static function pushValidationError(string $message): void |
| 56 |
{ |
| 57 |
$userId = get_current_user_id(); |
| 58 |
if ($userId <= 0) return; |
| 59 |
|
| 60 |
set_transient( |
| 61 |
self::VALIDATION_ERROR_TRANSIENT_PREFIX . $userId, |
| 62 |
wp_strip_all_tags($message), |
| 63 |
2 * MINUTE_IN_SECONDS |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
private static function redirectToOAuth() |
| 68 |
{ |
| 69 |
$OAuthManger = new OAuthManager(); |
| 70 |
$oauthUrls = $OAuthManger->getOAuthUrls(); |
| 71 |
|
| 72 |
// Mark this authorization as started by the current (authenticated) admin |
| 73 |
// so the OAuth callback can reject forged requests. This runs only after |
| 74 |
// the nonce-protected settings POST, so it cannot be triggered cross-site. |
| 75 |
OAuthManager::issueOauthState(); |
| 76 |
|
| 77 |
wp_redirect($oauthUrls['url_req_token']); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Intentional external/user-provided redirect. |
| 78 |
exit(); |
| 79 |
} |
| 80 |
|
| 81 |
public static function handleOauthCallback() |
| 82 |
{ |
| 83 |
$oauthSaved = OAuthManager::saveOauthData(); |
| 84 |
|
| 85 |
if ($oauthSaved) { |
| 86 |
$webhookService = new WebhookService(); |
| 87 |
$webhookService->setupWebhook(); |
| 88 |
$vendorInfoService = new VendorInfoService(); |
| 89 |
$vendorInfoService->FetchVendorInfo(); |
| 90 |
} |
| 91 |
|
| 92 |
$onboardingCompleted = get_option('sync_basalam_onboarding_completed'); |
| 93 |
|
| 94 |
if (!$onboardingCompleted) { |
| 95 |
wp_safe_redirect(admin_url('admin.php?page=basalam-onboarding&step=3')); |
| 96 |
} else { |
| 97 |
wp_safe_redirect(admin_url('admin.php?page=sync_basalam')); |
| 98 |
} |
| 99 |
exit(); |
| 100 |
} |
| 101 |
} |
| 102 |
|