PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/rewrite.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 1991 lines | 804 code | 227 blank | 960 comment | 141 complexity | 2bdb4f95117864e31fb5522f86d51926 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * WordPress Rewrite API
  4. *
  5. * @package WordPress
  6. * @subpackage Rewrite
  7. */
  8. /**
  9. * Add a straight rewrite rule.
  10. *
  11. * @see WP_Rewrite::add_rule() for long description.
  12. * @since 2.1.0
  13. *
  14. * @param string $regex Regular Expression to match request against.
  15. * @param string $redirect Page to redirect to.
  16. * @param string $after Optional, default is 'bottom'. Where to add rule, can also be 'top'.
  17. */
  18. function add_rewrite_rule($regex, $redirect, $after = 'bottom') {
  19. global $wp_rewrite;
  20. $wp_rewrite->add_rule($regex, $redirect, $after);
  21. }
  22. /**
  23. * Add a new tag (like %postname%).
  24. *
  25. * Warning: you must call this on init or earlier, otherwise the query var
  26. * addition stuff won't work.
  27. *
  28. * @since 2.1.0
  29. *
  30. * @param string $tagname
  31. * @param string $regex
  32. */
  33. function add_rewrite_tag($tagname, $regex) {
  34. //validation
  35. if ( strlen($tagname) < 3 || $tagname[0] != '%' || $tagname[strlen($tagname)-1] != '%' )
  36. return;
  37. $qv = trim($tagname, '%');
  38. global $wp_rewrite, $wp;
  39. $wp->add_query_var($qv);
  40. $wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '=');
  41. }
  42. /**
  43. * Add permalink structure.
  44. *
  45. * @see WP_Rewrite::add_permastruct()
  46. * @since 3.0.0
  47. *
  48. * @param string $name Name for permalink structure.
  49. * @param string $struct Permalink structure.
  50. * @param bool $with_front Prepend front base to permalink structure.
  51. */
  52. function add_permastruct( $name, $struct, $with_front = true, $ep_mask = EP_NONE ) {
  53. global $wp_rewrite;
  54. return $wp_rewrite->add_permastruct( $name, $struct, $with_front, $ep_mask );
  55. }
  56. /**
  57. * Add a new feed type like /atom1/.
  58. *
  59. * @since 2.1.0
  60. *
  61. * @param string $feedname
  62. * @param callback $function Callback to run on feed display.
  63. * @return string Feed action name.
  64. */
  65. function add_feed($feedname, $function) {
  66. global $wp_rewrite;
  67. if ( ! in_array($feedname, $wp_rewrite->feeds) ) //override the file if it is
  68. $wp_rewrite->feeds[] = $feedname;
  69. $hook = 'do_feed_' . $feedname;
  70. // Remove default function hook
  71. remove_action($hook, $hook, 10, 1);
  72. add_action($hook, $function, 10, 1);
  73. return $hook;
  74. }
  75. /**
  76. * Remove rewrite rules and then recreate rewrite rules.
  77. *
  78. * @see WP_Rewrite::flush_rules()
  79. * @since 3.0.0
  80. *
  81. * @param bool $hard Whether to update .htaccess (hard flush) or just update
  82. * rewrite_rules transient (soft flush). Default is true (hard).
  83. */
  84. function flush_rewrite_rules( $hard = true ) {
  85. global $wp_rewrite;
  86. $wp_rewrite->flush_rules( $hard );
  87. }
  88. //pseudo-places
  89. /**
  90. * Endpoint Mask for default, which is nothing.
  91. *
  92. * @since 2.1.0
  93. */
  94. define('EP_NONE', 0);
  95. /**
  96. * Endpoint Mask for Permalink.
  97. *
  98. * @since 2.1.0
  99. */
  100. define('EP_PERMALINK', 1);
  101. /**
  102. * Endpoint Mask for Attachment.
  103. *
  104. * @since 2.1.0
  105. */
  106. define('EP_ATTACHMENT', 2);
  107. /**
  108. * Endpoint Mask for date.
  109. *
  110. * @since 2.1.0
  111. */
  112. define('EP_DATE', 4);
  113. /**
  114. * Endpoint Mask for year
  115. *
  116. * @since 2.1.0
  117. */
  118. define('EP_YEAR', 8);
  119. /**
  120. * Endpoint Mask for month.
  121. *
  122. * @since 2.1.0
  123. */
  124. define('EP_MONTH', 16);
  125. /**
  126. * Endpoint Mask for day.
  127. *
  128. * @since 2.1.0
  129. */
  130. define('EP_DAY', 32);
  131. /**
  132. * Endpoint Mask for root.
  133. *
  134. * @since 2.1.0
  135. */
  136. define('EP_ROOT', 64);
  137. /**
  138. * Endpoint Mask for comments.
  139. *
  140. * @since 2.1.0
  141. */
  142. define('EP_COMMENTS', 128);
  143. /**
  144. * Endpoint Mask for searches.
  145. *
  146. * @since 2.1.0
  147. */
  148. define('EP_SEARCH', 256);
  149. /**
  150. * Endpoint Mask for categories.
  151. *
  152. * @since 2.1.0
  153. */
  154. define('EP_CATEGORIES', 512);
  155. /**
  156. * Endpoint Mask for tags.
  157. *
  158. * @since 2.3.0
  159. */
  160. define('EP_TAGS', 1024);
  161. /**
  162. * Endpoint Mask for authors.
  163. *
  164. * @since 2.1.0
  165. */
  166. define('EP_AUTHORS', 2048);
  167. /**
  168. * Endpoint Mask for pages.
  169. *
  170. * @since 2.1.0
  171. */
  172. define('EP_PAGES', 4096);
  173. /**
  174. * Endpoint Mask for everything.
  175. *
  176. * @since 2.1.0
  177. */
  178. define('EP_ALL', 8191);
  179. /**
  180. * Add an endpoint, like /trackback/.
  181. *
  182. * The endpoints are added to the end of the request. So a request matching
  183. * "/2008/10/14/my_post/myep/", the endpoint will be "/myep/".
  184. *
  185. * Be sure to flush the rewrite rules (wp_rewrite->flush_rules()) when your plugin gets
  186. * activated (register_activation_hook()) and deactivated (register_deactivation_hook())
  187. *
  188. * @since 2.1.0
  189. * @see WP_Rewrite::add_endpoint() Parameters and more description.
  190. * @uses $wp_rewrite
  191. *
  192. * @param unknown_type $name
  193. * @param unknown_type $places
  194. */
  195. function add_rewrite_endpoint($name, $places) {
  196. global $wp_rewrite;
  197. $wp_rewrite->add_endpoint($name, $places);
  198. }
  199. /**
  200. * Filter the URL base for taxonomies.
  201. *
  202. * To remove any manually prepended /index.php/.
  203. *
  204. * @access private
  205. * @since 2.6.0
  206. *
  207. * @param string $base The taxonomy base that we're going to filter
  208. * @return string
  209. */
  210. function _wp_filter_taxonomy_base( $base ) {
  211. if ( !empty( $base ) ) {
  212. $base = preg_replace( '|^/index\.php/|', '', $base );
  213. $base = trim( $base, '/' );
  214. }
  215. return $base;
  216. }
  217. /**
  218. * Examine a url and try to determine the post ID it represents.
  219. *
  220. * Checks are supposedly from the hosted site blog.
  221. *
  222. * @since 1.0.0
  223. *
  224. * @param string $url Permalink to check.
  225. * @return int Post ID, or 0 on failure.
  226. */
  227. function url_to_postid($url) {
  228. global $wp_rewrite;
  229. $url = apply_filters('url_to_postid', $url);
  230. // First, check to see if there is a 'p=N' or 'page_id=N' to match against
  231. if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
  232. $id = absint($values[2]);
  233. if ( $id )
  234. return $id;
  235. }
  236. // Check to see if we are using rewrite rules
  237. $rewrite = $wp_rewrite->wp_rewrite_rules();
  238. // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
  239. if ( empty($rewrite) )
  240. return 0;
  241. // Get rid of the #anchor
  242. $url_split = explode('#', $url);
  243. $url = $url_split[0];
  244. // Get rid of URL ?query=string
  245. $url_split = explode('?', $url);
  246. $url = $url_split[0];
  247. // Add 'www.' if it is absent and should be there
  248. if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
  249. $url = str_replace('://', '://www.', $url);
  250. // Strip 'www.' if it is present and shouldn't be
  251. if ( false === strpos(home_url(), '://www.') )
  252. $url = str_replace('://www.', '://', $url);
  253. // Strip 'index.php/' if we're not using path info permalinks
  254. if ( !$wp_rewrite->using_index_permalinks() )
  255. $url = str_replace('index.php/', '', $url);
  256. if ( false !== strpos($url, home_url()) ) {
  257. // Chop off http://domain.com
  258. $url = str_replace(home_url(), '', $url);
  259. } else {
  260. // Chop off /path/to/blog
  261. $home_path = parse_url(home_url());
  262. $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
  263. $url = str_replace($home_path, '', $url);
  264. }
  265. // Trim leading and lagging slashes
  266. $url = trim($url, '/');
  267. $request = $url;
  268. // Look for matches.
  269. $request_match = $request;
  270. foreach ( (array)$rewrite as $match => $query) {
  271. // If the requesting file is the anchor of the match, prepend it
  272. // to the path info.
  273. if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
  274. $request_match = $url . '/' . $request;
  275. if ( preg_match("!^$match!", $request_match, $matches) ) {
  276. // Got a match.
  277. // Trim the query of everything up to the '?'.
  278. $query = preg_replace("!^.+\?!", '', $query);
  279. // Substitute the substring matches into the query.
  280. $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
  281. // Filter out non-public query vars
  282. global $wp;
  283. parse_str($query, $query_vars);
  284. $query = array();
  285. foreach ( (array) $query_vars as $key => $value ) {
  286. if ( in_array($key, $wp->public_query_vars) )
  287. $query[$key] = $value;
  288. }
  289. // Do the query
  290. $query = new WP_Query($query);
  291. if ( !empty($query->posts) && $query->is_singular )
  292. return $query->post->ID;
  293. else
  294. return 0;
  295. }
  296. }
  297. return 0;
  298. }
  299. /**
  300. * WordPress Rewrite Component.
  301. *
  302. * The WordPress Rewrite class writes the rewrite module rules to the .htaccess
  303. * file. It also handles parsing the request to get the correct setup for the
  304. * WordPress Query class.
  305. *
  306. * The Rewrite along with WP class function as a front controller for WordPress.
  307. * You can add rules to trigger your page view and processing using this
  308. * component. The full functionality of a front controller does not exist,
  309. * meaning you can't define how the template files load based on the rewrite
  310. * rules.
  311. *
  312. * @since 1.5.0
  313. */
  314. class WP_Rewrite {
  315. /**
  316. * Default permalink structure for WordPress.
  317. *
  318. * @since 1.5.0
  319. * @access private
  320. * @var string
  321. */
  322. var $permalink_structure;
  323. /**
  324. * Whether to add trailing slashes.
  325. *
  326. * @since 2.2.0
  327. * @access private
  328. * @var bool
  329. */
  330. var $use_trailing_slashes;
  331. /**
  332. * Permalink author request base ( example.com/author/authorname ).
  333. *
  334. * @since 1.5.0
  335. * @access private
  336. * @var string
  337. */
  338. var $author_base = 'author';
  339. /**
  340. * Permalink request structure for author pages.
  341. *
  342. * @since 1.5.0
  343. * @access private
  344. * @var string
  345. */
  346. var $author_structure;
  347. /**
  348. * Permalink request structure for dates.
  349. *
  350. * @since 1.5.0
  351. * @access private
  352. * @var string
  353. */
  354. var $date_structure;
  355. /**
  356. * Permalink request structure for pages.
  357. *
  358. * @since 1.5.0
  359. * @access private
  360. * @var string
  361. */
  362. var $page_structure;
  363. /**
  364. * Search permalink base ( example.com/search/query ).
  365. *
  366. * @since 1.5.0
  367. * @access private
  368. * @var string
  369. */
  370. var $search_base = 'search';
  371. /**
  372. * Permalink request structure for searches.
  373. *
  374. * @since 1.5.0
  375. * @access private
  376. * @var string
  377. */
  378. var $search_structure;
  379. /**
  380. * Comments permalink base.
  381. *
  382. * @since 1.5.0
  383. * @access private
  384. * @var string
  385. */
  386. var $comments_base = 'comments';
  387. /**
  388. * Pagination permalink base.
  389. *
  390. * @since 3.1.0
  391. * @access private
  392. * @var string
  393. */
  394. var $pagination_base = 'page';
  395. /**
  396. * Feed permalink base.
  397. *
  398. * @since 1.5.0
  399. * @access private
  400. * @var string
  401. */
  402. var $feed_base = 'feed';
  403. /**
  404. * Comments feed request structure permalink.
  405. *
  406. * @since 1.5.0
  407. * @access private
  408. * @var string
  409. */
  410. var $comments_feed_structure;
  411. /**
  412. * Feed request structure permalink.
  413. *
  414. * @since 1.5.0
  415. * @access private
  416. * @var string
  417. */
  418. var $feed_structure;
  419. /**
  420. * Front URL path.
  421. *
  422. * The difference between the root property is that WordPress might be
  423. * located at example/WordPress/index.php, if permalinks are turned off. The
  424. * WordPress/index.php will be the front portion. If permalinks are turned
  425. * on, this will most likely be empty or not set.
  426. *
  427. * @since 1.5.0
  428. * @access private
  429. * @var string
  430. */
  431. var $front;
  432. /**
  433. * Root URL path to WordPress (without domain).
  434. *
  435. * The difference between front property is that WordPress might be located
  436. * at example.com/WordPress/. The root is the 'WordPress/' portion.
  437. *
  438. * @since 1.5.0
  439. * @access private
  440. * @var string
  441. */
  442. var $root = '';
  443. /**
  444. * Permalink to the home page.
  445. *
  446. * @since 1.5.0
  447. * @access public
  448. * @var string
  449. */
  450. var $index = 'index.php';
  451. /**
  452. * Request match string.
  453. *
  454. * @since 1.5.0
  455. * @access private
  456. * @var string
  457. */
  458. var $matches = '';
  459. /**
  460. * Rewrite rules to match against the request to find the redirect or query.
  461. *
  462. * @since 1.5.0
  463. * @access private
  464. * @var array
  465. */
  466. var $rules;
  467. /**
  468. * Additional rules added external to the rewrite class.
  469. *
  470. * Those not generated by the class, see add_rewrite_rule().
  471. *
  472. * @since 2.1.0
  473. * @access private
  474. * @var array
  475. */
  476. var $extra_rules = array(); //
  477. /**
  478. * Additional rules that belong at the beginning to match first.
  479. *
  480. * Those not generated by the class, see add_rewrite_rule().
  481. *
  482. * @since 2.3.0
  483. * @access private
  484. * @var array
  485. */
  486. var $extra_rules_top = array(); //
  487. /**
  488. * Rules that don't redirect to WP's index.php.
  489. *
  490. * These rules are written to the mod_rewrite portion of the .htaccess.
  491. *
  492. * @since 2.1.0
  493. * @access private
  494. * @var array
  495. */
  496. var $non_wp_rules = array(); //
  497. /**
  498. * Extra permalink structures.
  499. *
  500. * @since 2.1.0
  501. * @access private
  502. * @var array
  503. */
  504. var $extra_permastructs = array();
  505. /**
  506. * Endpoints permalinks
  507. *
  508. * @since 2.1.0
  509. * @access private
  510. * @var array
  511. */
  512. var $endpoints;
  513. /**
  514. * Whether to write every mod_rewrite rule for WordPress.
  515. *
  516. * This is off by default, turning it on might print a lot of rewrite rules
  517. * to the .htaccess file.
  518. *
  519. * @since 2.0.0
  520. * @access public
  521. * @var bool
  522. */
  523. var $use_verbose_rules = false;
  524. /**
  525. * Whether to write every mod_rewrite rule for WordPress pages.
  526. *
  527. * @since 2.5.0
  528. * @access public
  529. * @var bool
  530. */
  531. var $use_verbose_page_rules = true;
  532. /**
  533. * Permalink structure search for preg_replace.
  534. *
  535. * @since 1.5.0
  536. * @access private
  537. * @var array
  538. */
  539. var $rewritecode =
  540. array(
  541. '%year%',
  542. '%monthnum%',
  543. '%day%',
  544. '%hour%',
  545. '%minute%',
  546. '%second%',
  547. '%postname%',
  548. '%post_id%',
  549. '%author%',
  550. '%pagename%',
  551. '%search%'
  552. );
  553. /**
  554. * Preg_replace values for the search, see {@link WP_Rewrite::$rewritecode}.
  555. *
  556. * @since 1.5.0
  557. * @access private
  558. * @var array
  559. */
  560. var $rewritereplace =
  561. array(
  562. '([0-9]{4})',
  563. '([0-9]{1,2})',
  564. '([0-9]{1,2})',
  565. '([0-9]{1,2})',
  566. '([0-9]{1,2})',
  567. '([0-9]{1,2})',
  568. '([^/]+)',
  569. '([0-9]+)',
  570. '([^/]+)',
  571. '([^/]+?)',
  572. '(.+)'
  573. );
  574. /**
  575. * Search for the query to look for replacing.
  576. *
  577. * @since 1.5.0
  578. * @access private
  579. * @var array
  580. */
  581. var $queryreplace =
  582. array (
  583. 'year=',
  584. 'monthnum=',
  585. 'day=',
  586. 'hour=',
  587. 'minute=',
  588. 'second=',
  589. 'name=',
  590. 'p=',
  591. 'author_name=',
  592. 'pagename=',
  593. 's='
  594. );
  595. /**
  596. * Supported default feeds.
  597. *
  598. * @since 1.5.0
  599. * @access private
  600. * @var array
  601. */
  602. var $feeds = array ( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
  603. /**
  604. * Whether permalinks are being used.
  605. *
  606. * This can be either rewrite module or permalink in the HTTP query string.
  607. *
  608. * @since 1.5.0
  609. * @access public
  610. *
  611. * @return bool True, if permalinks are enabled.
  612. */
  613. function using_permalinks() {
  614. return ! empty($this->permalink_structure);
  615. }
  616. /**
  617. * Whether permalinks are being used and rewrite module is not enabled.
  618. *
  619. * Means that permalink links are enabled and index.php is in the URL.
  620. *
  621. * @since 1.5.0
  622. * @access public
  623. *
  624. * @return bool
  625. */
  626. function using_index_permalinks() {
  627. if ( empty($this->permalink_structure) )
  628. return false;
  629. // If the index is not in the permalink, we're using mod_rewrite.
  630. if ( preg_match('#^/*' . $this->index . '#', $this->permalink_structure) )
  631. return true;
  632. return false;
  633. }
  634. /**
  635. * Whether permalinks are being used and rewrite module is enabled.
  636. *
  637. * Using permalinks and index.php is not in the URL.
  638. *
  639. * @since 1.5.0
  640. * @access public
  641. *
  642. * @return bool
  643. */
  644. function using_mod_rewrite_permalinks() {
  645. if ( $this->using_permalinks() && ! $this->using_index_permalinks() )
  646. return true;
  647. else
  648. return false;
  649. }
  650. /**
  651. * Index for matches for usage in preg_*() functions.
  652. *
  653. * The format of the string is, with empty matches property value, '$NUM'.
  654. * The 'NUM' will be replaced with the value in the $number parameter. With
  655. * the matches property not empty, the value of the returned string will
  656. * contain that value of the matches property. The format then will be
  657. * '$MATCHES[NUM]', with MATCHES as the value in the property and NUM the
  658. * value of the $number parameter.
  659. *
  660. * @since 1.5.0
  661. * @access public
  662. *
  663. * @param int $number Index number.
  664. * @return string
  665. */
  666. function preg_index($number) {
  667. $match_prefix = '$';
  668. $match_suffix = '';
  669. if ( ! empty($this->matches) ) {
  670. $match_prefix = '$' . $this->matches . '[';
  671. $match_suffix = ']';
  672. }
  673. return "$match_prefix$number$match_suffix";
  674. }
  675. /**
  676. * Retrieve all page and attachments for pages URIs.
  677. *
  678. * The attachments are for those that have pages as parents and will be
  679. * retrieved.
  680. *
  681. * @since 2.5.0
  682. * @access public
  683. *
  684. * @return array Array of page URIs as first element and attachment URIs as second element.
  685. */
  686. function page_uri_index() {
  687. global $wpdb;
  688. //get pages in order of hierarchy, i.e. children after parents
  689. $posts = get_page_hierarchy( $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page' AND post_status != 'auto-draft'") );
  690. // If we have no pages get out quick
  691. if ( !$posts )
  692. return array( array(), array() );
  693. //now reverse it, because we need parents after children for rewrite rules to work properly
  694. $posts = array_reverse($posts, true);
  695. $page_uris = array();
  696. $page_attachment_uris = array();
  697. foreach ( $posts as $id => $post ) {
  698. // URL => page name
  699. $uri = get_page_uri($id);
  700. $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
  701. if ( !empty($attachments) ) {
  702. foreach ( $attachments as $attachment ) {
  703. $attach_uri = get_page_uri($attachment->ID);
  704. $page_attachment_uris[$attach_uri] = $attachment->ID;
  705. }
  706. }
  707. $page_uris[$uri] = $id;
  708. }
  709. return array( $page_uris, $page_attachment_uris );
  710. }
  711. /**
  712. * Retrieve all of the rewrite rules for pages.
  713. *
  714. * If the 'use_verbose_page_rules' property is false, then there will only
  715. * be a single rewrite rule for pages for those matching '%pagename%'. With
  716. * the property set to true, the attachments and the pages will be added for
  717. * each individual attachment URI and page URI, respectively.
  718. *
  719. * @since 1.5.0
  720. * @access public
  721. *
  722. * @return array
  723. */
  724. function page_rewrite_rules() {
  725. $rewrite_rules = array();
  726. $page_structure = $this->get_page_permastruct();
  727. if ( ! $this->use_verbose_page_rules ) {
  728. $this->add_rewrite_tag('%pagename%', "(.+?)", 'pagename=');
  729. $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
  730. return $rewrite_rules;
  731. }
  732. $page_uris = $this->page_uri_index();
  733. $uris = $page_uris[0];
  734. $attachment_uris = $page_uris[1];
  735. if ( is_array( $attachment_uris ) ) {
  736. foreach ( $attachment_uris as $uri => $pagename ) {
  737. $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
  738. $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
  739. }
  740. }
  741. if ( is_array( $uris ) ) {
  742. foreach ( $uris as $uri => $pagename ) {
  743. $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
  744. $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
  745. }
  746. }
  747. return $rewrite_rules;
  748. }
  749. /**
  750. * Retrieve date permalink structure, with year, month, and day.
  751. *
  752. * The permalink structure for the date, if not set already depends on the
  753. * permalink structure. It can be one of three formats. The first is year,
  754. * month, day; the second is day, month, year; and the last format is month,
  755. * day, year. These are matched against the permalink structure for which
  756. * one is used. If none matches, then the default will be used, which is
  757. * year, month, day.
  758. *
  759. * Prevents post ID and date permalinks from overlapping. In the case of
  760. * post_id, the date permalink will be prepended with front permalink with
  761. * 'date/' before the actual permalink to form the complete date permalink
  762. * structure.
  763. *
  764. * @since 1.5.0
  765. * @access public
  766. *
  767. * @return bool|string False on no permalink structure. Date permalink structure.
  768. */
  769. function get_date_permastruct() {
  770. if ( isset($this->date_structure) )
  771. return $this->date_structure;
  772. if ( empty($this->permalink_structure) ) {
  773. $this->date_structure = '';
  774. return false;
  775. }
  776. // The date permalink must have year, month, and day separated by slashes.
  777. $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
  778. $this->date_structure = '';
  779. $date_endian = '';
  780. foreach ( $endians as $endian ) {
  781. if ( false !== strpos($this->permalink_structure, $endian) ) {
  782. $date_endian= $endian;
  783. break;
  784. }
  785. }
  786. if ( empty($date_endian) )
  787. $date_endian = '%year%/%monthnum%/%day%';
  788. // Do not allow the date tags and %post_id% to overlap in the permalink
  789. // structure. If they do, move the date tags to $front/date/.
  790. $front = $this->front;
  791. preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
  792. $tok_index = 1;
  793. foreach ( (array) $tokens[0] as $token) {
  794. if ( '%post_id%' == $token && ($tok_index <= 3) ) {
  795. $front = $front . 'date/';
  796. break;
  797. }
  798. $tok_index++;
  799. }
  800. $this->date_structure = $front . $date_endian;
  801. return $this->date_structure;
  802. }
  803. /**
  804. * Retrieve the year permalink structure without month and day.
  805. *
  806. * Gets the date permalink structure and strips out the month and day
  807. * permalink structures.
  808. *
  809. * @since 1.5.0
  810. * @access public
  811. *
  812. * @return bool|string False on failure. Year structure on success.
  813. */
  814. function get_year_permastruct() {
  815. $structure = $this->get_date_permastruct($this->permalink_structure);
  816. if ( empty($structure) )
  817. return false;
  818. $structure = str_replace('%monthnum%', '', $structure);
  819. $structure = str_replace('%day%', '', $structure);
  820. $structure = preg_replace('#/+#', '/', $structure);
  821. return $structure;
  822. }
  823. /**
  824. * Retrieve the month permalink structure without day and with year.
  825. *
  826. * Gets the date permalink structure and strips out the day permalink
  827. * structures. Keeps the year permalink structure.
  828. *
  829. * @since 1.5.0
  830. * @access public
  831. *
  832. * @return bool|string False on failure. Year/Month structure on success.
  833. */
  834. function get_month_permastruct() {
  835. $structure = $this->get_date_permastruct($this->permalink_structure);
  836. if ( empty($structure) )
  837. return false;
  838. $structure = str_replace('%day%', '', $structure);
  839. $structure = preg_replace('#/+#', '/', $structure);
  840. return $structure;
  841. }
  842. /**
  843. * Retrieve the day permalink structure with month and year.
  844. *
  845. * Keeps date permalink structure with all year, month, and day.
  846. *
  847. * @since 1.5.0
  848. * @access public
  849. *
  850. * @return bool|string False on failure. Year/Month/Day structure on success.
  851. */
  852. function get_day_permastruct() {
  853. return $this->get_date_permastruct($this->permalink_structure);
  854. }
  855. /**
  856. * Retrieve the permalink structure for categories.
  857. *
  858. * If the category_base property has no value, then the category structure
  859. * will have the front property value, followed by 'category', and finally
  860. * '%category%'. If it does, then the root property will be used, along with
  861. * the category_base property value.
  862. *
  863. * @since 1.5.0
  864. * @access public
  865. *
  866. * @return bool|string False on failure. Category permalink structure.
  867. */
  868. function get_category_permastruct() {
  869. return $this->get_extra_permastruct('category');
  870. }
  871. /**
  872. * Retrieve the permalink structure for tags.
  873. *
  874. * If the tag_base property has no value, then the tag structure will have
  875. * the front property value, followed by 'tag', and finally '%tag%'. If it
  876. * does, then the root property will be used, along with the tag_base
  877. * property value.
  878. *
  879. * @since 2.3.0
  880. * @access public
  881. *
  882. * @return bool|string False on failure. Tag permalink structure.
  883. */
  884. function get_tag_permastruct() {
  885. return $this->get_extra_permastruct('post_tag');
  886. }
  887. /**
  888. * Retrieve extra permalink structure by name.
  889. *
  890. * @since 2.5.0
  891. * @access public
  892. *
  893. * @param string $name Permalink structure name.
  894. * @return string|bool False if not found. Permalink structure string.
  895. */
  896. function get_extra_permastruct($name) {
  897. if ( empty($this->permalink_structure) )
  898. return false;
  899. if ( isset($this->extra_permastructs[$name]) )
  900. return $this->extra_permastructs[$name][0];
  901. return false;
  902. }
  903. /**
  904. * Retrieve the author permalink structure.
  905. *
  906. * The permalink structure is front property, author base, and finally
  907. * '/%author%'. Will set the author_structure property and then return it
  908. * without attempting to set the value again.
  909. *
  910. * @since 1.5.0
  911. * @access public
  912. *
  913. * @return string|bool False if not found. Permalink structure string.
  914. */
  915. function get_author_permastruct() {
  916. if ( isset($this->author_structure) )
  917. return $this->author_structure;
  918. if ( empty($this->permalink_structure) ) {
  919. $this->author_structure = '';
  920. return false;
  921. }
  922. $this->author_structure = $this->front . $this->author_base . '/%author%';
  923. return $this->author_structure;
  924. }
  925. /**
  926. * Retrieve the search permalink structure.
  927. *
  928. * The permalink structure is root property, search base, and finally
  929. * '/%search%'. Will set the search_structure property and then return it
  930. * without attempting to set the value again.
  931. *
  932. * @since 1.5.0
  933. * @access public
  934. *
  935. * @return string|bool False if not found. Permalink structure string.
  936. */
  937. function get_search_permastruct() {
  938. if ( isset($this->search_structure) )
  939. return $this->search_structure;
  940. if ( empty($this->permalink_structure) ) {
  941. $this->search_structure = '';
  942. return false;
  943. }
  944. $this->search_structure = $this->root . $this->search_base . '/%search%';
  945. return $this->search_structure;
  946. }
  947. /**
  948. * Retrieve the page permalink structure.
  949. *
  950. * The permalink structure is root property, and '%pagename%'. Will set the
  951. * page_structure property and then return it without attempting to set the
  952. * value again.
  953. *
  954. * @since 1.5.0
  955. * @access public
  956. *
  957. * @return string|bool False if not found. Permalink structure string.
  958. */
  959. function get_page_permastruct() {
  960. if ( isset($this->page_structure) )
  961. return $this->page_structure;
  962. if (empty($this->permalink_structure)) {
  963. $this->page_structure = '';
  964. return false;
  965. }
  966. $this->page_structure = $this->root . '%pagename%';
  967. return $this->page_structure;
  968. }
  969. /**
  970. * Retrieve the feed permalink structure.
  971. *
  972. * The permalink structure is root property, feed base, and finally
  973. * '/%feed%'. Will set the feed_structure property and then return it
  974. * without attempting to set the value again.
  975. *
  976. * @since 1.5.0
  977. * @access public
  978. *
  979. * @return string|bool False if not found. Permalink structure string.
  980. */
  981. function get_feed_permastruct() {
  982. if ( isset($this->feed_structure) )
  983. return $this->feed_structure;
  984. if ( empty($this->permalink_structure) ) {
  985. $this->feed_structure = '';
  986. return false;
  987. }
  988. $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
  989. return $this->feed_structure;
  990. }
  991. /**
  992. * Retrieve the comment feed permalink structure.
  993. *
  994. * The permalink structure is root property, comment base property, feed
  995. * base and finally '/%feed%'. Will set the comment_feed_structure property
  996. * and then return it without attempting to set the value again.
  997. *
  998. * @since 1.5.0
  999. * @access public
  1000. *
  1001. * @return string|bool False if not found. Permalink structure string.
  1002. */
  1003. function get_comment_feed_permastruct() {
  1004. if ( isset($this->comment_feed_structure) )
  1005. return $this->comment_feed_structure;
  1006. if (empty($this->permalink_structure)) {
  1007. $this->comment_feed_structure = '';
  1008. return false;
  1009. }
  1010. $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
  1011. return $this->comment_feed_structure;
  1012. }
  1013. /**
  1014. * Append or update tag, pattern, and query for replacement.
  1015. *
  1016. * If the tag already exists, replace the existing pattern and query for
  1017. * that tag, otherwise add the new tag, pattern, and query to the end of the
  1018. * arrays.
  1019. *
  1020. * @internal What is the purpose of this function again? Need to finish long
  1021. * description.
  1022. *
  1023. * @since 1.5.0
  1024. * @access public
  1025. *
  1026. * @param string $tag Append tag to rewritecode property array.
  1027. * @param string $pattern Append pattern to rewritereplace property array.
  1028. * @param string $query Append query to queryreplace property array.
  1029. */
  1030. function add_rewrite_tag($tag, $pattern, $query) {
  1031. $position = array_search($tag, $this->rewritecode);
  1032. if ( false !== $position && null !== $position ) {
  1033. $this->rewritereplace[$position] = $pattern;
  1034. $this->queryreplace[$position] = $query;
  1035. } else {
  1036. $this->rewritecode[] = $tag;
  1037. $this->rewritereplace[] = $pattern;
  1038. $this->queryreplace[] = $query;
  1039. }
  1040. }
  1041. /**
  1042. * Generate the rules from permalink structure.
  1043. *
  1044. * The main WP_Rewrite function for building the rewrite rule list. The
  1045. * contents of the function is a mix of black magic and regular expressions,
  1046. * so best just ignore the contents and move to the parameters.
  1047. *
  1048. * @since 1.5.0
  1049. * @access public
  1050. *
  1051. * @param string $permalink_structure The permalink structure.
  1052. * @param int $ep_mask Optional, default is EP_NONE. Endpoint constant, see EP_* constants.
  1053. * @param bool $paged Optional, default is true. Whether permalink request is paged.
  1054. * @param bool $feed Optional, default is true. Whether for feed.
  1055. * @param bool $forcomments Optional, default is false. Whether for comments.
  1056. * @param bool $walk_dirs Optional, default is true. Whether to create list of directories to walk over.
  1057. * @param bool $endpoints Optional, default is true. Whether endpoints are enabled.
  1058. * @return array Rewrite rule list.
  1059. */
  1060. function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
  1061. //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
  1062. $feedregex2 = '';
  1063. foreach ( (array) $this->feeds as $feed_name)
  1064. $feedregex2 .= $feed_name . '|';
  1065. $feedregex2 = '(' . trim($feedregex2, '|') . ')/?$';
  1066. //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
  1067. //and <permalink>/atom are both possible
  1068. $feedregex = $this->feed_base . '/' . $feedregex2;
  1069. //build a regex to match the trackback and page/xx parts of URLs
  1070. $trackbackregex = 'trackback/?$';
  1071. $pageregex = $this->pagination_base . '/?([0-9]{1,})/?$';
  1072. $commentregex = 'comment-page-([0-9]{1,})/?$';
  1073. //build up an array of endpoint regexes to append => queries to append
  1074. if ( $endpoints ) {
  1075. $ep_query_append = array ();
  1076. foreach ( (array) $this->endpoints as $endpoint) {
  1077. //match everything after the endpoint name, but allow for nothing to appear there
  1078. $epmatch = $endpoint[1] . '(/(.*))?/?$';
  1079. //this will be appended on to the rest of the query for each dir
  1080. $epquery = '&' . $endpoint[1] . '=';
  1081. $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
  1082. }
  1083. }
  1084. //get everything up to the first rewrite tag
  1085. $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
  1086. //build an array of the tags (note that said array ends up being in $tokens[0])
  1087. preg_match_all('/%.+?%/', $permalink_structure, $tokens);
  1088. $num_tokens = count($tokens[0]);
  1089. $index = $this->index; //probably 'index.php'
  1090. $feedindex = $index;
  1091. $trackbackindex = $index;
  1092. //build a list from the rewritecode and queryreplace arrays, that will look something like
  1093. //tagname=$matches[i] where i is the current $i
  1094. for ( $i = 0; $i < $num_tokens; ++$i ) {
  1095. if ( 0 < $i )
  1096. $queries[$i] = $queries[$i - 1] . '&';
  1097. else
  1098. $queries[$i] = '';
  1099. $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
  1100. $queries[$i] .= $query_token;
  1101. }
  1102. //get the structure, minus any cruft (stuff that isn't tags) at the front
  1103. $structure = $permalink_structure;
  1104. if ( $front != '/' )
  1105. $structure = str_replace($front, '', $structure);
  1106. //create a list of dirs to walk over, making rewrite rules for each level
  1107. //so for example, a $structure of /%year%/%month%/%postname% would create
  1108. //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname%
  1109. $structure = trim($structure, '/');
  1110. $dirs = $walk_dirs ? explode('/', $structure) : array( $structure );
  1111. $num_dirs = count($dirs);
  1112. //strip slashes from the front of $front
  1113. $front = preg_replace('|^/+|', '', $front);
  1114. //the main workhorse loop
  1115. $post_rewrite = array();
  1116. $struct = $front;
  1117. for ( $j = 0; $j < $num_dirs; ++$j ) {
  1118. //get the struct for this dir, and trim slashes off the front
  1119. $struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
  1120. $struct = ltrim($struct, '/');
  1121. //replace tags with regexes
  1122. $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
  1123. //make a list of tags, and store how many there are in $num_toks
  1124. $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
  1125. //get the 'tagname=$matches[i]'
  1126. $query = ( isset($queries) && is_array($queries) ) ? $queries[$num_toks - 1] : '';
  1127. //set up $ep_mask_specific which is used to match more specific URL types
  1128. switch ( $dirs[$j] ) {
  1129. case '%year%':
  1130. $ep_mask_specific = EP_YEAR;
  1131. break;
  1132. case '%monthnum%':
  1133. $ep_mask_specific = EP_MONTH;
  1134. break;
  1135. case '%day%':
  1136. $ep_mask_specific = EP_DAY;
  1137. break;
  1138. default:
  1139. $ep_mask_specific = EP_NONE;
  1140. }
  1141. //create query for /page/xx
  1142. $pagematch = $match . $pageregex;
  1143. $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
  1144. //create query for /comment-page-xx
  1145. $commentmatch = $match . $commentregex;
  1146. $commentquery = $index . '?' . $query . '&cpage=' . $this->preg_index($num_toks + 1);
  1147. if ( get_option('page_on_front') ) {
  1148. //create query for Root /comment-page-xx
  1149. $rootcommentmatch = $match . $commentregex;
  1150. $rootcommentquery = $index . '?' . $query . '&page_id=' . get_option('page_on_front') . '&cpage=' . $this->preg_index($num_toks + 1);
  1151. }
  1152. //create query for /feed/(feed|atom|rss|rss2|rdf)
  1153. $feedmatch = $match . $feedregex;
  1154. $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
  1155. //create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
  1156. $feedmatch2 = $match . $feedregex2;
  1157. $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
  1158. //if asked to, turn the feed queries into comment feed ones
  1159. if ( $forcomments ) {
  1160. $feedquery .= '&withcomments=1';
  1161. $feedquery2 .= '&withcomments=1';
  1162. }
  1163. //start creating the array of rewrites for this dir
  1164. $rewrite = array();
  1165. if ( $feed ) //...adding on /feed/ regexes => queries
  1166. $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
  1167. if ( $paged ) //...and /page/xx ones
  1168. $rewrite = array_merge($rewrite, array($pagematch => $pagequery));
  1169. //only on pages with comments add ../comment-page-xx/
  1170. if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask )
  1171. $rewrite = array_merge($rewrite, array($commentmatch => $commentquery));
  1172. else if ( EP_ROOT & $ep_mask && get_option('page_on_front') )
  1173. $rewrite = array_merge($rewrite, array($rootcommentmatch => $rootcommentquery));
  1174. //do endpoints
  1175. if ( $endpoints ) {
  1176. foreach ( (array) $ep_query_append as $regex => $ep) {
  1177. //add the endpoints on if the mask fits
  1178. if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific )
  1179. $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
  1180. }
  1181. }
  1182. //if we've got some tags in this dir
  1183. if ( $num_toks ) {
  1184. $post = false;
  1185. $page = false;
  1186. //check to see if this dir is permalink-level: i.e. the structure specifies an
  1187. //individual post. Do this by checking it contains at least one of 1) post name,
  1188. //2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
  1189. //minute all present). Set these flags now as we need them for the endpoints.
  1190. if ( strpos($struct, '%postname%') !== false
  1191. || strpos($struct, '%post_id%') !== false
  1192. || strpos($struct, '%pagename%') !== false
  1193. || (strpos($struct, '%year%') !== false && strpos($struct, '%monthnum%') !== false && strpos($struct, '%day%') !== false && strpos($struct, '%hour%') !== false && strpos($struct, '%minute%') !== false && strpos($struct, '%second%') !== false)
  1194. ) {
  1195. $post = true;
  1196. if ( strpos($struct, '%pagename%') !== false )
  1197. $page = true;
  1198. }
  1199. if ( ! $post ) {
  1200. // For custom post types, we need to add on endpoints as well.
  1201. foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
  1202. if ( strpos($struct, "%$ptype%") !== false ) {
  1203. $post = true;
  1204. $page = is_post_type_hierarchical( $ptype ); // This is for page style attachment url's
  1205. break;
  1206. }
  1207. }
  1208. }
  1209. //if we're creating rules for a permalink, do all the endpoints like attachments etc
  1210. if ( $post ) {
  1211. //create query and regex for trackback
  1212. $trackbackmatch = $match . $trackbackregex;
  1213. $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
  1214. //trim slashes from the end of the regex for this dir
  1215. $match = rtrim($match, '/');
  1216. //get rid of brackets
  1217. $submatchbase = str_replace( array('(', ')'), '', $match);
  1218. //add a rule for at attachments, which take the form of <permalink>/some-text
  1219. $sub1 = $submatchbase . '/([^/]+)/';
  1220. $sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/...
  1221. $sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
  1222. $sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
  1223. $sub1comment = $sub1 . $commentregex; //and <permalink>/comment-page-xx
  1224. //add an ? as we don't have to match that last slash, and finally a $ so we
  1225. //match to the end of the URL
  1226. //add another rule to match attachments in the explicit form:
  1227. //<permalink>/attachment/some-text
  1228. $sub2 = $submatchbase . '/attachment/([^/]+)/';
  1229. $sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
  1230. $sub2feed = $sub2 . $feedregex; //feeds, <permalink>/attachment/feed/(atom|...)
  1231. $sub2feed2 = $sub2 . $feedregex2; //and feeds again on to this <permalink>/attachment/(feed|atom...)
  1232. $sub2comment = $sub2 . $commentregex; //and <permalink>/comment-page-xx
  1233. //create queries for these extra tag-ons we've just dealt with
  1234. $subquery = $index . '?attachment=' . $this->preg_index(1);
  1235. $subtbquery = $subquery . '&tb=1';
  1236. $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
  1237. $subcommentquery = $subquery . '&cpage=' . $this->preg_index(2);
  1238. //do endpoints for attachments
  1239. if ( !empty($endpoints) ) {
  1240. foreach ( (array) $ep_query_append as $regex => $ep ) {
  1241. if ( $ep[0] & EP_ATTACHMENT ) {
  1242. $rewrite[$sub1 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
  1243. $rewrite[$sub2 . $regex] = $subquery . $ep[1] . $this->preg_index(2);
  1244. }
  1245. }
  1246. }
  1247. //now we've finished with endpoints, finish off the $sub1 and $sub2 matches
  1248. $sub1 .= '?$';
  1249. $sub2 .= '?$';
  1250. //allow URLs like <permalink>/2 for <permalink>/page/2
  1251. $match = $match . '(/[0-9]+)?/?$';
  1252. $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
  1253. } else { //not matching a permalink so this is a lot simpler
  1254. //close the match and finalise the query
  1255. $match .= '?$';
  1256. $query = $index . '?' . $query;
  1257. }
  1258. //create the final array for this dir by joining the $rewrite array (which currently
  1259. //only contains rules/queries for trackback, pages etc) to the main regex/query for
  1260. //this dir
  1261. $rewrite = array_merge($rewrite, array($match => $query));
  1262. //if we're matching a permalink, add those extras (attachments etc) on
  1263. if ( $post ) {
  1264. //add trackback
  1265. $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
  1266. //add regexes/queries for attachments, attachment trackbacks and so on
  1267. if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
  1268. $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery, $sub1comment => $subcommentquery));
  1269. $rewrite = array_merge(array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery, $sub2comment => $subcommentquery), $rewrite);
  1270. }
  1271. } //if($num_toks)
  1272. //add the rules for this dir to the accumulating $post_rewrite
  1273. $post_rewrite = array_merge($rewrite, $post_rewrite);
  1274. } //foreach ($dir)
  1275. return $post_rewrite; //the finished rules. phew!
  1276. }
  1277. /**
  1278. * Generate Rewrite rules with permalink structure and walking directory only.
  1279. *
  1280. * Shorten version of {@link WP_Rewrite::generate_rewrite_rules()} that
  1281. * allows for shorter list of parameters. See the method for longer
  1282. * description of what generating rewrite rules does.
  1283. *
  1284. * @uses WP_Rewrite::generate_rewrite_rules() See for long description and rest of parameters.
  1285. * @since 1.5.0
  1286. * @access public
  1287. *
  1288. * @param string $permalink_structure The permalink structure to generate rules.
  1289. * @param bool $walk_dirs Optional, default is false. Whether to create list of directories to walk over.
  1290. * @return array
  1291. */
  1292. function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
  1293. return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
  1294. }
  1295. /**
  1296. * Construct rewrite matches and queries from permalink structure.
  1297. *
  1298. * Runs the action 'generate_rewrite_rules' with the parameter that is an
  1299. * reference to the current WP_Rewrite instance to further manipulate the
  1300. * permalink structures and rewrite rules. Runs the 'rewrite_rules_array'
  1301. * filter on the full rewrite rule array.
  1302. *
  1303. * There are two ways to manipulate the rewrite rules, one by hooking into
  1304. * the 'generate_rewrite_rules' action and gaining full control of the
  1305. * object or just manipulating the rewrite rule array before it is passed
  1306. * from the function.
  1307. *
  1308. * @since 1.5.0
  1309. * @access public
  1310. *
  1311. * @return array An associate array of matches and queries.
  1312. */
  1313. function rewrite_rules() {
  1314. $rewrite = array();
  1315. if ( empty($this->permalink_structure) )
  1316. return $rewrite;
  1317. // robots.txt -only if installed at the root
  1318. $home_path = parse_url( home_url() );
  1319. $robots_rewrite = ( empty( $home_path['path'] ) || '/' == $home_path['path'] ) ? array( 'robots\.txt$' => $this->index . '?robots=1' ) : array();
  1320. // Default Feed rules - These are require to allow for the direct access files to work with permalink structure starting with %category%
  1321. $default_feeds = array( '.*wp-atom.php$' => $this->index . '?feed=atom',
  1322. '.*wp-rdf.php$' => $this->index . '?feed=rdf',
  1323. '.*wp-rss.php$' => $this->index . '?feed=rss',
  1324. '.*wp-rss2.php$' => $this->index . '?feed=rss2',
  1325. '.*wp-feed.php$' => $this->index . '?feed=feed',
  1326. '.*wp-commentsrss2.php$' => $this->index . '?feed=rss2&withcomments=1');
  1327. // Registration rules
  1328. $registration_pages = array();
  1329. if ( is_multisite() && is_main_site() ) {
  1330. $registration_pages['.*wp-signup.php$'] = $this->index . '?signup=true';
  1331. $registration_pages['.*wp-activate.php$'] = $this->index . '?activate=true';
  1332. }
  1333. // Post
  1334. $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure, EP_PERMALINK);
  1335. $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
  1336. // Date
  1337. $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
  1338. $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
  1339. // Root
  1340. $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
  1341. $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
  1342. // Comments
  1343. $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false);
  1344. $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
  1345. // Search
  1346. $search_structure = $this->get_search_permastruct();
  1347. $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
  1348. $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
  1349. // Authors
  1350. $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
  1351. $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
  1352. // Pages
  1353. $page_rewrite = $this->page_rewrite_rules();
  1354. $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
  1355. // Extra permastructs
  1356. foreach ( $this->extra_permastructs as $permastructname => $permastruct ) {
  1357. if ( is_array($permastruct) )
  1358. $rules = $this->generate_rewrite_rules($permastruct[0], $permastruct[1]);
  1359. else
  1360. $rules = $this->generate_rewrite_rules($permastruct, EP_NONE);
  1361. $rules = apply_filters($permastructname . '_rewrite_rules', $rules);
  1362. if ( 'post_tag' == $permastructname )
  1363. $rules = apply_filters('tag_rewrite_rules', $rules);
  1364. $this->extra_rules_top = array_merge($this->extra_rules_top, $rules);
  1365. }
  1366. // Put them together.
  1367. if ( $this->use_verbose_page_rules )
  1368. $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules);
  1369. else
  1370. $this->rules = array_merge($this->extra_rules_top, $robots_rewrite, $default_feeds, $registration_pages, $root_rewrite, $comments_rewrite, $search_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $page_rewrite, $this->extra_rules);
  1371. do_action_ref_array('generate_rewrite_rules', array(&$this));
  1372. $this->rules = apply_filters('rewrite_rules_array', $this->rules);
  1373. return $this->rules;
  1374. }
  1375. /**
  1376. * Retrieve the rewrite rules.
  1377. *
  1378. * The difference between this method and {@link
  1379. * WP_Rewrite::rewrite_rules()} is that this method stores the rewrite rules
  1380. * in the 'rewrite_rules' option and retrieves it. This prevents having to
  1381. * process all of the permalinks to get the rewrite rules in the form of
  1382. * caching.
  1383. *
  1384. * @since 1.5.0
  1385. * @access public
  1386. *
  1387. * @return array Rewrite rules.
  1388. */
  1389. function wp_rewrite_rules() {
  1390. $this->rules = get_option('rewrite_rules');
  1391. if ( empty($this->rules) ) {
  1392. $this->matches = 'matches';
  1393. $this->rewrite_rules();
  1394. update_option('rewrite_rules', $this->rules);
  1395. }
  1396. return $this->rules;
  1397. }
  1398. /**
  1399. * Retrieve mod_rewrite formatted rewrite rules to write to .htaccess.
  1400. *
  1401. * Does not actually write to the .htaccess file, but creates the rules for
  1402. * the process that will.
  1403. *
  1404. * Will add the non_wp_rules property rules to the .htaccess file before
  1405. * the WordPress rewrite rules one.
  1406. *
  1407. * @since 1.5.0
  1408. * @access public
  1409. *
  1410. * @return string
  1411. */
  1412. function mod_rewrite_rules() {
  1413. if ( ! $this->using_permalinks() )
  1414. return '';
  1415. $site_root = parse_url(get_option('siteurl'));
  1416. if ( isset( $site_root['path'] ) )
  1417. $site_root = trailingslashit($site_root['path']);
  1418. $home_root = parse_url(home_url());
  1419. if ( isset( $home_root['path'] ) )
  1420. $home_root = trailingslashit($home_root['path']);
  1421. else
  1422. $home_root = '/';
  1423. $rules = "<IfModule mod_rewrite.c>\n";
  1424. $rules .= "RewriteEngine On\n";
  1425. $rules .= "RewriteBase $home_root\n";
  1426. $rules .= "RewriteRule ^index\.php$ - [L]\n"; // Prevent -f checks on index.php.
  1427. //add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
  1428. foreach ( (array) $this->non_wp_rules as $match => $query) {
  1429. // Apache 1.3 does not support the reluctant (non-greedy) modifier.
  1430. $match = str_replace('.+?', '.+', $match);
  1431. // If the match is unanchored and greedy, prepend rewrite conditions
  1432. // to avoid infinite redirects and eclipsing of real files.
  1433. //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
  1434. //nada.
  1435. //}
  1436. $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
  1437. }
  1438. if ( $this->use_verbose_rules ) {
  1439. $this->matches = '';
  1440. $rewrite = $this->rewrite_rules();
  1441. $num_rules = count($rewrite);
  1442. $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
  1443. "RewriteCond %{REQUEST_FILENAME} -d\n" .
  1444. "RewriteRule ^.*$ - [S=$num_rules]\n";
  1445. foreach ( (array) $rewrite as $match => $query) {
  1446. // Apache 1.3 does not support the reluctant (non-greedy) modifier.
  1447. $match = str_replace('.+?', '.+', $match);
  1448. // If the match is unanchored and greedy, prepend rewrite conditions
  1449. // to avoid infinite redirects and eclipsing of real files.
  1450. //if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
  1451. //nada.
  1452. //}
  1453. if ( strpos($query, $this->index) !== false )
  1454. $rules .= 'RewriteRule ^' . $match

Large files files are truncated, but you can click here to view the full file