->result(); $foundId = 0; foreach ( $posts as $post ) { if ( $post->post_name === $reversedParts[0] ) { $count = 0; $p = $post; // Loop through the given path parts from right to left, ensuring each matches the post ancestry. while ( 0 !== (int) $p->post_parent && isset( $posts[ $p->post_parent ] ) ) { $count++; $parent = $posts[ $p->post_parent ]; if ( ! isset( $reversedParts[ $count ] ) || $parent->post_name !== $reversedParts[ $count ] ) { break; } $p = $parent; } if ( 0 === (int) $p->post_parent && ( ! is_post_type_hierarchical( $p->post_type ) || count( $reversedParts ) === $count + 1 ) && $p->post_name === $reversedParts[ $count ] ) { $foundId = $post->ID; if ( $post->post_type === $postType ) { break; } } } } // We cache misses as well as hits. wp_cache_set( $cacheKey, $foundId, 'aioseo_posts_by_path' ); return $foundId ? get_post( $foundId, $output ) : false; } /** * Validates a URL. * * @since 4.1.2 * * @param string $url The url. * @return bool Is it a valid/safe url. */ public function isUrl( $url ) { return esc_url_raw( $url ) === $url; } /** * Retrieves the parameters for a given URL. * * @since 4.1.5 * * @param string $url The url. * @return array The parameters. */ public function getParametersFromUrl( $url ) { $parsedUrl = wp_parse_url( wp_unslash( $url ) ); $parameters = []; if ( empty( $parsedUrl['query'] ) ) { return []; } wp_parse_str( $parsedUrl['query'], $parameters ); return $parameters; } /** * Adds a leading slash to an url. * * @since 4.1.8 * * @param string $url The url. * @return string The url with a leading slash. */ public function leadingSlashIt( $url ) { return '/' . ltrim( $url, '/' ); } /** * Returns the path from a permalink. * This function will help get the correct path from WP installations in subfolders. * * @since 4.1.8 * * @param string $permalink A permalink from get_permalink(). * @return string The path without the home_url(). */ public function getPermalinkPath( $permalink ) { return $this->leadingSlashIt( str_replace( get_home_url(), '', $permalink ) ); } /** * Changed if permalinks are different and the before wasn't * the site url (we don't want to redirect the site URL). * * @since 4.2.3 * * @param string $before The URL before the change. * @param string $after The URL after the change. * @return boolean True if the permalink has changed. */ public function hasPermalinkChanged( $before, $after ) { // Check it's not redirecting from the root. if ( $this->getHomePath() === $before || '/' === $before ) { return false; } // Are the URLs the same? return ( $before !== $after ); } /** * Retrieve the home path. * * @since 4.2.3 * * @return string The home path. */ public function getHomePath() { $path = wp_parse_url( get_home_url(), PHP_URL_PATH ); return $path ? trailingslashit( $path ) : '/'; } /** * Checks if the given URL is an internal URL for the current site. * * @since 4.2.6 * * @param string $urlToCheck The URL to check. * @return bool Whether the given URL is an internal one. */ public function isInternalUrl( $urlToCheck ) { $parsedHomeUrl = wp_parse_url( home_url() ); $parsedUrlToCheck = wp_parse_url( $urlToCheck ); return ! empty( $parsedHomeUrl['host'] ) && ! empty( $parsedUrlToCheck['host'] ) ? $parsedHomeUrl['host'] === $parsedUrlToCheck['host'] : false; } /** * Helper for the rest url. * * @since 4.4.9 * * @return string */ public function getRestUrl() { $restUrl = get_rest_url(); if ( aioseo()->helpers->isWpmlActive() ) { global $sitepress; // Replace the rest url 'all' language prefix so our rest calls don't fail. if ( is_object( $sitepress ) && method_exists( $sitepress, 'get_current_language' ) && method_exists( $sitepress, 'get_default_language' ) && 'all' === $sitepress->get_current_language() ) { $restUrl = str_replace( get_home_url( null, '/all/' ), get_home_url( null, '/' . $sitepress->get_default_language() . '/' ), $restUrl ); } } return $restUrl; } /** * Exclude the home path from a full path. * * @since 1.2.3 Moved from aioseo-redirects. * @version 4.5.8 * * @param string $path The original path. * @return string The path without WP's home path. */ public function excludeHomePath( $path ) { return preg_replace( '@^' . $this->getHomePath() . '@', '/', $path ); } }