<?php
|
namespace JVBase\registrar;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
final class Terms {
|
public string $taxonomy;
|
public string $singular;
|
public string $plural;
|
public array $labels;
|
/**
|
* A string or array of strings of object types this taxonomy is for
|
* @var string|array
|
*/
|
public string|array $for;
|
/**
|
* A short descriptive summary of what the taxonomy is for.
|
* @var string
|
*/
|
public string $description;
|
/**
|
* Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.
|
* The default settings of $publicly_queryable, $show_ui, and $show_in_nav_menus are inherited from $public.
|
* @var bool
|
*/
|
public bool $public = true;
|
/**
|
* Whether the taxonomy is publicly queryable.
|
* If not set, the default is inherited from $public
|
* @var bool
|
*/
|
public bool $publicly_queryable;
|
/**
|
* Whether the taxonomy is hierarchical. Default false.
|
* @var bool
|
*/
|
public bool $hierarchical = false;
|
/**
|
* Whether to generate and allow a UI for managing terms in this taxonomy in the admin.
|
* If not set, the default is inherited from $public (default true).
|
* @var bool
|
*/
|
public bool $show_ui;
|
/**
|
* Whether to show the taxonomy in the admin menu. If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown.
|
* $show_ui must be true. If not set, default is inherited from $show_ui (default true).
|
* @var bool
|
*/
|
public bool $show_in_menu;
|
/**
|
* Makes this taxonomy available for selection in navigation menus.
|
* If not set, the default is inherited from $public (default true).
|
* @var bool
|
*/
|
public bool $show_in_nav_menus;
|
/**
|
* Whether to include the taxonomy in the REST API. Set this to true for the taxonomy to be available in the block editor.
|
* @var bool
|
*/
|
public bool $show_in_rest = true;
|
/**
|
* To change the base url of REST API route. Default is $taxonomy.
|
* @var string
|
*/
|
public string $rest_base;
|
/**
|
* To change the namespace URL of REST API route. Default is wp/v2.
|
* @var string
|
*/
|
public string $rest_namespace;
|
/**
|
* REST API Controller class name. Default is ‘WP_REST_Terms_Controller‘.
|
* @var string
|
*/
|
public string $rest_controller_class;
|
/**
|
* Whether to list the taxonomy in the Tag Cloud Widget controls.
|
* If not set, the default is inherited from $show_ui (default true).
|
* @var bool
|
*/
|
public bool $show_tag_cloud;
|
/**
|
* Whether to show the taxonomy in the quick/bulk edit panel.
|
* If not set, the default is inherited from $show_ui (default true).
|
* @var bool
|
*/
|
public bool $show_quick_edit;
|
/**
|
* Whether to display a column for the taxonomy on its post type listing screens. Default false
|
* @var bool
|
*/
|
public bool $show_admin_column;
|
/**
|
* Provide a callback function for the meta box display.
|
* If not set, post_categories_meta_box() is used for hierarchical taxonomies, and post_tags_meta_box() is used for non-hierarchical.
|
* If false, no meta box is shown.
|
* @var mixed
|
*/
|
public mixed $meta_box_cb;
|
/**
|
* Callback function for sanitizing taxonomy data saved from a meta box. If no callback is defined, an appropriate one is determined based on the value of $meta_box_cb.
|
* @var mixed
|
*/
|
public mixed $meta_box_sanitize_cb;
|
/**
|
* Array of capabilities for this taxonomy.
|
* manage_terms {string} - Default 'manage_categories'.
|
* edit_terms {string} - Default 'manage_categories'.
|
* delete_terms {string} - Default 'manage_categories'.
|
* assign_terms {string} - Default 'edit_posts'.
|
* @var array
|
*/
|
public array $capabilities;
|
/**
|
* Triggers the handling of rewrites for this taxonomy. Default true, using $taxonomy as slug. To prevent rewrite, set to false. To specify rewrite rules, an array can be passed with any of these keys:
|
* slug {string} - Customize the permastruct slug. Default $taxonomy key.
|
* with_front {bool} - Should the permastruct be prepended with WP_Rewrite::$front. Default true.
|
* hierarchical {bool}- Either hierarchical rewrite tag or not. Default false.
|
* ep_mask {int} - Assign an endpoint mask. Default EP_NONE.
|
* @var bool|array
|
*/
|
public bool|array $rewrite;
|
/**
|
* Sets the query var key for this taxonomy. Default $taxonomy key.
|
* If false, a taxonomy cannot be loaded at ?{query_var}={term_slug}.
|
* If a string, the query ?{query_var}={term_slug} will be valid.
|
* @var bool|string
|
*/
|
public bool|string $query_var;
|
/**
|
* Works much like a hook, in that it will be called when the count is updated.
|
* Default _update_post_term_count() for taxonomies attached to post types, which confirms that the objects are published before counting them.
|
* Default _update_generic_term_count() for taxonomies attached to other object types, such as users.
|
* @var mixed
|
*/
|
public mixed $update_count_callback;
|
/**
|
* Default term to be used for the taxonomy.
|
* name {string} - Name of the default term
|
* slug {string} - Slug for the default term
|
* description {string} - Description for default term
|
* @var string|array
|
*/
|
public string|array $default_term;
|
/**
|
* Whether terms in this taxonomy should be sorted in the order they are provided to wp_set_object_terms().
|
* Default null which equates to false.
|
* @var bool
|
*/
|
public bool $sort;
|
/**
|
* Array of arguments to automatically use inside wp_get_object_terms() for this taxonomy.
|
* @var array
|
*/
|
public array $args;
|
|
|
public function __construct(string $taxonomy, string $singular, string $plural)
|
{
|
$this->taxonomy = $taxonomy;
|
$this->labels = $this->buildLabels($singular, $plural);
|
}
|
private function buildLabels(string $singular, string $plural): array
|
{
|
return [
|
'name' => $plural,
|
'singular_name' => $singular,
|
'search_items' => "Search {$plural}",
|
'all_items' => "All {$plural}",
|
'edit_item' => "Edit {$singular}",
|
'update_item' => "Update {$singular}",
|
'add_new_item' => "Add New {$singular}",
|
'new_item_name' => "New {$singular} Name",
|
'menu_name' => $singular
|
];
|
}
|
|
public function register():void
|
{
|
|
$args = array_filter(get_object_vars($this));
|
// $args = [
|
// 'labels' => $this->labels,
|
// 'public' => $this->public,
|
// 'hierarchical' => $this->hierarchical,
|
// ];
|
// if (isset($this->description)) {
|
// $args['description'] = $this->description;
|
// }
|
// if (isset($this->publicly_queryable)) {
|
// $args['publicly_queryable'] = $this->publicly_queryable;
|
// }
|
// if (isset($this->show_ui)) {
|
// $args['show_ui'] = $this->show_ui;
|
// }
|
// if (isset($this->show_in_menu)) {
|
// $args['show_in_menu'] = $this->show_in_menu;
|
// }
|
// if (isset($this->show_in_nav_menus)) {
|
// $args['show_in_nav_menus'] = $this->show_in_nav_menus;
|
// }
|
// if (isset($this->show_in_rest)) {
|
// $args['show_in_rest'] = $this->show_in_rest;
|
// }
|
// if (isset($this->rest_base)) {
|
// $args['rest_base'] = $this->rest_base;
|
// }
|
// if (isset($this->rest_namespace)) {
|
// $args['rest_namespace'] = $this->rest_namespace;
|
// }
|
// if (isset($this->rest_controller_class)) {
|
// $args['rest_controller_class'] = $this->rest_controller_class;
|
// }
|
// if (isset($this->show_tag_cloud)) {
|
// $args['show_tag_cloud'] = $this->show_tag_cloud;
|
// }
|
// if (isset($this->show_quick_edit)) {
|
// $args['show_quick_edit'] = $this->show_quick_edit;
|
// }
|
// if (isset($this->show_admin_column)) {
|
// $args['show_admin_column'] = $this->show_admin_column;
|
// }
|
// if (isset($this->meta_box_cb) && is_callable($this->meta_box_cb)) {
|
// $args['meta_box_cb'] = $this->meta_box_cb;
|
// }
|
// if (isset($this->meta_box_sanitize_cb) && is_callable($this->meta_box_sanitize_cb)) {
|
// $args['meta_box_sanitize_cb'] = $this->meta_box_sanitize_cb;
|
// }
|
// if (isset($this->capabilities)) {
|
// $allowed = ['manage_terms', 'edit_terms', 'delete_terms', 'assign_terms'];
|
// $caps = array_filter($this->capabilities, function ($cap) use ($allowed) {
|
// return in_array($cap, $allowed);
|
// }, ARRAY_FILTER_USE_KEY);
|
// $args['capabilities'] = $caps;
|
// }
|
// if (isset($this->query_var)) {
|
// $args['query_var'] = $this->query_var;
|
// }
|
// if (isset($this->update_count_callback) && is_callable($this->update_count_callback)) {
|
// $args['update_count_callback'] = $this->update_count_callback;
|
// }
|
// if (isset($this->default_term)) {
|
// $args['default_term'] = $this->default_term;
|
// }
|
// if (isset($this->sort)) {
|
// $args['sort'] = $this->sort;
|
// }
|
// if (isset($this->args)) {
|
// $args['args'] = $this->args;
|
// }
|
unset($args['for']);
|
|
$for = array_map(function($item) { return jvbCheckBase($item);}, $this->for);
|
register_taxonomy(jvbCheckBase($this->taxonomy), $for, $args);
|
}
|
}
|