/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/ngglegacy/admin/roles.php

https://github.com/livinglab/openlab · PHP · 138 lines · 112 code · 21 blank · 5 comment · 10 complexity · 7e9c84b79393ce73aee07049db19d469 MD5 · raw file

  1. <?php
  2. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }
  3. function nggallery_admin_roles() {
  4. if ( !empty($_POST) ) {
  5. check_admin_referer('ngg_addroles');
  6. // now set or remove the capability
  7. ngg_set_capability($_POST['general'],"NextGEN Gallery overview");
  8. ngg_set_capability($_POST['tinymce'],"NextGEN Use TinyMCE");
  9. ngg_set_capability($_POST['add_gallery'],"NextGEN Upload images");
  10. ngg_set_capability($_POST['manage_gallery'],"NextGEN Manage gallery");
  11. ngg_set_capability($_POST['manage_others'],"NextGEN Manage others gallery");
  12. ngg_set_capability($_POST['manage_tags'],"NextGEN Manage tags");
  13. ngg_set_capability($_POST['edit_album'],"NextGEN Edit album");
  14. ngg_set_capability($_POST['change_style'],"NextGEN Change style");
  15. ngg_set_capability($_POST['change_options'],"NextGEN Change options");
  16. ngg_set_capability($_POST['attach_interface'],"NextGEN Attach Interface");
  17. }
  18. ?>
  19. <div class="wrap">
  20. <p>
  21. <?php _e('Select the lowest role which should be able to access the following capabilities. NextGEN Gallery supports the standard roles from WordPress.', 'nggallery') ?> <br />
  22. </p>
  23. <?php wp_nonce_field('ngg_addroles') ?>
  24. <table class="form-table">
  25. <tr valign="top">
  26. <th scope="row"><?php _e('Main NextGEN Gallery overview', 'nggallery') ;?>:</th>
  27. <td><label for="general"><select name="general" id="general"><?php wp_dropdown_roles( ngg_get_role('NextGEN Gallery overview') ); ?></select></label></td>
  28. </tr>
  29. <tr valign="top">
  30. <th scope="row"><?php _e('Use TinyMCE Button / Upload tab', 'nggallery') ;?>:</th>
  31. <td><label for="tinymce"><select name="tinymce" id="tinymce"><?php wp_dropdown_roles( ngg_get_role('NextGEN Use TinyMCE') ); ?></select></label></td>
  32. </tr>
  33. <tr valign="top">
  34. <th scope="row"><?php _e('Add gallery / Upload images', 'nggallery') ;?>:</th>
  35. <td><label for="add_gallery"><select name="add_gallery" id="add_gallery"><?php wp_dropdown_roles( ngg_get_role('NextGEN Upload images') ); ?></select></label></td>
  36. </tr>
  37. <tr valign="top">
  38. <th scope="row"><?php _e('Manage gallery', 'nggallery') ;?>:</th>
  39. <td><label for="manage_gallery"><select name="manage_gallery" id="manage_gallery"><?php wp_dropdown_roles( ngg_get_role('NextGEN Manage gallery') ); ?></select></label></td>
  40. </tr>
  41. <tr valign="top">
  42. <th scope="row"><?php _e('Manage others gallery', 'nggallery') ;?>:</th>
  43. <td><label for="manage_others"><select name="manage_others" id="manage_others"><?php wp_dropdown_roles( ngg_get_role('NextGEN Manage others gallery') ); ?></select></label></td>
  44. </tr>
  45. <tr valign="top">
  46. <th scope="row"><?php _e('Manage tags', 'nggallery') ;?>:</th>
  47. <td><label for="manage_tags"><select name="manage_tags" id="manage_tags"><?php wp_dropdown_roles( ngg_get_role('NextGEN Manage tags') ); ?></select></label></td>
  48. </tr>
  49. <tr valign="top">
  50. <th scope="row"><?php _e('Edit Album', 'nggallery') ;?>:</th>
  51. <td><label for="edit_album"><select name="edit_album" id="edit_album"><?php wp_dropdown_roles( ngg_get_role('NextGEN Edit album') ); ?></select></label></td>
  52. </tr>
  53. <tr valign="top">
  54. <th scope="row"><?php _e('Change style', 'nggallery') ;?>:</th>
  55. <td><label for="change_style"><select name="change_style" id="change_style"><?php wp_dropdown_roles( ngg_get_role('NextGEN Change style') ); ?></select></label></td>
  56. </tr>
  57. <tr valign="top">
  58. <th scope="row"><?php _e('Change options', 'nggallery') ;?>:</th>
  59. <td><label for="change_options"><select name="change_options" id="change_options"><?php wp_dropdown_roles( ngg_get_role('NextGEN Change options') ); ?></select></label></td>
  60. </tr>
  61. <tr valign="top">
  62. <th scope="row"><?php _e('NextGEN Attach Interface', 'nggallery') ;?>:</th>
  63. <td><label for="attach_interface"><select name="attach_interface" id="attach_interface"><?php wp_dropdown_roles( ngg_get_role('NextGEN Attach Interface') ); ?></select></label></td>
  64. </tr>
  65. </table>
  66. </div>
  67. <?php
  68. }
  69. function ngg_get_sorted_roles() {
  70. // This function returns all roles, sorted by user level (lowest to highest)
  71. global $wp_roles;
  72. $roles = $wp_roles->role_objects;
  73. $sorted = array();
  74. if( class_exists('RoleManager') ) {
  75. foreach( $roles as $role_key => $role_name ) {
  76. $role = get_role($role_key);
  77. if( empty($role) ) continue;
  78. $role_user_level = array_reduce(array_keys($role->capabilities), array('WP_User', 'level_reduction'), 0);
  79. $sorted[$role_user_level] = $role;
  80. }
  81. $sorted = array_values($sorted);
  82. } else {
  83. $role_order = array("subscriber", "contributor", "author", "editor", "administrator");
  84. foreach($role_order as $role_key) {
  85. $sorted[$role_key] = get_role($role_key);
  86. }
  87. }
  88. return $sorted;
  89. }
  90. function ngg_get_role($capability){
  91. // This function return the lowest roles which has the capabilities
  92. $check_order = ngg_get_sorted_roles();
  93. $args = array_slice(func_get_args(), 1);
  94. $args = array_merge(array($capability), $args);
  95. foreach ($check_order as $check_role) {
  96. if ( empty($check_role) )
  97. return false;
  98. if (call_user_func_array(array(&$check_role, 'has_cap'), $args))
  99. return $check_role->name;
  100. }
  101. return false;
  102. }
  103. function ngg_set_capability($lowest_role, $capability){
  104. // This function set or remove the $capability
  105. $check_order = ngg_get_sorted_roles();
  106. $add_capability = false;
  107. foreach ($check_order as $the_role) {
  108. $role = $the_role->name;
  109. if ( $lowest_role == $role )
  110. $add_capability = true;
  111. // If you rename the roles, then please use a role manager plugin
  112. if ( empty($the_role) )
  113. continue;
  114. $add_capability ? $the_role->add_cap($capability) : $the_role->remove_cap($capability) ;
  115. }
  116. }
  117. ?>