-
-
Save greathmaster/f7ce6528e76acb68be0c884d3dd00132 to your computer and use it in GitHub Desktop.
Link Paid Memberships Pro discount codes to affiliate accounts from another plugin/service.
This file contains 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
/* | |
Link discount codes to affiliates. | |
*/ | |
//this is the parameter we're looking for to find affiliates... based on which plugin/etc you are using | |
//define('PMPRO_LDC2AFF_KEY', 'wpam_id'); //Affiliates Manager Plugin | |
define('PMPRO_LDC2AFF_KEY', 'pa'); //WordPress Lightweight Affiliates | |
//define('PMPRO_LDC2AFF_KEY', 'ref'); //AffiliateWP | |
//helper function to get the values from options and make sure they are an array | |
function pmpro_ld2aff_getOptions() { | |
$d2a = get_option('pmpro_ldc2aff', array()); | |
if(!is_array($d2a)) | |
$d2a = array(); | |
return $d2a; | |
} | |
//helper function to add a discount code/affiliate pair | |
function pmpro_ldc2aff_updateCode($code_id, $affiliate) { | |
$d2a = pmpro_ld2aff_getOptions(); | |
if(empty($affiliate) && !empty($d2a[$code_id])) { | |
unset($d2a[$code_id]); | |
update_option('pmpro_ldc2aff', $d2a, 'no'); | |
} elseif(!empty($affiliate)) { | |
$d2a[$code_id] = $affiliate; | |
update_option('pmpro_ldc2aff', $d2a, 'no'); | |
} | |
} | |
//helper to get a discount code from an affiliate | |
function pmpro_ldc2aff_getCodeByAffiliate($affiliate) { | |
$d2a = pmpro_ld2aff_getOptions(); | |
return array_search($affiliate, $d2a); | |
} | |
//helper get an affiliate id from a discount code | |
function pmpro_ldc2aff_getAffiliateByCode($code_id) { | |
$d2a = pmpro_ld2aff_getOptions(); | |
if(isset($d2a[$code_id])) | |
return $d2a[$code_id]; | |
else | |
return ""; | |
} | |
//on init we try to sync the affiliate id and discount code | |
function pmpro_ldc2aff_init() { | |
global $wpdb; | |
//if this isn't set, we can't work | |
if(!defined('PMPRO_LDC2AFF_KEY')) | |
return; | |
//make sure PMPro is active | |
if(!defined('PMPRO_VERSION')) | |
return; | |
//might want to check if a specific affiliate plugin is also running | |
//affiliate id? set discount code | |
if(!empty($_REQUEST[PMPRO_LDC2AFF_KEY])) { | |
//bail if another discount code is already set | |
if(!empty($_REQUEST['discount_code'])) | |
return; | |
//otherwise, let's figure out the code to use | |
$affiliate = sanitize_text_field($_REQUEST[PMPRO_LDC2AFF_KEY]); | |
$code_id = pmpro_ldc2aff_getCodeByAffiliate($affiliate); | |
if(!empty($code_id)) { | |
$code = $wpdb->get_var("SELECT `code` FROM $wpdb->pmpro_discount_codes WHERE id ='" . intval($code_id) . "' LIMIT 1"); | |
$_REQUEST['discount_code'] = $code; | |
$_GET['discount_code'] = $code; | |
$_POST['discount_code'] = $code; | |
} | |
} | |
//discount code? set affiliate id | |
if(!empty($_REQUEST['discount_code'])) { | |
$code = sanitize_text_field($_REQUEST['discount_code']); | |
$code_id = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_discount_codes WHERE `code` = '" . esc_sql($code) . "' LIMIT 1"); | |
if(!empty($code_id)) { | |
$affiliate = pmpro_ldc2aff_getAffiliateByCode($code_id); | |
$_REQUEST[PMPRO_LDC2AFF_KEY] = $affiliate; | |
$_GET[PMPRO_LDC2AFF_KEY] = $affiliate; | |
$_POST[PMPRO_LDC2AFF_KEY] = $affiliate; | |
} | |
} | |
} | |
add_action('init', 'pmpro_ldc2aff_init', 1); | |
//show options on discont code page | |
function pmpro_ldc2aff_pmpro_discount_code_after_settings($code_id) { | |
$affiliate = pmpro_ldc2aff_getAffiliateByCode($code_id); | |
?> | |
<hr /> | |
<h3>Affiliate</h3> | |
<p>Link this discount code to an affiliate. Currently monitoring the '<?php echo PMPRO_LDC2AFF_KEY;?>' parameter. When the affiliate ID is set, this discount code will be defaulted to be used at checkout, and when a discount code is used the affiliate ID will be set.</p> | |
<p><label for="pmpro_ldc2aff_affiliate">Affiliate ID:</label><input type="text" id="pmpro_ldc2aff_affiliate" name="pmpro_ldc2aff_affiliate" value="<?php echo esc_attr($affiliate);?>" /></p> | |
<hr /> | |
<?php | |
} | |
add_action('pmpro_discount_code_after_settings', 'pmpro_ldc2aff_pmpro_discount_code_after_settings'); | |
//save options when code is updated | |
function pmpro_ldc2aff_pmpro_save_discount_code($code_id) { | |
if($code_id < 1) | |
return; | |
$affiliate = sanitize_text_field($_REQUEST['pmpro_ldc2aff_affiliate']); | |
pmpro_ldc2aff_updateCode($code_id, $affiliate); | |
} | |
add_action('pmpro_save_discount_code', 'pmpro_ldc2aff_pmpro_save_discount_code'); | |
//delete options when code is deleted | |
function pmpro_ldc2aff_pmpro_delete_discount_code($code_id) { | |
if($code_id < 1) | |
return; | |
pmpro_ldc2aff_updateCode($code_id, false); | |
} | |
add_action('pmpro_delete_discount_code', 'pmpro_ldc2aff_pmpro_delete_discount_code'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment