PDA Gold Custom Snippets

In this post, we’ll cover a few useful snippets you’d need to customize and extend our PDA Gold features.

Prevent Direct Access Gold

Check if a Specific File is Protected

Here’s how you can check if a specific file is protected.

if ( class_exists('PDA_v3_Gold_Repository') ) {
	$attachment_id = 15230;
	$repo = new PDA_v3_Gold_Repository();
	$is_protected = $repo->is_protected_file($attachment_id);
}

Get File Access Permission for Specific Files

Requirements:

Is it possible to get the allowed user roles from an attachment ID, URL, or any other attachment parameter?

You can use  get_post_meta( $attachment_id, ‚ pda_protection' );  to check if a file is protected. If you’d also like to know which user roles are allowed to access that certain file, please use the following snippet:

$repo_ip_block = new Pda_Ip_Block_Repository();
return $repo_ip_block->get_fap_info_by_post_id( $post_id );

Sample JSON data returned:

{"type":"custom-roles","roles":"editor","user_access":{"type":{"label":"Choose custom users","value":"custom-users"},"users":[{"value":2,"label":"editor"}]}}

Set Custom Whitelisted Roles

Is it possible to always allow the user role editor to access all files no matter what custom user roles are assigned?

Requirements:

add_filter( 'pda_file_access_permission', 'handle_pda_file_access_permission', 1, 2 );
/**
 * Allow custom whitelisted roles.
 *
 * @param bool $allowed       Whether having valid permission.
 * @param int  $attachment_id The attachment ID.
 *
 * @return bool
 */
function handle_pda_file_access_permission( $allowed, $attachment_id ) {
   if ( $allowed ) {
      return $allowed;
   }   
   $whitelisted_roles = [ 'editor' ];
   $current_roles     = wp_get_current_user()->roles;
   if ( ! empty( array_intersect( $current_roles, $whitelisted_roles ) ) ) {
      return true;
   }   
      return $allowed;
}

Grant File Access Permission to Multiple Users

Requirements:

In order to grant file access permission to multiple users at once, simply add the following code snippet to your (child) theme functions.php.

function pda_add_user($ids, $users) {
	if ( ! method_exists( 'Pda_Ip_Block_Repository', 'set_fap_to_db' ) ) {
		return;
	}
	foreach($ids as $post_id) {
		foreach ($users as $user_id) {
			$user = get_user_by( 'id', $user_id );
			$data = array(
		'post_id'               => $post_id,
		'file_access_permision' => 'default',
		'type'                  => array(
			"label" => "Choose custom users",
			"value" => "custom-users",
		),
		'user_roles'            => '',
		'users'                 => array(
			array(
				'label' => $user->user_login,
				'value' => $user_id,
			),)
	);
			$api = new Pda_Ip_Block_Repository();
			$api->set_fap_to_db( $data );
		}
	}
}
pda_add_user([file-id,8761,8762], [user-id,9,10]);

The first array [file-id,8761,8762] contains all your protected file IDs while the second array [user-id,9,10] contains all the user IDs of whom you want to grant access to.

You can get the ID number by hovering over the desired file’s title or username.

Since this code is required to be run once, you should remove it once the file permission is updated successfully.

Take a look at our PDA Gold hooks and APIs.

Prevent Direct Access Lite

Protect or unprotect a file

Use the following code snippet to protect or unprotect a file.

$api = new PDA_Lite_API();
$data = array(
	'id' => 6 // replace with your file ID
);
$pda_repo = new Repository();
$is_protected = $pda_repo->is_protected_file($data['id']);

//Unprotect file
if ($is_protected) {
	$result = $api->un_protect_files($data);
}

//Protect file
if (!$is_protected) {
	$result = $api->protect_files($data);
}

Please note that you can protect up to 9 files with our PDA Lite.

Protect Pages and Posts Gold

Use the following code snippet to generate private access links

if ( class_exists( 'Prevent_Page_Pup_Function_Gold' )
 && class_exists( 'Prevent_Page_Pup_Database_Gold' ) ) {
 $post_id = 2434;
 $pup_function = new Prevent_Page_Pup_Function_Gold();
 $db = new Prevent_Page_Pup_Database_Gold();
 $page_token_id = $pup_function->post_page_token_generator();
 $values = array(
 'expired_date' => $pup_function->get_expired_timestamp( 1, 'minutes' ),
 );
 $uri = $db->insert_page_private_link( $post_id, $page_token_id, 1, $values );

 $private_url = $pup_function->massagePrivateUrl( $uri, $post_id );
 }

PDA S3 Integration

Use the following code snippet to generate pre-signed S3 URLs

if ( class_exists( 'PDA_S3_UTILS' ) && function_exists( 'pda_get_signed_url_timeout' ) ) {
 $util = new PDA_S3_UTILS();
 $attachment_id = 1;
 $time_die = pda_get_signed_url_timeout();
 $signedURL = $util->create_presigned_url( $attachment_id, $time_die );
 }
Lasted updated on April 8, 2021