From d7b230ca262ec6c695c0aa620ff3379de758301d Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Sun, 17 Oct 2010 05:41:22 +0000 Subject: [PATCH] Merge Category/Tag URL creation/rewriting into general Taxonomy system. Removes the legacy handling for these url's. See #12659 git-svn-id: https://develop.svn.wordpress.org/trunk@15825 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/category-template.php | 42 ++---------- wp-includes/classes.php | 4 +- wp-includes/query.php | 12 ++-- wp-includes/rewrite.php | 105 +++++------------------------- wp-includes/taxonomy.php | 29 ++++++--- 5 files changed, 47 insertions(+), 145 deletions(-) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index 3d2495e38b..608c4d281b 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -10,32 +10,13 @@ * Retrieve category link URL. * * @since 1.0.0 - * @uses apply_filters() Calls 'category_link' filter on category link and category ID. + * @see get_term_link() * * @param int $category_id Category ID. * @return string */ function get_category_link( $category_id ) { - global $wp_rewrite; - $catlink = $wp_rewrite->get_category_permastruct(); - - if ( empty( $catlink ) ) { - $catlink = home_url('?cat=' . $category_id); - } else { - $category = &get_category( $category_id ); - if ( is_wp_error( $category ) ) - return $category; - $category_nicename = $category->slug; - - if ( $category->parent == $category_id ) // recursive recursion - $category->parent = 0; - elseif ($category->parent != 0 ) - $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename; - - $catlink = str_replace( '%category%', $category_nicename, $catlink ); - $catlink = home_url( user_trailingslashit( $catlink, 'category' ) ); - } - return apply_filters( 'category_link', $catlink, $category_id ); + return get_term_link((int)$category_id, 'category'); } /** @@ -770,28 +751,13 @@ function walk_category_dropdown_tree() { * Retrieve the link to the tag. * * @since 2.3.0 - * @uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters. + * @see get_term_link() * * @param int $tag_id Tag (term) ID. * @return string */ function get_tag_link( $tag_id ) { - global $wp_rewrite; - $taglink = $wp_rewrite->get_tag_permastruct(); - - $tag = &get_term( $tag_id, 'post_tag' ); - if ( is_wp_error( $tag ) ) - return $tag; - $slug = $tag->slug; - - if ( empty( $taglink ) ) { - $file = get_option( 'home' ) . '/'; - $taglink = $file . '?tag=' . $slug; - } else { - $taglink = str_replace( '%tag%', $slug, $taglink ); - $taglink = get_option( 'home' ) . user_trailingslashit( $taglink, 'category' ); - } - return apply_filters( 'tag_link', $taglink, $tag_id ); + return get_term_link( (int)$tag_id, 'post_tag'); } /** diff --git a/wp-includes/classes.php b/wp-includes/classes.php index a2790bd22d..da54a1c64c 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -384,7 +384,7 @@ class WP { foreach( (array) $headers as $name => $field_value ) @header("{$name}: {$field_value}"); - if ($exit_required) + if ( $exit_required ) exit(); do_action_ref_array('send_headers', array(&$this)); @@ -1903,4 +1903,4 @@ class WP_MatchesMapRegex { } -?> +?> \ No newline at end of file diff --git a/wp-includes/query.php b/wp-includes/query.php index 2df7afb5dc..f4bc110f31 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -1457,13 +1457,15 @@ class WP_Query extends WP_Object_Query { 'operator' => 'IN' ); + if ( $t->rewrite['hierarchical'] ) { + $q[$t->query_var] = basename($q[$t->query_var]); + if ( $taxonomy == $q['taxonomy'] ) + $q['term'] = basename($q['term']); + } + $term = $q[$t->query_var]; - if ( $t->rewrite['hierarchical'] ) { - $tax_query[] = array_merge( $tax_query_defaults, array( - 'terms' => array( basename( $term ) ) - ) ); - } elseif ( strpos($term, '+') !== false ) { + if ( strpos($term, '+') !== false ) { $terms = preg_split( '/[+\s]+/', $term ); foreach ( $terms as $term ) { $tax_query[] = array_merge( $tax_query_defaults, array( diff --git a/wp-includes/rewrite.php b/wp-includes/rewrite.php index 26e607d140..3feeb37371 100644 --- a/wp-includes/rewrite.php +++ b/wp-includes/rewrite.php @@ -380,42 +380,6 @@ class WP_Rewrite { */ var $use_trailing_slashes; - /** - * Customized or default category permalink base ( example.com/xx/tagname ). - * - * @since 1.5.0 - * @access private - * @var string - */ - var $category_base; - - /** - * Customized or default tag permalink base ( example.com/xx/tagname ). - * - * @since 2.3.0 - * @access private - * @var string - */ - var $tag_base; - - /** - * Permalink request structure for categories. - * - * @since 1.5.0 - * @access private - * @var string - */ - var $category_structure; - - /** - * Permalink request structure for tags. - * - * @since 2.3.0 - * @access private - * @var string - */ - var $tag_structure; - /** * Permalink author request base ( example.com/author/authorname ). * @@ -681,8 +645,6 @@ class WP_Rewrite { '([0-9]{1,2})', '([^/]+)', '([0-9]+)', - '(.+?)', - '(.+?)', '([^/]+)', '([^/]+?)', '(.+)' @@ -705,8 +667,6 @@ class WP_Rewrite { 'second=', 'name=', 'p=', - 'category_name=', - 'tag=', 'author_name=', 'pagename=', 's=' @@ -1028,22 +988,7 @@ class WP_Rewrite { * @return bool|string False on failure. Category permalink structure. */ function get_category_permastruct() { - if ( isset($this->category_structure) ) - return $this->category_structure; - - if ( empty($this->permalink_structure) ) { - $this->category_structure = ''; - return false; - } - - if ( empty($this->category_base) ) - $this->category_structure = trailingslashit( $this->front . 'category' ); - else - $this->category_structure = trailingslashit( '/' . $this->root . $this->category_base ); - - $this->category_structure .= '%category%'; - - return $this->category_structure; + return $this->get_extra_permastruct('category'); } /** @@ -1060,22 +1005,7 @@ class WP_Rewrite { * @return bool|string False on failure. Tag permalink structure. */ function get_tag_permastruct() { - if ( isset($this->tag_structure) ) - return $this->tag_structure; - - if ( empty($this->permalink_structure) ) { - $this->tag_structure = ''; - return false; - } - - if ( empty($this->tag_base) ) - $this->tag_structure = trailingslashit( $this->front . 'tag' ); - else - $this->tag_structure = trailingslashit( '/' . $this->root . $this->tag_base ); - - $this->tag_structure .= '%tag%'; - - return $this->tag_structure; + return $this->get_extra_permastruct('post_tag'); } /** @@ -1613,14 +1543,6 @@ class WP_Rewrite { $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH); $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); - // Categories - $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct(), EP_CATEGORIES); - $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite); - - // Tags - $tag_rewrite = $this->generate_rewrite_rules($this->get_tag_permastruct(), EP_TAGS); - $tag_rewrite = apply_filters('tag_rewrite_rules', $tag_rewrite); - // Authors $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS); $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite); @@ -1630,18 +1552,24 @@ class WP_Rewrite { $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite); // Extra permastructs - foreach ( $this->extra_permastructs as $permastruct ) { + foreach ( $this->extra_permastructs as $permastructname => $permastruct ) { if ( is_array($permastruct) ) - $this->extra_rules_top = array_merge($this->extra_rules_top, $this->generate_rewrite_rules($permastruct[0], $permastruct[1])); + $rules = $this->generate_rewrite_rules($permastruct[0], $permastruct[1]); else - $this->extra_rules_top = array_merge($this->extra_rules_top, $this->generate_rewrite_rules($permastruct, EP_NONE)); + $rules = $this->generate_rewrite_rules($permastruct, EP_NONE); + + $rules = apply_filters($permastructname . '_rewrite_rules', $rules); + if ( 'post_tag' == $permastructname ) + $rules = apply_filters('tag_rewrite_rules', $rules); + + $this->extra_rules_top = array_merge($this->extra_rules_top, $rules); } // Put them together. if ( $this->use_verbose_page_rules ) - $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules); + $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules); else - $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $tag_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules); + $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules); do_action_ref_array('generate_rewrite_rules', array(&$this)); $this->rules = apply_filters('rewrite_rules_array', $this->rules); @@ -1978,9 +1906,6 @@ class WP_Rewrite { $this->root = ''; if ( $this->using_index_permalinks() ) $this->root = $this->index . '/'; - $this->category_base = get_option( 'category_base' ); - $this->tag_base = get_option( 'tag_base' ); - unset($this->category_structure); unset($this->author_structure); unset($this->date_structure); unset($this->page_structure); @@ -2032,7 +1957,7 @@ class WP_Rewrite { * @param string $category_base Category permalink structure base. */ function set_category_base($category_base) { - if ( $category_base != $this->category_base ) { + if ( $category_base != get_option('category_base') ) { update_option('category_base', $category_base); $this->init(); } @@ -2051,7 +1976,7 @@ class WP_Rewrite { * @param string $tag_base Tag permalink structure base. */ function set_tag_base( $tag_base ) { - if ( $tag_base != $this->tag_base ) { + if ( $tag_base != get_option( 'tag_base') ) { update_option( 'tag_base', $tag_base ); $this->init(); } diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index ab82ac4523..edf23d57e9 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -19,7 +19,10 @@ function create_initial_taxonomies() { 'hierarchical' => true, 'update_count_callback' => '_update_post_term_count', 'query_var' => 'category_name', - 'rewrite' => false, + 'rewrite' => array( + 'hierarchical' => true, + 'slug' => get_option('category_base') ? get_option('category_base') : 'category', + 'with_front' => false), 'public' => true, 'show_ui' => true, '_builtin' => true, @@ -29,7 +32,9 @@ function create_initial_taxonomies() { 'hierarchical' => false, 'update_count_callback' => '_update_post_term_count', 'query_var' => 'tag', - 'rewrite' => false, + 'rewrite' => array( + 'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag' , + 'with_front' => false), 'public' => true, 'show_ui' => true, '_builtin' => true, @@ -313,7 +318,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { )); if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] ) - $tag = '(.*?)'; + $tag = '(.+?)'; else $tag = '([^/]+)'; @@ -349,7 +354,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { $wp_taxonomies[$taxonomy] = (object) $args; // register callback handling for metabox - add_filter('wp_ajax_add-'.$taxonomy, '_wp_ajax_add_hierarchical_term'); + add_filter('wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term'); } /** @@ -2581,6 +2586,10 @@ function _update_post_term_count( $terms, $taxonomy ) { * * @since 2.5.0 * + * @uses apply_filters() Calls 'term_link' with term link and term object, and taxonomy parameters. + * @uses apply_filters() For the post_tag Taxonomy, Calls 'tag_link' with tag link and tag ID as parameters. + * @uses apply_filters() For the category Taxonomy, Calls 'category_link' filter on category link and category ID. + * * @param object|int|string $term * @param string $taxonomy (optional if $term is object) * @return string HTML link to taxonomy term archive @@ -2604,12 +2613,6 @@ function get_term_link( $term, $taxonomy = '') { $taxonomy = $term->taxonomy; - // use legacy functions for core taxonomies until they are fully plugged in - if ( $taxonomy == 'category' ) - return get_category_link((int) $term->term_id); - if ( $taxonomy == 'post_tag' ) - return get_tag_link((int) $term->term_id); - $termlink = $wp_rewrite->get_extra_permastruct($taxonomy); $slug = $term->slug; @@ -2637,6 +2640,12 @@ function get_term_link( $term, $taxonomy = '') { } $termlink = home_url( user_trailingslashit($termlink, 'category') ); } + // Back Compat filters. + if ( 'post_tag' == $taxonomy ) + $termlink = apply_filters( 'tag_link', $termlink, $term->term_id ); + elseif ( 'category' == $taxonomy ) + $termlink = apply_filters( 'category_link', $termlink, $term->term_id ); + return apply_filters('term_link', $termlink, $term, $taxonomy); }