Make WordPress Core

Ticket #3723: tags.diff

File tags.diff, 9.2 KB (added by ryan, 18 years ago)

Tagging using categories and post2tag tables

  • wp-includes/category.php

     
    2323                'hide_empty' => true, 'include_last_update_time' => false, 'hierarchical' => 1, 'exclude' => '', 'include' => '',
    2424                'number' => '', 'pad_counts' => false);
    2525        $r = array_merge($defaults, $r);
    26         if ( 'count' == $r['orderby'] )
    27                 $r['orderby'] = 'category_count';
    28         else
     26        if ( 'count' == $r['orderby'] ) {
     27                if ( 'link' == $type )
     28                        $r['orderby'] = 'link_count';
     29                else if ( 'post' == $type )
     30                        $r['orderby'] = 'category_count';
     31                else
     32                        $r['orderby'] = 'tagged_count';
     33        } else
    2934                $r['orderby'] = "cat_" . $r['orderby'];  // restricts order by to cat_ID and cat_name fields
    3035        $r['number'] = (int) $r['number'];
     36        if ( 'tag' == $type )
     37                $r['hierarchical'] = 0;
    3138        extract($r);
    3239
    3340        $key = md5( serialize( $r ) );
     
    7582        if ( $hide_empty && !$hierarchical ) {
    7683                if ( 'link' == $type )
    7784                        $where .= ' AND link_count > 0';
     85                else if ( 'post' == $type )
     86                        $where .= ' AND category_count > 0';
    7887                else
    79                         $where .= ' AND category_count > 0';
     88                        $where .= ' AND tagged_count > 0';
    8089        }
    8190
    8291        if ( !empty($number) )
     
    276285                $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'");
    277286                foreach ( $results as $row )
    278287                        ++$cat_items[$row->category_id][$row->post_id];
    279         } else {
     288        } else if ( $type == 'link' ) {
    280289                $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'");
    281290                foreach ( $results as $row )
    282291                        ++$cat_items[$row->category_id][$row->link_id];
     292        } else {
     293                $results = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2tag LEFT JOIN $wpdb->posts ON post_id = ID WHERE category_id IN (".join(',', $cat_IDs).") AND post_type = 'post' AND post_status = 'publish'");
     294                foreach ( $results as $row )
     295                        ++$cat_items[$row->category_id][$row->post_id];
    283296        }
    284297
    285298        // Touch every ancestor's lookup row for each post in each category
     
    293306                }
    294307        }
    295308
    296         // Transfer the touched cells
     309        // Transfer the touched cells   // TAGFIX
    297310        foreach ( (array) $cat_items as $id => $items )
    298311                if ( isset($cats[$id]) )
    299312                        $cats[$id]->{'link' == $type ? 'link_count' : 'category_count'} = count($items);
  • wp-includes/post.php

     
    453453        return array_unique($cat_ids);
    454454}
    455455
     456function wp_get_post_tags($post_id = 0) {
     457        $cats = &get_the_tags($post_id);
     458        $cat_ids = array();
     459        foreach ( $cats as $cat )
     460                $cat_ids[] = (int) $cat->cat_ID;
     461        return array_unique($cat_ids);
     462}
     463
    456464function wp_get_recent_posts($num = 10) {
    457465        global $wpdb;
    458466
     
    761769        return wp_update_post(array('post_status' => 'publish', 'ID' => $post_id, 'no_filter' => true));
    762770}
    763771
    764 function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
     772function _set_post_categories($type = 'cat', $post_ID = 0, $post_categories = array()) {
    765773        global $wpdb;
    766         // If $post_categories isn't already an array, make it one:
    767         if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
    768                 $post_categories = array(get_option('default_category'));
    769774
    770775        $post_categories = array_unique($post_categories);
    771776
     777        if ( 'cat' == $type ) {
     778                $table = 'post2cat';
     779                $counter = 'category_count';
     780        } else {
     781                $table = 'post2tag';
     782                $counter = 'tagged_count';
     783        }
     784
    772785        // First the old categories
    773786        $old_categories = $wpdb->get_col("
    774787                SELECT category_id
    775                 FROM $wpdb->post2cat
     788                FROM $wpdb->$table
    776789                WHERE post_id = $post_ID");
    777790
    778791        if (!$old_categories) {
     
    787800        if ($delete_cats) {
    788801                foreach ($delete_cats as $del) {
    789802                        $wpdb->query("
    790                                 DELETE FROM $wpdb->post2cat
     803                                DELETE FROM $wpdb->$table
    791804                                WHERE category_id = $del
    792805                                        AND post_id = $post_ID
    793806                                ");
     
    801814                foreach ($add_cats as $new_cat) {
    802815                        if ( !empty($new_cat) )
    803816                                $wpdb->query("
    804                                         INSERT INTO $wpdb->post2cat (post_id, category_id)
     817                                        INSERT INTO $wpdb->$table (post_id, category_id)
    805818                                        VALUES ($post_ID, $new_cat)");
    806819                }
    807820        }
     
    809822        // Update category counts.
    810823        $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
    811824        foreach ( $all_affected_cats as $cat_id ) {
    812                 $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'");
    813                 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
     825                $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->$table, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->$table.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$cat_id'");
     826                $wpdb->query("UPDATE $wpdb->categories SET $counter = '$count' WHERE cat_ID = '$cat_id'");
    814827                clean_category_cache($cat_id);
    815828                do_action('edit_category', $cat_id);
    816829        }
    817 }       // wp_set_post_categories()
     830}
    818831
     832function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
     833        // If $post_categories isn't already an array, make it one:
     834        if (!is_array($post_categories) || 0 == count($post_categories) || empty($post_categories))
     835                $post_categories = array(get_option('default_category'));
     836
     837        return _set_post_categories($post_ID, $post_categories);
     838}
     839
     840function wp_set_post_tags($post_ID = 0, $post_tags = array()) {
     841        global $wpdb;
     842        // If $post_categories isn't already an array, make it one:
     843        if (!is_array($post_tags) || 0 == count($post_tags) || empty($post_tags))
     844                $post_tags = array();
     845
     846        return _set_post_categories($post_ID, $post_tags);
     847}
     848
    819849//
    820850// Trackback and ping functions
    821851//
  • wp-admin/admin-functions.php

     
    635635
    636636function return_categories_list( $parent = 0 ) {
    637637        global $wpdb;
    638         return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( link_count = 0 OR category_count != 0 OR ( link_count = 0 AND category_count = 0 ) ) ORDER BY category_count DESC" );
     638        return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( link_count = 0 OR category_count != 0 OR tagged_count = 0 OR ( link_count = 0 AND category_count = 0 AND tagged_count = 0 ) ) ORDER BY category_count DESC" );
    639639}
    640640
    641641function sort_cats( $cat1, $cat2 ) {
     
    709709
    710710function return_link_categories_list( $parent = 0 ) {
    711711        global $wpdb;
    712         return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( category_count = 0  OR link_count != 0 OR ( link_count = 0 AND category_count = 0 ) ) ORDER BY link_count DESC" );
     712        return $wpdb->get_col( "SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( category_count = 0  OR link_count != 0 OR tagged_count = 0 OR ( link_count = 0 AND category_count = 0 AND tagged_count = 0 ) ) ORDER BY link_count DESC" );
    713713}
    714714
    715715function get_nested_link_categories( $default = 0, $parent = 0 ) {
  • wp-admin/admin-db.php

     
    248248        return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
    249249}
    250250
     251function wp_create_tag($tag_name) {
     252        return wp_create_tag($tag_name);
     253}
     254
     255function wp_create_tags($tags, $post_id = '') {
     256        $cat_ids = array ();
     257        foreach ($tags as $tag) {
     258                if ($id = tag_exists($tag))
     259                        $tag_ids[] = $id;
     260                else
     261                        if ($id = wp_create_tag($tag))
     262                                $tag_ids[] = $id;
     263        }
     264
     265        if ($post_id)
     266                wp_set_post_tags($post_id, $tag_ids);
     267
     268        return $tag_ids;
     269}
     270
     271function tag_exists($tag_name) {
     272        return category_exists($tag_name);
     273}
     274
    251275function wp_delete_user($id, $reassign = 'novalue') {
    252276        global $wpdb;
    253277
  • wp-admin/upgrade-schema.php

     
    1818  category_parent bigint(20) NOT NULL default '0',
    1919  category_count bigint(20) NOT NULL default '0',
    2020  link_count bigint(20) NOT NULL default '0',
     21  tagged_count bigint(20) NOT NULL default '0',
    2122  posts_private tinyint(1) NOT NULL default '0',
    2223  links_private tinyint(1) NOT NULL default '0',
    2324  PRIMARY KEY  (cat_ID),
     
    9192  PRIMARY KEY  (rel_id),
    9293  KEY post_id (post_id,category_id)
    9394) $charset_collate;
     95CREATE TABLE $wpdb->post2tag (
     96  rel_id bigint(20) NOT NULL auto_increment,
     97  post_id bigint(20) NOT NULL default '0',
     98  tag_id bigint(20) NOT NULL default '0',
     99  PRIMARY KEY  (rel_id),
     100  KEY post_id (post_id,tag_id)
     101) $charset_collate;
    94102CREATE TABLE $wpdb->postmeta (
    95103  meta_id bigint(20) NOT NULL auto_increment,
    96104  post_id bigint(20) NOT NULL default '0',