Files
ogre-issuu/includes/class-settings.php
2025-12-04 11:20:12 -06:00

215 lines
7.3 KiB
PHP

<?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',
['name' => 'client_id']
);
add_settings_field(
'client_secret',
Plugin::__('Client Secret'),
[$this, 'the_field'],
$this->get_menu_slug(),
'api',
['name' => 'client_secret']
);
add_settings_field(
'token',
Plugin::__('Token'),
[$this, 'the_field'],
$this->get_menu_slug(),
'api',
['name' => 'token']
);
add_settings_section(
'help',
Plugin::__('Troubleshooting'),
function () {
?>
<p><strong><em>Is the API no longer working for you? Are your embeds left spinning?</em></strong><br>Don't fret, there is likely an easy solution to your woes.</p>
<ol>
<li>Let's start by checking your browser's console. Visit a page with an embed which isn't loading then follow this guide: <a href="https://appuals.com/open-browser-console/" target="_blank">https://appuals.com/open-browser-console/</a>. If the console reports something along the lines of "Bad request", then there is likely an issue with your Issuu API v2 authorization. Proceed to the next step.</li>
<li>Log in to your Issuu account and access the API tab within your Account settings: <a href="https://issuu.com/home/settings/apicredentials" target="_blank">https://issuu.com/home/settings/apicredentials</a>. If you see an API client that has expired, then you'll definitely need to create a new API client!</li>
<li>Follow the steps outlined on <a href="https://help.issuu.com/hc/en-us/articles/23059561681563-Issuu-API-v2-Getting-Started" target="_blank">Issuu's help guide</a> to create a new API client. You may want to delete the old one while you're at it.</li>
<li>Once you're all done there, copy over all three credentials (Client ID, Client Secret, and Token) to the fields on this page (Settings -> OgreIssuu) under "API Settings" and hit "Save Changes".</li>
<li>Now go back to your website and review the Issuu embeds. If they are working again, you're all good to go! If not, you may need to get in touch with your <a href="https://cleverogre.com/" target="_blank">website developer</a> to investigate further.</li>
</ol>
<?php
},
$this->get_menu_slug()
);
}
public function the_field(array $args):void {
printf(
'<input id="%s" name="%s[%s]" type="text" value="%s" />',
esc_attr($args['name']),
esc_attr($this->get_option_group()),
esc_attr($args['name']),
esc_attr($this->get_field($args['name']))
);
if (isset($args['description']) && !empty($args['description'])) {
printf('<p class="description">%s</p>', esc_html($args['description']));
}
}
public function sanitize(array $input):array {
return array_map('sanitize_text_field', $input);
}
// Output settings page
private function load_options():bool {
if (isset($this->options) && is_array($this->options)) return false;
$this->options = get_option($this->get_option_group());
return true;
}
public function render():void {
$this->load_options();
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));
}
// Field Accessors
public function get_field(string $name, string $default = ''):string {
$this->load_options();
return $this->options[$name] ?? $default;
}
public function set_field(string $name, string $value):bool {
$this->load_options();
$this->options[$name] = sanitize_text_field($value);
return update_option($this->get_option_group(), $this->options);
}
public static function get(string $name, string $default = ''):string {
return self::instance()->get_field($name, $default);
}
public static function set(string $name, string $value):bool {
return self::instance()->set_field($name, $value);
}
public static function exists(string $name):bool {
return !empty(self::get($name));
}
}
Settings::instance();