*/
function add_rewrite_tag($tagname, $regex) {
//validation
- if ( strlen($tagname) < 3 || $tagname{0} != '%' || $tagname{strlen($tagname)-1} != '%' )
+ if ( strlen($tagname) < 3 || $tagname[0] != '%' || $tagname[strlen($tagname)-1] != '%' )
return;
$qv = trim($tagname, '%');
* The endpoints are added to the end of the request. So a request matching
* "/2008/10/14/my_post/myep/", the endpoint will be "/myep/".
*
- * Be sure to flush the rewrite rules (wp_rewrite->flush()) when your plugin gets
+ * Be sure to flush the rewrite rules (wp_rewrite->flush_rules()) when your plugin gets
* activated (register_activation_hook()) and deactivated (register_deactivation_hook())
*
* @since 2.1.0
if ( empty($rewrite) )
return 0;
- // $url cleanup by Mark Jaquith
- // This fixes things like #anchors, ?query=strings, missing 'www.',
- // added 'www.', or added 'index.php/' that will mess up our WP_Query
- // and return a false negative
-
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
} else {
// Chop off /path/to/blog
$home_path = parse_url(home_url());
- $home_path = $home_path['path'];
+ $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
$url = str_replace($home_path, '', $url);
}
$request = $url;
- // Done with cleanup
-
// Look for matches.
$request_match = $request;
foreach ( (array)$rewrite as $match => $query) {
+
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
$request_match = $url . '/' . $request;
if ( preg_match("!^$match!", $request_match, $matches) ) {
+
+ if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
+ // this is a verbose page match, lets check to be sure about it
+ if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
+ continue;
+ }
+
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\?!", '', $query);
// Do the query
$query = new WP_Query($query);
- if ( $query->is_single || $query->is_page )
+ if ( !empty($query->posts) && $query->is_singular )
return $query->post->ID;
else
return 0;
*/
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 ).
*
*/
var $comments_base = 'comments';
+ /**
+ * Pagination permalink base.
+ *
+ * @since 3.1.0
+ * @access private
+ * @var string
+ */
+ var $pagination_base = 'page';
+
/**
* Feed permalink base.
*
/**
* Endpoints permalinks
*
- * @since unknown
+ * @since 2.1.0
* @access private
* @var array
*/
'%second%',
'%postname%',
'%post_id%',
- '%category%',
- '%tag%',
'%author%',
'%pagename%',
'%search%'
'([0-9]{1,2})',
'([^/]+)',
'([0-9]+)',
- '(.+?)',
- '(.+?)',
'([^/]+)',
'([^/]+?)',
'(.+)'
'second=',
'name=',
'p=',
- 'category_name=',
- 'tag=',
'author_name=',
'pagename=',
's='
global $wpdb;
//get pages in order of hierarchy, i.e. children after parents
- $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
+ $posts = get_page_hierarchy( $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'") );
// If we have no pages get out quick
if ( !$posts )
$rewrite_rules = array();
$page_structure = $this->get_page_permastruct();
- if ( ! $this->use_verbose_page_rules ) {
- $this->add_rewrite_tag('%pagename%', "(.+?)", 'pagename=');
- $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
- return $rewrite_rules;
- }
-
- $page_uris = $this->page_uri_index();
- $uris = $page_uris[0];
- $attachment_uris = $page_uris[1];
-
- if ( is_array( $attachment_uris ) ) {
- foreach ( $attachment_uris as $uri => $pagename ) {
- $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
- $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
- }
- }
- if ( is_array( $uris ) ) {
- foreach ( $uris as $uri => $pagename ) {
- $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
- $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
- }
- }
-
+ // the extra .? at the beginning prevents clashes with other regular expressions in the rules array
+ $this->add_rewrite_tag('%pagename%', "(.?.+?)", 'pagename=');
+ $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
return $rewrite_rules;
}
* @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');
}
/**
* @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');
}
/**
* Retrieve extra permalink structure by name.
*
- * @since unknown
+ * @since 2.5.0
* @access public
*
* @param string $name Permalink structure name.
//build a regex to match the trackback and page/xx parts of URLs
$trackbackregex = 'trackback/?$';
- $pageregex = 'page/?([0-9]{1,})/?$';
+ $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
$commentregex = 'comment-page-([0-9]{1,})/?$';
//build up an array of endpoint regexes to append => queries to append
$num_toks = preg_match_all('/%.+?%/', $struct, $toks);
//get the 'tagname=$matches[i]'
- $query = ( isset($queries) && is_array($queries) ) ? $queries[$num_toks - 1] : '';
+ $query = ( isset($queries) && is_array($queries) && !empty($num_toks) ) ? $queries[$num_toks - 1] : '';
//set up $ep_mask_specific which is used to match more specific URL types
switch ( $dirs[$j] ) {
$home_path = parse_url( home_url() );
$robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
- // Default Feed rules - These are require to allow for the direct access files to work with permalink structure starting with %category%
- $default_feeds = array( '.*wp-atom.php$' => $this->index . '?feed=atom',
- '.*wp-rdf.php$' => $this->index . '?feed=rdf',
- '.*wp-rss.php$' => $this->index . '?feed=rss',
- '.*wp-rss2.php$' => $this->index . '?feed=rss2',
- '.*wp-feed.php$' => $this->index . '?feed=feed',
- '.*wp-commentsrss2.php$' => $this->index . '?feed=rss2&withcomments=1');
+ // Old feed files
+ $old_feed_files = array( '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old' );
// Registration rules
$registration_pages = array();
$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);
$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, $old_feed_files, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $page_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, $old_feed_files, $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);
</rule>
<rule name="wordpress - Rule 5" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)" ignoreCase="false" />
- <action type="Rewrite" url="{R:2}" />
+ <action type="Rewrite" url="{R:1}" />
</rule>
<rule name="wordpress - Rule 6" stopProcessing="true">
<match url="^([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
* These are added along with the extra rewrite rules that are merged to the
* top.
*
- * @since unknown
+ * @since 2.5.0
* @access public
*
* @param string $name Name for permalink structure.
function add_permastruct($name, $struct, $with_front = true, $ep_mask = EP_NONE) {
if ( $with_front )
$struct = $this->front . $struct;
+ else
+ $struct = $this->root . $struct;
$this->extra_permastructs[$name] = array($struct, $ep_mask);
}
*
* @since 2.0.1
* @access public
- * @param $hard bool Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
+ * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
*/
function flush_rules($hard = true) {
delete_option('rewrite_rules');
$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);
* @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();
}
* @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();
}
}
/**
- * PHP4 Constructor - Calls init(), which runs setup.
+ * Constructor - Calls init(), which runs setup.
*
* @since 1.5.0
* @access public
*
* @return WP_Rewrite
*/
- function WP_Rewrite() {
+ function __construct() {
$this->init();
}
}