Skip to content

Instantly share code, notes, and snippets.

@eighty20results
Forked from strangerstudios/pmpro_ld2aff.php
Last active October 23, 2018 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eighty20results/e9148cdc218daea0481dc54ff89c7353 to your computer and use it in GitHub Desktop.
Save eighty20results/e9148cdc218daea0481dc54ff89c7353 to your computer and use it in GitHub Desktop.
Link Paid Memberships Pro discount codes to affiliate accounts from another plugin/service.
<?php
/*
Plugin Name: E20R - Link PMPro Discount Codes to Affiliate Accounts
Plugin URI: https://eighty20results.com/paid-memberships-pro/do-it-for-me/
Description: Link a PMPro Discount Code to the account of an affiliate
Version: 1.0
Requires at least: 4.5
Tested up to: 4.9.8
Author: Eighty / 20 Results by Wicked Strong Chicks, LLC <thomas@eighty20results.com>
Author URI: https://eighty20results.com/thomas-sjolshagen/
License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
License: GPLv2 or later
Text Domain: scba-customizations
Domain Path: /languages
*/
/*
* This is the parameter we're looking for to find affiliates
* Choose the 'define()' to uncomment, based on the plugin you are using to manage your affiliates
*/
define('PMPRO_LDC2AFF_KEY', 'wpam_id'); //Affiliates Manager Plugin
// define('PMPRO_LDC2AFF_KEY', 'pa'); //WordPress Lightweight Affiliates
// define('PMPRO_LDC2AFF_KEY', 'ref'); //AffiliateWP
/**
* Get the values from options and make sure they are an array
*
* @return array
*/
function pmpro_ld2aff_getOptions() {
$d2a = get_option('pmpro_ldc2aff', array());
if(!is_array($d2a)) {
$d2a = array();
}
return $d2a;
}
/**
* Add a discount code/affiliate pair
*
* @param int $code_id
* @param string|int $affiliate
*
*/
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');
}
}
/**
* Get a discount code from an affiliate
*
* @param string $affiliate
*
* @return int|string
*/
function pmpro_ldc2aff_getCodeByAffiliate($affiliate) {
$d2a = pmpro_ld2aff_getOptions();
return array_search($affiliate, $d2a);
}
/**
* Get an affiliate id from a discount code
*
* @param int $code_id
*/
function pmpro_ldc2aff_getAffiliateByCode($code_id) {
$d2a = pmpro_ld2aff_getOptions();
if(isset($d2a[$code_id])) {
return $d2a[$code_id];
}
return "";
}
/**
* Try to sync the affiliate id and discount code
*/
function pmpro_ldc2aff_init() {
global $wpdb;
// Cancel exeuction if key isn't set
if ( ! defined( 'PMPRO_LDC2AFF_KEY' ) ) {
return;
}
// Is PMPro active?
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
//TODO: Should check if the specifed affiliate plugin is also running
// Have an affiliate id? Then set the/a discount code
if( ! empty( $_REQUEST[ PMPRO_LDC2AFF_KEY ] ) ) {
// Quit if another discount code is already set
if( ! empty($_REQUEST[ 'discount_code' ] ) ) {
return;
}
// Let's figure out which 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(
$wpdb->prepare( "SELECT dc.code FROM {$wpdb->pmpro_discount_codes} AS dc WHERE dc.id = %d LIMIT 1", $code_id )
);
$_REQUEST['discount_code'] = $code;
$_GET['discount_code'] = $code;
$_POST['discount_code'] = $code;
}
}
// Have a discount code? Set the affiliate id
if( ! empty( $_REQUEST[ 'discount_code' ] ) ) {
$code = sanitize_text_field( $_REQUEST[ 'discount_code' ] );
$code_id = $wpdb->get_var(
$wpdb->prepare( "SELECT dc.id FROM {$wpdb->pmpro_discount_codes} AS dc WHERE dc.code = %s LIMIT 1", $code )
);
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 the Edit Discont Code page
*
* @param int $code_id
*/
function pmpro_ldc2aff_pmpro_discount_code_after_settings( $code_id ) {
$affiliate = pmpro_ldc2aff_getAffiliateByCode($code_id); ?>
<hr />
<h3>Affiliate</h3>
<php
printf( '<p>Link this discount code to an affiliate. Currently monitoring the "%s" 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>', PMPRO_LDC2AFF_KEY );
printf( '<p><label for="pmpro_ldc2aff_affiliate">Affiliate ID:</label>' );
printf( '<input type="text" id="pmpro_ldc2aff_affiliate" name="pmpro_ldc2aff_affiliate" value="%s" />', esc_attr($affiliate) );
printf( '</p>' ); ?>
<hr /><?php
}
add_action('pmpro_discount_code_after_settings', 'pmpro_ldc2aff_pmpro_discount_code_after_settings');
/**
* Save the options when an affiliate discount code is updated
* @param int $code_id
*/
function pmpro_ldc2aff_pmpro_save_discount_code($code_id) {
if($code_id < 1) {
return;
}
$affiliate = isset($_REQUEST['pmpro_ldc2aff_affiliate']) ? sanitize_text_field( $_REQUEST['pmpro_ldc2aff_affiliate']) : null;
if ( !empty( $affiliate ) ) {
pmpro_ldc2aff_updateCode($code_id, $affiliate);
}
}
add_action('pmpro_save_discount_code', 'pmpro_ldc2aff_pmpro_save_discount_code');
/**
* Delete affiliate options when their discount code is deleted
*
* @param int $code_id
*/
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