-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-taxonomy-meta.php
156 lines (140 loc) · 5.15 KB
/
class-taxonomy-meta.php
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
/**
* Helpers for working with Taxonomy Meta.
* Makes it a lot easier to add meta fields, and no have to manually handle them
* on multiple UIs, saving, adding to tables, etc.
*
* Primarily, you want to use Taxonomy_Meta::add();
*/
class Taxonomy_Meta {
/**
* Main interface to adding taxmeta. Call this with appropriate args.
* @param String $tax The taxonomy to add this meta field to.
* @param Array $args Details on how to set up this meta.
* - key: The key for storing/accessing this meta field.
* - value: The current value of this meta.
* - label: User-facing label to show next to the input field.
* - type: What sort of data is it? [ text ].
* - help: A user-facing help string to display.
* - table: Boolean indicating if this should be included in the term listing table.
*/
static function add( $tax, $args ) {
if ( ! $tax || ! taxonomy_exists( $tax ) ) {
return false;
}
$defaults = array(
'value' => '',
'type' => 'text',
'table' => true,
);
$args = wp_parse_args( $args, $defaults );
// Add fields to new/edit screens
add_action( $tax . '_add_form_fields', function() use ( $tax, $args ) {
Taxonomy_Meta::add_ui( $tax, $args );
}, 10, 2 );
add_action( 'created_' . $tax, function( $term_id, $tt_id ) use ( $tax, $args ) {
Taxonomy_Meta::add_handler( $term_id, $tt_id, $tax, $args );
}, 10, 2 );
add_action( $tax . '_edit_form_fields', function( $term, $taxonomy ) use ( $tax, $args ) {
Taxonomy_Meta::edit_ui( $term, $taxonomy, $tax, $args );
}, 10, 2 );
add_action( 'edited_' . $tax, function( $term_id, $tt_id ) use ( $tax, $args ) {
Taxonomy_Meta::edit_handler( $term_id, $tt_id, $tax, $args );
}, 10, 2 );
// Optionally include meta in the term listing table
if ( $args['table'] ) {
add_filter( 'manage_edit-' . $tax . '_columns', function( $columns ) use ( $tax, $args ) {
return Taxonomy_Meta::table_column( $columns, $tax, $args );
}, 10, 1 );
add_filter( 'manage_' . $tax . '_custom_column', function( $content, $column_name, $term_id ) use ( $tax, $args ) {
return Taxonomy_Meta::table_column_data( $content, $column_name, $term_id, $tax, $args );
}, 10, 3 );
}
}
/**
* Handles injecting the required UI for adding new term.
*/
static function add_ui( $tax, $args ) {
Taxonomy_Meta::ui( 'add', $tax, $args );
}
/**
* Handle the data received when saving new terms.
*/
static function add_handler( $term_id, $tt_id, $tax, $args ) {
if ( isset( $_POST[ $tax . '-' . $args['key'] ] ) && '' !== $_POST[ $tax . '-' . $args['key'] ] ){
$value = sanitize_text_field( $_POST[ $tax . '-' . $args['key'] ] );
add_term_meta( $term_id, $tax . '-' . $args['key'], $value, true );
}
}
/**
* Handles injecting the required UI for editing existing terms.
*/
static function edit_ui( $term, $taxonomy, $tax, $args ) {
$args['value'] = get_term_meta( $term->term_id, $tax . '-' . $args['key'], true );
Taxonomy_Meta::ui( 'edit', $tax, $args );
}
/**
* Handle the data received when saving existing terms.
*/
static function edit_handler( $term_id, $tt_id, $tax, $args ) {
if ( isset( $_POST[ $tax . '-' . $args['key'] ] ) && '' !== $_POST[ $tax . '-' . $args['key'] ] ){
$value = sanitize_text_field( $_POST[ $tax . '-' . $args['key'] ] );
update_term_meta( $term_id, $tax . '-' . $args['key'], $value );
}
}
/**
* Create consistent UI for save/edit screens.
* @param String $screen Which screen is this for [ add | edit ]
*/
static function ui( $screen = 'add', $tax, $args ) {
$id = esc_attr( $tax . '-' . $args['key'] );
if ( 'add' == $screen ) {
switch ( $args['type'] ) {
case 'text':
default:
?><div class="form-field term-group">
<label for="<?php echo $id; ?>"><?php esc_html_e( $args['label'] ); ?></label>
<input type="text" name="<?php echo $id; ?>" id="<?php echo $id; ?>" value="<?php esc_attr_e( $args['value'] ); ?>" />
<?php if ( ! empty( $args['help'] ) ) : ?><p><?php esc_html_e( $args['help'] ); ?></p><?php endif; ?>
</div><?php
break;
}
} else { // screen == edit
switch ( $args['type'] ) {
case 'text':
default:
?><tr class="form-field">
<th scope="row">
<label for="<?php echo $id; ?>"><?php esc_html_e( $args['label'] ); ?></label>
</th>
<td>
<input type="text" name="<?php echo $id; ?>" id="<?php echo $id; ?>" value="<?php esc_attr_e( $args['value'] ); ?>" />
<?php if ( ! empty( $args['help'] ) ) : ?><p class="description"><?php esc_html_e( $args['help'] ); ?></p><?php endif; ?>
</td>
</tr><?php
break;
}
}
}
/**
* Add this meta into the term listing table (optional).
*/
static function table_column( $columns, $tax, $args ) {
$columns[ $args['key'] ] = $args['label'];
return $columns;
}
/**
* Add this meta into the term listing table (optional).
*/
static function table_column_data( $content, $column_name, $term_id, $tax, $args ) {
if ( $args['key'] !== $column_name ) {
return $content;
}
$term_id = absint( $term_id );
$val = get_term_meta( $term_id, $tax . '-' . $args['key'], true );
if ( !empty( $val ) ){
$content .= esc_attr( $val );
}
return $content;
}
}