Upgrade add_permastruct() to allow more control over WP_Rewrite::generate_rewrite_rules(). See #16092.

The third argument is now a configuration array that mirrors the parameters of generate_rewrite_rules()
and allows for add_permastruct() specific args (i.e. with_front). The full configuration is stored in
WP_Rewrite::$extra_permastructs to be used by WP_Rewrite::rewrite_rules().


git-svn-id: https://develop.svn.wordpress.org/trunk@19743 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jon Cave 2012-01-24 18:27:18 +00:00
parent 28cfc3cd49
commit a9b6163795
1 changed files with 63 additions and 17 deletions

View File

@ -52,11 +52,19 @@ function add_rewrite_tag($tagname, $regex) {
*
* @param string $name Name for permalink structure.
* @param string $struct Permalink structure.
* @param bool $with_front Prepend front base to permalink structure.
* @param array $args Optional configuration for building the rules from the permalink structure,
* see {@link WP_Rewrite::add_permastruct()} for full details.
*/
function add_permastruct( $name, $struct, $with_front = true, $ep_mask = EP_NONE ) {
function add_permastruct( $name, $struct, $args = array() ) {
global $wp_rewrite;
return $wp_rewrite->add_permastruct( $name, $struct, $with_front, $ep_mask );
// 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 );
}
/**
@ -993,7 +1001,7 @@ class WP_Rewrite {
return false;
if ( isset($this->extra_permastructs[$name]) )
return $this->extra_permastructs[$name][0];
return $this->extra_permastructs[$name]['struct'];
return false;
}
@ -1518,11 +1526,15 @@ class WP_Rewrite {
$page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
// Extra permastructs
foreach ( $this->extra_permastructs as $permastructname => $permastruct ) {
if ( is_array($permastruct) )
$rules = $this->generate_rewrite_rules($permastruct[0], $permastruct[1]);
else
$rules = $this->generate_rewrite_rules($permastruct, EP_NONE);
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 );
}
$rules = apply_filters($permastructname . '_rewrite_rules', $rules);
if ( 'post_tag' == $permastructname )
@ -1817,24 +1829,58 @@ class WP_Rewrite {
}
/**
* Add permalink structure.
* Add a new permalink structure.
*
* These are added along with the extra rewrite rules that are merged to the
* top.
* 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.
*
* @since 2.5.0
* @access public
*
* @param string $name Name for permalink structure.
* @param string $struct Permalink structure.
* @param bool $with_front Prepend front base to permalink structure.
* @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.
*/
function add_permastruct($name, $struct, $with_front = true, $ep_mask = EP_NONE) {
if ( $with_front )
function add_permastruct( $name, $struct, $args = array() ) {
// 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,
);
$args = wp_parse_args( $args, $defaults );
if ( $args['with_front'] )
$struct = $this->front . $struct;
else
$struct = $this->root . $struct;
$this->extra_permastructs[$name] = array($struct, $ep_mask);
$args['struct'] = $struct;
$this->extra_permastructs[ $name ] = $args;
}
/**