Taxonomy.php
3.06 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace AC\Helper;
use WP_Term;
class Taxonomy {
/**
* @param WP_Term[] $terms Term objects
* @param null|string $post_type
*
* @return array
*/
public function get_term_links( $terms, $post_type = null ) {
if ( ! $terms || is_wp_error( $terms ) ) {
return [];
}
$values = [];
foreach ( $terms as $t ) {
if ( ! $t instanceof WP_Term ) {
continue;
}
$values[] = ac_helper()->html->link( $this->get_term_url( $t, $post_type ), sanitize_term_field( 'name', $t->name, $t->term_id, $t->taxonomy, 'display' ) );
}
return $values;
}
public function get_term_url( $term, $post_type = null ) {
if ( is_numeric( $term ) ) {
$term = get_term_by( 'term_taxonomy_id', $term );
}
$args = [
'post_type' => $post_type,
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
];
$page = 'attachment' === $post_type ? 'upload' : 'edit';
return add_query_arg( $args, $page . '.php' );
}
/**
* @param WP_Term $term
*
* @return false|string
*/
public function get_term_display_name( $term ) {
if ( ! $term || is_wp_error( $term ) ) {
return false;
}
return sanitize_term_field( 'name', $term->name, $term->term_id, $term->taxonomy, 'display' );
}
/**
* @param string $object_type post, page, user etc.
* @param string $taxonomy Taxonomy Name
*
* @return bool
*/
public function is_taxonomy_registered( $object_type, $taxonomy = '' ) {
if ( ! $object_type || ! $taxonomy ) {
return false;
}
return is_object_in_taxonomy( $object_type, $taxonomy );
}
/**
* @param string $field
* @param int $term_id
* @param string $taxonomy
*
* @return bool|mixed
* @since 3.0
*/
public function get_term_field( $field, $term_id, $taxonomy ) {
$term = get_term_by( 'id', $term_id, $taxonomy );
if ( ! $term || is_wp_error( $term ) ) {
return false;
}
if ( ! isset( $term->{$field} ) ) {
return false;
}
return $term->{$field};
}
/**
* @param $post_type
*
* @return array
* @since 3.0
*/
public function get_taxonomy_selection_options( $post_type ) {
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$options = [];
foreach ( $taxonomies as $index => $taxonomy ) {
if ( $taxonomy->name == 'post_format' ) {
unset( $taxonomies[ $index ] );
}
$options[ $taxonomy->name ] = $taxonomy->label;
}
natcasesort( $options );
return $options;
}
/**
* @param int $term_ids
* @param string $taxonomy
*
* @return WP_Term[]
*/
public function get_terms_by_ids( $term_ids, $taxonomy ) {
$terms = [];
foreach ( (array) $term_ids as $term_id ) {
$term = get_term( $term_id, $taxonomy );
if ( $term && ! is_wp_error( $term ) ) {
$terms[] = $term;
}
}
return $terms;
}
public function get_taxonomy_label( $taxonomy, $key = 'name' ) {
$label = $taxonomy;
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $taxonomy_object ) {
return $label;
}
$labels = get_taxonomy_labels( $taxonomy_object );
if ( property_exists( $labels, $key ) ) {
$label = $labels->$key;
}
return $label;
}
}