/wp-includes/class-wp-rewrite.php

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