Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories

Register::is_valid_path()publicWC 1.0

Indicates if the URL is within an approved directory. The approved directory must be enabled (it is possible for individual approved directories to be disabled).

For instance, for 'https://storage.king/12345/ebook.pdf' to be valid then 'https://storage.king/12345' would need to be within our register.

If the provided URL is a filepath it can be passed in without the 'file://' scheme.

Method of the class: Register{}

No Hooks.

Return

true|false.

Usage

$Register = new Register();
$Register->is_valid_path( $download_url ): bool;
$download_url(string) (required)
The URL to check.

Register::is_valid_path() code WC 8.6.1

public function is_valid_path( string $download_url ): bool {
	global $wpdb;

	$parent_directories = array();

	foreach ( ( new URL( $this->normalize_url( $download_url ) ) )->get_all_parent_urls() as $parent ) {
		$parent_directories[] = "'" . esc_sql( $parent ) . "'";
	}

	if ( empty( $parent_directories ) ) {
		return false;
	}

	$parent_directories = join( ',', $parent_directories );
	$table              = $this->get_table();

	// Look for a rule that matches the start of the download URL being tested. Since rules describe parent
	// directories, we also ensure it ends with a trailing slash.
	//
	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$matches = (int) $wpdb->get_var(
		"
			SELECT COUNT(*)
			FROM   {$table}
			WHERE  enabled = 1
			       AND url IN ( {$parent_directories} )
		"
	);
	// phpcs:enable

	return $matches > 0;
}