Skip to content

Instantly share code, notes, and snippets.

@DrewAPicture
Last active June 29, 2020 04:45
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 DrewAPicture/a496434830e0e4062db5c18673587fd3 to your computer and use it in GitHub Desktop.
Save DrewAPicture/a496434830e0e4062db5c18673587fd3 to your computer and use it in GitHub Desktop.
<?php
/**
* Attempts to log a referral when a new vendor is registered via an affiliate in FES.
*
* @see The 'edd_post_insert_' . $type hook, where $type is 'vendor'.
*
* @param int. $vendor_id Newly-created vendor ID.
* @param array $data Vendor data.
*/
function affwp_edd_fes_vendor_registration_referral( $vendor_id, $data ) {
if ( ! function_exists( 'affwp_sr_is_edd_active' ) ) {
return;
}
// Bail early if integration does not exist or the user ID isn't set.
if ( ! affwp_sr_is_edd_active() || empty( $data['user_id'] ) ) {
return;
}
$args = array(
'user_id' => $data['user_id'],
'context' => 'edd'
);
// Get the referring affiliate ID from the cookie.
$referring_affiliate_id = affiliate_wp()->tracking->get_affiliate_id();
// Bail early if no referring affiliate ID
if ( ! $referring_affiliate_id ) {
return;
}
// Bail if referral type not permitted
if ( ! array_key_exists( $args['context'], affwp_sr_allowed_referral_types() ) ) {
return;
}
// User ID. Given when a user registers via WP registration form or similar
$user_id = isset( $args['user_id'] ) ? $args['user_id'] : '';
// Check if affiliate approval is enabled.
$affiliate_approval = affiliate_wp()->settings->get( 'require_approval' );
// Set amount
$amount = affiliate_wp()->settings->get( 'affwp_sr_' . $args['context'] .'_registration_referral_amount' );
// Set referral description
$referral_description = __( 'EDD FES Vendor Registration', 'affiliatewp-signup-referrals' );
// Set up the reference
$reference = $args['user_id'];
// Set status. Pending or Unpaid
$referral_status = affiliate_wp()->settings->get( 'affwp_sr_referral_status', 'pending' );
// Check if the referring affiliate is valid.
if ( affwp_is_active_affiliate( $referring_affiliate_id ) ) {
$visit_id = affiliate_wp()->tracking->get_visit_id();
$args = apply_filters( 'affwp_signup_referrals_args', array(
'affiliate_id' => $referring_affiliate_id,
'amount' => $amount,
'status' => $referral_status,
'description' => $referral_description,
'reference' => $reference,
'visit_id' => $visit_id,
'context' => $args['context'] . '_signup'
), $referring_affiliate_id, $amount, $referral_status, $referral_description, $reference, $args['context'] );
$referral_id = affwp_add_referral( $args );
// Update visit.
affiliate_wp()->visits->update( $visit_id, array( 'referral_id' => $referral_id ), '', 'visit' );
}
}
add_action( 'edd_post_insert_vendor', 'affwp_edd_fes_vendor_registration_referral', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment