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