File: /home/salamatk/takarzan.ir/wp-content/plugins/luman-plus/Includes/App/Services/RecaptchaService.php
<?php
namespace lumanPlus\App\Services;
use lumanPlus\Core\Helper;
class RecaptchaService
{
protected $version;
protected $siteKey;
protected $secretKey;
public function __construct($version = 'v2')
{
$this->version = $version;
$this->siteKey = Helper::getOption("recaptcha_" . $version . "_site_key");
$this->secretKey = Helper::getOption("recaptcha_" . $version . "_secret_key");
}
public function render()
{
if ($this->version === 'v2') {
return '<div class="g-recaptcha" data-sitekey="' . esc_attr($this->siteKey) . '"></div>';
}
if ($this->version === 'v3') {
return '<input type="hidden" name="recaptcha_response" id="recaptchaResponse">' . PHP_EOL .
'<script src="https://www.google.com/recaptcha/api.js?render=' . esc_attr($this->siteKey) . '"></script>' . PHP_EOL .
'<script>
grecaptcha.ready(function() {
grecaptcha.execute("' . esc_attr($this->siteKey) . '", {action: "form_submit"}).then(function(token) {
document.getElementById("recaptchaResponse").value = token;
});
});
</script>';
}
return '';
}
public function verify($token)
{
$response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', [
'body' => [
'secret' => $this->secretKey,
'response' => $token,
'remoteip' => $_SERVER['REMOTE_ADDR'],
],
]);
if (is_wp_error($response)) {
return false;
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if ($this->version === 'v3') {
return isset($body['success'], $body['score']) && $body['success'] && $body['score'] >= 0.5;
}
return isset($body['success']) && $body['success'];
}
public function getSiteKey()
{
return $this->siteKey;
}
}