I have added the following filter to my utility plugin.
$__wpdm_all_roles = null;
add_filter('wpdm_allowed_roles', function ($roles, $id) {
$roles = get_post_meta($id, '__wpdm_access', true);
$roles = maybe_unserialize($roles);
if(!is_array($roles)) {
$roles = array();
$current_term = get_term_by('slug', get_query_var( 'term' ), 'wpdmcategory');
if ($current_term) {
$roles = \WPDM\libs\CategoryHandler::GetAllowedRoles($current_term->term_id);
} else {
$cats = get_the_terms( $id, 'wpdmcategory' );
if (is_array($cats)){
foreach($cats as $cat){
$croles = \WPDM\libs\CategoryHandler::GetAllowedRoles($cat->term_id);
$roles = array_merge($roles, $croles);
}
} else {
$roles[] = 'guest';
}
}
}
$roles = array_unique($roles);
if (count($roles) == 1) {
/*
* Take this one role as starting point in a hierarchical roles array of all WP roles.
* We assume the WP roles array stays in a hierarchical order from most to least permissive.
*
* We're going to add roles which are more permissive than this defined one.
*/
if (!$__wpdm_all_roles) {
$__wpdm_all_roles = array();
global $wp_roles;
foreach ($wp_roles->get_names() as $role ► $name) {
$__wpdm_all_roles[] = $role;
}
//$__wpdm_all_roles = array_reverse($__wpdm_all_roles);
$__wpdm_all_roles[] = 'guest';
}
// search this one role in the array with all role names
$index = array_search($roles[0], $__wpdm_all_roles);
if ($index !== FALSE) { // found it
// the roles before this one are more permissive. so, add these to the array
while ($index) {
$roles[] = $__wpdm_all_roles[--$index];
}
}
}
return $roles;
}, 10, 2);