[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-content/add-ons/ -> MailPress_tracking_ga.php (source)

   1  <?php
   2  if (class_exists('MailPress') && !class_exists('MailPress_tracking_ga'))
   3  {
   4  /*
   5  Plugin Name: MailPress_tracking_ga
   6  Plugin URI: http://blog.mailpress.org/tutorials/add-ons/tracking_ga/
   7  Description: Tracking : mails activity to your site with <a href='http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&amp;answer=55540'>google analytics</a> (<span style='color:red;'>beware !</span> Not compatible with <span style="color:#D54E21;">Tracking</span> add-on)
   8  Version: 5.4.4
   9  */
  10  
  11  class MailPress_tracking_ga
  12  {
  13  	function __construct()
  14      {
  15          if (class_exists('MailPress_tracking')) return;
  16  // prepare mail
  17          add_filter('MailPress_mail',    array(__CLASS__, 'mail'), 8, 2);
  18      }
  19  
  20  // prepare mail
  21  
  22  	public static function mail($mail)
  23      {
  24          $utms = array(    'utm_source'     =>     'mailpress',
  25                      'utm_medium'     =>     'email_link',
  26          //            'utm_term'         =>     '',
  27                      'utm_content'     =>     "{$mail->theme}_{$mail->template}_{$mail->id}",
  28                      'utm_campaign'     =>     date('c'),
  29          );
  30          $args = (isset($mail->utms) && is_array($mail->utms)) ? $mail->utms : $utms;
  31  
  32          $siteurl = site_url();
  33          $home    = home_url();
  34  
  35  
  36          $output = preg_match_all('/<a [^>]*href=[\'"]([^\'"]+)[\'"][^>]*>(.*?)<\/a>/is', $mail->html, $matches, PREG_SET_ORDER);
  37  
  38          $hrefs_txt = array();
  39          if ($matches)
  40          {
  41              foreach ($matches as $match)
  42              {
  43                  if (strpos($match[1], 'mailto:') !== false) continue;
  44                  if (strpos($match[1], $siteurl) === false && strpos($match[1], $home) === false) continue;
  45  
  46                  $t_url  = $match[1];
  47  
  48                  /* Strip any anchor reference off */
  49                  $anchor     = '';
  50                  $hash_pos     = strrpos($match[1], '#');
  51  
  52                  if ($hash_pos !== false)
  53                  {
  54                      $t_url  = substr($match[1], 0, $hash_pos);
  55                      $anchor = substr($match[1], $hash_pos);
  56                    }
  57  
  58                  $sep = (strpos($t_url, '?')) ? '&' : '?';
  59                  foreach ($args as $k => $v) {$t_url .= "{$sep}{$k}={$v}"; $sep = '&';}
  60  
  61                  $t_url .= $anchor;
  62  
  63                  $link = self::str_replace_count($match[1], $t_url, $match[0], 1);
  64                  $mail->html = str_replace($match[0], $link, $mail->html);
  65  
  66                  $hrefs_txt[$match[1]] = $t_url;
  67              }
  68          }
  69  
  70          if (!empty($hrefs_txt))
  71          {
  72              uksort($hrefs_txt, create_function('$a, $b', 'return strcmp(strlen($a), strlen($b));'));
  73              $hrefs_txt = array_reverse($hrefs_txt);
  74              $mail->plaintext = str_replace(array_keys($hrefs_txt), $hrefs_txt, $mail->plaintext);
  75          }
  76          return $mail;
  77      }
  78  
  79  	public static function str_replace_count($search, $replace, $subject, $times=1) 
  80      {
  81          $subject_original=$subject;
  82  
  83          $len=strlen($search);
  84          $pos=0;
  85          for ($i=1;$i<=$times;$i++) 
  86          {
  87              $pos=strpos($subject, $search, $pos);
  88              if($pos!==false) 
  89              {
  90                  $subject=substr($subject_original, 0, $pos);
  91                  $subject.=$replace;
  92                  $subject.=substr($subject_original, $pos+$len);
  93                  $subject_original=$subject;
  94              }
  95              else
  96              {
  97                  break;
  98              }
  99          }
 100          return($subject);
 101      }
 102  }
 103  new MailPress_tracking_ga();
 104  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1