PageRenderTime 71ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/msw/dev/wp-includes/rewrite.php

https://github.com/chrissiebrodigan/USC
PHP | 2051 lines | 822 code | 242 blank | 987 comment | 143 complexity | ad458cd8e2457d28ed19ed8abc47a25d MD5 | raw file

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

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