Initial commit.
This commit is contained in:
159
includes/class-settings.php
Normal file
159
includes/class-settings.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
/**
|
||||
* @package CleverOgre
|
||||
* @subpackage OgreIssuu
|
||||
* @version 0.1.0
|
||||
* @since 0.1.0
|
||||
*/
|
||||
|
||||
namespace OgreIssuu;
|
||||
|
||||
if (!defined('ABSPATH')) exit;
|
||||
|
||||
class Settings {
|
||||
use Singleton;
|
||||
|
||||
private const PARENT_SLUG = 'options-general.php';
|
||||
private const CAPABILITY = 'manage_options';
|
||||
|
||||
private array $options;
|
||||
|
||||
public function __construct() {
|
||||
add_action('admin_menu', [$this, 'admin_menu'], 20);
|
||||
add_action('admin_init', [$this, 'register_settings']);
|
||||
add_filter('plugin_action_links_' . plugin_basename(Plugin::instance()->get_file()), [$this, 'plugin_action_links'], 10, 1);
|
||||
}
|
||||
|
||||
private function get_menu_slug():string {
|
||||
return Plugin::instance()->get_id();
|
||||
}
|
||||
|
||||
private function get_menu_title():string {
|
||||
return Plugin::instance()->get_title();
|
||||
}
|
||||
|
||||
private function get_page_title():string {
|
||||
return sprintf(
|
||||
Plugin::__('%s Settings'),
|
||||
Plugin::instance()->get_title()
|
||||
);
|
||||
}
|
||||
|
||||
// Register settings page
|
||||
public function admin_menu():void {
|
||||
add_submenu_page(
|
||||
self::PARENT_SLUG,
|
||||
$this->get_page_title(),
|
||||
$this->get_menu_title(),
|
||||
self::CAPABILITY,
|
||||
$this->get_menu_slug(),
|
||||
[$this, 'render']
|
||||
);
|
||||
}
|
||||
|
||||
// Register settings fields
|
||||
|
||||
private function get_option_group():string {
|
||||
return Plugin::instance()->get_id();
|
||||
}
|
||||
|
||||
private function get_option_name():string {
|
||||
return Plugin::instance()->get_id();
|
||||
}
|
||||
|
||||
public function register_settings():void {
|
||||
register_setting($this->get_option_group(), $this->get_option_name(), [
|
||||
'type' => 'array',
|
||||
'label' => $this->get_page_title(),
|
||||
'sanitize_callback' => [$this, 'sanitize'],
|
||||
'default' => [
|
||||
'client_id' => '',
|
||||
'client_secret' => '',
|
||||
'token' => '',
|
||||
],
|
||||
]);
|
||||
|
||||
add_settings_section(
|
||||
'api',
|
||||
Plugin::__('API Settings'),
|
||||
function () {
|
||||
printf(
|
||||
'<p>%s</p>',
|
||||
Plugin::__('See <a href="https://help.issuu.com/hc/en-us/articles/23059561681563-Issuu-API-v2-Getting-Started">Issuu API documentation</a> to learn how to generate an API client with your Issuu account.')
|
||||
);
|
||||
},
|
||||
$this->get_menu_slug()
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'client_id',
|
||||
Plugin::__('Client ID'),
|
||||
[$this, 'the_field'],
|
||||
$this->get_menu_slug(),
|
||||
'api'
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'client_secret',
|
||||
Plugin::__('Client Secret'),
|
||||
[$this, 'the_field'],
|
||||
$this->get_menu_slug(),
|
||||
'api'
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'token',
|
||||
Plugin::__('Token'),
|
||||
[$this, 'the_field'],
|
||||
$this->get_menu_slug(),
|
||||
'api'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
public function the_field(array $args):void {
|
||||
printf(
|
||||
'<input id="%s" name="%s[%s]" type="text" value="%s" />',
|
||||
esc_attr($args['name']),
|
||||
esc_attr($args['option_group']),
|
||||
esc_attr($args['name']),
|
||||
esc_attr($this->options[$args['name']] ?? '')
|
||||
);
|
||||
if (isset($args['description']) && !empty($args['description'])) {
|
||||
printf('<p class="description">%s</p>', esc_html($args['description']));
|
||||
}
|
||||
}
|
||||
|
||||
// Output settings page
|
||||
|
||||
public function render():void {
|
||||
$this->options = get_option($this->get_option_group());
|
||||
echo '<div class="wrap">';
|
||||
printf('<h1>%s</h1>', esc_html($this->get_page_title()));
|
||||
echo '<form method="post" action="options.php">';
|
||||
settings_fields($this->get_option_group());
|
||||
do_settings_sections($this->get_menu_slug());
|
||||
submit_button();
|
||||
echo '</form>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
// Add plugin link
|
||||
public function plugin_action_links(array $links):array {
|
||||
array_unshift($links, sprintf(
|
||||
'<a href="%s">%s</a>',
|
||||
esc_url($this->get_url()),
|
||||
Plugin::esc_html__('Settings')
|
||||
));
|
||||
return $links;
|
||||
}
|
||||
|
||||
public function get_url():string {
|
||||
return add_query_arg(array_filter([
|
||||
'page' => $this->get_menu_slug(),
|
||||
]), admin_url(self::PARENT_SLUG));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Settings::instance();
|
||||
Reference in New Issue
Block a user