From e05c96316f073f4605151ac8802161ab39f1c8b0 Mon Sep 17 00:00:00 2001 From: duck_ Date: Wed, 25 Jan 2012 22:48:24 +0000 Subject: [PATCH] Add the $query parameter to add_rewrite_tag() so that it matches WP_Rewrite::add_rewrite_tag(). Fixes #19871. Also rename some other parameters so that they all match. git-svn-id: http://svn.automattic.com/wordpress/trunk@19756 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rewrite.php | 58 +++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/wp-includes/rewrite.php b/wp-includes/rewrite.php index e6c6c159a..7d2abad7b 100644 --- a/wp-includes/rewrite.php +++ b/wp-includes/rewrite.php @@ -22,26 +22,33 @@ function add_rewrite_rule($regex, $redirect, $after = 'bottom') { } /** - * Add a new tag (like %postname%). + * Add a new rewrite tag (like %postname%). * - * Warning: you must call this on init or earlier, otherwise the query var - * addition stuff won't work. + * The $query parameter is optional. If it is omitted you must ensure that + * you call this on, or before, the 'init' hook. This is because $query defaults + * to "$tag=", and for this to work a new query var has to be added. * + * @see WP_Rewrite::add_rewrite_tag() * @since 2.1.0 * - * @param string $tagname - * @param string $regex + * @param string $tag Name of the new rewrite tag. + * @param string $regex Regular expression to substitute the tag for in rewrite rules. + * @param string $query String to append to the rewritten query. Must end in '='. Optional. */ -function add_rewrite_tag($tagname, $regex) { - //validation - if ( strlen($tagname) < 3 || $tagname[0] != '%' || $tagname[strlen($tagname)-1] != '%' ) +function add_rewrite_tag( $tag, $regex, $query = '' ) { + // validate the tag's name + if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' ) return; - $qv = trim($tagname, '%'); - global $wp_rewrite, $wp; - $wp->add_query_var($qv); - $wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '='); + + if ( empty( $query ) ) { + $qv = trim( $tag, '%' ); + $wp->add_query_var( $qv ); + $query = $qv . '='; + } + + $wp_rewrite->add_rewrite_tag( $tag, $regex, $query ); } /** @@ -1169,30 +1176,29 @@ class WP_Rewrite { } /** - * Append or update tag, pattern, and query for replacement. + * Add or update existing rewrite tags (e.g. %postname%). * * If the tag already exists, replace the existing pattern and query for - * that tag, otherwise add the new tag, pattern, and query to the end of the - * arrays. - * - * @internal What is the purpose of this function again? Need to finish long - * description. + * that tag, otherwise add the new tag. * + * @see WP_Rewrite::$rewritecode + * @see WP_Rewrite::$rewritereplace + * @see WP_Rewrite::$queryreplace * @since 1.5.0 * @access public * - * @param string $tag Append tag to rewritecode property array. - * @param string $pattern Append pattern to rewritereplace property array. - * @param string $query Append query to queryreplace property array. + * @param string $tag Name of the rewrite tag to add or update. + * @param string $regex Regular expression to substitute the tag for in rewrite rules. + * @param string $query String to append to the rewritten query. Must end in '='. */ - function add_rewrite_tag($tag, $pattern, $query) { - $position = array_search($tag, $this->rewritecode); + function add_rewrite_tag( $tag, $regex, $query ) { + $position = array_search( $tag, $this->rewritecode ); if ( false !== $position && null !== $position ) { - $this->rewritereplace[$position] = $pattern; - $this->queryreplace[$position] = $query; + $this->rewritereplace[ $position ] = $regex; + $this->queryreplace[ $position ] = $query; } else { $this->rewritecode[] = $tag; - $this->rewritereplace[] = $pattern; + $this->rewritereplace[] = $regex; $this->queryreplace[] = $query; } }