collaborators = $collaborators; } /** * Detector wired to the real WordPress functions. */ public static function from_wordpress(): self { return new self( array( 'blog_public' => static function () { return get_option( 'blog_public' ); }, 'seo_plugin_active' => static function (): bool { return defined( 'WPSEO_VERSION' ) || class_exists( 'WPSEO_Sitemaps' ) || defined( 'RANK_MATH_VERSION' ) || class_exists( 'RankMath' ); }, 'aioseo_active' => static function (): bool { return defined( 'AIOSEO_VERSION' ) || function_exists( 'aioseo' ); }, 'home_url' => static function ( string $path = '' ): string { return home_url( $path ); }, 'sitemaps_enabled' => static function (): bool { if ( ! function_exists( 'wp_sitemaps_get_server' ) ) { return false; } $server = wp_sitemaps_get_server(); return is_object( $server ) && method_exists( $server, 'sitemaps_enabled' ) && $server->sitemaps_enabled(); }, 'core_sitemap_url' => static function () { return function_exists( 'get_sitemap_url' ) ? get_sitemap_url( 'index' ) : ''; }, 'robots_body' => static function () { return self::fetch_robots_body( home_url( '/robots.txt' ) ); }, ) ); } /** * Detect the sitemap. * * @return array{url: string|null, reason: string} Reason is one of * discouraged, seo_plugin, core, robots, none. */ public function detect(): array { if ( '0' === (string) $this->call( 'blog_public' ) ) { return $this->result( null, 'discouraged' ); } if ( $this->call( 'seo_plugin_active' ) ) { return $this->result( (string) $this->call( 'home_url', '/sitemap_index.xml' ), 'seo_plugin' ); } if ( $this->call( 'aioseo_active' ) ) { return $this->result( (string) $this->call( 'home_url', '/sitemap.xml' ), 'seo_plugin' ); } if ( $this->call( 'sitemaps_enabled' ) ) { $core = $this->call( 'core_sitemap_url' ); if ( is_string( $core ) && '' !== $core ) { return $this->result( $core, 'core' ); } } $robots = $this->sitemap_from_robots(); if ( null !== $robots ) { return $this->result( $robots, 'robots' ); } return $this->result( null, 'none' ); } /** * Fetch robots.txt from this site. Bounded, no redirects. * * @param string $robots_url Absolute URL of robots.txt on this site. * @return string|null Body, or null on any failure. */ public static function fetch_robots_body( string $robots_url ): ?string { $response = wp_safe_remote_get( $robots_url, array( 'timeout' => self::ROBOTS_TIMEOUT, 'redirection' => 0, 'limit_response_size' => self::ROBOTS_MAX_BYTES, ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return null; } return (string) wp_remote_retrieve_body( $response ); } /** * Invoke a named collaborator. * * @param string $name Collaborator name. * @param mixed ...$args Arguments. * @return mixed */ private function call( string $name, ...$args ) { return call_user_func_array( $this->collaborators[ $name ], $args ); } /** * Build a result row. * * @param string|null $url Sitemap URL or null. * @param string $reason Why. * @return array{url: string|null, reason: string} */ private function result( ?string $url, string $reason ): array { return array( 'url' => $url, 'reason' => $reason, ); } /** * First `Sitemap:` line in robots.txt that is https, credential-free and on * this site's host. */ private function sitemap_from_robots(): ?string { $body = $this->call( 'robots_body' ); if ( ! is_string( $body ) || '' === $body ) { return null; } $home_host = wp_parse_url( (string) $this->call( 'home_url', '/' ), PHP_URL_HOST ); if ( ! is_string( $home_host ) || '' === $home_host ) { return null; } foreach ( preg_split( '/\r\n|\r|\n/', $body ) as $line ) { if ( 1 !== preg_match( '/^\s*sitemap\s*:\s*(\S+)\s*$/i', $line, $match ) ) { continue; } if ( $this->is_acceptable_sitemap( $match[1], $home_host ) ) { return $match[1]; } } return null; } /** * Whether a robots.txt sitemap URL may be offered. * * @param string $url Candidate URL. * @param string $home_host This site's host. */ private function is_acceptable_sitemap( string $url, string $home_host ): bool { $parts = wp_parse_url( $url ); if ( ! is_array( $parts ) || ! isset( $parts['scheme'], $parts['host'] ) ) { return false; } return 'https' === strtolower( $parts['scheme'] ) && ! isset( $parts['user'] ) && ! isset( $parts['pass'] ) && strtolower( $parts['host'] ) === strtolower( $home_host ); } }