class-wp-rewrite.php 58 KB

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