| 1 | <?php |
| 2 | |
| 3 | class WPTaxonomy { |
| 4 | |
| 5 | // This will look like (1 => 'post_category', 2 => 'link_category') etc. |
| 6 | var $taxonomies; |
| 7 | |
| 8 | function WPTaxonomy() { |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Adds a new term to the database. Optionally marks it as an alias of an existing term. |
| 13 | * @param string $term The term to add. |
| 14 | * @param int|string $alias_of The id or slug of the new term's alias. |
| 15 | */ |
| 16 | function add_term($term, $alias_of = '') { |
| 17 | global $wpdb; |
| 18 | $term_slug = sanitize_title($term); |
| 19 | if ($alias_of) { |
| 20 | $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE term_slug = '$alias_of'"); |
| 21 | if ($alias->term_group) { |
| 22 | // The alias we want is already in a group, so let's use that one. |
| 23 | $term_group = $alias->term_group; |
| 24 | } else { |
| 25 | // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. |
| 26 | $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1; |
| 27 | $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id"); |
| 28 | } |
| 29 | } else { |
| 30 | $term_group = 0; |
| 31 | } |
| 32 | // Now add or replace the term. This works because we have a UNIQUE key on term_slug. |
| 33 | $wpdb->query("REPLACE INTO $wpdb->terms (term_name, term_slug, term_group) VALUES ($term, $term_slug, $term_group)"); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Removes a term from the database. |
| 38 | */ |
| 39 | function remove_term() { |
| 40 | |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * Returns the index of a defined term, or 0 (false) if the term doesn't exist. |
| 46 | */ |
| 47 | function is_defined_term($term) { |
| 48 | global $wpdb; |
| 49 | return $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE term_slug = '$term'"); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Given an array of terms, returns those that are defined term slugs. Ignores integers. |
| 54 | * @param array $terms The term slugs to check for a definition. |
| 55 | */ |
| 56 | function get_defined_terms($terms) { |
| 57 | global $wpdb; |
| 58 | foreach ($terms as $term) { |
| 59 | if (!is_int($term)) { |
| 60 | $searches[] = $term; |
| 61 | } |
| 62 | } |
| 63 | $terms = "'" . implode("', '", $searches) . "'"; |
| 64 | return $wpdb->get_col("SELECT term_slug FROM $wpdb->terms WHERE term_slug IN ($terms)"); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Adds a term taxonomy entry or increments the count if it already exists. |
| 69 | * @param int|string $term The term to add. |
| 70 | * @param string $taxonomy The context. |
| 71 | * @param string $description An optional description of the relationship. |
| 72 | * @param int $parent The parent of the term in this context. |
| 73 | */ |
| 74 | function add_term_taxonomy($term, $taxonomy, $description = '', $parent = 0) { |
| 75 | $clause = (is_int($term)) ? "t.term_id = $term" : "t.term_slug = '$term'"; |
| 76 | if ($count = $wpdb->query("SELECT tt.count FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '$taxonomy' AND $clause")) { |
| 77 | // We'll be updating whatever's there, and incrementing the count. |
| 78 | $count++; |
| 79 | $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = $count"); |
| 80 | } else { |
| 81 | // There was no existing entry for this term and taxonomy type, so add a new one, using the supplied description and parent, with a count of 1. |
| 82 | $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, term_description, parent, count) SELECT term_id, '$taxonomy', '$description', $parent, 1 FROM $wpdb->terms AS t1 WHERE $clause"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Removes a term from a particular taxonomy or set of taxonomies. Attaches orphaned children in one of various schemes. |
| 88 | * @param int|string $term The slug or id of the term. |
| 89 | * @param string|array $taxonomy The taxonomy or taxonomies to remove from. |
| 90 | * @param string (delete|parent|root|ignore) $attach_children. Where to attach orphaned children. |
| 91 | */ |
| 92 | function remove_term_taxonomy($term, $taxonomy, $attach_children = 'root') { |
| 93 | |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy |
| 98 | * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the term_slug). |
| 99 | * @param array|int|string $term The slug or id of the term. |
| 100 | * @param int $object_id The object to relate to. |
| 101 | * @param array|string $taxonomies The context(s) in which to relate the term to the object. |
| 102 | */ |
| 103 | function add_term_relationship($terms, $object_id, $taxonomies) { |
| 104 | global $wpdb; |
| 105 | |
| 106 | if (!is_array($taxonomies)) { |
| 107 | $taxonomies = array($taxonomies); |
| 108 | } |
| 109 | |
| 110 | if (!is_array($terms)) { |
| 111 | $terms = array($terms); |
| 112 | } |
| 113 | |
| 114 | $defined_terms = $this->get_defined_terms($terms); |
| 115 | |
| 116 | foreach ($terms as $term) { |
| 117 | if (!is_int($term)) { |
| 118 | if (!isset($defined_terms[$term])) { |
| 119 | $new_terms[] = $term; |
| 120 | } |
| 121 | $term_slugs[] = $term; |
| 122 | } else { |
| 123 | $term_ids[] = $term; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if ( isset($new_terms) ) { |
| 128 | // At least one of the terms specified doesn't exist, so add any new ones, using the term_slug as the name. |
| 129 | $this->add_terms($new_terms); |
| 130 | } |
| 131 | |
| 132 | $term_clause = isset($term_ids) ? 'tt.term_id IN (' . implode(', ', $term_ids) . ')' : ''; |
| 133 | if (isset($term_slugs)) { |
| 134 | if ($term_clause) { |
| 135 | $term_clause .= ' OR '; |
| 136 | } |
| 137 | $term_clause .= "t.term_slug IN ('" . implode("', '", $term_slugs) . "')"; |
| 138 | $term_join = "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id"; |
| 139 | } else { |
| 140 | $term_join = ''; |
| 141 | } |
| 142 | |
| 143 | // Now add or increment the term taxonomy relationships. This is inefficient at the moment. |
| 144 | foreach ($taxonomies as $taxonomy) { |
| 145 | foreach ($terms as $term) { |
| 146 | $this->add_term_taxonomy($term, $taxonomy); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | $taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
| 151 | |
| 152 | // Finally, relate the term and taxonomy to the object. |
| 153 | $wpdb->query("INSERT INTO $wpdb->term_relationships(object_id, term_taxonomy_id) SELECT $object_id, term_taxonomy_id FROM $wpdb->term_taxonomy AS tt $term_join WHERE ($term_clause) AND tt.taxonomy IN ($taxonomies)"); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns the terms associated with the given object(s), in the supplied taxonomies. |
| 158 | * @param int|array $object_id The id of the object(s)) to retrieve for. |
| 159 | * @param string|array $taxonomies The taxonomies to retrieve terms from. |
| 160 | * @return array The requested term data. |
| 161 | */ |
| 162 | function get_object_terms($object_id, $taxonomy) { |
| 163 | global $wpdb; |
| 164 | $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy; |
| 165 | $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; |
| 166 | |
| 167 | $taxonomies = "'" . implode("', '", $taxonomies) . "'"; |
| 168 | $object_ids = implode(', ', $object_ids); |
| 169 | |
| 170 | if ( $taxonomy_data = $wpdb->get_results("SELECT tr.object_id, tt.taxonomy, t.* 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)") ) { |
| 171 | if ($single_taxonomy && $single_object) { |
| 172 | // Just one kind of taxonomy for one object. |
| 173 | return $taxonomy_data; |
| 174 | } else { |
| 175 | foreach ($taxonomy_data as $data) { |
| 176 | if ($single_taxonomy) { |
| 177 | // Many objects, one taxonomy type. |
| 178 | $return[$data->object_id][] = $data; |
| 179 | } elseif ($single_object) { |
| 180 | // One object, many taxonomies. |
| 181 | $return[$data->taxonomy][] = $data; |
| 182 | } else { |
| 183 | // Many objects, many taxonomies. |
| 184 | $return[$data->object_id][$data->taxonomy][] = $data; |
| 185 | } |
| 186 | } |
| 187 | return $return; |
| 188 | } |
| 189 | } else { |
| 190 | return array(); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | ?> |