170 lines
4.8 KiB
PHP
170 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* @package plugin
|
|
* @author cleverogre
|
|
* @copyright 2026 CleverOgre
|
|
* @license GLP-3.0-or-later
|
|
* @version 0.0.0
|
|
* @since 0.0.0
|
|
*/
|
|
|
|
namespace Ogre\Example;
|
|
|
|
use Ogre\Singleton;
|
|
use Ogre\Example as Plugin;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
final class Settings {
|
|
use Singleton;
|
|
|
|
const CAPABILITY = 'manage_options';
|
|
|
|
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;
|
|
}
|
|
|
|
private function is_checked(string $name, string $value):bool {
|
|
return in_array($value, (array) $this->get($name));
|
|
}
|
|
|
|
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::__('General'),
|
|
[$this, 'section'],
|
|
Plugin::get_id(),
|
|
[
|
|
'description' => Plugin::__('General settings for this plugin.'),
|
|
]
|
|
);
|
|
|
|
add_settings_field(
|
|
Plugin::get_id() . '_text',
|
|
Plugin::__('Text'),
|
|
[$this, 'field'],
|
|
Plugin::get_id(),
|
|
args: [
|
|
'name' => 'text',
|
|
'placeholder' => Plugin::__('Insert some text here...'),
|
|
'description' => Plugin::__('An example field.'),
|
|
]
|
|
);
|
|
}
|
|
|
|
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 function checkbox_field(array $args):void {
|
|
extract($args);
|
|
|
|
echo implode('<br>', array_map(
|
|
fn (object $option):string => sprintf(
|
|
'<label><input type="checkbox" name="%s[%s][]" value="%s" %s> %s (%s)</label>',
|
|
esc_attr(Plugin::get_id()),
|
|
esc_attr($name),
|
|
esc_attr($option->name),
|
|
checked($this->is_checked($name, $option->name), display: false),
|
|
esc_html($option->label),
|
|
esc_html($option->name)
|
|
),
|
|
$options
|
|
));
|
|
}
|
|
|
|
public static function get_url():string {
|
|
return admin_url('options-general.php?page=' . Plugin::get_id());
|
|
}
|
|
|
|
}
|
|
|
|
Settings::instance();
|