Make WordPress Core

Changeset 5515


Ignore:
Timestamp:
05/22/2007 08:39:26 PM (19 years ago)
Author:
ryan
Message:

Fix primary key on term_relationships. Add wp_set_object_terms(). Setting post tags working now. see #4189

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/upgrade-schema.php

    r5510 r5515  
    3232 object_id bigint(20) NOT NULL default 0,
    3333 term_taxonomy_id bigint(20) NOT NULL default 0,
    34  PRIMARY KEY  (object_id),
     34 PRIMARY KEY  (object_id, term_taxonomy_id),
    3535 KEY (term_taxonomy_id)
    3636) $charset_collate;
  • trunk/wp-includes/post.php

    r5510 r5515  
    795795                $tags = array();
    796796        $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);
    798798}
    799799
  • trunk/wp-includes/taxonomy.php

    r5510 r5515  
    4444                       
    4545        $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);
    4848}
    4949
    … …  
    5959function is_term($term, $taxonomy = '') {
    6060        global $wpdb;
    61         if ( ! $term = sanitize_title($term) )
    62                 return 0;
    6361
    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);
    6576}
    6677       
    … …  
    8495 * Relates an object (post, link etc) to a term and taxonomy type.  Creates the term and taxonomy
    8596 * 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.
    8698 * @param array|int|string $term The slug or id of the term.
    87  * @param int $object_id The object to relate to.
    8899 * @param array|string $taxonomies The context(s) in which to relate the term to the object.
    89100 */
    90 function add_term_relationship($terms, $object_id, $taxonomies) {
     101function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) {
    91102        global $wpdb;
    92                
     103
     104        $object_id = (int) $object_id;
     105
    93106        if ( !is_array($taxonomies) )
    94107                $taxonomies = array($taxonomies);
    … …  
    96109        if ( !is_array($terms) )
    97110                $terms = array($terms);
    98                
    99         $defined_terms = get_defined_terms($terms);
    100111
    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')");
    108128                }
    109129        }
    110130
    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)");
    126136                }
    127137        }
    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;
    134140}
    135        
     141
    136142/**
    137143 * Returns the terms associated with the given object(s), in the supplied taxonomies.
    … …  
    145151        $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
    146152
    147         $taxonomies = "'" . implode("', '", $taxonomies) . "'";         
     153        $taxonomies = "'" . implode("', '", $taxonomies) . "'";
    148154        $object_ids = implode(', ', $object_ids);               
    149155
Note: See TracChangeset for help on using the changeset viewer.