WordPress.org

Make WordPress Core

Changeset 5528


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

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

Location:
trunk
Files:
5 edited

Legend:

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

    r5523 r5528  
    8585    extract($catarr);
    8686
    87     if( trim( $cat_name ) == '' )
     87    if ( trim( $cat_name ) == '' )
    8888        return 0;
    8989
     
    9191
    9292    // Are we updating or creating?
    93     if (!empty ($cat_ID))
     93    if ( !empty ($cat_ID) )
    9494        $update = true;
    9595    else
    9696        $update = false;
    9797
    98     $cat_name = apply_filters('pre_category_name', $cat_name);
    99 
    100     if (empty ($category_nicename))
    101         $category_nicename = sanitize_title($cat_name);
    102     else
    103         $category_nicename = sanitize_title($category_nicename);
    104     $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
    105 
    106     if (empty ($category_description))
    107         $category_description = '';
    108     $category_description = apply_filters('pre_category_description', $category_description);
    109 
    110     $category_parent = (int) $category_parent;
    111     if ( empty($category_parent) || !get_category( $category_parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $category_parent) ) )
    112         $category_parent = 0;
    113 
    114     if ( isset($posts_private) )
    115         $posts_private = (int) $posts_private;
    116     else
    117         $posts_private = 0;
    118 
    119     if ( isset($links_private) )
    120         $links_private = (int) $links_private;
    121     else
    122         $links_private = 0;
    123 
    124     if ( empty($type) )
    125         $type = TAXONOMY_CATEGORY;
    126 
    127     // Let's check if we have this category already, if so just do an update
    128     if ( !$update && $cat_ID = category_object_exists( $category_nicename ) )
    129         $update = true;
    130 
    131     if (!$update) {
    132         $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private, type) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private', '$type')");
    133         $cat_ID = (int) $wpdb->insert_id;
    134     } else {
    135         $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_private = '$links_private', posts_private = '$posts_private', type = '$type' WHERE cat_ID = '$cat_ID'");
    136     }
    137 
    138     if ( $category_nicename == '' ) {
    139         $category_nicename = sanitize_title($cat_name, $cat_ID );
    140         $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
    141     }
    142 
    143     // Keep in mind when using this filter and altering the cat_ID that the two queries above
    144     // have already taken place with the OLD cat_ID
    145     // Also note that you may have post2cat entries with the old cat_ID if this is an update
    146 
    147     if ($update) {
    148         do_action('edit_category', $cat_ID);
    149     } else {
    150         do_action('create_category', $cat_ID);
    151         do_action('add_category', $cat_ID);
    152     }
    153 
    154     $cat_ID = apply_filters('cat_id_filter', $cat_ID, $update);
    155 
    156     clean_category_cache($cat_ID);
    157 
    158     if ($update)
    159         do_action('edited_category', $cat_ID);
    160     else
    161         do_action('created_category', $cat_ID);
    162    
    163     return $cat_ID;
     98    $name = $cat_name;
     99    $description = $category_description;
     100    $slug = $category_nicename;
     101    $parent = $category_parent;
     102
     103    $name = apply_filters('pre_category_name', $name);
     104
     105    if ( empty ($slug) )
     106        $slug = sanitize_title($slug);
     107    else
     108        $slug = sanitize_title($slug);
     109    $slug = apply_filters('pre_category_nicename', $slug);
     110
     111    if ( empty ($description) )
     112        $description = '';
     113    $description = apply_filters('pre_category_description', $description);
     114
     115    $parent = (int) $parent;
     116    if ( empty($parent) || !get_category( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
     117        $parent = 0;
     118
     119    $args = compact('slug', 'parent', 'description');
     120
     121    if ( $update )
     122        $cat_ID = wp_update_term($cat_ID, 'category', $args);
     123    else
     124        $cat_ID = wp_insert_term($cat_name, 'category', $args);
     125
     126    return $cat_ID['term_id'];
    164127}
    165128
  • trunk/wp-admin/admin-functions.php

    r5527 r5528  
    770770// Dandy new recursive multiple category stuff.
    771771function cat_rows( $parent = 0, $level = 0, $categories = 0 ) {
    772     if (!$categories )
     772    if ( !$categories )
    773773        $categories = get_categories( 'hide_empty=0' );
    774774
     
    778778        ob_start();
    779779        foreach ( $categories as $category ) {
    780             if ( $category->category_parent == $parent) {
     780            if ( $category->parent == $parent) {
    781781                echo "\t" . _cat_row( $category, $level );
    782                 if ( isset($children[$category->cat_ID]) )
    783                     cat_rows( $category->cat_ID, $level +1, $categories );
     782                if ( isset($children[$category->term_id]) )
     783                    cat_rows( $category->term_id, $level +1, $categories );
    784784            }
    785785        }
     
    800800    $pad = str_repeat( '— ', $level );
    801801    if ( current_user_can( 'manage_categories' ) ) {
    802         $edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>".__( 'Edit' )."</a></td>";
     802        $edit = "<a href='categories.php?action=edit&amp;cat_ID=$category->term_id' class='edit'>".__( 'Edit' )."</a></td>";
    803803        $default_cat_id = (int) get_option( 'default_category' );
    804         $default_link_cat_id = (int) get_option( 'default_link_category' );
    805 
    806         if ( ($category->cat_ID != $default_cat_id ) && ($category->cat_ID != $default_link_cat_id ) )
    807             $edit .= "<td><a href='" . wp_nonce_url( "categories.php?action=delete&amp;cat_ID=$category->cat_ID", 'delete-category_' . $category->cat_ID ) . "' onclick=\"return deleteSomething( 'cat', $category->cat_ID, '" . js_escape(sprintf( __("You are about to delete the category '%s'.\nAll posts that were only assigned to this category will be assigned to the '%s' category.\nAll links that were only assigned to this category will be assigned to the '%s' category.\n'OK' to delete, 'Cancel' to stop." ), $category->cat_name, get_catname( $default_cat_id ), get_catname( $default_link_cat_id ) )) . "' );\" class='delete'>".__( 'Delete' )."</a>";
     804
     805        if ( $category->term_id != $default_cat_id )
     806            $edit .= "<td><a href='" . wp_nonce_url( "categories.php?action=delete&amp;cat_ID=$category->term_id", 'delete-category_' . $category->term_id ) . "' onclick=\"return deleteSomething( 'cat', $category->term_id, '" . js_escape(sprintf( __("You are about to delete the category '%s'.\nAll posts that were only assigned to this category will be assigned to the '%s' category.\nAll links that were only assigned to this category will be assigned to the '%s' category.\n'OK' to delete, 'Cancel' to stop." ), $category->name, get_catname( $default_cat_id ), get_catname( $default_link_cat_id ) )) . "' );\" class='delete'>".__( 'Delete' )."</a>";
    808807        else
    809808            $edit .= "<td style='text-align:center'>".__( "Default" );
     
    813812    $class = ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || " class='alternate'" == $class ) ? '' : " class='alternate'";
    814813
    815     $category->category_count = number_format_i18n( $category->category_count );
    816     $category->link_count = number_format_i18n( $category->link_count );
    817     $posts_count = ( $category->category_count > 0 ) ? "<a href='edit.php?cat=$category->cat_ID'>$category->category_count</a>" : $category->category_count;
    818     return "<tr id='cat-$category->cat_ID'$class>
    819         <th scope='row' style='text-align: center'>$category->cat_ID</th>
    820         <td>" . ( $name_override ? $name_override : $pad . ' ' . $category->cat_name ) . "</td>
    821         <td>$category->category_description</td>
     814    $category->count = number_format_i18n( $category->count );
     815    $posts_count = ( $category->count > 0 ) ? "<a href='edit.php?cat=$category->term_id'>$category->count</a>" : $category->count;
     816    return "<tr id='cat-$category->term_id'$class>
     817        <th scope='row' style='text-align: center'>$category->term_id</th>
     818        <td>" . ( $name_override ? $name_override : $pad . ' ' . $category->name ) . "</td>
     819        <td>$category->description</td>
    822820        <td align='center'>$posts_count</td>
    823         <td align='center'>$category->link_count</td>
    824821        <td>$edit</td>\n\t</tr>\n";
    825822}
  • trunk/wp-admin/categories.php

    r5217 r5528  
    100100        <th scope="col"><?php _e('Description') ?></th>
    101101        <th scope="col" width="90" style="text-align: center"><?php _e('Posts') ?></th>
    102         <th scope="col" width="90" style="text-align: center"><?php _e('Links') ?></th>
    103102        <th colspan="2" style="text-align: center"><?php _e('Action') ?></th>
    104103    </tr>
  • trunk/wp-includes/category.php

    r5525 r5528  
    8585    $cat_id = (int) $cat_id;
    8686    $category = &get_category($cat_id);
    87     return $category->cat_name;
     87    return $category->name;
    8888}
    8989
     
    175175
    176176function _get_category_hierarchy() {
    177     $children = get_option('category_children');
    178     if ( is_array($children) )
    179         return $children;
    180 
    181     $children = array();
    182     $categories = get_categories('hide_empty=0&hierarchical=0');
    183     foreach ( $categories as $cat ) {
    184         if ( $cat->category_parent > 0 )
    185             $children[$cat->category_parent][] = $cat->cat_ID;
    186     }
    187     update_option('category_children', $children);
    188 
    189     return $children;
     177    return _get_term_hierarchy('category');
    190178}
    191179
  • 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.