86 lines
2.1 KiB
PHP
86 lines
2.1 KiB
PHP
<?php
|
|
/**
|
|
* @package ogre-captcha
|
|
* @author cleverogre
|
|
* @copyright 2026 CleverOgre
|
|
* @license GLP-3.0-or-later
|
|
* @version 0.0.0
|
|
* @since 0.0.0
|
|
*/
|
|
|
|
namespace Ogre\Captcha;
|
|
|
|
use Ogre\Singleton;
|
|
use Ogre\Captcha as Plugin;
|
|
use Ogre\Captcha\Settings;
|
|
use WP_Error;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
abstract class Location {
|
|
use Singleton;
|
|
|
|
public const ERROR_EMPTY_TOKEN = 'empty_captcha_token';
|
|
public const ERROR_INVALID_CAPTCHA = 'invalid_captcha_result';
|
|
|
|
protected string $name;
|
|
|
|
protected function __construct(string $name) {
|
|
$this->name = $name;
|
|
add_action('init', [$this, 'init']);
|
|
}
|
|
|
|
public function enabled():bool {
|
|
return Settings::is_form_location_enabled($this->name);
|
|
}
|
|
|
|
public function init():void {
|
|
if (is_multisite()) {
|
|
$blog_id = get_current_blog_id();
|
|
switch_to_blog($blog_id);
|
|
}
|
|
$enabled = $this->enabled();
|
|
if (is_multisite()) {
|
|
restore_current_blog();
|
|
}
|
|
if (!$enabled) return;
|
|
|
|
$this->activate();
|
|
}
|
|
|
|
abstract public function activate():void;
|
|
|
|
public function render():void {
|
|
Widget::render();
|
|
}
|
|
|
|
public function process(WP_Error $errors):void {
|
|
if (!Settings::is_connected()) return;
|
|
|
|
$token = Widget::get_token();
|
|
if (empty($token)) {
|
|
$errors->add(self::ERROR_EMPTY_TOKEN, Plugin::__('<strong>Error:</strong> Please complete CAPTCHA verification.'));
|
|
return;
|
|
}
|
|
|
|
if (!API::verify($token)) {
|
|
$errors->add(self::ERROR_INVALID_CAPTCHA, Plugin::__('<strong>Error:</strong> Your answer was incorrect - please try again.'));
|
|
return;
|
|
}
|
|
}
|
|
|
|
protected function print_errors(WP_Error $errors):void {
|
|
if (!$errors->has_errors()) return;
|
|
ob_start();
|
|
echo '<ul>';
|
|
foreach ($errors->get_error_codes() as $code) {
|
|
foreach ($errors->get_error_messages($code) as $message) {
|
|
echo '<li>' . wp_kses($message, ['strong']) . '</li>';
|
|
}
|
|
}
|
|
echo '</ul>';
|
|
wp_die(ob_get_clean());
|
|
}
|
|
|
|
}
|