$rules, 'sitemaps' => $sitemaps, ); } /** * Whether a path is disallowed (longest match wins; Allow wins ties). * * @param string $url URL. * @param array $rules Rules from parse_robots(). * @return string|false Matching Disallow rule or false. */ public static function robots_disallowed( $url, array $rules ) { $path = (string) wp_parse_url( $url, PHP_URL_PATH ); $query = (string) wp_parse_url( $url, PHP_URL_QUERY ); $path = ( '' === $path ? '/' : $path ) . ( '' !== $query ? '?' . $query : '' ); $best = null; $len = -1; foreach ( $rules as $rule ) { $pattern = $rule[1]; $anchor = '$' === substr( $pattern, -1 ); $core = $anchor ? substr( $pattern, 0, -1 ) : $pattern; $regex = '#^' . str_replace( '\*', '.*', preg_quote( $core, '#' ) ) . ( $anchor ? '$' : '' ) . '#'; if ( preg_match( $regex, $path ) ) { $l = strlen( $pattern ); if ( $l > $len || ( $l === $len && $rule[0] ) ) { $len = $l; $best = $rule; } } } return ( $best && ! $best[0] ) ? 'Disallow: ' . $best[1] : false; } }