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() {
	if ( ! method_exists( 'Pda_Ip_Block_Repository', 'set_fap_to_db' ) ) {
		return;
	}
	$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' => "username",
				'value' => user_id,
			),
		),
	);
	$api = new Pda_Ip_Block_Repository();
	$api->set_fap_to_db( $data );
}
pda_add_user();

For example, the following code snippet will allow “dev”, “tester” and “admin” users to access the file with id 890.

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

function pda_add_user() {
	if ( ! method_exists( 'Pda_Ip_Block_Repository', 'set_fap_to_db' ) ) {
		return;
	}
	$data = array(
		'post_id'               => 890,
		'file_access_permision' => 'default',
		'type'                  => array(
			"label" => "Choose custom users",
			"value" => "custom-users",
		),
		'user_roles'            => '',
		'users'                 => array(
			array(
				'label' => 'dev',
				'value' => 7,
			),
            array(
				'label' => 'tester',
				'value' => 8,
			),
            array(
				'label' => 'admin',
				'value' => 10,
			),
		),
	);
	$api = new Pda_Ip_Block_Repository();
	$api->set_fap_to_db( $data );
}
pda_add_user();

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

Take a look at our PDA Gold hooks and APIs.

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 December 10, 2020