taxonomy.php
3.5 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
class CPAC_Storage_Model_Taxonomy extends CPAC_Storage_Model {
public $taxonomy;
public $taxonomy_object;
/**
* Constructor
*
* @since 1.2.0
*/
function __construct( $taxonomy ) {
$this->set_taxonomy_object( $taxonomy );
$this->key = 'wp-taxonomy_' . $taxonomy;
$this->type = 'taxonomy';
$this->page = 'edit-tags';
$this->taxonomy = $taxonomy;
$this->label = $this->taxonomy_object->labels->name;
$this->singular_label = $this->taxonomy_object->labels->singular_name;
$this->menu_type = $this->type;
// headings
add_filter( "manage_edit-{$this->taxonomy}_columns", array( $this, 'add_headings' ) );
// values
add_action( "manage_{$this->taxonomy}_custom_column", array( $this, 'manage_value' ), 10, 3 );
parent::__construct();
}
/**
* Get taxonomy
*
* @since 3.5
*
* @return string Taxonomy name
*/
public function set_taxonomy_object( $taxonomy ) {
$this->taxonomy_object = get_taxonomy( $taxonomy );
}
/**
* Get screen link
*
* @since 1.2.0
*
* @return string Link
*/
protected function get_screen_link() {
return add_query_arg( array( 'taxonomy' => $this->taxonomy ), admin_url( $this->page . '.php' ) );
}
/**
* Get taxonomy
*
* @since 3.4
*
* @return string Taxonomy name
*/
public function get_taxonomy() {
return $this->taxonomy;
}
/**
* Get WP default supported admin columns per post type.
*
* @since 1.0.0
*
* @return array
*/
public function get_default_columns() {
if ( ! function_exists('_get_list_table') ) {
return array();
}
// You can use this filter to add thirdparty columns by hooking into this.
// See classes/third_party.php for an example.
do_action( "cac/columns/default/storage_key={$this->key}" );
// get columns
$table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $this->taxonomy ) );
$columns = $table->get_columns();
return $columns;
}
/**
* Get original columns
*
* @since 3.5.1
*/
public function get_default_column_names() {
return array( 'cb', 'name', 'description', 'slug', 'posts', 'links' );
}
/**
* Get Meta
*
* @since 1.2.0
*
* @return array
*/
public function get_meta() {
global $wpdb;
$meta = array();
// Only works with ACF taxonomy fields
if ( $results = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT option_name FROM {$wpdb->options} WHERE option_name LIKE '%s' ORDER BY 1", $this->taxonomy . '_%' ), ARRAY_N ) ) {
foreach ( $results as $result ) {
$option_name = $result[0];
$underscore = strpos( $option_name, '_', strlen( $this->taxonomy ) + 1 );
if ( false === $underscore ) {
continue;
}
$key = substr( $option_name, $underscore + 1, strlen( $option_name ) );
$meta[][0] = $key;
}
}
return $meta;
}
/**
* Manage value
*
* @since 1.2.0
*
* @param string $column_name
* @param int $post_id
*/
public function manage_value( $content, $column_name, $term_id ) {
$value = $content;
// get column instance
if ( $column = $this->get_column_by_name( $column_name ) ) {
$value .= $column->get_value( $term_id );
}
// add hook
$value = apply_filters( "cac/column/value", $value, $term_id, $column, $this->key );
$value = apply_filters( "cac/column/value/{$this->type}", $value, $term_id, $column, $this->key );
return $value;
}
}