wc_tokenize_path()WC 4.3.0

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

No Hooks.

Return

String. The tokenized path.

Usage

wc_tokenize_path( $path, $path_tokens );
$path(string) (required)
The absolute path to tokenize.
$path_tokens(array) (required)
An array keyed with the token, containing paths that should be replaced.

Changelog

Since 4.3.0 Introduced.

wc_tokenize_path() code 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;
}