function memberaccess_get_member_products($user_id, $delay, $all = false) { /** Pull all the orders the member has ever made */ $orders = (array) get_user_option('acp_orders', $user_id); /** Initialize $active_subscriptions array */ $subscriptions = array(); /** Initial time calculations */ if (!$all) { $now = time(); $delay = (int) $delay ? $delay * 86400 : 0; } /** Cycle through $orders looking for active (non-expired) subscriptions */ foreach ($orders as $order) { // check order post status $order_post = get_post($order); if (empty($order_post->post_status) || $order_post->post_status != 'publish') { continue; } $product = (int) get_post_meta($order, '_acp_order_product_id', true); if (!$all) { $expiration = memberaccess_get_order_expiry($order, $product, $delay); /** If subscription can expire and has expired, skip this iteration */ if ($expiration < 0 || $expiration > 0 && $now > $expiration) { continue; } } /** If active, save the product ID */ $subscriptions[$order] = $product; } return $subscriptions; }
/** * Determines if a user has a certain active access level. * * Checks the user meta to see if the user has a particular access * level, and if it is still active. * * @since 0.1.0 * * @param string $access_level. * @param string $user_id, optional. * @return boolean */ function member_has_access_level($access_level = '', $user_id = '', $delay = 0) { /** Check to see if $user_id is provided. If not, assume current logged in user */ $user_id = $user_id ? (int) $user_id : get_current_user_id(); /** If user is not an AccessPress member, return false */ if (!user_can($user_id, 'access_membership')) { return false; } /** Pull all the orders the member has ever made */ $orders = (array) get_user_option('acp_orders', $user_id); /** Initialize $active_subscriptions array */ $active_subscriptions = array(); /** Initial time calculations */ $now = time(); $delay = (int) $delay ? $delay * 86400 : 0; /** Cycle through $orders looking for active (non-expired) subscriptions */ foreach ($orders as $order) { $product = (int) get_post_meta($order, '_acp_order_product_id', true); $expiration = memberaccess_get_order_expiry($order, $product, $delay); /** If subscription can expire and has expired, skip this iteration */ if ($expiration < 0 || $expiration > 0 && $now > $expiration) { continue; } /** If active, save the product ID */ $active_subscriptions[] = $product; } /** If no active subscriptions, return false */ if (!$active_subscriptions) { return false; } /** Cycle through active subscriptions, look for one that has the proper access level, return true as soon as we find one. */ foreach ((array) $access_level as $level) { $level = sanitize_title_with_dashes($level); foreach ($active_subscriptions as $product) { if (has_term($level, 'acp-access-level', $product)) { return true; } } } /** Else, return false */ return false; }
function memberaccess_member_products_content($atts, $content = '') { add_filter('comments_open', '__return_false'); if (!is_user_logged_in()) { return ''; } $user = wp_get_current_user(); /** Pull all the orders the member has ever made */ $orders = get_user_option('acp_orders', $user->ID); if (empty($orders)) { return ''; } /** check for cancel requests */ if (!empty($_GET['cancel']) && !empty($_GET['order_id']) && !empty($_GET['_wpnonce']) && $_GET['cancel'] == 'true' && wp_verify_nonce($_GET['_wpnonce'], 'cancel-subscription-' . $_GET['order_id'])) { memberaccess_cancel_subscription($_GET['order_id']); } $output = ''; $date_format = get_option('date_format'); $order_format = '<li><span class="premise-member-product">%s</span> - <span class="premise-member-product-expiry">%s</span> <span class="premise-member-product-cancel">%s</span></li>'; /** Cycle through $orders looking for active (non-expired) subscriptions */ foreach ($orders as $order) { // get product $product = (int) get_post_meta($order, '_acp_order_product_id', true); $product_post = get_post($product); if (!$product_post) { continue; } // get expiry time $expiration = memberaccess_get_order_expiry($order, $product, 0, true); if (0 == $expiration) { $output .= sprintf($order_format, esc_html($product_post->post_title), __('Lifetime', 'premise'), ''); continue; } $payment_profile = get_user_option('memberaccess_cc_payment_' . $product); if ($payment_profile) { $output .= sprintf($order_format, esc_html($product_post->post_title), date($date_format, $expiration), ''); continue; } $renew_url = add_query_arg(array('renew' => 'true', 'product_id' => $product), get_permalink(accesspress_get_option('checkout_page'))); $cancel_url = ''; $cancel_status = __('cancel', 'premise'); $renewal_time = get_post_meta($order, '_acp_order_renewal_time', true); $status = get_post_meta($order, '_acp_order_status', true); if ($payment_profile && $renewal_time > time() - 172800 && $status != $cancel_status) { $cancel_url = sprintf(__('<a href="%s" %s>Cancel</a>', 'premise'), wp_nonce_url(add_query_arg(array('cancel' => 'true', 'order_id' => $order), get_permalink(accesspress_get_option('member_page'))), 'cancel-subscription-' . $order), ''); } $output .= sprintf($order_format, esc_html($product_post->post_title), date($date_format, $expiration) . ' - ' . sprintf(__('<a href="%s" %s>Renew</a>', 'premise'), $renew_url, ''), $cancel_url); } return '<ul class="premise-member-products">' . $output . '</ul>'; }