]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/rewrite.php
WordPress 4.2
[autoinstalls/wordpress.git] / wp-includes / rewrite.php
index 0de3b3e34db5dd4176b776c15f4dd100a3b256c4..45d28cea97a072773e0c25cf08299b67068bcbf2 100644 (file)
@@ -226,22 +226,20 @@ define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEAR
  * Adding an endpoint creates extra rewrite rules for each of the matching
  * places specified by the provided bitmask. For example:
  *
- * <code>
- * add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
- * </code>
+ *     add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
  *
  * 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
- * "<permalink>/json/foo/").
+ * "[permalink]/json/foo/").
  *
  * 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
- * guaranteed to remain static (especially EP_ALL).
+ * guaranteed to remain static (especially `EP_ALL`).
  *
- * Be sure to flush the rewrite rules - flush_rewrite_rules() - when your plugin gets
+ * Be sure to flush the rewrite rules - {@see flush_rewrite_rules()} - when your plugin gets
  * activated and deactivated.
  *
  * @since 2.1.0
@@ -365,7 +363,7 @@ function url_to_postid($url) {
                if ( preg_match("#^$match#", $request_match, $matches) ) {
 
                        if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
-                               // this is a verbose page match, lets check to be sure about it
+                               // This is a verbose page match, let's check to be sure about it.
                                if ( ! get_page_by_path( $matches[ $varmatch[1] ] ) )
                                        continue;
                        }
@@ -422,19 +420,17 @@ class WP_Rewrite {
         * Permalink structure for posts.
         *
         * @since 1.5.0
-        * @access private
         * @var string
         */
-       var $permalink_structure;
+       public $permalink_structure;
 
        /**
         * Whether to add trailing slashes.
         *
         * @since 2.2.0
-        * @access private
         * @var bool
         */
-       var $use_trailing_slashes;
+       public $use_trailing_slashes;
 
        /**
         * Base for the author permalink structure (example.com/$author_base/authorname).
@@ -503,10 +499,18 @@ class WP_Rewrite {
         * Pagination permalink base.
         *
         * @since 3.1.0
+        * @var string
+        */
+       public $pagination_base = 'page';
+
+       /**
+        * Comments pagination permalink base.
+        *
+        * @since 4.2.0
         * @access private
         * @var string
         */
-       var $pagination_base = 'page';
+       var $comments_pagination_base = 'comment-page';
 
        /**
         * Feed permalink base.
@@ -524,7 +528,7 @@ class WP_Rewrite {
         * @access private
         * @var string
         */
-       var $comments_feed_structure;
+       var $comment_feed_structure;
 
        /**
         * Feed request permalink structure.
@@ -544,10 +548,9 @@ class WP_Rewrite {
         *
         * @see WP_Rewrite::init()
         * @since 1.5.0
-        * @access private
         * @var string
         */
-       var $front;
+       public $front;
 
        /**
         * The prefix for all permalink structures.
@@ -559,10 +562,9 @@ class WP_Rewrite {
         * @see WP_Rewrite::init()
         * @see WP_Rewrite::using_index_permalinks()
         * @since 1.5.0
-        * @access private
         * @var string
         */
-       var $root = '';
+       public $root = '';
 
        /**
         * The name of the index file which is the entry point to all requests.
@@ -571,7 +573,7 @@ class WP_Rewrite {
         * @access public
         * @var string
         */
-       var $index = 'index.php';
+       public $index = 'index.php';
 
        /**
         * Variable name to use for regex matches in the rewritten query.
@@ -654,7 +656,7 @@ class WP_Rewrite {
         * @access public
         * @var bool
         */
-       var $use_verbose_rules = false;
+       public $use_verbose_rules = false;
 
        /**
         * Could post permalinks be confused with those of pages?
@@ -666,13 +668,13 @@ class WP_Rewrite {
         * Now it tells {@link WP::parse_request()} to check if a URL matching the
         * page permastruct is actually a page before accepting it.
         *
-        * @link http://core.trac.wordpress.org/ticket/16687
+        * @link https://core.trac.wordpress.org/ticket/16687
         * @see WP_Rewrite::init()
         * @since 2.5.0
         * @access public
         * @var bool
         */
-       var $use_verbose_page_rules = true;
+       public $use_verbose_page_rules = true;
 
        /**
         * Rewrite tags that can be used in permalink structures.
@@ -748,10 +750,9 @@ class WP_Rewrite {
         * Supported default feeds.
         *
         * @since 1.5.0
-        * @access private
         * @var array
         */
-       var $feeds = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
+       public $feeds = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
 
        /**
         * Whether permalinks are being used.
@@ -763,7 +764,7 @@ class WP_Rewrite {
         *
         * @return bool True, if permalinks are enabled.
         */
-       function using_permalinks() {
+       public function using_permalinks() {
                return ! empty($this->permalink_structure);
        }
 
@@ -777,7 +778,7 @@ class WP_Rewrite {
         *
         * @return bool
         */
-       function using_index_permalinks() {
+       public function using_index_permalinks() {
                if ( empty($this->permalink_structure) )
                        return false;
 
@@ -798,7 +799,7 @@ class WP_Rewrite {
         *
         * @return bool
         */
-       function using_mod_rewrite_permalinks() {
+       public function using_mod_rewrite_permalinks() {
                if ( $this->using_permalinks() && ! $this->using_index_permalinks() )
                        return true;
                else
@@ -821,7 +822,7 @@ class WP_Rewrite {
         * @param int $number Index number.
         * @return string
         */
-       function preg_index($number) {
+       public function preg_index($number) {
                $match_prefix = '$';
                $match_suffix = '';
 
@@ -844,7 +845,7 @@ class WP_Rewrite {
         *
         * @return array Array of page URIs as first element and attachment URIs as second element.
         */
-       function page_uri_index() {
+       public function page_uri_index() {
                global $wpdb;
 
                //get pages in order of hierarchy, i.e. children after parents
@@ -886,7 +887,7 @@ class WP_Rewrite {
         *
         * @return array
         */
-       function page_rewrite_rules() {
+       public function page_rewrite_rules() {
                // the extra .? at the beginning prevents clashes with other regular expressions in the rules array
                $this->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' );
 
@@ -911,9 +912,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return bool|string False on no permalink structure. Date permalink structure.
+        * @return string|false False on no permalink structure. Date permalink structure.
         */
-       function get_date_permastruct() {
+       public function get_date_permastruct() {
                if ( isset($this->date_structure) )
                        return $this->date_structure;
 
@@ -965,9 +966,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return bool|string False on failure. Year structure on success.
+        * @return false|string False on failure. Year structure on success.
         */
-       function get_year_permastruct() {
+       public function get_year_permastruct() {
                $structure = $this->get_date_permastruct();
 
                if ( empty($structure) )
@@ -990,9 +991,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return bool|string False on failure. Year/Month structure on success.
+        * @return false|string False on failure. Year/Month structure on success.
         */
-       function get_month_permastruct() {
+       public function get_month_permastruct() {
                $structure = $this->get_date_permastruct();
 
                if ( empty($structure) )
@@ -1013,9 +1014,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return bool|string False on failure. Year/Month/Day structure on success.
+        * @return string|false False on failure. Year/Month/Day structure on success.
         */
-       function get_day_permastruct() {
+       public function get_day_permastruct() {
                return $this->get_date_permastruct();
        }
 
@@ -1032,7 +1033,7 @@ class WP_Rewrite {
         *
         * @return bool|string False on failure. Category permalink structure.
         */
-       function get_category_permastruct() {
+       public function get_category_permastruct() {
                return $this->get_extra_permastruct('category');
        }
 
@@ -1049,7 +1050,7 @@ class WP_Rewrite {
         *
         * @return bool|string False on failure. Tag permalink structure.
         */
-       function get_tag_permastruct() {
+       public function get_tag_permastruct() {
                return $this->get_extra_permastruct('post_tag');
        }
 
@@ -1062,7 +1063,7 @@ class WP_Rewrite {
         * @param string $name Permalink structure name.
         * @return string|bool False if not found. Permalink structure string.
         */
-       function get_extra_permastruct($name) {
+       public function get_extra_permastruct($name) {
                if ( empty($this->permalink_structure) )
                        return false;
 
@@ -1082,9 +1083,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return string|bool False if not found. Permalink structure string.
+        * @return string|false False if not found. Permalink structure string.
         */
-       function get_author_permastruct() {
+       public function get_author_permastruct() {
                if ( isset($this->author_structure) )
                        return $this->author_structure;
 
@@ -1108,9 +1109,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return string|bool False if not found. Permalink structure string.
+        * @return string|false False if not found. Permalink structure string.
         */
-       function get_search_permastruct() {
+       public function get_search_permastruct() {
                if ( isset($this->search_structure) )
                        return $this->search_structure;
 
@@ -1134,9 +1135,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return string|bool False if not found. Permalink structure string.
+        * @return string|false False if not found. Permalink structure string.
         */
-       function get_page_permastruct() {
+       public function get_page_permastruct() {
                if ( isset($this->page_structure) )
                        return $this->page_structure;
 
@@ -1160,9 +1161,9 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return string|bool False if not found. Permalink structure string.
+        * @return string|false False if not found. Permalink structure string.
         */
-       function get_feed_permastruct() {
+       public function get_feed_permastruct() {
                if ( isset($this->feed_structure) )
                        return $this->feed_structure;
 
@@ -1188,7 +1189,7 @@ class WP_Rewrite {
         *
         * @return string|bool False if not found. Permalink structure string.
         */
-       function get_comment_feed_permastruct() {
+       public function get_comment_feed_permastruct() {
                if ( isset($this->comment_feed_structure) )
                        return $this->comment_feed_structure;
 
@@ -1218,7 +1219,7 @@ class WP_Rewrite {
         * @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 '='.
         */
-       function add_rewrite_tag( $tag, $regex, $query ) {
+       public function add_rewrite_tag( $tag, $regex, $query ) {
                $position = array_search( $tag, $this->rewritecode );
                if ( false !== $position && null !== $position ) {
                        $this->rewritereplace[ $position ] = $regex;
@@ -1250,7 +1251,7 @@ class WP_Rewrite {
         * @param bool $endpoints Should endpoints be applied to the generated rewrite rules? Default is true.
         * @return array Rewrite rule list.
         */
-       function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
+       public function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
                //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
                $feedregex2 = '';
                foreach ( (array) $this->feeds as $feed_name)
@@ -1264,7 +1265,7 @@ class WP_Rewrite {
                //build a regex to match the trackback and page/xx parts of URLs
                $trackbackregex = 'trackback/?$';
                $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
-               $commentregex = 'comment-page-([0-9]{1,})/?$';
+               $commentregex = $this->comments_pagination_base . '-([0-9]{1,})/?$';
 
                //build up an array of endpoint regexes to append => queries to append
                if ( $endpoints ) {
@@ -1290,6 +1291,7 @@ class WP_Rewrite {
                $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
+               $queries = array();
                for ( $i = 0; $i < $num_tokens; ++$i ) {
                        if ( 0 < $i )
                                $queries[$i] = $queries[$i - 1] . '&';
@@ -1330,7 +1332,7 @@ class WP_Rewrite {
                        $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
 
                        //get the 'tagname=$matches[i]'
-                       $query = ( isset($queries) && is_array($queries) && !empty($num_toks) ) ? $queries[$num_toks - 1] : '';
+                       $query = ( ! empty( $num_toks ) && isset( $queries[$num_toks - 1] ) ) ? $queries[$num_toks - 1] : '';
 
                        //set up $ep_mask_specific which is used to match more specific URL types
                        switch ( $dirs[$j] ) {
@@ -1383,11 +1385,11 @@ class WP_Rewrite {
                                $rewrite = array_merge($rewrite, array($pagematch => $pagequery));
 
                        //only on pages with comments add ../comment-page-xx/
-                       if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask )
+                       if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) {
                                $rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
-                       else if ( EP_ROOT & $ep_mask && get_option('page_on_front') )
+                       } elseif ( EP_ROOT & $ep_mask && get_option('page_on_front') ) {
                                $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
-
+                       }
                        //do endpoints
                        if ( $endpoints ) {
                                foreach ( (array) $ep_query_append as $regex => $ep) {
@@ -1520,7 +1522,7 @@ class WP_Rewrite {
         * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
         * @return array
         */
-       function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
+       public function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
                return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
        }
 
@@ -1542,7 +1544,7 @@ class WP_Rewrite {
         *
         * @return array An associate array of matches and queries.
         */
-       function rewrite_rules() {
+       public function rewrite_rules() {
                $rewrite = array();
 
                if ( empty($this->permalink_structure) )
@@ -1678,7 +1680,7 @@ class WP_Rewrite {
                        /**
                         * Filter rewrite rules used for individual permastructs.
                         *
-                        * The dynamic portion of the hook name, $permastructname, refers
+                        * The dynamic portion of the hook name, `$permastructname`, refers
                         * to the name of the registered permastruct, e.g. 'post_tag' (tags),
                         * 'category' (categories), etc.
                         *
@@ -1744,7 +1746,7 @@ class WP_Rewrite {
         *
         * @return array Rewrite rules.
         */
-       function wp_rewrite_rules() {
+       public function wp_rewrite_rules() {
                $this->rules = get_option('rewrite_rules');
                if ( empty($this->rules) ) {
                        $this->matches = 'matches';
@@ -1769,7 +1771,7 @@ class WP_Rewrite {
         *
         * @return string
         */
-       function mod_rewrite_rules() {
+       public function mod_rewrite_rules() {
                if ( ! $this->using_permalinks() )
                        return '';
 
@@ -1867,7 +1869,7 @@ class WP_Rewrite {
         *
         * @return string
         */
-       function iis7_url_rewrite_rules( $add_parent_tags = false ) {
+       public function iis7_url_rewrite_rules( $add_parent_tags = false ) {
 
                if ( ! $this->using_permalinks() )
                        return '';
@@ -1922,7 +1924,7 @@ class WP_Rewrite {
         * @param string $redirect URL regex redirects to when regex matches request.
         * @param string $after Optional, default is bottom. Location to place rule.
         */
-       function add_rule($regex, $redirect, $after = 'bottom') {
+       public function add_rule($regex, $redirect, $after = 'bottom') {
                //get everything up to the first ?
                $index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?'));
                $front = substr($redirect, 0, $index);
@@ -1948,7 +1950,7 @@ class WP_Rewrite {
         * @param string $regex Regular expression to match against request.
         * @param string $redirect URL regex redirects to when regex matches request.
         */
-       function add_external_rule($regex, $redirect) {
+       public function add_external_rule($regex, $redirect) {
                $this->non_wp_rules[$regex] = $redirect;
        }
 
@@ -1966,7 +1968,7 @@ class WP_Rewrite {
         * @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.
         */
-       function add_endpoint( $name, $places, $query_var = null ) {
+       public function add_endpoint( $name, $places, $query_var = null ) {
                global $wp;
                if ( null === $query_var ) {
                        $query_var = $name;
@@ -2003,7 +2005,7 @@ class WP_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, $args = array() ) {
+       public 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 );
@@ -2042,7 +2044,21 @@ class WP_Rewrite {
         * @access public
         * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard).
         */
-       function flush_rules($hard = true) {
+       public function flush_rules( $hard = true ) {
+               static $do_hard_later;
+
+               // Prevent this action from running before everyone has registered their rewrites
+               if ( ! did_action( 'wp_loaded' ) ) {
+                       add_action( 'wp_loaded', array( $this, 'flush_rules' ) );
+                       $do_hard_later = ( isset( $do_hard_later ) ) ? $do_hard_later || $hard : $hard;
+                       return;
+               }
+
+               if ( isset( $do_hard_later ) ) {
+                       $hard = $do_hard_later;
+                       unset( $do_hard_later );
+               }
+
                delete_option('rewrite_rules');
                $this->wp_rewrite_rules();
                /**
@@ -2073,7 +2089,7 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         */
-       function init() {
+       public function init() {
                $this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
                $this->permalink_structure = get_option('permalink_structure');
                $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
@@ -2110,7 +2126,7 @@ class WP_Rewrite {
         *
         * @param string $permalink_structure Permalink structure.
         */
-       function set_permalink_structure($permalink_structure) {
+       public function set_permalink_structure($permalink_structure) {
                if ( $permalink_structure != $this->permalink_structure ) {
                        $old_permalink_structure = $this->permalink_structure;
                        update_option('permalink_structure', $permalink_structure);
@@ -2140,7 +2156,7 @@ class WP_Rewrite {
         *
         * @param string $category_base Category permalink structure base.
         */
-       function set_category_base($category_base) {
+       public function set_category_base($category_base) {
                if ( $category_base != get_option('category_base') ) {
                        update_option('category_base', $category_base);
                        $this->init();
@@ -2159,7 +2175,7 @@ class WP_Rewrite {
         *
         * @param string $tag_base Tag permalink structure base.
         */
-       function set_tag_base( $tag_base ) {
+       public function set_tag_base( $tag_base ) {
                if ( $tag_base != get_option( 'tag_base') ) {
                        update_option( 'tag_base', $tag_base );
                        $this->init();
@@ -2172,9 +2188,8 @@ class WP_Rewrite {
         * @since 1.5.0
         * @access public
         *
-        * @return WP_Rewrite
         */
-       function __construct() {
+       public function __construct() {
                $this->init();
        }
 }