Rarst asks: what magic turns pretty permalinks into query variables?
The setup:
- index.php
- wp-blog-header.php: wp();
- wp-includes/functions.php: function wp
- wp-includes/class-wp.php: function main
- wp-includes/class-wp.php: function parse_request
- $rewrite = $wp_rewrite->wp_rewrite_rules() (add bbPress)
The magic:
foreach ( $rewrite as $match => $query ) {
if ( preg_match("#^$match#", $request_match, $matches) ) {
// Got a match.
$this->matched_rule = $match;
break;
}
}
The real voodoo is in creating the rewrite rules. Example: bbPress
- register_post_types
- register_taxonomies
- add_rewrite_tags
- generate_rewrite_rules
Exercise: optimize parse_request by restructuring the rules into a tree.
Nacin suggests: wp-hackers Skip Main Query
- the grand scheme of things (png, blog post)
- $wp->init()
- class freshlypressed_wp extends wp
- wp() calls $wp->main()
- $wp->main() calls $this->parse_request()
- $this is a freshlypressed_wp
Problems:
- Can’t extend a variable class (class my_wp extends $wp_class)
- No pluggable inheritance chaining
- No way for several plugins to cooperatively extend a class