Created
August 16, 2024 12:30
-
-
Save goranefbl/4f2d6941860d1397c96fd5666861dad7 to your computer and use it in GitHub Desktop.
Funnelkit Automation Referral Tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!bwfan_is_autonami_pro_active() || version_compare(BWFAN_PRO_VERSION, '2.0.3', '>=')) { | |
class BWFAN_Contact_Referral_ID extends BWFAN_Merge_Tag { | |
private static $instance = null; | |
public function __construct() { | |
$this->tag_name = 'contact_referral_id'; | |
$this->tag_description = __('Contact Referral ID', 'wp-marketing-automations'); | |
add_shortcode('bwfan_contact_referral_id', array($this, 'parse_shortcode')); | |
$this->priority = 19; | |
$this->is_crm_broadcast = true; | |
} | |
public static function get_instance() { | |
if (null === self::$instance) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Parse the merge tag and return its value. | |
* | |
* @param $attr | |
* | |
* @return mixed|string|void | |
*/ | |
public function parse_shortcode($attr) { | |
$get_data = BWFAN_Merge_Tag_Loader::get_data(); | |
if (true === $get_data['is_preview']) { | |
return $this->parse_shortcode_output($this->get_dummy_preview(), $attr); | |
} | |
/** If Contact ID available */ | |
$cid = isset($get_data['contact_id']) ? $get_data['contact_id'] : ''; | |
$referral_id = $this->get_referral_id($cid); | |
if (!empty($referral_id)) { | |
return $this->parse_shortcode_output($referral_id, $attr); | |
} | |
/** If order */ | |
$order = $this->get_order_object($get_data); | |
if (!empty($order)) { | |
$user_id = $order->get_user_id(); | |
if ($user_id) { | |
$referral_id = get_user_meta($user_id, 'gens_referral_id', true); | |
if (!empty($referral_id)) { | |
return $this->parse_shortcode_output($referral_id, $attr); | |
} | |
} | |
} | |
/** If user ID or email */ | |
$user_id = isset($get_data['user_id']) ? $get_data['user_id'] : ''; | |
$email = isset($get_data['email']) ? $get_data['email'] : ''; | |
$contact = bwf_get_contact($user_id, $email); | |
if (absint($contact->get_id()) > 0 && class_exists('BWFCRM_Contact')) { | |
$contact_crm = new BWFCRM_Contact($contact); | |
$user_id = $contact_crm->get_wpid(); | |
if ($user_id) { | |
$referral_id = get_user_meta($user_id, 'gens_referral_id', true); | |
if (!empty($referral_id)) { | |
return $this->parse_shortcode_output($referral_id, $attr); | |
} | |
} | |
} | |
return $this->parse_shortcode_output('', $attr); | |
} | |
public function get_referral_id($cid) { | |
$cid = absint($cid); | |
if (!class_exists('BWFCRM_Contact') || 0 === $cid) { | |
return ''; | |
} | |
$contact = new BWFCRM_Contact($cid); | |
if (!$contact->is_contact_exists()) { | |
return ''; | |
} | |
$user_id = $contact->get_wpid(); | |
return get_user_meta($user_id, 'gens_referral_id', true); | |
} | |
/** | |
* Show dummy value of the current merge tag. | |
* | |
* @return string | |
*/ | |
public function get_dummy_preview() { | |
return 'SAMPLE_REFERRAL_ID_123'; | |
} | |
} | |
/** | |
* Register this merge tag to a group. | |
*/ | |
BWFAN_Merge_Tag_Loader::register('bwf_contact', 'BWFAN_Contact_Referral_ID', null, 'Contact'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment