Image
2 days ago
Select
2 days ago
Arrays.php
2 days ago
AvailableTranslations.php
2 days ago
Creatable.php
2 days ago
Dashicon.php
2 days ago
Date.php
2 days ago
Html.php
2 days ago
Icon.php
2 days ago
Image.php
2 days ago
LocalFile.php
2 days ago
Mbstring.php
2 days ago
MediaIdResolver.php
2 days ago
Menu.php
2 days ago
Network.php
2 days ago
Post.php
2 days ago
Strings.php
2 days ago
Taxonomy.php
2 days ago
User.php
2 days ago
UserRoles.php
2 days ago
WpDateFormat.php
2 days ago
Taxonomy.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper; |
| 6 | |
| 7 | use WP_Taxonomy; |
| 8 | use WP_Term; |
| 9 | |
| 10 | class Taxonomy extends Creatable |
| 11 | { |
| 12 | public function get_term_links(array $terms, ?string $post_type = null): array |
| 13 | { |
| 14 | $values = []; |
| 15 | |
| 16 | foreach ($terms as $term) { |
| 17 | if (! $term instanceof WP_Term) { |
| 18 | continue; |
| 19 | } |
| 20 | |
| 21 | $values[] = Html::create()->link( |
| 22 | $this->get_filter_by_term_url($term, $post_type), |
| 23 | $this->get_term_display_name($term) |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | return $values; |
| 28 | } |
| 29 | |
| 30 | public function get_filter_by_term_url(WP_Term $term, ?string $post_type = null): string |
| 31 | { |
| 32 | $args = [ |
| 33 | 'taxonomy' => $term->taxonomy, |
| 34 | 'term' => $term->slug, |
| 35 | ]; |
| 36 | |
| 37 | if ($post_type) { |
| 38 | $args['post_type'] = $post_type; |
| 39 | } |
| 40 | |
| 41 | $page = 'attachment' === $post_type |
| 42 | ? 'upload.php' |
| 43 | : 'edit.php'; |
| 44 | |
| 45 | return add_query_arg( |
| 46 | $args, |
| 47 | $page |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | public function get_term_display_name(WP_Term $term): string |
| 52 | { |
| 53 | return (string)sanitize_term_field('name', $term->name, $term->term_id, $term->taxonomy, 'display'); |
| 54 | } |
| 55 | |
| 56 | public function get_term_field(string $field, int $term_id, string $taxonomy): ?string |
| 57 | { |
| 58 | $term = get_term_by('id', $term_id, $taxonomy); |
| 59 | |
| 60 | if (! $term instanceof WP_Term) { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | if (! isset($term->{$field})) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | return (string)$term->{$field}; |
| 69 | } |
| 70 | |
| 71 | public function get_taxonomy_selection_options(string $post_type): array |
| 72 | { |
| 73 | $taxonomies = get_object_taxonomies($post_type, 'objects'); |
| 74 | |
| 75 | $options = []; |
| 76 | |
| 77 | foreach ($taxonomies as $taxonomy) { |
| 78 | if ($taxonomy->name === 'post_format') { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | $options[$taxonomy->name] = $taxonomy->label; |
| 83 | } |
| 84 | |
| 85 | natcasesort($options); |
| 86 | |
| 87 | return $options; |
| 88 | } |
| 89 | |
| 90 | public function get_terms_by_ids(array $term_ids, string $taxonomy): array |
| 91 | { |
| 92 | $terms = []; |
| 93 | |
| 94 | foreach ($term_ids as $term_id) { |
| 95 | $term = get_term((int)$term_id, $taxonomy); |
| 96 | |
| 97 | if ($term instanceof WP_Term) { |
| 98 | $terms[] = $term; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $terms; |
| 103 | } |
| 104 | |
| 105 | public function get_taxonomy_label(string $taxonomy, string $property = 'name'): string |
| 106 | { |
| 107 | $taxonomy_object = get_taxonomy($taxonomy); |
| 108 | |
| 109 | if (! $taxonomy_object instanceof WP_Taxonomy) { |
| 110 | return $taxonomy; |
| 111 | } |
| 112 | |
| 113 | return get_taxonomy_labels($taxonomy_object)->$property ?? $taxonomy; |
| 114 | } |
| 115 | |
| 116 | } |
| 117 |