WC_Product_Download::get_absolute_file_pathprivateWC 1.0

Resolve a stored "relative" download file reference to an absolute filesystem path.

A relative reference can be expressed either against the WordPress root (ABSPATH) or as a root-relative path into the content directory; this resolves both, honoring a relocated WP_CONTENT_DIR.

Method of the class: WC_Product_Download{}

No Hooks.

Returns

String|false. The absolute filesystem path, the reference unchanged, or false if realpath() fails.

Usage

// private - for code of main (parent) class only
$result = $this->get_absolute_file_path( $file_url );
$file_url(string) (required)
The stored file reference, of "relative" type.

WC_Product_Download::get_absolute_file_path() code WC 11.0.1

private function get_absolute_file_path( $file_url ) {
	// Paths starting with ".." or not starting with "/" are taken relative to ABSPATH.
	$is_abspath_relative = '..' === substr( $file_url, 0, 2 ) || '/' !== substr( $file_url, 0, 1 );
	if ( $is_abspath_relative ) {
		return realpath( ABSPATH . $file_url );
	}

	// Root-relative paths into the content directory are resolved against WP_CONTENT_DIR.
	// Match the content dirname only at a path-segment boundary, so a content dir such as
	// "/app" does not false-match an unrelated path like "/application/...".
	$content_dirname     = FilesystemUtil::get_content_directory_relative_path();
	$is_content_relative = $file_url === $content_dirname || 0 === strpos( $file_url, trailingslashit( $content_dirname ) );
	if ( $is_content_relative ) {
		$path_within_content_dir = substr( $file_url, strlen( $content_dirname ) );
		return realpath( WP_CONTENT_DIR . $path_within_content_dir );
	}

	// Not an ABSPATH- or content-relative path; leave it untouched.
	return $file_url;
}