diff --git a/wp-admin/admin-db.php b/wp-admin/admin-db.php index 07b7881ba..8cd708b9b 100644 --- a/wp-admin/admin-db.php +++ b/wp-admin/admin-db.php @@ -84,83 +84,46 @@ function wp_insert_category($catarr) { extract($catarr); - if( trim( $cat_name ) == '' ) + if ( trim( $cat_name ) == '' ) return 0; $cat_ID = (int) $cat_ID; // Are we updating or creating? - if (!empty ($cat_ID)) + if ( !empty ($cat_ID) ) $update = true; else $update = false; - $cat_name = apply_filters('pre_category_name', $cat_name); + $name = $cat_name; + $description = $category_description; + $slug = $category_nicename; + $parent = $category_parent; - if (empty ($category_nicename)) - $category_nicename = sanitize_title($cat_name); + $name = apply_filters('pre_category_name', $name); + + if ( empty ($slug) ) + $slug = sanitize_title($slug); else - $category_nicename = sanitize_title($category_nicename); - $category_nicename = apply_filters('pre_category_nicename', $category_nicename); + $slug = sanitize_title($slug); + $slug = apply_filters('pre_category_nicename', $slug); - if (empty ($category_description)) - $category_description = ''; - $category_description = apply_filters('pre_category_description', $category_description); + if ( empty ($description) ) + $description = ''; + $description = apply_filters('pre_category_description', $description); - $category_parent = (int) $category_parent; - if ( empty($category_parent) || !get_category( $category_parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $category_parent) ) ) - $category_parent = 0; + $parent = (int) $parent; + if ( empty($parent) || !get_category( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) ) + $parent = 0; - if ( isset($posts_private) ) - $posts_private = (int) $posts_private; + $args = compact('slug', 'parent', 'description'); + + if ( $update ) + $cat_ID = wp_update_term($cat_ID, 'category', $args); else - $posts_private = 0; + $cat_ID = wp_insert_term($cat_name, 'category', $args); - if ( isset($links_private) ) - $links_private = (int) $links_private; - else - $links_private = 0; - - if ( empty($type) ) - $type = TAXONOMY_CATEGORY; - - // Let's check if we have this category already, if so just do an update - if ( !$update && $cat_ID = category_object_exists( $category_nicename ) ) - $update = true; - - if (!$update) { - $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')"); - $cat_ID = (int) $wpdb->insert_id; - } else { - $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'"); - } - - if ( $category_nicename == '' ) { - $category_nicename = sanitize_title($cat_name, $cat_ID ); - $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" ); - } - - // Keep in mind when using this filter and altering the cat_ID that the two queries above - // have already taken place with the OLD cat_ID - // Also note that you may have post2cat entries with the old cat_ID if this is an update - - if ($update) { - do_action('edit_category', $cat_ID); - } else { - do_action('create_category', $cat_ID); - do_action('add_category', $cat_ID); - } - - $cat_ID = apply_filters('cat_id_filter', $cat_ID, $update); - - clean_category_cache($cat_ID); - - if ($update) - do_action('edited_category', $cat_ID); - else - do_action('created_category', $cat_ID); - - return $cat_ID; + return $cat_ID['term_id']; } function wp_update_category($catarr) { diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index ac5788898..53130e4dd 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -769,7 +769,7 @@ function dropdown_link_categories( $default = 0 ) { // Dandy new recursive multiple category stuff. function cat_rows( $parent = 0, $level = 0, $categories = 0 ) { - if (!$categories ) + if ( !$categories ) $categories = get_categories( 'hide_empty=0' ); $children = _get_category_hierarchy(); @@ -777,10 +777,10 @@ function cat_rows( $parent = 0, $level = 0, $categories = 0 ) { if ( $categories ) { ob_start(); foreach ( $categories as $category ) { - if ( $category->category_parent == $parent) { + if ( $category->parent == $parent) { echo "\t" . _cat_row( $category, $level ); - if ( isset($children[$category->cat_ID]) ) - cat_rows( $category->cat_ID, $level +1, $categories ); + if ( isset($children[$category->term_id]) ) + cat_rows( $category->term_id, $level +1, $categories ); } } $output = ob_get_contents(); @@ -799,12 +799,11 @@ function _cat_row( $category, $level, $name_override = false ) { $pad = str_repeat( '— ', $level ); if ( current_user_can( 'manage_categories' ) ) { - $edit = "".__( 'Edit' ).""; + $edit = "".__( 'Edit' ).""; $default_cat_id = (int) get_option( 'default_category' ); - $default_link_cat_id = (int) get_option( 'default_link_category' ); - if ( ($category->cat_ID != $default_cat_id ) && ($category->cat_ID != $default_link_cat_id ) ) - $edit .= "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' ).""; + if ( $category->term_id != $default_cat_id ) + $edit .= "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' ).""; else $edit .= "".__( "Default" ); } else @@ -812,15 +811,13 @@ function _cat_row( $category, $level, $name_override = false ) { $class = ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || " class='alternate'" == $class ) ? '' : " class='alternate'"; - $category->category_count = number_format_i18n( $category->category_count ); - $category->link_count = number_format_i18n( $category->link_count ); - $posts_count = ( $category->category_count > 0 ) ? "$category->category_count" : $category->category_count; - return " - $category->cat_ID - " . ( $name_override ? $name_override : $pad . ' ' . $category->cat_name ) . " - $category->category_description + $category->count = number_format_i18n( $category->count ); + $posts_count = ( $category->count > 0 ) ? "$category->count" : $category->count; + return " + $category->term_id + " . ( $name_override ? $name_override : $pad . ' ' . $category->name ) . " + $category->description $posts_count - $category->link_count $edit\n\t\n"; } diff --git a/wp-admin/categories.php b/wp-admin/categories.php index 18249cb7d..41caea225 100644 --- a/wp-admin/categories.php +++ b/wp-admin/categories.php @@ -99,7 +99,6 @@ $messages[5] = __('Category not updated.'); - diff --git a/wp-includes/category.php b/wp-includes/category.php index 9faa09ae3..75245ad32 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -84,7 +84,7 @@ function get_catname($cat_ID) { function get_cat_name($cat_id) { $cat_id = (int) $cat_id; $category = &get_category($cat_id); - return $category->cat_name; + return $category->name; } function cat_is_ancestor_of($cat1, $cat2) { @@ -174,19 +174,7 @@ function _pad_category_counts($type, &$categories) { } function _get_category_hierarchy() { - $children = get_option('category_children'); - if ( is_array($children) ) - return $children; - - $children = array(); - $categories = get_categories('hide_empty=0&hierarchical=0'); - foreach ( $categories as $cat ) { - if ( $cat->category_parent > 0 ) - $children[$cat->category_parent][] = $cat->cat_ID; - } - update_option('category_children', $children); - - return $children; + return _get_term_hierarchy('category'); } // Tags diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index a053e09be..f8f1e2d1b 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -39,14 +39,84 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { if ( ! is_taxonomy($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); - $update = false; - if ( is_int($term) ) { - $update = true; - $term_id = $term; - } else { - $name = $term; + $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); + $args = wp_parse_args($args, $defaults); + extract($args); + + $name = $term; + $parent = (int) $parent; + + if ( empty($slug) ) + $slug = sanitize_title($name); + else + $slug = sanitize_title($slug); + + $term_group = 0; + if ( $alias_of ) { + $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE slug = '$alias_of'"); + if ( $alias->term_group ) { + // The alias we want is already in a group, so let's use that one. + $term_group = $alias->term_group; + } else { + // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. + $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1; + $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id"); + } } + if ( ! $term_id = is_term($slug) ) { + $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')"); + $term_id = (int) $wpdb->insert_id; + } + + if ( empty($slug) ) { + $slug = sanitize_title($slug, $term_id); + $wpdb->query("UPDATE $wpdb->terms SET slug = '$slug' WHERE term_id = '$term_id'"); + } + + $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"); + + if ( !empty($tt_id) ) + return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); + + $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')"); + $tt_id = (int) $wpdb->insert_id; + + do_action("create_term", $term_id, $tt_id); + do_action("create_$taxonomy", $term_id, $tt_id); + + $term_id = apply_filters('term_id_filter', $term_id, $tt_id); + + //clean_term_cache($term_id); + + do_action("created_term", $term_id, $tt_id); + do_action("created_$taxonomy", $term_id, $tt_id); + + return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); +} + +/** + * Removes a term from the database. + */ +function wp_delete_term() {} + +function wp_update_term( $term, $taxonomy, $args = array() ) { + global $wpdb; + + if ( ! is_taxonomy($taxonomy) ) + return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); + + $term_id = (int) $term; + + // First, get all of the original args + $term = get_term ($term_id, $taxonomy, ARRAY_A); + + // Escape data pulled from DB. + $term = add_magic_quotes($term); + + // Merge old and new args with new args overwriting old ones. + $args = array_merge($term, $args); + $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); extract($args); @@ -71,12 +141,7 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { } } - if ( $update ) { - $wpdb->query("UPDATE $wpdb->terms SET name = '$name', slug = '$slug', term_group = '$term_group' WHERE term_id = '$term_id'"); - } else if ( ! $term_id = is_term($slug) ) { - $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')"); - $term_id = (int) $wpdb->insert_id; - } + $wpdb->query("UPDATE $wpdb->terms SET name = '$name', slug = '$slug', term_group = '$term_group' WHERE term_id = '$term_id'"); if ( empty($slug) ) { $slug = sanitize_title($slug, $term_id); @@ -85,59 +150,21 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { $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"); - if ( !$update && !empty($tt_id) ) - return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); + $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'"); - if ( $update ) { - $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'"); - } else { - $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')"); - $tt_id = (int) $wpdb->insert_id; - } + do_action("edit_term", $term_id, $tt_id); + do_action("edit_$taxonomy", $term_id, $tt_id); - if ($update) { - do_action("edit_term", $term_id, $tt_id); - do_action("edit_$taxonomy", $term_id, $tt_id); - } else { - do_action("create_term", $term_id, $tt_id); - do_action("create_$taxonomy", $term_id, $tt_id); - } - - $term_id = apply_filters('term_id_filter', $term_id, $tt_id, $update); + $term_id = apply_filters('term_id_filter', $term_id, $tt_id); //clean_term_cache($term_id); - if ($update) { - do_action("edited_term", $term_id, $tt_id); - do_action("edited_$taxonomy", $term_id, $tt_id); - } else { - do_action("created_term", $term_id, $tt_id); - do_action("created_$taxonomy", $term_id, $tt_id); - } + do_action("edited_term", $term_id, $tt_id); + do_action("edited_$taxonomy", $term_id, $tt_id); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); } -/** - * Removes a term from the database. - */ -function wp_delete_term() {} - -function wp_update_term( $term, $taxonomy, $fields = array() ) { - $term = (int) $term; - - // First, get all of the original fields - $term = get_term ($term, $taxonomy, ARRAY_A); - - // Escape data pulled from DB. - $term = add_magic_quotes($term); - - // Merge old and new fields with new fields overwriting old ones. - $fields = array_merge($term, $fields); - - return wp_insert_term($term, $taxonomy, $fields); -} - /** * Returns the index of a defined term, or 0 (false) if the term doesn't exist. */