JezK
Edit File: membership.php
<?php /* Plugin Name: Membership Description: A custom membership plugin by Advaxe. Makes products free for members, supports early renewal, custom pricing display (Free/Premium), configurable redirects, blocks non-members from adding premium products, and intercepts custom Buy Now buttons with instant checkout for members and free products. Includes First-Time Offer pricing and Premium UI. Version: 1.9.5 Author: Advaxe Text Domain: advaxe-membership */ if ( ! defined( 'ABSPATH' ) ) { exit; // সরাসরি অ্যাক্সেস ব্লক করার জন্য } // প্লাগিন একটিভ বা সেটিংস সেভ হলে উকমার্সে মেম্বারশিপ প্রোডাক্ট তৈরি/আপডেট করার ফাংশন function advaxe_sync_membership_products() { if ( ! class_exists( 'WooCommerce' ) ) return; $monthly_price = get_option('advaxe_membership_monthly_price', '10'); $yearly_price = get_option('advaxe_membership_yearly_price', '100'); // ১. মান্থলি প্রোডাক্ট চেক ও তৈরি $monthly_id = get_option('advaxe_membership_monthly_id'); if ( ! $monthly_id || ! get_post( $monthly_id ) ) { $post_id = wp_insert_post( array( 'post_title' => 'Membership - Monthly Plan', 'post_content' => 'Monthly membership subscription.', 'post_status' => 'publish', 'post_type' => 'product', ) ); update_option('advaxe_membership_monthly_id', $post_id); $monthly_id = $post_id; } $product_m = wc_get_product( $monthly_id ); if ( $product_m ) { $product_m->set_regular_price( $monthly_price ); $product_m->set_price( $monthly_price ); $product_m->set_virtual( true ); $product_m->set_stock_status( 'instock' ); $product_m->set_catalog_visibility( 'hidden' ); $product_m->save(); } // ২. ইয়ার্লি প্রোডাক্ট চেক ও তৈরি $yearly_id = get_option('advaxe_membership_yearly_id'); if ( ! $yearly_id || ! get_post( $yearly_id ) ) { $post_id = wp_insert_post( array( 'post_title' => 'Membership - Yearly Plan', 'post_content' => 'Yearly membership subscription.', 'post_status' => 'publish', 'post_type' => 'product', ) ); update_option('advaxe_membership_yearly_id', $post_id); $yearly_id = $post_id; } $product_y = wc_get_product( $yearly_id ); if ( $product_y ) { $product_y->set_regular_price( $yearly_price ); $product_y->set_price( $yearly_price ); $product_y->set_virtual( true ); $product_y->set_stock_status( 'instock' ); $product_y->set_catalog_visibility( 'hidden' ); $product_y->save(); } } // অ্যাডমিন প্যানেলে সেটিংস পেজ তৈরি add_action('admin_menu', 'advaxe_membership_admin_menu'); function advaxe_membership_admin_menu() { add_menu_page( 'Membership Settings', 'Membership', 'manage_options', 'advaxe-membership', 'advaxe_membership_settings_page', 'dashicons-groups' ); } function advaxe_membership_settings_page() { if (isset($_POST['advaxe_save_membership_settings'])) { update_option('advaxe_membership_monthly_price', sanitize_text_field($_POST['monthly_price'])); update_option('advaxe_membership_yearly_price', sanitize_text_field($_POST['yearly_price'])); // First time offer prices save update_option('advaxe_membership_monthly_offer_price', sanitize_text_field($_POST['monthly_offer_price'])); update_option('advaxe_membership_yearly_offer_price', sanitize_text_field($_POST['yearly_offer_price'])); // Custom Texts Save update_option('advaxe_msg_active', sanitize_textarea_field($_POST['msg_active'])); update_option('advaxe_msg_expired', sanitize_textarea_field($_POST['msg_expired'])); update_option('advaxe_msg_no_plan', sanitize_textarea_field($_POST['msg_no_plan'])); update_option('advaxe_msg_renew_info', sanitize_textarea_field($_POST['msg_renew_info'])); // রিডাইরেক্ট স্লাগ সেভ করা $redirect_slug = sanitize_title($_POST['redirect_slug']); update_option('advaxe_membership_redirect_slug', $redirect_slug); // লগইন পেইজের স্লাগ সেভ করা $login_slug = sanitize_title($_POST['login_slug']); update_option('advaxe_membership_login_slug', $login_slug); // Excluded Products ID সেভ করা $excluded_ids = sanitize_text_field($_POST['excluded_ids']); update_option('advaxe_membership_excluded_ids', $excluded_ids); // প্রাইস চেঞ্জ হলে উকমার্স প্রোডাক্টের প্রাইসও আপডেট হবে advaxe_sync_membership_products(); echo '<div class="updated"><p>Settings saved and WooCommerce products synced!</p></div>'; } $monthly_price = get_option('advaxe_membership_monthly_price', '10'); $yearly_price = get_option('advaxe_membership_yearly_price', '100'); $monthly_offer_price = get_option('advaxe_membership_monthly_offer_price', ''); $yearly_offer_price = get_option('advaxe_membership_yearly_offer_price', ''); $msg_active = get_option('advaxe_msg_active', 'আপনার মেম্বারশিপ বর্তমানেঃ ACTIVE'); $msg_expired = get_option('advaxe_msg_expired', 'আপনার মেম্বারশিপের মেয়াদ শেষ হয়ে গেছে। আবার উপভোগ করতে নিচে থেকে রিনিউ পেমেন্ট করুন।'); $msg_no_plan = get_option('advaxe_msg_no_plan', 'আপনার কোনো মেম্বারশিপ প্ল্যান সক্রিয় নেই। মেম্বারশিপ নিতে নিচে থেকে যেকোনো একটি প্ল্যান সিলেক্ট করে পেমেন্ট করুন।'); $msg_renew_info = get_option('advaxe_msg_renew_info', 'মেয়াদ শেষ হওয়ার আগেই আপনি রিনিউ বা মেয়াদ বাড়িয়ে নিতে পারেন। পেমেন্ট সম্পন্ন হলে নতুন মেয়াদ বর্তমান মেয়াদের সাথে যুক্ত হবে।'); $redirect_slug = get_option('advaxe_membership_redirect_slug', 'membership'); $login_slug = get_option('advaxe_membership_login_slug', 'login'); $excluded_ids = get_option('advaxe_membership_excluded_ids', ''); // পরিসংখ্যান বা ডাটা ক্যালকুলেশন $users_with_membership = get_users(array( 'meta_key' => '_membership_expires', 'fields' => 'ID', )); $active_count = 0; $expired_count = 0; $monthly_count = 0; $yearly_count = 0; $current_time = time(); foreach ($users_with_membership as $uid) { $expires = get_user_meta($uid, '_membership_expires', true); $plan = get_user_meta($uid, '_membership_plan_type', true); if ($expires > $current_time) { $active_count++; if ($plan == 'monthly') { $monthly_count++; } elseif ($plan == 'yearly') { $yearly_count++; } } else { $expired_count++; } } ?> <div class="wrap"> <h1>Membership Dashboard (by Advaxe)</h1> <div style="display: flex; gap: 20px; margin: 20px 0;"> <div style="background:#fff; padding:20px; border-radius:8px; border-left:4px solid #2ecc71; box-shadow:0 1px 3px rgba(0,0,0,0.1); flex:1;"> <h3 style="margin:0 0 10px 0; color:#555;">Active Members</h3> <span style="font-size:28px; font-weight:bold; color:#2ecc71;"><?php echo $active_count; ?></span> </div> <div style="background:#fff; padding:20px; border-radius:8px; border-left:4px solid #e74c3c; box-shadow:0 1px 3px rgba(0,0,0,0.1); flex:1;"> <h3 style="margin:0 0 10px 0; color:#555;">Expired Members</h3> <span style="font-size:28px; font-weight:bold; color:#e74c3c;"><?php echo $expired_count; ?></span> </div> <div style="background:#fff; padding:20px; border-radius:8px; border-left:4px solid #3498db; box-shadow:0 1px 3px rgba(0,0,0,0.1); flex:1;"> <h3 style="margin:0 0 10px 0; color:#555;">Monthly Plans (Active)</h3> <span style="font-size:28px; font-weight:bold; color:#3498db;"><?php echo $monthly_count; ?></span> </div> <div style="background:#fff; padding:20px; border-radius:8px; border-left:4px solid #9b59b6; box-shadow:0 1px 3px rgba(0,0,0,0.1); flex:1;"> <h3 style="margin:0 0 10px 0; color:#555;">Yearly Plans (Active)</h3> <span style="font-size:28px; font-weight:bold; color:#9b59b6;"><?php echo $yearly_count; ?></span> </div> </div> <hr style="border:0; border-top:1px solid #ddd; margin:30px 0;"> <h2>Plugin Settings</h2> <form method="POST"> <table class="form-table"> <!-- Pricing Settings --> <tr><th colspan="2" style="padding-top:20px;"><h3>Pricing Settings</h3></th></tr> <tr> <th><label for="monthly_price">Regular Monthly Price</label></th> <td><input type="number" name="monthly_price" id="monthly_price" value="<?php echo esc_attr($monthly_price); ?>" class="regular-text" step="0.01"></td> </tr> <tr> <th><label for="monthly_offer_price">First Time Monthly Offer</label></th> <td> <input type="number" name="monthly_offer_price" id="monthly_offer_price" value="<?php echo esc_attr($monthly_offer_price); ?>" class="regular-text" step="0.01"> <p class="description">ক্লায়েন্ট প্রথমবারের জন্য কিনলে এই প্রাইস পাবে। ফাঁকা রাখলে রেগুলার প্রাইস কাটবে, আর 0 দিলে ফ্রি হবে।</p> </td> </tr> <tr> <th><label for="yearly_price">Regular Yearly Price</label></th> <td><input type="number" name="yearly_price" id="yearly_price" value="<?php echo esc_attr($yearly_price); ?>" class="regular-text" step="0.01"></td> </tr> <tr> <th><label for="yearly_offer_price">First Time Yearly Offer</label></th> <td> <input type="number" name="yearly_offer_price" id="yearly_offer_price" value="<?php echo esc_attr($yearly_offer_price); ?>" class="regular-text" step="0.01"> <p class="description">ক্লায়েন্ট প্রথমবারের জন্য কিনলে এই প্রাইস পাবে। ফাঁকা রাখলে রেগুলার প্রাইস কাটবে, আর 0 দিলে ফ্রি হবে।</p> </td> </tr> <!-- Custom Texts Settings --> <tr><th colspan="2" style="padding-top:20px; border-top:1px solid #ddd;"><h3>Dashboard Custom Texts</h3></th></tr> <tr> <th><label for="msg_active">Active Status Text</label></th> <td><input type="text" name="msg_active" id="msg_active" value="<?php echo esc_attr($msg_active); ?>" class="regular-text large-text"></td> </tr> <tr> <th><label for="msg_expired">Expired Status Text</label></th> <td><textarea name="msg_expired" id="msg_expired" rows="2" class="regular-text large-text"><?php echo esc_textarea($msg_expired); ?></textarea></td> </tr> <tr> <th><label for="msg_no_plan">No Plan (New User) Text</label></th> <td><textarea name="msg_no_plan" id="msg_no_plan" rows="2" class="regular-text large-text"><?php echo esc_textarea($msg_no_plan); ?></textarea></td> </tr> <tr> <th><label for="msg_renew_info">Renewal Notice/Info Text</label></th> <td><textarea name="msg_renew_info" id="msg_renew_info" rows="2" class="regular-text large-text"><?php echo esc_textarea($msg_renew_info); ?></textarea></td> </tr> <!-- System Settings --> <tr><th colspan="2" style="padding-top:20px; border-top:1px solid #ddd;"><h3>System Settings</h3></th></tr> <tr> <th><label for="redirect_slug">Membership Page Slug</label></th> <td> <input type="text" name="redirect_slug" id="redirect_slug" value="<?php echo esc_attr($redirect_slug); ?>" class="regular-text"> <p class="description">নন-মেম্বাররা Premium প্রোডাক্ট কিনতে গেলে এই পেইজে রিডাইরেক্ট হবে।</p> </td> </tr> <tr> <th><label for="login_slug">Login Page Slug</label></th> <td> <input type="text" name="login_slug" id="login_slug" value="<?php echo esc_attr($login_slug); ?>" class="regular-text"> <p class="description">লগইন না করা ইউজারদের লগইন করার জন্য এই পেইজে পাঠানো হবে।</p> </td> </tr> <tr> <th><label for="excluded_ids">Excluded Product IDs</label></th> <td> <input type="text" name="excluded_ids" id="excluded_ids" value="<?php echo esc_attr($excluded_ids); ?>" class="regular-text"> <p class="description">যে প্রোডাক্টগুলোতে <b>Auto-Checkout / Instant Buy</b> কাজ করবে না, সেগুলোর ID কমা (,) দিয়ে দিন।</p> </td> </tr> </table> <p class="submit"> <?php submit_button('Save Settings', 'primary', 'advaxe_save_membership_settings', false); ?> </p> </form> </div> <?php } // মেম্বারশিপ প্রোডাক্ট দুটিকে উকমার্সের কাছে বাধ্যতামূলক "Purchasable" বা কেনার যোগ্য করা add_filter('woocommerce_is_purchasable', 'advaxe_membership_force_purchasable', 99, 2); function advaxe_membership_force_purchasable($is_purchasable, $product) { $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if ($product->get_id() == $monthly_id || $product->get_id() == $yearly_id) { return true; } return $is_purchasable; } // ইউজারের মেম্বারশিপ একটিভ আছে কিনা চেক করার হেল্পার ফাংশন function advaxe_is_user_member($user_id) { if (!$user_id) return false; $expires = get_user_meta($user_id, '_membership_expires', true); if ($expires && $expires > time()) { return true; } return false; } // মেম্বারদের জন্য প্রোডাক্ট ফ্রি করা এবং নতুন/গেস্ট ইউজারদের মেম্বারশিপ অফার প্রাইস দেওয়া add_filter('woocommerce_product_get_price', 'advaxe_membership_dynamic_pricing', 99, 2); add_filter('woocommerce_product_get_regular_price', 'advaxe_membership_dynamic_pricing', 99, 2); add_filter('woocommerce_product_variation_get_price', 'advaxe_membership_dynamic_pricing', 99, 2); function advaxe_membership_dynamic_pricing($price, $product) { if (is_admin() && !defined('DOING_AJAX')) return $price; $user_id = get_current_user_id(); $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); $product_id = $product->get_id(); // ১. মেম্বারশিপ প্রোডাক্টের ফার্স্ট টাইম অফার প্রাইস হ্যান্ডেল করা (ইউজার ও গেস্ট উভয়ের জন্য) if ($product_id == $monthly_id || $product_id == $yearly_id) { $has_history = ($user_id > 0) ? get_user_meta($user_id, '_membership_expires', true) : ''; // ইউজার যদি নতুন হয় বা গেস্ট থাকে (কখনো মেম্বারশিপ নেয়নি) if (empty($has_history)) { if ($product_id == $monthly_id) { $offer_m = get_option('advaxe_membership_monthly_offer_price', ''); if ($offer_m !== '') return floatval($offer_m); } else { $offer_y = get_option('advaxe_membership_yearly_offer_price', ''); if ($offer_y !== '') return floatval($offer_y); } } return $price; // অফার না থাকলে বা পুরানো ইউজার হলে রেগুলার প্রাইস } // ২. মেম্বারশিপ একটিভ থাকলে সাধারণ প্রোডাক্ট কার্টে ফ্রি করে দেওয়া if (advaxe_is_user_member($user_id)) { return 0; } return $price; } // প্রোডাক্ট পেজে প্রাইস 0 হলে Free এবং 1+ হলে Premium দেখানো add_filter('woocommerce_get_price_html', 'advaxe_custom_price_html_display', 99, 2); function advaxe_custom_price_html_display($price_html, $product) { $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); // মেম্বারশিপ প্রোডাক্ট হলে ডিফল্ট প্রাইস দেখাবে if ($product->get_id() == $monthly_id || $product->get_id() == $yearly_id) { return $price_html; } // অরিজিনাল প্রাইস পাওয়ার জন্য মেম্বারশিপের ডাইনামিক প্রাইসিং ফিল্টার সাময়িকভাবে রিমুভ করছি remove_filter('woocommerce_product_get_regular_price', 'advaxe_membership_dynamic_pricing', 99); $regular_price = $product->get_regular_price(); // চেক করা শেষ, তাই ফিল্টারটি আবার চালু করে দিচ্ছি add_filter('woocommerce_product_get_regular_price', 'advaxe_membership_dynamic_pricing', 99, 2); if (empty($regular_price) || $regular_price == 0) { return '<span class="amount" style="color:#2ecc71; font-weight:bold;">Free</span>'; } else { return '<span class="amount" style="color:#f39c12; font-weight:bold;">Premium</span>'; } } // নন-মেম্বারদের জন্য Premium প্রোডাক্টের Add to Cart বাটনের লিংক মেম্বারশিপ পেইজ করে দেওয়া add_filter('woocommerce_product_add_to_cart_url', 'advaxe_custom_add_to_cart_url', 10, 2); function advaxe_custom_add_to_cart_url($url, $product) { $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if ($product->get_id() == $monthly_id || $product->get_id() == $yearly_id) { return $url; } $regular_price = $product->get_regular_price(); if ($regular_price > 0) { $user_id = get_current_user_id(); if (!advaxe_is_user_member($user_id)) { $membership_page_slug = get_option('advaxe_membership_redirect_slug', 'membership'); return site_url('/' . $membership_page_slug . '/'); } } return $url; } // নন-মেম্বারদের জন্য Premium প্রোডাক্টের Add to Cart বাটনের টেক্সট পরিবর্তন add_filter('woocommerce_product_add_to_cart_text', 'advaxe_custom_add_to_cart_text', 10, 2); add_filter('woocommerce_product_single_add_to_cart_text', 'advaxe_custom_add_to_cart_text', 10, 2); function advaxe_custom_add_to_cart_text($text, $product) { $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if ($product->get_id() == $monthly_id || $product->get_id() == $yearly_id) { return $text; } $regular_price = $product->get_regular_price(); if ($regular_price > 0) { $user_id = get_current_user_id(); if (!advaxe_is_user_member($user_id)) { return 'Get Membership'; } } return $text; } // শপ পেইজে Premium এবং Free প্রোডাক্টের জন্য AJAX Add to Cart বন্ধ করা add_filter( 'woocommerce_loop_add_to_cart_args', 'advaxe_remove_ajax_cart_button', 10, 2 ); function advaxe_remove_ajax_cart_button( $args, $product ) { $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if ($product->get_id() == $monthly_id || $product->get_id() == $yearly_id) { return $args; } $excluded_ids_raw = get_option('advaxe_membership_excluded_ids', ''); $excluded_ids = array_filter(array_map('trim', explode(',', $excluded_ids_raw))); if (in_array($product->get_id(), $excluded_ids)) { return $args; } if (isset($args['class'])) { $args['class'] = str_replace('ajax_add_to_cart', '', $args['class']); } return $args; } // ========================================================================= // মেম্বার এবং নন-মেম্বার (ফ্রি প্রোডাক্টের জন্য) Instant Checkout / Auto Buy // ========================================================================= add_action('template_redirect', 'advaxe_member_instant_checkout', 4); function advaxe_member_instant_checkout() { if ( is_admin() ) return; $product_id = 0; if (isset($_REQUEST['add-to-cart']) && is_numeric($_REQUEST['add-to-cart'])) { $product_id = intval($_REQUEST['add-to-cart']); } elseif (isset($_GET['buy_now']) && is_numeric($_GET['buy_now'])) { $product_id = intval($_GET['buy_now']); } if ($product_id > 0) { $user_id = get_current_user_id(); $is_member = advaxe_is_user_member($user_id); $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if ($product_id == $monthly_id || $product_id == $yearly_id) return; $excluded_ids_raw = get_option('advaxe_membership_excluded_ids', ''); $excluded_ids = array_filter(array_map('trim', explode(',', $excluded_ids_raw))); if (in_array($product_id, $excluded_ids)) return; $variation_id = isset($_REQUEST['variation_id']) ? intval($_REQUEST['variation_id']) : 0; $final_product_id = ($variation_id > 0) ? $variation_id : $product_id; $product = wc_get_product($final_product_id); if (!$product) return; $is_free_checkout = false; if ($is_member) { $is_free_checkout = true; } else { $regular_price = $product->get_regular_price(); if (empty($regular_price) || $regular_price == 0) { $is_free_checkout = true; } } if (!$is_free_checkout) return; WC()->cart->empty_cart(); $order_args = array(); if ($user_id > 0) { $order_args['customer_id'] = $user_id; } $order = wc_create_order($order_args); $quantity = isset($_REQUEST['quantity']) ? intval($_REQUEST['quantity']) : 1; $item_id = $order->add_product($product, $quantity); $item = $order->get_item($item_id); $item->set_subtotal(0); $item->set_total(0); $item->save(); $order->calculate_totals(); $order->set_total(0); if ($user_id > 0) { $user_info = get_userdata($user_id); $order->set_billing_email($user_info->user_email); $order->set_billing_first_name($user_info->first_name); $order->set_billing_last_name($user_info->last_name); } $order->update_status('completed', 'Auto-completed 0 amount order for free product / member.'); wp_safe_redirect($order->get_checkout_order_received_url()); exit; } } // ========================================================================= // Buy Now Button Interceptor // ========================================================================= add_action('template_redirect', 'advaxe_membership_intercept_buy_now', 5); function advaxe_membership_intercept_buy_now() { $product_id = 0; if (isset($_GET['buy_now']) && is_numeric($_GET['buy_now'])) { $product_id = intval($_GET['buy_now']); } elseif (isset($_POST['is_buy_now']) && $_POST['is_buy_now'] == '1' && isset($_POST['add-to-cart'])) { $product_id = intval($_POST['add-to-cart']); } if ($product_id > 0) { $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if ($product_id == $monthly_id || $product_id == $yearly_id) { return; } $product = wc_get_product($product_id); if (!$product) return; $regular_price = $product->get_regular_price(); if ($regular_price > 0) { $user_id = get_current_user_id(); if (!advaxe_is_user_member($user_id)) { $membership_page_slug = get_option('advaxe_membership_redirect_slug', 'membership'); $redirect_url = site_url('/' . $membership_page_slug . '/'); wc_add_notice('এই Premium প্রোডাক্টটি কিনতে আপনাকে মেম্বারশিপ নিতে হবে।', 'error'); wp_safe_redirect($redirect_url); exit; } } } } // ব্যাকএন্ড সিকিউরিটি: add_filter('woocommerce_add_to_cart_validation', 'advaxe_redirect_non_members_to_membership', 10, 3); function advaxe_redirect_non_members_to_membership($passed, $product_id, $quantity) { $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if ($product_id == $monthly_id || $product_id == $yearly_id) { return $passed; } $product = wc_get_product($product_id); $regular_price = $product->get_regular_price(); if ($regular_price > 0) { $user_id = get_current_user_id(); if (!advaxe_is_user_member($user_id)) { $membership_page_slug = get_option('advaxe_membership_redirect_slug', 'membership'); $redirect_url = site_url('/' . $membership_page_slug . '/'); wc_add_notice('এই Premium প্রোডাক্টটি পেতে আপনাকে মেম্বারশিপ নিতে হবে।', 'error'); if (wp_doing_ajax()) { wp_send_json(array( 'error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', $redirect_url, $product_id) )); } else { wp_safe_redirect($redirect_url); exit; } } } return $passed; } // উকমার্সে পেমেন্ট কমপ্লিট বা প্রসেসিং হলে ইউজারের মেম্বারশিপের মেয়াদ বাড়ানো add_action('woocommerce_order_status_completed', 'advaxe_membership_grant_access_on_payment'); add_action('woocommerce_order_status_processing', 'advaxe_membership_grant_access_on_payment'); function advaxe_membership_grant_access_on_payment($order_id) { $order = wc_get_order($order_id); $user_id = $order->get_user_id(); if (!$user_id) return; if ($order->get_meta('_membership_processed')) return; $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); $membership_ordered = false; foreach ($order->get_items() as $item_id => $item) { $product_id = $item->get_product_id(); if ($product_id == $monthly_id || $product_id == $yearly_id) { $current_expires = get_user_meta($user_id, '_membership_expires', true); $base_time = ($current_expires && $current_expires > time()) ? $current_expires : time(); if ($product_id == $monthly_id) { $new_expires = strtotime('+1 month', $base_time); update_user_meta($user_id, '_membership_plan_type', 'monthly'); } else { $new_expires = strtotime('+1 year', $base_time); update_user_meta($user_id, '_membership_plan_type', 'yearly'); } update_user_meta($user_id, '_membership_expires', $new_expires); $membership_ordered = true; break; } } if ($membership_ordered) { $order->update_meta_data('_membership_processed', 'yes'); $order->save(); } } // ফ্রন্টএন্ড ড্যাশবোর্ড শর্টকোড [membership_dashboard] (Premium UI - Guest Accessible) add_shortcode('membership_dashboard', 'advaxe_membership_dashboard_shortcode'); function advaxe_membership_dashboard_shortcode() { if ( ! class_exists( 'WooCommerce' ) ) { return '<p>এই প্লাগিনটি ব্যবহারের জন্য WooCommerce প্লাগিনটি ইনস্টল ও একটিভ থাকা আবশ্যক।</p>'; } // প্যাকেজ সিলেক্ট এবং চেকাউটে পাঠানোর হ্যান্ডলার (গেস্ট ও মেম্বার উভয়ের জন্য) if (isset($_POST['advaxe_checkout_membership'])) { $plan_type = sanitize_text_field($_POST['membership_type']); $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); if(!$monthly_id || !$yearly_id) { advaxe_sync_membership_products(); $monthly_id = get_option('advaxe_membership_monthly_id'); $yearly_id = get_option('advaxe_membership_yearly_id'); } $target_product_id = ($plan_type == 'monthly') ? $monthly_id : $yearly_id; if ($target_product_id) { WC()->cart->empty_cart(); WC()->cart->add_to_cart($target_product_id); wp_safe_redirect(wc_get_checkout_url()); exit; } } $user_id = get_current_user_id(); $is_logged_in = is_user_logged_in(); $expires = $is_logged_in ? get_user_meta($user_id, '_membership_expires', true) : false; $is_active = ($expires && $expires > time()); // প্রাইস ও অফার চেক $monthly_price = get_option('advaxe_membership_monthly_price', '10'); $yearly_price = get_option('advaxe_membership_yearly_price', '100'); $monthly_offer = get_option('advaxe_membership_monthly_offer_price', ''); $yearly_offer = get_option('advaxe_membership_yearly_offer_price', ''); // কাস্টম টেক্সট $msg_active = get_option('advaxe_msg_active', 'আপনার মেম্বারশিপ বর্তমানেঃ ACTIVE'); $msg_expired = get_option('advaxe_msg_expired', 'আপনার মেম্বারশিপের মেয়াদ শেষ হয়ে গেছে। আবার উপভোগ করতে নিচে থেকে রিনিউ পেমেন্ট করুন।'); $msg_no_plan = get_option('advaxe_msg_no_plan', 'আপনার কোনো মেম্বারশিপ প্ল্যান সক্রিয় নেই। মেম্বারশিপ নিতে নিচে থেকে যেকোনো একটি প্ল্যান সিলেক্ট করে পেমেন্ট করুন।'); $msg_renew_info = get_option('advaxe_msg_renew_info', 'মেয়াদ শেষ হওয়ার আগেই আপনি রিনিউ বা মেয়াদ বাড়িয়ে নিতে পারেন। পেমেন্ট সম্পন্ন হলে নতুন মেয়াদ বর্তমান মেয়াদের সাথে যুক্ত হবে।'); $display_m_price = $monthly_price; $display_y_price = $yearly_price; $m_badge = ''; $y_badge = ''; $has_history = $is_logged_in ? get_user_meta($user_id, '_membership_expires', true) : false; if (empty($has_history)) { if ($monthly_offer !== '') { $display_m_price = $monthly_offer; $m_badge = '<span class="advaxe-badge">First Time Offer</span>'; } if ($yearly_offer !== '') { $display_y_price = $yearly_offer; $y_badge = '<span class="advaxe-badge">First Time Offer</span>'; } } // UI/UX Styling $output = ' <style> .advaxe-dashboard { max-width: 600px; margin: 30px auto; font-family: "Segoe UI", sans-serif; background: #ffffff; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,0.06); overflow: hidden; border: 1px solid #eaeaea; } .advaxe-header { background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%); padding: 25px 30px; text-align: center; } .advaxe-header h2 { margin: 0; font-size: 24px; font-weight: 600; color:#fff; } .advaxe-body { padding: 30px; } .advaxe-status-box { text-align: center; margin-bottom: 25px; } .status-badge { display: inline-block; padding: 6px 20px; border-radius: 20px; font-weight: 600; font-size: 14px; margin-bottom: 15px; } .badge-active { background: #e8f8f5; color: #1abc9c; border: 1px solid #1abc9c; } .badge-expired { background: #fdedec; color: #e74c3c; border: 1px solid #e74c3c; } .badge-none { background: #f4f6f9; color: #7f8c8d; border: 1px solid #bdc3c7; } .advaxe-info-text { font-size: 15px; color: #555; line-height: 1.6; } .advaxe-notice { background: #f8f9fa; border-left: 4px solid #3498db; padding: 12px 18px; border-radius: 4px; font-size: 14px; color: #555; margin-top: 20px; text-align: left; } .advaxe-guest-login-notice { background: #eef6ff; border-left: 4px solid #3498db; padding: 14px 18px; border-radius: 6px; font-size: 15px; color: #2c3e50; margin-bottom: 25px; text-align: center; font-weight: 500; } .advaxe-guest-login-notice a { color: #2980b9; font-weight: bold; text-decoration: underline; margin-left: 5px; } .advaxe-plan-wrapper { display: flex; flex-direction: column; gap: 15px; margin-top: 10px; margin-bottom: 25px; } .advaxe-plan-card { border: 2px solid #ecf0f1; border-radius: 8px; padding: 15px 20px; cursor: pointer; transition: all 0.3s ease; display: flex; align-items: center; justify-content: space-between; position: relative; } .advaxe-plan-card:hover { border-color: #bdc3c7; background: #fbfcfc; } .advaxe-plan-left { display: flex; align-items: center; } .advaxe-plan-card input[type="radio"] { margin-right: 15px; transform: scale(1.3); cursor: pointer; } .advaxe-plan-title { font-size: 16px; font-weight: 600; color: #2c3e50; } .advaxe-plan-price { font-size: 18px; font-weight: 700; color: #2980b9; } .advaxe-badge { background: #e74c3c; color: #fff; font-size: 10px; padding: 3px 8px; border-radius: 12px; margin-left: 8px; font-weight: bold; text-transform: uppercase; letter-spacing: 0.5px; position: relative; top: -1px; } .advaxe-btn { background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%); color: #fff; border: none; padding: 16px 30px; font-size: 16px; font-weight: 600; border-radius: 8px; cursor: pointer; width: 100%; transition: all 0.3s ease; box-shadow: 0 4px 15px rgba(39, 174, 96, 0.2); } .advaxe-btn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(39, 174, 96, 0.35); } </style>'; $output .= '<div class="advaxe-dashboard">'; $output .= '<div class="advaxe-header"><h2>Membership Dashboard</h2></div>'; $output .= '<div class="advaxe-body">'; // শর্ত ২: গেস্টদের জন্য লগইন নোটিশ দেখাবে if (!$is_logged_in) { $my_account_url = function_exists('wc_get_page_permalink') ? wc_get_page_permalink('myaccount') : site_url('/my-account/'); $output .= '<div class="advaxe-guest-login-notice">'; $output .= 'আপনার কি আগের মেম্বারশীপ নেয়া আছে? তাহলে <a href="' . esc_url($my_account_url) . '">লগইন করুন</a>'; $output .= '</div>'; } $output .= '<div class="advaxe-status-box">'; if ($is_active) { $days_left = ceil(($expires - time()) / DAY_IN_SECONDS); $output .= '<div class="status-badge badge-active">' . esc_html($msg_active) . '</div>'; $output .= '<p style="margin:5px 0; font-size: 15px; color:#555;">মেয়াদ শেষ হবেঃ <strong style="color:#2c3e50;">' . date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $expires) . '</strong></p>'; $output .= '<p style="margin:5px 0; font-size: 15px; color:#555;">বাকি আছেঃ <strong style="color:#e74c3c; font-size: 18px;">' . $days_left . ' দিন</strong></p>'; $output .= '<div class="advaxe-notice">' . esc_html($msg_renew_info) . '</div>'; } else { if ($is_logged_in && $expires) { $output .= '<div class="status-badge badge-expired">Status: EXPIRED</div>'; $output .= '<p class="advaxe-info-text">' . esc_html($msg_expired) . '</p>'; } else { $output .= '<div class="status-badge badge-none">Status: NO PLAN</div>'; $output .= '<p class="advaxe-info-text">' . esc_html($msg_no_plan) . '</p>'; } } $output .= '</div>'; // end status box $output .= '<div style="border-top:1px solid #f1f1f1; padding-top:25px;">'; $output .= '<h3 style="color:#2c3e50; margin-bottom:15px; font-size: 18px; text-align: left;">' . ($is_active ? 'Early Renewal / Extend Access' : 'Select a Plan') . '</h3>'; $output .= '<form method="POST">'; $output .= '<div class="advaxe-plan-wrapper">'; // Monthly Card $output .= '<label class="advaxe-plan-card">'; $output .= '<div class="advaxe-plan-left">'; $output .= '<input type="radio" name="membership_type" value="monthly" checked>'; $output .= '<span class="advaxe-plan-title">Monthly Plan ' . $m_badge . '</span>'; $output .= '</div>'; $output .= '<span class="advaxe-plan-price">' . ($display_m_price == 0 && $display_m_price !== '' ? 'Free' : wc_price($display_m_price)) . '</span>'; $output .= '</label>'; // Yearly Card $output .= '<label class="advaxe-plan-card">'; $output .= '<div class="advaxe-plan-left">'; $output .= '<input type="radio" name="membership_type" value="yearly">'; $output .= '<span class="advaxe-plan-title">Yearly Plan ' . $y_badge . '</span>'; $output .= '</div>'; $output .= '<span class="advaxe-plan-price">' . ($display_y_price == 0 && $display_y_price !== '' ? 'Free' : wc_price($display_y_price)) . '</span>'; $output .= '</label>'; $output .= '</div>'; $output .= '<input type="submit" name="advaxe_checkout_membership" class="advaxe-btn" value="' . ($is_active ? 'Pay & Renew Now' : 'Pay & Subscribe Now') . '">'; $output .= '</form>'; $output .= '</div>'; $output .= '</div>'; // end advaxe-body $output .= '</div>'; // end advaxe-dashboard return $output; }