How to Auto-Encrypt Files Uploaded via Advanced Forms

Requirements:

Step 1: Navigate to Prevent Direct Access Gold from your admin dashboard and enable “Auto-protect New File Uploads” option.

Step 2: Add the following code snippet to your (child) theme’s functions.php file.

add_filter( 'pda_after_protect_file_when_upload', 'pda_gold_maybe_encrypt_file', 1000, 2 );
function pda_gold_maybe_encrypt_file( $attachment_id ) {
	if ( ! class_exists( 'PDAGOLD\modules\Files\Service' ) ) {
		return;
	}
	$forms        = [ 'advanced-forms-id' ];
	$current_form = \Pda_v3_Gold_Helper::get( $_POST, 'af_form', false );
	if ( ! $current_form || ! in_array( $current_form, $forms ) ) {
		return;
	}
	try {
		$crypto_service = new PDAGOLD\modules\Files\Service();
		$crypto_service->encrypt_file( $attachment_id );
	} catch ( \Exception $exception ) {
		( new PDA_Logger() )->notice( $exception->getMessage() );
	}
	return;
}

Don’t forget to update the form ID accordingly. You can find it in the Advanced Forms shortcode.

Lasted updated on May 13, 2021