forked from not-only-code/qtranslate-slug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
termmeta-core.php
201 lines (163 loc) · 5.68 KB
/
termmeta-core.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
<?php
/**
* Install Term meta table - setup table, store db version for future updates
*/
function install_term_meta_table() {
global $wpdb;
$collate = '';
if($wpdb->supports_collation()) {
if(!empty($wpdb->charset)) $collate = "DEFAULT CHARACTER SET $wpdb->charset";
if(!empty($wpdb->collate)) $collate .= " COLLATE $wpdb->collate";
}
$sql = "CREATE TABLE IF NOT EXISTS ". $wpdb->prefix . "termmeta" ." (
`meta_id` bigint(20) NOT NULL AUTO_INCREMENT,
`term_id` bigint(20) NOT NULL DEFAULT '0',
`meta_key` varchar(255) NULL,
`meta_value` longtext NULL,
PRIMARY KEY id (`meta_id`)) $collate;";
$wpdb->query($sql);
}
function simple_post_meta_define_table() {
global $wpdb;
$wpdb->termmeta = $wpdb->prefix . 'termmeta';
}
add_action( 'init', 'simple_post_meta_define_table' );
/**
* Updates metadata cache for list of term IDs.
*
* Performs SQL query to retrieve the metadata for the term IDs and updates the
* metadata cache for the terms. Therefore, the functions, which call this
* function, do not need to perform SQL queries on their own.
*
* @param array $term_ids List of post IDs.
* @return bool|array Returns false if there is nothing to update or an array of metadata.
*/
function update_termmeta_cache($term_ids) {
return update_meta_cache('term', $term_ids);
}
/**
* Add meta data field to a term.
*
* @param int $term_id Term ID.
* @param string $key Metadata name.
* @param mixed $value Metadata value.
* @param bool $unique Optional, default is false. Whether the same key should not be added.
* @return bool False for failure. True for success.
*/
function add_term_meta( $term_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata('term', $term_id, $meta_key, $meta_value, $unique);
}
/**
* Remove metadata matching criteria from a term.
*
* You can match based on the key, or key and value. Removing based on key and
* value, will keep from removing duplicate metadata with the same key. It also
* allows removing all metadata matching key, if needed.
*
* @param int $term_id Term ID
* @param string $meta_key Metadata name.
* @param mixed $meta_value Optional. Metadata value.
* @return bool False for failure. True for success.
*/
function delete_term_meta( $term_id, $meta_key, $meta_value = '' ) {
return delete_metadata('term', $term_id, $meta_key, $meta_value);
}
/**
* Retrieve term meta field for a term.
*
* @param int $term_id Term ID.
* @param string $key The meta key to retrieve.
* @param bool $single Whether to return a single value.
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single
* is true.
*/
function get_term_meta( $term_id, $key, $single = false ) {
return get_metadata('term', $term_id, $key, $single);
}
/**
* Update term meta field based on term ID.
*
* Use the $prev_value parameter to differentiate between meta fields with the
* same key and term ID.
*
* If the meta field for the term does not exist, it will be added.
*
* @param int $term_id Term ID.
* @param string $key Metadata key.
* @param mixed $value Metadata value.
* @param mixed $prev_value Optional. Previous value to check before removing.
* @return bool False on failure, true if success.
*/
function update_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata('term', $term_id, $meta_key, $meta_value, $prev_value);
}
/**
* Delete everything from term meta matching meta key.
*
* @param string $term_meta_key Key to search for when deleting.
* @return bool Whether the term meta key was deleted from the database
*/
function delete_term_meta_by_key($term_meta_key) {
if ( !$term_meta_key )
return false;
global $wpdb;
$term_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT term_id FROM $wpdb->termmeta WHERE meta_key = %s", $term_meta_key));
if ( $term_ids ) {
$termmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE meta_key = %s", $term_meta_key ) );
$in = implode( ',', array_fill(1, count($termmetaids), '%d'));
do_action( 'delete_termmeta', $termmetaids );
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->termmeta WHERE meta_id IN($in)", $termmetaids ));
do_action( 'deleted_termmeta', $termmetaids );
foreach ( $term_ids as $term_id )
wp_cache_delete($term_id, 'term_meta');
return true;
}
return false;
}
/**
* Retrieve term meta fields, based on term ID.
*
* The term meta fields are retrieved from the cache, so the function is
* optimized to be called more than once. It also applies to the functions, that
* use this function.
*
* @param int $term_id term ID
* @return array
*/
function get_term_custom( $term_id ) {
$term_id = (int) $term_id;
if ( ! wp_cache_get($term_id, 'term_meta') )
update_termmeta_cache($term_id);
return wp_cache_get($term_id, 'term_meta');
}
/**
* Retrieve meta field names for a term.
*
* If there are no meta fields, then nothing (null) will be returned.
*
* @param int $term_id term ID
* @return array|null Either array of the keys, or null if keys could not be retrieved.
*/
function get_term_custom_keys( $term_id ) {
$custom = get_term_custom( $term_id );
if ( !is_array($custom) )
return;
if ( $keys = array_keys($custom) )
return $keys;
}
/**
* Retrieve values for a custom term field.
*
* The parameters must not be considered optional. All of the term meta fields
* will be retrieved and only the meta field key values returned.
*
* @param string $key Meta field key.
* @param int $term_id Term ID
* @return array Meta field values.
*/
function get_term_custom_values( $key = '', $term_id ) {
if ( !$key )
return null;
$custom = get_term_custom($term_id);
return isset($custom[$key]) ? $custom[$key] : null;
}