/** * Process WooCommerce Memberships data to see if a member has any active * memberships or not. Cache the result in the Akses specific data base. * * @param int $userId User ID. */ private static function process_wc_membership_member_status($userId) { $is_active = false; if (function_exists('wc_memberships')) { // check if the member has an active membership for any plan foreach (wc_memberships_get_membership_plans() as $plan) { if (wc_memberships_is_user_active_member($userId, $plan)) { $is_active = true; break; } } } else { error_log('Can not process membership statuses: WooCommerce Memberships is not installed.'); } Akses_Membership::set_member_active($userId, $is_active); }
/** * Replaces the Customer role with the Site Member role when a membership is reactivated * * @param \WC_User_Membership $user_membership the user's membership * @param string $old_status the previous membership status * @param string $new_status the new membership status */ function sv_reactivate_member_role($user_membership, $old_status, $new_status) { $user_id = $user_membership->user_id; $wp_user = get_userdata($user_id); $roles = $wp_user->roles; // Bail if the member currently has the Site Member role, doesn't have the customer role, or is inactive if (in_array('site_member', $roles) || !in_array('customer', $roles) || !wc_memberships_is_user_active_member($user_id, $user_membership->plan_id)) { return; } $wp_user->remove_role('customer'); $wp_user->add_role('site_member'); }
/** * Check if product purchasing is retricted * Example: Add a restricted product notice * Display a top notice to non-members for members-only products */ function sv_wc_memberships_members_only_product_notice() { // bail if Memberships isn't active if (!function_exists('wc_memberships')) { return; } $user_id = get_current_user_id(); // Bail if the user is already a silver or gold member if (wc_memberships_is_user_active_member($user_id, 'silver') || wc_memberships_is_user_active_member($user_id, 'gold')) { return; } // Add our top notice if purchasing is restricted if (wc_memberships_is_product_purchasing_restricted()) { wc_print_notice('This is a preview! Only silver or gold members can purchase this product.', 'notice'); } }
</header><!-- #masthead --> <?php /** * @hooked storefront_header_widget_region - 10 */ do_action('storefront_before_content'); ?> <div id="content" class="site-content" tabindex="-1"> <?php $user_id = get_current_user_id(); $this_year = date('Y'); $seasonal_membership_name = $this_year . '-seasonal-membership'; $is_annual_member = wc_memberships_is_user_active_member($user_id, 'empire-tri-membership'); $is_seasonal_member = wc_memberships_is_user_active_member($user_id, $seasonal_membership_name); if ($is_annual_member || $is_seasonal_member) { do_action('etc_fft_banner'); } else { do_action('etc_call_to_join'); } ?> <div id='featured-carousel' class="col-full"> <div id='featured-carousel-content' class='owl-carousel'> <?php do_action('empire_homepage_featured'); ?> </div><!-- #featured-carousel-content -->
function user_is_empire_member() { $user_id = get_current_user_id(); $this_year = date('Y'); $seasonal_membership_name = $this_year . '-seasonal-membership'; $is_annual_member = wc_memberships_is_user_active_member($user_id, 'empire-tri-membership'); $is_seasonal_member = wc_memberships_is_user_active_member($user_id, $seasonal_membership_name); if ($is_annual_member || $is_seasonal_member) { return true; } }
/** * Grant a user access to this plan from a purchase * * @since 1.0.0 * @param int $user_id User ID * @param int $product_id Product ID * @param int $order_id Order ID * @return int|null New/Existing User Membership ID or null on failure */ public function grant_access_from_purchase($user_id, $product_id, $order_id) { $user_membership_id = null; $action = 'create'; // Check if user is perhaps a member, but membership is expired/cancelled if (wc_memberships_is_user_member($user_id, $this->get_id())) { $user_membership = wc_memberships_get_user_membership($user_id, $this->get_id()); $user_membership_id = $user_membership->get_id(); // Do not allow the same order to renew or reactivate the membership. This // prevents admins changing order statuses from extending/reactivating the // membership. $order_ids = get_post_meta($user_membership_id, '_order_id'); if (!empty($order_ids) && in_array($order_id, $order_ids)) { return null; } // Otherwise... continue as usual $action = 'reactivate'; if (wc_memberships_is_user_active_member($user_id, $this->get_id())) { /** * Filter whether an already active membership will be renewed * * @since 1.0.0 * @param bool $renew * @param WC_Memberships_Membership_Plan $plan * @param array $args */ $renew_membership = apply_filters('wc_memberships_renew_membership', (bool) $this->get_access_length_amount(), $this, array('user_id' => $user_id, 'product_id' => $product_id, 'order_id' => $order_id)); if (!$renew_membership) { return null; } $action = 'renew'; } } // Create/update the user membership $user_membership = wc_memberships_create_user_membership(array('user_membership_id' => $user_membership_id, 'user_id' => $user_id, 'product_id' => $product_id, 'order_id' => $order_id, 'plan_id' => $this->get_id()), $action); // Add membership note $product = wc_get_product($product_id); $order = wc_get_order($order_id); $user_membership->add_note(sprintf(__('Membership access granted from purchasing %s (Order %s)'), $product->get_title(), $order->get_order_number())); /** * Fires after a user has been granted membership access from a purchase * * @since 1.0.0 * @param WC_Memberships_Membership_Plan $membership_plan The plan that user was granted access to * @param array $args */ do_action('wc_memberships_grant_membership_access_from_purchase', $this, array('user_id' => $user_id, 'product_id' => $product_id, 'order_id' => $order_id, 'user_membership_id' => $user_membership->get_id())); return $user_membership->get_id(); }
/** * Check if current page is restricted (works with BuddyPress who doesn't have any page/post id) * * @param string|int|array $plans Optional. The membership plan or plans to check against. * Accepts a plan slug, ID, or an array of slugs or IDs. Default: all plans. * @param string $delay * @param bool $exclude_trial */ function kmpl_wc_memberships_has_access_to_restricted_page($plans = null, $delay = null, $exclude_trial = false) { $has_access = false; $member_since = null; $access_time = null; // grant access to super users if (is_user_logged_in() && current_user_can('wc_memberships_access_all_restricted_content')) { $has_access = true; } // Convert to an array in all cases $plans = (array) $plans; // default to use all plans if no plan is specified if (empty($plans)) { $plans = wc_memberships_get_membership_plans(); } foreach ($plans as $plan_id_or_slug) { $membership_plan = wc_memberships_get_membership_plan($plan_id_or_slug); if ($membership_plan && wc_memberships_is_user_active_member(get_current_user_id(), $membership_plan->get_id())) { $has_access = true; if (!$delay && !$exclude_trial) { break; } // Determine the earliest membership for the user $user_membership = wc_memberships()->user_memberships->get_user_membership(get_current_user_id(), $membership_plan->get_id()); // Create a pseudo-rule to help applying filters $rule = new WC_Memberships_Membership_Plan_Rule(array('access_schedule_exclude_trial' => $exclude_trial ? 'yes' : 'no')); /** This filter is documented in includes/class-wc-memberships-capabilities.php **/ $from_time = apply_filters('wc_memberships_access_from_time', $user_membership->get_start_date('timestamp'), $rule, $user_membership); // If there is no time to calculate the access time from, simply // use the current time as access start time if (!$from_time) { $from_time = current_time('timestamp', true); } if (is_null($member_since) || $from_time < $member_since) { $member_since = $from_time; } } } // Add delay if ($has_access && ($delay || $exclude_trial) && $member_since) { $access_time = $member_since; // Determine access time if (strpos($delay, 'month') !== false) { $parts = explode(' ', $delay); $amount = isset($parts[1]) ? (int) $parts[0] : ''; $access_time = wc_memberships()->add_months($member_since, $amount); } else { if ($delay) { $access_time = strtotime($delay, $member_since); } } // Output or show delayed access message if ($access_time <= current_time('timestamp', true)) { $has_access = true; } else { $has_access = false; $message = __('This content is part of your membership, but not yet! You will gain access on {date}', 'woocommerce-memberships'); // Apply the deprecated filter if (has_filter('get_content_delayed_message')) { /** This filter is documented in includes/frontend/class-wc-memberships-frontend.php **/ $message = apply_filters('get_content_delayed_message', $message, null, $access_time); // Notify developers that this filter is deprecated _deprecated_function('The get_content_delayed_message filter', '1.3.1', 'wc_memberships_get_content_delayed_message'); } /** This filter is documented in includes/frontend/class-wc-memberships-frontend.php **/ $message = apply_filters('wc_memberships_get_content_delayed_message', $message, null, $access_time); $message = str_replace('{date}', date_i18n(wc_date_format(), $access_time), $message); $output = '<div class="wc-memberships-content-delayed-message">' . $message . '</div>'; echo $output; } } return $has_access; }
/** * Nonmember content shortcode * * @since 1.1.0 * @param mixed $atts Shortcode attributes * @return string Shortcode result */ public static function nonmember($atts, $content = null) { $plans = wc_memberships_get_membership_plans(); $active_member = array(); foreach ($plans as $plan) { $active = wc_memberships_is_user_active_member(get_current_user_id(), $plan); array_push($active_member, $active); } ob_start(); if (!in_array(true, $active_member)) { echo do_shortcode($content); } return ob_get_clean(); }