SHARE
TWEET

Untitled

a guest Aug 19th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // This code add basic access permissions to Real Media Library
  4.  
  5. /**
  6.  * @param string $role
  7.  * @param MatthiasWeb\RealMediaLibrary\api\IFolder $folder
  8.  * @return bool
  9.  */
  10. function rmlp_role_has_access_to_folder($role, MatthiasWeb\RealMediaLibrary\api\IFolder $folder) {
  11.     $allowed_groups = get_media_folder_meta($folder->getId(), 'allowed_groups', true);
  12.     if($allowed_groups) {
  13.         return in_array($role, $allowed_groups);
  14.     }
  15.     elseif($folder->getParent() >= 0) {
  16.         return rmlp_role_has_access_to_folder($role, wp_rml_get_by_id($folder->getParent()));
  17.     }
  18.     else {
  19.         return false;
  20.     }
  21. }
  22.  
  23. add_filter('RML/Tree/Parsed', function ($parsed) {
  24.     $logged_in_user = wp_get_current_user();
  25.     $roles = ( array )$logged_in_user->roles;
  26.    
  27.     // Skip permission checks for administrators
  28.     if(in_array('administrator', $roles)) {
  29.         return $parsed;
  30.     }
  31.  
  32.     foreach ($parsed as $folder) {
  33.         $folder->setVisible(false);
  34.     }
  35.     foreach ($roles as $role) {
  36.         foreach ($parsed as $folder) {
  37.             if(rmlp_role_has_access_to_folder($role, $folder)) {
  38.                 //var_dump($role);
  39.                 $folder->setVisible(true);
  40.             }
  41.         }
  42.     }
  43.    
  44.     return $parsed;
  45. });
  46.  
  47.  
  48. // Permissions metabox
  49.  
  50. // Allow only administrators to change access permissions
  51. if ( in_array( 'administrator', (array) wp_get_current_user()->roles ) ) {
  52.     add_filter("RML/Folder/Meta/Groups", function($groups) {
  53.         $groups["permissions"] = __("Permissions");
  54.         return $groups;
  55.     });
  56.    
  57.     add_rml_meta_box(
  58.         'allowed_groups',
  59.         new class implements MatthiasWeb\RealMediaLibrary\api\IMetadata {
  60.             public function content($content, $folder) {
  61.                 $allowed_groups = get_media_folder_meta($folder->getId(), 'allowed_groups', true);
  62.                 if(!is_array($allowed_groups)) $allowed_groups = [];
  63.                 ob_start();
  64.                 foreach(wp_roles()->role_names as $role_key => $role_name):
  65.                 ?>
  66.                     <div>
  67.                         <input type="checkbox" name="rmlp_allowed_groups[<?php echo $role_key; ?>]" <?php echo in_array($role_key, $allowed_groups) ? 'checked' : '' ?> value="<?php echo $role_key; ?>"> <?php echo $role_name; ?>
  68.                     </div>
  69.                 <?php
  70.                 endforeach;
  71.                
  72.                 return ob_get_clean();
  73.             }
  74.    
  75.             public function save($response, $folder, $request) {
  76.                 $allowed_groups = array_values($_POST['rmlp_allowed_groups']);
  77.                 update_media_folder_meta($folder->getId(), 'rmlp_allowed_groups', $allowed_groups);
  78.                 return $response;
  79.             }
  80.    
  81.             public function scripts($assets) {
  82.    
  83.             }
  84.         },
  85.         false,
  86.         10,
  87.         'permissions'
  88.     );
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top