Changeset 5515
- Timestamp:
- 05/22/2007 08:39:26 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/upgrade-schema.php
r5510 r5515 32 32 object_id bigint(20) NOT NULL default 0, 33 33 term_taxonomy_id bigint(20) NOT NULL default 0, 34 PRIMARY KEY (object_id ),34 PRIMARY KEY (object_id, term_taxonomy_id), 35 35 KEY (term_taxonomy_id) 36 36 ) $charset_collate; -
trunk/wp-includes/post.php
r5510 r5515 795 795 $tags = array(); 796 796 $tags = (is_array($tags)) ? $tags : explode( ',', $tags ); 797 add_term_relationship($tags, $post_id, 'post_tag');797 wp_set_object_terms($post_id, $tags, 'post_tag', $append); 798 798 } 799 799 -
trunk/wp-includes/taxonomy.php
r5510 r5515 44 44 45 45 $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')"); 46 // TODO: Maybe return both term_id and tt_id.47 return $term_id;46 $tt_id = (int) $wpdb->insert_id; 47 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); 48 48 } 49 49 … … 59 59 function is_term($term, $taxonomy = '') { 60 60 global $wpdb; 61 if ( ! $term = sanitize_title($term) )62 return 0;63 61 64 return $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug = '$term'"); 62 if ( is_int($term) ) { 63 $where = "t.term_id = '$term'"; 64 } else { 65 if ( ! $term = sanitize_title($term) ) 66 return 0; 67 $where = "t.slug = '$term'"; 68 } 69 70 $term_id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms as t WHERE $where"); 71 72 if ( empty($taxonomy) || empty($term_id) ) 73 return $term_id; 74 75 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); 65 76 } 66 77 … … 84 95 * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy 85 96 * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug). 97 * @param int $object_id The object to relate to. 86 98 * @param array|int|string $term The slug or id of the term. 87 * @param int $object_id The object to relate to.88 99 * @param array|string $taxonomies The context(s) in which to relate the term to the object. 89 100 */ 90 function add_term_relationship($terms, $object_id, $taxonomies) {101 function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) { 91 102 global $wpdb; 92 103 104 $object_id = (int) $object_id; 105 93 106 if ( !is_array($taxonomies) ) 94 107 $taxonomies = array($taxonomies); … … 96 109 if ( !is_array($terms) ) 97 110 $terms = array($terms); 98 99 $defined_terms = get_defined_terms($terms);100 111 101 foreach ( $terms as $term ) { 102 if ( !is_int($term) ) { 103 if ( !isset($defined_terms[$term]) ) 104 $new_terms[] = $term; 105 $slugs[] = $term; 106 } else { 107 $term_ids[] = $term; 112 if ( ! $append ) { 113 $in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; 114 $old_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 tt.taxonomy IN ($in_taxonomies) AND tr.object_id = '$object_id'"); 115 } 116 117 $tt_ids = array(); 118 119 foreach ( $taxonomies as $taxonomy ) { 120 foreach ($terms as $term) { 121 if ( !$id = is_term($term, $taxonomy) ) 122 $id = add_term($term, $taxonomy); 123 $id = $id['term_taxonomy_id']; 124 $tt_ids[] = $id; 125 if ( $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '$object_id' AND term_taxonomy_id = '$id'") ) 126 continue; 127 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES ('$object_id', '$id')"); 108 128 } 109 129 } 110 130 111 $term_clause = isset($term_ids) ? 'tt.term_id IN (' . implode(', ', $term_ids) . ')' : ''; 112 if ( isset($slugs) ) { 113 if ($term_clause) { 114 $term_clause .= ' OR '; 115 } 116 $term_clause .= "t.slug IN ('" . implode("', '", $slugs) . "')"; 117 $term_join = "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id"; 118 } else { 119 $term_join = ''; 120 } 121 122 // Now add or increment the term taxonomy relationships. This is inefficient at the moment. 123 foreach ( $taxonomies as $taxonomy ) { 124 foreach ( $terms as $term ) { 125 add_term($term, $taxonomy); 131 if ( ! $append ) { 132 $delete_terms = array_diff($old_terms, $tt_ids); 133 if ( $delete_terms ) { 134 $delete_terms = "'" . implode("', '", $delete_terms) . "'"; 135 $wpdb->query("DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ($delete_terms)"); 126 136 } 127 137 } 128 129 $taxonomies = "'" . implode("', '", $taxonomies) . "'"; 130 131 // Finally, relate the term and taxonomy to the object. 132 // Use IGNORE to avoid dupe warnings for now. 133 $wpdb->query("INSERT IGNORE 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)"); 138 139 return $tt_ids; 134 140 } 135 141 136 142 /** 137 143 * Returns the terms associated with the given object(s), in the supplied taxonomies. … … 145 151 $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; 146 152 147 $taxonomies = "'" . implode("', '", $taxonomies) . "'"; 153 $taxonomies = "'" . implode("', '", $taxonomies) . "'"; 148 154 $object_ids = implode(', ', $object_ids); 149 155
Note: See TracChangeset
for help on using the changeset viewer.