post_type ) { if ( 'inherit' !== $post->post_status ) { return null; } $permalink = wp_get_attachment_url( $post->ID ); } else { if ( 'publish' !== $post->post_status ) { return null; } $permalink = get_permalink( $post ); } if ( ! is_string( $permalink ) || '' === $permalink ) { return null; } // The page moved back to the address being requested. Redirecting would loop. if ( PathRedirectStore::normalise( self::path_of( $permalink ) ) === PathRedirectStore::normalise( $path ) ) { return null; } return $permalink; } /** * The site-relative path of a URL. * * @param string $url Absolute or relative URL. * @return string */ public static function path_of( $url ) { $path = (string) wp_parse_url( $url, PHP_URL_PATH ); $base = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); // Subdirectory installs: the request carries the subdirectory, stored paths never do. if ( '' !== $base && '/' !== $base && 0 === strpos( $path, $base ) ) { $path = substr( $path, strlen( $base ) ); } return PathRedirectStore::normalise( $path ); } /** * The path the visitor actually asked for. * * @return string */ public static function requested_path() { if ( empty( $_SERVER['REQUEST_URI'] ) ) { return ''; } return self::path_of( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); } }