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.

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

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?

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;
}

Take a look at our PDA Gold hooks and APIs.

Lasted updated on August 26, 2020