1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<?php
class SIW_Taxonomy {
protected $taxonomy;
public function __construct( $taxonomy, $post_type, $labels, $args, $slug ) {
$this->taxonomy = $taxonomy;
$this->post_type = $post_type;
$this->labels = $labels;
$this->args = $args;
$this->slug = $slug;
add_filter( 'taxonomy_template', [ $this, 'register_template'], 10, 3 );
add_action( 'init', [ $this, 'register'] );
}
public function register() {
$rewrite = [
'slug' => $this->slug,
'with_front' => false,
'hierarchical' => false,
];
$default_args = [
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'show_in_quick_edit' => false,
'meta_box_cb' => false,
];
$args = wp_parse_args( $this->args, $default_args );
$args['rewrite'] = $rewrite;
$args['labels'] = $this->labels;
register_taxonomy( "siw_{$this->post_type}_{$this->taxonomy}", "siw_{$this->post_type}", $args );
}
public function register_template( $template, $type, $templates ) {
if ( in_array( "taxonomy-siw_{$this->post_type}_{$this->taxonomy}.php", $templates ) && \SIW\Util::template_exists( "archive-{$this->post_type}.php") ) {
$template = SIW_TEMPLATES_DIR . "/archive-{$this->post_type}.php";
}
return $template;
}
}