| 8 | | function is_taxonomy( $taxonomy ) { |
|---|
| 9 | | global $wp_taxonomies; |
|---|
| 10 | | |
|---|
| 11 | | return isset($wp_taxonomies[$taxonomy]); |
|---|
| 12 | | } |
|---|
| 13 | | |
|---|
| 14 | | function get_taxonomy( $taxonomy ) { |
|---|
| 15 | | global $wp_taxonomies; |
|---|
| 16 | | |
|---|
| 17 | | if ( ! is_taxonomy($taxonomy) ) |
|---|
| 18 | | return false; |
|---|
| 19 | | |
|---|
| 20 | | return $wp_taxonomies[$taxonomy]; |
|---|
| 21 | | } |
|---|
| 22 | | |
|---|
| 23 | | function is_taxonomy_hierarchical($taxonomy) { |
|---|
| 24 | | if ( ! is_taxonomy($taxonomy) ) |
|---|
| 25 | | return false; |
|---|
| 26 | | |
|---|
| 27 | | $taxonomy = get_taxonomy($taxonomy); |
|---|
| 28 | | return $taxonomy->hierarchical; |
|---|
| 29 | | } |
|---|
| 30 | | |
|---|
| 31 | | function register_taxonomy( $taxonomy, $object_type, $args = array() ) { |
|---|
| 32 | | global $wp_taxonomies; |
|---|
| 33 | | |
|---|
| 34 | | $defaults = array('hierarchical' => false, 'update_count_callback' => ''); |
|---|
| 35 | | $args = wp_parse_args($args, $defaults); |
|---|
| 36 | | |
|---|
| 37 | | $args['name'] = $taxonomy; |
|---|
| 38 | | $args['object_type'] = $object_type; |
|---|
| 39 | | $wp_taxonomies[$taxonomy] = (object) $args; |
|---|
| 40 | | } |
|---|
| 41 | | |
|---|
| 42 | | function wp_count_terms( $taxonomy, $args = array() ) { |
|---|
| 43 | | global $wpdb; |
|---|
| 44 | | |
|---|
| 45 | | $defaults = array('ignore_empty' => false); |
|---|
| 46 | | $args = wp_parse_args($args, $defaults); |
|---|
| 47 | | extract($args, EXTR_SKIP); |
|---|
| 48 | | |
|---|
| 49 | | $where = ''; |
|---|
| 50 | | if ( $ignore_empty ) |
|---|
| 51 | | $where = 'AND count > 0'; |
|---|
| 52 | | |
|---|
| 53 | | return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = '$taxonomy' $where"); |
|---|
| 54 | | } |
|---|
| 55 | | |
|---|
| 56 | | /** |
|---|
| 57 | | * Adds a new term to the database. Optionally marks it as an alias of an existing term. |
|---|
| 58 | | * @param int|string $term The term to add or update. |
|---|
| 59 | | * @param string $taxonomy The taxonomy to which to add the term |
|---|
| 60 | | * @param int|string $alias_of The id or slug of the new term's alias. |
|---|
| 61 | | */ |
|---|
| 62 | | function wp_insert_term( $term, $taxonomy, $args = array() ) { |
|---|
| 63 | | global $wpdb; |
|---|
| 64 | | |
|---|
| 65 | | if ( ! is_taxonomy($taxonomy) ) |
|---|
| 66 | | return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|---|
| 67 | | |
|---|
| 68 | | $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); |
|---|
| 69 | | $args = wp_parse_args($args, $defaults); |
|---|
| 70 | | $args['name'] = $term; |
|---|
| 71 | | $args = sanitize_term($args, $taxonomy, 'db'); |
|---|
| 72 | | extract($args, EXTR_SKIP); |
|---|
| 73 | | |
|---|
| 74 | | if ( empty($slug) ) |
|---|
| 75 | | $slug = sanitize_title($name); |
|---|
| 76 | | else |
|---|
| 77 | | $slug = sanitize_title($slug); |
|---|
| 78 | | |
|---|
| 79 | | $term_group = 0; |
|---|
| 80 | | if ( $alias_of ) { |
|---|
| 81 | | $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$alias_of'"); |
|---|
| 82 | | if ( $alias->term_group ) { |
|---|
| 83 | | // The alias we want is already in a group, so let's use that one. |
|---|
| 84 | | $term_group = $alias->term_group; |
|---|
| 85 | | } else { |
|---|
| 86 | | // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
|---|
| 87 | | $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1; |
|---|
| 88 | | $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id"); |
|---|
| 89 | | } |
|---|
| 90 | | } |
|---|
| 91 | | |
|---|
| 92 | | if ( ! $term_id = is_term($slug) ) { |
|---|
| 93 | | $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')"); |
|---|
| 94 | | $term_id = (int) $wpdb->insert_id; |
|---|
| 95 | | } |
|---|
| 96 | | |
|---|
| 97 | | if ( empty($slug) ) { |
|---|
| 98 | | $slug = sanitize_title($slug, $term_id); |
|---|
| 99 | | $wpdb->query("UPDATE $wpdb->terms SET slug = '$slug' WHERE term_id = '$term_id'"); |
|---|
| 100 | | } |
|---|
| 101 | | |
|---|
| 102 | | $tt_id = $wpdb->get_var("SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = $term_id"); |
|---|
| 103 | | |
|---|
| 104 | | if ( !empty($tt_id) ) |
|---|
| 105 | | return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|---|
| 106 | | |
|---|
| 107 | | $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')"); |
|---|
| 108 | | $tt_id = (int) $wpdb->insert_id; |
|---|
| 109 | | |
|---|
| 110 | | do_action("create_term", $term_id, $tt_id); |
|---|
| 111 | | do_action("create_$taxonomy", $term_id, $tt_id); |
|---|
| 112 | | |
|---|
| 113 | | $term_id = apply_filters('term_id_filter', $term_id, $tt_id); |
|---|
| 114 | | |
|---|
| 115 | | clean_term_cache($term_id, $taxonomy); |
|---|
| 116 | | |
|---|
| 117 | | do_action("created_term", $term_id, $tt_id); |
|---|
| 118 | | do_action("created_$taxonomy", $term_id, $tt_id); |
|---|
| 119 | | |
|---|
| 120 | | return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|---|
| 121 | | } |
|---|
| 122 | | |
|---|
| 123 | | function wp_delete_object_term_relationships( $object_id, $taxonomies ) { |
|---|
| 124 | | global $wpdb; |
|---|
| 125 | | |
|---|
| 126 | | $object_id = (int) $object_id; |
|---|
| 127 | | |
|---|
| 128 | | if ( !is_array($taxonomies) ) |
|---|
| 129 | | $taxonomies = array($taxonomies); |
|---|
| 130 | | |
|---|
| 131 | | foreach ( $taxonomies as $taxonomy ) { |
|---|
| 132 | | $terms = get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); |
|---|
| 133 | | $in_terms = "'" . implode("', '", $terms) . "'"; |
|---|
| 134 | | $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_terms)"); |
|---|
| 135 | | |
|---|
| 136 | | wp_update_term_count($terms, $taxonomy); |
|---|
| 137 | | } |
|---|
| 138 | | |
|---|
| 139 | | // TODO clear the cache |
|---|
| 140 | | } |
|---|
| 141 | | |
|---|
| 142 | | /** |
|---|
| 143 | | * Removes a term from the database. |
|---|
| 144 | | */ |
|---|
| 145 | | function wp_delete_term( $term, $taxonomy, $args = array() ) { |
|---|
| 146 | | global $wpdb; |
|---|
| 147 | | |
|---|
| 148 | | $term = (int) $term; |
|---|
| 149 | | |
|---|
| 150 | | if ( ! $ids = is_term($term, $taxonomy) ) |
|---|
| 151 | | return false; |
|---|
| 152 | | $tt_id = $ids['term_taxonomy_id']; |
|---|
| 153 | | |
|---|
| 154 | | $defaults = array(); |
|---|
| 155 | | $args = wp_parse_args($args, $defaults); |
|---|
| 156 | | extract($args, EXTR_SKIP); |
|---|
| 157 | | |
|---|
| 158 | | if ( isset($default) ) { |
|---|
| 159 | | $default = (int) $default; |
|---|
| 160 | | if ( ! is_term($default, $taxonomy) ) |
|---|
| 161 | | unset($default); |
|---|
| 162 | | } |
|---|
| 163 | | |
|---|
| 164 | | // Update children to point to new parent |
|---|
| 165 | | if ( is_taxonomy_hierarchical($taxonomy) ) { |
|---|
| 166 | | $term_obj = get_term($term, $taxonomy); |
|---|
| 167 | | $parent = $term_obj->parent; |
|---|
| 168 | | |
|---|
| 169 | | $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = '$parent' WHERE parent = '$term_obj->term_id' AND taxonomy = '$taxonomy'"); |
|---|
| 170 | | } |
|---|
| 171 | | |
|---|
| 172 | | $objects = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$tt_id'"); |
|---|
| 173 | | |
|---|
| 174 | | foreach ( (array) $objects as $object ) { |
|---|
| 175 | | $terms = get_object_terms($object, $taxonomy, 'fields=ids'); |
|---|
| 176 | | if ( 1 == count($terms) && isset($default) ) |
|---|
| 177 | | $terms = array($default); |
|---|
| 178 | | else |
|---|
| 179 | | $terms = array_diff($terms, array($term)); |
|---|
| 180 | | wp_set_object_terms($object, $terms, $taxonomy); |
|---|
| 181 | | } |
|---|
| 182 | | |
|---|
| 183 | | $wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$tt_id'"); |
|---|
| 184 | | |
|---|
| 185 | | // Delete the term if no taxonomies use it. |
|---|
| 186 | | if ( !$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = '$term'") ) |
|---|
| 187 | | $wpdb->query("DELETE FROM $wpdb->terms WHERE term_id = '$term'"); |
|---|
| 188 | | |
|---|
| 189 | | clean_term_cache($term, $taxonomy); |
|---|
| 190 | | |
|---|
| 191 | | do_action("delete_$taxonomy", $term, $tt_id); |
|---|
| 192 | | |
|---|
| 193 | | return true; |
|---|
| 194 | | } |
|---|
| 195 | | |
|---|
| 196 | | function wp_update_term( $term, $taxonomy, $args = array() ) { |
|---|
| 197 | | global $wpdb; |
|---|
| 198 | | |
|---|
| 199 | | if ( ! is_taxonomy($taxonomy) ) |
|---|
| 200 | | return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|---|
| 201 | | |
|---|
| 202 | | $term_id = (int) $term; |
|---|
| 203 | | |
|---|
| 204 | | // First, get all of the original args |
|---|
| 205 | | $term = get_term ($term_id, $taxonomy, ARRAY_A); |
|---|
| 206 | | |
|---|
| 207 | | $term = sanitize_term($term, $taxonomy, 'db'); |
|---|
| 208 | | |
|---|
| 209 | | // Escape data pulled from DB. |
|---|
| 210 | | $term = add_magic_quotes($term); |
|---|
| 211 | | |
|---|
| 212 | | // Merge old and new args with new args overwriting old ones. |
|---|
| 213 | | $args = array_merge($term, $args); |
|---|
| 214 | | |
|---|
| 215 | | $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); |
|---|
| 216 | | $args = wp_parse_args($args, $defaults); |
|---|
| 217 | | extract($args, EXTR_SKIP); |
|---|
| 218 | | |
|---|
| 219 | | if ( empty($slug) ) |
|---|
| 220 | | $slug = sanitize_title($name); |
|---|
| 221 | | else |
|---|
| 222 | | $slug = sanitize_title($slug); |
|---|
| 223 | | |
|---|
| 224 | | if ( $alias_of ) { |
|---|
| 225 | | $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$alias_of'"); |
|---|
| 226 | | if ( $alias->term_group ) { |
|---|
| 227 | | // The alias we want is already in a group, so let's use that one. |
|---|
| 228 | | $term_group = $alias->term_group; |
|---|
| 229 | | } else { |
|---|
| 230 | | // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
|---|
| 231 | | $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1; |
|---|
| 232 | | $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id"); |
|---|
| 233 | | } |
|---|
| 234 | | } |
|---|
| 235 | | |
|---|
| 236 | | $wpdb->query("UPDATE $wpdb->terms SET name = '$name', slug = '$slug', term_group = '$term_group' WHERE term_id = '$term_id'"); |
|---|
| 237 | | |
|---|
| 238 | | if ( empty($slug) ) { |
|---|
| 239 | | $slug = sanitize_title($name, $term_id); |
|---|
| 240 | | $wpdb->query("UPDATE $wpdb->terms SET slug = '$slug' WHERE term_id = '$term_id'"); |
|---|
| 241 | | } |
|---|
| 242 | | |
|---|
| 243 | | $tt_id = $wpdb->get_var("SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = $term_id"); |
|---|
| 244 | | |
|---|
| 245 | | $wpdb->query("UPDATE $wpdb->term_taxonomy SET term_id = '$term_id', taxonomy = '$taxonomy', description = '$description', parent = '$parent' WHERE term_taxonomy_id = '$tt_id'"); |
|---|
| 246 | | |
|---|
| 247 | | do_action("edit_term", $term_id, $tt_id); |
|---|
| 248 | | do_action("edit_$taxonomy", $term_id, $tt_id); |
|---|
| 249 | | |
|---|
| 250 | | $term_id = apply_filters('term_id_filter', $term_id, $tt_id); |
|---|
| 251 | | |
|---|
| 252 | | clean_term_cache($term_id, $taxonomy); |
|---|
| 253 | | |
|---|
| 254 | | do_action("edited_term", $term_id, $tt_id); |
|---|
| 255 | | do_action("edited_$taxonomy", $term_id, $tt_id); |
|---|
| 256 | | |
|---|
| 257 | | return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|---|
| 258 | | } |
|---|
| 259 | | |
|---|
| 260 | | function wp_update_term_count( $terms, $taxonomy ) { |
|---|
| 261 | | global $wpdb; |
|---|
| 262 | | |
|---|
| 263 | | if ( empty($terms) ) |
|---|
| 264 | | return false; |
|---|
| 265 | | |
|---|
| 266 | | if ( !is_array($terms) ) |
|---|
| 267 | | $terms = array($terms); |
|---|
| 268 | | |
|---|
| 269 | | $terms = array_map('intval', $terms); |
|---|
| 270 | | |
|---|
| 271 | | $taxonomy = get_taxonomy($taxonomy); |
|---|
| 272 | | if ( isset($taxonomy->update_count_callback) ) |
|---|
| 273 | | return call_user_func($taxonomy->update_count_callback, $terms); |
|---|
| 274 | | |
|---|
| 275 | | // Default count updater |
|---|
| 276 | | $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$term'"); |
|---|
| 277 | | $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term'"); |
|---|
| 278 | | |
|---|
| 279 | | return true; |
|---|
| 280 | | } |
|---|
| 281 | | |
|---|
| 282 | | /** |
|---|
| 283 | | * Returns the index of a defined term, or 0 (false) if the term doesn't exist. |
|---|
| 284 | | */ |
|---|
| 285 | | function is_term($term, $taxonomy = '') { |
|---|
| 286 | | global $wpdb; |
|---|
| 287 | | |
|---|
| 288 | | if ( is_int($term) ) { |
|---|
| 289 | | $where = "t.term_id = '$term'"; |
|---|
| 290 | | } else { |
|---|
| 291 | | if ( ! $term = sanitize_title($term) ) |
|---|
| 292 | | return 0; |
|---|
| 293 | | $where = "t.slug = '$term'"; |
|---|
| 294 | | } |
|---|
| 295 | | |
|---|
| 296 | | $term_id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms as t WHERE $where"); |
|---|
| 297 | | |
|---|
| 298 | | if ( empty($taxonomy) || empty($term_id) ) |
|---|
| 299 | | return $term_id; |
|---|
| 300 | | |
|---|
| 301 | | return $wpdb->get_row("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = '$taxonomy'", ARRAY_A); |
|---|
| 302 | | } |
|---|
| 303 | | |
|---|
| 304 | | /** |
|---|
| 305 | | * Given an array of terms, returns those that are defined term slugs. Ignores integers. |
|---|
| 306 | | * @param array $terms The term slugs to check for a definition. |
|---|
| 307 | | */ |
|---|
| 308 | | function get_defined_terms($terms) { |
|---|
| 309 | | global $wpdb; |
|---|
| 310 | | |
|---|
| 311 | | foreach ( $terms as $term ) { |
|---|
| 312 | | if ( !is_int($term) ) |
|---|
| 313 | | $searches[] = $term; |
|---|
| 314 | | } |
|---|
| 315 | | |
|---|
| 316 | | $terms = "'" . implode("', '", $searches) . "'"; |
|---|
| 317 | | return $wpdb->get_col("SELECT slug FROM $wpdb->terms WHERE slug IN ($terms)"); |
|---|
| 318 | | } |
|---|
| 319 | | |
|---|
| 320 | | /** |
|---|
| 321 | | * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy |
|---|
| 322 | | * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug). |
|---|
| 323 | | * @param int $object_id The object to relate to. |
|---|
| 324 | | * @param array|int|string $term The slug or id of the term. |
|---|
| 325 | | * @param array|string $taxonomy The context in which to relate the term to the object. |
|---|
| 326 | | */ |
|---|
| 327 | | function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { |
|---|
| 328 | | global $wpdb; |
|---|
| 329 | | |
|---|
| 330 | | $object_id = (int) $object_id; |
|---|
| 331 | | |
|---|
| 332 | | if ( ! is_taxonomy($taxonomy) ) |
|---|
| 333 | | return false; |
|---|
| 334 | | |
|---|
| 335 | | if ( !is_array($terms) ) |
|---|
| 336 | | $terms = array($terms); |
|---|
| 337 | | |
|---|
| 338 | | if ( ! $append ) |
|---|
| 339 | | $old_terms = get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); |
|---|
| 340 | | |
|---|
| 341 | | $tt_ids = array(); |
|---|
| 342 | | $term_ids = array(); |
|---|
| 343 | | |
|---|
| 344 | | foreach ($terms as $term) { |
|---|
| 345 | | if ( !$id = is_term($term, $taxonomy) ) |
|---|
| 346 | | $id = wp_insert_term($term, $taxonomy); |
|---|
| 347 | | $term_ids[] = $id['term_id']; |
|---|
| 348 | | $id = $id['term_taxonomy_id']; |
|---|
| 349 | | $tt_ids[] = $id; |
|---|
| 350 | | |
|---|
| 351 | | if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) |
|---|
| 352 | | continue; |
|---|
| 353 | | $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); |
|---|
| 354 | | } |
|---|
| 355 | | |
|---|
| 356 | | wp_update_term_count($tt_ids, $taxonomy); |
|---|
| 357 | | |
|---|
| 358 | | if ( ! $append ) { |
|---|
| 359 | | $delete_terms = array_diff($old_terms, $tt_ids); |
|---|
| 360 | | if ( $delete_terms ) { |
|---|
| 361 | | $delete_terms = "'" . implode("', '", $delete_terms) . "'"; |
|---|
| 362 | | $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)"); |
|---|
| 363 | | $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = count - 1 WHERE term_taxonomy_id IN ($delete_terms)"); |
|---|
| 364 | | wp_update_term_count($delete_terms, $taxonomy); |
|---|
| 365 | | } |
|---|
| 366 | | } |
|---|
| 367 | | |
|---|
| 368 | | // TODO clean old_terms. Need term_id instead of tt_id |
|---|
| 369 | | clean_term_cache($term_ids, $taxonomy); |
|---|
| 370 | | |
|---|
| 371 | | return $tt_ids; |
|---|
| 372 | | } |
|---|
| 373 | | |
|---|
| 374 | | function get_objects_in_term( $terms, $taxonomies, $args = array() ) { |
|---|
| 375 | | global $wpdb; |
|---|
| 376 | | |
|---|
| 377 | | if ( !is_array( $terms) ) |
|---|
| 378 | | $terms = array($terms); |
|---|
| 379 | | |
|---|
| 380 | | if ( !is_array($taxonomies) ) |
|---|
| 381 | | $taxonomies = array($taxonomies); |
|---|
| 382 | | |
|---|
| 383 | | $defaults = array('order' => 'ASC'); |
|---|
| 384 | | $args = wp_parse_args( $args, $defaults ); |
|---|
| 385 | | extract($args, EXTR_SKIP); |
|---|
| 386 | | |
|---|
| 387 | | $terms = array_map('intval', $terms); |
|---|
| 388 | | |
|---|
| 389 | | $taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
|---|
| 390 | | $terms = "'" . implode("', '", $terms) . "'"; |
|---|
| 391 | | |
|---|
| 392 | | $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($terms) ORDER BY tr.object_id $order"); |
|---|
| 393 | | |
|---|
| 394 | | if ( ! $object_ids ) |
|---|
| 395 | | return array(); |
|---|
| 396 | | |
|---|
| 397 | | return $object_ids; |
|---|
| 398 | | } |
|---|
| 399 | | |
|---|
| 461 | | return $terms; |
|---|
| | 90 | return $object_ids; |
|---|
| | 91 | } |
|---|
| | 92 | |
|---|
| | 93 | function &get_term(&$term, $taxonomy, $output = OBJECT) { |
|---|
| | 94 | global $wpdb; |
|---|
| | 95 | |
|---|
| | 96 | if ( empty($term) ) |
|---|
| | 97 | return null; |
|---|
| | 98 | |
|---|
| | 99 | if ( ! is_taxonomy($taxonomy) ) |
|---|
| | 100 | return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|---|
| | 101 | |
|---|
| | 102 | if ( is_object($term) ) { |
|---|
| | 103 | wp_cache_add($term->term_id, $term, $taxonomy); |
|---|
| | 104 | $_term = $term; |
|---|
| | 105 | } else { |
|---|
| | 106 | $term = (int) $term; |
|---|
| | 107 | if ( ! $_term = wp_cache_get($term, $taxonomy) ) { |
|---|
| | 108 | $_term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = '$term' LIMIT 1"); |
|---|
| | 109 | wp_cache_add($term, $_term, $taxonomy); |
|---|
| | 110 | } |
|---|
| | 111 | } |
|---|
| | 112 | |
|---|
| | 113 | $_term = apply_filters('get_term', $_term, $taxonomy); |
|---|
| | 114 | $_term = apply_filters("get_$taxonomy", $_term, $taxonomy); |
|---|
| | 115 | |
|---|
| | 116 | if ( $output == OBJECT ) { |
|---|
| | 117 | return $_term; |
|---|
| | 118 | } elseif ( $output == ARRAY_A ) { |
|---|
| | 119 | return get_object_vars($_term); |
|---|
| | 120 | } elseif ( $output == ARRAY_N ) { |
|---|
| | 121 | return array_values(get_object_vars($_term)); |
|---|
| | 122 | } else { |
|---|
| | 123 | return $_term; |
|---|
| | 124 | } |
|---|
| | 125 | } |
|---|
| | 126 | |
|---|
| | 127 | function get_term_by($field, $value, $taxonomy, $output = OBJECT) { |
|---|
| | 128 | global $wpdb; |
|---|
| | 129 | |
|---|
| | 130 | if ( ! is_taxonomy($taxonomy) ) |
|---|
| | 131 | return false; |
|---|
| | 132 | |
|---|
| | 133 | if ( 'slug' == $field ) { |
|---|
| | 134 | $field = 't.slug'; |
|---|
| | 135 | $value = sanitize_title($field); |
|---|
| | 136 | if ( empty($value) ) |
|---|
| | 137 | return false; |
|---|
| | 138 | } else if ( 'name' == $field ) { |
|---|
| | 139 | // Assume already escaped |
|---|
| | 140 | $field = 't.name'; |
|---|
| | 141 | } else { |
|---|
| | 142 | $field = 't.term_id'; |
|---|
| | 143 | $value = (int) $value; |
|---|
| | 144 | } |
|---|
| | 145 | |
|---|
| | 146 | $term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND $field = '$value' LIMIT 1"); |
|---|
| | 147 | if ( !$term ) |
|---|
| | 148 | return false; |
|---|
| | 149 | |
|---|
| | 150 | wp_cache_add($term->term_id, $term, $taxonomy); |
|---|
| | 151 | |
|---|
| | 152 | if ( $output == OBJECT ) { |
|---|
| | 153 | return $term; |
|---|
| | 154 | } elseif ( $output == ARRAY_A ) { |
|---|
| | 155 | return get_object_vars($term); |
|---|
| | 156 | } elseif ( $output == ARRAY_N ) { |
|---|
| | 157 | return array_values(get_object_vars($term)); |
|---|
| | 158 | } else { |
|---|
| | 159 | return $term; |
|---|
| | 160 | } |
|---|
| | 161 | } |
|---|
| | 162 | |
|---|
| | 163 | function get_term_children( $term, $taxonomy ) { |
|---|
| | 164 | if ( ! is_taxonomy($taxonomy) ) |
|---|
| | 165 | return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|---|
| | 166 | |
|---|
| | 167 | $terms = _get_term_hierarchy($taxonomy); |
|---|
| | 168 | |
|---|
| | 169 | if ( ! isset($terms[$term]) ) |
|---|
| | 170 | return array(); |
|---|
| | 171 | |
|---|
| | 172 | $children = $terms[$term]; |
|---|
| | 173 | |
|---|
| | 174 | foreach ( $terms[$term] as $child ) { |
|---|
| | 175 | if ( isset($terms[$child]) ) |
|---|
| | 176 | $children = array_merge($children, get_term_children($child, $taxonomy)); |
|---|
| | 177 | } |
|---|
| | 178 | |
|---|
| | 179 | return $children; |
|---|
| | 180 | } |
|---|
| | 181 | |
|---|
| | 182 | function get_term_field( $field, $term, $taxonomy, $context = 'display' ) { |
|---|
| | 183 | $term = (int) $term; |
|---|
| | 184 | $term = get_term( $term, $taxonomy ); |
|---|
| | 185 | |
|---|
| | 186 | if ( is_wp_error($term) ) |
|---|
| | 187 | return $term; |
|---|
| | 188 | |
|---|
| | 189 | if ( !is_object($term) ) |
|---|
| | 190 | return ''; |
|---|
| | 191 | |
|---|
| | 192 | if ( !isset($term->$field) ) |
|---|
| | 193 | return ''; |
|---|
| | 194 | |
|---|
| | 195 | return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context); |
|---|
| | 196 | } |
|---|
| | 197 | |
|---|
| | 198 | function get_term_to_edit( $id, $taxonomy ) { |
|---|
| | 199 | $term = get_term( $id, $taxonomy ); |
|---|
| | 200 | |
|---|
| | 201 | if ( is_wp_error($term) ) |
|---|
| | 202 | return $term; |
|---|
| | 203 | |
|---|
| | 204 | if ( !is_object($term) ) |
|---|
| | 205 | return ''; |
|---|
| | 206 | |
|---|
| | 207 | return sanitize_term($term, $taxonomy, 'edit'); |
|---|
| 636 | | $term = (int) $term; |
|---|
| 637 | | if ( ! $_term = wp_cache_get($term, $taxonomy) ) { |
|---|
| 638 | | $_term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = '$term' LIMIT 1"); |
|---|
| 639 | | wp_cache_add($term, $_term, $taxonomy); |
|---|
| 640 | | } |
|---|
| 641 | | } |
|---|
| 642 | | |
|---|
| 643 | | $_term = apply_filters('get_term', $_term, $taxonomy); |
|---|
| 644 | | $_term = apply_filters("get_$taxonomy", $_term, $taxonomy); |
|---|
| 645 | | |
|---|
| 646 | | if ( $output == OBJECT ) { |
|---|
| 647 | | return $_term; |
|---|
| 648 | | } elseif ( $output == ARRAY_A ) { |
|---|
| 649 | | return get_object_vars($_term); |
|---|
| 650 | | } elseif ( $output == ARRAY_N ) { |
|---|
| 651 | | return array_values(get_object_vars($_term)); |
|---|
| 652 | | } else { |
|---|
| 653 | | return $_term; |
|---|
| 654 | | } |
|---|
| 655 | | } |
|---|
| 656 | | |
|---|
| 657 | | function get_term_by($field, $value, $taxonomy, $output = OBJECT) { |
|---|
| 658 | | global $wpdb; |
|---|
| 659 | | |
|---|
| 660 | | if ( ! is_taxonomy($taxonomy) ) |
|---|
| 661 | | return false; |
|---|
| 662 | | |
|---|
| 663 | | if ( 'slug' == $field ) { |
|---|
| 664 | | $field = 't.slug'; |
|---|
| 665 | | $value = sanitize_title($field); |
|---|
| 666 | | if ( empty($value) ) |
|---|
| 667 | | return false; |
|---|
| 668 | | } else if ( 'name' == $field ) { |
|---|
| 669 | | // Assume already escaped |
|---|
| 670 | | $field = 't.name'; |
|---|
| 671 | | } else { |
|---|
| 672 | | $field = 't.term_id'; |
|---|
| 673 | | $value = (int) $value; |
|---|
| 674 | | } |
|---|
| 675 | | |
|---|
| 676 | | $term = $wpdb->get_row("SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = '$taxonomy' AND $field = '$value' LIMIT 1"); |
|---|
| 677 | | if ( !$term ) |
|---|
| 678 | | return false; |
|---|
| 679 | | |
|---|
| 680 | | wp_cache_add($term->term_id, $term, $taxonomy); |
|---|
| 681 | | |
|---|
| 682 | | if ( $output == OBJECT ) { |
|---|
| 683 | | return $term; |
|---|
| 684 | | } elseif ( $output == ARRAY_A ) { |
|---|
| 685 | | return get_object_vars($term); |
|---|
| 686 | | } elseif ( $output == ARRAY_N ) { |
|---|
| 687 | | return array_values(get_object_vars($term)); |
|---|
| 688 | | } else { |
|---|
| 689 | | return $term; |
|---|
| 690 | | } |
|---|
| 691 | | } |
|---|
| 692 | | |
|---|
| 693 | | function get_term_children( $term, $taxonomy ) { |
|---|
| 694 | | $terms = _get_term_hierarchy($taxonomy); |
|---|
| 695 | | |
|---|
| 696 | | if ( ! isset($terms[$term]) ) |
|---|
| 697 | | return array(); |
|---|
| 698 | | |
|---|
| 699 | | $children = $terms[$term]; |
|---|
| 700 | | |
|---|
| 701 | | foreach ( $terms[$term] as $child ) { |
|---|
| 702 | | if ( isset($terms[$child]) ) |
|---|
| 703 | | $children = array_merge($children, get_term_children($child, $taxonomy)); |
|---|
| 704 | | } |
|---|
| 705 | | |
|---|
| 706 | | return $children; |
|---|
| 707 | | } |
|---|
| 708 | | |
|---|
| 709 | | function get_term_field( $field, $term, $taxonomy, $context = 'display' ) { |
|---|
| 710 | | $term = (int) $term; |
|---|
| 711 | | $term = get_term( $term, $taxonomy ); |
|---|
| 712 | | |
|---|
| 713 | | if ( !is_object($term) ) |
|---|
| 714 | | return ''; |
|---|
| 715 | | |
|---|
| 716 | | if ( !isset($term->$field) ) |
|---|
| 717 | | return ''; |
|---|
| 718 | | |
|---|
| 719 | | return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context); |
|---|
| 720 | | } |
|---|
| 721 | | |
|---|
| 722 | | function get_term_to_edit( $id, $taxonomy ) { |
|---|
| 723 | | $term = get_term( $id, $taxonomy ); |
|---|
| 724 | | |
|---|
| 725 | | if ( !is_object($term) ) |
|---|
| 726 | | return ''; |
|---|
| 727 | | |
|---|
| 728 | | return sanitize_term($term, $taxonomy, 'edit'); |
|---|
| | 386 | if ( ! $term = sanitize_title($term) ) |
|---|
| | 387 | return 0; |
|---|
| | 388 | $where = "t.slug = '$term'"; |
|---|
| | 389 | } |
|---|
| | 390 | |
|---|
| | 391 | $term_id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms as t WHERE $where"); |
|---|
| | 392 | |
|---|
| | 393 | if ( empty($taxonomy) || empty($term_id) ) |
|---|
| | 394 | return $term_id; |
|---|
| | 395 | |
|---|
| | 396 | return $wpdb->get_row("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = '$taxonomy'", ARRAY_A); |
|---|
| | 447 | function wp_count_terms( $taxonomy, $args = array() ) { |
|---|
| | 448 | global $wpdb; |
|---|
| | 449 | |
|---|
| | 450 | $defaults = array('ignore_empty' => false); |
|---|
| | 451 | $args = wp_parse_args($args, $defaults); |
|---|
| | 452 | extract($args, EXTR_SKIP); |
|---|
| | 453 | |
|---|
| | 454 | $where = ''; |
|---|
| | 455 | if ( $ignore_empty ) |
|---|
| | 456 | $where = 'AND count > 0'; |
|---|
| | 457 | |
|---|
| | 458 | return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = '$taxonomy' $where"); |
|---|
| | 459 | } |
|---|
| | 460 | |
|---|
| | 461 | function wp_delete_object_term_relationships( $object_id, $taxonomies ) { |
|---|
| | 462 | global $wpdb; |
|---|
| | 463 | |
|---|
| | 464 | $object_id = (int) $object_id; |
|---|
| | 465 | |
|---|
| | 466 | if ( !is_array($taxonomies) ) |
|---|
| | 467 | $taxonomies = array($taxonomies); |
|---|
| | 468 | |
|---|
| | 469 | foreach ( $taxonomies as $taxonomy ) { |
|---|
| | 470 | $terms = get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); |
|---|
| | 471 | $in_terms = "'" . implode("', '", $terms) . "'"; |
|---|
| | 472 | $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id IN ($in_terms)"); |
|---|
| | 473 | |
|---|
| | 474 | wp_update_term_count($terms, $taxonomy); |
|---|
| | 475 | } |
|---|
| | 476 | |
|---|
| | 477 | // TODO clear the cache |
|---|
| | 478 | } |
|---|
| | 479 | |
|---|
| | 480 | /** |
|---|
| | 481 | * Removes a term from the database. |
|---|
| | 482 | */ |
|---|
| | 483 | function wp_delete_term( $term, $taxonomy, $args = array() ) { |
|---|
| | 484 | global $wpdb; |
|---|
| | 485 | |
|---|
| | 486 | $term = (int) $term; |
|---|
| | 487 | |
|---|
| | 488 | if ( ! $ids = is_term($term, $taxonomy) ) |
|---|
| | 489 | return false; |
|---|
| | 490 | $tt_id = $ids['term_taxonomy_id']; |
|---|
| | 491 | |
|---|
| | 492 | $defaults = array(); |
|---|
| | 493 | $args = wp_parse_args($args, $defaults); |
|---|
| | 494 | extract($args, EXTR_SKIP); |
|---|
| | 495 | |
|---|
| | 496 | if ( isset($default) ) { |
|---|
| | 497 | $default = (int) $default; |
|---|
| | 498 | if ( ! is_term($default, $taxonomy) ) |
|---|
| | 499 | unset($default); |
|---|
| | 500 | } |
|---|
| | 501 | |
|---|
| | 502 | // Update children to point to new parent |
|---|
| | 503 | if ( is_taxonomy_hierarchical($taxonomy) ) { |
|---|
| | 504 | $term_obj = get_term($term, $taxonomy); |
|---|
| | 505 | $parent = $term_obj->parent; |
|---|
| | 506 | |
|---|
| | 507 | $wpdb->query("UPDATE $wpdb->term_taxonomy SET parent = '$parent' WHERE parent = '$term_obj->term_id' AND taxonomy = '$taxonomy'"); |
|---|
| | 508 | } |
|---|
| | 509 | |
|---|
| | 510 | $objects = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '$tt_id'"); |
|---|
| | 511 | |
|---|
| | 512 | foreach ( (array) $objects as $object ) { |
|---|
| | 513 | $terms = get_object_terms($object, $taxonomy, 'fields=ids'); |
|---|
| | 514 | if ( 1 == count($terms) && isset($default) ) |
|---|
| | 515 | $terms = array($default); |
|---|
| | 516 | else |
|---|
| | 517 | $terms = array_diff($terms, array($term)); |
|---|
| | 518 | wp_set_object_terms($object, $terms, $taxonomy); |
|---|
| | 519 | } |
|---|
| | 520 | |
|---|
| | 521 | $wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = '$tt_id'"); |
|---|
| | 522 | |
|---|
| | 523 | // Delete the term if no taxonomies use it. |
|---|
| | 524 | if ( !$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = '$term'") ) |
|---|
| | 525 | $wpdb->query("DELETE FROM $wpdb->terms WHERE term_id = '$term'"); |
|---|
| | 526 | |
|---|
| | 527 | clean_term_cache($term, $taxonomy); |
|---|
| | 528 | |
|---|
| | 529 | do_action("delete_$taxonomy", $term, $tt_id); |
|---|
| | 530 | |
|---|
| | 531 | return true; |
|---|
| | 532 | } |
|---|
| | 533 | |
|---|
| | 534 | /** |
|---|
| | 535 | * Returns the terms associated with the given object(s), in the supplied taxonomies. |
|---|
| | 536 | * @param int|array $object_id The id of the object(s)) to retrieve for. |
|---|
| | 537 | * @param string|array $taxonomies The taxonomies to retrieve terms from. |
|---|
| | 538 | * @return array The requested term data. |
|---|
| | 539 | */ |
|---|
| | 540 | function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { |
|---|
| | 541 | global $wpdb; |
|---|
| | 542 | |
|---|
| | 543 | if ( !is_array($taxonomies) ) |
|---|
| | 544 | $taxonomies = array($taxonomies); |
|---|
| | 545 | |
|---|
| | 546 | foreach ( $taxonomies as $taxonomy ) { |
|---|
| | 547 | if ( ! is_taxonomy($taxonomy) ) |
|---|
| | 548 | return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|---|
| | 549 | } |
|---|
| | 550 | |
|---|
| | 551 | if ( !is_array($object_ids) ) |
|---|
| | 552 | $object_ids = array($object_ids); |
|---|
| | 553 | $object_ids = array_map('intval', $object_ids); |
|---|
| | 554 | |
|---|
| | 555 | $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); |
|---|
| | 556 | $args = wp_parse_args( $args, $defaults ); |
|---|
| | 557 | extract($args, EXTR_SKIP); |
|---|
| | 558 | |
|---|
| | 559 | if ( 'count' == $orderby ) |
|---|
| | 560 | $orderby = 'tt.count'; |
|---|
| | 561 | else if ( 'name' == $orderby ) |
|---|
| | 562 | $orderby = 't.name'; |
|---|
| | 563 | |
|---|
| | 564 | $taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
|---|
| | 565 | $object_ids = implode(', ', $object_ids); |
|---|
| | 566 | |
|---|
| | 567 | if ( 'all' == $fields ) |
|---|
| | 568 | $select_this = 't.*, tt.*'; |
|---|
| | 569 | else if ( 'ids' == $fields ) |
|---|
| | 570 | $select_this = 't.term_id'; |
|---|
| | 571 | else if ( 'all_with_object_id' == $fields ) |
|---|
| | 572 | $select_this = 't.*, tt.*, tr.object_id'; |
|---|
| | 573 | |
|---|
| | 574 | $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) ORDER BY $orderby $order"; |
|---|
| | 575 | |
|---|
| | 576 | if ( 'all' == $fields || 'all_with_object_id' == $fields ) { |
|---|
| | 577 | $terms = $wpdb->get_results($query); |
|---|
| | 578 | update_term_cache($terms); |
|---|
| | 579 | } else if ( 'ids' == $fields ) { |
|---|
| | 580 | $terms = $wpdb->get_col($query); |
|---|
| | 581 | } else if ( 'tt_ids' == $fields ) { |
|---|
| | 582 | $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) ORDER BY tr.term_taxonomy_id $order"); |
|---|
| | 583 | } |
|---|
| | 584 | |
|---|
| | 585 | if ( ! $terms ) |
|---|
| | 586 | return array(); |
|---|
| | 587 | |
|---|
| | 588 | return $terms; |
|---|
| | 589 | } |
|---|
| | 590 | |
|---|
| | 591 | /** |
|---|
| | 592 | * Adds a new term to the database. Optionally marks it as an alias of an existing term. |
|---|
| | 593 | * @param int|string $term The term to add or update. |
|---|
| | 594 | * @param string $taxonomy The taxonomy to which to add the term |
|---|
| | 595 | * @param int|string $alias_of The id or slug of the new term's alias. |
|---|
| | 596 | */ |
|---|
| | 597 | function wp_insert_term( $term, $taxonomy, $args = array() ) { |
|---|
| | 598 | global $wpdb; |
|---|
| | 599 | |
|---|
| | 600 | if ( ! is_taxonomy($taxonomy) ) |
|---|
| | 601 | return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); |
|---|
| | 602 | |
|---|
| | 603 | $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); |
|---|
| | 604 | $args = wp_parse_args($args, $defaults); |
|---|
| | 605 | $args['name'] = $term; |
|---|
| | 606 | $args = sanitize_term($args, $taxonomy, 'db'); |
|---|
| | 607 | extract($args, EXTR_SKIP); |
|---|
| | 608 | |
|---|
| | 609 | if ( empty($slug) ) |
|---|
| | 610 | $slug = sanitize_title($name); |
|---|
| | 611 | else |
|---|
| | 612 | $slug = sanitize_title($slug); |
|---|
| | 613 | |
|---|
| | 614 | $term_group = 0; |
|---|
| | 615 | if ( $alias_of ) { |
|---|
| | 616 | $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$alias_of'"); |
|---|
| | 617 | if ( $alias->term_group ) { |
|---|
| | 618 | // The alias we want is already in a group, so let's use that one. |
|---|
| | 619 | $term_group = $alias->term_group; |
|---|
| | 620 | } else { |
|---|
| | 621 | // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
|---|
| | 622 | $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms GROUP BY term_group") + 1; |
|---|
| | 623 | $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id"); |
|---|
| | 624 | } |
|---|
| | 625 | } |
|---|
| | 626 | |
|---|
| | 627 | if ( ! $term_id = is_term($slug) ) { |
|---|
| | 628 | $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')"); |
|---|
| | 629 | $term_id = (int) $wpdb->insert_id; |
|---|
| | 630 | } |
|---|
| | 631 | |
|---|
| | 632 | if ( empty($slug) ) { |
|---|
| | 633 | $slug = sanitize_title($slug, $term_id); |
|---|
| | 634 | $wpdb->query("UPDATE $wpdb->terms SET slug = '$slug' WHERE term_id = '$term_id'"); |
|---|
| | 635 | } |
|---|
| | 636 | |
|---|
| | 637 | $tt_id = $wpdb->get_var("SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = $term_id"); |
|---|
| | 638 | |
|---|
| | 639 | if ( !empty($tt_id) ) |
|---|
| | 640 | return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|---|
| | 641 | |
|---|
| | 642 | $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')"); |
|---|
| | 643 | $tt_id = (int) $wpdb->insert_id; |
|---|
| | 644 | |
|---|
| | 645 | do_action("create_term", $term_id, $tt_id); |
|---|
| | 646 | do_action("create_$taxonomy", $term_id, $tt_id); |
|---|
| | 647 | |
|---|
| | 648 | $term_id = apply_filters('term_id_filter', $term_id, $tt_id); |
|---|
| | 649 | |
|---|
| | 650 | clean_term_cache($term_id, $taxonomy); |
|---|
| | 651 | |
|---|
| | 652 | do_action("created_term", $term_id, $tt_id); |
|---|
| | 653 | do_action("created_$taxonomy", $term_id, $tt_id); |
|---|
| | 654 | |
|---|
| | 655 | return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|---|
| | 656 | } |
|---|
| | 657 | |
|---|
| | 658 | /** |
|---|
| | 659 | * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy |
|---|
| | 660 | * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug). |
|---|
| | 661 | * @param int $object_id The object to relate to. |
|---|
| | 662 | * @param array|int|string $term The slug or id of the term. |
|---|
| | 663 | * @param array|string $taxonomy The context in which to relate the term to the object. |
|---|
| | 664 | */ |
|---|
| | 665 | function wp_set_object_terms($object_id, $terms, $taxonomy, $append = false) { |
|---|
| | 666 | global $wpdb; |
|---|
| | 667 | |
|---|
| | 668 | $object_id = (int) $object_id; |
|---|
| | 669 | |
|---|
| | 670 | if ( ! is_taxonomy($taxonomy) ) |
|---|
| | 671 | return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); |
|---|
| | 672 | |
|---|
| | 673 | if ( !is_array($terms) ) |
|---|
| | 674 | $terms = array($terms); |
|---|
| | 675 | |
|---|
| | 676 | if ( ! $append ) |
|---|
| | 677 | $old_terms = wp_get_object_terms($object_id, $taxonomy, 'fields=tt_ids'); |
|---|
| | 678 | |
|---|
| | 679 | $tt_ids = array(); |
|---|
| | 680 | $term_ids = array(); |
|---|
| | 681 | |
|---|
| | 682 | foreach ($terms as $term) { |
|---|
| | 683 | if ( !$id = is_term($term, $taxonomy) ) |
|---|
| | 684 | $id = wp_insert_term($term, $taxonomy); |
|---|
| | 685 | $term_ids[] = $id['term_id']; |
|---|
| | 686 | $id = $id['term_taxonomy_id']; |
|---|
| | 687 | $tt_ids[] = $id; |
|---|
| | 688 | |
|---|
| | 689 | if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) |
|---|
| | 690 | continue; |
|---|
| | 691 | $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); |
|---|
| | 692 | } |
|---|
| | 693 | |
|---|
| | 694 | wp_update_term_count($tt_ids, $taxonomy); |
|---|
| | 695 | |
|---|
| | 696 | if ( ! $append ) { |
|---|
| | 697 | $delete_terms = array_diff($old_terms, $tt_ids); |
|---|
| | 698 | &n |
|---|