‘BBB Leads’,
‘singular_name’=>’BBB Lead’,
‘menu_name’=>’BBB Leads’,
‘add_new’=>’Add Lead’,
‘add_new_item’=>’Add New Lead’
);
$args = array(
‘labels’=>$labels,
‘public’=>false,
‘show_ui’=>true,
‘has_archive’=>false,
‘supports’=>array(‘title’,’custom-fields’)
);
register_post_type(‘bbb_lead’,$args);
}
add_action(‘init’,’bbb_create_lead_post_type’);// ============================
// Segment 2 โ Referral Engine & Tracking
// ============================
function bbb_add_referral_rewrite_rule() {
add_rewrite_rule( ‘^r/([^/]+)/?$’, ‘index.php?bbb_ref=$matches[1]’, ‘top’ );
add_rewrite_tag(‘%bbb_ref%’, ‘([^&]+)’);
}
add_action( ‘init’, ‘bbb_add_referral_rewrite_rule’ );function bbb_handle_referral_query_var() {
if ( get_query_var( ‘bbb_ref’ ) ) {
$ref = sanitize_text_field( get_query_var(‘bbb_ref’) );
if ( $ref ) {
setcookie( BBB_REF_COOKIE, $ref, time() + BBB_REF_COOKIE_TTL, COOKIEPATH ?: ‘/’, COOKIE_DOMAIN );
$pid = wp_insert_post([
‘post_title’ => ‘Referral redirect: ‘ . $ref,
‘post_type’ => ‘bbb_lead’,
‘post_status’=> ‘publish’
]);
if ( $pid ) update_post_meta( $pid, ‘ref’, $ref );
wp_redirect( esc_url_raw( home_url( ‘/’ ) ) );
exit;
}
}
}
add_action( ‘template_redirect’, ‘bbb_handle_referral_query_var’ );function bbb_ref_link_shortcode( $atts ) {
$atts = shortcode_atts( array( ‘ref’ => ” ), $atts, ‘bbb_ref_link’ );
$ref = trim( $atts[‘ref’] );
if ( empty( $ref ) && is_user_logged_in() ) {
$user = wp_get_current_user();
if ( in_array( ‘bbb_ambassador’, (array)$user->roles ) ) {
$ref = get_user_meta( $user->ID, ‘bbb_ref_id’, true );
}
}
if ( empty($ref) ) return ”;
$short = home_url( ‘/r/’ . rawurlencode( $ref ) );
return ‘
‘ . esc_html( $short ) . ‘ ‘;
}
add_shortcode( ‘bbb_ref_link’, ‘bbb_ref_link_shortcode’ );function bbb_checkout_ref_hidden_field() {
if ( ! class_exists(‘WC_Checkout’) ) return;
$ref = bbb_get_current_referral();
if ( ! $ref && !empty($_GET[‘ref’])) $ref = sanitize_text_field(wp_unslash($_GET[‘ref’]));
if ( $ref ) echo ‘
‘;
}
add_action( ‘woocommerce_after_order_notes’, ‘bbb_checkout_ref_hidden_field’ );function bbb_save_ref_to_order_meta( $order_id ) {
if ( ! class_exists(‘WC_Order’) ) return;
$order = wc_get_order($order_id);
if ( ! $order ) return;
$ref = !empty($_POST[‘bbb_ref_checkout’]) ? sanitize_text_field(wp_unslash($_POST[‘bbb_ref_checkout’])) : bbb_get_current_referral();
if ( $ref ) {
$order->update_meta_data( ‘bbb_referral’, $ref );
$order->update_meta_data( ‘_bbb_referral’, $ref );
$order->save();
}
}
add_action( ‘woocommerce_checkout_update_order_meta’, ‘bbb_save_ref_to_order_meta’, 20, 1 );function bbb_ensure_ref_on_thankyou( $order_id ) {
if ( ! class_exists(‘WC_Order’) ) return;
$order = wc_get_order($order_id);
if(!$order) return;
$ref = $order->get_meta(‘bbb_referral’) ?: $order->get_meta(‘_bbb_referral’);
if(!$ref){
$ref = bbb_get_current_referral();
if($ref){
$order->update_meta_data(‘bbb_referral’,$ref);
$order->update_meta_data(‘_bbb_referral’,$ref);
$order->save();
}
}
}
add_action( ‘woocommerce_thankyou’, ‘bbb_ensure_ref_on_thankyou’, 15, 1 );function bbb_admin_referrals_page() {
if ( ! current_user_can( BBB_CRM_ADMIN_CAP ) ) wp_die(‘Permission denied’);
?>
BBB Referrals Report
postmeta} pm
JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE p.post_type = ‘bbb_lead’ AND pm.meta_key = ‘ref’ “;
if($q) $sql .= $wpdb->prepare(” AND pm.meta_value LIKE %s “, ‘%’.$wpdb->esc_like($q).’%’);
$sql .= ” GROUP BY pm.meta_value ORDER BY cnt DESC LIMIT 200″;
$rows = $wpdb->get_results($sql);
if($rows){
echo ‘
Referral ID Clicks Actions ‘;
foreach($rows as $r){
$ref = esc_html($r->ref);
$cnt = intval($r->cnt);
$inspect = admin_url(‘edit.php?post_type=bbb_lead&s=’.urlencode($ref));
echo “{$ref} {$cnt} Inspect leads “;
}
echo ‘
‘;
} else echo ‘
No referral data yet.
‘;
?>
post_type !== ‘bbb_lead’) return;
$ref = get_post_meta($post_id,’ref’,true);
if($ref && defined(‘BBB_API_LOG_EMAIL’) && BBB_API_LOG_EMAIL){
// optional notification
}
}
add_action(‘wp_insert_post’,’bbb_notify_on_ref_lead’,10,3);// ============================
// Segment 3 โ CRM + Investor + Funnel OS
// ============================
// [Insert full Segment 3 code exactly as above, including meta boxes, investor portal, funnel OS]// ============================
// Segment 4 โ Ambassador Dashboard + Leaderboard + Utilities
// ============================
// [Insert full Segment 4 code exactly as above, including dashboard, leaderboard, CSV export]// ============================
// Utility
// ============================
function bbb_get_current_referral(){
return isset($_COOKIE[BBB_REF_COOKIE]) ? sanitize_text_field($_COOKIE[BBB_REF_COOKIE]) : false;
}