Changeset 5231
- Timestamp:
- 04/01/2017 02:24:41 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/rosetta/inc/site/class-locale-support.php
r4028 r5231 3 3 4 4 use WP_Site; 5 use WP_User; 5 6 6 7 class Locale_Support implements Site { … … 23 24 * Tests whether this site manager is eligible for a site. 24 25 * 25 * @param WP_Site $site The site object. 26 * 26 * @param \WP_Site $site The site object. 27 27 * @return bool True if site is eligible, false otherwise. 28 28 */ … … 39 39 */ 40 40 public function register_events() { 41 // TODO: Implement register_events() method. 41 add_action( 'bbp_loaded', [ $this, 'initialize_bbpress_customizations' ] ); 42 } 43 44 /** 45 * Initializes customizations for bbPress. 46 */ 47 public function initialize_bbpress_customizations() { 48 if ( is_admin() ) { 49 add_action( 'bbp_init', [ $this, 'set_minimum_capability_for_bbpress' ], 11 ); // after add_action( 'bbp_init', 'bbp_admin' ); 50 } 51 52 add_filter( 'user_has_cap', [ $this, 'extend_bbpress_roles' ], 10, 4 ); 53 add_filter( 'editable_roles', [ $this, 'limit_editable_roles' ] ); 54 add_action( 'set_user_role', [ $this, 'restore_bbpress_role_on_bulk_edit' ], 10, 3 ); 55 } 56 57 /** 58 * Only allow super admins to access tools and settings of bbPress. 59 * Default is 'keep_gate' for any keymaster. 60 */ 61 public function set_minimum_capability_for_bbpress() { 62 if ( ! is_super_admin() ) { 63 bbpress()->admin->minimum_capability = 'do_not_allow'; 64 } 65 } 66 67 /** 68 * Restores the bbPress role if an user has been promoted via bulk action. 69 * 70 * @link https://bbpress.trac.wordpress.org/ticket/2597 71 * @link https://core.trac.wordpress.org/ticket/17924 72 * 73 * @param int $user_id The user ID. 74 * @param string $role The new role. 75 * @param array $old_roles An array of the user's previous roles. 76 */ 77 public function restore_bbpress_role_on_bulk_edit( $user_id, $role, $old_roles ) { 78 $bbp_roles = array_keys( bbp_get_dynamic_roles() ); 79 80 // Do nothing if the new role is a bbPress role or if the user had no bbPress role. 81 if ( in_array( $role, $bbp_roles ) || ! array_intersect( $old_roles, $bbp_roles ) ) { 82 return; 83 } 84 85 $user = new WP_User( $user_id ); 86 foreach ( $old_roles as $old_role ) { 87 // Add only bbPress roles. 88 if ( ! in_array( $old_role, $bbp_roles ) ) { 89 continue; 90 } 91 $user->add_role( $old_role ); 92 } 93 } 94 95 /** 96 * Extends capabilities for keymasters and moderators to allow access to the 97 * admin and to manage users and pages. 98 * 99 * @param array $allcaps An array of all the user's capabilities. 100 * @param array $caps Actual capabilities for meta capability. 101 * @param array $args Optional parameters passed to has_cap(), typically object ID. 102 * @param \WP_User $user The user object. 103 * @return array Extended capabilities 104 */ 105 public function extend_bbpress_roles( $allcaps, $caps, $args, $user ) { 106 if ( in_array( bbp_get_keymaster_role(), $user->roles, true ) ) { 107 $extra_caps = [ 108 // Access dashboard. 109 'read' => true, 110 // Manage users. 111 'list_users' => true, 112 'promote_users' => true, 113 'remove_users' => true, 114 // Manage pages. 115 'edit_pages' => true, 116 'edit_others_pages' => true, 117 'edit_published_pages' => true, 118 'publish_pages' => true, 119 'delete_pages' => true, 120 'delete_others_pages' => true, 121 'delete_published_pages' => true, 122 'delete_private_pages' => true, 123 'edit_private_pages' => true, 124 'read_private_pages' => true, 125 ]; 126 127 $allcaps = array_merge( $allcaps, $extra_caps ); 128 } elseif ( in_array( bbp_get_moderator_role(), $user->roles, true ) ) { 129 $extra_caps = [ 130 // Access dashboard. 131 'read' => true, 132 // Manage pages. 133 'read_private_pages' => true, 134 ]; 135 136 $allcaps = array_merge( $allcaps, $extra_caps ); 137 } 138 139 return $allcaps; 140 } 141 142 /** 143 * Filters the list of editable roles. 144 * 145 * Non-super admins can promote subscribers and administrators, 146 * others only subscribers. 147 * 148 * @param array $roles List of roles. 149 * @return array Filtered ist of roles. 150 */ 151 public function limit_editable_roles( $roles ) { 152 if ( ! is_super_admin() ) { 153 return [ 'subscriber' => $roles['subscriber'] ]; 154 } 155 156 $roles = array_intersect_key( $roles, array_flip( [ 'subscriber', 'administrator' ] ) ); 157 158 return $roles; 42 159 } 43 160 }
Note: See TracChangeset
for help on using the changeset viewer.