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()); ?>