settings = $settings ?? new SitemapRepository(); } /** * @param array> $url_list * * @return array> */ public function filter( array $url_list ): array { $rules = $this->settings->get()['excluded_urls']; if ( array() === $rules ) { return $url_list; } return array_values( array_filter( $url_list, function ( $entry ) use ( $rules ): bool { $loc = is_array( $entry ) ? (string) ( $entry['loc'] ?? '' ) : ''; return '' === $loc || ! $this->matches( $loc, $rules ); } ) ); } /** * @param string[] $rules */ private function matches( string $url, array $rules ): bool { $path = (string) ( wp_parse_url( $url, PHP_URL_PATH ) ?? '/' ); // Compare DECODED paths: WordPress stores non-Latin slugs with // lowercase octets ("%d0%ba"), a browser copies them uppercase // ("%D0%BA") and a user may type the plain characters ("к") — all // three name the same URL. $path = '' === $path ? '/' : rawurldecode( $path ); foreach ( $rules as $rule ) { $rule = rawurldecode( $rule ); if ( str_ends_with( $rule, '*' ) ) { $prefix = rtrim( substr( $rule, 0, -1 ), '/' ); // "/promo/*" matches /promo/, /promo/x and /promo itself — // but never /promocja (a path-segment boundary is required). if ( '' === $prefix || rtrim( $path, '/' ) === $prefix || str_starts_with( $path, $prefix . '/' ) ) { return true; } continue; } if ( rtrim( $path, '/' ) === rtrim( $rule, '/' ) && ( '/' !== $rule || '/' === $path ) ) { return true; } } return false; } }