Taxonomy.php
1.51 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
<?php
namespace AC\Column;
use AC\Collection;
use AC\Column;
use AC\Settings;
/**
* Taxonomy column, displaying terms from a taxonomy for any object type (i.e. posts)
* supporting WordPress' native way of handling terms.
* @since 2.0
*/
class Taxonomy extends Column {
public function __construct() {
$this->set_type( 'column-taxonomy' );
$this->set_label( __( 'Taxonomy', 'codepress-admin-columns' ) );
}
public function get_taxonomy() {
return $this->get_option( 'taxonomy' );
}
public function get_value( $post_id ) {
$_terms = $this->get_raw_value( $post_id );
if ( empty( $_terms ) ) {
return $this->get_empty_char();
}
$collection = new Collection( $_terms );
$terms = [];
foreach ( $collection as $term ) {
$terms[] = $this->get_formatted_value( $term->name, $term );
}
if ( empty( $terms ) ) {
return $this->get_empty_char();
}
$setting_limit = $this->get_setting( 'number_of_items' );
return ac_helper()->html->more( $terms, $setting_limit ? $setting_limit->get_value() : false );
}
/**
* @param int $post_id
*
* @return array|false
*/
public function get_raw_value( $post_id ) {
$terms = get_the_terms( $post_id, $this->get_taxonomy() );
if ( ! $terms || is_wp_error( $terms ) ) {
return false;
}
return $terms;
}
public function register_settings() {
$this->add_setting( new Settings\Column\Taxonomy( $this ) );
$this->add_setting( new Settings\Column\TermLink( $this ) );
$this->add_setting( new Settings\Column\NumberOfItems( $this ) );
}
}