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

/htdocs/wp-includes/rewrite.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 621 lines | 230 code | 85 blank | 306 comment | 74 complexity | b2db55197f7f602dff609744db928005 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 {@see '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 WordPress rewrite component.
  134. * @global WP $wp Current WordPress environment instance.
  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. }
  145. global $wp_rewrite, $wp;
  146. if ( empty( $query ) ) {
  147. $qv = trim( $tag, '%' );
  148. $wp->add_query_var( $qv );
  149. $query = $qv . '=';
  150. }
  151. $wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
  152. }
  153. /**
  154. * Removes an existing rewrite tag (like %postname%).
  155. *
  156. * @since 4.5.0
  157. *
  158. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  159. *
  160. * @param string $tag Name of the rewrite tag.
  161. */
  162. function remove_rewrite_tag( $tag ) {
  163. global $wp_rewrite;
  164. $wp_rewrite->remove_rewrite_tag( $tag );
  165. }
  166. /**
  167. * Add permalink structure.
  168. *
  169. * @since 3.0.0
  170. *
  171. * @see WP_Rewrite::add_permastruct()
  172. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  173. *
  174. * @param string $name Name for permalink structure.
  175. * @param string $struct Permalink structure.
  176. * @param array $args Optional. Arguments for building the rules from the permalink structure,
  177. * see WP_Rewrite::add_permastruct() for full details. Default empty array.
  178. */
  179. function add_permastruct( $name, $struct, $args = array() ) {
  180. global $wp_rewrite;
  181. // Back-compat for the old parameters: $with_front and $ep_mask.
  182. if ( ! is_array( $args ) ) {
  183. $args = array( 'with_front' => $args );
  184. }
  185. if ( func_num_args() == 4 ) {
  186. $args['ep_mask'] = func_get_arg( 3 );
  187. }
  188. $wp_rewrite->add_permastruct( $name, $struct, $args );
  189. }
  190. /**
  191. * Removes a permalink structure.
  192. *
  193. * Can only be used to remove permastructs that were added using add_permastruct().
  194. * Built-in permastructs cannot be removed.
  195. *
  196. * @since 4.5.0
  197. *
  198. * @see WP_Rewrite::remove_permastruct()
  199. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  200. *
  201. * @param string $name Name for permalink structure.
  202. */
  203. function remove_permastruct( $name ) {
  204. global $wp_rewrite;
  205. $wp_rewrite->remove_permastruct( $name );
  206. }
  207. /**
  208. * Add a new feed type like /atom1/.
  209. *
  210. * @since 2.1.0
  211. *
  212. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  213. *
  214. * @param string $feedname Feed name.
  215. * @param callable $function Callback to run on feed display.
  216. * @return string Feed action name.
  217. */
  218. function add_feed( $feedname, $function ) {
  219. global $wp_rewrite;
  220. if ( ! in_array( $feedname, $wp_rewrite->feeds ) ) {
  221. $wp_rewrite->feeds[] = $feedname;
  222. }
  223. $hook = 'do_feed_' . $feedname;
  224. // Remove default function hook.
  225. remove_action( $hook, $hook );
  226. add_action( $hook, $function, 10, 2 );
  227. return $hook;
  228. }
  229. /**
  230. * Remove rewrite rules and then recreate rewrite rules.
  231. *
  232. * @since 3.0.0
  233. *
  234. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  235. *
  236. * @param bool $hard Whether to update .htaccess (hard flush) or just update
  237. * rewrite_rules transient (soft flush). Default is true (hard).
  238. */
  239. function flush_rewrite_rules( $hard = true ) {
  240. global $wp_rewrite;
  241. if ( is_callable( array( $wp_rewrite, 'flush_rules' ) ) ) {
  242. $wp_rewrite->flush_rules( $hard );
  243. }
  244. }
  245. /**
  246. * Add an endpoint, like /trackback/.
  247. *
  248. * Adding an endpoint creates extra rewrite rules for each of the matching
  249. * places specified by the provided bitmask. For example:
  250. *
  251. * add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
  252. *
  253. * will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct
  254. * that describes a permalink (post) or page. This is rewritten to "json=$match"
  255. * where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in
  256. * "[permalink]/json/foo/").
  257. *
  258. * A new query var with the same name as the endpoint will also be created.
  259. *
  260. * When specifying $places ensure that you are using the EP_* constants (or a
  261. * combination of them using the bitwise OR operator) as their values are not
  262. * guaranteed to remain static (especially `EP_ALL`).
  263. *
  264. * Be sure to flush the rewrite rules - see flush_rewrite_rules() - when your plugin gets
  265. * activated and deactivated.
  266. *
  267. * @since 2.1.0
  268. * @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`.
  269. *
  270. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  271. *
  272. * @param string $name Name of the endpoint.
  273. * @param int $places Endpoint mask describing the places the endpoint should be added.
  274. * @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var
  275. * for this endpoint. Defaults to the value of `$name`.
  276. */
  277. function add_rewrite_endpoint( $name, $places, $query_var = true ) {
  278. global $wp_rewrite;
  279. $wp_rewrite->add_endpoint( $name, $places, $query_var );
  280. }
  281. /**
  282. * Filters the URL base for taxonomies.
  283. *
  284. * To remove any manually prepended /index.php/.
  285. *
  286. * @access private
  287. * @since 2.6.0
  288. *
  289. * @param string $base The taxonomy base that we're going to filter
  290. * @return string
  291. */
  292. function _wp_filter_taxonomy_base( $base ) {
  293. if ( ! empty( $base ) ) {
  294. $base = preg_replace( '|^/index\.php/|', '', $base );
  295. $base = trim( $base, '/' );
  296. }
  297. return $base;
  298. }
  299. /**
  300. * Resolve numeric slugs that collide with date permalinks.
  301. *
  302. * Permalinks of posts with numeric slugs can sometimes look to WP_Query::parse_query()
  303. * like a date archive, as when your permalink structure is `/%year%/%postname%/` and
  304. * a post with post_name '05' has the URL `/2015/05/`.
  305. *
  306. * This function detects conflicts of this type and resolves them in favor of the
  307. * post permalink.
  308. *
  309. * Note that, since 4.3.0, wp_unique_post_slug() prevents the creation of post slugs
  310. * that would result in a date archive conflict. The resolution performed in this
  311. * function is primarily for legacy content, as well as cases when the admin has changed
  312. * the site's permalink structure in a way that introduces URL conflicts.
  313. *
  314. * @since 4.3.0
  315. *
  316. * @param array $query_vars Optional. Query variables for setting up the loop, as determined in
  317. * WP::parse_request(). Default empty array.
  318. * @return array Returns the original array of query vars, with date/post conflicts resolved.
  319. */
  320. function wp_resolve_numeric_slug_conflicts( $query_vars = array() ) {
  321. if ( ! isset( $query_vars['year'] ) && ! isset( $query_vars['monthnum'] ) && ! isset( $query_vars['day'] ) ) {
  322. return $query_vars;
  323. }
  324. // Identify the 'postname' position in the permastruct array.
  325. $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
  326. $postname_index = array_search( '%postname%', $permastructs );
  327. if ( false === $postname_index ) {
  328. return $query_vars;
  329. }
  330. /*
  331. * A numeric slug could be confused with a year, month, or day, depending on position. To account for
  332. * the possibility of post pagination (eg 2015/2 for the second page of a post called '2015'), our
  333. * `is_*` checks are generous: check for year-slug clashes when `is_year` *or* `is_month`, and check
  334. * for month-slug clashes when `is_month` *or* `is_day`.
  335. */
  336. $compare = '';
  337. if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) {
  338. $compare = 'year';
  339. } elseif ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
  340. $compare = 'monthnum';
  341. } elseif ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
  342. $compare = 'day';
  343. }
  344. if ( ! $compare ) {
  345. return $query_vars;
  346. }
  347. // This is the potentially clashing slug.
  348. $value = $query_vars[ $compare ];
  349. $post = get_page_by_path( $value, OBJECT, 'post' );
  350. if ( ! ( $post instanceof WP_Post ) ) {
  351. return $query_vars;
  352. }
  353. // If the date of the post doesn't match the date specified in the URL, resolve to the date archive.
  354. if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) {
  355. // $matches[1] is the year the post was published.
  356. if ( intval( $query_vars['year'] ) !== intval( $matches[1] ) ) {
  357. return $query_vars;
  358. }
  359. // $matches[2] is the month the post was published.
  360. if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && intval( $query_vars['monthnum'] ) !== intval( $matches[2] ) ) {
  361. return $query_vars;
  362. }
  363. }
  364. /*
  365. * If the located post contains nextpage pagination, then the URL chunk following postname may be
  366. * intended as the page number. Verify that it's a valid page before resolving to it.
  367. */
  368. $maybe_page = '';
  369. if ( 'year' === $compare && isset( $query_vars['monthnum'] ) ) {
  370. $maybe_page = $query_vars['monthnum'];
  371. } elseif ( 'monthnum' === $compare && isset( $query_vars['day'] ) ) {
  372. $maybe_page = $query_vars['day'];
  373. }
  374. // Bug found in #11694 - 'page' was returning '/4'.
  375. $maybe_page = (int) trim( $maybe_page, '/' );
  376. $post_page_count = substr_count( $post->post_content, '<!--nextpage-->' ) + 1;
  377. // If the post doesn't have multiple pages, but a 'page' candidate is found, resolve to the date archive.
  378. if ( 1 === $post_page_count && $maybe_page ) {
  379. return $query_vars;
  380. }
  381. // If the post has multiple pages and the 'page' number isn't valid, resolve to the date archive.
  382. if ( $post_page_count > 1 && $maybe_page > $post_page_count ) {
  383. return $query_vars;
  384. }
  385. // If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage.
  386. if ( '' !== $maybe_page ) {
  387. $query_vars['page'] = intval( $maybe_page );
  388. }
  389. // Next, unset autodetected date-related query vars.
  390. unset( $query_vars['year'] );
  391. unset( $query_vars['monthnum'] );
  392. unset( $query_vars['day'] );
  393. // Then, set the identified post.
  394. $query_vars['name'] = $post->post_name;
  395. // Finally, return the modified query vars.
  396. return $query_vars;
  397. }
  398. /**
  399. * Examine a URL and try to determine the post ID it represents.
  400. *
  401. * Checks are supposedly from the hosted site blog.
  402. *
  403. * @since 1.0.0
  404. *
  405. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  406. * @global WP $wp Current WordPress environment instance.
  407. *
  408. * @param string $url Permalink to check.
  409. * @return int Post ID, or 0 on failure.
  410. */
  411. function url_to_postid( $url ) {
  412. global $wp_rewrite;
  413. /**
  414. * Filters the URL to derive the post ID from.
  415. *
  416. * @since 2.2.0
  417. *
  418. * @param string $url The URL to derive the post ID from.
  419. */
  420. $url = apply_filters( 'url_to_postid', $url );
  421. $url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
  422. $home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
  423. // Bail early if the URL does not belong to this site.
  424. if ( $url_host && $url_host !== $home_url_host ) {
  425. return 0;
  426. }
  427. // First, check to see if there is a 'p=N' or 'page_id=N' to match against.
  428. if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) {
  429. $id = absint( $values[2] );
  430. if ( $id ) {
  431. return $id;
  432. }
  433. }
  434. // Get rid of the #anchor.
  435. $url_split = explode( '#', $url );
  436. $url = $url_split[0];
  437. // Get rid of URL ?query=string.
  438. $url_split = explode( '?', $url );
  439. $url = $url_split[0];
  440. // Set the correct URL scheme.
  441. $scheme = parse_url( home_url(), PHP_URL_SCHEME );
  442. $url = set_url_scheme( $url, $scheme );
  443. // Add 'www.' if it is absent and should be there.
  444. if ( false !== strpos( home_url(), '://www.' ) && false === strpos( $url, '://www.' ) ) {
  445. $url = str_replace( '://', '://www.', $url );
  446. }
  447. // Strip 'www.' if it is present and shouldn't be.
  448. if ( false === strpos( home_url(), '://www.' ) ) {
  449. $url = str_replace( '://www.', '://', $url );
  450. }
  451. if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
  452. $page_on_front = get_option( 'page_on_front' );
  453. if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
  454. return (int) $page_on_front;
  455. }
  456. }
  457. // Check to see if we are using rewrite rules.
  458. $rewrite = $wp_rewrite->wp_rewrite_rules();
  459. // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options.
  460. if ( empty( $rewrite ) ) {
  461. return 0;
  462. }
  463. // Strip 'index.php/' if we're not using path info permalinks.
  464. if ( ! $wp_rewrite->using_index_permalinks() ) {
  465. $url = str_replace( $wp_rewrite->index . '/', '', $url );
  466. }
  467. if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
  468. // Chop off http://domain.com/[path].
  469. $url = str_replace( home_url(), '', $url );
  470. } else {
  471. // Chop off /path/to/blog.
  472. $home_path = parse_url( home_url( '/' ) );
  473. $home_path = isset( $home_path['path'] ) ? $home_path['path'] : '';
  474. $url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
  475. }
  476. // Trim leading and lagging slashes.
  477. $url = trim( $url, '/' );
  478. $request = $url;
  479. $post_type_query_vars = array();
  480. foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) {
  481. if ( ! empty( $t->query_var ) ) {
  482. $post_type_query_vars[ $t->query_var ] = $post_type;
  483. }
  484. }
  485. // Look for matches.
  486. $request_match = $request;
  487. foreach ( (array) $rewrite as $match => $query ) {
  488. // If the requesting file is the anchor of the match,
  489. // prepend it to the path info.
  490. if ( ! empty( $url ) && ( $url != $request ) && ( strpos( $match, $url ) === 0 ) ) {
  491. $request_match = $url . '/' . $request;
  492. }
  493. if ( preg_match( "#^$match#", $request_match, $matches ) ) {
  494. if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
  495. // This is a verbose page match, let's check to be sure about it.
  496. $page = get_page_by_path( $matches[ $varmatch[1] ] );
  497. if ( ! $page ) {
  498. continue;
  499. }
  500. $post_status_obj = get_post_status_object( $page->post_status );
  501. if ( ! $post_status_obj->public && ! $post_status_obj->protected
  502. && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
  503. continue;
  504. }
  505. }
  506. // Got a match.
  507. // Trim the query of everything up to the '?'.
  508. $query = preg_replace( '!^.+\?!', '', $query );
  509. // Substitute the substring matches into the query.
  510. $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
  511. // Filter out non-public query vars.
  512. global $wp;
  513. parse_str( $query, $query_vars );
  514. $query = array();
  515. foreach ( (array) $query_vars as $key => $value ) {
  516. if ( in_array( $key, $wp->public_query_vars ) ) {
  517. $query[ $key ] = $value;
  518. if ( isset( $post_type_query_vars[ $key ] ) ) {
  519. $query['post_type'] = $post_type_query_vars[ $key ];
  520. $query['name'] = $value;
  521. }
  522. }
  523. }
  524. // Resolve conflicts between posts with numeric slugs and date archive queries.
  525. $query = wp_resolve_numeric_slug_conflicts( $query );
  526. // Do the query.
  527. $query = new WP_Query( $query );
  528. if ( ! empty( $query->posts ) && $query->is_singular ) {
  529. return $query->post->ID;
  530. } else {
  531. return 0;
  532. }
  533. }
  534. }
  535. return 0;
  536. }