WP_URL_Pattern_Prefixer::prefix_path_pattern │ public │ WP 6.8.0

Prefixes the given URL path pattern with the base path for the given context.

This ensures that these path patterns work correctly on WordPress subdirectory sites, for example in a multisite network, or when WordPress itself is installed in a subdirectory of the hostname.

The given URL path pattern is only prefixed if it does not already include the expected prefix.

Метод класса: WP_URL_Pattern_Prefixer{}

Хуков нет.

Возвращает

string. URL pattern, prefixed as necessary.

Использование

$WP_URL_Pattern_Prefixer = new WP_URL_Pattern_Prefixer();
$WP_URL_Pattern_Prefixer->prefix_path_pattern( $path_pattern, $context ): string;
$path_pattern(строка) (обязательный)
URL pattern starting with the path segment.
$context(строка)
Context to use for prefixing the path pattern.
По умолчанию: 'home'

Список изменений

С версии 6.8.0 Введена.

Код WP_URL_Pattern_Prefixer::prefix_path_pattern() WP 7.1.2

public function prefix_path_pattern( string $path_pattern, string $context = 'home' ): string {
	// If context path does not exist, the context is invalid.
	if ( ! isset( $this->contexts[ $context ] ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			esc_html(
				sprintf(
					/* translators: %s: context string */
					__( 'Invalid URL pattern context %s.' ),
					$context
				)
			),
			'6.8.0'
		);
		return $path_pattern;
	}

	/*
	 * In the event that the context path contains a :, ? or # (which can cause the URL pattern parser to switch to
	 * another state, though only the latter two should be percent encoded anyway), it additionally needs to be
	 * enclosed in grouping braces. The final forward slash (trailingslashit ensures there is one) affects the
	 * meaning of the * wildcard, so is left outside the braces.
	 */
	$context_path         = $this->contexts[ $context ];
	$escaped_context_path = $context_path;
	if ( strcspn( $context_path, ':?#' ) !== strlen( $context_path ) ) {
		$escaped_context_path = '{' . substr( $context_path, 0, -1 ) . '}/';
	}

	/*
	 * If the path already starts with the context path (including '/'), remove it first
	 * since it is about to be added back.
	 */
	if ( str_starts_with( $path_pattern, $context_path ) ) {
		$path_pattern = substr( $path_pattern, strlen( $context_path ) );
	}

	return $escaped_context_path . ltrim( $path_pattern, '/' );
}