TermName.php
1.65 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
<?php
namespace ACP\Helper\Select\Formatter;
use AC;
use ACP\Helper\Select\Value;
use WP_Term;
class TermName extends AC\Helper\Select\Formatter {
/**
* @var array
*/
private $taxonomies;
public function __construct( AC\Helper\Select\Entities $entities, AC\Helper\Select\Value $value = null ) {
if ( ! $value ) {
$value = new Value\Taxonomy( Value\Taxonomy::ID );
}
parent::__construct( $entities, $value );
}
/**
* @return array
*/
private function get_taxonomies() {
if ( null === $this->taxonomies ) {
$this->taxonomies = get_taxonomies();
}
return $this->taxonomies;
}
/**
* @param WP_Term $term
*
* @return bool
*/
private function is_term_post_format( $term ) {
$slug = str_replace( 'post-format-', '', $term->slug );
return 0 === strpos( $term->slug, 'post-format-' ) && in_array( $slug, get_post_format_slugs() );
}
/**
* @param WP_Term $term
*
* @return string
*/
protected function get_label( $term ) {
// Remove corrupt post formats. There can be post format added to the
// DB that are not officially registered. Those are skipped.
if ( 'post_format' === $term->taxonomy && ! $this->is_term_post_format( $term ) ) {
return '';
}
// Extra check if the taxonomy (still) exists
if ( ! in_array( $term->taxonomy, $this->get_taxonomies() ) ) {
return '';
}
$label = htmlspecialchars_decode( $term->name );
if ( ! $label ) {
$label = $term->term_id;
}
if ( 0 !== $term->parent ) {
$label = $this->get_label( get_term_by( 'id', $term->parent, $term->taxonomy ) ) . ' > ' . $label;
}
return (string) apply_filters( 'acp/select/formatter/term_name', $label, $term );
}
}