135 lines
4.0 KiB
PHP
135 lines
4.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class Wiaas_Access_Management
|
|
*/
|
|
class Wiaas_Access_Management {
|
|
|
|
/**
|
|
* Handles organization and role based access management to wiaas objects (orders, products)
|
|
*
|
|
* Using Groups Access for achieve this
|
|
*
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
add_action( 'save_post', array( __CLASS__, 'maybe_handle_product_access' ), 999, 2 );
|
|
|
|
add_action('woocommerce_checkout_order_processed', array( __CLASS__, 'assign_order_to_customer_organization' ));
|
|
add_action('woocommerce_checkout_order_processed', array( __CLASS__, 'assign_order_to_commercial_lead_organization' ));
|
|
add_action('woocommerce_checkout_order_processed', array( __CLASS__, 'assign_order_to_supplier_organizations'));
|
|
add_action('wiaas_order_item_installation_assigned', array(__CLASS__, 'assign_order_to_installation_organization'), 10, 3);
|
|
}
|
|
|
|
/**
|
|
* Automatize access control for product and packages
|
|
*
|
|
* @param int $post_id
|
|
* @param WP_Post $post
|
|
*/
|
|
public static function maybe_handle_product_access($post_id, $post) {
|
|
|
|
// $post_id and $post are required
|
|
if ( empty( $post_id ) || empty( $post ) || $post->post_type !== 'product') {
|
|
return;
|
|
}
|
|
|
|
$product = wc_get_product($post_id);
|
|
|
|
$access_group = null;
|
|
|
|
// if product is not bundle or it not completed set it visible only for admin
|
|
if ($product->get_type() !== 'bundle' ||
|
|
$product->get_status() !== 'publish' ||
|
|
empty(Wiaas_Package_Pricing::get_package_prices($product))) {
|
|
|
|
$access_group = Groups_Group::read_by_name('admin');
|
|
|
|
} else {
|
|
|
|
$access_group = Groups_Group::read_by_name('Registered');
|
|
}
|
|
|
|
if ($access_group) {
|
|
Groups_Post_Access::update(
|
|
array(
|
|
'post_id' => $product->get_id(),
|
|
'groups_read' => $access_group->group_id
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Assignees order to corresponding user organization when order is created.
|
|
*
|
|
* @param int $order_id
|
|
*/
|
|
public static function assign_order_to_customer_organization($order_id) {
|
|
// assign order to customer organization
|
|
$customer_id = wiaas_get_current_user_organization_id();
|
|
Wiaas_User_Organization::assign_post_to_organization($order_id, $customer_id);
|
|
}
|
|
|
|
/**
|
|
* Assign order to commercial lead organization
|
|
*
|
|
* @param int $order_id
|
|
*/
|
|
public static function assign_order_to_commercial_lead_organization($order_id) {
|
|
$order = wc_get_order($order_id);
|
|
|
|
$commercial_lead_id = absint($order->get_meta('_wiaas_commercial_lead_id', true));
|
|
if ($commercial_lead_id) {
|
|
Wiaas_User_Organization::assign_post_to_organization($order_id, $commercial_lead_id);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Assignees order to supplier organizations extracted from ordered items except installation
|
|
*
|
|
* Order will be assigned to corresponding installation company during delivery process
|
|
*
|
|
* @param int $order_id
|
|
*/
|
|
public static function assign_order_to_supplier_organizations($order_id) {
|
|
|
|
$order = wc_get_order($order_id);
|
|
|
|
$order_items = $order->get_items('line_item');
|
|
|
|
foreach ($order_items as $key => $order_item) {
|
|
|
|
$supplier_organisation_id = $order_item->get_meta('_wiaas_supplier_organization_id');
|
|
|
|
if (! empty($supplier_organisation_id) && $order_item->get_meta('_wiaas_category') !== 'installation') {
|
|
|
|
Wiaas_User_Organization::assign_post_to_organization($order_id, $supplier_organisation_id);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Assign order to installation organization that handles installation of corresponding ordered bundle item
|
|
*
|
|
* @param WC_Order $order
|
|
* @param WC_Order_Item $bundle_item
|
|
* @param WC_Order_Item $installation_item
|
|
*/
|
|
public static function assign_order_to_installation_organization($order, $bundle_item, $installation_item) {
|
|
|
|
$supplier_organisation_id = $installation_item->get_meta('_wiaas_supplier_organization_id');
|
|
|
|
if (! empty($supplier_organisation_id) ) {
|
|
|
|
Wiaas_User_Organization::assign_post_to_organization($order->get_id(), $supplier_organisation_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
Wiaas_Access_Management::init();
|