/** * Determines the set of post IDs that the current user cannot access * * If a page/post requires products a,b then the user will be granted access * to the content if they have bought either product a or b * * TODO: This is calculated on every page load, maybe use a cache? * * @return array Map of post ID => post ID */ function memberful_wp_user_disallowed_post_ids($user_id) { static $ids = array(); $user_id = (int) $user_id; $user_signed_in = $user_id !== 0; if (isset($ids[$user_id])) { return $ids[$user_id]; } $acl = get_option('memberful_acl', array()); $global_product_acl = isset($acl['product']) ? $acl['product'] : array(); $global_subscription_acl = isset($acl['subscription']) ? $acl['subscription'] : array(); $posts_for_any_registered_users = memberful_wp_get_all_posts_available_to_any_registered_user(); // Items the user has access to $user_products = memberful_wp_user_downloads($user_id); $user_subs = memberful_wp_user_plans_subscribed_to($user_id); if (!empty($user_subs)) { $user_subs = array_filter($user_subs, 'memberful_wp_filter_active_subscriptions'); } // Work out the set of posts the user is and isn't allowed to access $user_product_acl = memberful_wp_generate_user_specific_acl_from_global_acl($user_products, $global_product_acl); $user_subscription_acl = memberful_wp_generate_user_specific_acl_from_global_acl($user_subs, $global_subscription_acl); $user_allowed_posts = array_merge($user_product_acl['allowed'], $user_subscription_acl['allowed']); // At this point we dont know if the user is signed in, so assume they're not & that they can't access // "registered users only" posts $user_restricted_posts = array_merge($user_product_acl['restricted'], $user_subscription_acl['restricted'], $posts_for_any_registered_users); // Remove the set of posts a user can access from the set they can't. // If a post requires 1 of 2 subscriptions, and a member only has 1 of them // then the post will be in the restricted set and the allowed set $posts_user_is_not_allowed_to_access = array_diff($user_restricted_posts, $user_allowed_posts); if ($user_signed_in) { $posts_user_is_not_allowed_to_access = array_diff($posts_user_is_not_allowed_to_access, $posts_for_any_registered_users); } return $ids[$user_id] = empty($posts_user_is_not_allowed_to_access) ? array() : array_combine($posts_user_is_not_allowed_to_access, $posts_user_is_not_allowed_to_access); }
function memberful_wp_set_post_available_to_any_registered_users($post_id, $is_viewable_by_any_registered_users) { update_post_meta($post_id, 'memberful_available_to_any_registered_user', $is_viewable_by_any_registered_users); $globally_viewable_by_by_any_registered_users = memberful_wp_get_all_posts_available_to_any_registered_user(); if ($is_viewable_by_any_registered_users) { $globally_viewable_by_by_any_registered_users[$post_id] = $post_id; } else { unset($globally_viewable_by_by_any_registered_users[$post_id]); } update_option('memberful_posts_available_to_any_registered_user', $globally_viewable_by_by_any_registered_users); }