wc_tokenize_path( string $path, array $path_tokens )

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


Description Description


Parameters Parameters

$path

(Required) The absolute path to tokenize.

$path_tokens

(Required) An array keyed with the token, containing paths that should be replaced.


Top ↑

Return Return

(string) The tokenized path.


Top ↑

Source Source

File: includes/wc-core-functions.php

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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.