Ejemplo n.º 1
0
/**
 *  Load the frontend scripts and styles
 *  
 *  @since 1.0
 *  @return void
 */
function affwp_frontend_scripts_and_styles()
{
    global $post;
    if (!is_object($post)) {
        return;
    }
    if (has_shortcode($post->post_content, 'affiliate_area') || apply_filters('affwp_force_frontend_scripts', false)) {
        $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
        wp_enqueue_script('affwp-frontend', AFFILIATEWP_PLUGIN_URL . 'assets/js/frontend' . $suffix . '.js', array('jquery'), AFFILIATEWP_VERSION);
        wp_localize_script('affwp-frontend', 'affwp_vars', array('affwp_version' => AFFILIATEWP_VERSION, 'permalinks' => get_option('permalink_structure'), 'pretty_affiliate_urls' => affwp_is_pretty_referral_urls(), 'currency_sign' => affwp_currency_filter(''), 'currency_pos' => affiliate_wp()->settings->get('currency_position', 'before')));
        wp_enqueue_style('affwp-forms', AFFILIATEWP_PLUGIN_URL . 'assets/css/forms' . $suffix . '.css', AFFILIATEWP_VERSION);
        wp_enqueue_style('dashicons');
    }
}
/**
 * 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;
}