Ticket #4189: terms.2.diff
File terms.2.diff, 15.4 KB (added by , 17 years ago) |
---|
-
wp-includes/wp-db.php
34 34 var $optiongroups; 35 35 var $optiongroup_options; 36 36 var $postmeta; 37 var $terms; 38 var $term_taxonomy; 39 var $term_relationships; 37 40 38 41 var $charset; 39 42 var $collate; -
wp-includes/taxonomy.php
1 <?php 2 3 /** 4 * Adds a new term to the database. Optionally marks it as an alias of an existing term. 5 * @param string $term The term to add. 6 * @param string $taxonomy The taxonomy to which to add the term 7 * @param int|string $alias_of The id or slug of the new term's alias. 8 */ 9 function add_term( $term, $taxonomy, $args = array() ) { 10 global $wpdb; 11 $term_slug = sanitize_title($term); 12 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0); 13 $args = wp_parse_args($args, $defaults); 14 extract($args); 15 16 $term_group = 0; 17 if ($alias_of) { 18 $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE term_slug = '$alias_of'"); 19 if ($alias->term_group) { 20 // The alias we want is already in a group, so let's use that one. 21 $term_group = $alias->term_group; 22 } else { 23 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. 24 $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1; 25 $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id"); 26 } 27 } 28 29 // Now add or replace the term. This works because we have a UNIQUE key on term_slug. 30 $wpdb->query("REPLACE INTO $wpdb->terms (term_name, term_slug, term_group) VALUES ('$term', '$term_slug', '$term_group')"); 31 $term_id = (int) $wpdb->insert_id; 32 33 $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"); 34 35 if ( ! empty($tt_id) ) 36 return $tt_id; 37 38 $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, term_description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')"); 39 } 40 41 /** 42 * Removes a term from the database. 43 */ 44 function remove_term() {} 45 46 47 /** 48 * Returns the index of a defined term, or 0 (false) if the term doesn't exist. 49 */ 50 function is_term($term, $taxonomy = '') { 51 global $wpdb; 52 $term = sanitize_title($term); 53 return $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE term_slug = '$term'"); 54 } 55 56 /** 57 * Given an array of terms, returns those that are defined term slugs. Ignores integers. 58 * @param array $terms The term slugs to check for a definition. 59 */ 60 function get_defined_terms($terms) { 61 global $wpdb; 62 63 foreach ($terms as $term) { 64 if (!is_int($term)) { 65 $searches[] = $term; 66 } 67 } 68 $terms = "'" . implode("', '", $searches) . "'"; 69 return $wpdb->get_col("SELECT term_slug FROM $wpdb->terms WHERE term_slug IN ($terms)"); 70 } 71 72 /** 73 * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy 74 * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the term_slug). 75 * @param array|int|string $term The slug or id of the term. 76 * @param int $object_id The object to relate to. 77 * @param array|string $taxonomies The context(s) in which to relate the term to the object. 78 */ 79 function add_term_relationship($terms, $object_id, $taxonomies) { 80 global $wpdb; 81 82 if (!is_array($taxonomies)) { 83 $taxonomies = array($taxonomies); 84 } 85 86 if (!is_array($terms)) { 87 $terms = array($terms); 88 } 89 90 $defined_terms = get_defined_terms($terms); 91 92 foreach ($terms as $term) { 93 if (!is_int($term)) { 94 if (!isset($defined_terms[$term])) { 95 $new_terms[] = $term; 96 } 97 $term_slugs[] = $term; 98 } else { 99 $term_ids[] = $term; 100 } 101 } 102 103 $term_clause = isset($term_ids) ? 'tt.term_id IN (' . implode(', ', $term_ids) . ')' : ''; 104 if (isset($term_slugs)) { 105 if ($term_clause) { 106 $term_clause .= ' OR '; 107 } 108 $term_clause .= "t.term_slug IN ('" . implode("', '", $term_slugs) . "')"; 109 $term_join = "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id"; 110 } else { 111 $term_join = ''; 112 } 113 114 // Now add or increment the term taxonomy relationships. This is inefficient at the moment. 115 foreach ($taxonomies as $taxonomy) { 116 foreach ($terms as $term) { 117 add_term($term, $taxonomy); 118 } 119 } 120 121 $taxonomies = "'" . implode("', '", $taxonomies) . "'"; 122 123 // Finally, relate the term and taxonomy to the object. 124 $wpdb->query("INSERT INTO $wpdb->term_relationships(object_id, term_taxonomy_id) SELECT '$object_id', term_taxonomy_id FROM $wpdb->term_taxonomy AS tt $term_join WHERE ($term_clause) AND tt.taxonomy IN ($taxonomies)"); 125 } 126 127 /** 128 * Returns the terms associated with the given object(s), in the supplied taxonomies. 129 * @param int|array $object_id The id of the object(s)) to retrieve for. 130 * @param string|array $taxonomies The taxonomies to retrieve terms from. 131 * @return array The requested term data. 132 */ 133 function get_object_terms($object_id, $taxonomy) { 134 global $wpdb; 135 $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy; 136 $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id; 137 138 $taxonomies = "'" . implode("', '", $taxonomies) . "'"; 139 $object_ids = implode(', ', $object_ids); 140 141 if ( $taxonomy_data = $wpdb->get_results("SELECT t.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids)") ) { 142 if ($single_taxonomy && $single_object) { 143 // Just one kind of taxonomy for one object. 144 return $taxonomy_data; 145 } else { 146 foreach ($taxonomy_data as $data) { 147 if ($single_taxonomy) { 148 // Many objects, one taxonomy type. 149 $return[$data->object_id][] = $data; 150 } elseif ($single_object) { 151 // One object, many taxonomies. 152 $return[$data->taxonomy][] = $data; 153 } else { 154 // Many objects, many taxonomies. 155 $return[$data->object_id][$data->taxonomy][] = $data; 156 } 157 } 158 return $return; 159 } 160 } else { 161 return array(); 162 } 163 } 164 165 ?> -
wp-includes/version.php
3 3 // This holds the version number in a separate file so we can bump it without cluttering the SVN 4 4 5 5 $wp_version = '2.3-alpha'; 6 $wp_db_version = 5 200;6 $wp_db_version = 5495; 7 7 8 8 ?> -
wp-includes/post.php
459 459 460 460 $post_id = (int) $post_id; 461 461 462 if ( !isset( $tag_cache[$blog_id][$post_id] ) ) 463 update_post_category_cache( $post_id ); // loads $tag_cache 464 465 return $tag_cache[$blog_id][$post_id]; 462 $tags = get_object_terms($post_id, 'post_tag'); 463 return $tags; 466 464 } 467 465 468 466 function wp_get_recent_posts($num = 10) { … … 792 790 793 791 if ( !$post_id ) 794 792 return false; 795 796 // prevent warnings for unintialized variables797 $tag_ids = array();798 793 799 794 if ( empty($tags) ) 800 795 $tags = array(); 801 796 $tags = (is_array($tags)) ? $tags : explode( ',', $tags ); 802 803 foreach ( $tags as $tag ) { 804 $tag = trim( $tag ); 805 if ( !$tag_slug = sanitize_title( $tag ) ) 806 continue; // discard 807 if ( !$tag_id = tag_exists( $tag ) ) 808 $tag_id = wp_create_tag( $tag ); 809 $tag_ids[] = $tag_id; 810 } 811 812 if ( empty($tag_ids) && ( !empty($tags) || $append ) ) 813 return false; 814 815 $tag_ids = array_unique( $tag_ids ); 816 817 // First the old tags 818 $old_tags = $wpdb->get_col(" 819 SELECT category_id 820 FROM $wpdb->post2cat 821 WHERE post_id = '$post_id' AND rel_type = 'tag'"); 822 823 if ( !$old_tags ) { 824 $old_tags = array(); 825 } else { 826 $old_tags = array_unique( $old_tags ); 827 } 828 829 // Delete any? 830 $delete_tags = array_diff( $old_tags, $tag_ids); 831 if ( $delete_tags && !$append ) { 832 foreach ( $delete_tags as $del ) { 833 $wpdb->query(" 834 DELETE FROM $wpdb->post2cat 835 WHERE category_id = '$del' 836 AND post_id = '$post_id' 837 AND rel_type = 'tag' 838 "); 839 } 840 } 841 842 // Add any? 843 $add_tags = array_diff( $tag_ids, $old_tags ); 844 if ( $add_tags ) { 845 foreach ( $add_tags as $new_tag ) { 846 $new_tag = (int) $new_tag; 847 if ( !empty($new_tag) ) 848 $wpdb->query(" 849 INSERT INTO $wpdb->post2cat (post_id, category_id, rel_type) 850 VALUES ('$post_id', '$new_tag', 'tag')"); 851 } 852 } 853 854 // Update category counts. 855 $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) ); 856 foreach ( $all_affected_tags as $tag_id ) { 857 $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'" ); 858 $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '$count', type = type | " . TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" ); 859 if ( $count == 0 ) 860 $wpdb->query( "UPDATE $wpdb->categories SET type = type & ~". TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" ); 861 clean_category_cache( $tag_id ); 862 do_action( 'edit_category', $tag_id ); 863 do_action( 'edit_tag', $tag_id ); 864 } 797 add_term_relationship($tags, $post_id, 'post_tag'); 865 798 } 866 799 867 800 function wp_set_post_categories($post_ID = 0, $post_categories = array()) { -
wp-settings.php
116 116 $wpdb->options = $wpdb->prefix . 'options'; 117 117 $wpdb->postmeta = $wpdb->prefix . 'postmeta'; 118 118 $wpdb->usermeta = $wpdb->prefix . 'usermeta'; 119 $wpdb->terms = $wpdb->prefix . 'terms'; 120 $wpdb->term_taxonomy = $wpdb->prefix . 'term_taxonomy'; 121 $wpdb->term_relationships = $wpdb->prefix . 'term_relationships'; 119 122 120 123 if ( defined('CUSTOM_USER_TABLE') ) 121 124 $wpdb->users = CUSTOM_USER_TABLE; … … 168 171 require (ABSPATH . WPINC . '/version.php'); 169 172 require (ABSPATH . WPINC . '/deprecated.php'); 170 173 require (ABSPATH . WPINC . '/script-loader.php'); 174 require (ABSPATH . WPINC . '/taxonomy.php'); 171 175 172 176 if (strpos($_SERVER['PHP_SELF'], 'install.php') === false) { 173 177 // Used to guarantee unique hash cookies -
wp-admin/admin-functions.php
664 664 if ( !$post_id ) 665 665 return false; 666 666 667 $tags = $wpdb->get_results( " 668 SELECT category_id, cat_name 669 FROM $wpdb->categories, $wpdb->post2cat 670 WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_id' AND rel_type = 'tag' 671 " ); 667 $tags = wp_get_post_tags($post_id); 668 672 669 if ( !$tags ) 673 670 return false; 674 671 675 672 foreach ( $tags as $tag ) 676 $tag_names[] = $tag-> cat_name;673 $tag_names[] = $tag->term_name; 677 674 $tags_to_edit = join( ', ', $tag_names ); 678 675 $tags_to_edit = attribute_escape( $tags_to_edit ); 679 676 $tags_to_edit = apply_filters( 'tags_to_edit', $tags_to_edit ); -
wp-admin/admin-db.php
286 286 if (! $tag_nicename = sanitize_title($tag_name)) 287 287 return 0; 288 288 289 return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$tag_nicename' AND ( type & " . TAXONOMY_TAG . " != 0 )");289 return is_term($tag_name, 'post_tag'); 290 290 } 291 291 292 292 function wp_create_tag($tag_name) { 293 293 if ( $id = tag_exists($tag_name) ) 294 294 return $id; 295 $tag_array = array('cat_name' => $tag_name, 'type' => TAXONOMY_TAG);296 295 297 if ( $id = category_object_exists($tag_name) ) { 298 $category = get_category($id); 299 $tag_array['type'] = $category->type | $tag_array['type']; 300 $tag_array['cat_ID'] = $id; 301 $id = wp_update_category($tag_array); 302 return $id; 303 } else { 304 return wp_insert_category($tag_array); 305 } 296 $tag_id = add_term($tag_name, 'post_tag'); 306 297 } 307 298 308 299 function wp_delete_user($id, $reassign = 'novalue') { -
wp-admin/upgrade-schema.php
10 10 $charset_collate .= " COLLATE $wpdb->collate"; 11 11 } 12 12 13 $wp_queries="CREATE TABLE $wpdb->categories ( 14 cat_ID bigint(20) NOT NULL auto_increment, 15 cat_name varchar(55) NOT NULL default '', 16 category_nicename varchar(200) NOT NULL default '', 17 category_description longtext NOT NULL, 18 category_parent bigint(20) NOT NULL default '0', 19 category_count bigint(20) NOT NULL default '0', 20 link_count bigint(20) NOT NULL default '0', 21 tag_count bigint(20) NOT NULL default '0', 22 posts_private tinyint(1) NOT NULL default '0', 23 links_private tinyint(1) NOT NULL default '0', 24 type tinyint NOT NULL default '1', 25 PRIMARY KEY (cat_ID), 26 KEY category_nicename (category_nicename) 13 $wp_queries="CREATE TABLE $wpdb->terms ( 14 term_id bigint(20) NOT NULL auto_increment, 15 term_name varchar(55) NOT NULL default '', 16 term_slug varchar(200) NOT NULL default '', 17 term_group bigint(10) NOT NULL default 0, 18 PRIMARY KEY (term_id), 19 UNIQUE KEY term_slug (term_slug) 27 20 ) $charset_collate; 21 CREATE TABLE $wpdb->term_taxonomy ( 22 term_taxonomy_id bigint(20) NOT NULL auto_increment, 23 term_id bigint(20) NOT NULL default 0, 24 taxonomy varchar(32) NOT NULL default '', 25 term_description longtext NOT NULL, 26 parent bigint(20) NOT NULL default 0, 27 count bigint(20) NOT NULL default 0, 28 PRIMARY KEY (term_taxonomy_id), 29 UNIQUE KEY (term_id, taxonomy) 30 ) $charset_collate; 31 CREATE TABLE $wpdb->term_relationships ( 32 object_id bigint(20) NOT NULL default 0, 33 term_taxonomy_id bigint(20) NOT NULL default 0, 34 PRIMARY KEY (object_id), 35 KEY (term_taxonomy_id) 36 ) $charset_collate; 28 37 CREATE TABLE $wpdb->comments ( 29 38 comment_ID bigint(20) unsigned NOT NULL auto_increment, 30 39 comment_post_ID int(11) NOT NULL default '0', … … 45 54 KEY comment_approved (comment_approved), 46 55 KEY comment_post_ID (comment_post_ID) 47 56 ) $charset_collate; 48 CREATE TABLE $wpdb->link2cat (49 rel_id bigint(20) NOT NULL auto_increment,50 link_id bigint(20) NOT NULL default '0',51 category_id bigint(20) NOT NULL default '0',52 PRIMARY KEY (rel_id),53 KEY link_id (link_id,category_id)54 ) $charset_collate;55 57 CREATE TABLE $wpdb->links ( 56 58 link_id bigint(20) NOT NULL auto_increment, 57 59 link_url varchar(255) NOT NULL default '', … … 86 88 PRIMARY KEY (option_id,blog_id,option_name), 87 89 KEY option_name (option_name) 88 90 ) $charset_collate; 89 CREATE TABLE $wpdb->post2cat (90 rel_id bigint(20) NOT NULL auto_increment,91 post_id bigint(20) NOT NULL default '0',92 category_id bigint(20) NOT NULL default '0',93 rel_type varchar(64) NOT NULL default 'category',94 PRIMARY KEY (rel_id),95 KEY post_id (post_id,category_id)96 ) $charset_collate;97 91 CREATE TABLE $wpdb->postmeta ( 98 92 meta_id bigint(20) NOT NULL auto_increment, 99 93 post_id bigint(20) NOT NULL default '0', … … 404 398 } 405 399 } 406 400 407 ?> 408 No newline at end of file 401 ?>