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
This commit is contained in:
Dion Hulse 2010-10-17 05:41:22 +00:00
parent a47b78e95b
commit d7b230ca26
5 changed files with 47 additions and 145 deletions

View File

@ -10,32 +10,13 @@
* Retrieve category link URL. * Retrieve category link URL.
* *
* @since 1.0.0 * @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. * @param int $category_id Category ID.
* @return string * @return string
*/ */
function get_category_link( $category_id ) { function get_category_link( $category_id ) {
global $wp_rewrite; return get_term_link((int)$category_id, 'category');
$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 );
} }
/** /**
@ -770,28 +751,13 @@ function walk_category_dropdown_tree() {
* Retrieve the link to the tag. * Retrieve the link to the tag.
* *
* @since 2.3.0 * @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. * @param int $tag_id Tag (term) ID.
* @return string * @return string
*/ */
function get_tag_link( $tag_id ) { function get_tag_link( $tag_id ) {
global $wp_rewrite; return get_term_link( (int)$tag_id, 'post_tag');
$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 );
} }
/** /**

View File

@ -384,7 +384,7 @@ class WP {
foreach( (array) $headers as $name => $field_value ) foreach( (array) $headers as $name => $field_value )
@header("{$name}: {$field_value}"); @header("{$name}: {$field_value}");
if ($exit_required) if ( $exit_required )
exit(); exit();
do_action_ref_array('send_headers', array(&$this)); do_action_ref_array('send_headers', array(&$this));
@ -1903,4 +1903,4 @@ class WP_MatchesMapRegex {
} }
?> ?>

View File

@ -1457,13 +1457,15 @@ class WP_Query extends WP_Object_Query {
'operator' => 'IN' '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]; $term = $q[$t->query_var];
if ( $t->rewrite['hierarchical'] ) { if ( strpos($term, '+') !== false ) {
$tax_query[] = array_merge( $tax_query_defaults, array(
'terms' => array( basename( $term ) )
) );
} elseif ( strpos($term, '+') !== false ) {
$terms = preg_split( '/[+\s]+/', $term ); $terms = preg_split( '/[+\s]+/', $term );
foreach ( $terms as $term ) { foreach ( $terms as $term ) {
$tax_query[] = array_merge( $tax_query_defaults, array( $tax_query[] = array_merge( $tax_query_defaults, array(

View File

@ -380,42 +380,6 @@ class WP_Rewrite {
*/ */
var $use_trailing_slashes; 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 ). * Permalink author request base ( example.com/author/authorname ).
* *
@ -681,8 +645,6 @@ class WP_Rewrite {
'([0-9]{1,2})', '([0-9]{1,2})',
'([^/]+)', '([^/]+)',
'([0-9]+)', '([0-9]+)',
'(.+?)',
'(.+?)',
'([^/]+)', '([^/]+)',
'([^/]+?)', '([^/]+?)',
'(.+)' '(.+)'
@ -705,8 +667,6 @@ class WP_Rewrite {
'second=', 'second=',
'name=', 'name=',
'p=', 'p=',
'category_name=',
'tag=',
'author_name=', 'author_name=',
'pagename=', 'pagename=',
's=' 's='
@ -1028,22 +988,7 @@ class WP_Rewrite {
* @return bool|string False on failure. Category permalink structure. * @return bool|string False on failure. Category permalink structure.
*/ */
function get_category_permastruct() { function get_category_permastruct() {
if ( isset($this->category_structure) ) return $this->get_extra_permastruct('category');
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;
} }
/** /**
@ -1060,22 +1005,7 @@ class WP_Rewrite {
* @return bool|string False on failure. Tag permalink structure. * @return bool|string False on failure. Tag permalink structure.
*/ */
function get_tag_permastruct() { function get_tag_permastruct() {
if ( isset($this->tag_structure) ) return $this->get_extra_permastruct('post_tag');
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;
} }
/** /**
@ -1613,14 +1543,6 @@ class WP_Rewrite {
$search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH); $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
$search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite); $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 // Authors
$author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS); $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
$author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite); $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); $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
// Extra permastructs // Extra permastructs
foreach ( $this->extra_permastructs as $permastruct ) { foreach ( $this->extra_permastructs as $permastructname => $permastruct ) {
if ( is_array($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 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. // Put them together.
if ( $this->use_verbose_page_rules ) 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 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)); do_action_ref_array('generate_rewrite_rules', array(&$this));
$this->rules = apply_filters('rewrite_rules_array', $this->rules); $this->rules = apply_filters('rewrite_rules_array', $this->rules);
@ -1978,9 +1906,6 @@ class WP_Rewrite {
$this->root = ''; $this->root = '';
if ( $this->using_index_permalinks() ) if ( $this->using_index_permalinks() )
$this->root = $this->index . '/'; $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->author_structure);
unset($this->date_structure); unset($this->date_structure);
unset($this->page_structure); unset($this->page_structure);
@ -2032,7 +1957,7 @@ class WP_Rewrite {
* @param string $category_base Category permalink structure base. * @param string $category_base Category permalink structure base.
*/ */
function set_category_base($category_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); update_option('category_base', $category_base);
$this->init(); $this->init();
} }
@ -2051,7 +1976,7 @@ class WP_Rewrite {
* @param string $tag_base Tag permalink structure base. * @param string $tag_base Tag permalink structure base.
*/ */
function set_tag_base( $tag_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 ); update_option( 'tag_base', $tag_base );
$this->init(); $this->init();
} }

View File

@ -19,7 +19,10 @@ function create_initial_taxonomies() {
'hierarchical' => true, 'hierarchical' => true,
'update_count_callback' => '_update_post_term_count', 'update_count_callback' => '_update_post_term_count',
'query_var' => 'category_name', '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, 'public' => true,
'show_ui' => true, 'show_ui' => true,
'_builtin' => true, '_builtin' => true,
@ -29,7 +32,9 @@ function create_initial_taxonomies() {
'hierarchical' => false, 'hierarchical' => false,
'update_count_callback' => '_update_post_term_count', 'update_count_callback' => '_update_post_term_count',
'query_var' => 'tag', 'query_var' => 'tag',
'rewrite' => false, 'rewrite' => array(
'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag' ,
'with_front' => false),
'public' => true, 'public' => true,
'show_ui' => true, 'show_ui' => true,
'_builtin' => true, '_builtin' => true,
@ -313,7 +318,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
)); ));
if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] ) if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )
$tag = '(.*?)'; $tag = '(.+?)';
else else
$tag = '([^/]+)'; $tag = '([^/]+)';
@ -349,7 +354,7 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
$wp_taxonomies[$taxonomy] = (object) $args; $wp_taxonomies[$taxonomy] = (object) $args;
// register callback handling for metabox // 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 * @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 object|int|string $term
* @param string $taxonomy (optional if $term is object) * @param string $taxonomy (optional if $term is object)
* @return string HTML link to taxonomy term archive * @return string HTML link to taxonomy term archive
@ -2604,12 +2613,6 @@ function get_term_link( $term, $taxonomy = '') {
$taxonomy = $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); $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
$slug = $term->slug; $slug = $term->slug;
@ -2637,6 +2640,12 @@ function get_term_link( $term, $taxonomy = '') {
} }
$termlink = home_url( user_trailingslashit($termlink, 'category') ); $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); return apply_filters('term_link', $termlink, $term, $taxonomy);
} }