Files
ogre-captcha/inc/class-api.php
T
Cooper Dalrymple 9ad3558ac4
Validate Build / validate-build (push) Failing after 29s
Update version to 1.0.0
2026-06-04 17:52:08 -05:00

56 lines
1.6 KiB
PHP

<?php
/**
* @package ogre-captcha
* @author cleverogre
* @copyright 2026 CleverOgre
* @license GLP-3.0-or-later
* @version 1.0.0
* @since 1.0.0
*/
namespace Ogre\Captcha;
use Ogre\Captcha\Settings;
defined('ABSPATH') || exit;
final class API {
private static function transaction(string $url, array $data):array|string|bool {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, wp_json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'content-type: application/json',
'accept: application/json',
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if ($result === false || !is_string($result) || empty($result)) return 'Invalid response';
$result = json_decode($result, true);
if (!is_array($result) || is_null($result)) return 'Invalid formatting';
if (isset($result['error']) && !empty($result['error'])) return $result['error'];
if (isset($result['success']) && is_bool($result['success'])) return $result['success'];
return $result;
}
private static function get_siteverify_url():string {
if (!Settings::is_connected()) return '';
return Settings::get_instance_url(Settings::get_site_key() . '/siteverify');
}
public static function verify(string $token):bool {
if (empty($token) || !Settings::is_connected()) return false;
$result = self::transaction(self::get_siteverify_url(), [
'secret' => Settings::get_secret_key(),
'response' => $token,
]);
return is_bool($result) ? $result : false;
}
}