class-wp-rewrite.php 58 KB

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