WordPress.org

Make WordPress Core

Ticket #3723: taxonomy_type.diff

File taxonomy_type.diff, 8.5 KB (added by ryan, 8 years ago)

Add category type field and use it instead of -1 counts

  • wp-includes/category.php

     
    11<?php 
    22 
     3define('TAXONOMY_CATEGORY', 1); 
     4define('TAXONOMY_TAG', 2); 
     5 
    36function get_all_category_ids() { 
    47        global $wpdb; 
    58 
     
    7881                else 
    7982                        $where .= ' AND category_count > 0'; 
    8083        } else { 
    81                 $where .= ' AND ( tag_count = 0 OR ( tag_count != 0 AND ( link_count > 0 OR category_count > 0 ) ) ) '; 
     84                $where .= ' AND ( type & ' . TAXONOMY_CATEGORY . ' != 0 ) '; 
    8285        } 
    8386 
    8487         
  • wp-includes/version.php

     
    33// This holds the version number in a separate file so we can bump it without cluttering the SVN 
    44 
    55$wp_version = '2.2-bleeding'; 
    6 $wp_db_version = 4865; 
     6$wp_db_version = 5183; 
    77 
    88?> 
  • wp-includes/post.php

     
    787787                if ( !$tag_slug = sanitize_title( $tag ) ) 
    788788                        continue; // discard 
    789789                if ( !$tag_id = category_exists( $tag ) ) 
    790                         $tag_id = wp_create_category( $tag ); 
     790                        $tag_id = wp_create_tag( $tag ); 
    791791                $tag_ids[] = $tag_id; 
    792792        } 
    793793 
     
    837837        $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) ); 
    838838        foreach ( $all_affected_tags as $tag_id ) { 
    839839                $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 = '$tag_id' AND rel_type = 'tag'" ); 
    840                 $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '$count' WHERE cat_ID = '$tag_id'" ); 
     840                $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '$count', type = type | " . TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" ); 
    841841                if ( $count == 0 ) 
    842                         $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '-1' WHERE cat_ID = '$tag_id'" ); 
     842                        $wpdb->query( "UPDATE $wpdb->categories SET type = type & ~". TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" ); 
    843843                clean_category_cache( $tag_id ); 
    844844                do_action( 'edit_category', $tag_id ); 
    845845                do_action( 'edit_tag', $tag_id ); 
     
    876876                        $wpdb->query(" 
    877877                                DELETE FROM $wpdb->post2cat 
    878878                                WHERE category_id = '$del' 
    879                                         AND post_id = '$post_ID' 
     879                                        AND post_id = '$post_ID' AND rel_type = 'category' 
    880880                                "); 
    881881                } 
    882882        } 
     
    898898        $all_affected_cats = array_unique(array_merge($post_categories, $old_categories)); 
    899899        foreach ( $all_affected_cats as $cat_id ) { 
    900900                $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'"); 
    901                 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); 
     901                $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count', type = type | " . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_id'"); 
    902902                clean_category_cache($cat_id); 
    903903                do_action('edit_category', $cat_id); 
    904904        } 
  • wp-admin/admin-functions.php

     
    647647 
    648648function return_categories_list( $parent = 0 ) { 
    649649        global $wpdb; 
    650         return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( ( link_count = 0 AND tag_count = 0 ) OR category_count != 0 OR ( link_count = 0 AND category_count = 0 AND tag_count = 0 ) ) ORDER BY category_count DESC" ); 
     650        return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( type & " . TAXONOMY_CATEGORY . " != 0 ) AND ( link_count = 0 OR category_count != 0 ) ORDER BY category_count DESC" ); 
    651651} 
    652652 
    653653function sort_cats( $cat1, $cat2 ) { 
     
    744744 
    745745function return_link_categories_list( $parent = 0 ) { 
    746746        global $wpdb; 
    747         return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( ( category_count = 0 AND tag_count = 0 ) OR link_count != 0 OR ( link_count = 0 AND category_count = 0 AND tag_count = 0 ) ) ORDER BY link_count DESC" ); 
     747        return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( type & " . TAXONOMY_CATEGORY . " != 0 ) AND ( category_count = 0 OR link_count != 0 ) ORDER BY link_count DESC" ); 
    748748} 
    749749 
    750750function get_nested_link_categories( $default = 0, $parent = 0 ) { 
  • wp-admin/admin-db.php

     
    121121        else 
    122122                $links_private = 0; 
    123123 
     124        if ( empty($type) ) 
     125                $type = TAXONOMY_CATEGORY; 
     126 
    124127        // Let's check if we have this category already, if so just do an update 
    125         if ( $cat_ID = category_exists( $category_nicename ) ) 
     128        if ( $cat_ID = category_exists( $category_nicename ) ) { 
    126129                $update = true; 
     130                $category = get_category($cat_ID); 
     131                // If inserting a category that already exists, OR in the new type rather 
     132                // than replacing the entire value. 
     133                $type = $category->type | $type; 
     134        } 
    127135 
    128136        if (!$update) { 
    129                 $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')"); 
     137                $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')"); 
    130138                $cat_ID = (int) $wpdb->insert_id; 
    131139        } else { 
    132                 $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' WHERE cat_ID = '$cat_ID'"); 
     140                $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'"); 
    133141        } 
    134142 
    135143        if ( $category_nicename == '' ) { 
     
    195203 
    196204        $parent = $category->category_parent; 
    197205 
    198         // Delete the category 
    199         if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") ) 
    200                 return 0; 
    201  
     206        // Delete the category if it is not also a tag. 
     207        if ( 0 == ($category->type & TAXONOMY_TAG) ) { 
     208                if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") ) 
     209                        return 0; 
     210        } else { 
     211                $wpdb->query("UPDATE $wpdb->categories SET type = type & ~" . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_ID'"); 
     212        } 
    202213        // Update children to point to new parent 
    203214        $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'"); 
    204215 
    205216        // Only set posts and links to the default category if they're not in another category already 
    206         $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'"); 
     217        $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID' AND rel_type = 'category'"); 
    207218        foreach ( (array) $posts as $post_id ) { 
    208219                $cats = wp_get_post_categories($post_id); 
    209220                if ( 1 == count($cats) ) 
     
    257268        return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'"); 
    258269} 
    259270 
     271function wp_create_tag($tag_name) { 
     272        $tag_array = array('cat_name' => $tag_name, 'type' => TAXONOMY_TAG); 
     273        return wp_insert_category($tag_array); 
     274} 
     275 
    260276function wp_delete_user($id, $reassign = 'novalue') { 
    261277        global $wpdb; 
    262278 
  • wp-admin/upgrade-schema.php

     
    2121  tag_count bigint(20) NOT NULL default '0', 
    2222  posts_private tinyint(1) NOT NULL default '0', 
    2323  links_private tinyint(1) NOT NULL default '0', 
     24  type tinyint NOT NULL default '1', 
    2425  PRIMARY KEY  (cat_ID), 
    2526  KEY category_nicename (category_nicename) 
    2627) $charset_collate;