2006-03-12 17:57:00 -05:00
< ? php
2008-09-01 01:47:08 -04:00
/**
* WordPress Rewrite API
*
* @ package WordPress
* @ subpackage Rewrite
*/
2006-03-12 17:57:00 -05:00
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Add a straight rewrite rule .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* @ see WP_Rewrite :: add_rule () for long description .
2008-09-01 01:47:08 -04:00
* @ since 2.1 . 0
*
2008-09-15 20:35:56 -04:00
* @ param string $regex Regular Expression to match request against .
* @ param string $redirect Page to redirect to .
* @ param string $after Optional , default is 'bottom' . Where to add rule , can also be 'top' .
2008-09-01 01:47:08 -04:00
*/
2007-06-27 17:39:50 -04:00
function add_rewrite_rule ( $regex , $redirect , $after = 'bottom' ) {
2006-03-12 17:57:00 -05:00
global $wp_rewrite ;
2007-08-28 15:55:11 -04:00
$wp_rewrite -> add_rule ( $regex , $redirect , $after );
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2012-01-25 17:48:24 -05:00
* Add a new rewrite tag ( like % postname % ) .
2008-09-01 01:47:08 -04:00
*
2012-01-25 17:48:24 -05:00
* 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 .
2008-09-01 01:47:08 -04:00
*
2012-01-25 17:48:24 -05:00
* @ see WP_Rewrite :: add_rewrite_tag ()
2008-09-01 01:47:08 -04:00
* @ since 2.1 . 0
*
2012-01-25 17:48:24 -05:00
* @ 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 .
2008-09-01 01:47:08 -04:00
*/
2012-01-25 17:48:24 -05:00
function add_rewrite_tag ( $tag , $regex , $query = '' ) {
// validate the tag's name
if ( strlen ( $tag ) < 3 || $tag [ 0 ] != '%' || $tag [ strlen ( $tag ) - 1 ] != '%' )
2006-03-12 17:57:00 -05:00
return ;
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
global $wp_rewrite , $wp ;
2012-01-25 17:48:24 -05:00
if ( empty ( $query ) ) {
$qv = trim ( $tag , '%' );
$wp -> add_query_var ( $qv );
$query = $qv . '=' ;
}
$wp_rewrite -> add_rewrite_tag ( $tag , $regex , $query );
2006-03-12 17:57:00 -05:00
}
2010-02-26 14:23:58 -05:00
/**
* Add permalink structure .
*
* @ see WP_Rewrite :: add_permastruct ()
* @ since 3.0 . 0
*
* @ param string $name Name for permalink structure .
* @ param string $struct Permalink structure .
2012-01-24 13:27:18 -05:00
* @ param array $args Optional configuration for building the rules from the permalink structure ,
* see { @ link WP_Rewrite :: add_permastruct ()} for full details .
2010-02-26 14:23:58 -05:00
*/
2012-01-24 13:27:18 -05:00
function add_permastruct ( $name , $struct , $args = array () ) {
2010-02-26 14:23:58 -05:00
global $wp_rewrite ;
2012-01-24 13:27:18 -05:00
// backwards compatibility for the old parameters: $with_front and $ep_mask
if ( ! is_array ( $args ) )
$args = array ( 'with_front' => $args );
if ( func_num_args () == 4 )
$args [ 'ep_mask' ] = func_get_arg ( 3 );
return $wp_rewrite -> add_permastruct ( $name , $struct , $args );
2010-02-26 14:23:58 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Add a new feed type like / atom1 /.
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*
2008-09-15 20:35:56 -04:00
* @ param string $feedname
* @ param callback $function Callback to run on feed display .
* @ return string Feed action name .
2008-09-01 01:47:08 -04:00
*/
2006-03-12 17:57:00 -05:00
function add_feed ( $feedname , $function ) {
global $wp_rewrite ;
2010-03-13 02:29:10 -05:00
if ( ! in_array ( $feedname , $wp_rewrite -> feeds ) ) //override the file if it is
2006-03-12 17:57:00 -05:00
$wp_rewrite -> feeds [] = $feedname ;
$hook = 'do_feed_' . $feedname ;
2008-06-23 20:29:04 -04:00
// Remove default function hook
2013-04-29 09:25:15 -04:00
remove_action ( $hook , $hook );
2006-03-12 17:57:00 -05:00
add_action ( $hook , $function , 10 , 1 );
return $hook ;
}
2010-02-26 14:23:58 -05:00
/**
* Remove rewrite rules and then recreate rewrite rules .
*
* @ see WP_Rewrite :: flush_rules ()
* @ since 3.0 . 0
*
* @ param bool $hard Whether to update . htaccess ( hard flush ) or just update
* rewrite_rules transient ( soft flush ) . Default is true ( hard ) .
*/
function flush_rewrite_rules ( $hard = true ) {
global $wp_rewrite ;
$wp_rewrite -> flush_rules ( $hard );
}
2010-03-13 02:29:10 -05:00
/**
* Endpoint Mask for default , which is nothing .
*
* @ since 2.1 . 0
*/
define ( 'EP_NONE' , 0 );
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for Permalink .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_PERMALINK' , 1 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for Attachment .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_ATTACHMENT' , 2 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for date .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_DATE' , 4 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for year
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_YEAR' , 8 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for month .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_MONTH' , 16 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for day .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_DAY' , 32 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for root .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_ROOT' , 64 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for comments .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_COMMENTS' , 128 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for searches .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_SEARCH' , 256 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for categories .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_CATEGORIES' , 512 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for tags .
2008-09-01 01:47:08 -04:00
*
* @ since 2.3 . 0
*/
define ( 'EP_TAGS' , 1024 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for authors .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_AUTHORS' , 2048 );
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for pages .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
define ( 'EP_PAGES' , 4096 );
2013-07-28 15:18:19 -04:00
/**
* Endpoint Mask for all archive views .
*
* @ since 3.7 . 0
*/
define ( 'EP_ALL_ARCHIVES' , EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS );
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Endpoint Mask for everything .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
*/
2013-07-28 15:18:19 -04:00
define ( 'EP_ALL' , EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES );
2006-03-12 17:57:00 -05:00
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Add an endpoint , like / trackback /.
2008-09-01 01:47:08 -04:00
*
2012-01-25 16:58:23 -05:00
* Adding an endpoint creates extra rewrite rules for each of the matching
* places specified by the provided bitmask . For example :
2008-09-01 01:47:08 -04:00
*
2014-11-24 01:15:24 -05:00
* add_rewrite_endpoint ( 'json' , EP_PERMALINK | EP_PAGES );
2012-01-25 16:58:23 -05:00
*
* will add a new rewrite rule ending with " json(/(.*))?/? $ " for every permastruct
* that describes a permalink ( post ) or page . This is rewritten to " json= $match "
* where $match is the part of the URL matched by the endpoint regex ( e . g . " foo " in
2014-11-24 01:15:24 -05:00
* " [permalink]/json/foo/ " ) .
2012-01-25 16:58:23 -05:00
*
* A new query var with the same name as the endpoint will also be created .
*
* When specifying $places ensure that you are using the EP_ * constants ( or a
* combination of them using the bitwise OR operator ) as their values are not
2014-11-24 01:15:24 -05:00
* guaranteed to remain static ( especially `EP_ALL` ) .
2012-01-25 16:58:23 -05:00
*
2014-11-24 01:15:24 -05:00
* Be sure to flush the rewrite rules - { @ see flush_rewrite_rules ()} - when your plugin gets
2012-01-25 16:58:23 -05:00
* activated and deactivated .
2009-04-27 17:11:01 -04:00
*
2008-09-01 01:47:08 -04:00
* @ since 2.1 . 0
2012-01-25 16:58:23 -05:00
* @ see WP_Rewrite :: add_endpoint ()
* @ global object $wp_rewrite
2008-09-01 01:47:08 -04:00
*
2012-01-25 16:58:23 -05:00
* @ param string $name Name of the endpoint .
* @ param int $places Endpoint mask describing the places the endpoint should be added .
2014-02-28 15:55:14 -05:00
* @ param string $query_var Name of the corresponding query variable . Defaults to $name .
2008-09-01 01:47:08 -04:00
*/
2014-02-28 15:55:14 -05:00
function add_rewrite_endpoint ( $name , $places , $query_var = null ) {
2006-03-12 17:57:00 -05:00
global $wp_rewrite ;
2014-02-28 15:55:14 -05:00
$wp_rewrite -> add_endpoint ( $name , $places , $query_var );
2006-03-12 17:57:00 -05:00
}
2008-06-29 04:20:25 -04:00
/**
2008-09-18 02:14:01 -04:00
* Filter the URL base for taxonomies .
*
* To remove any manually prepended / index . php /.
*
* @ access private
* @ since 2.6 . 0
*
* @ param string $base The taxonomy base that we ' re going to filter
* @ return string
*/
2008-06-29 04:20:25 -04:00
function _wp_filter_taxonomy_base ( $base ) {
2008-07-17 15:41:48 -04:00
if ( ! empty ( $base ) ) {
$base = preg_replace ( '|^/index\.php/|' , '' , $base );
$base = trim ( $base , '/' );
}
2008-06-29 04:20:25 -04:00
return $base ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Examine a url and try to determine the post ID it represents .
2008-09-01 01:47:08 -04:00
*
* Checks are supposedly from the hosted site blog .
*
* @ since 1.0 . 0
*
2008-09-15 20:35:56 -04:00
* @ param string $url Permalink to check .
* @ return int Post ID , or 0 on failure .
2008-09-01 01:47:08 -04:00
*/
2006-04-18 00:47:26 -04:00
function url_to_postid ( $url ) {
global $wp_rewrite ;
2007-06-13 22:25:30 -04:00
2014-03-01 04:31:15 -05:00
/**
* Filter the URL to derive the post ID from .
*
* @ since 2.2 . 0
*
* @ param string $url The URL to derive the post ID from .
*/
$url = apply_filters ( 'url_to_postid' , $url );
2006-04-18 00:47:26 -04:00
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
2008-03-27 18:05:51 -04:00
if ( preg_match ( '#[?&](p|page_id|attachment_id)=(\d+)#' , $url , $values ) ) {
2008-01-10 15:51:07 -05:00
$id = absint ( $values [ 2 ]);
2010-03-13 02:29:10 -05:00
if ( $id )
2008-01-10 15:51:07 -05:00
return $id ;
}
2006-04-18 00:47:26 -04:00
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite -> wp_rewrite_rules ();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if ( empty ( $rewrite ) )
return 0 ;
// Get rid of the #anchor
$url_split = explode ( '#' , $url );
$url = $url_split [ 0 ];
2006-08-30 12:40:17 -04:00
// Get rid of URL ?query=string
2006-04-18 00:47:26 -04:00
$url_split = explode ( '?' , $url );
$url = $url_split [ 0 ];
// Add 'www.' if it is absent and should be there
2010-01-04 12:23:29 -05:00
if ( false !== strpos ( home_url (), '://www.' ) && false === strpos ( $url , '://www.' ) )
2006-04-18 00:47:26 -04:00
$url = str_replace ( '://' , '://www.' , $url );
// Strip 'www.' if it is present and shouldn't be
2010-01-04 12:23:29 -05:00
if ( false === strpos ( home_url (), '://www.' ) )
2006-04-18 00:47:26 -04:00
$url = str_replace ( '://www.' , '://' , $url );
// Strip 'index.php/' if we're not using path info permalinks
2006-09-20 20:09:37 -04:00
if ( ! $wp_rewrite -> using_index_permalinks () )
2013-01-18 08:44:22 -05:00
$url = str_replace ( $wp_rewrite -> index . '/' , '' , $url );
2006-04-18 00:47:26 -04:00
2013-09-05 13:29:13 -04:00
if ( false !== strpos ( trailingslashit ( $url ), home_url ( '/' ) ) ) {
// Chop off http://domain.com/[path]
2010-01-04 12:23:29 -05:00
$url = str_replace ( home_url (), '' , $url );
2006-04-18 00:47:26 -04:00
} else {
// Chop off /path/to/blog
2013-09-05 13:29:13 -04:00
$home_path = parse_url ( home_url ( '/' ) );
2010-10-29 03:07:28 -04:00
$home_path = isset ( $home_path [ 'path' ] ) ? $home_path [ 'path' ] : '' ;
2013-09-05 13:29:13 -04:00
$url = preg_replace ( sprintf ( '#^%s#' , preg_quote ( $home_path ) ), '' , trailingslashit ( $url ) );
2006-04-18 00:47:26 -04:00
}
// Trim leading and lagging slashes
$url = trim ( $url , '/' );
$request = $url ;
2013-10-02 15:42:09 -04:00
$post_type_query_vars = array ();
foreach ( get_post_types ( array () , 'objects' ) as $post_type => $t ) {
if ( ! empty ( $t -> query_var ) )
$post_type_query_vars [ $t -> query_var ] = $post_type ;
}
2006-04-18 00:47:26 -04:00
// Look for matches.
$request_match = $request ;
2010-03-13 02:29:10 -05:00
foreach ( ( array ) $rewrite as $match => $query ) {
2011-08-11 21:55:08 -04:00
2006-04-18 00:47:26 -04:00
// If the requesting file is the anchor of the match, prepend it
// to the path info.
2010-03-13 02:29:10 -05:00
if ( ! empty ( $url ) && ( $url != $request ) && ( strpos ( $match , $url ) === 0 ) )
2006-04-18 00:47:26 -04:00
$request_match = $url . '/' . $request ;
2014-03-29 01:18:15 -04:00
if ( preg_match ( " #^ $match # " , $request_match , $matches ) ) {
2011-08-11 21:55:08 -04:00
2011-10-19 18:49:45 -04:00
if ( $wp_rewrite -> use_verbose_page_rules && preg_match ( '/pagename=\$matches\[([0-9]+)\]/' , $query , $varmatch ) ) {
2014-12-01 23:43:22 -05:00
// This is a verbose page match, let's check to be sure about it.
2011-10-19 18:49:45 -04:00
if ( ! get_page_by_path ( $matches [ $varmatch [ 1 ] ] ) )
2011-08-11 21:55:08 -04:00
continue ;
}
2006-04-18 00:47:26 -04:00
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace ( " !^.+ \ ?! " , '' , $query );
// Substitute the substring matches into the query.
2009-08-20 16:09:36 -04:00
$query = addslashes ( WP_MatchesMapRegex :: apply ( $query , $matches ));
2010-03-13 02:29:10 -05:00
2007-08-23 12:09:37 -04:00
// Filter out non-public query vars
global $wp ;
2013-10-02 15:42:09 -04:00
parse_str ( $query , $query_vars );
2007-08-23 12:09:37 -04:00
$query = array ();
2008-08-06 16:31:54 -04:00
foreach ( ( array ) $query_vars as $key => $value ) {
2013-10-02 15:42:09 -04:00
if ( in_array ( $key , $wp -> public_query_vars ) ){
2007-08-23 12:09:37 -04:00
$query [ $key ] = $value ;
2013-10-02 15:42:09 -04:00
if ( isset ( $post_type_query_vars [ $key ] ) ) {
$query [ 'post_type' ] = $post_type_query_vars [ $key ];
$query [ 'name' ] = $value ;
}
}
2007-08-23 12:09:37 -04:00
}
2010-03-13 02:29:10 -05:00
2007-08-23 12:09:37 -04:00
// Do the query
2013-10-02 15:42:09 -04:00
$query = new WP_Query ( $query );
if ( ! empty ( $query -> posts ) && $query -> is_singular )
2006-04-18 00:47:26 -04:00
return $query -> post -> ID ;
else
return 0 ;
}
}
return 0 ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* WordPress Rewrite Component .
*
* The WordPress Rewrite class writes the rewrite module rules to the . htaccess
* file . It also handles parsing the request to get the correct setup for the
* WordPress Query class .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* The Rewrite along with WP class function as a front controller for WordPress .
* You can add rules to trigger your page view and processing using this
* component . The full functionality of a front controller does not exist ,
* meaning you can ' t define how the template files load based on the rewrite
* rules .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
*/
2006-03-12 17:57:00 -05:00
class WP_Rewrite {
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Permalink structure for posts .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2015-01-08 00:44:23 -05:00
public $permalink_structure ;
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Whether to add trailing slashes .
2008-09-01 01:47:08 -04:00
*
* @ since 2.2 . 0
* @ var bool
*/
2015-01-08 00:44:23 -05:00
public $use_trailing_slashes ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Base for the author permalink structure ( example . com / $author_base / authorname ) .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var string
*/
2006-03-12 17:57:00 -05:00
var $author_base = 'author' ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Permalink structure for author archives .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2006-03-12 17:57:00 -05:00
var $author_structure ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Permalink structure for date archives .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2006-03-12 17:57:00 -05:00
var $date_structure ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Permalink structure for pages .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2006-03-12 17:57:00 -05:00
var $page_structure ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Base of the search permalink structure ( example . com / $search_base / query ) .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var string
*/
2006-03-12 17:57:00 -05:00
var $search_base = 'search' ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Permalink structure for searches .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2006-03-12 17:57:00 -05:00
var $search_structure ;
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Comments permalink base .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var string
*/
2006-03-12 17:57:00 -05:00
var $comments_base = 'comments' ;
2008-09-01 01:47:08 -04:00
2010-09-06 21:18:42 -04:00
/**
* Pagination permalink base .
*
* @ since 3.1 . 0
* @ var string
*/
2015-01-08 00:44:23 -05:00
public $pagination_base = 'page' ;
2010-09-06 21:18:42 -04:00
2015-02-13 22:48:27 -05:00
/**
* Comments pagination permalink base .
*
* @ since 4.2 . 0
* @ access private
* @ var string
*/
var $comments_pagination_base = 'comment-page' ;
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Feed permalink base .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var string
*/
2006-03-12 17:57:00 -05:00
var $feed_base = 'feed' ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Comments feed permalink structure .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var string
*/
2015-01-08 00:44:23 -05:00
var $comment_feed_structure ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Feed request permalink structure .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var string
*/
2006-03-12 17:57:00 -05:00
var $feed_structure ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* The static portion of the post permalink structure .
2008-09-15 20:35:56 -04:00
*
2012-01-25 16:44:02 -05:00
* If the permalink structure is " /archive/%post_id% " then the front
* is " /archive/ " . If the permalink structure is " /%year%/%postname%/ "
* then the front is " / " .
2008-09-01 01:47:08 -04:00
*
2012-01-25 16:44:02 -05:00
* @ see WP_Rewrite :: init ()
2008-09-01 01:47:08 -04:00
* @ since 1.5 . 0
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2015-01-08 00:44:23 -05:00
public $front ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* The prefix for all permalink structures .
2008-09-15 20:35:56 -04:00
*
2012-01-25 16:44:02 -05:00
* If PATHINFO / index permalinks are in use then the root is the value of
* { @ link WP_Rewrite :: $index } with a trailing slash appended . Otherwise
* the root will be empty .
2008-09-01 01:47:08 -04:00
*
2012-01-25 16:44:02 -05:00
* @ see WP_Rewrite :: init ()
* @ see WP_Rewrite :: using_index_permalinks ()
2008-09-01 01:47:08 -04:00
* @ since 1.5 . 0
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2015-01-08 00:44:23 -05:00
public $root = '' ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* The name of the index file which is the entry point to all requests .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
* @ var string
*/
2014-05-19 13:41:13 -04:00
public $index = 'index.php' ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Variable name to use for regex matches in the rewritten query .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
2008-09-15 20:35:56 -04:00
* @ var string
2008-09-01 01:47:08 -04:00
*/
2006-03-12 17:57:00 -05:00
var $matches = '' ;
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Rewrite rules to match against the request to find the redirect or query .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var array
*/
2006-03-12 17:57:00 -05:00
var $rules ;
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Additional rules added external to the rewrite class .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Those not generated by the class , see add_rewrite_rule () .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
* @ access private
* @ var array
*/
2012-01-25 16:44:02 -05:00
var $extra_rules = array ();
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Additional rules that belong at the beginning to match first .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Those not generated by the class , see add_rewrite_rule () .
2008-09-01 01:47:08 -04:00
*
* @ since 2.3 . 0
* @ access private
* @ var array
*/
2012-01-25 16:44:02 -05:00
var $extra_rules_top = array ();
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Rules that don 't redirect to WordPress' index . php .
2008-09-18 02:14:01 -04:00
*
2012-01-25 16:44:02 -05:00
* These rules are written to the mod_rewrite portion of the . htaccess ,
* and are added by { @ link add_external_rule ()} .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
* @ access private
* @ var array
*/
2012-01-25 16:44:02 -05:00
var $non_wp_rules = array ();
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Extra permalink structures , e . g . categories , added by { @ link add_permastruct ()} .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
* @ access private
2008-09-15 20:35:56 -04:00
* @ var array
2008-09-01 01:47:08 -04:00
*/
2008-03-23 18:10:00 -04:00
var $extra_permastructs = array ();
2008-09-15 20:35:56 -04:00
/**
2012-01-25 16:44:02 -05:00
* Endpoints ( like / trackback / ) added by { @ link add_rewrite_endpoint ()} .
2008-09-15 20:35:56 -04:00
*
2010-12-20 04:25:21 -05:00
* @ since 2.1 . 0
2008-09-15 20:35:56 -04:00
* @ access private
* @ var array
*/
2006-03-12 17:57:00 -05:00
var $endpoints ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Whether to write every mod_rewrite rule for WordPress into the . htaccess file .
2008-09-18 02:14:01 -04:00
*
* This is off by default , turning it on might print a lot of rewrite rules
* to the . htaccess file .
2008-09-01 01:47:08 -04:00
*
2012-01-25 16:44:02 -05:00
* @ see WP_Rewrite :: mod_rewrite_rules ()
2008-09-01 01:47:08 -04:00
* @ since 2.0 . 0
* @ access public
* @ var bool
*/
2014-05-19 13:41:13 -04:00
public $use_verbose_rules = false ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Could post permalinks be confused with those of pages ?
*
* If the first rewrite tag in the post permalink structure is one that could
* also match a page name ( e . g . % postname % or % author % ) then this flag is
* set to true . Prior to WordPress 3.3 this flag indicated that every page
* would have a set of rules added to the top of the rewrite rules array .
* Now it tells { @ link WP :: parse_request ()} to check if a URL matching the
* page permastruct is actually a page before accepting it .
2012-02-27 14:46:52 -05:00
*
2014-09-29 09:28:16 -04:00
* @ link https :// core . trac . wordpress . org / ticket / 16687
2012-01-25 16:44:02 -05:00
* @ see WP_Rewrite :: init ()
2008-09-01 01:47:08 -04:00
* @ since 2.5 . 0
* @ access public
* @ var bool
*/
2014-05-19 13:41:13 -04:00
public $use_verbose_page_rules = true ;
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Rewrite tags that can be used in permalink structures .
*
* These are translated into the regular expressions stored in
* { @ link WP_Rewrite :: $rewritereplace } and are rewritten to the
* query variables listed in { @ link WP_Rewrite :: $queryreplace } .
*
* Additional tags can be added with { @ link add_rewrite_tag ()} .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var array
*/
2012-01-25 16:44:02 -05:00
var $rewritecode = array (
'%year%' ,
'%monthnum%' ,
'%day%' ,
'%hour%' ,
'%minute%' ,
'%second%' ,
'%postname%' ,
'%post_id%' ,
'%author%' ,
'%pagename%' ,
'%search%'
);
2006-03-12 17:57:00 -05:00
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Regular expressions to be substituted into rewrite rules in place
* of rewrite tags , see { @ link WP_Rewrite :: $rewritecode } .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var array
*/
2012-01-25 16:44:02 -05:00
var $rewritereplace = array (
'([0-9]{4})' ,
'([0-9]{1,2})' ,
'([0-9]{1,2})' ,
'([0-9]{1,2})' ,
'([0-9]{1,2})' ,
'([0-9]{1,2})' ,
'([^/]+)' ,
'([0-9]+)' ,
'([^/]+)' ,
'([^/]+?)' ,
'(.+)'
);
2006-03-12 17:57:00 -05:00
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Query variables that rewrite tags map to , see { @ link WP_Rewrite :: $rewritecode } .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access private
* @ var array
*/
2012-01-25 16:44:02 -05:00
var $queryreplace = array (
'year=' ,
'monthnum=' ,
'day=' ,
'hour=' ,
'minute=' ,
'second=' ,
'name=' ,
'p=' ,
'author_name=' ,
'pagename=' ,
's='
);
2006-03-12 17:57:00 -05:00
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Supported default feeds .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ var array
*/
2015-01-08 00:44:23 -05:00
public $feeds = array ( 'feed' , 'rdf' , 'rss' , 'rss2' , 'atom' );
2006-11-19 02:56:05 -05:00
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Whether permalinks are being used .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* This can be either rewrite module or permalink in the HTTP query string .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ return bool True , if permalinks are enabled .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function using_permalinks () {
2010-03-13 02:29:10 -05:00
return ! empty ( $this -> permalink_structure );
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Whether permalinks are being used and rewrite module is not enabled .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Means that permalink links are enabled and index . php is in the URL .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ return bool
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function using_index_permalinks () {
2010-03-13 02:29:10 -05:00
if ( empty ( $this -> permalink_structure ) )
2006-03-12 17:57:00 -05:00
return false ;
// If the index is not in the permalink, we're using mod_rewrite.
2010-03-13 02:29:10 -05:00
if ( preg_match ( '#^/*' . $this -> index . '#' , $this -> permalink_structure ) )
2006-03-12 17:57:00 -05:00
return true ;
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
return false ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Whether permalinks are being used and rewrite module is enabled .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Using permalinks and index . php is not in the URL .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ return bool
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function using_mod_rewrite_permalinks () {
2010-03-13 02:29:10 -05:00
if ( $this -> using_permalinks () && ! $this -> using_index_permalinks () )
2006-03-12 17:57:00 -05:00
return true ;
else
return false ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Index for matches for usage in preg_ * () functions .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* The format of the string is , with empty matches property value , '$NUM' .
* The 'NUM' will be replaced with the value in the $number parameter . With
* the matches property not empty , the value of the returned string will
* contain that value of the matches property . The format then will be
* '$MATCHES[NUM]' , with MATCHES as the value in the property and NUM the
* value of the $number parameter .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ param int $number Index number .
* @ return string
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function preg_index ( $number ) {
2006-03-12 17:57:00 -05:00
$match_prefix = '$' ;
$match_suffix = '' ;
2008-09-18 02:14:01 -04:00
if ( ! empty ( $this -> matches ) ) {
2006-11-19 02:56:05 -05:00
$match_prefix = '$' . $this -> matches . '[' ;
2006-03-12 17:57:00 -05:00
$match_suffix = ']' ;
2006-11-19 02:56:05 -05:00
}
2006-03-12 17:57:00 -05:00
2006-11-19 02:56:05 -05:00
return " $match_prefix $number $match_suffix " ;
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Retrieve all page and attachments for pages URIs .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* The attachments are for those that have pages as parents and will be
* retrieved .
2008-09-01 01:47:08 -04:00
*
* @ since 2.5 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ return array Array of page URIs as first element and attachment URIs as second element .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function page_uri_index () {
2007-12-03 19:19:10 -05:00
global $wpdb ;
//get pages in order of hierarchy, i.e. children after parents
2013-09-05 17:53:09 -04:00
$pages = $wpdb -> get_results ( " SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft' " );
$posts = get_page_hierarchy ( $pages );
2010-01-15 17:11:12 -05:00
2009-12-23 11:23:40 -05:00
// If we have no pages get out quick
if ( ! $posts )
return array ( array (), array () );
2007-12-03 19:19:10 -05:00
//now reverse it, because we need parents after children for rewrite rules to work properly
$posts = array_reverse ( $posts , true );
$page_uris = array ();
$page_attachment_uris = array ();
2010-03-13 02:29:10 -05:00
foreach ( $posts as $id => $post ) {
2007-12-03 19:19:10 -05:00
// URL => page name
$uri = get_page_uri ( $id );
$attachments = $wpdb -> get_results ( $wpdb -> prepare ( " SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d " , $id ));
2010-03-13 02:29:10 -05:00
if ( ! empty ( $attachments ) ) {
2007-12-03 19:19:10 -05:00
foreach ( $attachments as $attachment ) {
$attach_uri = get_page_uri ( $attachment -> ID );
$page_attachment_uris [ $attach_uri ] = $attachment -> ID ;
}
}
$page_uris [ $uri ] = $id ;
}
return array ( $page_uris , $page_attachment_uris );
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Retrieve all of the rewrite rules for pages .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ return array
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function page_rewrite_rules () {
2011-08-18 13:57:22 -04:00
// the extra .? at the beginning prevents clashes with other regular expressions in the rules array
2012-01-22 12:21:29 -05:00
$this -> add_rewrite_tag ( '%pagename%' , '(.?.+?)' , 'pagename=' );
2012-02-01 14:19:32 -05:00
return $this -> generate_rewrite_rules ( $this -> get_page_permastruct (), EP_PAGES , true , true , false , false );
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve date permalink structure , with year , month , and day .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* The permalink structure for the date , if not set already depends on the
* permalink structure . It can be one of three formats . The first is year ,
* month , day ; the second is day , month , year ; and the last format is month ,
* day , year . These are matched against the permalink structure for which
* one is used . If none matches , then the default will be used , which is
* year , month , day .
*
* Prevents post ID and date permalinks from overlapping . In the case of
* post_id , the date permalink will be prepended with front permalink with
* 'date/' before the actual permalink to form the complete date permalink
* structure .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return string | false False on no permalink structure . Date permalink structure .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_date_permastruct () {
2010-03-13 02:29:10 -05:00
if ( isset ( $this -> date_structure ) )
2006-03-12 17:57:00 -05:00
return $this -> date_structure ;
2010-03-13 02:29:10 -05:00
if ( empty ( $this -> permalink_structure ) ) {
2006-03-12 17:57:00 -05:00
$this -> date_structure = '' ;
return false ;
}
// The date permalink must have year, month, and day separated by slashes.
$endians = array ( '%year%/%monthnum%/%day%' , '%day%/%monthnum%/%year%' , '%monthnum%/%day%/%year%' );
$this -> date_structure = '' ;
$date_endian = '' ;
2010-03-13 02:29:10 -05:00
foreach ( $endians as $endian ) {
if ( false !== strpos ( $this -> permalink_structure , $endian ) ) {
2006-03-12 17:57:00 -05:00
$date_endian = $endian ;
break ;
}
2006-11-19 02:56:05 -05:00
}
2006-03-12 17:57:00 -05:00
if ( empty ( $date_endian ) )
$date_endian = '%year%/%monthnum%/%day%' ;
// Do not allow the date tags and %post_id% to overlap in the permalink
2006-11-19 02:56:05 -05:00
// structure. If they do, move the date tags to $front/date/.
2006-03-12 17:57:00 -05:00
$front = $this -> front ;
preg_match_all ( '/%.+?%/' , $this -> permalink_structure , $tokens );
$tok_index = 1 ;
2008-08-06 16:31:54 -04:00
foreach ( ( array ) $tokens [ 0 ] as $token ) {
2010-03-13 02:29:10 -05:00
if ( '%post_id%' == $token && ( $tok_index <= 3 ) ) {
2006-03-12 17:57:00 -05:00
$front = $front . 'date/' ;
break ;
}
2006-10-03 11:56:23 -04:00
$tok_index ++ ;
2006-03-12 17:57:00 -05:00
}
$this -> date_structure = $front . $date_endian ;
return $this -> date_structure ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the year permalink structure without month and day .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Gets the date permalink structure and strips out the month and day
* permalink structures .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return false | string False on failure . Year structure on success .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_year_permastruct () {
2012-01-25 16:44:02 -05:00
$structure = $this -> get_date_permastruct ();
2006-03-12 17:57:00 -05:00
2010-03-13 02:29:10 -05:00
if ( empty ( $structure ) )
2006-03-12 17:57:00 -05:00
return false ;
$structure = str_replace ( '%monthnum%' , '' , $structure );
$structure = str_replace ( '%day%' , '' , $structure );
$structure = preg_replace ( '#/+#' , '/' , $structure );
return $structure ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the month permalink structure without day and with year .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Gets the date permalink structure and strips out the day permalink
* structures . Keeps the year permalink structure .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return false | string False on failure . Year / Month structure on success .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_month_permastruct () {
2012-01-25 16:44:02 -05:00
$structure = $this -> get_date_permastruct ();
2006-03-12 17:57:00 -05:00
2010-03-13 02:29:10 -05:00
if ( empty ( $structure ) )
2006-03-12 17:57:00 -05:00
return false ;
$structure = str_replace ( '%day%' , '' , $structure );
$structure = preg_replace ( '#/+#' , '/' , $structure );
return $structure ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the day permalink structure with month and year .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Keeps date permalink structure with all year , month , and day .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return string | false False on failure . Year / Month / Day structure on success .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_day_permastruct () {
2012-01-25 16:44:02 -05:00
return $this -> get_date_permastruct ();
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the permalink structure for categories .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* If the category_base property has no value , then the category structure
* will have the front property value , followed by 'category' , and finally
* '%category%' . If it does , then the root property will be used , along with
* the category_base property value .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ return bool | string False on failure . Category permalink structure .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_category_permastruct () {
2010-10-17 01:41:22 -04:00
return $this -> get_extra_permastruct ( 'category' );
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the permalink structure for tags .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* If the tag_base property has no value , then the tag structure will have
* the front property value , followed by 'tag' , and finally '%tag%' . If it
* does , then the root property will be used , along with the tag_base
* property value .
2008-09-01 01:47:08 -04:00
*
* @ since 2.3 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ return bool | string False on failure . Tag permalink structure .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_tag_permastruct () {
2010-10-17 01:41:22 -04:00
return $this -> get_extra_permastruct ( 'post_tag' );
2007-03-31 05:19:32 -04:00
}
2008-09-15 20:35:56 -04:00
/**
* Retrieve extra permalink structure by name .
*
2010-12-20 04:25:21 -05:00
* @ since 2.5 . 0
2008-09-15 20:35:56 -04:00
* @ access public
*
* @ param string $name Permalink structure name .
* @ return string | bool False if not found . Permalink structure string .
*/
2014-05-19 13:41:13 -04:00
public function get_extra_permastruct ( $name ) {
2009-11-19 17:47:11 -05:00
if ( empty ( $this -> permalink_structure ) )
return false ;
2010-03-13 02:29:10 -05:00
2008-03-26 02:37:19 -04:00
if ( isset ( $this -> extra_permastructs [ $name ]) )
2012-01-24 13:27:18 -05:00
return $this -> extra_permastructs [ $name ][ 'struct' ];
2010-03-13 02:29:10 -05:00
2008-03-26 02:37:19 -04:00
return false ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the author permalink structure .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* The permalink structure is front property , author base , and finally
* '/%author%' . Will set the author_structure property and then return it
* without attempting to set the value again .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return string | false False if not found . Permalink structure string .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_author_permastruct () {
2010-03-13 02:29:10 -05:00
if ( isset ( $this -> author_structure ) )
2006-03-12 17:57:00 -05:00
return $this -> author_structure ;
2010-03-13 02:29:10 -05:00
if ( empty ( $this -> permalink_structure ) ) {
2006-03-12 17:57:00 -05:00
$this -> author_structure = '' ;
return false ;
}
$this -> author_structure = $this -> front . $this -> author_base . '/%author%' ;
return $this -> author_structure ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the search permalink structure .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* The permalink structure is root property , search base , and finally
* '/%search%' . Will set the search_structure property and then return it
* without attempting to set the value again .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return string | false False if not found . Permalink structure string .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_search_permastruct () {
2010-03-13 02:29:10 -05:00
if ( isset ( $this -> search_structure ) )
2006-03-12 17:57:00 -05:00
return $this -> search_structure ;
2010-03-13 02:29:10 -05:00
if ( empty ( $this -> permalink_structure ) ) {
2006-03-12 17:57:00 -05:00
$this -> search_structure = '' ;
return false ;
}
$this -> search_structure = $this -> root . $this -> search_base . '/%search%' ;
return $this -> search_structure ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the page permalink structure .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* The permalink structure is root property , and '%pagename%' . Will set the
* page_structure property and then return it without attempting to set the
* value again .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return string | false False if not found . Permalink structure string .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_page_permastruct () {
2010-03-13 02:29:10 -05:00
if ( isset ( $this -> page_structure ) )
2006-03-12 17:57:00 -05:00
return $this -> page_structure ;
if ( empty ( $this -> permalink_structure )) {
$this -> page_structure = '' ;
return false ;
}
$this -> page_structure = $this -> root . '%pagename%' ;
return $this -> page_structure ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the feed permalink structure .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* The permalink structure is root property , feed base , and finally
* '/%feed%' . Will set the feed_structure property and then return it
* without attempting to set the value again .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2014-11-30 17:25:23 -05:00
* @ return string | false False if not found . Permalink structure string .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_feed_permastruct () {
2010-03-13 02:29:10 -05:00
if ( isset ( $this -> feed_structure ) )
2006-03-12 17:57:00 -05:00
return $this -> feed_structure ;
2010-03-13 02:29:10 -05:00
if ( empty ( $this -> permalink_structure ) ) {
2006-03-12 17:57:00 -05:00
$this -> feed_structure = '' ;
return false ;
}
$this -> feed_structure = $this -> root . $this -> feed_base . '/%feed%' ;
return $this -> feed_structure ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Retrieve the comment feed permalink structure .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* The permalink structure is root property , comment base property , feed
* base and finally '/%feed%' . Will set the comment_feed_structure property
* and then return it without attempting to set the value again .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ return string | bool False if not found . Permalink structure string .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function get_comment_feed_permastruct () {
2010-03-13 02:29:10 -05:00
if ( isset ( $this -> comment_feed_structure ) )
2006-03-12 17:57:00 -05:00
return $this -> comment_feed_structure ;
if ( empty ( $this -> permalink_structure )) {
$this -> comment_feed_structure = '' ;
return false ;
}
$this -> comment_feed_structure = $this -> root . $this -> comments_base . '/' . $this -> feed_base . '/%feed%' ;
return $this -> comment_feed_structure ;
}
2008-09-01 01:47:08 -04:00
/**
2012-01-25 17:48:24 -05:00
* Add or update existing rewrite tags ( e . g . % postname % ) .
2008-09-18 02:14:01 -04:00
*
* If the tag already exists , replace the existing pattern and query for
2012-01-25 17:48:24 -05:00
* that tag , otherwise add the new tag .
2008-09-01 01:47:08 -04:00
*
2012-01-25 17:48:24 -05:00
* @ see WP_Rewrite :: $rewritecode
* @ see WP_Rewrite :: $rewritereplace
* @ see WP_Rewrite :: $queryreplace
2008-09-01 01:47:08 -04:00
* @ since 1.5 . 0
* @ access public
*
2012-01-25 17:48:24 -05:00
* @ 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 '=' .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function add_rewrite_tag ( $tag , $regex , $query ) {
2012-01-25 17:48:24 -05:00
$position = array_search ( $tag , $this -> rewritecode );
2008-09-18 02:14:01 -04:00
if ( false !== $position && null !== $position ) {
2012-01-25 17:48:24 -05:00
$this -> rewritereplace [ $position ] = $regex ;
$this -> queryreplace [ $position ] = $query ;
2006-03-12 17:57:00 -05:00
} else {
$this -> rewritecode [] = $tag ;
2012-01-25 17:48:24 -05:00
$this -> rewritereplace [] = $regex ;
2006-03-12 17:57:00 -05:00
$this -> queryreplace [] = $query ;
}
}
2008-09-01 01:47:08 -04:00
/**
2012-01-25 16:44:02 -05:00
* Generate rewrite rules from a permalink structure .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* The main WP_Rewrite function for building the rewrite rule list . The
* contents of the function is a mix of black magic and regular expressions ,
* so best just ignore the contents and move to the parameters .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ param string $permalink_structure The permalink structure .
2012-01-25 16:44:02 -05:00
* @ param int $ep_mask Endpoint mask defining what endpoints are added to the structure . Default is EP_NONE .
* @ param bool $paged Should archive pagination rules be added for the structure ? Default is true .
* @ param bool $feed Should feed rewrite rules be added for the structure ? Default is true .
* @ param bool $forcomments Should the feed rules be a query for a comments feed ? Default is false .
* @ param bool $walk_dirs Should the 'directories' making up the structure be walked over and rewrite rules
* built for each in turn ? Default is true .
* @ param bool $endpoints Should endpoints be applied to the generated rewrite rules ? Default is true .
2008-09-18 02:14:01 -04:00
* @ return array Rewrite rule list .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function generate_rewrite_rules ( $permalink_structure , $ep_mask = EP_NONE , $paged = true , $feed = true , $forcomments = false , $walk_dirs = true , $endpoints = true ) {
2006-03-12 17:57:00 -05:00
//build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
$feedregex2 = '' ;
2010-03-13 02:29:10 -05:00
foreach ( ( array ) $this -> feeds as $feed_name )
2006-03-12 17:57:00 -05:00
$feedregex2 .= $feed_name . '|' ;
2011-12-13 18:45:31 -05:00
$feedregex2 = '(' . trim ( $feedregex2 , '|' ) . ')/?$' ;
2010-03-13 02:29:10 -05:00
2006-03-12 17:57:00 -05:00
//$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
//and <permalink>/atom are both possible
2011-12-13 18:45:31 -05:00
$feedregex = $this -> feed_base . '/' . $feedregex2 ;
2006-03-12 17:57:00 -05:00
//build a regex to match the trackback and page/xx parts of URLs
$trackbackregex = 'trackback/?$' ;
2010-09-06 21:18:42 -04:00
$pageregex = $this -> pagination_base . '/?([0-9]{1,})/?$' ;
2015-02-13 22:48:27 -05:00
$commentregex = $this -> comments_pagination_base . '-([0-9]{1,})/?$' ;
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//build up an array of endpoint regexes to append => queries to append
2010-03-13 02:29:10 -05:00
if ( $endpoints ) {
2006-03-12 17:57:00 -05:00
$ep_query_append = array ();
2008-08-06 16:31:54 -04:00
foreach ( ( array ) $this -> endpoints as $endpoint ) {
2006-03-12 17:57:00 -05:00
//match everything after the endpoint name, but allow for nothing to appear there
$epmatch = $endpoint [ 1 ] . '(/(.*))?/?$' ;
//this will be appended on to the rest of the query for each dir
2014-02-28 15:55:14 -05:00
$epquery = '&' . $endpoint [ 2 ] . '=' ;
2006-03-12 17:57:00 -05:00
$ep_query_append [ $epmatch ] = array ( $endpoint [ 0 ], $epquery );
}
}
//get everything up to the first rewrite tag
$front = substr ( $permalink_structure , 0 , strpos ( $permalink_structure , '%' ));
//build an array of the tags (note that said array ends up being in $tokens[0])
preg_match_all ( '/%.+?%/' , $permalink_structure , $tokens );
$num_tokens = count ( $tokens [ 0 ]);
$index = $this -> index ; //probably 'index.php'
$feedindex = $index ;
$trackbackindex = $index ;
//build a list from the rewritecode and queryreplace arrays, that will look something like
//tagname=$matches[i] where i is the current $i
2014-12-20 17:47:22 -05:00
$queries = array ();
2010-03-13 02:29:10 -05:00
for ( $i = 0 ; $i < $num_tokens ; ++ $i ) {
if ( 0 < $i )
2006-03-12 17:57:00 -05:00
$queries [ $i ] = $queries [ $i - 1 ] . '&' ;
2010-03-13 02:29:10 -05:00
else
2008-01-10 15:51:07 -05:00
$queries [ $i ] = '' ;
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
$query_token = str_replace ( $this -> rewritecode , $this -> queryreplace , $tokens [ 0 ][ $i ]) . $this -> preg_index ( $i + 1 );
$queries [ $i ] .= $query_token ;
}
//get the structure, minus any cruft (stuff that isn't tags) at the front
$structure = $permalink_structure ;
2010-03-13 02:29:10 -05:00
if ( $front != '/' )
2006-03-12 17:57:00 -05:00
$structure = str_replace ( $front , '' , $structure );
2010-03-13 02:29:10 -05:00
2006-03-12 17:57:00 -05:00
//create a list of dirs to walk over, making rewrite rules for each level
2012-01-25 16:44:02 -05:00
//so for example, a $structure of /%year%/%monthnum%/%postname% would create
//rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname%
2006-03-12 17:57:00 -05:00
$structure = trim ( $structure , '/' );
2010-03-13 02:29:10 -05:00
$dirs = $walk_dirs ? explode ( '/' , $structure ) : array ( $structure );
2006-03-12 17:57:00 -05:00
$num_dirs = count ( $dirs );
//strip slashes from the front of $front
$front = preg_replace ( '|^/+|' , '' , $front );
//the main workhorse loop
$post_rewrite = array ();
$struct = $front ;
2010-03-13 02:29:10 -05:00
for ( $j = 0 ; $j < $num_dirs ; ++ $j ) {
2006-03-12 17:57:00 -05:00
//get the struct for this dir, and trim slashes off the front
$struct .= $dirs [ $j ] . '/' ; //accumulate. see comment near explode('/', $structure) above
$struct = ltrim ( $struct , '/' );
2010-03-13 02:29:10 -05:00
2006-03-12 17:57:00 -05:00
//replace tags with regexes
$match = str_replace ( $this -> rewritecode , $this -> rewritereplace , $struct );
2010-03-13 02:29:10 -05:00
2006-03-12 17:57:00 -05:00
//make a list of tags, and store how many there are in $num_toks
$num_toks = preg_match_all ( '/%.+?%/' , $struct , $toks );
2010-03-13 02:29:10 -05:00
2006-03-12 17:57:00 -05:00
//get the 'tagname=$matches[i]'
2014-12-20 17:47:22 -05:00
$query = ( ! empty ( $num_toks ) && isset ( $queries [ $num_toks - 1 ] ) ) ? $queries [ $num_toks - 1 ] : '' ;
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//set up $ep_mask_specific which is used to match more specific URL types
2010-03-13 02:29:10 -05:00
switch ( $dirs [ $j ] ) {
case '%year%' :
$ep_mask_specific = EP_YEAR ;
break ;
case '%monthnum%' :
$ep_mask_specific = EP_MONTH ;
break ;
case '%day%' :
$ep_mask_specific = EP_DAY ;
break ;
2010-03-19 22:05:32 -04:00
default :
$ep_mask_specific = EP_NONE ;
2006-03-12 17:57:00 -05:00
}
//create query for /page/xx
$pagematch = $match . $pageregex ;
$pagequery = $index . '?' . $query . '&paged=' . $this -> preg_index ( $num_toks + 1 );
2008-10-23 14:55:22 -04:00
//create query for /comment-page-xx
$commentmatch = $match . $commentregex ;
$commentquery = $index . '?' . $query . '&cpage=' . $this -> preg_index ( $num_toks + 1 );
2009-05-20 18:24:37 -04:00
if ( get_option ( 'page_on_front' ) ) {
//create query for Root /comment-page-xx
$rootcommentmatch = $match . $commentregex ;
$rootcommentquery = $index . '?' . $query . '&page_id=' . get_option ( 'page_on_front' ) . '&cpage=' . $this -> preg_index ( $num_toks + 1 );
}
2006-03-12 17:57:00 -05:00
//create query for /feed/(feed|atom|rss|rss2|rdf)
$feedmatch = $match . $feedregex ;
$feedquery = $feedindex . '?' . $query . '&feed=' . $this -> preg_index ( $num_toks + 1 );
//create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
$feedmatch2 = $match . $feedregex2 ;
$feedquery2 = $feedindex . '?' . $query . '&feed=' . $this -> preg_index ( $num_toks + 1 );
//if asked to, turn the feed queries into comment feed ones
2010-03-13 02:29:10 -05:00
if ( $forcomments ) {
2006-03-12 17:57:00 -05:00
$feedquery .= '&withcomments=1' ;
$feedquery2 .= '&withcomments=1' ;
}
//start creating the array of rewrites for this dir
$rewrite = array ();
2010-03-13 02:29:10 -05:00
if ( $feed ) //...adding on /feed/ regexes => queries
2006-03-12 17:57:00 -05:00
$rewrite = array ( $feedmatch => $feedquery , $feedmatch2 => $feedquery2 );
2010-03-13 02:29:10 -05:00
if ( $paged ) //...and /page/xx ones
2006-03-12 17:57:00 -05:00
$rewrite = array_merge ( $rewrite , array ( $pagematch => $pagequery ));
2006-11-19 02:56:05 -05:00
2008-10-23 14:55:22 -04:00
//only on pages with comments add ../comment-page-xx/
2015-01-08 02:05:25 -05:00
if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) {
2008-10-23 14:55:22 -04:00
$rewrite = array_merge ( $rewrite , array ( $commentmatch => $commentquery ));
2015-01-08 02:05:25 -05:00
} elseif ( EP_ROOT & $ep_mask && get_option ( 'page_on_front' ) ) {
2009-05-20 18:24:37 -04:00
$rewrite = array_merge ( $rewrite , array ( $rootcommentmatch => $rootcommentquery ));
2015-01-08 02:05:25 -05:00
}
2007-03-28 20:17:40 -04:00
//do endpoints
2010-03-13 02:29:10 -05:00
if ( $endpoints ) {
2008-08-06 16:31:54 -04:00
foreach ( ( array ) $ep_query_append as $regex => $ep ) {
2007-03-28 20:17:40 -04:00
//add the endpoints on if the mask fits
2010-03-13 02:29:10 -05:00
if ( $ep [ 0 ] & $ep_mask || $ep [ 0 ] & $ep_mask_specific )
2007-03-28 20:17:40 -04:00
$rewrite [ $match . $regex ] = $index . '?' . $query . $ep [ 1 ] . $this -> preg_index ( $num_toks + 2 );
}
}
2006-03-12 17:57:00 -05:00
//if we've got some tags in this dir
2010-03-13 02:29:10 -05:00
if ( $num_toks ) {
2006-03-12 17:57:00 -05:00
$post = false ;
$page = false ;
2006-11-19 02:56:05 -05:00
//check to see if this dir is permalink-level: i.e. the structure specifies an
2006-03-12 17:57:00 -05:00
//individual post. Do this by checking it contains at least one of 1) post name,
2006-11-19 02:56:05 -05:00
//2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
2006-03-12 17:57:00 -05:00
//minute all present). Set these flags now as we need them for the endpoints.
2010-03-13 02:29:10 -05:00
if ( strpos ( $struct , '%postname%' ) !== false
|| strpos ( $struct , '%post_id%' ) !== false
2007-03-07 00:29:15 -05:00
|| strpos ( $struct , '%pagename%' ) !== false
2010-03-13 02:29:10 -05:00
|| ( strpos ( $struct , '%year%' ) !== false && strpos ( $struct , '%monthnum%' ) !== false && strpos ( $struct , '%day%' ) !== false && strpos ( $struct , '%hour%' ) !== false && strpos ( $struct , '%minute%' ) !== false && strpos ( $struct , '%second%' ) !== false )
) {
2006-03-12 17:57:00 -05:00
$post = true ;
2010-03-13 02:29:10 -05:00
if ( strpos ( $struct , '%pagename%' ) !== false )
2006-03-12 17:57:00 -05:00
$page = true ;
}
2006-11-19 02:56:05 -05:00
2010-03-13 01:59:07 -05:00
if ( ! $post ) {
// For custom post types, we need to add on endpoints as well.
foreach ( get_post_types ( array ( '_builtin' => false ) ) as $ptype ) {
if ( strpos ( $struct , " % $ptype % " ) !== false ) {
$post = true ;
2010-04-18 11:54:45 -04:00
$page = is_post_type_hierarchical ( $ptype ); // This is for page style attachment url's
2010-03-13 02:29:10 -05:00
break ;
2010-03-13 01:59:07 -05:00
}
}
}
2006-03-12 17:57:00 -05:00
//if we're creating rules for a permalink, do all the endpoints like attachments etc
2010-03-13 02:29:10 -05:00
if ( $post ) {
2006-03-12 17:57:00 -05:00
//create query and regex for trackback
$trackbackmatch = $match . $trackbackregex ;
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1' ;
//trim slashes from the end of the regex for this dir
$match = rtrim ( $match , '/' );
//get rid of brackets
2010-03-13 02:29:10 -05:00
$submatchbase = str_replace ( array ( '(' , ')' ), '' , $match );
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//add a rule for at attachments, which take the form of <permalink>/some-text
$sub1 = $submatchbase . '/([^/]+)/' ;
$sub1tb = $sub1 . $trackbackregex ; //add trackback regex <permalink>/trackback/...
$sub1feed = $sub1 . $feedregex ; //and <permalink>/feed/(atom|...)
$sub1feed2 = $sub1 . $feedregex2 ; //and <permalink>/(feed|atom...)
2008-10-23 14:55:22 -04:00
$sub1comment = $sub1 . $commentregex ; //and <permalink>/comment-page-xx
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//add another rule to match attachments in the explicit form:
//<permalink>/attachment/some-text
$sub2 = $submatchbase . '/attachment/([^/]+)/' ;
$sub2tb = $sub2 . $trackbackregex ; //and add trackbacks <permalink>/attachment/trackback
$sub2feed = $sub2 . $feedregex ; //feeds, <permalink>/attachment/feed/(atom|...)
$sub2feed2 = $sub2 . $feedregex2 ; //and feeds again on to this <permalink>/attachment/(feed|atom...)
2008-10-23 14:55:22 -04:00
$sub2comment = $sub2 . $commentregex ; //and <permalink>/comment-page-xx
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//create queries for these extra tag-ons we've just dealt with
$subquery = $index . '?attachment=' . $this -> preg_index ( 1 );
$subtbquery = $subquery . '&tb=1' ;
$subfeedquery = $subquery . '&feed=' . $this -> preg_index ( 2 );
2008-10-23 14:55:22 -04:00
$subcommentquery = $subquery . '&cpage=' . $this -> preg_index ( 2 );
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//do endpoints for attachments
2010-03-13 02:29:10 -05:00
if ( ! empty ( $endpoints ) ) {
foreach ( ( array ) $ep_query_append as $regex => $ep ) {
if ( $ep [ 0 ] & EP_ATTACHMENT ) {
2013-07-28 15:02:08 -04:00
$rewrite [ $sub1 . $regex ] = $subquery . $ep [ 1 ] . $this -> preg_index ( 3 );
$rewrite [ $sub2 . $regex ] = $subquery . $ep [ 1 ] . $this -> preg_index ( 3 );
2010-03-13 02:29:10 -05:00
}
2006-03-12 17:57:00 -05:00
}
2010-03-13 02:29:10 -05:00
}
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//now we've finished with endpoints, finish off the $sub1 and $sub2 matches
2012-01-25 16:44:02 -05:00
//add a ? as we don't have to match that last slash, and finally a $ so we
//match to the end of the URL
2006-03-12 17:57:00 -05:00
$sub1 .= '?$' ;
$sub2 .= '?$' ;
2006-11-19 02:56:05 -05:00
2012-01-25 16:44:02 -05:00
//post pagination, e.g. <permalink>/2/
2006-03-12 17:57:00 -05:00
$match = $match . '(/[0-9]+)?/?$' ;
$query = $index . '?' . $query . '&page=' . $this -> preg_index ( $num_toks + 1 );
} else { //not matching a permalink so this is a lot simpler
//close the match and finalise the query
$match .= '?$' ;
$query = $index . '?' . $query ;
}
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//create the final array for this dir by joining the $rewrite array (which currently
//only contains rules/queries for trackback, pages etc) to the main regex/query for
//this dir
$rewrite = array_merge ( $rewrite , array ( $match => $query ));
//if we're matching a permalink, add those extras (attachments etc) on
2010-03-13 02:29:10 -05:00
if ( $post ) {
2006-03-12 17:57:00 -05:00
//add trackback
$rewrite = array_merge ( array ( $trackbackmatch => $trackbackquery ), $rewrite );
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//add regexes/queries for attachments, attachment trackbacks and so on
if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
2008-10-23 14:55:22 -04:00
$rewrite = array_merge ( $rewrite , array ( $sub1 => $subquery , $sub1tb => $subtbquery , $sub1feed => $subfeedquery , $sub1feed2 => $subfeedquery , $sub1comment => $subcommentquery ));
$rewrite = array_merge ( array ( $sub2 => $subquery , $sub2tb => $subtbquery , $sub2feed => $subfeedquery , $sub2feed2 => $subfeedquery , $sub2comment => $subcommentquery ), $rewrite );
2006-03-12 17:57:00 -05:00
}
} //if($num_toks)
//add the rules for this dir to the accumulating $post_rewrite
$post_rewrite = array_merge ( $rewrite , $post_rewrite );
} //foreach ($dir)
return $post_rewrite ; //the finished rules. phew!
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Generate Rewrite rules with permalink structure and walking directory only .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* Shorten version of { @ link WP_Rewrite :: generate_rewrite_rules ()} that
* allows for shorter list of parameters . See the method for longer
* description of what generating rewrite rules does .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* @ uses WP_Rewrite :: generate_rewrite_rules () See for long description and rest of parameters .
2008-09-01 01:47:08 -04:00
* @ since 1.5 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ param string $permalink_structure The permalink structure to generate rules .
* @ param bool $walk_dirs Optional , default is false . Whether to create list of directories to walk over .
* @ return array
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function generate_rewrite_rule ( $permalink_structure , $walk_dirs = false ) {
2006-03-12 17:57:00 -05:00
return $this -> generate_rewrite_rules ( $permalink_structure , EP_NONE , false , false , false , $walk_dirs );
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Construct rewrite matches and queries from permalink structure .
*
* Runs the action 'generate_rewrite_rules' with the parameter that is an
* reference to the current WP_Rewrite instance to further manipulate the
* permalink structures and rewrite rules . Runs the 'rewrite_rules_array'
* filter on the full rewrite rule array .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* There are two ways to manipulate the rewrite rules , one by hooking into
* the 'generate_rewrite_rules' action and gaining full control of the
* object or just manipulating the rewrite rule array before it is passed
* from the function .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ return array An associate array of matches and queries .
2006-03-12 17:57:00 -05:00
*/
2014-05-19 13:41:13 -04:00
public function rewrite_rules () {
2006-03-12 17:57:00 -05:00
$rewrite = array ();
2010-03-13 02:29:10 -05:00
if ( empty ( $this -> permalink_structure ) )
2006-03-12 17:57:00 -05:00
return $rewrite ;
2010-04-28 02:37:15 -04:00
// robots.txt -only if installed at the root
$home_path = parse_url ( home_url () );
$robots_rewrite = ( empty ( $home_path [ 'path' ] ) || '/' == $home_path [ 'path' ] ) ? array ( 'robots\.txt$' => $this -> index . '?robots=1' ) : array ();
2010-05-03 16:26:11 -04:00
2012-09-11 16:11:39 -04:00
// Old feed and service files
$deprecated_files = array (
'.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this -> index . '?feed=old' ,
2012-11-07 16:27:02 -05:00
'.*wp-app\.php(/.*)?$' => $this -> index . '?error=403' ,
2012-09-11 16:11:39 -04:00
);
2006-11-29 20:54:05 -05:00
2010-05-31 16:50:27 -04:00
// Registration rules
$registration_pages = array ();
if ( is_multisite () && is_main_site () ) {
$registration_pages [ '.*wp-signup.php$' ] = $this -> index . '?signup=true' ;
$registration_pages [ '.*wp-activate.php$' ] = $this -> index . '?activate=true' ;
}
2012-04-25 16:49:57 -04:00
$registration_pages [ '.*wp-register.php$' ] = $this -> index . '?register=true' ; // Deprecated
2010-05-31 16:50:27 -04:00
2014-03-01 04:31:15 -05:00
// Post rewrite rules.
2012-09-03 18:06:28 -04:00
$post_rewrite = $this -> generate_rewrite_rules ( $this -> permalink_structure , EP_PERMALINK );
2006-03-12 17:57:00 -05:00
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for " post " archives .
*
* @ since 1.5 . 0
*
* @ param array $post_rewrite The rewrite rules for posts .
*/
$post_rewrite = apply_filters ( 'post_rewrite_rules' , $post_rewrite );
// Date rewrite rules.
2006-03-12 17:57:00 -05:00
$date_rewrite = $this -> generate_rewrite_rules ( $this -> get_date_permastruct (), EP_DATE );
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for date archives .
*
* Likely date archives would include / yyyy / , / yyyy / mm / , and / yyyy / mm / dd /.
*
* @ since 1.5 . 0
*
* @ param array $date_rewrite The rewrite rules for date archives .
*/
$date_rewrite = apply_filters ( 'date_rewrite_rules' , $date_rewrite );
// Root-level rewrite rules.
2006-03-12 17:57:00 -05:00
$root_rewrite = $this -> generate_rewrite_rules ( $this -> root . '/' , EP_ROOT );
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for root - level archives .
*
* Likely root - level archives would include pagination rules for the homepage
* as well as site - wide post feeds ( e . g . / feed / , and / feed / atom / ) .
*
* @ since 1.5 . 0
*
* @ param array $root_rewrite The root - level rewrite rules .
*/
$root_rewrite = apply_filters ( 'root_rewrite_rules' , $root_rewrite );
// Comments rewrite rules.
2013-02-28 13:35:13 -05:00
$comments_rewrite = $this -> generate_rewrite_rules ( $this -> root . $this -> comments_base , EP_COMMENTS , false , true , true , false );
2006-03-12 17:57:00 -05:00
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for comment feed archives .
*
* Likely comments feed archives include / comments / feed / , and / comments / feed / atom /.
*
* @ since 1.5 . 0
*
* @ param array $comments_rewrite The rewrite rules for the site - wide comments feeds .
*/
$comments_rewrite = apply_filters ( 'comments_rewrite_rules' , $comments_rewrite );
// Search rewrite rules.
2006-03-12 17:57:00 -05:00
$search_structure = $this -> get_search_permastruct ();
$search_rewrite = $this -> generate_rewrite_rules ( $search_structure , EP_SEARCH );
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for search archives .
*
* Likely search - related archives include / search / search + query / as well as
* pagination and feed paths for a search .
*
* @ since 1.5 . 0
*
* @ param array $search_rewrite The rewrite rules for search queries .
*/
$search_rewrite = apply_filters ( 'search_rewrite_rules' , $search_rewrite );
// Author rewrite rules.
2006-03-12 17:57:00 -05:00
$author_rewrite = $this -> generate_rewrite_rules ( $this -> get_author_permastruct (), EP_AUTHORS );
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for author archives .
*
2014-03-01 21:50:14 -05:00
* Likely author archives would include / author / author - name / , as well as
2014-03-01 04:31:15 -05:00
* pagination and feed paths for author archives .
*
* @ since 1.5 . 0
*
* @ param array $author_rewrite The rewrite rules for author archives .
*/
$author_rewrite = apply_filters ( 'author_rewrite_rules' , $author_rewrite );
// Pages rewrite rules.
2006-03-12 17:57:00 -05:00
$page_rewrite = $this -> page_rewrite_rules ();
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for " page " post type archives .
*
* @ since 1.5 . 0
*
* @ param array $page_rewrite The rewrite rules for the " page " post type .
*/
$page_rewrite = apply_filters ( 'page_rewrite_rules' , $page_rewrite );
// Extra permastructs.
2012-01-24 13:27:18 -05:00
foreach ( $this -> extra_permastructs as $permastructname => $struct ) {
if ( is_array ( $struct ) ) {
if ( count ( $struct ) == 2 )
$rules = $this -> generate_rewrite_rules ( $struct [ 0 ], $struct [ 1 ] );
else
$rules = $this -> generate_rewrite_rules ( $struct [ 'struct' ], $struct [ 'ep_mask' ], $struct [ 'paged' ], $struct [ 'feed' ], $struct [ 'forcomments' ], $struct [ 'walk_dirs' ], $struct [ 'endpoints' ] );
} else {
$rules = $this -> generate_rewrite_rules ( $struct );
}
2010-10-17 01:41:22 -04:00
2014-03-01 04:31:15 -05:00
/**
* Filter rewrite rules used for individual permastructs .
*
2014-11-30 07:10:23 -05:00
* The dynamic portion of the hook name , `$permastructname` , refers
2014-03-01 04:31:15 -05:00
* to the name of the registered permastruct , e . g . 'post_tag' ( tags ),
* 'category' ( categories ), etc .
*
* @ since 3.1 . 0
*
* @ param array $rules The rewrite rules generated for the current permastruct .
*/
$rules = apply_filters ( $permastructname . '_rewrite_rules' , $rules );
if ( 'post_tag' == $permastructname ) {
/**
* Filter rewrite rules used specifically for Tags .
*
* @ since 2.3 . 0
* @ deprecated 3.1 . 0 Use 'post_tag_rewrite_rules' instead
*
* @ param array $rules The rewrite rules generated for tags .
*/
$rules = apply_filters ( 'tag_rewrite_rules' , $rules );
}
2010-10-17 01:41:22 -04:00
$this -> extra_rules_top = array_merge ( $this -> extra_rules_top , $rules );
2010-03-19 22:05:32 -04:00
}
2008-03-23 13:02:11 -04:00
2006-03-12 17:57:00 -05:00
// Put them together.
2007-12-03 19:19:10 -05:00
if ( $this -> use_verbose_page_rules )
2012-09-11 16:15:56 -04:00
$this -> rules = array_merge ( $this -> extra_rules_top , $robots_rewrite , $deprecated_files , $registration_pages , $root_rewrite , $comments_rewrite , $search_rewrite , $author_rewrite , $date_rewrite , $page_rewrite , $post_rewrite , $this -> extra_rules );
2007-12-03 19:19:10 -05:00
else
2012-09-11 16:15:56 -04:00
$this -> rules = array_merge ( $this -> extra_rules_top , $robots_rewrite , $deprecated_files , $registration_pages , $root_rewrite , $comments_rewrite , $search_rewrite , $author_rewrite , $date_rewrite , $post_rewrite , $page_rewrite , $this -> extra_rules );
2006-03-12 17:57:00 -05:00
2014-03-01 04:31:15 -05:00
/**
* Fires after the rewrite rules are generated .
*
* @ since 1.5 . 0
*
* @ param WP_Rewrite $this Current WP_Rewrite instance , passed by reference .
*/
do_action_ref_array ( 'generate_rewrite_rules' , array ( & $this ) );
/**
* Filter the full set of generated rewrite rules .
*
* @ since 1.5 . 0
*
* @ param array $this -> rules The compiled array of rewrite rules .
*/
$this -> rules = apply_filters ( 'rewrite_rules_array' , $this -> rules );
2006-03-12 17:57:00 -05:00
return $this -> rules ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Retrieve the rewrite rules .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* The difference between this method and { @ link
* WP_Rewrite :: rewrite_rules ()} is that this method stores the rewrite rules
* in the 'rewrite_rules' option and retrieves it . This prevents having to
* process all of the permalinks to get the rewrite rules in the form of
* caching .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ return array Rewrite rules .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function wp_rewrite_rules () {
2009-10-19 17:10:46 -04:00
$this -> rules = get_option ( 'rewrite_rules' );
2006-03-12 17:57:00 -05:00
if ( empty ( $this -> rules ) ) {
$this -> matches = 'matches' ;
$this -> rewrite_rules ();
2009-10-19 17:10:46 -04:00
update_option ( 'rewrite_rules' , $this -> rules );
2006-03-12 17:57:00 -05:00
}
return $this -> rules ;
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Retrieve mod_rewrite formatted rewrite rules to write to . htaccess .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* Does not actually write to the . htaccess file , but creates the rules for
* the process that will .
*
2012-01-25 16:44:02 -05:00
* Will add the non_wp_rules property rules to the . htaccess file before
2008-09-18 02:14:01 -04:00
* the WordPress rewrite rules one .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ return string
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function mod_rewrite_rules () {
2010-03-13 02:29:10 -05:00
if ( ! $this -> using_permalinks () )
2006-03-12 17:57:00 -05:00
return '' ;
2012-09-10 21:44:48 -04:00
$site_root = parse_url ( site_url () );
2010-03-13 02:29:10 -05:00
if ( isset ( $site_root [ 'path' ] ) )
2008-11-03 22:22:24 -05:00
$site_root = trailingslashit ( $site_root [ 'path' ]);
2006-03-12 17:57:00 -05:00
2010-01-04 12:23:29 -05:00
$home_root = parse_url ( home_url ());
2010-03-13 02:29:10 -05:00
if ( isset ( $home_root [ 'path' ] ) )
2008-11-03 22:22:24 -05:00
$home_root = trailingslashit ( $home_root [ 'path' ]);
2010-03-13 02:29:10 -05:00
else
2008-11-04 16:58:30 -05:00
$home_root = '/' ;
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
$rules = " <IfModule mod_rewrite.c> \n " ;
$rules .= " RewriteEngine On \n " ;
$rules .= " RewriteBase $home_root\n " ;
2010-03-11 18:33:01 -05:00
$rules .= " RewriteRule ^index \ .php $ - [L] \n " ; // Prevent -f checks on index.php.
2006-11-19 02:56:05 -05:00
2006-03-12 17:57:00 -05:00
//add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
2008-08-06 16:31:54 -04:00
foreach ( ( array ) $this -> non_wp_rules as $match => $query ) {
2006-03-12 17:57:00 -05:00
// Apache 1.3 does not support the reluctant (non-greedy) modifier.
$match = str_replace ( '.+?' , '.+' , $match );
// If the match is unanchored and greedy, prepend rewrite conditions
// to avoid infinite redirects and eclipsing of real files.
2010-03-13 02:29:10 -05:00
//if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
2006-03-12 17:57:00 -05:00
//nada.
2010-03-13 02:29:10 -05:00
//}
2006-03-12 17:57:00 -05:00
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L] \n " ;
}
2010-03-13 02:29:10 -05:00
if ( $this -> use_verbose_rules ) {
2006-03-12 17:57:00 -05:00
$this -> matches = '' ;
$rewrite = $this -> rewrite_rules ();
$num_rules = count ( $rewrite );
$rules .= " RewriteCond % { REQUEST_FILENAME} -f [OR] \n " .
" RewriteCond % { REQUEST_FILENAME} -d \n " .
" RewriteRule ^.* $ - [S= $num_rules ] \n " ;
2008-08-06 16:31:54 -04:00
foreach ( ( array ) $rewrite as $match => $query ) {
2006-03-12 17:57:00 -05:00
// Apache 1.3 does not support the reluctant (non-greedy) modifier.
$match = str_replace ( '.+?' , '.+' , $match );
// If the match is unanchored and greedy, prepend rewrite conditions
// to avoid infinite redirects and eclipsing of real files.
2010-03-13 02:29:10 -05:00
//if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
2006-03-12 17:57:00 -05:00
//nada.
2010-03-13 02:29:10 -05:00
//}
2006-03-12 17:57:00 -05:00
2010-03-13 02:29:10 -05:00
if ( strpos ( $query , $this -> index ) !== false )
2006-03-12 17:57:00 -05:00
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L] \n " ;
2010-03-13 02:29:10 -05:00
else
2006-03-12 17:57:00 -05:00
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L] \n " ;
}
} else {
$rules .= " RewriteCond % { REQUEST_FILENAME} !-f \n " .
" RewriteCond % { REQUEST_FILENAME} !-d \n " .
" RewriteRule . { $home_root } { $this -> index } [L] \n " ;
}
$rules .= " </IfModule> \n " ;
2014-03-01 04:31:15 -05:00
/**
*
* Filter the list of rewrite rules formatted for output to an . htaccess file .
*
* @ since 1.5 . 0
*
* @ param string $rules mod_rewrite Rewrite rules formatted for . htaccess .
*/
$rules = apply_filters ( 'mod_rewrite_rules' , $rules );
/**
* Filter the list of rewrite rules formatted for output to an . htaccess file .
*
* @ since 1.5 . 0
* @ deprecated 1.5 . 0 Use the mod_rewrite_rules filter instead .
*
* @ param string $rules mod_rewrite Rewrite rules formatted for . htaccess .
*/
$rules = apply_filters ( 'rewrite_rules' , $rules ); // Deprecated
2006-03-12 17:57:00 -05:00
return $rules ;
}
2009-05-24 19:47:49 -04:00
2009-05-15 22:04:36 -04:00
/**
* Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web . config file .
*
* Does not actually write to the web . config file , but creates the rules for
* the process that will .
*
2009-05-24 19:47:49 -04:00
* @ since 2.8 . 0
2009-05-15 22:04:36 -04:00
* @ access public
*
* @ return string
*/
2014-05-19 13:41:13 -04:00
public function iis7_url_rewrite_rules ( $add_parent_tags = false ) {
2009-05-24 19:47:49 -04:00
2010-03-13 02:29:10 -05:00
if ( ! $this -> using_permalinks () )
2009-05-15 22:04:36 -04:00
return '' ;
2010-05-25 01:12:47 -04:00
$rules = '' ;
if ( $add_parent_tags ) {
2010-06-02 10:58:36 -04:00
$rules .= ' < configuration >
< system . webServer >
< rewrite >
< rules > ' ;
2010-05-25 01:12:47 -04:00
}
2012-09-27 02:07:21 -04:00
$rules .= '
< rule name = " wordpress " patternSyntax = " Wildcard " >
< match url = " * " />
< conditions >
< add input = " { REQUEST_FILENAME} " matchType = " IsFile " negate = " true " />
< add input = " { REQUEST_FILENAME} " matchType = " IsDirectory " negate = " true " />
2010-06-02 10:58:36 -04:00
</ conditions >
2012-09-27 02:07:21 -04:00
< action type = " Rewrite " url = " index.php " />
</ rule > ' ;
2010-05-25 01:12:47 -04:00
if ( $add_parent_tags ) {
2010-05-25 11:31:28 -04:00
$rules .= '
2010-06-02 10:58:36 -04:00
</ rules >
</ rewrite >
</ system . webServer >
</ configuration > ' ;
2010-01-11 17:30:21 -05:00
}
2010-05-25 11:31:28 -04:00
2014-03-01 04:31:15 -05:00
/**
* Filter the list of rewrite rules formatted for output to a web . config .
*
* @ since 2.8 . 0
*
* @ param string $rules Rewrite rules formatted for IIS web . config .
*/
$rules = apply_filters ( 'iis7_url_rewrite_rules' , $rules );
2009-05-24 19:47:49 -04:00
2009-05-15 22:04:36 -04:00
return $rules ;
}
2006-11-19 02:56:05 -05:00
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Add a straight rewrite rule .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* Any value in the $after parameter that isn 't ' bottom ' will be placed at
* the top of the rules .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ param string $regex Regular expression to match against request .
* @ param string $redirect URL regex redirects to when regex matches request .
* @ param string $after Optional , default is bottom . Location to place rule .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function add_rule ( $regex , $redirect , $after = 'bottom' ) {
2006-03-12 17:57:00 -05:00
//get everything up to the first ?
$index = ( strpos ( $redirect , '?' ) == false ? strlen ( $redirect ) : strpos ( $redirect , '?' ));
$front = substr ( $redirect , 0 , $index );
2010-03-13 02:29:10 -05:00
if ( $front != $this -> index ) { //it doesn't redirect to WP's index.php
2006-03-12 17:57:00 -05:00
$this -> add_external_rule ( $regex , $redirect );
} else {
2007-06-27 17:39:50 -04:00
if ( 'bottom' == $after )
$this -> extra_rules = array_merge ( $this -> extra_rules , array ( $regex => $redirect ));
else
$this -> extra_rules_top = array_merge ( $this -> extra_rules_top , array ( $regex => $redirect ));
//$this->extra_rules[$regex] = $redirect;
2006-03-12 17:57:00 -05:00
}
}
2006-11-19 02:56:05 -05:00
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Add a rule that doesn ' t redirect to index . php .
2008-09-01 01:47:08 -04:00
*
2008-09-18 02:14:01 -04:00
* Can redirect to any place .
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ param string $regex Regular expression to match against request .
* @ param string $redirect URL regex redirects to when regex matches request .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function add_external_rule ( $regex , $redirect ) {
2006-03-12 17:57:00 -05:00
$this -> non_wp_rules [ $regex ] = $redirect ;
}
2006-11-19 02:56:05 -05:00
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Add an endpoint , like / trackback /.
2008-09-01 01:47:08 -04:00
*
* @ since 2.1 . 0
2014-02-28 15:55:14 -05:00
* @ since 3.9 . 0 $query_var parameter added .
2008-09-01 01:47:08 -04:00
* @ access public
2014-04-07 17:39:15 -04:00
*
* @ see add_rewrite_endpoint () for full documentation .
2012-01-25 16:58:23 -05:00
* @ uses WP :: add_query_var ()
2008-09-01 01:47:08 -04:00
*
2014-04-07 17:39:15 -04:00
* @ param string $name Name of the endpoint .
* @ param int $places Endpoint mask describing the places the endpoint should be added .
* @ param string $query_var Name of the corresponding query variable . Default is value of $name .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function add_endpoint ( $name , $places , $query_var = null ) {
2006-03-12 17:57:00 -05:00
global $wp ;
2014-02-28 15:55:14 -05:00
if ( null === $query_var ) {
$query_var = $name ;
}
$this -> endpoints [] = array ( $places , $name , $query_var );
$wp -> add_query_var ( $query_var );
2006-03-12 17:57:00 -05:00
}
2008-09-15 20:35:56 -04:00
/**
2012-01-24 13:27:18 -05:00
* Add a new permalink structure .
2008-09-15 20:35:56 -04:00
*
2012-01-24 13:27:18 -05:00
* A permalink structure ( permastruct ) is an abstract definition of a set of rewrite rules ; it
* is an easy way of expressing a set of regular expressions that rewrite to a set of query strings .
* The new permastruct is added to the { @ link WP_Rewrite :: $extra_permastructs } array . When the
* rewrite rules are built by { @ link WP_Rewrite :: rewrite_rules ()} all of these extra permastructs
* are passed to { @ link WP_Rewrite :: generate_rewrite_rules ()} which transforms them into the
* regular expressions that many love to hate .
*
* The $args parameter gives you control over how { @ link WP_Rewrite :: generate_rewrite_rules ()}
* works on the new permastruct .
2008-09-15 20:35:56 -04:00
*
2010-12-20 04:25:21 -05:00
* @ since 2.5 . 0
2008-09-15 20:35:56 -04:00
* @ access public
*
2008-09-18 02:14:01 -04:00
* @ param string $name Name for permalink structure .
2012-01-24 13:27:18 -05:00
* @ param string $struct Permalink structure ( e . g . category /% category % )
* @ param array $args Optional configuration for building the rules from the permalink structure :
* - with_front ( bool ) - Should the structure be prepended with WP_Rewrite :: $front ? Default is true .
* - ep_mask ( int ) - Endpoint mask defining what endpoints are added to the structure . Default is EP_NONE .
* - paged ( bool ) - Should archive pagination rules be added for the structure ? Default is true .
* - feed ( bool ) - Should feed rewrite rules be added for the structure ? Default is true .
* - forcomments ( bool ) - Should the feed rules be a query for a comments feed ? Default is false .
* - walk_dirs ( bool ) - Should the 'directories' making up the structure be walked over and rewrite
* rules built for each in turn ? Default is true .
* - endpoints ( bool ) - Should endpoints be applied to the generated rewrite rules ? Default is true .
2008-09-15 20:35:56 -04:00
*/
2014-05-19 13:41:13 -04:00
public function add_permastruct ( $name , $struct , $args = array () ) {
2012-01-24 13:27:18 -05:00
// backwards compatibility for the old parameters: $with_front and $ep_mask
if ( ! is_array ( $args ) )
$args = array ( 'with_front' => $args );
if ( func_num_args () == 4 )
$args [ 'ep_mask' ] = func_get_arg ( 3 );
$defaults = array (
'with_front' => true ,
'ep_mask' => EP_NONE ,
'paged' => true ,
'feed' => true ,
'forcomments' => false ,
'walk_dirs' => true ,
'endpoints' => true ,
);
2012-01-28 16:00:59 -05:00
$args = array_intersect_key ( $args , $defaults );
2012-01-24 13:27:18 -05:00
$args = wp_parse_args ( $args , $defaults );
if ( $args [ 'with_front' ] )
2008-03-23 13:02:11 -04:00
$struct = $this -> front . $struct ;
2011-03-09 23:24:48 -05:00
else
$struct = $this -> root . $struct ;
2012-01-24 13:27:18 -05:00
$args [ 'struct' ] = $struct ;
$this -> extra_permastructs [ $name ] = $args ;
2008-03-23 13:02:11 -04:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Remove rewrite rules and then recreate rewrite rules .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Calls { @ link WP_Rewrite :: wp_rewrite_rules ()} after removing the
* 'rewrite_rules' option . If the function named 'save_mod_rewrite_rules'
* exists , it will be called .
2008-09-01 01:47:08 -04:00
*
* @ since 2.0 . 1
* @ access public
2010-09-07 07:21:11 -04:00
* @ param bool $hard Whether to update . htaccess ( hard flush ) or just update rewrite_rules option ( soft flush ) . Default is true ( hard ) .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function flush_rules ( $hard = true ) {
2015-01-08 17:00:23 -05:00
// Prevent this action from running before everyone has registered their rewrites
if ( ! did_action ( 'wp_loaded' ) ) {
add_action ( 'wp_loaded' , array ( $this , 'flush_rules' ) );
return ;
}
2009-10-19 17:10:46 -04:00
delete_option ( 'rewrite_rules' );
2006-03-12 17:57:00 -05:00
$this -> wp_rewrite_rules ();
2013-09-16 18:00:09 -04:00
/**
* Filter whether a " hard " rewrite rule flush should be performed when requested .
*
* A " hard " flush updates . htaccess ( Apache ) or web . config ( IIS ) .
*
* @ since 3.7 . 0
2014-03-01 04:31:15 -05:00
*
* @ param bool $hard Whether to flush rewrite rules " hard " . Default true .
2013-09-16 18:00:09 -04:00
*/
2014-03-01 04:31:15 -05:00
if ( ! $hard || ! apply_filters ( 'flush_rewrite_rules_hard' , true ) ) {
2013-09-10 18:19:09 -04:00
return ;
2014-03-01 04:31:15 -05:00
}
2013-09-10 18:19:09 -04:00
if ( function_exists ( 'save_mod_rewrite_rules' ) )
2006-03-12 17:57:00 -05:00
save_mod_rewrite_rules ();
2013-09-10 18:19:09 -04:00
if ( function_exists ( 'iis7_save_url_rewrite_rules' ) )
2009-05-15 22:04:36 -04:00
iis7_save_url_rewrite_rules ();
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-18 02:14:01 -04:00
* Sets up the object ' s properties .
2008-09-01 01:47:08 -04:00
*
2009-01-29 17:11:27 -05:00
* The 'use_verbose_page_rules' object property will be set to true if the
* permalink structure begins with one of the following : '%postname%' , '%category%' ,
2008-09-18 02:14:01 -04:00
* '%tag%' , or '%author%' .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*/
2014-05-19 13:41:13 -04:00
public function init () {
2006-03-12 17:57:00 -05:00
$this -> extra_rules = $this -> non_wp_rules = $this -> endpoints = array ();
2006-08-30 17:46:31 -04:00
$this -> permalink_structure = get_option ( 'permalink_structure' );
2006-03-12 17:57:00 -05:00
$this -> front = substr ( $this -> permalink_structure , 0 , strpos ( $this -> permalink_structure , '%' ));
$this -> root = '' ;
2010-03-13 02:29:10 -05:00
if ( $this -> using_index_permalinks () )
2006-03-12 17:57:00 -05:00
$this -> root = $this -> index . '/' ;
unset ( $this -> author_structure );
unset ( $this -> date_structure );
unset ( $this -> page_structure );
unset ( $this -> search_structure );
unset ( $this -> feed_structure );
unset ( $this -> comment_feed_structure );
2010-03-19 17:15:00 -04:00
$this -> use_trailing_slashes = ( '/' == substr ( $this -> permalink_structure , - 1 , 1 ) );
2007-12-17 01:29:30 -05:00
2011-01-05 22:59:22 -05:00
// Enable generic rules for pages if permalink structure doesn't begin with a wildcard.
if ( preg_match ( " /^[^%]*%(?:postname|category|tag|author)%/ " , $this -> permalink_structure ) )
$this -> use_verbose_page_rules = true ;
else
$this -> use_verbose_page_rules = false ;
2006-03-12 17:57:00 -05:00
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Set the main permalink structure for the blog .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Will update the 'permalink_structure' option , if there is a difference
* between the current permalink structure and the parameter value . Calls
* { @ link WP_Rewrite :: init ()} after the option is updated .
2009-05-24 19:47:49 -04:00
*
2009-05-14 02:38:34 -04:00
* Fires the 'permalink_structure_changed' action once the init call has
* processed passing the old and new values
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ param string $permalink_structure Permalink structure .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function set_permalink_structure ( $permalink_structure ) {
2010-03-13 02:29:10 -05:00
if ( $permalink_structure != $this -> permalink_structure ) {
2012-11-06 07:41:33 -05:00
$old_permalink_structure = $this -> permalink_structure ;
2006-03-12 17:57:00 -05:00
update_option ( 'permalink_structure' , $permalink_structure );
$this -> init ();
2014-03-01 04:31:15 -05:00
/**
* Fires after the permalink structure is updated .
*
* @ since 2.8 . 0
*
* @ param string $old_permalink_structure The previous permalink structure .
* @ param string $permalink_structure The new permalink structure .
*/
do_action ( 'permalink_structure_changed' , $old_permalink_structure , $permalink_structure );
2006-03-12 17:57:00 -05:00
}
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Set the category base for the category permalink .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Will update the 'category_base' option , if there is a difference between
* the current category base and the parameter value . Calls
* { @ link WP_Rewrite :: init ()} after the option is updated .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ param string $category_base Category permalink structure base .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function set_category_base ( $category_base ) {
2010-10-17 01:41:22 -04:00
if ( $category_base != get_option ( 'category_base' ) ) {
2006-03-12 17:57:00 -05:00
update_option ( 'category_base' , $category_base );
$this -> init ();
}
}
2008-09-01 01:47:08 -04:00
/**
2008-09-15 20:35:56 -04:00
* Set the tag base for the tag permalink .
2008-09-01 01:47:08 -04:00
*
2008-09-15 20:35:56 -04:00
* Will update the 'tag_base' option , if there is a difference between the
* current tag base and the parameter value . Calls
* { @ link WP_Rewrite :: init ()} after the option is updated .
2008-09-01 01:47:08 -04:00
*
* @ since 2.3 . 0
* @ access public
*
2008-09-15 20:35:56 -04:00
* @ param string $tag_base Tag permalink structure base .
2008-09-01 01:47:08 -04:00
*/
2014-05-19 13:41:13 -04:00
public function set_tag_base ( $tag_base ) {
2010-10-17 01:41:22 -04:00
if ( $tag_base != get_option ( 'tag_base' ) ) {
2007-03-31 05:19:32 -04:00
update_option ( 'tag_base' , $tag_base );
$this -> init ();
}
}
2008-09-01 01:47:08 -04:00
/**
2011-04-29 16:05:12 -04:00
* Constructor - Calls init (), which runs setup .
2008-09-01 01:47:08 -04:00
*
* @ since 1.5 . 0
* @ access public
*
*/
2014-05-19 13:41:13 -04:00
public function __construct () {
2006-03-12 17:57:00 -05:00
$this -> init ();
}
}