Files
ogre-sort/inc/class-settings.php
Cooper Dalrymple 43faa3df05 Improve settings
2025-11-18 17:50:38 -06:00

217 lines
6.4 KiB
PHP

<?php
/**
* @package ogre-sort
* @author cleverogre
* @copyright 2025 CleverOgre
* @license GLP-3.0-or-later
* @version 1.0.0
* @since 1.0.0
*/
namespace Ogre\Sort;
use Ogre\Singleton;
use Ogre\Sort as Plugin;
use WP_Post_Type;
use WP_Taxonomy;
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_post_types('names') as $post_type) {
foreach ($this->get($post_type) as $taxonomy) {
$relationships[] = [
'post_type' => $post_type,
'taxonomy' => $taxonomy,
];
}
}
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']);
}
private function get_post_types(string $output = 'objects'):array {
$post_types = array_filter(
get_post_types(
[
'show_ui' => true,
'show_in_menu' => true,
],
'objects'
),
fn (WP_Post_Type $post_type):bool => $post_type->name !== 'attachment'
);
if ($output == 'names') {
return wp_list_pluck($post_types, 'name');
} else {
return $post_types;
}
}
private function get_taxonomies(string $output = 'objects'):array {
$taxonomies = array_filter(
get_taxonomies(
[
'show_ui' => true,
],
'objects'
),
fn (WP_Taxonomy $taxonomy):bool => $taxonomy->name !== 'post_format'
);
if ($output == 'names') {
return wp_list_pluck($taxonomies, 'name');
} else {
return $taxonomies;
}
}
private function get_object_taxonomies(string $object_type, string $output = 'objects'):array {
$taxonomies = array_filter(
get_object_taxonomies($object_type, 'objects'),
fn (WP_Taxonomy $taxonomy):bool => $taxonomy->show_ui
);
if ($output == 'names') {
return wp_list_pluck($taxonomies, 'name');
} else {
return $taxonomies;
}
}
public function init() {
$post_types = $this->get_post_types();
$taxonomies = $this->get_taxonomies();
register_setting(Plugin::get_id(), Plugin::get_id());
add_settings_section('default', Plugin::__('Sortable Objects'), [$this, 'section'], Plugin::get_id());
if (!empty($post_types)) {
add_settings_field(
Plugin::get_id() . '_post_types',
Plugin::__('Post Types'),
[$this, 'field'],
Plugin::get_id(),
args: [
'name' => 'post_types',
'options' => $post_types,
]
);
}
if (!empty($taxonomies)) {
add_settings_field(
Plugin::get_id() . '_taxonomies',
Plugin::__('Taxonomies'),
[$this, 'field'],
Plugin::get_id(),
args: [
'name' => 'taxonomies',
'options' => $taxonomies,
]
);
}
if (!empty($post_types)) {
foreach ($post_types as $post_type) {
$object_taxonomies = $this->get_object_taxonomies($post_type->name);
if (!empty($object_taxonomies)) {
add_settings_field(
Plugin::get_id() . '_post_type_' . $post_type->name,
sprintf(Plugin::__('%s Taxonomies'), $post_type->labels->singular_name),
[$this, 'field'],
Plugin::get_id(),
args: [
'name' => $post_type->name,
'options' => $object_taxonomies,
]
);
}
}
}
}
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():void { }
public function 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>&nbsp;%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)
),
$options
));
}
public static function get_url():string {
return admin_url('options-general.php?page=' . Plugin::get_id());
}
}
Settings::instance();