get_settings(); return !empty($settings['site_key']) ? trim($settings['site_key']) : ''; } /** * Get Secret Key. * * @return string */ public function get_secret_key() { $settings = $this->get_settings(); return !empty($settings['secret_key']) ? trim($settings['secret_key']) : ''; } /** * Check if a specific form protection is enabled. * * @param string $key * @return bool */ public function is_enabled($key) { $settings = $this->get_settings(); return !empty($settings[$key]) && $settings[$key] === '1'; } /** * Verify captcha response token. * * @param string|null $token * @return array */ public function verify($token = null) { if (!is_null($this->cached_result)) { return $this->cached_result; } if (is_null($token)) { if (isset($_POST['darban-captcha-response'])) { $token = sanitize_text_field(wp_unslash($_POST['darban-captcha-response'])); } elseif (isset($_POST['shielda-captcha-response'])) { $token = sanitize_text_field(wp_unslash($_POST['shielda-captcha-response'])); } } $secret_key = $this->get_secret_key(); if (empty($token)) { $result = array( 'success' => false, 'message' => __('لطفاً چالش امنیتی شیلدا (کپچا) را حل کنید.', 'shielda-captcha'), 'code' => 'missing_token' ); $this->cached_result = $result; return $result; } if (empty($secret_key)) { $result = array( 'success' => false, 'message' => __('کلید امنیتی (Secret Key) شیلدا در تنظیمات وارد نشده است.', 'shielda-captcha'), 'code' => 'missing_secret' ); $this->cached_result = $result; return $result; } $verify_endpoint = SHIELDA_VERIFY_API_URL; $payload = array( 'secret' => $secret_key, 'response' => $token, ); $response = wp_remote_post($verify_endpoint, array( 'method' => 'POST', 'timeout' => 10, 'redirection' => 5, 'httpversion' => '1.1', 'blocking' => true, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => wp_json_encode($payload), )); if (is_wp_error($response)) { $result = array( 'success' => false, 'message' => sprintf(__('خطا در ارتباط با سرور اعتبارسنجی شیلدا: %s', 'shielda-captcha'), $response->get_error_message()), 'code' => 'connection_error' ); $this->cached_result = $result; return $result; } $response_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if ($response_code !== 200 || !is_array($data) || empty($data['success'])) { $error_info = ''; if (!empty($data['error_codes']) && is_array($data['error_codes'])) { $error_info = ' (' . implode(', ', $data['error_codes']) . ')'; } $result = array( 'success' => false, 'message' => __('پاسخ اعتبارسنجی کپچا نامعتبر است یا زمان آن منقضی شده است.', 'shielda-captcha') . $error_info, 'code' => 'invalid_captcha', 'raw' => $data ); $this->cached_result = $result; return $result; } $result = array( 'success' => true, 'message' => __('اعتبارسنجی با موفقیت انجام شد.', 'shielda-captcha'), 'challenge_ts' => isset($data['challenge_ts']) ? $data['challenge_ts'] : '', 'hostname' => isset($data['hostname']) ? $data['hostname'] : '', ); $this->cached_result = $result; return $result; } }