Files
ogre-captcha/inc/abstract-location.php
2026-06-09 13:09:33 -05:00

124 lines
2.8 KiB
PHP

<?php
/**
* @package ogre-captcha
* @author cleverogre
* @copyright 2026 CleverOgre
* @license GLP-3.0-or-later
* @version 1.2.0
* @since 1.0.0
*/
namespace Ogre\Captcha;
use Ogre\Singleton;
use Ogre\Captcha\Settings;
use Override;
use WP_Error;
defined('ABSPATH') || exit;
abstract class Location {
use Singleton;
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|null $errors = null):void {
API::process(Widget::get_token(), $errors);
}
protected function print_errors(WP_Error $errors):void {
if (!$errors->has_errors()) return;
$messages = [];
foreach ($errors->get_error_codes() as $code) {
foreach ($errors->get_error_messages($code) as $message) {
$messages[] = $message;
}
}
ob_start();
echo '<ul><li>' . implode('</li><li>', $messages) . '</li></ul>';
wp_die(ob_get_clean());
}
public function header():void {
if (!Settings::is_widget_floating()) return;
ob_start();
}
public function footer():void {
if (!Settings::is_widget_floating()) return;
$html = ob_get_clean();
$html = Widget::set_button_floating($html);
echo $html;
}
}
abstract class LoginLocation extends Location {
protected string $action;
protected function __construct(string $name, string $action) {
$this->action = $action;
parent::__construct($name);
}
#[Override]
public function activate(): void
{
add_action('login_head', [$this, 'header']);
add_action('login_footer', [$this, 'footer']);
}
private function get_action():string {
return isset($_REQUEST['action']) && is_string($_REQUEST['action']) ? $_REQUEST['action'] : 'login';
}
protected function is_action():bool {
return $this->get_action() == $this->action;
}
#[Override]
public function header():void {
if (!$this->is_action()) return;
parent::header();
}
#[Override]
public function footer():void {
if (!$this->is_action()) return;
parent::footer();
}
}