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
- Prevent Direct Access Lite
- Protect Pages and Posts Gold
- PDA S3 Integration
Prevent Direct Access Gold
Protect a File
Use the following code snippet to protect a file.
if ( class_exists( 'PDA_Private_Link_Services' ) ) { //669 is attachment's id. $result = PDA_Private_Link_Services::protect_file( 669 ); if ( is_wp_error( $result ) ) { echo $result->get_error_message(); } }
You can get the file ID by hovering over the desired file’s title.
Unprotect a File
Use the following code snippet to unprotect a file.
if ( class_exists('PDA_v3_Gold_Repository') ) {
$attachment_id = 15230;
$repo = new PDA_v3_Gold_Repository();
$repo->un_protect_file($attachment_id);
}
You can get the file ID by hovering over the desired file’s title.
Check File Protection Status
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);
}
Generate Private Download Links
Use the following code snippet to generate private download links for a specific protected file.
if ( class_exists( 'PDA_Private_Link_Services' ) ) { //669 is file ID return PDA_Private_Link_Services::create_private_link( 669, array( 'expired_days' => 1, 'limit_downloads' => 1, 'url' => 'custom-slug' ) ); }
You can get the file ID by hovering over the desired file’s title.
Check File Access Permission
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.
Show Protected Files in Media Library to Authorized Users only
Requirements:
While users can’t access your protected files without logging in with correct permission, they can see them listed in Media Library.
In order to completely hide protected files in Media Library from unauthorized users, simply add the following code snippet to your (child) theme functions.php.
add_action( 'pre_get_posts', function ( $query ) { global $pagenow; if ( ( ! isset( $_POST['action'] ) || $_POST['action'] !== 'query-attachments' ) && ( ( ! is_admin() || ! $query->is_main_query() ) && $pagenow === 'upload.php' ) ) { return; } if ( ! method_exists( 'PDA_v3_Gold_Repository', 'get_all_post_id_protect' ) ) { return; } if ( ! method_exists( 'Pda_Gold_Functions', 'check_file_access_permission_for_post' ) ) { return; } $repo = new PDA_v3_Gold_Repository(); $func = new Pda_Gold_Functions(); $all_post_id_protect = $repo->get_all_post_id_protect(); $un_accessed_file = array_filter( $all_post_id_protect, function ( $post ) use ( $func ) { return ! $func->check_file_access_permission_for_post( $post->post_id ); } ); $un_accessed_file_ids = array_map( function ( $post ) { return $post->post_id; }, $un_accessed_file ); $query->set( 'post__not_in', $un_accessed_file_ids ); } );
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 ); }