Make WordPress Core


Ignore:
Timestamp:
05/23/2007 05:28:13 PM (19 years ago)
Author:
ryan
Message:

wp_insert_category(), cat_rows(), and others using taxonomy. see #4189

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r5525 r5528  
    4040                return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    4141
    42         $update = false;
    43         if ( is_int($term) ) {
    44                 $update = true;
    45                 $term_id = $term;
    46         } else {
    47                 $name = $term;
    48         }
    49 
    5042        $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
    5143        $args = wp_parse_args($args, $defaults);
    5244        extract($args);
    5345
     46        $name = $term;
    5447        $parent = (int) $parent;
    5548
     
    7265        }
    7366
    74         if ( $update ) {
    75                 $wpdb->query("UPDATE $wpdb->terms SET name = '$name', slug = '$slug', term_group = '$term_group' WHERE term_id = '$term_id'");
    76         } else if ( ! $term_id = is_term($slug) ) {
     67        if ( ! $term_id = is_term($slug) ) {
    7768                $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')");
    7869                $term_id = (int) $wpdb->insert_id;
     
    8677        $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");
    8778
    88         if ( !$update && !empty($tt_id) )
     79        if ( !empty($tt_id) )
    8980                return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
    9081
    91         if ( $update ) {
    92                 $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'");
    93         } else {
    94                 $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')");
    95                 $tt_id = (int) $wpdb->insert_id;
    96         }
    97 
    98         if ($update) {
    99                 do_action("edit_term", $term_id, $tt_id);
    100                 do_action("edit_$taxonomy", $term_id, $tt_id);
    101         } else {
    102                 do_action("create_term", $term_id, $tt_id);
    103                 do_action("create_$taxonomy", $term_id, $tt_id);
    104         }
    105 
    106         $term_id = apply_filters('term_id_filter', $term_id, $tt_id, $update);
     82        $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')");
     83        $tt_id = (int) $wpdb->insert_id;
     84
     85        do_action("create_term", $term_id, $tt_id);
     86        do_action("create_$taxonomy", $term_id, $tt_id);
     87
     88        $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
    10789
    10890        //clean_term_cache($term_id);
    10991
    110         if ($update) {
    111                 do_action("edited_term", $term_id, $tt_id);
    112                 do_action("edited_$taxonomy", $term_id, $tt_id);
    113         } else {
    114                 do_action("created_term", $term_id, $tt_id);
    115                 do_action("created_$taxonomy", $term_id, $tt_id);
    116         }
     92        do_action("created_term", $term_id, $tt_id);
     93        do_action("created_$taxonomy", $term_id, $tt_id);
    11794
    11895        return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
     
    124101function wp_delete_term() {}
    125102
    126 function wp_update_term( $term, $taxonomy, $fields = array() ) {
    127         $term = (int) $term;
    128 
    129         // First, get all of the original fields
    130         $term = get_term ($term, $taxonomy, ARRAY_A);
     103function wp_update_term( $term, $taxonomy, $args = array() ) {
     104        global $wpdb;
     105
     106        if ( ! is_taxonomy($taxonomy) )
     107                return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
     108
     109        $term_id = (int) $term;
     110
     111        // First, get all of the original args
     112        $term = get_term ($term_id, $taxonomy, ARRAY_A);
    131113
    132114        // Escape data pulled from DB.
    133115        $term = add_magic_quotes($term);
    134116
    135         // Merge old and new fields with new fields overwriting old ones.
    136         $fields = array_merge($term, $fields);
    137 
    138         return wp_insert_term($term, $taxonomy, $fields);
     117        // Merge old and new args with new args overwriting old ones.
     118        $args = array_merge($term, $args);
     119
     120        $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
     121        $args = wp_parse_args($args, $defaults);
     122        extract($args);
     123
     124        $parent = (int) $parent;
     125
     126        if ( empty($slug) )
     127                $slug = sanitize_title($name);
     128        else
     129                $slug = sanitize_title($slug);
     130
     131        $term_group = 0;       
     132        if ( $alias_of ) {
     133                $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$alias_of'");
     134                if ( $alias->term_group ) {
     135                        // The alias we want is already in a group, so let's use that one.
     136                        $term_group = $alias->term_group;
     137                } else {
     138                        // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
     139                        $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1;
     140                        $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id");
     141                }
     142        }
     143
     144        $wpdb->query("UPDATE $wpdb->terms SET name = '$name', slug = '$slug', term_group = '$term_group' WHERE term_id = '$term_id'");
     145
     146        if ( empty($slug) ) {
     147                $slug = sanitize_title($slug, $term_id);
     148                $wpdb->query("UPDATE $wpdb->terms SET slug = '$slug' WHERE term_id = '$term_id'");
     149        }
     150               
     151        $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");
     152
     153        $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'");
     154
     155        do_action("edit_term", $term_id, $tt_id);
     156        do_action("edit_$taxonomy", $term_id, $tt_id);
     157
     158        $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
     159
     160        //clean_term_cache($term_id);
     161
     162        do_action("edited_term", $term_id, $tt_id);
     163        do_action("edited_$taxonomy", $term_id, $tt_id);
     164
     165        return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
    139166}
    140167
Note: See TracChangeset for help on using the changeset viewer.