WP_Rewrite

Core class used to implement a rewrite component API.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-rewrite.php  
  1. class WP_Rewrite { 
  2. /** 
  3. * Permalink structure for posts. 
  4. * @since 1.5.0 
  5. * @var string 
  6. */ 
  7. public $permalink_structure; 
  8.  
  9. /** 
  10. * Whether to add trailing slashes. 
  11. * @since 2.2.0 
  12. * @var bool 
  13. */ 
  14. public $use_trailing_slashes; 
  15.  
  16. /** 
  17. * Base for the author permalink structure (example.com/$author_base/authorname). 
  18. * @since 1.5.0 
  19. * @access private 
  20. * @var string 
  21. */ 
  22. var $author_base = 'author'; 
  23.  
  24. /** 
  25. * Permalink structure for author archives. 
  26. * @since 1.5.0 
  27. * @access private 
  28. * @var string 
  29. */ 
  30. var $author_structure; 
  31.  
  32. /** 
  33. * Permalink structure for date archives. 
  34. * @since 1.5.0 
  35. * @access private 
  36. * @var string 
  37. */ 
  38. var $date_structure; 
  39.  
  40. /** 
  41. * Permalink structure for pages. 
  42. * @since 1.5.0 
  43. * @access private 
  44. * @var string 
  45. */ 
  46. var $page_structure; 
  47.  
  48. /** 
  49. * Base of the search permalink structure (example.com/$search_base/query). 
  50. * @since 1.5.0 
  51. * @access private 
  52. * @var string 
  53. */ 
  54. var $search_base = 'search'; 
  55.  
  56. /** 
  57. * Permalink structure for searches. 
  58. * @since 1.5.0 
  59. * @access private 
  60. * @var string 
  61. */ 
  62. var $search_structure; 
  63.  
  64. /** 
  65. * Comments permalink base. 
  66. * @since 1.5.0 
  67. * @access private 
  68. * @var string 
  69. */ 
  70. var $comments_base = 'comments'; 
  71.  
  72. /** 
  73. * Pagination permalink base. 
  74. * @since 3.1.0 
  75. * @var string 
  76. */ 
  77. public $pagination_base = 'page'; 
  78.  
  79. /** 
  80. * Comments pagination permalink base. 
  81. * @since 4.2.0 
  82. * @access private 
  83. * @var string 
  84. */ 
  85. var $comments_pagination_base = 'comment-page'; 
  86.  
  87. /** 
  88. * Feed permalink base. 
  89. * @since 1.5.0 
  90. * @access private 
  91. * @var string 
  92. */ 
  93. var $feed_base = 'feed'; 
  94.  
  95. /** 
  96. * Comments feed permalink structure. 
  97. * @since 1.5.0 
  98. * @access private 
  99. * @var string 
  100. */ 
  101. var $comment_feed_structure; 
  102.  
  103. /** 
  104. * Feed request permalink structure. 
  105. * @since 1.5.0 
  106. * @access private 
  107. * @var string 
  108. */ 
  109. var $feed_structure; 
  110.  
  111. /** 
  112. * The static portion of the post permalink structure. 
  113. * If the permalink structure is "/archive/%post_id%" then the front 
  114. * is "/archive/". If the permalink structure is "/%year%/%postname%/" 
  115. * then the front is "/". 
  116. * @since 1.5.0 
  117. * @access public 
  118. * @var string 
  119. * @see WP_Rewrite::init() 
  120. */ 
  121. public $front; 
  122.  
  123. /** 
  124. * The prefix for all permalink structures. 
  125. * If PATHINFO/index permalinks are in use then the root is the value of 
  126. * `WP_Rewrite::$index` with a trailing slash appended. Otherwise the root 
  127. * will be empty. 
  128. * @since 1.5.0 
  129. * @access public 
  130. * @var string 
  131. * @see WP_Rewrite::init() 
  132. * @see WP_Rewrite::using_index_permalinks() 
  133. */ 
  134. public $root = ''; 
  135.  
  136. /** 
  137. * The name of the index file which is the entry point to all requests. 
  138. * @since 1.5.0 
  139. * @access public 
  140. * @var string 
  141. */ 
  142. public $index = 'index.php'; 
  143.  
  144. /** 
  145. * Variable name to use for regex matches in the rewritten query. 
  146. * @since 1.5.0 
  147. * @access private 
  148. * @var string 
  149. */ 
  150. var $matches = ''; 
  151.  
  152. /** 
  153. * Rewrite rules to match against the request to find the redirect or query. 
  154. * @since 1.5.0 
  155. * @access private 
  156. * @var array 
  157. */ 
  158. var $rules; 
  159.  
  160. /** 
  161. * Additional rules added external to the rewrite class. 
  162. * Those not generated by the class, see add_rewrite_rule(). 
  163. * @since 2.1.0 
  164. * @access private 
  165. * @var array 
  166. */ 
  167. var $extra_rules = array(); 
  168.  
  169. /** 
  170. * Additional rules that belong at the beginning to match first. 
  171. * Those not generated by the class, see add_rewrite_rule(). 
  172. * @since 2.3.0 
  173. * @access private 
  174. * @var array 
  175. */ 
  176. var $extra_rules_top = array(); 
  177.  
  178. /** 
  179. * Rules that don't redirect to WordPress' index.php. 
  180. * These rules are written to the mod_rewrite portion of the .htaccess,  
  181. * and are added by add_external_rule(). 
  182. * @since 2.1.0 
  183. * @access private 
  184. * @var array 
  185. */ 
  186. var $non_wp_rules = array(); 
  187.  
  188. /** 
  189. * Extra permalink structures, e.g. categories, added by add_permastruct(). 
  190. * @since 2.1.0 
  191. * @access private 
  192. * @var array 
  193. */ 
  194. var $extra_permastructs = array(); 
  195.  
  196. /** 
  197. * Endpoints (like /trackback/) added by add_rewrite_endpoint(). 
  198. * @since 2.1.0 
  199. * @access private 
  200. * @var array 
  201. */ 
  202. var $endpoints; 
  203.  
  204. /** 
  205. * Whether to write every mod_rewrite rule for WordPress into the .htaccess file. 
  206. * This is off by default, turning it on might print a lot of rewrite rules 
  207. * to the .htaccess file. 
  208. * @since 2.0.0 
  209. * @access public 
  210. * @var bool 
  211. * @see WP_Rewrite::mod_rewrite_rules() 
  212. */ 
  213. public $use_verbose_rules = false; 
  214.  
  215. /** 
  216. * Could post permalinks be confused with those of pages? 
  217. * If the first rewrite tag in the post permalink structure is one that could 
  218. * also match a page name (e.g. %postname% or %author%) then this flag is 
  219. * set to true. Prior to WordPress 3.3 this flag indicated that every page 
  220. * would have a set of rules added to the top of the rewrite rules array. 
  221. * Now it tells WP::parse_request() to check if a URL matching the page 
  222. * permastruct is actually a page before accepting it. 
  223. * @since 2.5.0 
  224. * @access public 
  225. * @var bool 
  226. * @see WP_Rewrite::init() 
  227. */ 
  228. public $use_verbose_page_rules = true; 
  229.  
  230. /** 
  231. * Rewrite tags that can be used in permalink structures. 
  232. * These are translated into the regular expressions stored in 
  233. * `WP_Rewrite::$rewritereplace` and are rewritten to the query 
  234. * variables listed in WP_Rewrite::$queryreplace. 
  235. * Additional tags can be added with add_rewrite_tag(). 
  236. * @since 1.5.0 
  237. * @access private 
  238. * @var array 
  239. */ 
  240. var $rewritecode = array( 
  241. '%year%',  
  242. '%monthnum%',  
  243. '%day%',  
  244. '%hour%',  
  245. '%minute%',  
  246. '%second%',  
  247. '%postname%',  
  248. '%post_id%',  
  249. '%author%',  
  250. '%pagename%',  
  251. '%search%' 
  252. ); 
  253.  
  254. /** 
  255. * Regular expressions to be substituted into rewrite rules in place 
  256. * of rewrite tags, see WP_Rewrite::$rewritecode. 
  257. * @since 1.5.0 
  258. * @access private 
  259. * @var array 
  260. */ 
  261. var $rewritereplace = array( 
  262. '([0-9]{4})',  
  263. '([0-9]{1, 2})',  
  264. '([0-9]{1, 2})',  
  265. '([0-9]{1, 2})',  
  266. '([0-9]{1, 2})',  
  267. '([0-9]{1, 2})',  
  268. '([^/]+)',  
  269. '([0-9]+)',  
  270. '([^/]+)',  
  271. '([^/]+?)',  
  272. '(.+)' 
  273. ); 
  274.  
  275. /** 
  276. * Query variables that rewrite tags map to, see WP_Rewrite::$rewritecode. 
  277. * @since 1.5.0 
  278. * @access private 
  279. * @var array 
  280. */ 
  281. var $queryreplace = array( 
  282. 'year=',  
  283. 'monthnum=',  
  284. 'day=',  
  285. 'hour=',  
  286. 'minute=',  
  287. 'second=',  
  288. 'name=',  
  289. 'p=',  
  290. 'author_name=',  
  291. 'pagename=',  
  292. 's=' 
  293. ); 
  294.  
  295. /** 
  296. * Supported default feeds. 
  297. * @since 1.5.0 
  298. * @var array 
  299. */ 
  300. public $feeds = array( 'feed', 'rdf', 'rss', 'rss2', 'atom' ); 
  301.  
  302. /** 
  303. * Determines whether permalinks are being used. 
  304. * This can be either rewrite module or permalink in the HTTP query string. 
  305. * @since 1.5.0 
  306. * @access public 
  307. * @return bool True, if permalinks are enabled. 
  308. */ 
  309. public function using_permalinks() { 
  310. return ! empty($this->permalink_structure); 
  311.  
  312. /** 
  313. * Determines whether permalinks are being used and rewrite module is not enabled. 
  314. * Means that permalink links are enabled and index.php is in the URL. 
  315. * @since 1.5.0 
  316. * @access public 
  317. * @return bool Whether permalink links are enabled and index.php is in the URL. 
  318. */ 
  319. public function using_index_permalinks() { 
  320. if ( empty( $this->permalink_structure ) ) { 
  321. return false; 
  322.  
  323. // If the index is not in the permalink, we're using mod_rewrite. 
  324. return preg_match( '#^/*' . $this->index . '#', $this->permalink_structure ); 
  325.   
  326. /** 
  327. * Determines whether permalinks are being used and rewrite module is enabled. 
  328. * Using permalinks and index.php is not in the URL. 
  329. * @since 1.5.0 
  330. * @access public 
  331. * @return bool Whether permalink links are enabled and index.php is NOT in the URL. 
  332. */ 
  333. public function using_mod_rewrite_permalinks() { 
  334. return $this->using_permalinks() && ! $this->using_index_permalinks(); 
  335.  
  336. /** 
  337. * Indexes for matches for usage in preg_*() functions. 
  338. * The format of the string is, with empty matches property value, '$NUM'. 
  339. * The 'NUM' will be replaced with the value in the $number parameter. With 
  340. * the matches property not empty, the value of the returned string will 
  341. * contain that value of the matches property. The format then will be 
  342. * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the 
  343. * value of the $number parameter. 
  344. * @since 1.5.0 
  345. * @access public 
  346. * @param int $number Index number. 
  347. * @return string 
  348. */ 
  349. public function preg_index($number) { 
  350. $match_prefix = '$'; 
  351. $match_suffix = ''; 
  352.  
  353. if ( ! empty($this->matches) ) { 
  354. $match_prefix = '$' . $this->matches . '['; 
  355. $match_suffix = ']'; 
  356.  
  357. return "$match_prefix$number$match_suffix"; 
  358.  
  359. /** 
  360. * Retrieves all page and attachments for pages URIs. 
  361. * The attachments are for those that have pages as parents and will be 
  362. * retrieved. 
  363. * @since 2.5.0 
  364. * @access public 
  365. * @global wpdb $wpdb WordPress database abstraction object. 
  366. * @return array Array of page URIs as first element and attachment URIs as second element. 
  367. */ 
  368. public function page_uri_index() { 
  369. global $wpdb; 
  370.  
  371. // Get pages in order of hierarchy, i.e. children after parents. 
  372. $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'"); 
  373. $posts = get_page_hierarchy( $pages ); 
  374.  
  375. // If we have no pages get out quick. 
  376. if ( !$posts ) 
  377. return array( array(), array() ); 
  378.  
  379. // Now reverse it, because we need parents after children for rewrite rules to work properly. 
  380. $posts = array_reverse($posts, true); 
  381.  
  382. $page_uris = array(); 
  383. $page_attachment_uris = array(); 
  384.  
  385. foreach ( $posts as $id => $post ) { 
  386. // URL => page name 
  387. $uri = get_page_uri($id); 
  388. $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id )); 
  389. if ( !empty($attachments) ) { 
  390. foreach ( $attachments as $attachment ) { 
  391. $attach_uri = get_page_uri($attachment->ID); 
  392. $page_attachment_uris[$attach_uri] = $attachment->ID; 
  393.  
  394. $page_uris[$uri] = $id; 
  395.  
  396. return array( $page_uris, $page_attachment_uris ); 
  397.  
  398. /** 
  399. * Retrieves all of the rewrite rules for pages. 
  400. * @since 1.5.0 
  401. * @access public 
  402. * @return array Page rewrite rules. 
  403. */ 
  404. public function page_rewrite_rules() { 
  405. // The extra .? at the beginning prevents clashes with other regular expressions in the rules array. 
  406. $this->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' ); 
  407.  
  408. return $this->generate_rewrite_rules( $this->get_page_permastruct(), EP_PAGES, true, true, false, false ); 
  409.  
  410. /** 
  411. * Retrieves date permalink structure, with year, month, and day. 
  412. * The permalink structure for the date, if not set already depends on the 
  413. * permalink structure. It can be one of three formats. The first is year,  
  414. * month, day; the second is day, month, year; and the last format is month,  
  415. * day, year. These are matched against the permalink structure for which 
  416. * one is used. If none matches, then the default will be used, which is 
  417. * year, month, day. 
  418. * Prevents post ID and date permalinks from overlapping. In the case of 
  419. * post_id, the date permalink will be prepended with front permalink with 
  420. * 'date/' before the actual permalink to form the complete date permalink 
  421. * structure. 
  422. * @since 1.5.0 
  423. * @access public 
  424. * @return string|false False on no permalink structure. Date permalink structure. 
  425. */ 
  426. public function get_date_permastruct() { 
  427. if ( isset($this->date_structure) ) 
  428. return $this->date_structure; 
  429.  
  430. if ( empty($this->permalink_structure) ) { 
  431. $this->date_structure = ''; 
  432. return false; 
  433.  
  434. // The date permalink must have year, month, and day separated by slashes. 
  435. $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%'); 
  436.  
  437. $this->date_structure = ''; 
  438. $date_endian = ''; 
  439.  
  440. foreach ( $endians as $endian ) { 
  441. if ( false !== strpos($this->permalink_structure, $endian) ) { 
  442. $date_endian= $endian; 
  443. break; 
  444.  
  445. if ( empty($date_endian) ) 
  446. $date_endian = '%year%/%monthnum%/%day%'; 
  447.  
  448. /** 
  449. * Do not allow the date tags and %post_id% to overlap in the permalink 
  450. * structure. If they do, move the date tags to $front/date/. 
  451. */ 
  452. $front = $this->front; 
  453. preg_match_all('/%.+?%/', $this->permalink_structure, $tokens); 
  454. $tok_index = 1; 
  455. foreach ( (array) $tokens[0] as $token) { 
  456. if ( '%post_id%' == $token && ($tok_index <= 3) ) { 
  457. $front = $front . 'date/'; 
  458. break; 
  459. $tok_index++; 
  460.  
  461. $this->date_structure = $front . $date_endian; 
  462.  
  463. return $this->date_structure; 
  464.  
  465. /** 
  466. * Retrieves the year permalink structure without month and day. 
  467. * Gets the date permalink structure and strips out the month and day 
  468. * permalink structures. 
  469. * @since 1.5.0 
  470. * @access public 
  471. * @return false|string False on failure. Year structure on success. 
  472. */ 
  473. public function get_year_permastruct() { 
  474. $structure = $this->get_date_permastruct(); 
  475.  
  476. if ( empty($structure) ) 
  477. return false; 
  478.  
  479. $structure = str_replace('%monthnum%', '', $structure); 
  480. $structure = str_replace('%day%', '', $structure); 
  481. $structure = preg_replace('#/+#', '/', $structure); 
  482.  
  483. return $structure; 
  484.  
  485. /** 
  486. * Retrieves the month permalink structure without day and with year. 
  487. * Gets the date permalink structure and strips out the day permalink 
  488. * structures. Keeps the year permalink structure. 
  489. * @since 1.5.0 
  490. * @access public 
  491. * @return false|string False on failure. Year/Month structure on success. 
  492. */ 
  493. public function get_month_permastruct() { 
  494. $structure = $this->get_date_permastruct(); 
  495.  
  496. if ( empty($structure) ) 
  497. return false; 
  498.  
  499. $structure = str_replace('%day%', '', $structure); 
  500. $structure = preg_replace('#/+#', '/', $structure); 
  501.  
  502. return $structure; 
  503.  
  504. /** 
  505. * Retrieves the day permalink structure with month and year. 
  506. * Keeps date permalink structure with all year, month, and day. 
  507. * @since 1.5.0 
  508. * @access public 
  509. * @return string|false False on failure. Year/Month/Day structure on success. 
  510. */ 
  511. public function get_day_permastruct() { 
  512. return $this->get_date_permastruct(); 
  513.  
  514. /** 
  515. * Retrieves the permalink structure for categories. 
  516. * If the category_base property has no value, then the category structure 
  517. * will have the front property value, followed by 'category', and finally 
  518. * '%category%'. If it does, then the root property will be used, along with 
  519. * the category_base property value. 
  520. * @since 1.5.0 
  521. * @access public 
  522. * @return string|false False on failure. Category permalink structure. 
  523. */ 
  524. public function get_category_permastruct() { 
  525. return $this->get_extra_permastruct('category'); 
  526.  
  527. /** 
  528. * Retrieve the permalink structure for tags. 
  529. * If the tag_base property has no value, then the tag structure will have 
  530. * the front property value, followed by 'tag', and finally '%tag%'. If it 
  531. * does, then the root property will be used, along with the tag_base 
  532. * property value. 
  533. * @since 2.3.0 
  534. * @access public 
  535. * @return string|false False on failure. Tag permalink structure. 
  536. */ 
  537. public function get_tag_permastruct() { 
  538. return $this->get_extra_permastruct('post_tag'); 
  539.  
  540. /** 
  541. * Retrieves an extra permalink structure by name. 
  542. * @since 2.5.0 
  543. * @access public 
  544. * @param string $name Permalink structure name. 
  545. * @return string|false False if not found. Permalink structure string. 
  546. */ 
  547. public function get_extra_permastruct($name) { 
  548. if ( empty($this->permalink_structure) ) 
  549. return false; 
  550.  
  551. if ( isset($this->extra_permastructs[$name]) ) 
  552. return $this->extra_permastructs[$name]['struct']; 
  553.  
  554. return false; 
  555.  
  556. /** 
  557. * Retrieves the author permalink structure. 
  558. * The permalink structure is front property, author base, and finally 
  559. * '/%author%'. Will set the author_structure property and then return it 
  560. * without attempting to set the value again. 
  561. * @since 1.5.0 
  562. * @access public 
  563. * @return string|false False if not found. Permalink structure string. 
  564. */ 
  565. public function get_author_permastruct() { 
  566. if ( isset($this->author_structure) ) 
  567. return $this->author_structure; 
  568.  
  569. if ( empty($this->permalink_structure) ) { 
  570. $this->author_structure = ''; 
  571. return false; 
  572.  
  573. $this->author_structure = $this->front . $this->author_base . '/%author%'; 
  574.  
  575. return $this->author_structure; 
  576.  
  577. /** 
  578. * Retrieves the search permalink structure. 
  579. * The permalink structure is root property, search base, and finally 
  580. * '/%search%'. Will set the search_structure property and then return it 
  581. * without attempting to set the value again. 
  582. * @since 1.5.0 
  583. * @access public 
  584. * @return string|false False if not found. Permalink structure string. 
  585. */ 
  586. public function get_search_permastruct() { 
  587. if ( isset($this->search_structure) ) 
  588. return $this->search_structure; 
  589.  
  590. if ( empty($this->permalink_structure) ) { 
  591. $this->search_structure = ''; 
  592. return false; 
  593.  
  594. $this->search_structure = $this->root . $this->search_base . '/%search%'; 
  595.  
  596. return $this->search_structure; 
  597.  
  598. /** 
  599. * Retrieves the page permalink structure. 
  600. * The permalink structure is root property, and '%pagename%'. Will set the 
  601. * page_structure property and then return it without attempting to set the 
  602. * value again. 
  603. * @since 1.5.0 
  604. * @access public 
  605. * @return string|false False if not found. Permalink structure string. 
  606. */ 
  607. public function get_page_permastruct() { 
  608. if ( isset($this->page_structure) ) 
  609. return $this->page_structure; 
  610.  
  611. if (empty($this->permalink_structure)) { 
  612. $this->page_structure = ''; 
  613. return false; 
  614.  
  615. $this->page_structure = $this->root . '%pagename%'; 
  616.  
  617. return $this->page_structure; 
  618.  
  619. /** 
  620. * Retrieves the feed permalink structure. 
  621. * The permalink structure is root property, feed base, and finally 
  622. * '/%feed%'. Will set the feed_structure property and then return it 
  623. * without attempting to set the value again. 
  624. * @since 1.5.0 
  625. * @access public 
  626. * @return string|false False if not found. Permalink structure string. 
  627. */ 
  628. public function get_feed_permastruct() { 
  629. if ( isset($this->feed_structure) ) 
  630. return $this->feed_structure; 
  631.  
  632. if ( empty($this->permalink_structure) ) { 
  633. $this->feed_structure = ''; 
  634. return false; 
  635.  
  636. $this->feed_structure = $this->root . $this->feed_base . '/%feed%'; 
  637.  
  638. return $this->feed_structure; 
  639.  
  640. /** 
  641. * Retrieves the comment feed permalink structure. 
  642. * The permalink structure is root property, comment base property, feed 
  643. * base and finally '/%feed%'. Will set the comment_feed_structure property 
  644. * and then return it without attempting to set the value again. 
  645. * @since 1.5.0 
  646. * @access public 
  647. * @return string|false False if not found. Permalink structure string. 
  648. */ 
  649. public function get_comment_feed_permastruct() { 
  650. if ( isset($this->comment_feed_structure) ) 
  651. return $this->comment_feed_structure; 
  652.  
  653. if (empty($this->permalink_structure)) { 
  654. $this->comment_feed_structure = ''; 
  655. return false; 
  656.  
  657. $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%'; 
  658.  
  659. return $this->comment_feed_structure; 
  660.  
  661. /** 
  662. * Adds or updates existing rewrite tags (e.g. %postname%). 
  663. * If the tag already exists, replace the existing pattern and query for 
  664. * that tag, otherwise add the new tag. 
  665. * @since 1.5.0 
  666. * @access public 
  667. * @see WP_Rewrite::$rewritecode 
  668. * @see WP_Rewrite::$rewritereplace 
  669. * @see WP_Rewrite::$queryreplace 
  670. * @param string $tag Name of the rewrite tag to add or update. 
  671. * @param string $regex Regular expression to substitute the tag for in rewrite rules. 
  672. * @param string $query String to append to the rewritten query. Must end in '='. 
  673. */ 
  674. public function add_rewrite_tag( $tag, $regex, $query ) { 
  675. $position = array_search( $tag, $this->rewritecode ); 
  676. if ( false !== $position && null !== $position ) { 
  677. $this->rewritereplace[ $position ] = $regex; 
  678. $this->queryreplace[ $position ] = $query; 
  679. } else { 
  680. $this->rewritecode[] = $tag; 
  681. $this->rewritereplace[] = $regex; 
  682. $this->queryreplace[] = $query; 
  683.  
  684.  
  685. /** 
  686. * Removes an existing rewrite tag. 
  687. * @since 4.5.0 
  688. * @access public 
  689. * @see WP_Rewrite::$rewritecode 
  690. * @see WP_Rewrite::$rewritereplace 
  691. * @see WP_Rewrite::$queryreplace 
  692. * @param string $tag Name of the rewrite tag to remove. 
  693. */ 
  694. public function remove_rewrite_tag( $tag ) { 
  695. $position = array_search( $tag, $this->rewritecode ); 
  696. if ( false !== $position && null !== $position ) { 
  697. unset( $this->rewritecode[ $position ] ); 
  698. unset( $this->rewritereplace[ $position ] ); 
  699. unset( $this->queryreplace[ $position ] ); 
  700.  
  701. /** 
  702. * Generates rewrite rules from a permalink structure. 
  703. * The main WP_Rewrite function for building the rewrite rule list. The 
  704. * contents of the function is a mix of black magic and regular expressions,  
  705. * so best just ignore the contents and move to the parameters. 
  706. * @since 1.5.0 
  707. * @access public 
  708. * @param string $permalink_structure The permalink structure. 
  709. * @param int $ep_mask Optional. Endpoint mask defining what endpoints are added to the structure. 
  710. * Accepts `EP_NONE`, `EP_PERMALINK`, `EP_ATTACHMENT`, `EP_DATE`, `EP_YEAR`,  
  711. * `EP_MONTH`, `EP_DAY`, `EP_ROOT`, `EP_COMMENTS`, `EP_SEARCH`, `EP_CATEGORIES`,  
  712. * `EP_TAGS`, `EP_AUTHORS`, `EP_PAGES`, `EP_ALL_ARCHIVES`, and `EP_ALL`. 
  713. * Default `EP_NONE`. 
  714. * @param bool $paged Optional. Whether archive pagination rules should be added for the structure. 
  715. * Default true. 
  716. * @param bool $feed Optional Whether feed rewrite rules should be added for the structure. 
  717. * Default true. 
  718. * @param bool $forcomments Optional. Whether the feed rules should be a query for a comments feed. 
  719. * Default false. 
  720. * @param bool $walk_dirs Optional. Whether the 'directories' making up the structure should be walked 
  721. * over and rewrite rules built for each in-turn. Default true. 
  722. * @param bool $endpoints Optional. Whether endpoints should be applied to the generated rewrite rules. 
  723. * Default true. 
  724. * @return array Rewrite rule list. 
  725. */ 
  726. public function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) { 
  727. // Build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/? 
  728. $feedregex2 = ''; 
  729. foreach ( (array) $this->feeds as $feed_name) 
  730. $feedregex2 .= $feed_name . '|'; 
  731. $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$'; 
  732.  
  733. /** 
  734. * $feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom 
  735. * and <permalink>/atom are both possible 
  736. */ 
  737. $feedregex = $this->feed_base . '/' . $feedregex2; 
  738.  
  739. // Build a regex to match the trackback and page/xx parts of URLs. 
  740. $trackbackregex = 'trackback/?$'; 
  741. $pageregex = $this->pagination_base . '/?([0-9]{1, })/?$'; 
  742. $commentregex = $this->comments_pagination_base . '-([0-9]{1, })/?$'; 
  743. $embedregex = 'embed/?$'; 
  744.  
  745. // Build up an array of endpoint regexes to append => queries to append. 
  746. if ( $endpoints ) { 
  747. $ep_query_append = array (); 
  748. foreach ( (array) $this->endpoints as $endpoint) { 
  749. // Match everything after the endpoint name, but allow for nothing to appear there. 
  750. $epmatch = $endpoint[1] . '(/(.*))?/?$'; 
  751.  
  752. // This will be appended on to the rest of the query for each dir. 
  753. $epquery = '&' . $endpoint[2] . '='; 
  754. $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery ); 
  755.  
  756. // Get everything up to the first rewrite tag. 
  757. $front = substr($permalink_structure, 0, strpos($permalink_structure, '%')); 
  758.  
  759. // Build an array of the tags (note that said array ends up being in $tokens[0]). 
  760. preg_match_all('/%.+?%/', $permalink_structure, $tokens); 
  761.  
  762. $num_tokens = count($tokens[0]); 
  763.  
  764. $index = $this->index; //probably 'index.php' 
  765. $feedindex = $index; 
  766. $trackbackindex = $index; 
  767. $embedindex = $index; 
  768.  
  769. /** 
  770. * Build a list from the rewritecode and queryreplace arrays, that will look something 
  771. * like tagname=$matches[i] where i is the current $i. 
  772. */ 
  773. $queries = array(); 
  774. for ( $i = 0; $i < $num_tokens; ++$i ) { 
  775. if ( 0 < $i ) 
  776. $queries[$i] = $queries[$i - 1] . '&'; 
  777. else 
  778. $queries[$i] = ''; 
  779.  
  780. $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1); 
  781. $queries[$i] .= $query_token; 
  782.  
  783. // Get the structure, minus any cruft (stuff that isn't tags) at the front. 
  784. $structure = $permalink_structure; 
  785. if ( $front != '/' ) 
  786. $structure = str_replace($front, '', $structure); 
  787.  
  788. /** 
  789. * Create a list of dirs to walk over, making rewrite rules for each level 
  790. * so for example, a $structure of /%year%/%monthnum%/%postname% would create 
  791. * rewrite rules for /%year%/, /%year%/%monthnum%/ and /%year%/%monthnum%/%postname% 
  792. */ 
  793. $structure = trim($structure, '/'); 
  794. $dirs = $walk_dirs ? explode('/', $structure) : array( $structure ); 
  795. $num_dirs = count($dirs); 
  796.  
  797. // Strip slashes from the front of $front. 
  798. $front = preg_replace('|^/+|', '', $front); 
  799.  
  800. // The main workhorse loop. 
  801. $post_rewrite = array(); 
  802. $struct = $front; 
  803. for ( $j = 0; $j < $num_dirs; ++$j ) { 
  804. // Get the struct for this dir, and trim slashes off the front. 
  805. $struct .= $dirs[$j] . '/'; // Accumulate. see comment near explode('/', $structure) above. 
  806. $struct = ltrim($struct, '/'); 
  807.  
  808. // Replace tags with regexes. 
  809. $match = str_replace($this->rewritecode, $this->rewritereplace, $struct); 
  810.  
  811. // Make a list of tags, and store how many there are in $num_toks. 
  812. $num_toks = preg_match_all('/%.+?%/', $struct, $toks); 
  813.  
  814. // Get the 'tagname=$matches[i]'. 
  815. $query = ( ! empty( $num_toks ) && isset( $queries[$num_toks - 1] ) ) ? $queries[$num_toks - 1] : ''; 
  816.  
  817. // Set up $ep_mask_specific which is used to match more specific URL types. 
  818. switch ( $dirs[$j] ) { 
  819. case '%year%': 
  820. $ep_mask_specific = EP_YEAR; 
  821. break; 
  822. case '%monthnum%': 
  823. $ep_mask_specific = EP_MONTH; 
  824. break; 
  825. case '%day%': 
  826. $ep_mask_specific = EP_DAY; 
  827. break; 
  828. default: 
  829. $ep_mask_specific = EP_NONE; 
  830.  
  831. // Create query for /page/xx. 
  832. $pagematch = $match . $pageregex; 
  833. $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1); 
  834.  
  835. // Create query for /comment-page-xx. 
  836. $commentmatch = $match . $commentregex; 
  837. $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1); 
  838.  
  839. if ( get_option('page_on_front') ) { 
  840. // Create query for Root /comment-page-xx. 
  841. $rootcommentmatch = $match . $commentregex; 
  842. $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1); 
  843.  
  844. // Create query for /feed/(feed|atom|rss|rss2|rdf). 
  845. $feedmatch = $match . $feedregex; 
  846. $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); 
  847.  
  848. // Create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex). 
  849. $feedmatch2 = $match . $feedregex2; 
  850. $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1); 
  851.  
  852. // Create query and regex for embeds. 
  853. $embedmatch = $match . $embedregex; 
  854. $embedquery = $embedindex . '?' . $query . '&embed=true'; 
  855.  
  856. // If asked to, turn the feed queries into comment feed ones. 
  857. if ( $forcomments ) { 
  858. $feedquery .= '&withcomments=1'; 
  859. $feedquery2 .= '&withcomments=1'; 
  860.  
  861. // Start creating the array of rewrites for this dir. 
  862. $rewrite = array(); 
  863.  
  864. // ...adding on /feed/ regexes => queries 
  865. if ( $feed ) { 
  866. $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2, $embedmatch => $embedquery ); 
  867.  
  868. //...and /page/xx ones 
  869. if ( $paged ) { 
  870. $rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) ); 
  871.  
  872. // Only on pages with comments add ../comment-page-xx/. 
  873. if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) { 
  874. $rewrite = array_merge($rewrite, array($commentmatch => $commentquery)); 
  875. } elseif ( EP_ROOT & $ep_mask && get_option('page_on_front') ) { 
  876. $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery)); 
  877.  
  878. // Do endpoints. 
  879. if ( $endpoints ) { 
  880. foreach ( (array) $ep_query_append as $regex => $ep) { 
  881. // Add the endpoints on if the mask fits. 
  882. if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) 
  883. $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2); 
  884.  
  885. // If we've got some tags in this dir. 
  886. if ( $num_toks ) { 
  887. $post = false; 
  888. $page = false; 
  889.  
  890. /** 
  891. * Check to see if this dir is permalink-level: i.e. the structure specifies an 
  892. * individual post. Do this by checking it contains at least one of 1) post name,  
  893. * 2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and 
  894. * minute all present). Set these flags now as we need them for the endpoints. 
  895. */ 
  896. if ( strpos($struct, '%postname%') !== false 
  897. || strpos($struct, '%post_id%') !== false 
  898. || strpos($struct, '%pagename%') !== false 
  899. || (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false) 
  900. ) { 
  901. $post = true; 
  902. if ( strpos($struct, '%pagename%') !== false ) 
  903. $page = true; 
  904.  
  905. if ( ! $post ) { 
  906. // For custom post types, we need to add on endpoints as well. 
  907. foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) { 
  908. if ( strpos($struct, "%$ptype%") !== false ) { 
  909. $post = true; 
  910.  
  911. // This is for page style attachment URLs. 
  912. $page = is_post_type_hierarchical( $ptype ); 
  913. break; 
  914.  
  915. // If creating rules for a permalink, do all the endpoints like attachments etc. 
  916. if ( $post ) { 
  917. // Create query and regex for trackback. 
  918. $trackbackmatch = $match . $trackbackregex; 
  919. $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; 
  920.  
  921. // Create query and regex for embeds. 
  922. $embedmatch = $match . $embedregex; 
  923. $embedquery = $embedindex . '?' . $query . '&embed=true'; 
  924.  
  925. // Trim slashes from the end of the regex for this dir. 
  926. $match = rtrim($match, '/'); 
  927.  
  928. // Get rid of brackets. 
  929. $submatchbase = str_replace( array('(', ')'), '', $match); 
  930.  
  931. // Add a rule for at attachments, which take the form of <permalink>/some-text. 
  932. $sub1 = $submatchbase . '/([^/]+)/'; 
  933.  
  934. // Add trackback regex <permalink>/trackback/... 
  935. $sub1tb = $sub1 . $trackbackregex; 
  936.  
  937. // And <permalink>/feed/(atom|...) 
  938. $sub1feed = $sub1 . $feedregex; 
  939.  
  940. // And <permalink>/(feed|atom...) 
  941. $sub1feed2 = $sub1 . $feedregex2; 
  942.  
  943. // And <permalink>/comment-page-xx 
  944. $sub1comment = $sub1 . $commentregex; 
  945.  
  946. // And <permalink>/embed/... 
  947. $sub1embed = $sub1 . $embedregex; 
  948.  
  949. /** 
  950. * Add another rule to match attachments in the explicit form: 
  951. * <permalink>/attachment/some-text 
  952. */ 
  953. $sub2 = $submatchbase . '/attachment/([^/]+)/'; 
  954.  
  955. // And add trackbacks <permalink>/attachment/trackback. 
  956. $sub2tb = $sub2 . $trackbackregex; 
  957.  
  958. // Feeds, <permalink>/attachment/feed/(atom|...) 
  959. $sub2feed = $sub2 . $feedregex; 
  960.  
  961. // And feeds again on to this <permalink>/attachment/(feed|atom...) 
  962. $sub2feed2 = $sub2 . $feedregex2; 
  963.  
  964. // And <permalink>/comment-page-xx 
  965. $sub2comment = $sub2 . $commentregex; 
  966.  
  967. // And <permalink>/embed/... 
  968. $sub2embed = $sub2 . $embedregex; 
  969.  
  970. // Create queries for these extra tag-ons we've just dealt with. 
  971. $subquery = $index . '?attachment=' . $this->preg_index(1); 
  972. $subtbquery = $subquery . '&tb=1'; 
  973. $subfeedquery = $subquery . '&feed=' . $this->preg_index(2); 
  974. $subcommentquery = $subquery . '&cpage=' . $this->preg_index(2); 
  975. $subembedquery = $subquery . '&embed=true'; 
  976.  
  977. // Do endpoints for attachments. 
  978. if ( !empty($endpoints) ) { 
  979. foreach ( (array) $ep_query_append as $regex => $ep ) { 
  980. if ( $ep[0] & EP_ATTACHMENT ) { 
  981. $rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(3); 
  982. $rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(3); 
  983.  
  984. /** 
  985. * Now we've finished with endpoints, finish off the $sub1 and $sub2 matches 
  986. * add a ? as we don't have to match that last slash, and finally a $ so we 
  987. * match to the end of the URL 
  988. */ 
  989. $sub1 .= '?$'; 
  990. $sub2 .= '?$'; 
  991.  
  992. /** 
  993. * Post pagination, e.g. <permalink>/2/ 
  994. * Previously: '(/[0-9]+)?/?$', which produced '/2' for page. 
  995. * When cast to int, returned 0. 
  996. */ 
  997. $match = $match . '(?:/([0-9]+))?/?$'; 
  998. $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1); 
  999.  
  1000. // Not matching a permalink so this is a lot simpler. 
  1001. } else { 
  1002. // Close the match and finalise the query. 
  1003. $match .= '?$'; 
  1004. $query = $index . '?' . $query; 
  1005.  
  1006. /** 
  1007. * Create the final array for this dir by joining the $rewrite array (which currently 
  1008. * only contains rules/queries for trackback, pages etc) to the main regex/query for 
  1009. * this dir 
  1010. */ 
  1011. $rewrite = array_merge($rewrite, array($match => $query)); 
  1012.  
  1013. // If we're matching a permalink, add those extras (attachments etc) on. 
  1014. if ( $post ) { 
  1015. // Add trackback. 
  1016. $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite); 
  1017.  
  1018. // Add embed. 
  1019. $rewrite = array_merge( array( $embedmatch => $embedquery ), $rewrite ); 
  1020.  
  1021. // Add regexes/queries for attachments, attachment trackbacks and so on. 
  1022. if ( ! $page ) { 
  1023. // Require <permalink>/attachment/stuff form for pages because of confusion with subpages. 
  1024. $rewrite = array_merge( $rewrite, array( 
  1025. $sub1 => $subquery,  
  1026. $sub1tb => $subtbquery,  
  1027. $sub1feed => $subfeedquery,  
  1028. $sub1feed2 => $subfeedquery,  
  1029. $sub1comment => $subcommentquery,  
  1030. $sub1embed => $subembedquery 
  1031. ) ); 
  1032.  
  1033. $rewrite = array_merge( array( $sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery, $sub2embed => $subembedquery ), $rewrite ); 
  1034. // Add the rules for this dir to the accumulating $post_rewrite. 
  1035. $post_rewrite = array_merge($rewrite, $post_rewrite); 
  1036.  
  1037. // The finished rules. phew! 
  1038. return $post_rewrite; 
  1039.  
  1040. /** 
  1041. * Generates rewrite rules with permalink structure and walking directory only. 
  1042. * Shorten version of WP_Rewrite::generate_rewrite_rules() that allows for shorter 
  1043. * list of parameters. See the method for longer description of what generating 
  1044. * rewrite rules does. 
  1045. * @since 1.5.0 
  1046. * @access public 
  1047. * @see WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters. 
  1048. * @param string $permalink_structure The permalink structure to generate rules. 
  1049. * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over. 
  1050. * @return array 
  1051. */ 
  1052. public function generate_rewrite_rule($permalink_structure, $walk_dirs = false) { 
  1053. return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs); 
  1054.  
  1055. /** 
  1056. * Constructs rewrite matches and queries from permalink structure. 
  1057. * Runs the action {@see 'generate_rewrite_rules'} with the parameter that is an 
  1058. * reference to the current WP_Rewrite instance to further manipulate the 
  1059. * permalink structures and rewrite rules. Runs the {@see 'rewrite_rules_array'} 
  1060. * filter on the full rewrite rule array. 
  1061. * There are two ways to manipulate the rewrite rules, one by hooking into 
  1062. * the {@see 'generate_rewrite_rules'} action and gaining full control of the 
  1063. * object or just manipulating the rewrite rule array before it is passed 
  1064. * from the function. 
  1065. * @since 1.5.0 
  1066. * @access public 
  1067. * @return array An associate array of matches and queries. 
  1068. */ 
  1069. public function rewrite_rules() { 
  1070. $rewrite = array(); 
  1071.  
  1072. if ( empty($this->permalink_structure) ) 
  1073. return $rewrite; 
  1074.  
  1075. // robots.txt -only if installed at the root 
  1076. $home_path = parse_url( home_url() ); 
  1077. $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array(); 
  1078.  
  1079. // Old feed and service files. 
  1080. $deprecated_files = array( 
  1081. '.*wp-(atom|rdf|rss|rss2|feed|commentsrss2)\.php$' => $this->index . '?feed=old',  
  1082. '.*wp-app\.php(/.*)?$' => $this->index . '?error=403',  
  1083. ); 
  1084.  
  1085. // Registration rules. 
  1086. $registration_pages = array(); 
  1087. if ( is_multisite() && is_main_site() ) { 
  1088. $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true'; 
  1089. $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true'; 
  1090.  
  1091. // Deprecated. 
  1092. $registration_pages['.*wp-register.php$'] = $this->index . '?register=true'; 
  1093.  
  1094. // Post rewrite rules. 
  1095. $post_rewrite = $this->generate_rewrite_rules( $this->permalink_structure, EP_PERMALINK ); 
  1096.  
  1097. /** 
  1098. * Filters rewrite rules used for "post" archives. 
  1099. * @since 1.5.0 
  1100. * @param array $post_rewrite The rewrite rules for posts. 
  1101. */ 
  1102. $post_rewrite = apply_filters( 'post_rewrite_rules', $post_rewrite ); 
  1103.  
  1104. // Date rewrite rules. 
  1105. $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE); 
  1106.  
  1107. /** 
  1108. * Filters rewrite rules used for date archives. 
  1109. * Likely date archives would include /yyyy/, /yyyy/mm/, and /yyyy/mm/dd/. 
  1110. * @since 1.5.0 
  1111. * @param array $date_rewrite The rewrite rules for date archives. 
  1112. */ 
  1113. $date_rewrite = apply_filters( 'date_rewrite_rules', $date_rewrite ); 
  1114.  
  1115. // Root-level rewrite rules. 
  1116. $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT); 
  1117.  
  1118. /** 
  1119. * Filters rewrite rules used for root-level archives. 
  1120. * Likely root-level archives would include pagination rules for the homepage 
  1121. * as well as site-wide post feeds (e.g. /feed/, and /feed/atom/). 
  1122. * @since 1.5.0 
  1123. * @param array $root_rewrite The root-level rewrite rules. 
  1124. */ 
  1125. $root_rewrite = apply_filters( 'root_rewrite_rules', $root_rewrite ); 
  1126.  
  1127. // Comments rewrite rules. 
  1128. $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, false, true, true, false); 
  1129.  
  1130. /** 
  1131. * Filters rewrite rules used for comment feed archives. 
  1132. * Likely comments feed archives include /comments/feed/, and /comments/feed/atom/. 
  1133. * @since 1.5.0 
  1134. * @param array $comments_rewrite The rewrite rules for the site-wide comments feeds. 
  1135. */ 
  1136. $comments_rewrite = apply_filters( 'comments_rewrite_rules', $comments_rewrite ); 
  1137.  
  1138. // Search rewrite rules. 
  1139. $search_structure = $this->get_search_permastruct(); 
  1140. $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH); 
  1141.  
  1142. /** 
  1143. * Filters rewrite rules used for search archives. 
  1144. * Likely search-related archives include /search/search+query/ as well as 
  1145. * pagination and feed paths for a search. 
  1146. * @since 1.5.0 
  1147. * @param array $search_rewrite The rewrite rules for search queries. 
  1148. */ 
  1149. $search_rewrite = apply_filters( 'search_rewrite_rules', $search_rewrite ); 
  1150.  
  1151. // Author rewrite rules. 
  1152. $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS); 
  1153.  
  1154. /** 
  1155. * Filters rewrite rules used for author archives. 
  1156. * Likely author archives would include /author/author-name/, as well as 
  1157. * pagination and feed paths for author archives. 
  1158. * @since 1.5.0 
  1159. * @param array $author_rewrite The rewrite rules for author archives. 
  1160. */ 
  1161. $author_rewrite = apply_filters( 'author_rewrite_rules', $author_rewrite ); 
  1162.  
  1163. // Pages rewrite rules. 
  1164. $page_rewrite = $this->page_rewrite_rules(); 
  1165.  
  1166. /** 
  1167. * Filters rewrite rules used for "page" post type archives. 
  1168. * @since 1.5.0 
  1169. * @param array $page_rewrite The rewrite rules for the "page" post type. 
  1170. */ 
  1171. $page_rewrite = apply_filters( 'page_rewrite_rules', $page_rewrite ); 
  1172.  
  1173. // Extra permastructs. 
  1174. foreach ( $this->extra_permastructs as $permastructname => $struct ) { 
  1175. if ( is_array( $struct ) ) { 
  1176. if ( count( $struct ) == 2 ) 
  1177. $rules = $this->generate_rewrite_rules( $struct[0], $struct[1] ); 
  1178. else 
  1179. $rules = $this->generate_rewrite_rules( $struct['struct'], $struct['ep_mask'], $struct['paged'], $struct['feed'], $struct['forcomments'], $struct['walk_dirs'], $struct['endpoints'] ); 
  1180. } else { 
  1181. $rules = $this->generate_rewrite_rules( $struct ); 
  1182.  
  1183. /** 
  1184. * Filters rewrite rules used for individual permastructs. 
  1185. * The dynamic portion of the hook name, `$permastructname`, refers 
  1186. * to the name of the registered permastruct, e.g. 'post_tag' (tags),  
  1187. * 'category' (categories), etc. 
  1188. * @since 3.1.0 
  1189. * @param array $rules The rewrite rules generated for the current permastruct. 
  1190. */ 
  1191. $rules = apply_filters( $permastructname . '_rewrite_rules', $rules ); 
  1192. if ( 'post_tag' == $permastructname ) { 
  1193.  
  1194. /** 
  1195. * Filters rewrite rules used specifically for Tags. 
  1196. * @since 2.3.0 
  1197. * @deprecated 3.1.0 Use 'post_tag_rewrite_rules' instead 
  1198. * @param array $rules The rewrite rules generated for tags. 
  1199. */ 
  1200. $rules = apply_filters( 'tag_rewrite_rules', $rules ); 
  1201.  
  1202. $this->extra_rules_top = array_merge($this->extra_rules_top, $rules); 
  1203.  
  1204. // Put them together. 
  1205. if ( $this->use_verbose_page_rules ) 
  1206. $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); 
  1207. else 
  1208. $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); 
  1209.  
  1210. /** 
  1211. * Fires after the rewrite rules are generated. 
  1212. * @since 1.5.0 
  1213. * @param WP_Rewrite $this Current WP_Rewrite instance, passed by reference. 
  1214. */ 
  1215. do_action_ref_array( 'generate_rewrite_rules', array( &$this ) ); 
  1216.  
  1217. /** 
  1218. * Filters the full set of generated rewrite rules. 
  1219. * @since 1.5.0 
  1220. * @param array $this->rules The compiled array of rewrite rules. 
  1221. */ 
  1222. $this->rules = apply_filters( 'rewrite_rules_array', $this->rules ); 
  1223.  
  1224. return $this->rules; 
  1225.  
  1226. /** 
  1227. * Retrieves the rewrite rules. 
  1228. * The difference between this method and WP_Rewrite::rewrite_rules() is that 
  1229. * this method stores the rewrite rules in the 'rewrite_rules' option and retrieves 
  1230. * it. This prevents having to process all of the permalinks to get the rewrite rules 
  1231. * in the form of caching. 
  1232. * @since 1.5.0 
  1233. * @access public 
  1234. * @return array Rewrite rules. 
  1235. */ 
  1236. public function wp_rewrite_rules() { 
  1237. $this->rules = get_option('rewrite_rules'); 
  1238. if ( empty($this->rules) ) { 
  1239. $this->matches = 'matches'; 
  1240. $this->rewrite_rules(); 
  1241. update_option('rewrite_rules', $this->rules); 
  1242.  
  1243. return $this->rules; 
  1244.  
  1245. /** 
  1246. * Retrieves mod_rewrite-formatted rewrite rules to write to .htaccess. 
  1247. * Does not actually write to the .htaccess file, but creates the rules for 
  1248. * the process that will. 
  1249. * Will add the non_wp_rules property rules to the .htaccess file before 
  1250. * the WordPress rewrite rules one. 
  1251. * @since 1.5.0 
  1252. * @access public 
  1253. * @return string 
  1254. */ 
  1255. public function mod_rewrite_rules() { 
  1256. if ( ! $this->using_permalinks() ) 
  1257. return ''; 
  1258.  
  1259. $site_root = parse_url( site_url() ); 
  1260. if ( isset( $site_root['path'] ) ) 
  1261. $site_root = trailingslashit($site_root['path']); 
  1262.  
  1263. $home_root = parse_url(home_url()); 
  1264. if ( isset( $home_root['path'] ) ) 
  1265. $home_root = trailingslashit($home_root['path']); 
  1266. else 
  1267. $home_root = '/'; 
  1268.  
  1269. $rules = "<IfModule mod_rewrite.c>\n"; 
  1270. $rules .= "RewriteEngine On\n"; 
  1271. $rules .= "RewriteBase $home_root\n"; 
  1272.  
  1273. // Prevent -f checks on index.php. 
  1274. $rules .= "RewriteRule ^index\.php$ - [L]\n"; 
  1275.  
  1276. // Add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all). 
  1277. foreach ( (array) $this->non_wp_rules as $match => $query) { 
  1278. // Apache 1.3 does not support the reluctant (non-greedy) modifier. 
  1279. $match = str_replace('.+?', '.+', $match); 
  1280.  
  1281. $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA, L]\n"; 
  1282.  
  1283. if ( $this->use_verbose_rules ) { 
  1284. $this->matches = ''; 
  1285. $rewrite = $this->rewrite_rules(); 
  1286. $num_rules = count($rewrite); 
  1287. $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" . 
  1288. "RewriteCond %{REQUEST_FILENAME} -d\n" . 
  1289. "RewriteRule ^.*$ - [S=$num_rules]\n"; 
  1290.  
  1291. foreach ( (array) $rewrite as $match => $query) { 
  1292. // Apache 1.3 does not support the reluctant (non-greedy) modifier. 
  1293. $match = str_replace('.+?', '.+', $match); 
  1294.  
  1295. if ( strpos($query, $this->index) !== false ) 
  1296. $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA, L]\n"; 
  1297. else 
  1298. $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA, L]\n"; 
  1299. } else { 
  1300. $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" . 
  1301. "RewriteCond %{REQUEST_FILENAME} !-d\n" . 
  1302. "RewriteRule . {$home_root}{$this->index} [L]\n"; 
  1303.  
  1304. $rules .= "</IfModule>\n"; 
  1305.  
  1306. /** 
  1307. * Filters the list of rewrite rules formatted for output to an .htaccess file. 
  1308. * @since 1.5.0 
  1309. * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess. 
  1310. */ 
  1311. $rules = apply_filters( 'mod_rewrite_rules', $rules ); 
  1312.  
  1313. /** 
  1314. * Filters the list of rewrite rules formatted for output to an .htaccess file. 
  1315. * @since 1.5.0 
  1316. * @deprecated 1.5.0 Use the mod_rewrite_rules filter instead. 
  1317. * @param string $rules mod_rewrite Rewrite rules formatted for .htaccess. 
  1318. */ 
  1319. return apply_filters( 'rewrite_rules', $rules ); 
  1320.  
  1321. /** 
  1322. * Retrieves IIS7 URL Rewrite formatted rewrite rules to write to web.config file. 
  1323. * Does not actually write to the web.config file, but creates the rules for 
  1324. * the process that will. 
  1325. * @since 2.8.0 
  1326. * @access public 
  1327. * @param bool $add_parent_tags Optional. Whether to add parent tags to the rewrite rule sets. 
  1328. * Default false. 
  1329. * @return string IIS7 URL rewrite rule sets. 
  1330. */ 
  1331. public function iis7_url_rewrite_rules( $add_parent_tags = false ) { 
  1332. if ( ! $this->using_permalinks() ) 
  1333. return ''; 
  1334. $rules = ''; 
  1335. if ( $add_parent_tags ) { 
  1336. $rules .= '<configuration> 
  1337. <system.webServer> 
  1338. <rewrite> 
  1339. <rules>'; 
  1340.  
  1341. $rules .= ' 
  1342. <rule name="WordPress: ' . esc_attr( home_url() ) . '" patternSyntax="Wildcard"> 
  1343. <match url="*" /> 
  1344. <conditions> 
  1345. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
  1346. <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
  1347. </conditions> 
  1348. <action type="Rewrite" url="index.php" /> 
  1349. </rule>'; 
  1350.  
  1351. if ( $add_parent_tags ) { 
  1352. $rules .= ' 
  1353. </rules> 
  1354. </rewrite> 
  1355. </system.webServer> 
  1356. </configuration>'; 
  1357.  
  1358. /** 
  1359. * Filters the list of rewrite rules formatted for output to a web.config. 
  1360. * @since 2.8.0 
  1361. * @param string $rules Rewrite rules formatted for IIS web.config. 
  1362. */ 
  1363. return apply_filters( 'iis7_url_rewrite_rules', $rules ); 
  1364.  
  1365. /** 
  1366. * Adds a rewrite rule that transforms a URL structure to a set of query vars. 
  1367. * Any value in the $after parameter that isn't 'bottom' will result in the rule 
  1368. * being placed at the top of the rewrite rules. 
  1369. * @since 2.1.0 
  1370. * @since 4.4.0 Array support was added to the `$query` parameter. 
  1371. * @access public 
  1372. * @param string $regex Regular expression to match request against. 
  1373. * @param string|array $query The corresponding query vars for this rewrite rule. 
  1374. * @param string $after Optional. Priority of the new rule. Accepts 'top' 
  1375. * or 'bottom'. Default 'bottom'. 
  1376. */ 
  1377. public function add_rule( $regex, $query, $after = 'bottom' ) { 
  1378. if ( is_array( $query ) ) { 
  1379. $external = false; 
  1380. $query = add_query_arg( $query, 'index.php' ); 
  1381. } else { 
  1382. $index = false === strpos( $query, '?' ) ? strlen( $query ) : strpos( $query, '?' ); 
  1383. $front = substr( $query, 0, $index ); 
  1384.  
  1385. $external = $front != $this->index; 
  1386.  
  1387. // "external" = it doesn't correspond to index.php. 
  1388. if ( $external ) { 
  1389. $this->add_external_rule( $regex, $query ); 
  1390. } else { 
  1391. if ( 'bottom' == $after ) { 
  1392. $this->extra_rules = array_merge( $this->extra_rules, array( $regex => $query ) ); 
  1393. } else { 
  1394. $this->extra_rules_top = array_merge( $this->extra_rules_top, array( $regex => $query ) ); 
  1395.  
  1396. /** 
  1397. * Adds a rewrite rule that doesn't correspond to index.php. 
  1398. * @since 2.1.0 
  1399. * @access public 
  1400. * @param string $regex Regular expression to match request against. 
  1401. * @param string $query The corresponding query vars for this rewrite rule. 
  1402. */ 
  1403. public function add_external_rule( $regex, $query ) { 
  1404. $this->non_wp_rules[ $regex ] = $query; 
  1405.  
  1406. /** 
  1407. * Adds an endpoint, like /trackback/. 
  1408. * @since 2.1.0 
  1409. * @since 3.9.0 $query_var parameter added. 
  1410. * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`. 
  1411. * @access public 
  1412. * @see add_rewrite_endpoint() for full documentation. 
  1413. * @global WP $wp 
  1414. * @param string $name Name of the endpoint. 
  1415. * @param int $places Endpoint mask describing the places the endpoint should be added. 
  1416. * @param string|bool $query_var Optional. Name of the corresponding query variable. Pass `false` to 
  1417. * skip registering a query_var for this endpoint. Defaults to the 
  1418. * value of `$name`. 
  1419. */ 
  1420. public function add_endpoint( $name, $places, $query_var = true ) { 
  1421. global $wp; 
  1422.  
  1423. // For backward compatibility, if null has explicitly been passed as `$query_var`, assume `true`. 
  1424. if ( true === $query_var || null === func_get_arg( 2 ) ) { 
  1425. $query_var = $name; 
  1426. $this->endpoints[] = array( $places, $name, $query_var ); 
  1427.  
  1428. if ( $query_var ) { 
  1429. $wp->add_query_var( $query_var ); 
  1430.  
  1431. /** 
  1432. * Adds a new permalink structure. 
  1433. * A permalink structure (permastruct) is an abstract definition of a set of rewrite rules; 
  1434. * it is an easy way of expressing a set of regular expressions that rewrite to a set of 
  1435. * query strings. The new permastruct is added to the WP_Rewrite::$extra_permastructs array. 
  1436. * When the rewrite rules are built by WP_Rewrite::rewrite_rules(), all of these extra 
  1437. * permastructs are passed to WP_Rewrite::generate_rewrite_rules() which transforms them 
  1438. * into the regular expressions that many love to hate. 
  1439. * The `$args` parameter gives you control over how WP_Rewrite::generate_rewrite_rules() 
  1440. * works on the new permastruct. 
  1441. * @since 2.5.0 
  1442. * @access public 
  1443. * @param string $name Name for permalink structure. 
  1444. * @param string $struct Permalink structure (e.g. category/%category%) 
  1445. * @param array $args { 
  1446. * Optional. Arguments for building rewrite rules based on the permalink structure. 
  1447. * Default empty array. 
  1448. * @type bool $with_front Whether the structure should be prepended with `WP_Rewrite::$front`. 
  1449. * Default true. 
  1450. * @type int $ep_mask The endpoint mask defining which endpoints are added to the structure. 
  1451. * Accepts `EP_NONE`, `EP_PERMALINK`, `EP_ATTACHMENT`, `EP_DATE`, `EP_YEAR`,  
  1452. * `EP_MONTH`, `EP_DAY`, `EP_ROOT`, `EP_COMMENTS`, `EP_SEARCH`, `EP_CATEGORIES`,  
  1453. * `EP_TAGS`, `EP_AUTHORS`, `EP_PAGES`, `EP_ALL_ARCHIVES`, and `EP_ALL`. 
  1454. * Default `EP_NONE`. 
  1455. * @type bool $paged Whether archive pagination rules should be added for the structure. 
  1456. * Default true. 
  1457. * @type bool $feed Whether feed rewrite rules should be added for the structure. Default true. 
  1458. * @type bool $forcomments Whether the feed rules should be a query for a comments feed. Default false. 
  1459. * @type bool $walk_dirs Whether the 'directories' making up the structure should be walked over 
  1460. * and rewrite rules built for each in-turn. Default true. 
  1461. * @type bool $endpoints Whether endpoints should be applied to the generated rules. Default true. 
  1462. * } 
  1463. */ 
  1464. public function add_permastruct( $name, $struct, $args = array() ) { 
  1465. // Back-compat for the old parameters: $with_front and $ep_mask. 
  1466. if ( ! is_array( $args ) ) 
  1467. $args = array( 'with_front' => $args ); 
  1468. if ( func_num_args() == 4 ) 
  1469. $args['ep_mask'] = func_get_arg( 3 ); 
  1470.  
  1471. $defaults = array( 
  1472. 'with_front' => true,  
  1473. 'ep_mask' => EP_NONE,  
  1474. 'paged' => true,  
  1475. 'feed' => true,  
  1476. 'forcomments' => false,  
  1477. 'walk_dirs' => true,  
  1478. 'endpoints' => true,  
  1479. ); 
  1480. $args = array_intersect_key( $args, $defaults ); 
  1481. $args = wp_parse_args( $args, $defaults ); 
  1482.  
  1483. if ( $args['with_front'] ) 
  1484. $struct = $this->front . $struct; 
  1485. else 
  1486. $struct = $this->root . $struct; 
  1487. $args['struct'] = $struct; 
  1488.  
  1489. $this->extra_permastructs[ $name ] = $args; 
  1490.  
  1491. /** 
  1492. * Removes a permalink structure. 
  1493. * @since 4.5.0 
  1494. * @access public 
  1495. * @param string $name Name for permalink structure. 
  1496. */ 
  1497. public function remove_permastruct( $name ) { 
  1498. unset( $this->extra_permastructs[ $name ] ); 
  1499.  
  1500. /** 
  1501. * Removes rewrite rules and then recreate rewrite rules. 
  1502. * Calls WP_Rewrite::wp_rewrite_rules() after removing the 'rewrite_rules' option. 
  1503. * If the function named 'save_mod_rewrite_rules' exists, it will be called. 
  1504. * @since 2.0.1 
  1505. * @access public 
  1506. * @staticvar bool $do_hard_later 
  1507. * @param bool $hard Whether to update .htaccess (hard flush) or just update rewrite_rules option (soft flush). Default is true (hard). 
  1508. */ 
  1509. public function flush_rules( $hard = true ) { 
  1510. static $do_hard_later = null; 
  1511.  
  1512. // Prevent this action from running before everyone has registered their rewrites. 
  1513. if ( ! did_action( 'wp_loaded' ) ) { 
  1514. add_action( 'wp_loaded', array( $this, 'flush_rules' ) ); 
  1515. $do_hard_later = ( isset( $do_hard_later ) ) ? $do_hard_later || $hard : $hard; 
  1516. return; 
  1517.  
  1518. if ( isset( $do_hard_later ) ) { 
  1519. $hard = $do_hard_later; 
  1520. unset( $do_hard_later ); 
  1521.  
  1522. update_option( 'rewrite_rules', '' ); 
  1523. $this->wp_rewrite_rules(); 
  1524.  
  1525. /** 
  1526. * Filters whether a "hard" rewrite rule flush should be performed when requested. 
  1527. * A "hard" flush updates .htaccess (Apache) or web.config (IIS). 
  1528. * @since 3.7.0 
  1529. * @param bool $hard Whether to flush rewrite rules "hard". Default true. 
  1530. */ 
  1531. if ( ! $hard || ! apply_filters( 'flush_rewrite_rules_hard', true ) ) { 
  1532. return; 
  1533. if ( function_exists( 'save_mod_rewrite_rules' ) ) 
  1534. save_mod_rewrite_rules(); 
  1535. if ( function_exists( 'iis7_save_url_rewrite_rules' ) ) 
  1536. iis7_save_url_rewrite_rules(); 
  1537.  
  1538. /** 
  1539. * Sets up the object's properties. 
  1540. * The 'use_verbose_page_rules' object property will be set to true if the 
  1541. * permalink structure begins with one of the following: '%postname%', '%category%',  
  1542. * '%tag%', or '%author%'. 
  1543. * @since 1.5.0 
  1544. * @access public 
  1545. */ 
  1546. public function init() { 
  1547. $this->extra_rules = $this->non_wp_rules = $this->endpoints = array(); 
  1548. $this->permalink_structure = get_option('permalink_structure'); 
  1549. $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%')); 
  1550. $this->root = ''; 
  1551.  
  1552. if ( $this->using_index_permalinks() ) 
  1553. $this->root = $this->index . '/'; 
  1554.  
  1555. unset($this->author_structure); 
  1556. unset($this->date_structure); 
  1557. unset($this->page_structure); 
  1558. unset($this->search_structure); 
  1559. unset($this->feed_structure); 
  1560. unset($this->comment_feed_structure); 
  1561. $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) ); 
  1562.  
  1563. // Enable generic rules for pages if permalink structure doesn't begin with a wildcard. 
  1564. if ( preg_match("/^[^%]*%(?:postname|category|tag|author)%/", $this->permalink_structure) ) 
  1565. $this->use_verbose_page_rules = true; 
  1566. else 
  1567. $this->use_verbose_page_rules = false; 
  1568.  
  1569. /** 
  1570. * Sets the main permalink structure for the site. 
  1571. * Will update the 'permalink_structure' option, if there is a difference 
  1572. * between the current permalink structure and the parameter value. Calls 
  1573. * WP_Rewrite::init() after the option is updated. 
  1574. * Fires the {@see 'permalink_structure_changed'} action once the init call has 
  1575. * processed passing the old and new values 
  1576. * @since 1.5.0 
  1577. * @access public 
  1578. * @param string $permalink_structure Permalink structure. 
  1579. */ 
  1580. public function set_permalink_structure($permalink_structure) { 
  1581. if ( $permalink_structure != $this->permalink_structure ) { 
  1582. $old_permalink_structure = $this->permalink_structure; 
  1583. update_option('permalink_structure', $permalink_structure); 
  1584.  
  1585. $this->init(); 
  1586.  
  1587. /** 
  1588. * Fires after the permalink structure is updated. 
  1589. * @since 2.8.0 
  1590. * @param string $old_permalink_structure The previous permalink structure. 
  1591. * @param string $permalink_structure The new permalink structure. 
  1592. */ 
  1593. do_action( 'permalink_structure_changed', $old_permalink_structure, $permalink_structure ); 
  1594.  
  1595. /** 
  1596. * Sets the category base for the category permalink. 
  1597. * Will update the 'category_base' option, if there is a difference between 
  1598. * the current category base and the parameter value. Calls WP_Rewrite::init() 
  1599. * after the option is updated. 
  1600. * @since 1.5.0 
  1601. * @access public 
  1602. * @param string $category_base Category permalink structure base. 
  1603. */ 
  1604. public function set_category_base($category_base) { 
  1605. if ( $category_base != get_option('category_base') ) { 
  1606. update_option('category_base', $category_base); 
  1607. $this->init(); 
  1608.  
  1609. /** 
  1610. * Sets the tag base for the tag permalink. 
  1611. * Will update the 'tag_base' option, if there is a difference between the 
  1612. * current tag base and the parameter value. Calls WP_Rewrite::init() after 
  1613. * the option is updated. 
  1614. * @since 2.3.0 
  1615. * @access public 
  1616. * @param string $tag_base Tag permalink structure base. 
  1617. */ 
  1618. public function set_tag_base( $tag_base ) { 
  1619. if ( $tag_base != get_option( 'tag_base') ) { 
  1620. update_option( 'tag_base', $tag_base ); 
  1621. $this->init(); 
  1622.  
  1623. /** 
  1624. * Constructor - Calls init(), which runs setup. 
  1625. * @since 1.5.0 
  1626. * @access public 
  1627. */ 
  1628. public function __construct() { 
  1629. $this->init();