From 2b134cc1f71d76dd029079cb0cf2fdc68aa644f6 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Wed, 23 May 2007 18:07:53 +0000 Subject: [PATCH] Set and get post cats to taxonomy. see #4189 git-svn-id: https://develop.svn.wordpress.org/trunk@5529 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/admin-functions.php | 39 +++++----------- wp-admin/edit-category-form.php | 10 ++--- wp-includes/category-template.php | 59 ++++++++++++------------ wp-includes/category.php | 75 ------------------------------- wp-includes/post.php | 55 ++--------------------- wp-includes/taxonomy.php | 3 ++ 6 files changed, 52 insertions(+), 189 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 53130e4dd8..8bf152b16e 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -678,25 +678,10 @@ function get_tags_to_edit( $post_id ) { } function get_nested_categories( $default = 0, $parent = 0 ) { - global $post_ID, $link_id, $mode, $wpdb; + global $post_ID, $mode, $wpdb; if ( $post_ID ) { - $checked_categories = $wpdb->get_col( " - SELECT category_id - FROM $wpdb->categories, $wpdb->post2cat - WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_ID' AND rel_type = 'category' - " ); - - if ( count( $checked_categories ) == 0 ) { - // No selected categories, strange - $checked_categories[] = $default; - } - } else if ( $link_id ) { - $checked_categories = $wpdb->get_col( " - SELECT category_id - FROM $wpdb->categories, $wpdb->link2cat - WHERE $wpdb->link2cat.category_id = cat_ID AND $wpdb->link2cat.link_id = '$link_id' - " ); + $checked_categories = wp_get_post_categories($post_ID); if ( count( $checked_categories ) == 0 ) { // No selected categories, strange @@ -706,12 +691,12 @@ function get_nested_categories( $default = 0, $parent = 0 ) { $checked_categories[] = $default; } - $cats = return_categories_list( $parent); + $cats = get_categories("child_of=$parent&hide_empty=0&get=ids"); $result = array (); if ( is_array( $cats ) ) { foreach ( $cats as $cat) { - $result[$cat]['children'] = get_nested_categories( $default, $cat); + //$result[$cat]['children'] = get_nested_categories( $default, $cat); $result[$cat]['cat_ID'] = $cat; $result[$cat]['checked'] = in_array( $cat, $checked_categories ); $result[$cat]['cat_name'] = get_the_category_by_ID( $cat); @@ -754,7 +739,7 @@ function dropdown_link_categories( $default = 0 ) { $checked_categories[] = $default; } - $categories = get_terms('link_category', 'orderby=count'); + $categories = get_terms('link_category', 'orderby=count&hide_empty=0'); if ( empty($categories) ) return; @@ -772,7 +757,7 @@ function cat_rows( $parent = 0, $level = 0, $categories = 0 ) { if ( !$categories ) $categories = get_categories( 'hide_empty=0' ); - $children = _get_category_hierarchy(); + $children = _get_term_hierarchy('category'); if ( $categories ) { ob_start(); @@ -959,14 +944,14 @@ function wp_dropdown_cats( $currentcat = 0, $currentparent = 0, $parent = 0, $le if ( $categories ) { foreach ( $categories as $category ) { - if ( $currentcat != $category->cat_ID && $parent == $category->category_parent) { + if ( $currentcat != $category->term_id && $parent == $category->parent) { $pad = str_repeat( '– ', $level ); - $category->cat_name = wp_specialchars( $category->cat_name ); - echo "\n\t"; - wp_dropdown_cats( $currentcat, $currentparent, $category->cat_ID, $level +1, $categories ); + echo ">$pad$category->name"; + wp_dropdown_cats( $currentcat, $currentparent, $category->term_id, $level +1, $categories ); } } } else { diff --git a/wp-admin/edit-category-form.php b/wp-admin/edit-category-form.php index 9430ae876e..bf24c51d04 100644 --- a/wp-admin/edit-category-form.php +++ b/wp-admin/edit-category-form.php @@ -21,26 +21,26 @@ if ( ! empty($cat_ID) ) {
- + - + - + - +
- category_parent . '&hierarchical=1&show_option_none=' . __('None')); ?> + parent . '&hierarchical=1&show_option_none=' . __('None')); ?>

diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index ca4eb446ca..78a6e34f10 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -12,9 +12,9 @@ function get_category_children($id, $before = '/', $after = '') { continue; $category = get_category($cat_id); - if ( $category->category_parent == $id ) { - $chain .= $before.$category->cat_ID.$after; - $chain .= get_category_children($category->cat_ID, $before, $after); + if ( $category->parent == $id ) { + $chain .= $before.$category->term_id.$after; + $chain .= get_category_children($category->term_id, $before, $after); } } return $chain; @@ -29,9 +29,9 @@ function get_category_link($category_id) { $catlink = $file . '?cat=' . $category_id; } else { $category = &get_category($category_id); - $category_nicename = $category->category_nicename; + $category_nicename = $category->slug; - if ( $parent = $category->category_parent ) + if ( $parent = $category->parent ) $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename; $catlink = str_replace('%category%', $category_nicename, $catlink); @@ -45,31 +45,28 @@ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = $parent = &get_category($id); if ( $nicename ) - $name = $parent->category_nicename; + $name = $parent->slug; else $name = $parent->cat_name; - if ( $parent->category_parent && ($parent->category_parent != $parent->cat_ID) ) - $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename); + if ( $parent->parent && ($parent->parent != $parent->term_id) ) + $chain .= get_category_parents($parent->parent, $link, $separator, $nicename); if ( $link ) - $chain .= 'cat_name) . '">'.$name.'' . $separator; + $chain .= 'cat_name) . '">'.$name.'' . $separator; else $chain .= $name.$separator; return $chain; } function get_the_category($id = false) { -global $post, $category_cache, $blog_id; + global $post, $category_cache, $blog_id; $id = (int) $id; if ( !$id ) $id = (int) $post->ID; - if ( !isset($category_cache[$blog_id][$id]) ) - update_post_category_cache($id); - - $categories = $category_cache[$blog_id][$id]; + $categories = get_object_terms($id, 'category'); if ( !empty($categories) ) usort($categories, '_get_the_category_usort'); @@ -86,7 +83,7 @@ function _get_the_category_usort($a, $b) { function get_the_category_by_ID($cat_ID) { $cat_ID = (int) $cat_ID; $category = &get_category($cat_ID); - return $category->cat_name; + return $category->name; } function get_the_category_list($separator = '', $parents='') { @@ -104,19 +101,19 @@ function get_the_category_list($separator = '', $parents='') { $thelist .= "\n\t
  • "; switch ( strtolower($parents) ) { case 'multiple': - if ($category->category_parent) - $thelist .= get_category_parents($category->category_parent, TRUE); - $thelist .= 'cat_name) . '" ' . $rel . '>' . $category->cat_name.'
  • '; + if ($category->parent) + $thelist .= get_category_parents($category->parent, TRUE); + $thelist .= 'name) . '" ' . $rel . '>' . $category->name.''; break; case 'single': - $thelist .= 'cat_name) . '" ' . $rel . '>'; - if ($category->category_parent) - $thelist .= get_category_parents($category->category_parent, FALSE); - $thelist .= $category->cat_name.''; + $thelist .= 'name) . '" ' . $rel . '>'; + if ($category->parent) + $thelist .= get_category_parents($category->parent, FALSE); + $thelist .= $category->name.''; break; case '': default: - $thelist .= 'cat_name) . '" ' . $rel . '>' . $category->cat_name.''; + $thelist .= 'name) . '" ' . $rel . '>' . $category->cat_name.''; } } $thelist .= ''; @@ -127,19 +124,19 @@ function get_the_category_list($separator = '', $parents='') { $thelist .= $separator . ' '; switch ( strtolower($parents) ) { case 'multiple': - if ( $category->category_parent ) - $thelist .= get_category_parents($category->category_parent, TRUE); - $thelist .= 'cat_name) . '" ' . $rel . '>' . $category->cat_name.''; + if ( $category->parent ) + $thelist .= get_category_parents($category->parent, TRUE); + $thelist .= 'name) . '" ' . $rel . '>' . $category->cat_name.''; break; case 'single': - $thelist .= 'cat_name) . '" ' . $rel . '>'; - if ( $category->category_parent ) - $thelist .= get_category_parents($category->category_parent, FALSE); + $thelist .= 'name) . '" ' . $rel . '>'; + if ( $category->parent ) + $thelist .= get_category_parents($category->parent, FALSE); $thelist .= "$category->cat_name"; break; case '': default: - $thelist .= 'cat_name) . '" ' . $rel . '>' . $category->cat_name.''; + $thelist .= 'name) . '" ' . $rel . '>' . $category->name.''; } ++$i; } @@ -165,7 +162,7 @@ function category_description($category = 0) { if ( !$category ) $category = $cat; $category = & get_category($category); - return apply_filters('category_description', $category->category_description, $category->cat_ID); + return apply_filters('category_description', $category->description, $category->term_id); } function wp_dropdown_categories($args = '') { diff --git a/wp-includes/category.php b/wp-includes/category.php index 75245ad32f..0b463742ba 100644 --- a/wp-includes/category.php +++ b/wp-includes/category.php @@ -102,81 +102,6 @@ function cat_is_ancestor_of($cat1, $cat2) { return cat_is_ancestor_of($cat1, get_category($cat2->parent)); } -// -// Private -// - -function &_get_cat_children($category_id, $categories) { - if ( empty($categories) ) - return array(); - - $category_list = array(); - $has_children = _get_category_hierarchy(); - - if ( ( 0 != $category_id ) && ! isset($has_children[$category_id]) ) - return array(); - - foreach ( $categories as $category ) { - if ( $category->cat_ID == $category_id ) - continue; - - if ( $category->category_parent == $category_id ) { - $category_list[] = $category; - - if ( !isset($has_children[$category->cat_ID]) ) - continue; - - if ( $children = _get_cat_children($category->cat_ID, $categories) ) - $category_list = array_merge($category_list, $children); - } - } - - return $category_list; -} - -// Recalculates link or post counts by including items from child categories -// Assumes all relevant children are already in the $categories argument -function _pad_category_counts($type, &$categories) { - global $wpdb; - - // Set up some useful arrays - foreach ( $categories as $key => $cat ) { - $cats[$cat->cat_ID] = & $categories[$key]; - $cat_IDs[] = $cat->cat_ID; - } - - // Get the relevant post2cat or link2cat records and stick them in a lookup table - if ( $type == 'post' ) { - $results = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat LEFT JOIN $wpdb->posts ON post_id = ID WHERE category_id IN (".join(',', $cat_IDs).") AND post_type = 'post' AND post_status = 'publish'"); - foreach ( $results as $row ) - ++$cat_items[$row->category_id][$row->post_id]; - } else { - $results = $wpdb->get_results("SELECT $wpdb->link2cat.link_id, category_id FROM $wpdb->link2cat LEFT JOIN $wpdb->links USING (link_id) WHERE category_id IN (".join(',', $cat_IDs).") AND link_visible = 'Y'"); - foreach ( $results as $row ) - ++$cat_items[$row->category_id][$row->link_id]; - } - - // Touch every ancestor's lookup row for each post in each category - foreach ( $cat_IDs as $cat_ID ) { - $child = $cat_ID; - while ( $parent = $cats[$child]->category_parent ) { - if ( !empty($cat_items[$cat_ID]) ) - foreach ( $cat_items[$cat_ID] as $item_id => $touches ) - ++$cat_items[$parent][$item_id]; - $child = $parent; - } - } - - // Transfer the touched cells - foreach ( (array) $cat_items as $id => $items ) - if ( isset($cats[$id]) ) - $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items); -} - -function _get_category_hierarchy() { - return _get_term_hierarchy('category'); -} - // Tags function &get_tags($args = '') { diff --git a/wp-includes/post.php b/wp-includes/post.php index 95142a52a5..e3d95e209c 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -447,11 +447,8 @@ function wp_delete_post($postid = 0) { function wp_get_post_categories($post_id = 0) { $post_id = (int) $post_id; - $cats = &get_the_category($post_id); - $cat_ids = array(); - foreach ( $cats as $cat ) - $cat_ids[] = (int) $cat->cat_ID; - return array_unique($cat_ids); + $cats = get_object_terms($post_id, 'category', 'get=ids'); + return $cats; } function wp_get_post_tags( $post_id = 0 ) { @@ -805,54 +802,10 @@ function wp_set_post_categories($post_ID = 0, $post_categories = array()) { if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories)) $post_categories = array(get_option('default_category')); + $post_categories = array_map('intval', $post_categories); $post_categories = array_unique($post_categories); - // First the old categories - $old_categories = $wpdb->get_col(" - SELECT category_id - FROM $wpdb->post2cat - WHERE post_id = '$post_ID' AND rel_type = 'category'"); - - if (!$old_categories) { - $old_categories = array(); - } else { - $old_categories = array_unique($old_categories); - } - - // Delete any? - $delete_cats = array_diff($old_categories,$post_categories); - - if ($delete_cats) { - foreach ($delete_cats as $del) { - $wpdb->query(" - DELETE FROM $wpdb->post2cat - WHERE category_id = '$del' - AND post_id = '$post_ID' AND rel_type = 'category' - "); - } - } - - // Add any? - $add_cats = array_diff($post_categories, $old_categories); - - if ($add_cats) { - foreach ($add_cats as $new_cat) { - $new_cat = (int) $new_cat; - if ( !empty($new_cat) ) - $wpdb->query(" - INSERT INTO $wpdb->post2cat (post_id, category_id) - VALUES ('$post_ID', '$new_cat')"); - } - } - - // Update category counts. - $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); - foreach ( $all_affected_cats as $cat_id ) { - $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$cat_id' AND rel_type = 'category'"); - $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count', type = type | " . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_id'"); - clean_category_cache($cat_id); - do_action('edit_category', $cat_id); - } + return wp_set_object_terms($post_ID, $post_categories, 'category'); } // wp_set_post_categories() // diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index f8f1e2d1ba..6951ff48c5 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -224,6 +224,8 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) { if ( ! $append ) { $in_taxonomies = "'" . implode("', '", $taxonomies) . "'"; $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'"); + if ( empty($old_terms) ) + $old_terms = array(); } $tt_ids = array(); @@ -262,6 +264,7 @@ function wp_set_object_terms($object_id, $terms, $taxonomies, $append = false) { function get_object_terms($object_id, $taxonomy, $args = array()) { global $wpdb; $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy; + // TODO cast to int $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; $defaults = array('orderby' => 'name', 'order' => 'ASC', 'get' => 'everything');