WordPress.org

Make WordPress Core

Changeset 19743


Ignore:
Timestamp:
01/24/12 18:27:18 (2 years ago)
Author:
duck_
Message:

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().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/rewrite.php

    r19737 r19743  
    5353 * @param string $name Name for permalink structure. 
    5454 * @param string $struct Permalink structure. 
    55  * @param bool $with_front Prepend front base to permalink structure. 
    56  */ 
    57 function add_permastruct( $name, $struct, $with_front = true, $ep_mask = EP_NONE ) { 
     55 * @param array $args Optional configuration for building the rules from the permalink structure, 
     56 *     see {@link WP_Rewrite::add_permastruct()} for full details. 
     57 */ 
     58function add_permastruct( $name, $struct, $args = array() ) { 
    5859    global $wp_rewrite; 
    59     return $wp_rewrite->add_permastruct( $name, $struct, $with_front, $ep_mask ); 
     60 
     61    // backwards compatibility for the old parameters: $with_front and $ep_mask 
     62    if ( ! is_array( $args ) ) 
     63        $args = array( 'with_front' => $args ); 
     64    if ( func_num_args() == 4 ) 
     65        $args['ep_mask'] = func_get_arg( 3 ); 
     66 
     67    return $wp_rewrite->add_permastruct( $name, $struct, $args ); 
    6068} 
    6169 
     
    9941002 
    9951003        if ( isset($this->extra_permastructs[$name]) ) 
    996             return $this->extra_permastructs[$name][0]; 
     1004            return $this->extra_permastructs[$name]['struct']; 
    9971005 
    9981006        return false; 
     
    15191527 
    15201528        // Extra permastructs 
    1521         foreach ( $this->extra_permastructs as $permastructname => $permastruct ) { 
    1522             if ( is_array($permastruct) ) 
    1523                 $rules = $this->generate_rewrite_rules($permastruct[0], $permastruct[1]); 
    1524             else 
    1525                 $rules = $this->generate_rewrite_rules($permastruct, EP_NONE); 
     1529        foreach ( $this->extra_permastructs as $permastructname => $struct ) { 
     1530            if ( is_array( $struct ) ) { 
     1531                if ( count( $struct ) == 2 ) 
     1532                    $rules = $this->generate_rewrite_rules( $struct[0], $struct[1] ); 
     1533                else 
     1534                    $rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] ); 
     1535            } else { 
     1536                $rules = $this->generate_rewrite_rules( $struct ); 
     1537            } 
    15261538 
    15271539            $rules = apply_filters($permastructname . '_rewrite_rules', $rules); 
     
    18181830 
    18191831    /** 
    1820      * Add permalink structure. 
    1821      * 
    1822      * These are added along with the extra rewrite rules that are merged to the 
    1823      * top. 
     1832     * Add a new permalink structure. 
     1833     * 
     1834     * A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; it 
     1835     * is an easy way of expressing a set of regular expressions that rewrite to a set of query strings. 
     1836     * The new permastruct is added to the {@link WP_Rewrite::$extra_permastructs} array. When the 
     1837     * rewrite rules are built by {@link WP_Rewrite::rewrite_rules()} all of these extra permastructs 
     1838     * are passed to {@link WP_Rewrite::generate_rewrite_rules()} which transforms them into the 
     1839     * regular expressions that many love to hate. 
     1840     * 
     1841     * The $args parameter gives you control over how {@link WP_Rewrite::generate_rewrite_rules()} 
     1842     * works on the new permastruct. 
    18241843     * 
    18251844     * @since 2.5.0 
     
    18271846     * 
    18281847     * @param string $name Name for permalink structure. 
    1829      * @param string $struct Permalink structure. 
    1830      * @param bool $with_front Prepend front base to permalink structure. 
    1831      */ 
    1832     function add_permastruct($name, $struct, $with_front = true, $ep_mask = EP_NONE) { 
    1833         if ( $with_front ) 
     1848     * @param string $struct Permalink structure (e.g. category/%category%) 
     1849     * @param array $args Optional configuration for building the rules from the permalink structure: 
     1850     *     - with_front (bool) - Should the structure be prepended with WP_Rewrite::$front? Default is true. 
     1851     *     - ep_mask (int) - Endpoint mask defining what endpoints are added to the structure. Default is EP_NONE. 
     1852     *     - paged (bool) - Should archive pagination rules be added for the structure? Default is true. 
     1853     *     - feed (bool) - Should feed rewrite rules be added for the structure? Default is true. 
     1854     *     - forcomments (bool) - Should the feed rules be a query for a comments feed? Default is false. 
     1855     *     - walk_dirs (bool) - Should the 'directories' making up the structure be walked over and rewrite 
     1856     *                          rules built for each in turn? Default is true. 
     1857     *     - endpoints (bool) - Should endpoints be applied to the generated rewrite rules? Default is true. 
     1858     */ 
     1859    function add_permastruct( $name, $struct, $args = array() ) { 
     1860        // backwards compatibility for the old parameters: $with_front and $ep_mask 
     1861        if ( ! is_array( $args ) ) 
     1862            $args = array( 'with_front' => $args ); 
     1863        if ( func_num_args() == 4 ) 
     1864            $args['ep_mask'] = func_get_arg( 3 ); 
     1865 
     1866        $defaults = array( 
     1867            'with_front' => true, 
     1868            'ep_mask' => EP_NONE, 
     1869            'paged' => true, 
     1870            'feed' => true, 
     1871            'forcomments' => false, 
     1872            'walk_dirs' => true, 
     1873            'endpoints' => true, 
     1874        ); 
     1875        $args = wp_parse_args( $args, $defaults ); 
     1876 
     1877        if ( $args['with_front'] ) 
    18341878            $struct = $this->front . $struct; 
    18351879        else 
    18361880            $struct = $this->root . $struct; 
    1837         $this->extra_permastructs[$name] = array($struct, $ep_mask); 
     1881        $args['struct'] = $struct; 
     1882 
     1883        $this->extra_permastructs[ $name ] = $args; 
    18381884    } 
    18391885 
Note: See TracChangeset for help on using the changeset viewer.