Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r15426 r17443  
    1919        'hierarchical' => true,
    2020        'update_count_callback' => '_update_post_term_count',
    21         'query_var' => false,
    22         'rewrite' => false,
    23         'public' => true,
    24         'show_ui' => true,
    25         '_builtin' => true,
    26     ) ) ;
    27 
    28     register_taxonomy( 'post_tag', 'post', array(
    29         'hierarchical' => false,
    30         'update_count_callback' => '_update_post_term_count',
    31         'query_var' => false,
    32         'rewrite' => false,
     21        'query_var' => 'category_name',
     22        'rewrite' => did_action( 'init' ) ? array(
     23                    'hierarchical' => true,
     24                    'slug' => get_option('category_base') ? get_option('category_base') : 'category',
     25                    'with_front' => false) : false,
    3326        'public' => true,
    3427        'show_ui' => true,
     
    3629    ) );
    3730
     31    register_taxonomy( 'post_tag', 'post', array(
     32        'hierarchical' => false,
     33        'update_count_callback' => '_update_post_term_count',
     34        'query_var' => 'tag',
     35        'rewrite' => did_action( 'init' ) ? array(
     36                    'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
     37                    'with_front' => false) : false,
     38        'public' => true,
     39        'show_ui' => true,
     40        '_builtin' => true,
     41    ) );
     42
    3843    register_taxonomy( 'nav_menu', 'nav_menu_item', array(
     44        'public' => false,
    3945        'hierarchical' => false,
    4046        'labels' => array(
     
    4753        '_builtin' => true,
    4854        'show_in_nav_menus' => false,
    49     ) ) ;
     55    ) );
    5056
    5157    register_taxonomy( 'link_category', 'link', array(
    5258        'hierarchical' => false,
    5359        'labels' => array(
    54             'name' => __( 'Categories' ),
    55             'singular_name' => __( 'Category' ),
    56             'update_item' => __( 'Update Category' ),
     60            'name' => __( 'Link Categories' ),
     61            'singular_name' => __( 'Link Category' ),
     62            'search_items' => __( 'Search Link Categories' ),
     63            'popular_items' => null,
     64            'all_items' => __( 'All Link Categories' ),
     65            'edit_item' => __( 'Edit Link Category' ),
     66            'update_item' => __( 'Update Link Category' ),
     67            'add_new_item' => __( 'Add New Link Category' ),
     68            'new_item_name' => __( 'New Link Category Name' ),
     69            'separate_items_with_commas' => null,
     70            'add_or_remove_items' => null,
     71            'choose_from_most_used' => null,
    5772        ),
    5873        'query_var' => false,
     
    6176        'show_ui' => false,
    6277        '_builtin' => true,
    63     ) ) ;
     78    ) );
     79
     80    $rewrite = false;
     81    if ( did_action( 'init' ) ) {
     82        $rewrite = apply_filters( 'post_format_rewrite_base', 'type' );
     83        $rewrite = $rewrite ? array( 'slug' => $rewrite ) : false;
     84    }
     85
     86    register_taxonomy( 'post_format', 'post', array(
     87        'public' => true,
     88        'hierarchical' => false,
     89        'labels' => array(
     90            'name' => _x( 'Format', 'post format' ),
     91            'singular_name' => _x( 'Format', 'post format' ),
     92        ),
     93        'query_var' => true,
     94        'rewrite' => $rewrite,
     95        'show_ui' => false,
     96        '_builtin' => true,
     97        'show_in_nav_menus' => false,
     98    ) );
    6499}
    65100add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
     
    286321            'slug' => sanitize_title_with_dashes($taxonomy),
    287322            'with_front' => true,
     323            'hierarchical' => false
    288324        ));
    289         $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
    290         $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']);
     325
     326        if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )
     327            $tag = '(.+?)';
     328        else
     329            $tag = '([^/]+)';
     330
     331        $wp_rewrite->add_rewrite_tag("%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
     332        $wp_rewrite->add_permastruct($taxonomy, "{$wp_rewrite->root}{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']);
    291333    }
    292334
     
    319361
    320362    // register callback handling for metabox
    321     add_filter('wp_ajax_add-'.$taxonomy, '_wp_ajax_add_hierarchical_term');
     363    add_filter('wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term');
    322364}
    323365
     
    368410        'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
    369411    );
     412    $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
    370413
    371414    return _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );
     
    461504
    462505    return $object_ids;
     506}
     507
     508/**
     509 * Given a taxonomy query, generates SQL to be appended to a main query.
     510 *
     511 * @since 3.1.0
     512 *
     513 * @see WP_Tax_Query
     514 *
     515 * @param array $tax_query A compact tax query
     516 * @param string $primary_table
     517 * @param string $primary_id_column
     518 * @return array
     519 */
     520function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
     521    $tax_query_obj = new WP_Tax_Query( $tax_query );
     522    return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
     523}
     524
     525/**
     526 * Container class for a multiple taxonomy query.
     527 *
     528 * @since 3.1.0
     529 */
     530class WP_Tax_Query {
     531
     532    /**
     533     * List of taxonomy queries. A single taxonomy query is an associative array:
     534     * - 'taxonomy' string The taxonomy being queried
     535     * - 'terms' string|array The list of terms
     536     * - 'field' string (optional) Which term field is being used.
     537     *      Possible values: 'term_id', 'slug' or 'name'
     538     *      Default: 'term_id'
     539     * - 'operator' string (optional)
     540     *      Possible values: 'IN' and 'NOT IN'.
     541     *      Default: 'IN'
     542     * - 'include_children' bool (optional) Whether to include child terms.
     543     *      Default: true
     544     *
     545     * @since 3.1.0
     546     * @access public
     547     * @var array
     548     */
     549    var $queries = array();
     550
     551    /**
     552     * The relation between the queries. Can be one of 'AND' or 'OR'.
     553     *
     554     * @since 3.1.0
     555     * @access public
     556     * @var string
     557     */
     558    var $relation;
     559
     560    /**
     561     * PHP4 type constructor.
     562     *
     563     * Parses a compact tax query and sets defaults.
     564     *
     565     * @since 3.1.0
     566     * @access public
     567     *
     568     * @param array $tax_query A compact tax query:
     569     *  array(
     570     *    'relation' => 'OR',
     571     *    array(
     572     *      'taxonomy' => 'tax1',
     573     *      'terms' => array( 'term1', 'term2' ),
     574     *      'field' => 'slug',
     575     *    ),
     576     *    array(
     577     *      'taxonomy' => 'tax2',
     578     *      'terms' => array( 'term-a', 'term-b' ),
     579     *      'field' => 'slug',
     580     *    ),
     581     *  )
     582     *
     583     * @return WP_Tax_Query
     584     */
     585    function WP_Tax_Query( $tax_query ) {
     586        if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
     587            $this->relation = 'OR';
     588        } else {
     589            $this->relation = 'AND';
     590        }
     591
     592        $defaults = array(
     593            'taxonomy' => '',
     594            'terms' => array(),
     595            'include_children' => true,
     596            'field' => 'term_id',
     597            'operator' => 'IN',
     598        );
     599
     600        foreach ( $tax_query as $query ) {
     601            if ( ! is_array( $query ) )
     602                continue;
     603
     604            $query = array_merge( $defaults, $query );
     605
     606            $query['terms'] = (array) $query['terms'];
     607
     608            $this->queries[] = $query;
     609        }
     610    }
     611
     612    /**
     613     * Generates SQL clauses to be appended to a main query.
     614     *
     615     * @since 3.1.0
     616     * @access public
     617     *
     618     * @param string $primary_table
     619     * @param string $primary_id_column
     620     * @return array
     621     */
     622    function get_sql( $primary_table, $primary_id_column ) {
     623        global $wpdb;
     624
     625        $join = '';
     626        $where = array();
     627        $i = 0;
     628
     629        foreach ( $this->queries as $query ) {
     630            extract( $query );
     631
     632            if ( ! taxonomy_exists( $taxonomy ) )
     633                return array( 'join' => '', 'where' => ' AND 0 = 1');
     634
     635            $terms = array_unique( (array) $terms );
     636
     637            if ( empty( $terms ) )
     638                continue;
     639
     640            if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) {
     641                $this->_transform_terms( $terms, $taxonomy, $field, 'term_id' );
     642
     643                $children = array();
     644                foreach ( $terms as $term ) {
     645                    $children = array_merge( $children, get_term_children( $term, $taxonomy ) );
     646                    $children[] = $term;
     647                }
     648                $terms = $children;
     649
     650                $this->_transform_terms( $terms, $taxonomy, 'term_id', 'term_taxonomy_id' );
     651            }
     652            else {
     653                $this->_transform_terms( $terms, $taxonomy, $field, 'term_taxonomy_id' );
     654            }
     655
     656            if ( 'IN' == $operator ) {
     657
     658                if ( empty( $terms ) ) {
     659                    if ( 'OR' == $this->relation )
     660                        continue;
     661                    else
     662                        return array( 'join' => '', 'where' => ' AND 0 = 1' );
     663                }
     664
     665                $terms = implode( ',', $terms );
     666
     667                $alias = $i ? 'tt' . $i : $wpdb->term_relationships;
     668
     669                $join .= " INNER JOIN $wpdb->term_relationships";
     670                $join .= $i ? " AS $alias" : '';
     671                $join .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
     672
     673                $where[] = "$alias.term_taxonomy_id $operator ($terms)";
     674            } elseif ( 'NOT IN' == $operator ) {
     675
     676                if ( empty( $terms ) )
     677                    continue;
     678
     679                $terms = implode( ',', $terms );
     680
     681                $where[] = "$primary_table.$primary_id_column NOT IN (
     682                    SELECT object_id
     683                    FROM $wpdb->term_relationships
     684                    WHERE term_taxonomy_id IN ($terms)
     685                )";
     686            } elseif ( 'AND' == $operator ) {
     687
     688                if ( empty( $terms ) )
     689                    continue;
     690
     691                $num_terms = count( $terms );
     692
     693                $terms = implode( ',', $terms );
     694
     695                $where[] = "$primary_table.$primary_id_column IN (
     696                    SELECT object_id
     697                    FROM $wpdb->term_relationships
     698                    WHERE term_taxonomy_id IN ($terms)
     699                    GROUP BY object_id HAVING COUNT(object_id) = $num_terms
     700                )";
     701            }
     702
     703            $i++;
     704        }
     705
     706        if ( !empty( $where ) )
     707            $where = ' AND ( ' . implode( " $this->relation ", $where ) . ' )';
     708        else
     709            $where = '';
     710
     711        return compact( 'join', 'where' );
     712    }
     713
     714    /**
     715     * Transforms a list of terms, from one field to another.
     716     *
     717     * @since 3.1.0
     718     * @access private
     719     *
     720     * @param array &$terms The list of terms
     721     * @param string $taxonomy The taxonomy of the terms
     722     * @param string $field The initial field
     723     * @param string $resulting_field The resulting field
     724     */
     725    function _transform_terms( &$terms, $taxonomy, $field, $resulting_field ) {
     726        global $wpdb;
     727
     728        if ( empty( $terms ) )
     729            return;
     730
     731        if ( $field == $resulting_field )
     732            return;
     733
     734        $resulting_field = esc_sql( $resulting_field );
     735
     736        switch ( $field ) {
     737            case 'slug':
     738            case 'name':
     739                $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $terms ) ) . "'";
     740                $terms = $wpdb->get_col( "
     741                    SELECT $wpdb->term_taxonomy.$resulting_field
     742                    FROM $wpdb->term_taxonomy
     743                    INNER JOIN $wpdb->terms USING (term_id)
     744                    WHERE taxonomy = '$taxonomy'
     745                    AND $wpdb->terms.$field IN ($terms)
     746                " );
     747                break;
     748
     749            default:
     750                $terms = implode( ',', array_map( 'intval', $terms ) );
     751                $terms = $wpdb->get_col( "
     752                    SELECT $resulting_field
     753                    FROM $wpdb->term_taxonomy
     754                    WHERE taxonomy = '$taxonomy'
     755                    AND term_id IN ($terms)
     756                " );
     757        }
     758    }
    463759}
    464760
     
    632928 * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term
    633929 *
    634  * @param string $term ID of Term to get children
     930 * @param string $term_id ID of Term to get children
    635931 * @param string $taxonomy Taxonomy Name
    636932 * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
     
    8071103 * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.
    8081104 *
    809  * @param string|array Taxonomy name or list of Taxonomy names
     1105 * @param string|array $taxonomies Taxonomy name or list of Taxonomy names
    8101106 * @param string|array $args The values of what to search for when returning terms
    8111107 * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
     
    8211117    }
    8221118
    823     foreach ( (array) $taxonomies as $taxonomy ) {
     1119    foreach ( $taxonomies as $taxonomy ) {
    8241120        if ( ! taxonomy_exists($taxonomy) ) {
    8251121            $error = & new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     
    8271123        }
    8281124    }
    829 
    830     $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
    8311125
    8321126    $defaults = array('orderby' => 'name', 'order' => 'ASC',
     
    8511145        $args['pad_counts'] = false;
    8521146    }
     1147
     1148    $args = apply_filters( 'get_terms_args', $args, $taxonomies );
     1149
    8531150    extract($args, EXTR_SKIP);
    8541151
     
    9011198        $order = '';
    9021199
    903     $where = '';
     1200    $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
    9041201    $inclusions = '';
    9051202    if ( !empty($include) ) {
     
    9231220        $excluded_trunks = wp_parse_id_list($exclude_tree);
    9241221        foreach ( $excluded_trunks as $extrunk ) {
    925             $excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids'));
     1222            $excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids', 'hide_empty' => 0));
    9261223            $excluded_children[] = $extrunk;
    9271224            foreach( $excluded_children as $exterm ) {
     
    9551252
    9561253    if ( !empty($name__like) )
    957         $where .= " AND t.name LIKE '{$name__like}%'";
     1254        $where .= " AND t.name LIKE '" . like_escape( $name__like ) . "%'";
    9581255
    9591256    if ( '' !== $parent ) {
     
    9681265    if ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {
    9691266        if ( $offset )
    970             $limit = 'LIMIT ' . $offset . ',' . $number;
     1267            $limits = 'LIMIT ' . $offset . ',' . $number;
    9711268        else
    972             $limit = 'LIMIT ' . $number;
     1269            $limits = 'LIMIT ' . $number;
    9731270    } else {
    974         $limit = '';
     1271        $limits = '';
    9751272    }
    9761273
     
    9821279    $selects = array();
    9831280    switch ( $fields ) {
    984         case 'all':
    985             $selects = array('t.*', 'tt.*');
    986             break;
    987         case 'ids':
     1281        case 'all':
     1282            $selects = array('t.*', 'tt.*');
     1283            break;
     1284        case 'ids':
    9881285        case 'id=>parent':
    989             $selects = array('t.term_id', 'tt.parent', 'tt.count');
    990             break;
    991         case 'names':
    992             $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
    993             break;
    994         case 'count':
     1286            $selects = array('t.term_id', 'tt.parent', 'tt.count');
     1287            break;
     1288        case 'names':
     1289            $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
     1290            break;
     1291        case 'count':
    9951292            $orderby = '';
    9961293            $order = '';
    997             $selects = array('COUNT(*)');
    998     }
    999     $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));
    1000 
    1001     $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where $orderby $order $limit";
     1294            $selects = array('COUNT(*)');
     1295    }
     1296
     1297    $_fields = $fields;
     1298
     1299    $fields = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));
     1300
     1301    $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
     1302
     1303    $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
     1304    $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
     1305    foreach ( $pieces as $piece )
     1306        $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
     1307
     1308    $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
     1309
     1310    $fields = $_fields;
    10021311
    10031312    if ( 'count' == $fields ) {
     
    10121321
    10131322    if ( empty($terms) ) {
    1014         wp_cache_add( $cache_key, array(), 'terms' );
     1323        wp_cache_add( $cache_key, array(), 'terms', 86400 ); // one day
    10151324        $terms = apply_filters('get_terms', array(), $taxonomies, $args);
    10161325        return $terms;
     
    10631372    }
    10641373
    1065     wp_cache_add( $cache_key, $terms, 'terms' );
     1374    wp_cache_add( $cache_key, $terms, 'terms', 86400 ); // one day
    10661375
    10671376    $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
     
    12241533
    12251534    if ( 'edit' == $context ) {
    1226         $value = apply_filters("edit_term_$field", $value, $term_id, $taxonomy);
    1227         $value = apply_filters("edit_${taxonomy}_$field", $value, $term_id);
     1535        $value = apply_filters("edit_term_{$field}", $value, $term_id, $taxonomy);
     1536        $value = apply_filters("edit_{$taxonomy}_{$field}", $value, $term_id);
    12281537        if ( 'description' == $field )
    1229             $value = format_to_edit($value);
     1538            $value = esc_html($value); // textarea_escaped
    12301539        else
    12311540            $value = esc_attr($value);
    12321541    } else if ( 'db' == $context ) {
    1233         $value = apply_filters("pre_term_$field", $value, $taxonomy);
    1234         $value = apply_filters("pre_${taxonomy}_$field", $value);
     1542        $value = apply_filters("pre_term_{$field}", $value, $taxonomy);
     1543        $value = apply_filters("pre_{$taxonomy}_{$field}", $value);
    12351544        // Back compat filters
    12361545        if ( 'slug' == $field )
     
    12381547
    12391548    } else if ( 'rss' == $context ) {
    1240         $value = apply_filters("term_${field}_rss", $value, $taxonomy);
    1241         $value = apply_filters("${taxonomy}_${field}_rss", $value);
     1549        $value = apply_filters("term_{$field}_rss", $value, $taxonomy);
     1550        $value = apply_filters("{$taxonomy}_{$field}_rss", $value);
    12421551    } else {
    12431552        // Use display filters by default.
    1244         $value = apply_filters("term_$field", $value, $term_id, $taxonomy, $context);
    1245         $value = apply_filters("${taxonomy}_$field", $value, $term_id, $context);
     1553        $value = apply_filters("term_{$field}", $value, $term_id, $taxonomy, $context);
     1554        $value = apply_filters("{$taxonomy}_{$field}", $value, $term_id, $context);
    12461555    }
    12471556
     
    12861595
    12871596/**
    1288  * Will unlink the term from the taxonomy.
    1289  *
    1290  * Will remove the term's relationship to the taxonomy, not the term or taxonomy
    1291  * itself. The term and taxonomy will still exist. Will require the term's
    1292  * object ID to perform the operation.
     1597 * Will unlink the object from the taxonomy or taxonomies.
     1598 *
     1599 * Will remove all relationships between the object and any terms in
     1600 * a particular taxonomy or taxonomies. Does not remove the term or
     1601 * taxonomy itself.
    12931602 *
    12941603 * @package WordPress
     
    12981607 *
    12991608 * @param int $object_id The term Object Id that refers to the term
    1300  * @param string|array $taxonomy List of Taxonomy Names or single Taxonomy name.
     1609 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.
    13011610 */
    13021611function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
     
    13561665
    13571666    $defaults = array();
     1667
     1668    if ( 'category' == $taxonomy ) {
     1669        $defaults['default'] = get_option( 'default_category' );
     1670        if ( $defaults['default'] == $term )
     1671            return 0; // Don't delete the default category
     1672    }
     1673
    13581674    $args = wp_parse_args($args, $defaults);
    13591675    extract($args, EXTR_SKIP);
    13601676
    1361     if ( isset($default) ) {
     1677    if ( isset( $default ) ) {
    13621678        $default = (int) $default;
    13631679        if ( ! term_exists($default, $taxonomy) )
     
    13931709    }
    13941710
     1711    // Clean the relationship caches for all object types using this term
     1712    $tax_object = get_taxonomy( $taxonomy );
     1713    foreach ( $tax_object->object_type as $object_type )
     1714        clean_object_term_cache( $objects, $object_type );
     1715
    13951716    do_action( 'delete_term_taxonomy', $tt_id );
    13961717    $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
     
    14071728
    14081729    return true;
     1730}
     1731
     1732/**
     1733 * Deletes one existing category.
     1734 *
     1735 * @since 2.0.0
     1736 * @uses wp_delete_term()
     1737 *
     1738 * @param int $cat_ID
     1739 * @return mixed Returns true if completes delete action; false if term doesnt exist;
     1740 *  Zero on attempted deletion of default Category; WP_Error object is also a possibility.
     1741 */
     1742function wp_delete_category( $cat_ID ) {
     1743    return wp_delete_term( $cat_ID, 'category' );
    14091744}
    14101745
     
    14361771 * @uses $wpdb
    14371772 *
    1438  * @param int|array $object_id The id of the object(s) to retrieve.
     1773 * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
    14391774 * @param string|array $taxonomies The taxonomies to retrieve terms from.
    14401775 * @param array|string $args Change what is returned
     
    17062041 *
    17072042 * @param int $object_id The object to relate to.
    1708  * @param array|int|string $term The slug or id of the term, will replace all existing
     2043 * @param array|int|string $terms The slug or id of the term, will replace all existing
    17092044 * related terms in this taxonomy.
    17102045 * @param array|string $taxonomy The context in which to relate the term to the object.
     
    19372272    }
    19382273
     2274    // Check $parent to see if it will cause a hierarchy loop
     2275    $parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args );
     2276
    19392277    // Check for duplicate slug
    19402278    $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
     
    24722810 * @since 2.5.0
    24732811 *
     2812 * @uses apply_filters() Calls 'term_link' with term link and term object, and taxonomy parameters.
     2813 * @uses apply_filters() For the post_tag Taxonomy, Calls 'tag_link' with tag link and tag ID as parameters.
     2814 * @uses apply_filters() For the category Taxonomy, Calls 'category_link' filter on category link and category ID.
     2815 *
    24742816 * @param object|int|string $term
    2475  * @param string $taxonomy
    2476  * @return string HTML link to taxonomy term archive
    2477  */
    2478 function get_term_link( $term, $taxonomy ) {
     2817 * @param string $taxonomy (optional if $term is object)
     2818 * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist.
     2819 */
     2820function get_term_link( $term, $taxonomy = '') {
    24792821    global $wp_rewrite;
    24802822
     
    24932835        return $term;
    24942836
    2495     // use legacy functions for core taxonomies until they are fully plugged in
    2496     if ( $taxonomy == 'category' )
    2497         return get_category_link((int) $term->term_id);
    2498     if ( $taxonomy == 'post_tag' )
    2499         return get_tag_link((int) $term->term_id);
     2837    $taxonomy = $term->taxonomy;
    25002838
    25012839    $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
    25022840
    25032841    $slug = $term->slug;
     2842    $t = get_taxonomy($taxonomy);
    25042843
    25052844    if ( empty($termlink) ) {
    2506         $t = get_taxonomy($taxonomy);
    25072845        if ( $t->query_var )
    25082846            $termlink = "?$t->query_var=$slug";
     
    25112849        $termlink = home_url($termlink);
    25122850    } else {
    2513         $termlink = str_replace("%$taxonomy%", $slug, $termlink);
     2851        if ( $t->rewrite['hierarchical'] ) {
     2852            $hierarchical_slugs = array();
     2853            $ancestors = get_ancestors($term->term_id, $taxonomy);
     2854            foreach ( (array)$ancestors as $ancestor ) {
     2855                $ancestor_term = get_term($ancestor, $taxonomy);
     2856                $hierarchical_slugs[] = $ancestor_term->slug;
     2857            }
     2858            $hierarchical_slugs = array_reverse($hierarchical_slugs);
     2859            $hierarchical_slugs[] = $slug;
     2860            $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
     2861        } else {
     2862            $termlink = str_replace("%$taxonomy%", $slug, $termlink);
     2863        }
    25142864        $termlink = home_url( user_trailingslashit($termlink, 'category') );
    25152865    }
     2866    // Back Compat filters.
     2867    if ( 'post_tag' == $taxonomy )
     2868        $termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
     2869    elseif ( 'category' == $taxonomy )
     2870        $termlink = apply_filters( 'category_link', $termlink, $term->term_id );
     2871
    25162872    return apply_filters('term_link', $termlink, $term, $taxonomy);
    25172873}
     
    25292885 * 'sep' : default is empty string. Separate every taxonomy with value in this.
    25302886 * 'after' : default is empty string. Display this after the taxonomies list.
     2887 * 'template' : The template to use for displaying the taxonomy terms.
    25312888 *
    25322889 * @since 2.5.0
     
    25412898        'sep' => ' ',
    25422899        'after' => '',
     2900        'template' => '%s: %l.'
    25432901    );
    25442902
     
    25462904    extract( $r, EXTR_SKIP );
    25472905
    2548     echo $before . join($sep, get_the_taxonomies($post)) . $after;
     2906    echo $before . join($sep, get_the_taxonomies($post, $r)) . $after;
    25492907}
    25502908
     
    25582916 *
    25592917 * @param int $post Optional. Post ID or will use Global Post ID (in loop).
     2918 * @param array $args Override the defaults.
    25602919 * @return array
    25612920 */
    2562 function get_the_taxonomies($post = 0) {
     2921function get_the_taxonomies($post = 0, $args = array() ) {
    25632922    if ( is_int($post) )
    25642923        $post =& get_post($post);
     
    25662925        $post =& $GLOBALS['post'];
    25672926
     2927    $args = wp_parse_args( $args, array(
     2928        'template' => '%s: %l.',
     2929    ) );
     2930    extract( $args, EXTR_SKIP );
     2931
    25682932    $taxonomies = array();
    25692933
    25702934    if ( !$post )
    25712935        return $taxonomies;
    2572 
    2573     $template = apply_filters('taxonomy_template', '%s: %l.');
    25742936
    25752937    foreach ( get_object_taxonomies($post) as $taxonomy ) {
     
    25892951
    25902952        foreach ( $terms as $term )
    2591             $links[] = "<a href='" . esc_attr(get_term_link($term, $taxonomy)) . "'>$term->name</a>";
     2953            $links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>";
    25922954
    25932955        if ( $links )
     
    26232985 * @uses wp_get_object_terms()
    26242986 *
    2625  * @param int $object_id. ID of the object (post ID, link ID, ...)
    2626  * @param string $taxonomy. Single taxonomy name
     2987 * @param int $object_id ID of the object (post ID, link ID, ...)
     2988 * @param string $taxonomy Single taxonomy name
    26272989 * @param int|string|array $terms Optional.  Term term_id, name, slug or array of said
    26282990 * @return bool|WP_Error. WP_Error on input error.
     
    26693031 *
    26703032 * @param string $object_type Object type string
    2671  * @param string $taxonomy. Single taxonomy name
     3033 * @param string $taxonomy Single taxonomy name
    26723034 * @return bool True if object is associated with the taxonomy, otherwise false.
    26733035 */
     
    26833045    return false;
    26843046}
     3047
     3048/**
     3049 * Get an array of ancestor IDs for a given object.
     3050 *
     3051 * @param int $object_id The ID of the object
     3052 * @param string $object_type The type of object for which we'll be retrieving ancestors.
     3053 * @return array of ancestors from lowest to highest in the hierarchy.
     3054 */
     3055function get_ancestors($object_id = 0, $object_type = '') {
     3056    $object_id = (int) $object_id;
     3057
     3058    $ancestors = array();
     3059
     3060    if ( empty( $object_id ) ) {
     3061        return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
     3062    }
     3063
     3064    if ( is_taxonomy_hierarchical( $object_type ) ) {
     3065        $term = get_term($object_id, $object_type);
     3066        while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
     3067            $ancestors[] = (int) $term->parent;
     3068            $term = get_term($term->parent, $object_type);
     3069        }
     3070    } elseif ( null !== get_post_type_object( $object_type ) ) {
     3071        $object = get_post($object_id);
     3072        if ( ! is_wp_error( $object ) && isset( $object->ancestors ) && is_array( $object->ancestors ) )
     3073            $ancestors = $object->ancestors;
     3074        else {
     3075            while ( ! is_wp_error($object) && ! empty( $object->post_parent ) && ! in_array( $object->post_parent, $ancestors ) ) {
     3076                $ancestors[] = (int) $object->post_parent;
     3077                $object = get_post($object->post_parent);
     3078            }
     3079        }
     3080    }
     3081
     3082    return apply_filters('get_ancestors', $ancestors, $object_id, $object_type);
     3083}
     3084
     3085/**
     3086 * Returns the term's parent's term_ID
     3087 *
     3088 * @since 3.1.0
     3089 *
     3090 * @param int $term_id
     3091 * @param string $taxonomy
     3092 *
     3093 * @return int|bool false on error
     3094 */
     3095function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {
     3096    $term = get_term( $term_id, $taxonomy );
     3097    if ( !$term || is_wp_error( $term ) )
     3098        return false;
     3099    return (int) $term->parent;
     3100}
     3101
     3102/**
     3103 * Checks the given subset of the term hierarchy for hierarchy loops.
     3104 * Prevents loops from forming and breaks those that it finds.
     3105 *
     3106 * Attached to the wp_update_term_parent filter.
     3107 *
     3108 * @since 3.1.0
     3109 * @uses wp_find_hierarchy_loop()
     3110 *
     3111 * @param int $parent term_id of the parent for the term we're checking.
     3112 * @param int $term_id The term we're checking.
     3113 * @param string $taxonomy The taxonomy of the term we're checking.
     3114 *
     3115 * @return int The new parent for the term.
     3116 */
     3117function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
     3118    // Nothing fancy here - bail
     3119    if ( !$parent )
     3120        return 0;
     3121
     3122    // Can't be its own parent
     3123    if ( $parent == $term_id )
     3124        return 0;
     3125
     3126    // Now look for larger loops
     3127
     3128    if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )
     3129        return $parent; // No loop
     3130
     3131    // Setting $parent to the given value causes a loop
     3132    if ( isset( $loop[$term_id] ) )
     3133        return 0;
     3134
     3135    // There's a loop, but it doesn't contain $term_id.  Break the loop.
     3136    foreach ( array_keys( $loop ) as $loop_member )
     3137        wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
     3138
     3139    return $parent;
     3140}
Note: See TracChangeset for help on using the changeset viewer.