wc_tokenize_path()WC 4.3.0

Given a path, this will convert any of the subpaths into their corresponding tokens.

Хуков нет.

Возвращает

Строку. The tokenized path.

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

wc_tokenize_path( $path, $path_tokens );
$path(строка) (обязательный)
The absolute path to tokenize.
$path_tokens(массив) (обязательный)
An array keyed with the token, containing paths that should be replaced.

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

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

Код wc_tokenize_path() WC 8.7.0

function wc_tokenize_path( $path, $path_tokens ) {
	// Order most to least specific so that the token can encompass as much of the path as possible.
	uasort(
		$path_tokens,
		function ( $a, $b ) {
			$a = strlen( $a );
			$b = strlen( $b );

			if ( $a > $b ) {
				return -1;
			}

			if ( $b > $a ) {
				return 1;
			}

			return 0;
		}
	);

	foreach ( $path_tokens as $token => $token_path ) {
		if ( 0 !== strpos( $path, $token_path ) ) {
			continue;
		}

		$path = str_replace( $token_path, '{{' . $token . '}}', $path );
	}

	return $path;
}