Array.php
9 years ago
Date.php
9 years ago
File.php
9 years ago
Html.php
9 years ago
Icon.php
9 years ago
Image.php
9 years ago
Network.php
9 years ago
Post.php
9 years ago
String.php
9 years ago
Taxonomy.php
9 years ago
User.php
9 years ago
Taxonomy.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Helper_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 array(); |
| 18 | } |
| 19 | |
| 20 | $values = array(); |
| 21 | |
| 22 | foreach ( $terms as $t ) { |
| 23 | if ( ! is_a( $t, 'WP_Term' ) ) { |
| 24 | continue; |
| 25 | } |
| 26 | |
| 27 | $args = array( |
| 28 | 'post_type' => $post_type, |
| 29 | 'taxonomy' => $t->taxonomy, |
| 30 | 'term' => $t->slug, |
| 31 | ); |
| 32 | |
| 33 | $page = 'attachment' === $post_type ? 'upload' : 'edit'; |
| 34 | |
| 35 | $values[] = ac_helper()->html->link( add_query_arg( $args, $page . '.php' ), sanitize_term_field( 'name', $t->name, $t->term_id, $t->taxonomy, 'display' ) ); |
| 36 | } |
| 37 | |
| 38 | return $values; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param WP_Term $term |
| 43 | * |
| 44 | * @return false|string |
| 45 | */ |
| 46 | public function get_term_display_name( $term ) { |
| 47 | if ( ! $term || is_wp_error( $term ) ) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return sanitize_term_field( 'name', $term->name, $term->term_id, $term->taxonomy, 'display' ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param string $object_type post, page, user etc. |
| 56 | * @param string $taxonomy Taxonomy Name |
| 57 | * |
| 58 | * @return bool |
| 59 | */ |
| 60 | public function is_taxonomy_registered( $object_type, $taxonomy = '' ) { |
| 61 | if ( ! $object_type ) { |
| 62 | return false; |
| 63 | } |
| 64 | $taxonomies = get_object_taxonomies( $object_type ); |
| 65 | |
| 66 | if ( ! $taxonomies ) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | if ( $taxonomy ) { |
| 71 | return in_array( $taxonomy, $taxonomies ); |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @since 3.0 |
| 79 | */ |
| 80 | public function get_taxonomy_selection_options( $post_type ) { |
| 81 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 82 | |
| 83 | $options = array(); |
| 84 | foreach ( $taxonomies as $index => $taxonomy ) { |
| 85 | if ( $taxonomy->name == 'post_format' ) { |
| 86 | unset( $taxonomies[ $index ] ); |
| 87 | } |
| 88 | $options[ $taxonomy->name ] = $taxonomy->label; |
| 89 | } |
| 90 | |
| 91 | natcasesort( $options ); |
| 92 | |
| 93 | return $options; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param string $field |
| 98 | * @param int $term_id |
| 99 | * @param string $taxonomy |
| 100 | * |
| 101 | * @since 3.0 |
| 102 | */ |
| 103 | public function get_term_field( $field, $term_id, $taxonomy ) { |
| 104 | $term = get_term_by( 'id', $term_id, $taxonomy ); |
| 105 | |
| 106 | if ( ! $term || is_wp_error( $term ) ) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | if ( ! isset( $term->{$field} ) ) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | return $term->{$field}; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param int $term_ids |
| 119 | * @param string $taxonomy |
| 120 | * |
| 121 | * @return WP_Term[] |
| 122 | */ |
| 123 | public function get_terms_by_ids( $term_ids, $taxonomy ) { |
| 124 | $terms = array(); |
| 125 | |
| 126 | foreach ( (array) $term_ids as $term_id ) { |
| 127 | $term = get_term( $term_id, $taxonomy ); |
| 128 | if ( $term && ! is_wp_error( $term ) ) { |
| 129 | $terms[] = $term; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return $terms; |
| 134 | } |
| 135 | |
| 136 | } |
| 137 |