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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
<?php
namespace SIW;
class Media_Taxonomies {
public static function init() {
$self = new self();
add_action( 'init', [ $self, 'register_taxonomies'] );
add_filter( 'rwmb_meta_boxes', [ $self, 'add_metabox'] );
}
public function register_taxonomies() {
$taxonomies = $this->get_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
$labels = [
'name' => $taxonomy['name'],
'singular_name' => $taxonomy['name'],
];
$args = [
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => false,
'show_tagcloud' => false,
'show_admin_column' => false,
];
register_taxonomy( "siw_attachment_{$taxonomy['slug']}", 'attachment', $args );
}
}
public function add_metabox( $metaboxes ) {
$taxonomies = $this->get_taxonomies();
$fields = [];
foreach ( $taxonomies as $taxonomy ) {
$fields[] = [
'id' => "siw_attachment_taxonomy_{$taxonomy['slug']}",
'name' => $taxonomy['name'],
'type' => 'taxonomy',
'remove_default' => true,
'taxonomy' => "siw_attachment_{$taxonomy['slug']}",
'ajax' => false,
'field_type' => $taxonomy['multiple'] ? 'checkbox_list' : 'select',
];
}
$metaboxes[] = [
'id' => 'siw_attachment_taxonomies',
'title' => __( 'Media taxonomieën', 'siw' ),
'post_types' => ['attachment'],
'context' => 'side',
'priority' => 'low',
'fields' => $fields,
];
return $metaboxes;
}
protected function get_taxonomies() {
$taxonomies = [
[
'slug' => 'continent',
'name' => __( 'Continent', 'siw' ),
'multiple' => false,
],
[
'slug' => 'country',
'name' => __( 'Land', 'siw' ),
'multiple' => false,
],
[
'slug' => 'work_type',
'name' => __( 'Soort werk', 'siw' ),
'multiple' => true,
],
];
return $taxonomies;
}
}