/** * Extend user capabilities with Groups user capabilities. * * @param array $allcaps the capabilities the user has * @param array $caps the requested capabilities * @param array $args capability context which can provide the requested capability as $args[0], the user ID as $args[1] and the related object's ID as $args[2] */ public static function user_has_cap($allcaps, $caps, $args) { $user_id = isset($args[1]) ? $args[1] : null; $groups_user = new Groups_User($user_id); if (is_array($caps)) { // we need to deactivate this because invoking $groups_user->can() // would trigger this same function and we would end up // in an infinite loop remove_filter('user_has_cap', array(__CLASS__, 'user_has_cap'), 10, 3); foreach ($caps as $cap) { if ($groups_user->can($cap)) { $allcaps[$cap] = true; } } add_filter('user_has_cap', array(__CLASS__, 'user_has_cap'), 10, 3); } return $allcaps; }
/** * Returns true if the user has any of the capabilities that grant access to the post. * * @param int $post_id post id * @param int $user_id user id or null for current user * @return boolean true if user can read the post */ public static function user_can_read_post($post_id, $user_id = null) { $result = false; if (!empty($post_id)) { if ($user_id === null) { $user_id = get_current_user_id(); } $cached = Groups_Cache::get(self::CAN_READ_POST . '_' . $user_id . '_' . $post_id, self::CACHE_GROUP); if ($cached !== null) { $result = $cached->value; unset($cached); } else { $groups_user = new Groups_User($user_id); $read_caps = self::get_read_post_capabilities($post_id); if (!empty($read_caps)) { foreach ($read_caps as $read_cap) { if ($groups_user->can($read_cap)) { $result = true; break; } } } else { $result = true; } $result = apply_filters('groups_post_access_user_can_read_post', $result, $post_id, $user_id); Groups_Cache::set(self::CAN_READ_POST . '_' . $user_id . '_' . $post_id, $result, self::CACHE_GROUP); } } return $result; }
/** * Takes one attribute "capability" that must be a valid capability label, * or a comma-separaed list of those. * The content is shown if the current user has none of the capabilities. * * @param array $atts attributes * @param string $content content to render */ public static function groups_can_not($atts, $content = null) { $output = ""; $options = shortcode_atts(array("capability" => ""), $atts); if ($content !== null) { $groups_user = new Groups_User(get_current_user_id()); $capability = $options['capability']; $capabilities = array_map('trim', explode(',', $capability)); $show_content = true; foreach ($capabilities as $capability) { if ($groups_user->can($capability)) { $show_content = false; break; } } if ($show_content) { remove_shortcode('groups_can_not'); $content = do_shortcode($content); add_shortcode('groups_can_not', array(__CLASS__, 'groups_can_not')); $output = $content; } } return $output; }
/** * @return array of valid read capabilities for the current or given user */ public static function get_valid_read_caps_for_user($user_id = null) { $result = array(); $user = new Groups_User($user_id === null ? get_current_user_id() : $user_id); $valid_read_caps = Groups_Options::get_option(Groups_Post_Access::READ_POST_CAPABILITIES, array(Groups_Post_Access::READ_POST_CAPABILITY)); foreach ($valid_read_caps as $valid_read_cap) { if ($capability = Groups_Capability::read_by_capability($valid_read_cap)) { if ($user->can($capability->capability)) { $result[] = $valid_read_cap; } } } return $result; }
get_header(); // // // // check if user is logged in // // // if (is_user_logged_in()) { ?> <?php // // // // get user id // // // $groups_user = new Groups_User(get_current_user_id()); // // // // if users has access to My Consumables group // // // if ($groups_user->can('My Consumables')) { ?> <div class="page-title"> <h1>Hello <?php echo $current_user->first_name; ?> ,</h1> <?php $my_products = array(); $my_groups = array(); query_posts(array('post_type' => 'product', 'posts_per_page' => '-1')); while (have_posts()) { the_post(); get_page_group_machines(); foreach ($page_machines as $machine) {
/** * Takes one attribute "capability" that must be a valid capability label. * The content is shown if the current user does NOT have the capability. * * @param array $atts attributes * @param string $content content to render */ public static function groups_can_not($atts, $content = null) { $output = ""; $options = shortcode_atts(array("capability" => ""), $atts); if ($content !== null) { $groups_user = new Groups_User(get_current_user_id()); $capability = $options['capability']; if (!$groups_user->can($capability)) { remove_shortcode('groups_can_not'); $content = do_shortcode($content); add_shortcode('groups_can_not', array(__CLASS__, 'groups_can_not')); $output = $content; } } return $output; }
/** * Returns true if the user has any of the capabilities that grant access to the post. * * @param int $post_id post id * @param int $user_id user id or null for current user * @return boolean true if user can read the post */ public static function user_can_read_post($post_id, $user_id = null) { $result = false; if (!empty($post_id)) { if ($user_id === null) { $user_id = get_current_user_id(); } $groups_user = new Groups_User($user_id); $read_caps = self::get_read_post_capabilities($post_id); if (!empty($read_caps)) { foreach ($read_caps as $read_cap) { if ($groups_user->can($read_cap)) { $result = true; break; } } } else { $result = true; } } return $result; }