PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/rewrite.php

https://gitlab.com/webkod3r/tripolis
PHP | 599 lines | 212 code | 82 blank | 305 comment | 70 complexity | 82858bce770a11d5bad3e0b31049a23a MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Rewrite API
  4. *
  5. * @package WordPress
  6. * @subpackage Rewrite
  7. */
  8. /**
  9. * Endpoint Mask for default, which is nothing.
  10. *
  11. * @since 2.1.0
  12. */
  13. define('EP_NONE', 0);
  14. /**
  15. * Endpoint Mask for Permalink.
  16. *
  17. * @since 2.1.0
  18. */
  19. define('EP_PERMALINK', 1);
  20. /**
  21. * Endpoint Mask for Attachment.
  22. *
  23. * @since 2.1.0
  24. */
  25. define('EP_ATTACHMENT', 2);
  26. /**
  27. * Endpoint Mask for date.
  28. *
  29. * @since 2.1.0
  30. */
  31. define('EP_DATE', 4);
  32. /**
  33. * Endpoint Mask for year
  34. *
  35. * @since 2.1.0
  36. */
  37. define('EP_YEAR', 8);
  38. /**
  39. * Endpoint Mask for month.
  40. *
  41. * @since 2.1.0
  42. */
  43. define('EP_MONTH', 16);
  44. /**
  45. * Endpoint Mask for day.
  46. *
  47. * @since 2.1.0
  48. */
  49. define('EP_DAY', 32);
  50. /**
  51. * Endpoint Mask for root.
  52. *
  53. * @since 2.1.0
  54. */
  55. define('EP_ROOT', 64);
  56. /**
  57. * Endpoint Mask for comments.
  58. *
  59. * @since 2.1.0
  60. */
  61. define('EP_COMMENTS', 128);
  62. /**
  63. * Endpoint Mask for searches.
  64. *
  65. * @since 2.1.0
  66. */
  67. define('EP_SEARCH', 256);
  68. /**
  69. * Endpoint Mask for categories.
  70. *
  71. * @since 2.1.0
  72. */
  73. define('EP_CATEGORIES', 512);
  74. /**
  75. * Endpoint Mask for tags.
  76. *
  77. * @since 2.3.0
  78. */
  79. define('EP_TAGS', 1024);
  80. /**
  81. * Endpoint Mask for authors.
  82. *
  83. * @since 2.1.0
  84. */
  85. define('EP_AUTHORS', 2048);
  86. /**
  87. * Endpoint Mask for pages.
  88. *
  89. * @since 2.1.0
  90. */
  91. define('EP_PAGES', 4096);
  92. /**
  93. * Endpoint Mask for all archive views.
  94. *
  95. * @since 3.7.0
  96. */
  97. define( 'EP_ALL_ARCHIVES', EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS );
  98. /**
  99. * Endpoint Mask for everything.
  100. *
  101. * @since 2.1.0
  102. */
  103. define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES );
  104. /**
  105. * Adds a rewrite rule that transforms a URL structure to a set of query vars.
  106. *
  107. * Any value in the $after parameter that isn't 'bottom' will result in the rule
  108. * being placed at the top of the rewrite rules.
  109. *
  110. * @since 2.1.0
  111. * @since 4.4.0 Array support was added to the `$query` parameter.
  112. *
  113. * @global WP_Rewrite $wp_rewrite WordPress Rewrite Component.
  114. *
  115. * @param string $regex Regular expression to match request against.
  116. * @param string|array $query The corresponding query vars for this rewrite rule.
  117. * @param string $after Optional. Priority of the new rule. Accepts 'top'
  118. * or 'bottom'. Default 'bottom'.
  119. */
  120. function add_rewrite_rule( $regex, $query, $after = 'bottom' ) {
  121. global $wp_rewrite;
  122. $wp_rewrite->add_rule( $regex, $query, $after );
  123. }
  124. /**
  125. * Add a new rewrite tag (like %postname%).
  126. *
  127. * The $query parameter is optional. If it is omitted you must ensure that
  128. * you call this on, or before, the 'init' hook. This is because $query defaults
  129. * to "$tag=", and for this to work a new query var has to be added.
  130. *
  131. * @since 2.1.0
  132. *
  133. * @global WP_Rewrite $wp_rewrite
  134. * @global WP $wp
  135. *
  136. * @param string $tag Name of the new rewrite tag.
  137. * @param string $regex Regular expression to substitute the tag for in rewrite rules.
  138. * @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty.
  139. */
  140. function add_rewrite_tag( $tag, $regex, $query = '' ) {
  141. // validate the tag's name
  142. if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' )
  143. return;
  144. global $wp_rewrite, $wp;
  145. if ( empty( $query ) ) {
  146. $qv = trim( $tag, '%' );
  147. $wp->add_query_var( $qv );
  148. $query = $qv . '=';
  149. }
  150. $wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
  151. }
  152. /**
  153. * Removes an existing rewrite tag (like %postname%).
  154. *
  155. * @since 4.5.0
  156. *
  157. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  158. *
  159. * @param string $tag Name of the rewrite tag.
  160. */
  161. function remove_rewrite_tag( $tag ) {
  162. global $wp_rewrite;
  163. $wp_rewrite->remove_rewrite_tag( $tag );
  164. }
  165. /**
  166. * Add permalink structure.
  167. *
  168. * @since 3.0.0
  169. *
  170. * @see WP_Rewrite::add_permastruct()
  171. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  172. *
  173. * @param string $name Name for permalink structure.
  174. * @param string $struct Permalink structure.
  175. * @param array $args Optional. Arguments for building the rules from the permalink structure,
  176. * see WP_Rewrite::add_permastruct() for full details. Default empty array.
  177. */
  178. function add_permastruct( $name, $struct, $args = array() ) {
  179. global $wp_rewrite;
  180. // backwards compatibility for the old parameters: $with_front and $ep_mask
  181. if ( ! is_array( $args ) )
  182. $args = array( 'with_front' => $args );
  183. if ( func_num_args() == 4 )
  184. $args['ep_mask'] = func_get_arg( 3 );
  185. $wp_rewrite->add_permastruct( $name, $struct, $args );
  186. }
  187. /**
  188. * Removes a permalink structure.
  189. *
  190. * Can only be used to remove permastructs that were added using add_permastruct().
  191. * Built-in permastructs cannot be removed.
  192. *
  193. * @since 4.5.0
  194. *
  195. * @see WP_Rewrite::remove_permastruct()
  196. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  197. *
  198. * @param string $name Name for permalink structure.
  199. */
  200. function remove_permastruct( $name ) {
  201. global $wp_rewrite;
  202. $wp_rewrite->remove_permastruct( $name );
  203. }
  204. /**
  205. * Add a new feed type like /atom1/.
  206. *
  207. * @since 2.1.0
  208. *
  209. * @global WP_Rewrite $wp_rewrite
  210. *
  211. * @param string $feedname Feed name.
  212. * @param callable $function Callback to run on feed display.
  213. * @return string Feed action name.
  214. */
  215. function add_feed( $feedname, $function ) {
  216. global $wp_rewrite;
  217. if ( ! in_array( $feedname, $wp_rewrite->feeds ) ) {
  218. $wp_rewrite->feeds[] = $feedname;
  219. }
  220. $hook = 'do_feed_' . $feedname;
  221. // Remove default function hook
  222. remove_action( $hook, $hook );
  223. add_action( $hook, $function, 10, 2 );
  224. return $hook;
  225. }
  226. /**
  227. * Remove rewrite rules and then recreate rewrite rules.
  228. *
  229. * @since 3.0.0
  230. *
  231. * @global WP_Rewrite $wp_rewrite
  232. *
  233. * @param bool $hard Whether to update .htaccess (hard flush) or just update
  234. * rewrite_rules transient (soft flush). Default is true (hard).
  235. */
  236. function flush_rewrite_rules( $hard = true ) {
  237. global $wp_rewrite;
  238. $wp_rewrite->flush_rules( $hard );
  239. }
  240. /**
  241. * Add an endpoint, like /trackback/.
  242. *
  243. * Adding an endpoint creates extra rewrite rules for each of the matching
  244. * places specified by the provided bitmask. For example:
  245. *
  246. * add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
  247. *
  248. * will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct
  249. * that describes a permalink (post) or page. This is rewritten to "json=$match"
  250. * where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in
  251. * "[permalink]/json/foo/").
  252. *
  253. * A new query var with the same name as the endpoint will also be created.
  254. *
  255. * When specifying $places ensure that you are using the EP_* constants (or a
  256. * combination of them using the bitwise OR operator) as their values are not
  257. * guaranteed to remain static (especially `EP_ALL`).
  258. *
  259. * Be sure to flush the rewrite rules - see flush_rewrite_rules() - when your plugin gets
  260. * activated and deactivated.
  261. *
  262. * @since 2.1.0
  263. * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
  264. *
  265. * @global WP_Rewrite $wp_rewrite
  266. *
  267. * @param string $name Name of the endpoint.
  268. * @param int $places Endpoint mask describing the places the endpoint should be added.
  269. * @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
  270. * for this endpoint. Defaults to the value of `$name`.
  271. */
  272. function add_rewrite_endpoint( $name, $places, $query_var = true ) {
  273. global $wp_rewrite;
  274. $wp_rewrite->add_endpoint( $name, $places, $query_var );
  275. }
  276. /**
  277. * Filter the URL base for taxonomies.
  278. *
  279. * To remove any manually prepended /index.php/.
  280. *
  281. * @access private
  282. * @since 2.6.0
  283. *
  284. * @param string $base The taxonomy base that we're going to filter
  285. * @return string
  286. */
  287. function _wp_filter_taxonomy_base( $base ) {
  288. if ( !empty( $base ) ) {
  289. $base = preg_replace( '|^/index\.php/|', '', $base );
  290. $base = trim( $base, '/' );
  291. }
  292. return $base;
  293. }
  294. /**
  295. * Resolve numeric slugs that collide with date permalinks.
  296. *
  297. * Permalinks of posts with numeric slugs can sometimes look to WP_Query::parse_query()
  298. * like a date archive, as when your permalink structure is `/%year%/%postname%/` and
  299. * a post with post_name '05' has the URL `/2015/05/`.
  300. *
  301. * This function detects conflicts of this type and resolves them in favor of the
  302. * post permalink.
  303. *
  304. * Note that, since 4.3.0, wp_unique_post_slug() prevents the creation of post slugs
  305. * that would result in a date archive conflict. The resolution performed in this
  306. * function is primarily for legacy content, as well as cases when the admin has changed
  307. * the site's permalink structure in a way that introduces URL conflicts.
  308. *
  309. * @since 4.3.0
  310. *
  311. * @param array $query_vars Optional. Query variables for setting up the loop, as determined in
  312. * WP::parse_request(). Default empty array.
  313. * @return array Returns the original array of query vars, with date/post conflicts resolved.
  314. */
  315. function wp_resolve_numeric_slug_conflicts( $query_vars = array() ) {
  316. if ( ! isset( $query_vars['year'] ) && ! isset( $query_vars['monthnum'] ) && ! isset( $query_vars['day'] ) ) {
  317. return $query_vars;
  318. }
  319. // Identify the 'postname' position in the permastruct array.
  320. $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
  321. $postname_index = array_search( '%postname%', $permastructs );
  322. if ( false === $postname_index ) {
  323. return $query_vars;
  324. }
  325. /*
  326. * A numeric slug could be confused with a year, month, or day, depending on position. To account for
  327. * the possibility of post pagination (eg 2015/2 for the second page of a post called '2015'), our
  328. * `is_*` checks are generous: check for year-slug clashes when `is_year` *or* `is_month`, and check
  329. * for month-slug clashes when `is_month` *or* `is_day`.
  330. */
  331. $compare = '';
  332. if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) {
  333. $compare = 'year';
  334. } elseif ( '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
  335. $compare = 'monthnum';
  336. } elseif ( '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
  337. $compare = 'day';
  338. }
  339. if ( ! $compare ) {
  340. return $query_vars;
  341. }
  342. // This is the potentially clashing slug.
  343. $value = $query_vars[ $compare ];
  344. $post = get_page_by_path( $value, OBJECT, 'post' );
  345. if ( ! ( $post instanceof WP_Post ) ) {
  346. return $query_vars;
  347. }
  348. // If the date of the post doesn't match the date specified in the URL, resolve to the date archive.
  349. if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) {
  350. // $matches[1] is the year the post was published.
  351. if ( intval( $query_vars['year'] ) !== intval( $matches[1] ) ) {
  352. return $query_vars;
  353. }
  354. // $matches[2] is the month the post was published.
  355. if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && intval( $query_vars['monthnum'] ) !== intval( $matches[2] ) ) {
  356. return $query_vars;
  357. }
  358. }
  359. /*
  360. * If the located post contains nextpage pagination, then the URL chunk following postname may be
  361. * intended as the page number. Verify that it's a valid page before resolving to it.
  362. */
  363. $maybe_page = '';
  364. if ( 'year' === $compare && isset( $query_vars['monthnum'] ) ) {
  365. $maybe_page = $query_vars['monthnum'];
  366. } elseif ( 'monthnum' === $compare && isset( $query_vars['day'] ) ) {
  367. $maybe_page = $query_vars['day'];
  368. }
  369. // Bug found in #11694 - 'page' was returning '/4'
  370. $maybe_page = (int) trim( $maybe_page, '/' );
  371. $post_page_count = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
  372. // If the post doesn't have multiple pages, but a 'page' candidate is found, resolve to the date archive.
  373. if ( 1 === $post_page_count && $maybe_page ) {
  374. return $query_vars;
  375. }
  376. // If the post has multiple pages and the 'page' number isn't valid, resolve to the date archive.
  377. if ( $post_page_count > 1 && $maybe_page > $post_page_count ) {
  378. return $query_vars;
  379. }
  380. // If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage.
  381. if ( '' !== $maybe_page ) {
  382. $query_vars['page'] = intval( $maybe_page );
  383. }
  384. // Next, unset autodetected date-related query vars.
  385. unset( $query_vars['year'] );
  386. unset( $query_vars['monthnum'] );
  387. unset( $query_vars['day'] );
  388. // Then, set the identified post.
  389. $query_vars['name'] = $post->post_name;
  390. // Finally, return the modified query vars.
  391. return $query_vars;
  392. }
  393. /**
  394. * Examine a URL and try to determine the post ID it represents.
  395. *
  396. * Checks are supposedly from the hosted site blog.
  397. *
  398. * @since 1.0.0
  399. *
  400. * @global WP_Rewrite $wp_rewrite
  401. * @global WP $wp
  402. *
  403. * @param string $url Permalink to check.
  404. * @return int Post ID, or 0 on failure.
  405. */
  406. function url_to_postid( $url ) {
  407. global $wp_rewrite;
  408. /**
  409. * Filter the URL to derive the post ID from.
  410. *
  411. * @since 2.2.0
  412. *
  413. * @param string $url The URL to derive the post ID from.
  414. */
  415. $url = apply_filters( 'url_to_postid', $url );
  416. // First, check to see if there is a 'p=N' or 'page_id=N' to match against
  417. if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
  418. $id = absint($values[2]);
  419. if ( $id )
  420. return $id;
  421. }
  422. // Get rid of the #anchor
  423. $url_split = explode('#', $url);
  424. $url = $url_split[0];
  425. // Get rid of URL ?query=string
  426. $url_split = explode('?', $url);
  427. $url = $url_split[0];
  428. // Set the correct URL scheme.
  429. $scheme = parse_url( home_url(), PHP_URL_SCHEME );
  430. $url = set_url_scheme( $url, $scheme );
  431. // Add 'www.' if it is absent and should be there
  432. if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
  433. $url = str_replace('://', '://www.', $url);
  434. // Strip 'www.' if it is present and shouldn't be
  435. if ( false === strpos(home_url(), '://www.') )
  436. $url = str_replace('://www.', '://', $url);
  437. if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
  438. $page_on_front = get_option( 'page_on_front' );
  439. if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
  440. return (int) $page_on_front;
  441. }
  442. }
  443. // Check to see if we are using rewrite rules
  444. $rewrite = $wp_rewrite->wp_rewrite_rules();
  445. // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
  446. if ( empty($rewrite) )
  447. return 0;
  448. // Strip 'index.php/' if we're not using path info permalinks
  449. if ( !$wp_rewrite->using_index_permalinks() )
  450. $url = str_replace( $wp_rewrite->index . '/', '', $url );
  451. if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
  452. // Chop off http://domain.com/[path]
  453. $url = str_replace(home_url(), '', $url);
  454. } else {
  455. // Chop off /path/to/blog
  456. $home_path = parse_url( home_url( '/' ) );
  457. $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
  458. $url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
  459. }
  460. // Trim leading and lagging slashes
  461. $url = trim($url, '/');
  462. $request = $url;
  463. $post_type_query_vars = array();
  464. foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
  465. if ( ! empty( $t->query_var ) )
  466. $post_type_query_vars[ $t->query_var ] = $post_type;
  467. }
  468. // Look for matches.
  469. $request_match = $request;
  470. foreach ( (array)$rewrite as $match => $query) {
  471. // If the requesting file is the anchor of the match, prepend it
  472. // to the path info.
  473. if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
  474. $request_match = $url . '/' . $request;
  475. if ( preg_match("#^$match#", $request_match, $matches) ) {
  476. if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
  477. // This is a verbose page match, let's check to be sure about it.
  478. $page = get_page_by_path( $matches[ $varmatch[1] ] );
  479. if ( ! $page ) {
  480. continue;
  481. }
  482. $post_status_obj = get_post_status_object( $page->post_status );
  483. if ( ! $post_status_obj->public && ! $post_status_obj->protected
  484. && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
  485. continue;
  486. }
  487. }
  488. // Got a match.
  489. // Trim the query of everything up to the '?'.
  490. $query = preg_replace("!^.+\?!", '', $query);
  491. // Substitute the substring matches into the query.
  492. $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
  493. // Filter out non-public query vars
  494. global $wp;
  495. parse_str( $query, $query_vars );
  496. $query = array();
  497. foreach ( (array) $query_vars as $key => $value ) {
  498. if ( in_array( $key, $wp->public_query_vars ) ){
  499. $query[$key] = $value;
  500. if ( isset( $post_type_query_vars[$key] ) ) {
  501. $query['post_type'] = $post_type_query_vars[$key];
  502. $query['name'] = $value;
  503. }
  504. }
  505. }
  506. // Resolve conflicts between posts with numeric slugs and date archive queries.
  507. $query = wp_resolve_numeric_slug_conflicts( $query );
  508. // Do the query
  509. $query = new WP_Query( $query );
  510. if ( ! empty( $query->posts ) && $query->is_singular )
  511. return $query->post->ID;
  512. else
  513. return 0;
  514. }
  515. }
  516. return 0;
  517. }