Skip to content

Instantly share code, notes, and snippets.

@davidallenlewis
Last active September 23, 2024 14:18
Show Gist options
  • Save davidallenlewis/a75c6cbd2056b9db060a93343e0fda9d to your computer and use it in GitHub Desktop.
Save davidallenlewis/a75c6cbd2056b9db060a93343e0fda9d to your computer and use it in GitHub Desktop.
Patches for WP Display Files Plugin version Version 4.0.8 from WePlugins
<?php
/* @PATCH
* /inc/listing/class-wpdf-downloader.php on line 52
* Allows shortcode files to download properly when a user has multiple roles or one role that is not zero-indexed
*/
// @HACK by David Lewis start
$current_user_info = wp_get_current_user();
$user_id = $current_user_info->data->ID;
// @FIX get full array of user’s roles not just the first role at key "0"
//$user_role = $current_user_info->roles[0];
$user_roles = $current_user_info->roles;
// @FIX convert ['wpdf_user_names'][0] to an array otherwise it's a string and in_array() won't work
$permissions_wpdf_user_names = explode(',', $permissions['wpdf_user_names'][0]);
// Shortcode has User AND Role restrictions
if( ! empty( $permissions_wpdf_user_names ) && ! empty( $permissions['wpdf_user_roles'] ) ) {
if ( ! in_array( $user_id, $permissions_wpdf_user_names ) ) {
wp_die( $msg ); // not allowed
}
// Shortcode ONLY has User restrictions
} else if( ! empty( $permissions_wpdf_user_names ) ) {
if ( ! in_array( $user_id, $permissions_wpdf_user_names ) ) {
wp_die( $msg ); // not allowed
}
// Shortcode ONLY has Role restrictions
} else if( ! empty( $permissions['wpdf_user_roles'] ) ) {
if ( ! array_intersect( $user_roles, $permissions['wpdf_user_roles'] ) ) {
wp_die( $msg ); // not allowed
}
}
// @HACK by David Lewis end
/* @PATCH
* /modules/display/modal.display.php on line 201
* Allows shortcode files to show properly when a user has multiple roles or one role that is not zero-indexed
*/
} else if ( ! empty( $this->shortcode->wpdf_permissions_setting['wpdf_user_roles'] ) && $this->shortcode->wpdf_permissions_setting['wpdf_user_roles'][0] != 'all' ) {
$user_roles = $current_user_info->roles;
if( ! empty( $this->shortcode->wpdf_permissions_setting['wpdf_user_names'] ) ) {
$user_id = $current_user_info->data->ID;
if ( ! in_array( $user_id, $this->shortcode->wpdf_permissions_setting['wpdf_user_names'] ) && empty( array_intersect( $user_roles, $this->shortcode->wpdf_permissions_setting['wpdf_user_roles']) ) ) {
$this->display_content = false;
}
}else{
if ( empty( array_intersect( $user_roles, $this->shortcode->wpdf_permissions_setting['wpdf_user_roles'] ) ) ) {
$this->display_content = false;
}
}
}
@davidallenlewis
Copy link
Author

The original code uses $current_user_info->roles[0]; which will break functionality (permission checkes) if a user account has multiple roles or one role that is not zero-indexed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment