/**
 * Builds an affiliate's referral URL
 * Used by creatives, referral URL generator and [affiliate_referral_url] shortcode
 *
 * @since  1.6
 * @return string
 * @param  $args array of arguments. $base_url, $format, $pretty
 */
function affwp_get_affiliate_referral_url($args = array())
{
    $defaults = array('pretty' => '', 'format' => '');
    $args = wp_parse_args($args, $defaults);
    // get affiliate ID if passed in
    $affiliate_id = isset($args['affiliate_id']) ? $args['affiliate_id'] : '';
    // get format, username or id
    $format = isset($args['format']) ? $args['format'] : affwp_get_referral_format();
    // pretty URLs
    if (!empty($args['pretty']) && 'yes' == $args['pretty']) {
        // pretty URLS explicitly turned on
        $pretty = true;
    } elseif (!empty($args['pretty']) && 'no' == $args['pretty'] || false === $args['pretty']) {
        // pretty URLS explicitly turned off
        $pretty = false;
    } else {
        // pretty URLs set from admin
        $pretty = affwp_is_pretty_referral_urls();
    }
    // get base URL
    if (isset($args['base_url'])) {
        $base_url = $args['base_url'];
    } else {
        $base_url = affwp_get_affiliate_base_url();
    }
    // add trailing slash only if no query string exists and there's no fragment identifier
    if (isset($args['base_url']) && !array_key_exists('query', parse_url($base_url)) && !array_key_exists('fragment', parse_url($base_url))) {
        $base_url = trailingslashit($args['base_url']);
    }
    // the format value, either affiliate's ID or username
    $format_value = affwp_get_referral_format_value($format, $affiliate_id);
    $url_parts = parse_url($base_url);
    // if fragment identifier exists in base URL, strip it and store in variable so we can append it later
    $fragment = array_key_exists('fragment', $url_parts) ? '#' . $url_parts['fragment'] : '';
    // if query exists in base URL, strip it and store in variable so we can append to the end of the URL
    $query_string = array_key_exists('query', $url_parts) ? '?' . $url_parts['query'] : '';
    $url_scheme = isset($url_parts['scheme']) ? $url_parts['scheme'] : 'http';
    $url_host = isset($url_parts['host']) ? $url_parts['host'] : '';
    $constructed_url = $url_scheme . '://' . $url_host . $url_parts['path'];
    $base_url = $constructed_url;
    // set up URLs
    $pretty_urls = trailingslashit($base_url) . trailingslashit(affiliate_wp()->tracking->get_referral_var()) . trailingslashit($format_value) . $query_string . $fragment;
    $non_pretty_urls = esc_url(add_query_arg(affiliate_wp()->tracking->get_referral_var(), $format_value, $base_url . $query_string . $fragment));
    if ($pretty) {
        $referral_url = $pretty_urls;
    } else {
        $referral_url = $non_pretty_urls;
    }
    return $referral_url;
}
<div id="affwp-affiliate-dashboard-url-generator" class="affwp-tab-content">

	<h4><?php 
_e('Referral URL Generator', 'affiliate-wp');
?>
</h4>

	<?php 
if ('id' == affwp_get_referral_format()) {
    ?>
		<p><?php 
    printf(__('Your affiliate ID is: <strong>%s</strong>', 'affiliate-wp'), affwp_get_affiliate_id());
    ?>
</p>
	<?php 
} elseif ('username' == affwp_get_referral_format()) {
    ?>
		<p><?php 
    printf(__('Your affiliate username is: <strong>%s</strong>', 'affiliate-wp'), affwp_get_affiliate_username());
    ?>
</p>
	<?php 
}
?>

	<p><?php 
printf(__('Your referral URL is: <strong>%s</strong>', 'affiliate-wp'), esc_url(urldecode(affwp_get_affiliate_referral_url())));
?>
</p>
	<p><?php 
_e('Enter any URL from this website in the form below to generate a referral link!', 'affiliate-wp');
Example #3
0
/**
 * Get the referral format value
 *
 * @since 1.6
 * @param string $format referral format passed in via [affiliate_referral_url] shortcode
 * @return string affiliate ID or username
 */
function affwp_get_referral_format_value($format = '', $affiliate_id = 0)
{
    // get affiliate's user ID
    $user_id = affwp_get_affiliate_user_id($affiliate_id);
    if (!$format) {
        $format = affwp_get_referral_format();
    }
    switch ($format) {
        case 'username':
            $value = urlencode(affwp_get_affiliate_username($affiliate_id));
            break;
        case 'id':
        default:
            $value = affwp_get_affiliate_id($user_id);
            break;
    }
    return apply_filters('affwp_get_referral_format_value', $value, $format, $affiliate_id);
}
/**
 * Get the referral format value
 *
 * @since 1.6
 * @param string $format referral format passed in via [affiliate_referral_url] shortcode
 * @return string affiliate ID or username
 */
function affwp_get_referral_format_value($format = '', $affiliate_id = 0)
{
    // get affiliate's user ID
    $user_id = affwp_get_affiliate_user_id($affiliate_id);
    if (!$format) {
        $format = affwp_get_referral_format();
    }
    switch ($format) {
        case 'username':
            $value = affwp_get_affiliate_username($affiliate_id);
            break;
        case 'id':
        default:
            $value = affwp_get_affiliate_id($user_id);
            break;
    }
    return $value;
}