class-wp-rewrite.php 62 KB

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