NGG_Roles

The roles admin screen.

Defined (1)

The class is defined in the following location(s).

/admin/class-ngg-roles.php  
  1. class NGG_Roles extends NGG_Post_Admin_Page { 
  2.  
  3. public function display() { 
  4. parent::display(); 
  5.  
  6. ?> 
  7. <div class="wrap"> 
  8. <h2><?php _e('Roles / capabilities', 'nggallery') ;?></h2> 
  9. <p><?php _e('Select the lowest role which should be able to access the following capabilities. NextCellent Gallery supports the standard roles from WordPress.', 'nggallery') ?> <br /> 
  10. <?php _e('For a more flexible user management you can use the', 'nggallery') ?> <a href="http://wordpress.org/extend/plugins/capsman/" target="_blank">Capability Manager</a>.</p> 
  11. <form name="addroles" id="addroles" method="POST" accept-charset="utf-8" > 
  12. <?php wp_nonce_field('ngg_addroles') ?> 
  13. <table class="form-table"> 
  14. <tr valign="top"> 
  15. <th scope="row"><label for="general"><?php _e('NextCellent Gallery overview', 'nggallery') ;?></label></th> 
  16. <td><select name="general" id="general"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Gallery overview') ); ?></select></td> 
  17. </tr> 
  18. <tr valign="top"> 
  19. <th scope="row"><label for="tinymce"><?php _e('Use TinyMCE Button / Add Media', 'nggallery') ;?></label></th> 
  20. <td><select name="tinymce" id="tinymce"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Use TinyMCE') ); ?></select></td> 
  21. </tr> 
  22. <tr valign="top"> 
  23. <th scope="row"><label for="add_gallery"><?php _e('Add gallery / Upload images', 'nggallery') ;?></label></th> 
  24. <td><select name="add_gallery" id="add_gallery"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Upload images') ); ?></select></td> 
  25. </tr> 
  26. <tr valign="top"> 
  27. <th scope="row"><label for="manage_gallery"><?php _e('Manage gallery', 'nggallery') ;?></label></th> 
  28. <td><select name="manage_gallery" id="manage_gallery"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Manage gallery') ); ?></select></td> 
  29. </tr> 
  30. <tr valign="top"> 
  31. <th scope="row"><label for="manage_others"><?php _e('Manage others gallery', 'nggallery') ;?></label></th> 
  32. <td><select name="manage_others" id="manage_others"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Manage others gallery') ); ?></select></td> 
  33. </tr> 
  34. <tr valign="top"> 
  35. <th scope="row"><label for="manage_tags"><?php _e('Manage tags', 'nggallery') ;?></label></th> 
  36. <td><select name="manage_tags" id="manage_tags"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Manage tags') ); ?></select></td> 
  37. </tr> 
  38. <tr valign="top"> 
  39. <th scope="row"><label for="edit_album"><?php _e('Edit Album', 'nggallery') ;?></label></th> 
  40. <td><select name="edit_album" id="edit_album"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Edit album') ); ?></select></td> 
  41. </tr> 
  42. <tr valign="top"> 
  43. <th scope="row"><label for="change_style"><?php _e('Change style', 'nggallery') ;?></label></th> 
  44. <td><select name="change_style" id="change_style"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Change style') ); ?></select></td> 
  45. </tr> 
  46. <tr valign="top"> 
  47. <th scope="row"><label for="change_options"><?php _e('Change options', 'nggallery') ;?></label></th> 
  48. <td><select name="change_options" id="change_options"><?php wp_dropdown_roles( $this->ngg_get_role('NextGEN Change options') ); ?></select></td> 
  49. </tr> 
  50. </table> 
  51. <div class="submit"><input type="submit" class="button-primary" name= "update_cap" value="<?php _e('Update capabilities', 'nggallery') ;?>"/></div> 
  52. </form> 
  53. </div> 
  54. <?php 
  55.  
  56.  
  57. protected function processor() { 
  58. if ( isset($_POST['update_cap']) ) { 
  59.  
  60. check_admin_referer('ngg_addroles'); 
  61.  
  62. // now set or remove the capability 
  63. $this->ngg_set_capability($_POST['general'], "NextGEN Gallery overview"); 
  64. $this->ngg_set_capability($_POST['tinymce'], "NextGEN Use TinyMCE"); 
  65. $this->ngg_set_capability($_POST['add_gallery'], "NextGEN Upload images"); 
  66. $this->ngg_set_capability($_POST['manage_gallery'], "NextGEN Manage gallery"); 
  67. $this->ngg_set_capability($_POST['manage_others'], "NextGEN Manage others gallery"); 
  68. $this->ngg_set_capability($_POST['manage_tags'], "NextGEN Manage tags"); 
  69. $this->ngg_set_capability($_POST['edit_album'], "NextGEN Edit album"); 
  70. $this->ngg_set_capability($_POST['change_style'], "NextGEN Change style"); 
  71. $this->ngg_set_capability($_POST['change_options'], "NextGEN Change options"); 
  72.  
  73. nggGallery::show_message(__('Updated capabilities', "nggallery")); 
  74.  
  75. private function ngg_get_sorted_roles() { 
  76. // This function returns all roles, sorted by user level (lowest to highest) 
  77. global $wp_roles; 
  78. $roles = $wp_roles->role_objects; 
  79. $sorted = array(); 
  80.  
  81. if( class_exists('RoleManager') ) { 
  82. foreach( $roles as $role_key => $role_name ) { 
  83. $role = get_role($role_key); 
  84. if( empty($role) ) continue; 
  85. $role_user_level = array_reduce(array_keys($role->capabilities), array('WP_User', 'level_reduction'), 0); 
  86. $sorted[$role_user_level] = $role; 
  87. $sorted = array_values($sorted); 
  88. } else { 
  89. $role_order = array("subscriber", "contributor", "author", "editor", "administrator"); 
  90. foreach($role_order as $role_key) { 
  91. $sorted[$role_key] = get_role($role_key); 
  92. return $sorted; 
  93.  
  94. private function ngg_get_role($capability) { 
  95. // This function return the lowest roles which has the capabilities 
  96. $check_order = $this->ngg_get_sorted_roles(); 
  97.  
  98. $args = array_slice(func_get_args(), 1); 
  99. $args = array_merge(array($capability), $args); 
  100.  
  101. foreach ($check_order as $check_role) { 
  102. if ( empty($check_role) ) 
  103. return false; 
  104.  
  105. if (call_user_func_array(array(&$check_role, 'has_cap'), $args)) 
  106. return $check_role->name; 
  107. return false; 
  108.  
  109. private function ngg_set_capability($lowest_role, $capability) { 
  110. // This function set or remove the $capability 
  111. $check_order = $this->ngg_get_sorted_roles(); 
  112.  
  113. $add_capability = false; 
  114.  
  115. foreach ($check_order as $the_role) { 
  116. $role = $the_role->name; 
  117.  
  118. if ( $lowest_role == $role ) 
  119. $add_capability = true; 
  120.  
  121. // If you rename the roles, then please use a role manager plugin 
  122.  
  123. if ( empty($the_role) ) 
  124. continue; 
  125.  
  126. $add_capability ? $the_role->add_cap($capability) : $the_role->remove_cap($capability) ;