Files
ogre-captcha/inc/class-settings.php
T
Cooper Dalrymple aaff0e5c41
Validate Build / validate-build (push) Failing after 1m3s
Add API connection settings
2026-06-04 13:12:10 -05:00

184 lines
5.3 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;
defined('ABSPATH') || exit;
final class Settings {
use Singleton;
const CAPABILITY = 'manage_options';
const DEFAULT_INSTANCE_URL = 'https://captcha.cleverogre.com';
protected function __construct() {
add_action('admin_menu', [$this, 'menu']);
add_action('admin_init', [$this, 'init']);
}
private function get(string $key = '') {
$data = (array) get_option(Plugin::get_id(), []);
if (!empty($key)) return array_key_exists($key, $data) ? $data[$key] : null;
return $data;
}
public function get_instance_url():string {
return !empty($value = (string) $this->get('instance_url')) ? $value : self::DEFAULT_INSTANCE_URL;
}
public function get_site_key():string {
return (string) $this->get('site_key');
}
public function get_secret_key():string {
return (string) $this->get('secret_key');
}
public function menu() {
if (!current_user_can(self::CAPABILITY)) return;
add_options_page(sprintf(Plugin::__('%s Settings'), Plugin::get_title()), Plugin::get_title(), self::CAPABILITY, Plugin::get_id(), [$this, 'page']);
}
public function init() {
register_setting(Plugin::get_id(), Plugin::get_id());
add_settings_section(
'default',
Plugin::__('Cap API Credentials'),
[$this, 'section'],
Plugin::get_id(),
[
'description' => Plugin::__('The configuration settings for your Cap CAPTCHA API connection.'),
]
);
add_settings_field(
Plugin::get_id() . '_instance_url',
Plugin::__('Instance URL'),
[$this, 'field'],
Plugin::get_id(),
args: [
'name' => 'instance_url',
'placeholder' => Plugin::__('https://captcha.cleverogre.com/'),
'description' => Plugin::__('Provide the base URL to an instance of Cap. Defaults to CleverOgre\'s CAPTCHA server.'),
]
);
add_settings_field(
Plugin::get_id() . '_site_key',
Plugin::__('Site key'),
[$this, 'field'],
Plugin::get_id(),
args: [
'name' => 'site_key',
'description' => Plugin::__('Provide your Cap site key.'),
]
);
add_settings_field(
Plugin::get_id() . '_secret_key',
Plugin::__('Secret key'),
[$this, 'field'],
Plugin::get_id(),
args: [
'name' => 'secret_key',
'description' => Plugin::__('Provide your Cap secret key.'),
]
);
}
public function page() {
if (!current_user_can(self::CAPABILITY)) return;
// Show error/update messages
settings_errors(Plugin::get_id());
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post"><?php
settings_fields(Plugin::get_id());
do_settings_sections(Plugin::get_id());
submit_button();
?></form>
</div>
<?php
}
public function section(array $args):void {
if (!empty($args['description'] ?? '')) {
echo wpautop($args['description']);
}
}
public function field(array $args):void {
if (empty($args['name'] ?? '')) return;
$attributes = [
'type' => 'text',
'class' => 'regular-text',
'name' => sprintf(
'%s[%s]',
Plugin::get_id(),
$args['name']
),
'id' => $args['name'],
'value' => (string) $this->get($args['name']),
];
if ($args['readonly'] ?? false) $attributes['readonly'] = 'readonly';
if ($args['required'] ?? false) $attributes['required'] = 'required';
if (!empty($args['placeholder'] ?? '')) {
$attributes['placeholder'] = $args['placeholder'];
}
if (!empty($args['description'] ?? '')) {
$attributes['describedby'] = $args['name'] . '-description';
}
if (isset($args['attributes']) && is_array($args['attributes'])) {
$attributes = array_merge($attributes, $args['attributes']);
}
printf(
'<input %s />',
implode(
' ',
array_map(
fn (string $name, string $value): string => sprintf(
'%s="%s"',
$name,
esc_attr($value)
),
array_keys($attributes),
array_values($attributes)
)
)
);
if (!empty($args['description'] ?? '')) {
printf(
'<p class="description" id="%s-description">%s</p>',
esc_attr($args['name']),
nl2br($args['description'])
);
}
}
public static function get_url():string {
return admin_url('options-general.php?page=' . Plugin::get_id());
}
}
Settings::instance();