WC_Product_Download::approved_directory_checks()privateWC 1.0

Confirms that the download exists within an approved directory.

If it is not within an approved directory but the current user has sufficient capabilities, then the method will try to add the download's directory to the approved directory list.

{} Это метод класса: WC_Product_Download{}

Возвращает

null. Ничего.

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

// private - только в коде основоного (родительского) класса
$result = $this->approved_directory_checks( $auto_add_to_approved_directory_list );
$auto_add_to_approved_directory_list(true|false)
If the download is not already in the approved directory list, automatically add it if possible.
По умолчанию: true

Код WC_Product_Download::approved_directory_checks() WC 6.6.1

private function approved_directory_checks( bool $auto_add_to_approved_directory_list = true ) {
	$download_directories = wc_get_container()->get( Download_Directories::class );

	if ( $download_directories->get_mode() !== Download_Directories::MODE_ENABLED ) {
		return;
	}

	$download_file = $this->get_file();

	/**
	 * Controls whether shortcodes should be resolved and validated using the Approved Download Directory feature.
	 *
	 * @param bool $should_validate
	 */
	if ( apply_filters( 'woocommerce_product_downloads_approved_directory_validation_for_shortcodes', true ) && 'shortcode' === $this->get_type_of_file_path() ) {
		$download_file = do_shortcode( $download_file );
	}

	$is_site_administrator   = is_multisite() ? current_user_can( 'manage_sites' ) : current_user_can( 'manage_options' );
	$valid_storage_directory = $download_directories->is_valid_path( $download_file );

	if ( $valid_storage_directory ) {
		return;
	}

	if ( $auto_add_to_approved_directory_list ) {
		try {
			// Add the parent URL to the approved directories list, but *do not enable it* unless the current user is a site admin.
			$download_directories->add_approved_directory( ( new URL( $download_file ) )->get_parent_url(), $is_site_administrator );
			$valid_storage_directory = $download_directories->is_valid_path( $download_file );
		} catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
			// At this point, $valid_storage_directory will be false. Fall-through so the appropriate exception is
			// triggered (same as if the storage directory was invalid and $auto_add_to_approved_directory_list was false.
		}
	}

	if ( ! $valid_storage_directory ) {
		throw new Exception(
			sprintf(
				/* translators: %1$s is the downloadable file path, %2$s is an opening link tag, %3%s is a closing link tag. */
				__( 'The downloadable file %1$s cannot be used: it is not located in an approved directory. Please contact a site administrator for help. %2$sLearn more.%3$s', 'woocommerce' ),
				'<code>' . $download_file . '</code>',
				'<a href="https://woocommerce.com/document/approved-download-directories">',
				'</a>'
			)
		);
	}
}