52 lines
1.2 KiB
PHP
52 lines
1.2 KiB
PHP
<?php
|
|
|
|
class Wiaas_Access_Management {
|
|
|
|
public static function init() {
|
|
|
|
add_action( 'save_post', array( __CLASS__, 'maybe_handle_product_access' ), 999, 2 );
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
|
|
$admin_access_group = Groups_Group::read_by_name('admin');
|
|
|
|
$access_group_ids = array();
|
|
|
|
if ($admin_access_group) {
|
|
$access_group_ids[] = $admin_access_group->group_id;
|
|
}
|
|
|
|
// allow commercial lead to see published bundle products
|
|
$cl_access_group = Groups_Group::read_by_name('commercial_lead');
|
|
|
|
if ($product->get_type() === 'bundle' &&
|
|
$product->get_status() === 'publish' &&
|
|
$cl_access_group) {
|
|
$access_group_ids[] = $cl_access_group->group_id;
|
|
}
|
|
|
|
Groups_Post_Access::update(
|
|
array(
|
|
'post_id' => $product->get_id(),
|
|
'groups_read' => $access_group_ids
|
|
) );
|
|
|
|
}
|
|
}
|
|
|
|
Wiaas_Access_Management::init();
|