Improve settings
This commit is contained in:
@@ -12,6 +12,8 @@ namespace Ogre\Sort;
|
||||
|
||||
use Ogre\Singleton;
|
||||
use Ogre\Sort as Plugin;
|
||||
use WP_Post_Type;
|
||||
use WP_Taxonomy;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
@@ -29,21 +31,24 @@ final class Settings {
|
||||
public function register_relationships(array $relationships):array {
|
||||
// Post Types
|
||||
foreach ($this->get('post_types') as $post_type) {
|
||||
$relationships[] = ['post_type' => $post_type];
|
||||
$relationships[] = [
|
||||
'post_type' => $post_type,
|
||||
];
|
||||
}
|
||||
|
||||
// Taxonomies
|
||||
foreach ($this->get('taxonomies') as $taxonomy) {
|
||||
$relationships[] = ['taxonomy' => $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],
|
||||
foreach ($this->get_post_types('names') as $post_type) {
|
||||
foreach ($this->get($post_type) as $taxonomy) {
|
||||
$relationships[] = [
|
||||
'post_type' => $post_type,
|
||||
'taxonomy' => $taxonomy,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -67,56 +72,102 @@ final class Settings {
|
||||
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() {
|
||||
register_setting('options', Plugin::get_id());
|
||||
$post_types = $this->get_post_types();
|
||||
$taxonomies = $this->get_taxonomies();
|
||||
|
||||
add_settings_section('post_types', Plugin::__('Post Types'), [$this, 'section'], Plugin::get_id());
|
||||
register_setting(Plugin::get_id(), Plugin::get_id());
|
||||
|
||||
foreach (get_post_types(output: 'objects') as $post_type) {
|
||||
add_settings_section('default', Plugin::__('Sortable Objects'), [$this, 'section'], Plugin::get_id());
|
||||
|
||||
if (!empty($post_types)) {
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_post_type_' . $post_type->name,
|
||||
$post_type->label,
|
||||
[$this, 'checkbox'],
|
||||
Plugin::get_id() . '_post_types',
|
||||
Plugin::__('Post Types'),
|
||||
[$this, 'field'],
|
||||
Plugin::get_id(),
|
||||
'post_types',
|
||||
[
|
||||
args: [
|
||||
'name' => 'post_types',
|
||||
'value' => $post_type->name,
|
||||
'options' => $post_types,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
add_settings_section('taxonomies', Plugin::__('Taxonomies'), [$this, 'section'], Plugin::get_id());
|
||||
|
||||
foreach (get_taxonomies(output: 'objects') as $taxonomy) {
|
||||
if (!empty($taxonomies)) {
|
||||
add_settings_field(
|
||||
Plugin::get_id() . '_taxonomy_' . $post_type->name,
|
||||
$post_type->label,
|
||||
[$this, 'checkbox'],
|
||||
Plugin::get_id() . '_taxonomies',
|
||||
Plugin::__('Taxonomies'),
|
||||
[$this, 'field'],
|
||||
Plugin::get_id(),
|
||||
'taxonomies',
|
||||
[
|
||||
args: [
|
||||
'name' => 'taxonomies',
|
||||
'value' => $taxonomy->name,
|
||||
'options' => $taxonomies,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
]
|
||||
);
|
||||
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,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,9 +175,6 @@ final class Settings {
|
||||
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());
|
||||
?>
|
||||
@@ -145,12 +193,18 @@ final class Settings {
|
||||
|
||||
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)
|
||||
);
|
||||
|
||||
echo implode('<br>', array_map(
|
||||
fn (object $option):string => sprintf(
|
||||
'<label><input type="checkbox" name="%s[%s][]" value="%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)
|
||||
),
|
||||
$options
|
||||
));
|
||||
}
|
||||
|
||||
public static function get_url():string {
|
||||
|
||||
Reference in New Issue
Block a user