Make WordPress Core

Ticket #4189: 4189c.diff

File 4189c.diff, 10.5 KB (added by ryan, 18 years ago)

4189b + syntax fixes

  • wp-includes/taxonomy.php

     
     1<?php
     2
     3class WPTaxonomy {
     4
     5        // This will look like (1 => 'post_category', 2 => 'link_category') etc.
     6        var $taxonomies;
     7       
     8        function WPTaxonomy() {
     9        }
     10
     11        /**
     12         * Adds a new term to the database.  Optionally marks it as an alias of an existing term.
     13         * @param string $term The term to add.
     14         * @param int|string $alias_of The id or slug of the new term's alias.
     15         */
     16        function add_term($term, $alias_of = '') {
     17                global $wpdb;
     18                $term_slug = sanitize_title($term);             
     19                if ($alias_of) {
     20                        $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE term_slug = '$alias_of'");
     21                        if ($alias->term_group) {
     22                                // The alias we want is already in a group, so let's use that one.
     23                                $term_group = $alias->term_group;
     24                        } else {
     25                                // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
     26                                $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1;
     27                                $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id");
     28                        }
     29                } else {
     30                        $term_group = 0;
     31                }
     32                // Now add or replace the term.  This works because we have a UNIQUE key on term_slug.
     33                $wpdb->query("REPLACE INTO $wpdb->terms (term_name, term_slug, term_group) VALUES ($term, $term_slug, $term_group)");
     34        }
     35
     36        /**
     37         * Removes a term from the database.
     38         */
     39        function remove_term() {
     40               
     41        }
     42       
     43       
     44        /**
     45         * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
     46         */
     47        function is_defined_term($term) {
     48                global $wpdb;
     49                return $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE term_slug = '$term'");
     50        }
     51       
     52        /**
     53         * Given an array of terms, returns those that are defined term slugs.  Ignores integers.
     54         * @param array $terms The term slugs to check for a definition.
     55         */
     56        function get_defined_terms($terms) {
     57                global $wpdb;
     58                foreach ($terms as $term) {
     59                        if (!is_int($term)) {
     60                                $searches[] = $term;
     61                        }
     62                }
     63                $terms = "'" . implode("', '", $searches) . "'";
     64                return $wpdb->get_col("SELECT term_slug FROM $wpdb->terms WHERE term_slug IN ($terms)");
     65        }
     66       
     67        /**
     68         * Adds a term taxonomy entry or increments the count if it already exists.
     69         * @param int|string $term The term to add.
     70         * @param string $taxonomy The context.
     71         * @param string $description An optional description of the relationship.
     72         * @param int $parent The parent of the term in this context.
     73         */
     74        function add_term_taxonomy($term, $taxonomy, $description = '', $parent = 0) {
     75                $clause = (is_int($term)) ? "t.term_id = $term" : "t.term_slug = '$term'";
     76                if ($count = $wpdb->query("SELECT tt.count FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '$taxonomy' AND $clause")) {
     77      // We'll be updating whatever's there, and incrementing the count.
     78      $count++;
     79                        $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = $count");
     80                } else {
     81      // There was no existing entry for this term and taxonomy type, so add a new one, using the supplied description and parent, with a count of 1.
     82      $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, term_description, parent, count) SELECT term_id, '$taxonomy', '$description', $parent, 1 FROM $wpdb->terms AS t1 WHERE $clause");
     83                }
     84        }       
     85
     86        /**
     87         * Removes a term from a particular taxonomy or set of taxonomies.  Attaches orphaned children in one of various schemes.
     88         * @param int|string $term The slug or id of the term.
     89         * @param string|array $taxonomy The taxonomy or taxonomies to remove from.
     90         * @param string (delete|parent|root|ignore) $attach_children.  Where to attach orphaned children.               
     91         */             
     92        function remove_term_taxonomy($term, $taxonomy, $attach_children = 'root') {
     93               
     94        }
     95       
     96        /**
     97         * Relates an object (post, link etc) to a term and taxonomy type.  Creates the term and taxonomy
     98         * relationship if it doesn't already exist.  Creates a term if it doesn't exist (using the term_slug).
     99         * @param array|int|string $term The slug or id of the term.
     100         * @param int $object_id The object to relate to.
     101         * @param array|string $taxonomies The context(s) in which to relate the term to the object.
     102         */
     103        function add_term_relationship($terms, $object_id, $taxonomies) {
     104                global $wpdb;
     105               
     106                if (!is_array($taxonomies)) {
     107                        $taxonomies = array($taxonomies);
     108                }
     109               
     110                if (!is_array($terms)) {
     111                        $terms = array($terms);
     112                }
     113               
     114                $defined_terms = $this->get_defined_terms($terms);
     115
     116                foreach ($terms as $term) {
     117                        if (!is_int($term)) {
     118                                if (!isset($defined_terms[$term])) {
     119                                        $new_terms[] = $term;
     120                                }
     121                                $term_slugs[] = $term;
     122                        } else {
     123                                $term_ids[] = $term;
     124                        }
     125                }
     126               
     127                if ( isset($new_terms) ) {
     128                        // At least one of the terms specified doesn't exist, so add any new ones, using the term_slug as the name.
     129                        $this->add_terms($new_terms);
     130                }
     131
     132                $term_clause = isset($term_ids) ? 'tt.term_id IN (' . implode(', ', $term_ids) . ')' : '';
     133                if (isset($term_slugs)) {
     134                        if ($term_clause) {
     135                                $term_clause .= ' OR ';
     136                        }
     137                        $term_clause .= "t.term_slug IN ('" . implode("', '", $term_slugs) . "')";
     138                        $term_join = "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id";
     139                } else {
     140                        $term_join = '';
     141                }
     142               
     143                // Now add or increment the term taxonomy relationships.  This is inefficient at the moment.
     144                foreach ($taxonomies as $taxonomy) {
     145                        foreach ($terms as $term) {
     146                                $this->add_term_taxonomy($term, $taxonomy);
     147                        }
     148                }
     149               
     150                $taxonomies = "'" . implode("', '", $taxonomies) . "'";
     151               
     152                // Finally, relate the term and taxonomy to the object.
     153                $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)");
     154        }
     155       
     156        /**
     157         * Returns the terms associated with the given object(s), in the supplied taxonomies.
     158         * @param int|array $object_id The id of the object(s)) to retrieve for.
     159         * @param string|array $taxonomies The taxonomies to retrieve terms from.
     160         * @return array The requested term data.                       
     161         */
     162        function get_object_terms($object_id, $taxonomy) {
     163                global $wpdb;
     164                $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy;
     165                $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
     166
     167                $taxonomies = "'" . implode("', '", $taxonomies) . "'";         
     168                $object_ids = implode(', ', $object_ids);               
     169
     170                if ( $taxonomy_data = $wpdb->get_results("SELECT tr.object_id, tt.taxonomy, 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)") ) {
     171                        if ($single_taxonomy && $single_object) {
     172                                // Just one kind of taxonomy for one object.
     173                                return $taxonomy_data;
     174                        } else {
     175                                foreach ($taxonomy_data as $data) {
     176                                        if ($single_taxonomy) {
     177                                                // Many objects, one taxonomy type.
     178                                                $return[$data->object_id][] = $data;
     179                                        } elseif ($single_object) {
     180                                                // One object, many taxonomies.
     181                                                $return[$data->taxonomy][] = $data;
     182                                        } else {
     183                                                // Many objects, many taxonomies.
     184                                                $return[$data->object_id][$data->taxonomy][] = $data;
     185                                        }
     186                                }
     187                                return $return;                 
     188                        }
     189                } else {
     190                        return array();
     191                }               
     192        }       
     193}
     194
     195?>
  • wp-admin/upgrade-schema.php

     
    1010                $charset_collate .= " COLLATE $wpdb->collate";
    1111}
    1212
    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)
    2720) $charset_collate;
     21CREATE 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;
     31CREATE 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;
    2837CREATE TABLE $wpdb->comments (
    2938  comment_ID bigint(20) unsigned NOT NULL auto_increment,
    3039  comment_post_ID int(11) NOT NULL default '0',
     
    4554  KEY comment_approved (comment_approved),
    4655  KEY comment_post_ID (comment_post_ID)
    4756) $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;
    5557CREATE TABLE $wpdb->links (
    5658  link_id bigint(20) NOT NULL auto_increment,
    5759  link_url varchar(255) NOT NULL default '',
     
    8688  PRIMARY KEY  (option_id,blog_id,option_name),
    8789  KEY option_name (option_name)
    8890) $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;
    9791CREATE TABLE $wpdb->postmeta (
    9892  meta_id bigint(20) NOT NULL auto_increment,
    9993  post_id bigint(20) NOT NULL default '0',
     
    404398        }
    405399}
    406400
    407 ?>
    408  No newline at end of file
     401?>