Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories

Synchronize::process_product()privateWC 1.0

Processes an individual downloadable product, adding the parent paths for any downloadable files to the Approved Download Directories list.

Any such paths will be added with the disabled flag set, because we want a site administrator to review and approve first.

Method of the class: Synchronize{}

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->process_product( $product );
$product(WC_Product) (required)
The product we wish to examine for downloadable file paths.

Synchronize::process_product() code WC 8.7.0

private function process_product( WC_Product $product ) {
	$downloads = $product->get_downloads();

	foreach ( $downloads as $downloadable ) {
		$parent_url = _x( 'invalid URL', 'Approved product download URLs migration', 'woocommerce' );

		try {
			$download_file = $downloadable->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' === $downloadable->get_type_of_file_path() ) {
				$download_file = do_shortcode( $download_file );
			}

			$parent_url = ( new URL( $download_file ) )->get_parent_url();
			$this->register->add_approved_directory( $parent_url, false );
		} catch ( Exception $e ) {
			wc_get_logger()->log(
				'error',
				sprintf(
				/* translators: %s is a URL, %d is a product ID. */
					__( 'Product download migration: %1$s (for product %1$d) could not be added to the list of approved download directories.', 'woocommerce' ),
					$parent_url,
					$product->get_id()
				)
			);
		}
	}
}