208 |
208 |
209 // Back-compat for the old parameters: $with_front and $ep_mask. |
209 // Back-compat for the old parameters: $with_front and $ep_mask. |
210 if ( ! is_array( $args ) ) { |
210 if ( ! is_array( $args ) ) { |
211 $args = array( 'with_front' => $args ); |
211 $args = array( 'with_front' => $args ); |
212 } |
212 } |
213 if ( func_num_args() == 4 ) { |
213 |
|
214 if ( func_num_args() === 4 ) { |
214 $args['ep_mask'] = func_get_arg( 3 ); |
215 $args['ep_mask'] = func_get_arg( 3 ); |
215 } |
216 } |
216 |
217 |
217 $wp_rewrite->add_permastruct( $name, $struct, $args ); |
218 $wp_rewrite->add_permastruct( $name, $struct, $args ); |
218 } |
219 } |
242 * @since 2.1.0 |
243 * @since 2.1.0 |
243 * |
244 * |
244 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
245 * @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
245 * |
246 * |
246 * @param string $feedname Feed name. |
247 * @param string $feedname Feed name. |
247 * @param callable $function Callback to run on feed display. |
248 * @param callable $callback Callback to run on feed display. |
248 * @return string Feed action name. |
249 * @return string Feed action name. |
249 */ |
250 */ |
250 function add_feed( $feedname, $function ) { |
251 function add_feed( $feedname, $callback ) { |
251 global $wp_rewrite; |
252 global $wp_rewrite; |
252 |
253 |
253 if ( ! in_array( $feedname, $wp_rewrite->feeds, true ) ) { |
254 if ( ! in_array( $feedname, $wp_rewrite->feeds, true ) ) { |
254 $wp_rewrite->feeds[] = $feedname; |
255 $wp_rewrite->feeds[] = $feedname; |
255 } |
256 } |
257 $hook = 'do_feed_' . $feedname; |
258 $hook = 'do_feed_' . $feedname; |
258 |
259 |
259 // Remove default function hook. |
260 // Remove default function hook. |
260 remove_action( $hook, $hook ); |
261 remove_action( $hook, $hook ); |
261 |
262 |
262 add_action( $hook, $function, 10, 2 ); |
263 add_action( $hook, $callback, 10, 2 ); |
263 |
264 |
264 return $hook; |
265 return $hook; |
265 } |
266 } |
266 |
267 |
267 /** |
268 /** |
408 if ( ! $compare ) { |
409 if ( ! $compare ) { |
409 return $query_vars; |
410 return $query_vars; |
410 } |
411 } |
411 |
412 |
412 // This is the potentially clashing slug. |
413 // This is the potentially clashing slug. |
413 $value = $query_vars[ $compare ]; |
414 $value = ''; |
|
415 if ( $compare && array_key_exists( $compare, $query_vars ) ) { |
|
416 $value = $query_vars[ $compare ]; |
|
417 } |
414 |
418 |
415 $post = get_page_by_path( $value, OBJECT, 'post' ); |
419 $post = get_page_by_path( $value, OBJECT, 'post' ); |
416 if ( ! ( $post instanceof WP_Post ) ) { |
420 if ( ! ( $post instanceof WP_Post ) ) { |
417 return $query_vars; |
421 return $query_vars; |
418 } |
422 } |
495 * |
499 * |
496 * @param string $url The URL to derive the post ID from. |
500 * @param string $url The URL to derive the post ID from. |
497 */ |
501 */ |
498 $url = apply_filters( 'url_to_postid', $url ); |
502 $url = apply_filters( 'url_to_postid', $url ); |
499 |
503 |
500 $url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) ); |
504 $url_host = parse_url( $url, PHP_URL_HOST ); |
501 $home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) ); |
505 |
|
506 if ( is_string( $url_host ) ) { |
|
507 $url_host = str_replace( 'www.', '', $url_host ); |
|
508 } else { |
|
509 $url_host = ''; |
|
510 } |
|
511 |
|
512 $home_url_host = parse_url( home_url(), PHP_URL_HOST ); |
|
513 |
|
514 if ( is_string( $home_url_host ) ) { |
|
515 $home_url_host = str_replace( 'www.', '', $home_url_host ); |
|
516 } else { |
|
517 $home_url_host = ''; |
|
518 } |
502 |
519 |
503 // Bail early if the URL does not belong to this site. |
520 // Bail early if the URL does not belong to this site. |
504 if ( $url_host && $url_host !== $home_url_host ) { |
521 if ( $url_host && $url_host !== $home_url_host ) { |
505 return 0; |
522 return 0; |
506 } |
523 } |
524 // Set the correct URL scheme. |
541 // Set the correct URL scheme. |
525 $scheme = parse_url( home_url(), PHP_URL_SCHEME ); |
542 $scheme = parse_url( home_url(), PHP_URL_SCHEME ); |
526 $url = set_url_scheme( $url, $scheme ); |
543 $url = set_url_scheme( $url, $scheme ); |
527 |
544 |
528 // Add 'www.' if it is absent and should be there. |
545 // Add 'www.' if it is absent and should be there. |
529 if ( false !== strpos( home_url(), '://www.' ) && false === strpos( $url, '://www.' ) ) { |
546 if ( str_contains( home_url(), '://www.' ) && ! str_contains( $url, '://www.' ) ) { |
530 $url = str_replace( '://', '://www.', $url ); |
547 $url = str_replace( '://', '://www.', $url ); |
531 } |
548 } |
532 |
549 |
533 // Strip 'www.' if it is present and shouldn't be. |
550 // Strip 'www.' if it is present and shouldn't be. |
534 if ( false === strpos( home_url(), '://www.' ) ) { |
551 if ( ! str_contains( home_url(), '://www.' ) ) { |
535 $url = str_replace( '://www.', '://', $url ); |
552 $url = str_replace( '://www.', '://', $url ); |
536 } |
553 } |
537 |
554 |
538 if ( trim( $url, '/' ) === home_url() && 'page' === get_option( 'show_on_front' ) ) { |
555 if ( trim( $url, '/' ) === home_url() && 'page' === get_option( 'show_on_front' ) ) { |
539 $page_on_front = get_option( 'page_on_front' ); |
556 $page_on_front = get_option( 'page_on_front' ); |
554 // Strip 'index.php/' if we're not using path info permalinks. |
571 // Strip 'index.php/' if we're not using path info permalinks. |
555 if ( ! $wp_rewrite->using_index_permalinks() ) { |
572 if ( ! $wp_rewrite->using_index_permalinks() ) { |
556 $url = str_replace( $wp_rewrite->index . '/', '', $url ); |
573 $url = str_replace( $wp_rewrite->index . '/', '', $url ); |
557 } |
574 } |
558 |
575 |
559 if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) { |
576 if ( str_contains( trailingslashit( $url ), home_url( '/' ) ) ) { |
560 // Chop off http://domain.com/[path]. |
577 // Chop off http://domain.com/[path]. |
561 $url = str_replace( home_url(), '', $url ); |
578 $url = str_replace( home_url(), '', $url ); |
562 } else { |
579 } else { |
563 // Chop off /path/to/blog. |
580 // Chop off /path/to/blog. |
564 $home_path = parse_url( home_url( '/' ) ); |
581 $home_path = parse_url( home_url( '/' ) ); |
580 |
597 |
581 // Look for matches. |
598 // Look for matches. |
582 $request_match = $request; |
599 $request_match = $request; |
583 foreach ( (array) $rewrite as $match => $query ) { |
600 foreach ( (array) $rewrite as $match => $query ) { |
584 |
601 |
585 // If the requesting file is the anchor of the match, |
602 /* |
586 // prepend it to the path info. |
603 * If the requesting file is the anchor of the match, |
587 if ( ! empty( $url ) && ( $url != $request ) && ( strpos( $match, $url ) === 0 ) ) { |
604 * prepend it to the path info. |
|
605 */ |
|
606 if ( ! empty( $url ) && ( $url !== $request ) && str_starts_with( $match, $url ) ) { |
588 $request_match = $url . '/' . $request; |
607 $request_match = $url . '/' . $request; |
589 } |
608 } |
590 |
609 |
591 if ( preg_match( "#^$match#", $request_match, $matches ) ) { |
610 if ( preg_match( "#^$match#", $request_match, $matches ) ) { |
592 |
611 |
602 && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) { |
621 && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) { |
603 continue; |
622 continue; |
604 } |
623 } |
605 } |
624 } |
606 |
625 |
607 // Got a match. |
626 /* |
608 // Trim the query of everything up to the '?'. |
627 * Got a match. |
|
628 * Trim the query of everything up to the '?'. |
|
629 */ |
609 $query = preg_replace( '!^.+\?!', '', $query ); |
630 $query = preg_replace( '!^.+\?!', '', $query ); |
610 |
631 |
611 // Substitute the substring matches into the query. |
632 // Substitute the substring matches into the query. |
612 $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) ); |
633 $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) ); |
613 |
634 |