handle user roles by organization

This commit is contained in:
Almira Krdzic
2018-10-11 04:16:43 +02:00
parent 9a8e05e341
commit 84cd11f9c9
29 changed files with 1341 additions and 124 deletions

View File

@@ -32,13 +32,24 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
parent::hooks();
add_action('user_new_form', array( $this, 'show_organizations_selection' ));
add_action('user_register', array( $this, 'save_terms_for_user' ));
add_action( 'created_' . self::TAXONOMY_NAME, array( __CLASS__, 'on_organization_added' ));
add_action( 'pre_delete_term', array( __CLASS__, 'on_taxonomy_term_will_be_deleted' ), 10, 2);
add_action('set_object_terms', array( __CLASS__, 'on_taxonomy_term_assigned' ), 10, 4);
add_action('deleted_term_relationships', array( __CLASS__, 'on_taxonomy_term_unassigned' ), 10, 3);
// Remove bulk editor for organizations on users list
remove_filter( 'admin_notices', array( $this, 'bulk_notice'));
remove_filter( 'bulk_actions-users', array( $this, 'bulk_actions'));
remove_filter( 'bulk_actions-users', array( $this, 'bulk_actions_sort'));
remove_action( 'handle_bulk_actions-users', array( $this, 'handle_bulk_actions'));
// remove default organization info from profiles
// it will be handled by custom fields
remove_action( 'show_user_profile', array( $this, 'edit_user_relationships' ), 99);
remove_action( 'edit_user_profile', array( $this, 'edit_user_relationships' ), 99 );
}
// hooks functions
@@ -78,7 +89,7 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
if ($taxonomy === self::TAXONOMY_NAME) {
$user_id = $object_id;
$organization_id = $tt_ids[0];
add_user_meta($user_id, 'organization_id', $organization_id, true);
self::_add_user_to_access_group($user_id, $organization_id);
}
}
@@ -95,7 +106,7 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
if ($taxonomy === self::TAXONOMY_NAME) {
$user_id = $object_id;
$organization_id = $tt_ids[0];
delete_user_meta($user_id, 'organization_id');
self::_remove_user_from_organization_access_groups($user_id, $organization_id);
}
}
@@ -106,13 +117,13 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
* @param null $user_id
* @return mixed
*/
public static function get_user_organization($user_id = null) {
public static function get_user_organization_id($user_id = null) {
if (!isset($user_id)) {
$user = wp_get_current_user();
$user_id = $user->ID;
$user_id = get_current_user_id();
}
$terms = wp_get_object_terms($user_id, self::TAXONOMY_NAME);
return $terms[0];
$organization_id = get_user_meta($user_id, '_wiaas_organization_id', true);
return empty($organization_id) ? null : (int) $organization_id;
}
/**
@@ -124,8 +135,8 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
* @param $user_id
*/
public static function assign_post_to_user_organization($post_id, $user_id) {
$organization = self::get_user_organization($user_id);
self::_assign_post_to_organization($post_id, $organization->term_id);
$organization_id = self::get_user_organization_id($user_id);
self::_assign_post_to_organization($post_id, $organization_id);
}
@@ -240,73 +251,4 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
}
}
}
/**
* Show organizations selection on new user form
*/
function show_organizations_selection() {
$terms = get_terms( array(
'taxonomy' => self::TAXONOMY_NAME,
'hide_empty' => false,
) );
$taxonomy = get_taxonomy(self::TAXONOMY_NAME);
$this->table_contents(null, $taxonomy, $terms);
}
function edit_user_relationships($user = false) {
$tax = get_taxonomy( $this->taxonomy );
// Get the terms of the taxonomy.
$terms = get_terms( $this->taxonomy, array(
'hide_empty' => false
) );
$this->table_contents( $user, $tax, $terms );
}
function table_contents( $user, $tax, $terms ) {
$active_organization_id = -1;
if ($user) {
$active_organization = self::get_user_organization($user->ID);
$active_organization_id = $active_organization ? $active_organization->term_id : -1;
}
?>
<table class="form-table">
<tbody>
<tr class="form-field">
<th scope="row">
<label for="<?php echo esc_attr( $this->taxonomy ); ?>[]">
<?php echo esc_html( $tax->labels->singular_name ); ?>
</label>
</th>
<td>
<select name="<?php echo esc_attr( $this->taxonomy ); ?>[]" id="<?php echo esc_attr( $this->taxonomy ); ?>">
<?php
foreach ( $terms as $term ) :
$selected = $active_organization_id === $term->term_id;
?>
<option
value="<?php echo esc_attr( $term->slug ); ?>"
<?php selected( $selected ); ?>
>
<?php echo esc_attr( $term->name ); ?>
</option>
<?php
endforeach;
?>
</select>
<?php
wp_nonce_field( $this->taxonomy, $this->get_nonce_key() );
?>
</td>
</tr>
</tbody>
</table>
<?php
}
private function get_nonce_key() {
return "wp_user_taxonomy_{$this->taxonomy}";
}
}