UAM фильтрация

function uamIsUserInGroup($groupName='', $userId='') {
    if (empty($userId)) {
        $userId = get_current_user_id();
    }

    global $userAccessManager;

    if (isset($userAccessManager)) {
        $userGroupHandler = $userAccessManager->getUserGroupHandler();
        $userGroupsForUser = $userGroupHandler->getUserGroupsForObject(
            \UserAccessManager\Object\ObjectHandler::GENERAL_USER_OBJECT_TYPE,
            $userId
        );

        foreach($userGroupsForUser as $element) {
            if ($element->getName() == $groupName) {
                return true;
            }
        }
    } else {
        return false;
    }
}
function getPostUamGroupsId($id) {
    global $wpdb;

    $q = $wpdb->prepare( "SELECT group_id FROM wp_uam_accessgroup_to_object WHERE object_id = %s AND object_type = 'page'", $id );
	$query = $wpdb->get_results($q);
	
	$result_array = array();
	
	foreach ($query as $row) {
		array_push($result_array, $row->group_id);
	}
	
	return $result_array;
}
function getUserUamGroupsId($id) {
    $result_array = array();
	
	/*global $wpdb;

    $q = $wpdb->prepare( "SELECT group_id FROM wp_uam_accessgroup_to_object WHERE object_id = %s AND object_type = '_user_'", $id );
	$query = $wpdb->get_results($q);
	
	
	
	foreach ($query as $row) {
		array_push($result_array, $row->group_id);
	}*/
	
	global $userAccessManager;

	if (isset($userAccessManager)) {
		$userGroupHandler = $userAccessManager->getUserGroupHandler();
		$userGroupsForUser = $userGroupHandler->getUserGroupsForObject(
			\UserAccessManager\Object\ObjectHandler::GENERAL_USER_OBJECT_TYPE,
			$id
		);
	}
	
	foreach ($userGroupsForUser as $row) {
		array_push($result_array, $row->getId());
	}
	
	return $result_array;
	//return $result_array;
}
function findMatchesUamUserAndPost($user_groups_id, $post_groups_id){
	$uam_ok = false;
	foreach($user_groups_id as $user_group_id){
		foreach($post_groups_id as $post_group_id){
			if($user_group_id == $post_group_id) {
				return true;
			}
		}
	}
	return $uam_ok;
}
function findMatchesUamUserAndPostVoid($user_groups_id, $post_groups_id, $post){
	$uam_ok = false;
	if(count($user_groups_id) > 0){
		if(count($post_groups_id) > 0){
			$uam_ok = findMatchesUamUserAndPost($user_groups_id, $post_groups_id);
		} else {
			$uam_ok = false;
		}
	} else {
		$uam_ok = false;
	}
	
	if(!$uam_ok){
				
		$uam_ok = findUAMForParentsVoid($user_groups_id, $post_groups_id, $post);
	}
	return $uam_ok;
}
function findUAMForParentsVoid($user_groups_id, $post_groups_id, $post){
	return findUAMForParents($user_groups_id, $post_groups_id, $post);
}
function findUAMForParents($user_groups_id, $post_groups_id, $post){
	$uam_ok = false;
	
	if($post->post_parent !=0){
		$post_parent_ID = $post->post_parent;
		$post = get_post( $post_parent_ID );
		$post_groups_id = getPostUamGroupsId($post->ID);
		$uam_ok = findMatchesUamUserAndPost($user_groups_id, $post_groups_id);
		if(!$uam_ok){
			$uam_ok = findUAMForParents($user_groups_id, $post_groups_id, $post);
		}
	} 

	return $uam_ok;
}
function wpb_list_child_pages( $atts = array() ) {
    $user = wp_get_current_user();
	//$user = $user ? new WP_User( $user ) : wp_get_current_user();
	$user_roles = $user->roles;
	if(!is_user_logged_in()) return '';
	$atts = shortcode_atts( array(
        'id'   => 0,
        'slug' => '',
    ), $atts );

    if ( $atts['slug'] ) {
        global $wpdb;

        $q = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s", $atts['slug'] );
        $post_id = $wpdb->get_var( $q );

        if ( ! $post_id ) {
            return '';
        }
    } else {
        $post_id = absint( $atts['id'] );
    }
	$childpages = '';
	
    $post = get_post( $post_id ); // WP_Post on success.c
	$post_parent_ID = $post->post_parent ? $post->post_parent : $post->ID;
	//$post_parent_ID = $post->ID;
	$user_uam_groups_id = getUserUamGroupsId($user->ID);
	
	$children_pages_array = get_posts(array('numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'any', 'post_parent' => $post->ID, 'suppress_filters' => false));
	$ID_pages_rol_array = array();
	$children_pages_array_ids = array();
	$pages_array_roles = array();
    foreach ($children_pages_array as $child_page_obj) {
        //we get the rol that can access this page
        $page_role = get_post_meta($child_page_obj->ID, '_members_access_role', true);
		//array_push($pages_array_roles, $page_role);
		//do the current user rol have access to this page?
		//UAM
		$post_uam_groups_id = getPostUamGroupsId($child_page_obj->ID);
		//array_push($pages_array_roles, $post_uam_groups_id);
		$is_uam = findMatchesUamUserAndPostVoid($user_uam_groups_id, $post_uam_groups_id, $post);
		
		if($is_uam){
			array_push($ID_pages_rol_array, $child_page_obj->ID);
		} else {
			if( current_user_can('editor') || current_user_can('administrator') ){
				array_push($ID_pages_rol_array, $child_page_obj->ID);
			} else {
				if(count($post_uam_groups_id) < 1){
					array_push($ID_pages_rol_array, $child_page_obj->ID);
				} else {
					foreach ($user_roles as $user_role) {
						if ($page_role == $user_role) {
							//yes it does add to the IDs of pages that this rol can access
							array_push($ID_pages_rol_array, $child_page_obj->ID);
						}
					}
				}
			}
		}
    }
	
    if ( $post && is_post_type_hierarchical( $post->post_type ) ) {
		if(count($ID_pages_rol_array) > 0){
			$childpages = wp_list_pages( array(
				'authors' => '',
				'child_of' => $post->ID,
				'post_status' => 'publish',
				'title_li' => '',
				'depth'        => 2,
				'echo'     => 0,
			) );
		}
    }

    if ( $childpages ) {
        $childpages = '<ul>' . $childpages . '</ul>';
    }
	//if(count($pages_array_roles) < 1) array_push($pages_array_roles, 0);
	//$childpages = $childpages.'                 <br /> post='.implode(" | " , $pages_array_roles).' <br/> user='.implode(" | " , $user_uam_groups_id);
    return $childpages;
}