Add hierarhical managment

This commit is contained in:
Almira Krdzic
2018-07-09 12:34:06 +02:00
parent 06982f22e5
commit 77cb549a3f
9320 changed files with 436076 additions and 4793 deletions

View File

@@ -0,0 +1,49 @@
<?php
if ( !class_exists('Order_Management')) :
class Order_Management {
const ORDER_TAXONOMY_NAME = 'order-organization';
const ORDER_TAXONOMY_SLUG = 'order/organization';
const ORDER_OBJECT_TYPE = 'shop_order';
public static function init() {
add_action('woocommerce_new_order', array( __CLASS__, 'assign_order_to_organization' ));
add_filter('woocommerce_rest_check_permissions', array( __CLASS__, 'check_order_access'), 10, 4);
}
/**
* Checks if current user has access to requested order/{orderId} via woocommerce REST API.
* Endpoint `/orders` is filtered correctly by groups, but endpoint `/orders/{orderId}` will return order even if
* user does not have access to it.
* Groups has general support for this using rest_prepare_{$post_type} but woocommerce api does not
* use this filter anymore. So we will just call the same function just with woocommerce filter.
*
* @param $permission
* @param $context
* @param $object_id
* @param $post_type
* @return bool
*/
public static function check_order_access($permission, $context, $object_id, $post_type) {
if ($post_type === self::ORDER_OBJECT_TYPE && $object_id !== 0) {
return Groups_Post_Access::user_can_read_post($object_id);
}
return $permission;
}
/**
* Assignees order to corresponding user organization when order is created.
*
* @param $order_id
*/
public static function assign_order_to_organization($order_id) {
$user = wp_get_current_user();
User_Organizations_Directory::assign_post_to_user_organization($order_id, $user->ID);
}
}
Order_Management::init();
endif;

View File

@@ -0,0 +1,224 @@
<?php
if ( !class_exists('User_Organizations_Directory')) :
class User_Organizations_Directory
{
const TAXONOMY_NAME = 'user-organization';
const TAXONOMY_SLUG = 'users/organization';
public static function init()
{
// create taxonomy that will be used as organizations directory
new User_Organization_Taxonomy();
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);
}
// hooks functions
/**
* Creates corresponding access group for newly created organizational term
*
* @param $organization_id id of the organization term
*/
public static function on_organization_added($organization_id) {
self::_create_organization_access_group($organization_id);
}
/**
* Removes corresponding acces group when organization term is deleted
*
* @param $term_id - term id that will be deleted
* @param $taxonomy - taxonomy to which term belongs (in our case `user-organizations`)
*/
public static function on_taxonomy_term_will_be_deleted($term_id, $taxonomy) {
if ($taxonomy === self::TAXONOMY_NAME) {
$organization_id = $term_id;
self::_remove_organization_access_group($organization_id);
}
}
/**
* Adds user to corresponding access groups when he is assigned to organization.
* User will also be added to child organizations access groups.
*
* @param $object_id - id of object to which term is assigned (in our case $user_id)
* @param $terms - assigned terms (in our case $organizations)
* @param $tt_ids - assigned terms ids (in our case $organizations_ids)
* @param $taxonomy - taxonomy to which term belongs (in our case `user-organizations`)
*/
public static function on_taxonomy_term_assigned($object_id, $terms, $tt_ids, $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);
}
}
/**
* Removes user from corresponding access groups when he is removed from organization.
* User will also be removed from child organizations access groups.
*
* @param $object_id - id of object to which term is assigned (in our case $user_id)
* @param $tt_ids - assigned terms ids (in our case $organizations_ids)
* @param $taxonomy - taxonomy to which term belongs (in our case `user-organizations`)
*/
public static function on_taxonomy_term_unassigned($object_id, $tt_ids, $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);
}
}
/**
* Retrieves user organization based on user id
*
* @param null $user_id
* @return mixed
*/
public static function get_user_organization($user_id = null) {
if (!isset($user_id)) {
$user = wp_get_current_user();
$user_id = $user->ID;
}
$terms = wp_get_object_terms($user_id, self::TAXONOMY_NAME);
return $terms[0];
}
/**
* Assignees post to user organization. Post will be assigned to corresponding access groups.
* If user organization has parent organizations, staff from parent organizations will also be able
* to access order.
*
* @param $post_id - custom post id (product, order, ...)
* @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);
}
// private helper functions
/**
* Retrieves organization object based organization id
*
* @param $organization_id
* @return mixed
*/
private static function _get_organization_access_group_id($organization_id) {
return get_term_meta($organization_id, 'group_id', true);
}
/**
* Retrieves all access groups ids for organization. This includes corresponding access group
* for provided organization and also access groups for all of its child organizations.
*
* @param $organization_id
* @return array
*/
private static function _get_organization_all_access_groups_ids($organization_id) {
$access_groups_ids = array();
$access_groups_ids[] = self::_get_organization_access_group_id($organization_id);
$organization_departments_ids = self::_get_organization_departments_ids($organization_id);
foreach ($organization_departments_ids as $organization_department_id) {
$access_groups_ids[] = self::_get_organization_access_group_id($organization_department_id);
}
return $access_groups_ids;
}
/**
* Retrieves all departments of organization
*
* @param $organization_id
* @return array|WP_Error
*/
private static function _get_organization_departments_ids($organization_id) {
return get_term_children($organization_id, self::TAXONOMY_NAME);
}
/**
* Assign custom post to corresponding organizational acccess group.
*
* @param $post_id
* @param $organization_id
*/
private static function _assign_post_to_organization($post_id, $organization_id) {
if (class_exists('Groups_Post_Access')) {
$access_group_id = self::_get_organization_access_group_id($organization_id);
Groups_Post_Access::update( array( 'post_id' => $post_id, 'groups_read' => [$access_group_id] ) );
}
}
/**
* Create corresponding access group for organization
*
* @param $organization_id
*/
private static function _create_organization_access_group($organization_id) {
if (class_exists('Groups_Group')) {
$organization = get_term_by('id', $organization_id, self::TAXONOMY_NAME);
$access_group_id = Groups_Group::create(array(
'name' => $organization->name,
));
add_term_meta($organization_id, 'group_id', $access_group_id);
}
}
/**
* Remove corresponding access group for organization
*
* @param $organization_id
*/
private static function _remove_organization_access_group($organization_id) {
if (class_exists('Groups_Group')) {
$access_group_id = self::_get_organization_access_group_id($organization_id);
Groups_Group::delete($access_group_id);
}
}
/**
* Add user to all access groups found in provided organization.
*
* @param $user_id
* @param $organization_id
*/
private static function _add_user_to_access_group($user_id, $organization_id) {
if (class_exists('Groups_User_Group')) {
$access_groups_ids = self::_get_organization_all_access_groups_ids($organization_id);
foreach ($access_groups_ids as $access_group_id) {
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $access_group_id ) );
}
}
}
/**
* Remove user from all access groups found in provided organization,
*
* @param $user_id
* @param $organization_id
*/
private static function _remove_user_from_organization_access_groups($user_id, $organization_id) {
if (class_exists('Groups_User_Group')) {
$access_groups_ids = self::_get_organization_all_access_groups_ids($organization_id);
foreach ($access_groups_ids as $access_group_id) {
Groups_User_Group::delete($user_id, $access_group_id);
}
}
}
}
User_Organizations_Directory::init();
endif;

