'نام کاربری و رمز عبور را وارد کنید.']); } // Keep credentials and tokens out of the general request logger. $response = wp_remote_post(POSTEXKETAB_API_PATH . '/shipping/token', [ 'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'], 'body' => wp_json_encode([ 'username' => $username, 'password' => $password, 'regenerate' => false, ]), 'timeout' => 30, 'redirection' => 0, ]); if (is_wp_error($response)) { wp_send_json_error(['message' => 'ارتباط با پنل خانه کتاب برقرار نشد. دوباره تلاش کنید.']); } $status = wp_remote_retrieve_response_code($response); $body = json_decode(wp_remote_retrieve_body($response), true); if ($status < 200 || $status >= 300 || !is_array($body) || ($body['IsSuccess'] ?? false) !== true) { $message = is_array($body) && isset($body['Message']) && is_string($body['Message']) ? sanitize_text_field($body['Message']) : 'دریافت کلید از پنل خانه کتاب ناموفق بود.'; wp_send_json_error(['message' => $message]); } $data = $body['Data'] ?? null; if (!is_array($data) || ($data['IsEnabled'] ?? false) !== true) { wp_send_json_error(['message' => 'حساب ارسال پنل خانه کتاب فعال نیست.']); } $api_key = $data['ApiKey'] ?? $data['Token'] ?? ''; if (!is_string($api_key) || trim($api_key) === '') { wp_send_json_error(['message' => 'کلید API در پاسخ پنل خانه کتاب موجود نیست.']); } // The token endpoint has authenticated this key; skip a second validation request. $priority = has_filter('sanitize_option_postex_api_key', 'postexketab_validate_api_key'); if ($priority !== false) { remove_filter('sanitize_option_postex_api_key', 'postexketab_validate_api_key', $priority); } $api_key = trim($api_key); update_option('postex_api_key', $api_key); if ($priority !== false) { add_filter('sanitize_option_postex_api_key', 'postexketab_validate_api_key', $priority); } if (get_option('postex_api_key') !== $api_key) { wp_send_json_error(['message' => 'ذخیره کلید API انجام نشد. دوباره تلاش کنید.']); } update_option('postex_api_key_valid', 'yes'); update_option('postex_shipping_enabled', 'yes'); delete_transient('postex_api_key_error'); wp_send_json_success(['redirect' => admin_url('admin.php?page=postexketab-setup&step=zones')]); } add_action('admin_post_postex_save_wizard_api_key', 'postexketab_save_wizard_api_key'); function postexketab_save_wizard_api_key() { if (!current_user_can('manage_woocommerce')) { wp_die('دسترسی غیر مجاز'); } check_admin_referer('postex_save_wizard_api_key', 'postex_nonce'); $api_key = postexketab_get_wizard_post_text('postex_api_key'); update_option('postex_api_key', $api_key); $next_step = get_option('postex_api_key_valid') === 'yes' ? 'zones' : 'api'; wp_safe_redirect(admin_url('admin.php?page=postexketab-setup&step=' . $next_step)); exit; } //Wizard Fixed add_action('admin_post_postex_save_fixed_settings', 'postexketab_save_fixed_settings'); function postexketab_save_fixed_settings() { if (!current_user_can('manage_woocommerce')) { wp_die('Access denied'); } $nonce = postexketab_get_wizard_post_text('postex_nonce'); if ($nonce === '' || !wp_verify_nonce($nonce, 'postex_general_nonce')) {wp_die('Invalid nonce');} // Normal delivery update_option('postex_normal_delivery_status', postexketab_get_wizard_post_text('postex_normal_delivery_status')); update_option( 'postex_normal_delivery_label', postexketab_get_wizard_post_text('postex_normal_delivery_label') ); update_option( 'postex_normal_delivery_price', postexketab_get_wizard_post_price('postex_normal_delivery_price', 1000000) ); // Fast delivery update_option('postex_fast_delivery_status', postexketab_get_wizard_post_text('postex_fast_delivery_status')); update_option( 'postex_fast_delivery_label', postexketab_get_wizard_post_text('postex_fast_delivery_label') ); update_option( 'postex_fast_delivery_price', postexketab_get_wizard_post_price('postex_fast_delivery_price', 500000) ); wp_safe_redirect(admin_url('admin.php?page=postexketab-setup&step=free')); exit; } //Wizard Free add_action('admin_post_postex_save_free_settings', 'postexketab_save_free_settings'); function postexketab_save_free_settings() { if (!current_user_can('manage_woocommerce')) { wp_die('دسترسی غیر مجاز'); } check_admin_referer('postex_general_nonce'); update_option('postex_free_shipping_status', postexketab_get_wizard_post_text('postex_free_shipping_status', 'off')); update_option('postex_free_shipping_label', postexketab_get_wizard_post_text('postex_free_shipping_label')); update_option('postex_free_shipping_amount', postexketab_get_wizard_post_price('postex_free_shipping_amount')); update_option('postex_in_town_state', postexketab_get_wizard_post_text('postex_in_town_state')); update_option('postex_in_town_city', postexketab_get_wizard_post_text('postex_in_town_city')); update_option('postex_in_town_city_label', postexketab_get_wizard_post_text('postex_in_town_city_label')); update_option('postex_free_shipping_status_suburban', postexketab_get_wizard_post_text('postex_free_shipping_status_suburban', 'off')); update_option('postex_free_shipping_label_suburban', postexketab_get_wizard_post_text('postex_free_shipping_label_suburban')); update_option('postex_free_shipping_amount_suburban', postexketab_get_wizard_post_price('postex_free_shipping_amount_suburban')); wp_safe_redirect( admin_url('admin.php?page=postexketab-setup&step=defaults') ); exit; } //Wizard defaults add_action('admin_post_postex_save_defaults_settings', 'postexketab_save_defaults_settings'); function postexketab_save_defaults_settings() { if ( ! current_user_can('manage_woocommerce') ) { wp_die('دسترسی غیر مجاز'); } check_admin_referer('postex_general_nonce'); update_option('postex_default_payment', postexketab_get_wizard_post_text('postex_default_payment')); update_option('postex_default_shipping_method', postexketab_get_wizard_post_text('postex_default_shipping_method')); update_option('postex_default_collect_type', postexketab_get_wizard_post_text('postex_default_collect_type')); $additional = postexketab_value_added_services_enabled() && is_array($_POST['postex_default_additional'] ?? null) ? postexketab_sanitize_array_recursive(wp_unslash($_POST['postex_default_additional'])) : []; update_option('postex_default_additional', $additional); update_option('postex_default_box', postexketab_get_wizard_post_text('postex_default_box')); wp_safe_redirect(admin_url('admin.php?page=postexketab-setup&step=done')); exit; }