Files
old-new-wiaas/backend/app/plugins/wiaas/includes/class-wiaas-access-management.php
2018-10-17 13:44:15 +02:00

50 lines
1.0 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);
$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') {
$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
)
);
}
}
}
Wiaas_Access_Management::init();