WordPress.org

Make WordPress Core

Changeset 5522


Ignore:
Timestamp:
05/23/2007 05:42:31 AM (11 years ago)
Author:
ryan
Message:

wp_insert_term() and wp_update_term(). see #4189

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-db.php

    r5510 r5522  
    290290        return $id;
    291291
    292     $tag_id = add_term($tag_name, 'post_tag'); 
     292    $tag_id = wp_insert_term($tag_name, 'post_tag');   
    293293}
    294294
  • trunk/wp-includes/taxonomy.php

    r5521 r5522  
    11<?php
    22
    3 // TODO: add_term(), edit_term(), and remove_term() work with terms within context of
    4 // taxonomies.  insert_term(), update_term(), delete_term() work with just the terms table.
    5 // insert_term_taxonomy(), update_term_taxonomy(), and delete_term_taxonomy() work
    6 // with just the term taxonomy table.  Right now we only have add_term().
    7 
    83/**
    94 * Adds a new term to the database.  Optionally marks it as an alias of an existing term.
    10  * @param string $term The term to add.
     5 * @param int|string $term The term to add or update.
    116 * @param string $taxonomy The taxonomy to which to add the term
    127 * @param int|string $alias_of The id or slug of the new term's alias.
    138 */
    14 function add_term( $term, $taxonomy, $args = array() ) {
    15     global $wpdb;
    16 
    17     $slug = sanitize_title($term);
    18     $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0);
     9function wp_insert_term( $term, $taxonomy, $args = array() ) {
     10    global $wpdb;
     11
     12    $update = false;
     13    if ( is_int($term) ) {
     14        $update = true;
     15        $term_id = $term;
     16    } else {
     17        $name = $term;
     18    }
     19
     20    $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
    1921    $args = wp_parse_args($args, $defaults);
    2022    extract($args);
     23
     24    $parent = (int) $parent;
     25
     26    if ( empty($slug) )
     27        $slug = sanitize_title($name);
     28    else
     29        $slug = sanitize_title($slug);
    2130
    2231    $term_group = 0;   
     
    3342    }
    3443
    35     if ( ! $term_id = is_term($slug) ) {
    36         $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$term', '$slug', '$term_group')");
     44    if ( $update ) {
     45        $wpdb->query("UPDATE $wpdb->terms SET name = '$name', slug = '$slug', term_group = '$term_group' WHERE term_id = '$term_id'");
     46    } else if ( ! $term_id = is_term($slug) ) {
     47        $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')");
    3748        $term_id = (int) $wpdb->insert_id;
    3849    }
    39    
     50
     51    if ( empty($slug) ) {
     52        $slug = sanitize_title($slug, $term_id);
     53        $wpdb->query("UPDATE $wpdb->terms SET slug = '$slug' WHERE term_id = '$term_id'");
     54    }
     55       
    4056    $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");
    4157
    42     if ( !empty($tt_id) )
    43         return $term_id;
    44            
    45     $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')");
    46     $tt_id = (int) $wpdb->insert_id;
     58    if ( !$update && !empty($tt_id) )
     59        return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
     60
     61    if ( $update ) {
     62        $wpdb->query("UPDATE $wpdb->term_taxonomy SET term_id = '$term_id', taxonomy = '$taxonomy', description = '$description', parent = '$parent', count = 0 WHERE term_taxonomy_id = '$tt_id'");
     63    } else {
     64        $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')");
     65        $tt_id = (int) $wpdb->insert_id;
     66    }
     67
    4768    return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
    4869}
     
    5172 * Removes a term from the database.
    5273 */
    53 function remove_term() {}
    54    
    55    
     74function wp_delete_term() {}
     75
     76function wp_update_term( $term, $taxonomy, $fields = array() ) {
     77    $term = (int) $term;
     78
     79    // First, get all of the original fields
     80    $term = get_term ($term, $taxonomy, ARRAY_A);
     81
     82    // Escape data pulled from DB.
     83    $term = add_magic_quotes($term);
     84
     85    // Merge old and new fields with new fields overwriting old ones.
     86    $fields = array_merge($term, $fields);
     87
     88    return wp_insert_term($term, $taxonomy, $fields);
     89}
     90
    5691/**
    5792 * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
     
    120155        foreach ($terms as $term) {
    121156            if ( !$id = is_term($term, $taxonomy) )
    122                 $id = add_term($term, $taxonomy);
     157                $id = wp_insert_term($term, $taxonomy);
    123158            $id = $id['term_taxonomy_id'];
    124159            $tt_ids[] = $id;
Note: See TracChangeset for help on using the changeset viewer.