View File

@@ -0,0 +1,49 @@
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'User_Organization_Taxonomy' ) ) :
class User_Organization_Taxonomy extends WP_User_Taxonomy {
const TAXONOMY_NAME = 'user-organization';
const TAXONOMY_SLUG = 'users/organization';
public function __construct()
{
$args = array(
'singular' => __('Organization', 'wp-user-groups'),
'plural' => __('Organizations', 'wp-user-groups'),
'exclusive' => true,
'public' => true,
'show_in_rest' => true,
'rest_base' => 'organization'
);
$labels = array();
$caps = array();
parent::__construct(self::TAXONOMY_NAME, self::TAXONOMY_SLUG, $args, $labels, $caps);
}
/**
* Add organization specific hooks
*/
function hooks() {
parent::hooks();
add_action('user_new_form', array( $this, 'show_organizations_selection' ));
add_action('user_register', array( $this, 'save_terms_for_user' ));
}
/**
* Show organizations selection without $user info
*/
function show_organizations_selection() {
$terms = get_terms( array(
'taxonomy' => self::TAXONOMY_NAME,
) );
$taxonomy = get_taxonomy(self::TAXONOMY_NAME);
$this->table_contents(null, $taxonomy, $terms);
}
}
endif;

View File

@@ -0,0 +1,42 @@
<?php
/**
* Plugin Name: User Organizations
* Plugin URI: https://wordpress.org/plugins/wp-user-groups/
* Author: Saburly
* Description: Hierarchical organization managment
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
function load() {
if (class_exists('WP_User_Taxonomy')) {
require_once ('includes/class_user_organization_taxonomy.php');
require_once ('includes/class-user-organizations-directory.php');
require_once('includes/class-order-managment.php');
apply_filters('user_organizations_loaded');
}
}
function remove_default_user_groups() {
remove_action( 'init', 'wp_register_default_user_group_taxonomy' );
remove_action( 'init', 'wp_register_default_user_type_taxonomy' );
}
function fetch_user_organization() {
return User_Organizations_Directory::get_user_organization();
}
add_action('init', 'load');
add_action('plugins_loaded', 'remove_default_user_groups', 30);
add_action('user_organizations_loaded', function() {
register_rest_route('wiaas/v1', '/organization', array(
'methods' => 'GET',
'permission_callback' => 'check_permission',
'callback' => 'fetch_user_organization',
));
});