# content
<?php /*







Affiliate Link Mapping



======================







Map affiliate links from the main landing page to individual affiliate links



for specific stores.







Usage



-----







Define your affiliate link mapping in the `$AFFILIATES` array. The top-level



keys are your own affiliate links you make up for your main site. Each



is an array listing out the query string parameters for the other stores



affiliate programs.







```



$AFFILIATES = [



  'user1' => [



    'store1' => ['p' => '1'],



    'store2' => ['aff' => 'y2'],



    'store3' => ['aff' => '1'],



    'store4' => ['tag' => 'hello-20'],



  ],



  'user2' => [



    'store3' => ['aff' => 'y3'],



  ],



  'user3' => [



    'store1' => ['p' => '3', 'q' => '4'],



  ],



  'user4' => [



    'store2' => ['aff' => 'y8'],



    'store3' => ['aff' => '4'],



  ],



];



```







In this example, we create 4 new affiliate codes `user1`, `user2`, etc.



There are also 4 stores defined, each with their own different set of



affiliate link parameters.







The links from your main website, e.g. `https://example.com/?ref=user1`



should be turned into links to the individual stores with different



parameters such as: `https://store1.example/?p=1` and



`https://store2.example/?aff=y2` etc.







When you want to display a link to one of the products, call the



function `affiliate_link()` with the normal URL to the store and



the name of the store, e.g.







`<?= affiliate_link('https://store1.example/', 'store1'); ?>`







The function will add the appropriate query string parameters to



the store1 link based on the referral code in the URL if present.







Note: If you want to persist this across pageloads, you'll need to



modify the function to store the main affiliate code in the session.











*/







$AFFILIATES = [



  'user1' => [



    'store1' => ['p' => '1'],



    'store2' => ['aff' => 'y2'],



    'store3' => ['aff' => '1'],



    'store4' => ['tag' => 'hello-20'],



  ],



  'user2' => [



    'store3' => ['aff' => 'y3'],



  ],



  'user3' => [



    'store1' => ['p' => '3', 'q' => '4'],



  ],



  'user4' => [



    'store2' => ['aff' => 'y8'],



    'store3' => ['aff' => '4'],



  ],



];







function affiliate_link($link, $store) {



  global $AFFILIATES;







  // Find current affiliate from query string



  if(isset($_GET['ref'])) {



    $affiliate = $_GET['ref'];



  } else {



    // default to the first affiliate defined



    $affiliate = $AFFILIATES[];



  }







  // Check if that affiliate has a tracking code for the given store



  if(isset($AFFILIATES[$affiliate][$store]))



    $params = $AFFILIATES[$affiliate][$store];



  else



    $params = [];







  // Add tracking code to the store link



  return add_query_params_to_url($link, $params);



}











function add_query_params_to_url($url, $add_params) {



  $parts = parse_url($url);



  if(array_key_exists('query', $parts) && $parts['query']) {



    parse_str($parts['query'], $params);



  } else {



    $params = [];



  }







  foreach($add_params as $k=>$v) {



    $params[$k] = $v;



  }







  $parts['query'] = http_build_query($params);







  return build_url($parts);



}







// Inverse of parse_url()



// http://php.net/parse_url



function build_url($parsed_url) {



  $scheme   = !empty($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';



  $host     = !empty($parsed_url['host']) ? $parsed_url['host'] : '';



  $port     = !empty($parsed_url['port']) ? ':' . $parsed_url['port'] : '';



  $user     = !empty($parsed_url['user']) ? $parsed_url['user'] : '';



  $pass     = !empty($parsed_url['pass']) ? ':' . $parsed_url['pass']  : '';



  $pass     = ($user || $pass) ? "$pass@" : '';



  $path     = !empty($parsed_url['path']) ? $parsed_url['path'] : '';



  $query    = !empty($parsed_url['query']) ? '?' . $parsed_url['query'] : '';



  $fragment = !empty($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';



  return "$scheme$user$pass$host$port$path$query$fragment";



}