Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r19077 r20972  
    1313
    1414/**
    15  * Creates the initial taxonomies when 'init' action is fired.
     15 * Creates the initial taxonomies.
     16 *
     17 * This function fires twice: in wp-settings.php before plugins are loaded (for
     18 * backwards compatibility reasons), and again on the 'init' action. We must avoid
     19 * registering rewrite rules before the 'init' action.
    1620 */
    1721function create_initial_taxonomies() {
    1822        global $wp_rewrite;
     23
     24        if ( ! did_action( 'init' ) ) {
     25                $rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false );
     26        } else {
     27                $post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
     28                $rewrite = array(
     29                        'category' => array(
     30                                'hierarchical' => true,
     31                                'slug' => get_option('category_base') ? get_option('category_base') : 'category',
     32                                'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),
     33                                'ep_mask' => EP_CATEGORIES,
     34                        ),
     35                        'post_tag' => array(
     36                                'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
     37                                'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
     38                                'ep_mask' => EP_TAGS,
     39                        ),
     40                        'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
     41                );
     42        }
    1943
    2044        register_taxonomy( 'category', 'post', array(
    2145                'hierarchical' => true,
    2246                'query_var' => 'category_name',
    23                 'rewrite' => did_action( 'init' ) ? array(
    24                                         'hierarchical' => true,
    25                                         'slug' => get_option('category_base') ? get_option('category_base') : 'category',
    26                                         'with_front' => ( get_option('category_base') && ! $wp_rewrite->using_index_permalinks() ) ? false : true ) : false,
     47                'rewrite' => $rewrite['category'],
    2748                'public' => true,
    2849                'show_ui' => true,
    … …  
    3354                'hierarchical' => false,
    3455                'query_var' => 'tag',
    35                 'rewrite' => did_action( 'init' ) ? array(
    36                                         'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
    37                                         'with_front' => ( get_option('tag_base') && ! $wp_rewrite->using_index_permalinks() ) ? false : true ) : false,
     56                'rewrite' => $rewrite['post_tag'],
    3857                'public' => true,
    3958                'show_ui' => true,
    … …  
    7897        ) );
    7998
    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 
    8699        register_taxonomy( 'post_format', 'post', array(
    87100                'public' => true,
    … …  
    92105                ),
    93106                'query_var' => true,
    94                 'rewrite' => $rewrite,
     107                'rewrite' => $rewrite['post_format'],
    95108                'show_ui' => false,
    96109                '_builtin' => true,
    97                 'show_in_nav_menus' => false,
     110                'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
    98111        ) );
    99112}
    … …  
    122135        return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
    123136}
    124 
    125137
    126138/**
    … …  
    285297 * @since 2.3.0
    286298 * @uses $wp_taxonomies Inserts new taxonomy object into the list
    287  * @uses $wp_rewrite Adds rewrite tags and permastructs
    288299 * @uses $wp Adds query vars
    289300 *
    … …  
    293304 */
    294305function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
    295         global $wp_taxonomies, $wp_rewrite, $wp;
     306        global $wp_taxonomies, $wp;
    296307
    297308        if ( ! is_array($wp_taxonomies) )
    … …  
    319330        }
    320331
    321         if ( false !== $args['rewrite'] && '' != get_option('permalink_structure') ) {
     332        if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option('permalink_structure') ) ) {
    322333                $args['rewrite'] = wp_parse_args($args['rewrite'], array(
    323334                        'slug' => sanitize_title_with_dashes($taxonomy),
    324335                        'with_front' => true,
    325                         'hierarchical' => false
     336                        'hierarchical' => false,
     337                        'ep_mask' => EP_NONE,
    326338                ));
    327339
    … …  
    331343                        $tag = '([^/]+)';
    332344
    333                 $wp_rewrite->add_rewrite_tag("%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
    334                 $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']);
     345                add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=" );
     346                add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite'] );
    335347        }
    336348
    … …  
    380392 * - parent_item_colon - The same as <code>parent_item</code>, but with colon <code>:</code> in the end
    381393 * - edit_item - Default is Edit Tag/Edit Category
     394 * - view_item - Default is View Tag/View Category
    382395 * - update_item - Default is Update Tag/Update Category
    383396 * - add_new_item - Default is Add New Tag/Add New Category
    384397 * - new_item_name - Default is New Tag Name/New Category Name
    385  * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas," used in the meta box.
    386  * - add_or_remove_items - This string isn't used on hierarchical taxonomies. Default is "Add or remove tags," used in the meta box when JavaScript is disabled.
    387  * - choose_from_most_used - This string isn't used on hierarchical taxonomies. Default is "Choose from the most used tags," used in the meta box.
    388  *
    389  * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories.)
     398 * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas", used in the meta box.
     399 * - add_or_remove_items - This string isn't used on hierarchical taxonomies. Default is "Add or remove tags", used in the meta box when JavaScript is disabled.
     400 * - choose_from_most_used - This string isn't used on hierarchical taxonomies. Default is "Choose from the most used tags", used in the meta box.
     401 *
     402 * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories).
    390403 *
    391404 * @since 3.0.0
    … …  
    429442 *
    430443 * @param string $taxonomy Name of taxonomy object
    431  * @param array|string $object_type Name of the object type
     444 * @param string $object_type Name of the object type
    432445 * @return bool True if successful, false if not
    433446 */
    … …  
    490503        foreach ( (array) $taxonomies as $taxonomy ) {
    491504                if ( ! taxonomy_exists( $taxonomy ) )
    492                         return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
     505                        return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
    493506        }
    494507
    … …  
    843856
    844857        if ( ! taxonomy_exists($taxonomy) ) {
    845                 $error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     858                $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    846859                return $error;
    847860        }
    … …  
    972985function get_term_children( $term_id, $taxonomy ) {
    973986        if ( ! taxonomy_exists($taxonomy) )
    974                 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     987                return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    975988
    976989        $term_id = intval( $term_id );
    … …  
    10841097 * terms whose count is 0 according to the given taxonomy.
    10851098 *
    1086  * exclude - Default is an empty array.  An array, comma- or space-delimited string
    1087  * of term ids to exclude from the return array.  If 'include' is non-empty,
     1099 * exclude - Default is an empty array. An array, comma- or space-delimited string
     1100 * of term ids to exclude from the return array. If 'include' is non-empty,
    10881101 * 'exclude' is ignored.
    10891102 *
    1090  * exclude_tree - Default is an empty array.  An array, comma- or space-delimited
     1103 * exclude_tree - Default is an empty array. An array, comma- or space-delimited
    10911104 * string of term ids to exclude from the return array, along with all of their
    1092  * descendant terms according to the primary taxonomy.  If 'include' is non-empty,
     1105 * descendant terms according to the primary taxonomy. If 'include' is non-empty,
    10931106 * 'exclude_tree' is ignored.
    10941107 *
    1095  * include - Default is an empty array.  An array, comma- or space-delimited string
     1108 * include - Default is an empty array. An array, comma- or space-delimited string
    10961109 * of term ids to include in the return array.
    10971110 *
    1098  * number - The maximum number of terms to return.  Default is to return them all.
     1111 * number - The maximum number of terms to return. Default is to return them all.
    10991112 *
    11001113 * offset - The number by which to offset the terms query.
    … …  
    11101123 *
    11111124 * search - Returned terms' names will contain the value of 'search',
    1112  * case-insensitive.  Default is an empty string.
     1125 * case-insensitive. Default is an empty string.
    11131126 *
    11141127 * name__like - Returned terms' names will begin with the value of 'name__like',
    … …  
    11211134 * returns terms regardless of ancestry or whether the terms are empty.
    11221135 *
    1123  * The 'child_of' argument, when used, should be set to the integer of a term ID.  Its default
    1124  * is 0.  If set to a non-zero value, all returned terms will be descendants
    1125  * of that term according to the given taxonomy.  Hence 'child_of' is set to 0
     1136 * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default
     1137 * is 0. If set to a non-zero value, all returned terms will be descendants
     1138 * of that term according to the given taxonomy. Hence 'child_of' is set to 0
    11261139 * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies
    11271140 * make term ancestry ambiguous.
    11281141 *
    1129  * The 'parent' argument, when used, should be set to the integer of a term ID.  Its default is
     1142 * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is
    11301143 * the empty string '', which has a different meaning from the integer 0.
    11311144 * If set to an integer value, all returned terms will have as an immediate
    … …  
    11621175        foreach ( $taxonomies as $taxonomy ) {
    11631176                if ( ! taxonomy_exists($taxonomy) ) {
    1164                         $error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     1177                        $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    11651178                        return $error;
    11661179                }
    … …  
    14321445 * Check if Term exists.
    14331446 *
    1434  * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
    1435  *
    14361447 * Formerly is_term(), introduced in 2.3.0.
    14371448 *
    … …  
    14451456 * @param string $taxonomy The taxonomy name to use
    14461457 * @param int $parent ID of parent term under which to confine the exists search.
    1447  * @return mixed Get the term id or Term Object, if exists.
     1458 * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified
     1459 *      and the term ID exists. Returns an array of the term ID and the taxonomy if the pairing exists.
    14481460 */
    14491461function term_exists($term, $taxonomy = '', $parent = 0) {
    … …  
    14941506
    14951507        return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );
     1508}
     1509
     1510/**
     1511 * Check if a term is an ancestor of another term.
     1512 *
     1513 * You can use either an id or the term object for both parameters.
     1514 *
     1515 * @since 3.4.0
     1516 *
     1517 * @param int|object $term1 ID or object to check if this is the parent term.
     1518 * @param int|object $term2 The child term.
     1519 * @param string $taxonomy Taxonomy name that $term1 and $term2 belong to.
     1520 * @return bool Whether $term2 is child of $term1
     1521 */
     1522function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
     1523        if ( ! isset( $term1->term_id ) )
     1524                $term1 = get_term( $term1, $taxonomy );
     1525        if ( ! isset( $term2->parent ) )
     1526                $term2 = get_term( $term2, $taxonomy );
     1527
     1528        if ( empty( $term1->term_id ) || empty( $term2->parent ) )
     1529                return false;
     1530        if ( $term2->parent == $term1->term_id )
     1531                return true;
     1532
     1533        return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
    14961534}
    14971535
    … …  
    17661804
    17671805        do_action( 'delete_term_taxonomy', $tt_id );
    1768         $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
     1806        $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
    17691807        do_action( 'deleted_term_taxonomy', $tt_id );
    17701808
    17711809        // Delete the term if no taxonomies use it.
    17721810        if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
    1773                 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
     1811                $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
    17741812
    17751813        clean_term_cache($term, $taxonomy);
    … …  
    18251863 * @param string|array $taxonomies The taxonomies to retrieve terms from.
    18261864 * @param array|string $args Change what is returned
    1827  * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.
     1865 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.
    18281866 */
    18291867function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
    18301868        global $wpdb;
    18311869
     1870        if ( empty( $object_ids ) || empty( $taxonomies ) )
     1871                return array();
     1872
    18321873        if ( !is_array($taxonomies) )
    18331874                $taxonomies = array($taxonomies);
    … …  
    18351876        foreach ( (array) $taxonomies as $taxonomy ) {
    18361877                if ( ! taxonomy_exists($taxonomy) )
    1837                         return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     1878                        return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    18381879        }
    18391880
    … …  
    21062147
    21072148        if ( ! taxonomy_exists($taxonomy) )
    2108                 return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
     2149                return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    21092150
    21102151        if ( !is_array($terms) )
    … …  
    24742515//
    24752516
    2476 
    24772517/**
    24782518 * Removes the taxonomy relationship to terms from the cache.
    … …  
    25032543        do_action('clean_object_term_cache', $object_ids, $object_type);
    25042544}
    2505 
    25062545
    25072546/**
    … …  
    25652604}
    25662605
    2567 
    25682606/**
    25692607 * Retrieves the taxonomy relationship to the term object id.
    … …  
    25832621        return $cache;
    25842622}
    2585 
    25862623
    25872624/**
    … …  
    26382675
    26392676        foreach ( $ids as $id ) {
    2640                 foreach ( $taxonomies  as $taxonomy ) {
     2677                foreach ( $taxonomies as $taxonomy ) {
    26412678                        if ( ! isset($object_terms[$id][$taxonomy]) ) {
    26422679                                if ( !isset($object_terms[$id]) )
    … …  
    26492686        foreach ( $object_terms as $id => $value ) {
    26502687                foreach ( $value as $taxonomy => $terms ) {
    2651                         wp_cache_set($id, $terms, "{$taxonomy}_relationships");
    2652                 }
    2653         }
    2654 }
    2655 
     2688                        wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
     2689                }
     2690        }
     2691}
    26562692
    26572693/**
    … …  
    26782714// Private
    26792715//
    2680 
    26812716
    26822717/**
    … …  
    27122747}
    27132748
    2714 
    27152749/**
    27162750 * Get the subset of $terms that are descendants of $term_id.
    … …  
    27682802        return $term_list;
    27692803}
    2770 
    27712804
    27722805/**
    … …  
    28782911                if ( $object_types )
    28792912                        $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
    2880 
    28812913
    28822914                do_action( 'edit_term_taxonomy', $term, $taxonomy );
    … …  
    30953127 * @param int $object_id ID of the object (post ID, link ID, ...)
    30963128 * @param string $taxonomy Single taxonomy name
    3097  * @param int|string|array $terms Optional.  Term term_id, name, slug or array of said
     3129 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said
    30983130 * @return bool|WP_Error. WP_Error on input error.
    30993131 */
    … …  
    32413273                return 0;
    32423274
    3243         // There's a loop, but it doesn't contain $term_id.  Break the loop.
     3275        // There's a loop, but it doesn't contain $term_id. Break the loop.
    32443276        foreach ( array_keys( $loop ) as $loop_member )
    32453277                wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
Note: See TracChangeset for help on using the changeset viewer.