Commit 50968033 authored by Christian Wach's avatar Christian Wach
Browse files

Introduce capability level setting

parent f5201554
......@@ -32,6 +32,16 @@
</td>
</tr>
<tr>
<th scope="row">
<?php _e( 'User capability level', 'civicrm-network-groups' ); ?>
</th>
<td>
<input type="checkbox" class="settings-checkbox" id="cng_level" name="cng_level" value="1"<?php echo $level_checked; ?> /> <label class="civicrm_network_groups_settings_label" for="cng_level"><?php _e( 'Assign capability at lower level', 'civicrm-network-groups' ); ?></label>
<p class="description"><?php _e( 'When checked, users will only be able to grant/remove membership of groups that are lower in the precedence stack than the one they themselves hold. Uncheck to allow users to grant/remove membership of groups at the same level as themselves.', 'civicrm-network-groups' ); ?></p>
</td>
</tr>
</table>
<hr />
......
......@@ -342,6 +342,18 @@ class CiviCRM_Network_Groups_Admin {
$cng_title = esc_attr( $defaults['title'] );
}
// Get capability level.
$level = $this->setting_get( 'level' );
if ( empty( $level ) ) {
$level = $defaults['level'];
}
// Assign capability level checkbox status.
$level_checked = '';
if ( $level === 'lower' ) {
$level_checked = ' checked="checked"';
}
// Init final markup.
$groups_markup = '';
......@@ -472,6 +484,9 @@ class CiviCRM_Network_Groups_Admin {
// Set default title.
$settings['title'] = __( 'Network Groups', 'civicrm-network-groups' );
// Set default capability level ('lower' or 'same').
$settings['level'] = 'lower';
// Set default precedence if defined.
$settings['precedence'] = array();
if ( defined( 'CIVICRM_NETWORK_GROUPS_PRECEDENCE' ) ) {
......@@ -525,6 +540,13 @@ class CiviCRM_Network_Groups_Admin {
// Save title.
$this->setting_set( 'title', $title );
// Read level if present and set accordingly.
if ( ! empty( $_POST['cng_level'] ) AND trim( $_POST['cng_level'] ) == '1' ) {
$this->setting_set( 'level', 'lower' );
} else {
$this->setting_set( 'level', 'same' );
}
// Read precedence if present, fall back to default.
if ( ! empty( $_POST['cng_precedence'] ) AND is_array( $_POST['cng_precedence'] ) ) {
......
......@@ -89,6 +89,9 @@ class CiviCRM_Network_Groups_Hooks {
// Switch to original site.
add_filter( 'civicrm_network_groups_synced_groups', array( $this, 'switch_back' ), 50 );
// Filter capability level.
add_filter( 'civicrm_network_groups_edit_own', array( $this, 'capability_level' ), 10, 2 );
}
......@@ -106,6 +109,7 @@ class CiviCRM_Network_Groups_Hooks {
remove_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_defined_groups' ), 30 );
remove_filter( 'civicrm_network_groups_synced_groups', array( $this, 'filter_by_precedence' ), 40 );
remove_filter( 'civicrm_network_groups_synced_groups', array( $this, 'switch_back' ), 50 );
remove_filter( 'civicrm_network_groups_edit_own', array( $this, 'capability_level' ), 10, 2 );
}
......@@ -216,7 +220,7 @@ class CiviCRM_Network_Groups_Hooks {
// Network Admins don't get groups filtered by precedence.
if ( is_super_admin() ) {
return $civicrm_groups;
//return $civicrm_groups;
}
// Bail if none.
......@@ -347,4 +351,36 @@ class CiviCRM_Network_Groups_Hooks {
//##########################################################################
/**
* Switch capability level depending on plugin setting.
*
* @since 0.2
*
* @param bool $edit_own True if user can see same-level groups, false otherwise.
* @param WP_User $user The logged-in WordPress user object.
* @return bool $edit_own True if user can see same-level groups, false otherwise.
*/
public function capability_level( $edit_own, $user ) {
// Get capability level.
$level = $this->plugin->admin->setting_get( 'level' );
// Set return accordingly.
if ( $level == 'lower' ) {
$edit_own = false;
} else {
$edit_own = true;
}
// --<
return $edit_own;
}
} // Class ends.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment