Last active
April 20, 2020 11:15
-
-
Save gaupoit/503bd20303e2a81aec670064465947f1 to your computer and use it in GitHub Desktop.
Integrate Prevent Direct Access Gold with WordPress File Manager Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'pda_s3_pre_sync', 'pda_fix_nmedia_files' ); // Add this line if you are using PDA S3 Integration | |
add_action( 'pda_before_protect_file', 'pda_fix_nmedia_files' ); | |
/** | |
* Try to correct the _wp_attached_file data. | |
* | |
* Files are uploaded by nmedia plugin its _wp_attached_file are post_url. | |
* This function tries to correct them: | |
* 1. Get the post parent because nmedia creates two kind of post data. One appears in the WordPress Media UI and its parent is managed by nmedia. | |
* 2. Get the parent file path | |
* 3. Update the correct _wp_attached_file post data. | |
* | |
* @param int $attachment_id The attachment's ID. | |
*/ | |
function pda_fix_nmedia_files( $attachment_id ) { | |
if ( ! is_callable( 'wpfm_get_file_path_by_id' ) ) { | |
return; | |
} | |
$parent_id = wp_get_post_parent_id( $attachment_id ); | |
if ( empty( $parent_id ) ) { | |
return; | |
} | |
$upload_dir = wp_upload_dir(); | |
$nmedia_file = wpfm_get_file_path_by_id( $parent_id ); | |
if ( ! isset( $nmedia_file ) || ! is_file( $nmedia_file ) ) { | |
return; | |
} | |
// The nmedia file path is joined by: | |
// $upload_dir ['basedir'], WPFM_USER_UPLOADS, $current_user -> user_login | |
// The correct _wp_attached_file doesn't need the $upload_dir ['basedir'] | |
$nmedia_file_file = str_replace( $upload_dir ['basedir'] . '/', '', $nmedia_file ); | |
update_post_meta( $attachment_id, '_wp_attached_file', $nmedia_file_file ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment