Select
4 years ago
Arrays.php
4 years ago
Date.php
4 years ago
File.php
4 years ago
Html.php
4 years ago
Icon.php
4 years ago
Image.php
4 years ago
Media.php
4 years ago
Menu.php
4 years ago
Network.php
4 years ago
Post.php
4 years ago
Strings.php
4 years ago
Taxonomy.php
4 years ago
User.php
4 years ago
Taxonomy.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Helper; |
| 4 | |
| 5 | use WP_Term; |
| 6 | |
| 7 | class Taxonomy { |
| 8 | |
| 9 | /** |
| 10 | * @param WP_Term[] $terms Term objects |
| 11 | * @param null|string $post_type |
| 12 | * |
| 13 | * @return array |
| 14 | */ |
| 15 | public function get_term_links( $terms, $post_type = null ) { |
| 16 | if ( ! $terms || is_wp_error( $terms ) ) { |
| 17 | return []; |
| 18 | } |
| 19 | |
| 20 | $values = []; |
| 21 | |
| 22 | foreach ( $terms as $t ) { |
| 23 | if ( ! $t instanceof WP_Term ) { |
| 24 | continue; |
| 25 | } |
| 26 | |
| 27 | $values[] = ac_helper()->html->link( $this->get_term_url( $t, $post_type ), sanitize_term_field( 'name', $t->name, $t->term_id, $t->taxonomy, 'display' ) ); |
| 28 | } |
| 29 | |
| 30 | return $values; |
| 31 | } |
| 32 | |
| 33 | public function get_term_url( $term, $post_type = null ) { |
| 34 | if ( is_numeric( $term ) ) { |
| 35 | $term = get_term_by( 'term_taxonomy_id', $term ); |
| 36 | } |
| 37 | |
| 38 | $args = [ |
| 39 | 'post_type' => $post_type, |
| 40 | 'taxonomy' => $term->taxonomy, |
| 41 | 'term' => $term->slug, |
| 42 | ]; |
| 43 | |
| 44 | $page = 'attachment' === $post_type ? 'upload' : 'edit'; |
| 45 | |
| 46 | return add_query_arg( $args, $page . '.php' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param WP_Term $term |
| 51 | * |
| 52 | * @return false|string |
| 53 | */ |
| 54 | public function get_term_display_name( $term ) { |
| 55 | if ( ! $term || is_wp_error( $term ) ) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | return sanitize_term_field( 'name', $term->name, $term->term_id, $term->taxonomy, 'display' ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param string $object_type post, page, user etc. |
| 64 | * @param string $taxonomy Taxonomy Name |
| 65 | * |
| 66 | * @return bool |
| 67 | */ |
| 68 | public function is_taxonomy_registered( $object_type, $taxonomy = '' ) { |
| 69 | if ( ! $object_type || ! $taxonomy ) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return is_object_in_taxonomy( $object_type, $taxonomy ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string $field |
| 78 | * @param int $term_id |
| 79 | * @param string $taxonomy |
| 80 | * |
| 81 | * @return bool|mixed |
| 82 | * @since 3.0 |
| 83 | */ |
| 84 | public function get_term_field( $field, $term_id, $taxonomy ) { |
| 85 | $term = get_term_by( 'id', $term_id, $taxonomy ); |
| 86 | |
| 87 | if ( ! $term || is_wp_error( $term ) ) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if ( ! isset( $term->{$field} ) ) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | return $term->{$field}; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param $post_type |
| 100 | * |
| 101 | * @return array |
| 102 | * @since 3.0 |
| 103 | */ |
| 104 | public function get_taxonomy_selection_options( $post_type ) { |
| 105 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 106 | |
| 107 | $options = []; |
| 108 | foreach ( $taxonomies as $index => $taxonomy ) { |
| 109 | if ( $taxonomy->name == 'post_format' ) { |
| 110 | unset( $taxonomies[ $index ] ); |
| 111 | } |
| 112 | $options[ $taxonomy->name ] = $taxonomy->label; |
| 113 | } |
| 114 | |
| 115 | natcasesort( $options ); |
| 116 | |
| 117 | return $options; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @param int $term_ids |
| 122 | * @param string $taxonomy |
| 123 | * |
| 124 | * @return WP_Term[] |
| 125 | */ |
| 126 | public function get_terms_by_ids( $term_ids, $taxonomy ) { |
| 127 | $terms = []; |
| 128 | |
| 129 | foreach ( (array) $term_ids as $term_id ) { |
| 130 | $term = get_term( $term_id, $taxonomy ); |
| 131 | if ( $term && ! is_wp_error( $term ) ) { |
| 132 | $terms[] = $term; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return $terms; |
| 137 | } |
| 138 | |
| 139 | public function get_taxonomy_label( $taxonomy, $key = 'name' ) { |
| 140 | $label = $taxonomy; |
| 141 | $taxonomy_object = get_taxonomy( $taxonomy ); |
| 142 | |
| 143 | if ( ! $taxonomy_object ) { |
| 144 | return $label; |
| 145 | } |
| 146 | |
| 147 | $labels = get_taxonomy_labels( $taxonomy_object ); |
| 148 | |
| 149 | if ( property_exists( $labels, $key ) ) { |
| 150 | $label = $labels->$key; |
| 151 | } |
| 152 | |
| 153 | return $label; |
| 154 | } |
| 155 | |
| 156 | } |