| 1 |
<?php |
| 2 |
/** |
| 3 |
* Taxonomy functions file. |
| 4 |
* |
| 5 |
* @package Bricksable/Includes |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Taxonomy functions class. |
| 14 |
*/ |
| 15 |
class Bricksable_Taxonomy { |
| 16 |
|
| 17 |
/** |
| 18 |
* The name for the taxonomy. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
* @access public |
| 22 |
* @since 1.0.0 |
| 23 |
*/ |
| 24 |
public $taxonomy; |
| 25 |
|
| 26 |
/** |
| 27 |
* The plural name for the taxonomy terms. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
* @access public |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public $plural; |
| 34 |
|
| 35 |
/** |
| 36 |
* The singular name for the taxonomy terms. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
* @access public |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
public $single; |
| 43 |
|
| 44 |
/** |
| 45 |
* The array of post types to which this taxonomy applies. |
| 46 |
* |
| 47 |
* @var array |
| 48 |
* @access public |
| 49 |
* @since 1.0.0 |
| 50 |
*/ |
| 51 |
public $post_types; |
| 52 |
|
| 53 |
/** |
| 54 |
* The array of taxonomy arguments |
| 55 |
* |
| 56 |
* @var array |
| 57 |
* @access public |
| 58 |
* @since 1.0.0 |
| 59 |
*/ |
| 60 |
public $taxonomy_args; |
| 61 |
|
| 62 |
/** |
| 63 |
* Taxonomy constructor. |
| 64 |
* |
| 65 |
* @param string $taxonomy Taxonomy variable nnam. |
| 66 |
* @param string $plural Taxonomy plural name. |
| 67 |
* @param string $single Taxonomy singular name. |
| 68 |
* @param array $post_types Affected post types. |
| 69 |
* @param array $tax_args Taxonomy additional args. |
| 70 |
*/ |
| 71 |
public function __construct( $taxonomy = '', $plural = '', $single = '', $post_types = array(), $tax_args = array() ) { |
| 72 |
|
| 73 |
if ( ! $taxonomy || ! $plural || ! $single ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
// Post type name and labels. |
| 78 |
$this->taxonomy = $taxonomy; |
| 79 |
$this->plural = $plural; |
| 80 |
$this->single = $single; |
| 81 |
if ( ! is_array( $post_types ) ) { |
| 82 |
$post_types = array( $post_types ); |
| 83 |
} |
| 84 |
$this->post_types = $post_types; |
| 85 |
$this->taxonomy_args = $tax_args; |
| 86 |
|
| 87 |
// Register taxonomy. |
| 88 |
add_action( 'init', array( $this, 'register_taxonomy' ) ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Register new taxonomy |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function register_taxonomy() { |
| 97 |
//phpcs:disable |
| 98 |
$labels = array( |
| 99 |
'name' => $this->plural, |
| 100 |
'singular_name' => $this->single, |
| 101 |
'menu_name' => $this->plural, |
| 102 |
'all_items' => sprintf( __( 'All %s', 'bricksable' ), $this->plural ), |
| 103 |
'edit_item' => sprintf( __( 'Edit %s', 'bricksable' ), $this->single ), |
| 104 |
'view_item' => sprintf( __( 'View %s', 'bricksable' ), $this->single ), |
| 105 |
'update_item' => sprintf( __( 'Update %s', 'bricksable' ), $this->single ), |
| 106 |
'add_new_item' => sprintf( __( 'Add New %s', 'bricksable' ), $this->single ), |
| 107 |
'new_item_name' => sprintf( __( 'New %s Name', 'bricksable' ), $this->single ), |
| 108 |
'parent_item' => sprintf( __( 'Parent %s', 'bricksable' ), $this->single ), |
| 109 |
'parent_item_colon' => sprintf( __( 'Parent %s:', 'bricksable' ), $this->single ), |
| 110 |
'search_items' => sprintf( __( 'Search %s', 'bricksable' ), $this->plural ), |
| 111 |
'popular_items' => sprintf( __( 'Popular %s', 'bricksable' ), $this->plural ), |
| 112 |
'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'bricksable' ), $this->plural ), |
| 113 |
'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'bricksable' ), $this->plural ), |
| 114 |
'choose_from_most_used' => sprintf( __( 'Choose from the most used %s', 'bricksable' ), $this->plural ), |
| 115 |
'not_found' => sprintf( __( 'No %s found', 'bricksable' ), $this->plural ), |
| 116 |
); |
| 117 |
//phpcs:enable |
| 118 |
$args = array( |
| 119 |
'label' => $this->plural, |
| 120 |
'labels' => apply_filters( $this->taxonomy . '_labels', $labels ), |
| 121 |
'hierarchical' => true, |
| 122 |
'public' => true, |
| 123 |
'show_ui' => true, |
| 124 |
'show_in_nav_menus' => true, |
| 125 |
'show_tagcloud' => true, |
| 126 |
'meta_box_cb' => null, |
| 127 |
'show_admin_column' => true, |
| 128 |
'show_in_quick_edit' => true, |
| 129 |
'update_count_callback' => '', |
| 130 |
'show_in_rest' => true, |
| 131 |
'rest_base' => $this->taxonomy, |
| 132 |
'rest_controller_class' => 'WP_REST_Terms_Controller', |
| 133 |
'query_var' => $this->taxonomy, |
| 134 |
'rewrite' => true, |
| 135 |
'sort' => '', |
| 136 |
); |
| 137 |
|
| 138 |
$args = array_merge( $args, $this->taxonomy_args ); |
| 139 |
|
| 140 |
register_taxonomy( $this->taxonomy, $this->post_types, apply_filters( $this->taxonomy . '_register_args', $args, $this->taxonomy, $this->post_types ) ); |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|