Improve settings
This commit is contained in:
@@ -12,6 +12,8 @@ namespace Ogre\Sort;
|
|||||||
|
|
||||||
use Ogre\Singleton;
|
use Ogre\Singleton;
|
||||||
use Ogre\Sort as Plugin;
|
use Ogre\Sort as Plugin;
|
||||||
|
use WP_Post_Type;
|
||||||
|
use WP_Taxonomy;
|
||||||
|
|
||||||
defined('ABSPATH') || exit;
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
@@ -29,21 +31,24 @@ final class Settings {
|
|||||||
public function register_relationships(array $relationships):array {
|
public function register_relationships(array $relationships):array {
|
||||||
// Post Types
|
// Post Types
|
||||||
foreach ($this->get('post_types') as $post_type) {
|
foreach ($this->get('post_types') as $post_type) {
|
||||||
$relationships[] = ['post_type' => $post_type];
|
$relationships[] = [
|
||||||
|
'post_type' => $post_type,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Taxonomies
|
// Taxonomies
|
||||||
foreach ($this->get('taxonomies') as $taxonomy) {
|
foreach ($this->get('taxonomies') as $taxonomy) {
|
||||||
$relationships[] = ['taxonomy' => $taxonomy];
|
$relationships[] = [
|
||||||
|
'taxonomy' => $taxonomy,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Taxonomy Posts
|
// Taxonomy Posts
|
||||||
foreach ($this->get('taxonomy_post_types') as $key) {
|
foreach ($this->get_post_types('names') as $post_type) {
|
||||||
$parts = explode('~', $key);
|
foreach ($this->get($post_type) as $taxonomy) {
|
||||||
if (count($parts) == 2) {
|
$relationships[] = [
|
||||||
$relationships = [
|
'post_type' => $post_type,
|
||||||
'post_type' => $parts[0],
|
'taxonomy' => $taxonomy,
|
||||||
'taxonomy' => $parts[1],
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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']);
|
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() {
|
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_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(
|
add_settings_field(
|
||||||
Plugin::get_id() . '_post_type_' . $post_type->name,
|
Plugin::get_id() . '_post_type_' . $post_type->name,
|
||||||
$post_type->label,
|
sprintf(Plugin::__('%s Taxonomies'), $post_type->labels->singular_name),
|
||||||
[$this, 'checkbox'],
|
[$this, 'field'],
|
||||||
Plugin::get_id(),
|
Plugin::get_id(),
|
||||||
'post_types',
|
args: [
|
||||||
[
|
'name' => $post_type->name,
|
||||||
'name' => 'post_types',
|
'options' => $object_taxonomies,
|
||||||
'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,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,9 +175,6 @@ final class Settings {
|
|||||||
public function page() {
|
public function page() {
|
||||||
if (!current_user_can(self::CAPABILITY)) return;
|
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
|
// Show error/update messages
|
||||||
settings_errors(Plugin::get_id());
|
settings_errors(Plugin::get_id());
|
||||||
?>
|
?>
|
||||||
@@ -145,12 +193,18 @@ final class Settings {
|
|||||||
|
|
||||||
public function field(array $args):void {
|
public function field(array $args):void {
|
||||||
extract($args);
|
extract($args);
|
||||||
printf(
|
|
||||||
'<input type="checkbox" name="%s[]" value="%s" %s>',
|
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($name),
|
||||||
esc_attr($value),
|
esc_attr($option->name),
|
||||||
checked($this->is_checked($name, $value), display: false)
|
checked($this->is_checked($name, $option->name), display: false),
|
||||||
);
|
esc_html($option->label)
|
||||||
|
),
|
||||||
|
$options
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function get_url():string {
|
public static function get_url():string {
|
||||||
|
|||||||
Reference in New Issue
Block a user