-
Notifications
You must be signed in to change notification settings - Fork 3
/
term-meta.php
258 lines (188 loc) · 5.53 KB
/
term-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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* @package Term Meta
* @version 0.1
*/
/*
Plugin Name: Term Meta
Plugin URI: http://www.alleyinteractive.com/
Description: Creates a framework to store additional meta data for taxonomy terms.
Author: Alley Interactive (Bradford Campeau-Laurion)
Version: 0.1
Author URI: http://www.alleyinteractive.com/
*/
if( !class_exists( 'Term_Meta' ) ) {
define( 'TERM_META_PATH', dirname( __FILE__ ) );
require_once( TERM_META_PATH . '/functions.php' );
class Term_Meta {
/**
* Constructor to set necessary action hooks and filters
*
* @return void
*/
public function __construct() {
$this->create_content_type();
}
/**
* Create the custom content type
*
* @return void
*/
public function create_content_type() {
$args = array(
'public' => false,
'publicly_queryable' => false,
'exclude_from_search' => false,
'query_var' => 'term-meta',
'rewrite' => false,
'show_ui' => false,
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => false
);
register_post_type( 'term-meta', $args );
}
/**
* Handles getting metadata for taxonomy terms
* @param int $term_id
* @param string $meta_key
* @param string $meta_value optional
* @return bool
*/
public function get_term_meta( $term_id, $meta_key='', $single=false ) {
// Check if this term has a post to store meta data
$term_meta_post_id = $this->get_term_meta_post_id( $term_id );
if ( $term_meta_post_id === false ) {
// If not, exit. There is no meta data for this term at all.
// Mimic the normal return behavior of get_post_meta
if ( $single ) return '';
else return array();
}
// Get the meta data
return get_post_meta( $term_meta_post_id, $meta_key, $single );
}
/**
* Handles adding metadata for taxonomy terms
* @param int $term_id
* @param string $meta_key
* @param string $meta_value
* @param bool $unique optional
* @return bool
*/
public function add_term_meta( $term_id, $meta_key, $meta_value, $unique=false ) {
// Check if this term already has a post to store meta data
$term_meta_post_id = $this->get_term_meta_post_id( $term_id );
if ( $term_meta_post_id === false ) {
// If not, create the post to store the metadata
$term_meta_post_id = $this->add_term_meta_post( $term_id );
// Check for errors
if ( $term_meta_post_id === false ) {
return false;
}
}
// Add this key/value pair as post meta data
$result = add_post_meta( $term_meta_post_id, $meta_key, $meta_value, $unique );
if ( $result === false ) {
return false;
} else {
return true;
}
}
/**
* Handles updating metadata for taxonomy terms
* @param int $term_id
* @param string $meta_key
* @param string $meta_value optional
* @return bool
*/
public function update_term_meta( $term_id, $meta_key, $meta_value, $meta_prev_value='' ) {
// Check if this term already has a post to store meta data
$term_meta_post_id = $this->get_term_meta_post_id( $term_id );
if ( $term_meta_post_id === false ) {
// If not, create the post to store the metadata
$term_meta_post_id = $this->add_term_meta_post( $term_id );
// Check for errors
if ( $term_meta_post_id === false ) {
return false;
}
}
// Add this key/value pair as post meta data
$result = update_post_meta( $term_meta_post_id, $meta_key, $meta_value, $meta_prev_value );
if ( $result === false ) {
return false;
} else {
return true;
}
}
/**
* Handles deleting metadata for taxonomy terms
* @param int $term_id
* @param string $meta_key
* @param string $meta_value
* @return bool
*/
public function delete_term_meta( $term_id, $meta_key, $meta_value='' ) {
// Get the post used for this term
$term_meta_post_id = $this->get_term_meta_post_id( $term_id );
// If no post exist, there is nothing further to do here. This is not necessarily an error.
if( $term_meta_post_id === false ) {
return false;
}
// Remove the meta data
$result = delete_post_meta( $term_meta_post_id, $meta_key, $meta_value );
// Check if this term has any metadata at all
$post_terms = get_post_meta( $term_meta_post_id );
if ( empty( $post_terms ) ) {
// If not, remove the post to store the metadata to free up space in wp_posts
$result = wp_delete_post( $term_meta_post_id, true );
}
return $result;
}
/**
* Handles checking if post exists and returning its ID to store taxonomy term meta data
* @param int $term_id
* @return bool
*/
public function get_term_meta_post_id( $term_id ) {
// Check if a post exists for this term
$query = new WP_Query(
array(
'name' => 'term-meta-' . $term_id,
'post_type' => 'term-meta'
)
);
// Return the post ID if it exists, otherwise false
if( $query->have_posts() ) {
$query->next_post();
return $query->post->ID;
} else {
return false;
}
}
/**
* Handles adding a post to store taxonomy term meta data
* @param int $term_id
* @return bool
*/
public function add_term_meta_post( $term_id ) {
// Add the skeleton post to store meta data for this taxonomy term
$result = wp_insert_post(
array(
'post_name' => 'term-meta-' . $term_id,
'post_title' => 'term-meta-' . $term_id,
'post_type' => 'term-meta',
'post_status' => 'publish'
)
);
// Check the result
if ( $result != 0 ) {
return $result;
} else {
return false;
}
}
}
// Create an instance of the class
global $term_meta;
$term_meta = new Term_Meta;
}