Initial commit
This commit is contained in:
160
inc/class-settings.php
Normal file
160
inc/class-settings.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-sort
|
||||
* @author cleverogre
|
||||
* @version 1.0.0
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Sort;
|
||||
|
||||
use Ogre\Singleton;
|
||||
use Ogre\Sort as Plugin;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class Settings {
|
||||
use Singleton;
|
||||
|
||||
const CAPABILITY = 'manage_options';
|
||||
|
||||
protected function __construct() {
|
||||
add_filter('ogre_sort_relationships', [$this, 'register_relationships'], 10, 1);
|
||||
add_action('admin_menu', [$this, 'menu']);
|
||||
add_action('admin_init', [$this, 'init']);
|
||||
}
|
||||
|
||||
public function register_relationships(array $relationships):array {
|
||||
// Post Types
|
||||
foreach ($this->get('post_types') as $post_type) {
|
||||
$relationships[] = ['post_type' => $post_type];
|
||||
}
|
||||
|
||||
// Taxonomies
|
||||
foreach ($this->get('taxonomies') as $taxonomy) {
|
||||
$relationships[] = ['taxonomy' => $taxonomy];
|
||||
}
|
||||
|
||||
// Taxonomy Posts
|
||||
foreach ($this->get('taxonomy_post_types') as $key) {
|
||||
$parts = explode('~', $key);
|
||||
if (count($parts) == 2) {
|
||||
$relationships = [
|
||||
'post_type' => $parts[0],
|
||||
'taxonomy' => $parts[1],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $relationships;
|
||||
}
|
||||
|
||||
private function get(string $key = ''):array {
|
||||
$data = (array) get_option(Plugin::get_id(), []);
|
||||
if (!empty($key)) return array_key_exists($key, $data) ? (array) $data[$key] : [];
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function is_checked(string $name, string $value):bool {
|
||||
return is_array($option = $this->get($name)) && in_array($value, $option);
|
||||
}
|
||||
|
||||
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('options', Plugin::get_id());
|
||||
|
||||
add_settings_section('post_types', Plugin::__('Post Types'), [$this, 'section'], Plugin::get_id());
|
||||
|
||||
foreach (get_post_types(output: 'objects') as $post_type) {
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_post_type_' . $post_type->name,
|
||||
$post_type->label,
|
||||
[$this, 'checkbox'],
|
||||
Plugin::get_id(),
|
||||
'post_types',
|
||||
[
|
||||
'name' => 'post_types',
|
||||
'value' => $post_type->name,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
add_settings_section('taxonomies', Plugin::__('Taxonomies'), [$this, 'section'], Plugin::get_id());
|
||||
|
||||
foreach (get_taxonomies(output: 'objects') as $taxonomy) {
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_taxonomy_' . $post_type->name,
|
||||
$post_type->label,
|
||||
[$this, 'checkbox'],
|
||||
Plugin::get_id(),
|
||||
'taxonomies',
|
||||
[
|
||||
'name' => 'taxonomies',
|
||||
'value' => $taxonomy->name,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
add_settings_section('taxonomy_post_types', Plugin::__('Taxonomy Posts'), [$this, 'section'], Plugin::get_id());
|
||||
|
||||
foreach (get_post_types(output: 'objects') as $post_type) {
|
||||
foreach (get_object_taxonomies($post_type->name, 'objects') as $taxonomy) {
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_post_type_' . $post_type->name . '_taxonomy_' . $taxonomy->name,
|
||||
sprintf('%s: %s', $post_type->label, $taxonomy->label),
|
||||
[$this, 'checkbox'],
|
||||
Plugin::get_id(),
|
||||
'taxonomy_post_types',
|
||||
[
|
||||
'name' => 'taxonomy_post_types',
|
||||
'value' => $post_type->name . '~' . $taxonomy->name,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function page() {
|
||||
if (!current_user_can(self::CAPABILITY)) return;
|
||||
|
||||
// Show update message
|
||||
if (isset($_GET['settings-updated'])) add_settings_error(Plugin::get_id(), 'updated', Plugin::__('Settings Saved'), 'updated');
|
||||
|
||||
// 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():void { }
|
||||
|
||||
public function field(array $args):void {
|
||||
extract($args);
|
||||
printf(
|
||||
'<input type="checkbox" name="%s[]" value="%s" %s>',
|
||||
esc_attr($name),
|
||||
esc_attr($value),
|
||||
checked($this->is_checked($name, $value), display: false)
|
||||
);
|
||||
}
|
||||
|
||||
public static function get_url():string {
|
||||
return admin_url('options-general.php?page=' . Plugin::get_id());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Settings::instance();
|
||||
Reference in New Issue
Block a user