PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 2.0.4
Tracking Code Manager v2.0.4
trunk 1.11.8 1.11.9 1.12.0 1.12.1 1.12.2 1.12.3 1.4 1.5 2.0.0 2.0.1 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0
tracking-code-manager / includes / classes / core / Manager.php
tracking-code-manager / includes / classes / core Last commit date
Manager.php 4 years ago Singleton.php 4 years ago
Manager.php
658 lines
1 <?php
2 if (!defined('ABSPATH')) exit;
3
4 class TCMP_Manager {
5 public function __construct() {
6 }
7 public function init() {
8 add_action('wp_ajax_TCMP_changeOrder', array(&$this, 'changeOrder'));
9 }
10 public function isLimitReached($notice=TRUE) {
11 global $tcmp;
12 $cnt=$this->codesCount();
13 $result=($cnt>=TCMP_SNIPPETS_LIMIT);
14 if ($result && $notice) {
15 $tcmp->Options->pushWarningMessage('SnippetsLimitReached', TCMP_SNIPPETS_LIMIT, TCMP_PAGE_PREMIUM);
16 } elseif($notice && $cnt>0) {
17 $tcmp->Options->pushSuccessMessage('SnippetsLimitNotice', $cnt, TCMP_SNIPPETS_LIMIT, TCMP_PAGE_PREMIUM);
18 }
19 return $result;
20 }
21 public function changeOrder() {
22 global $tcmp;
23 if(!isset($_POST['order'])) {
24 return;
25 }
26
27 $data=array();
28 parse_str(TCMP_SQS('order'), $data);
29
30 if (isset($data['row'])) {
31 $snippets=$this->values();
32 foreach($snippets as $id=>$v) {
33 $v['order']=0;
34 $snippets[$id]=$v;
35 }
36
37 $index=1;
38 foreach($data['row'] as $order=>$id) {
39 $v=$snippets[$id];
40 $v['order']=$index;
41 $snippets[$id]=$v;
42 ++$index;
43 }
44
45 foreach($snippets as $id=>$v) {
46 $this->put($id, $v);
47 }
48 }
49 echo 'OK';
50 wp_die();
51 }
52
53 public function matchDeviceType($snippet) {
54 global $tcmp;
55 $deviceType=$tcmp->Utils->get($snippet, 'deviceType', FALSE);
56 $deviceType=$tcmp->Utils->toArray($deviceType);
57 if($deviceType===FALSE || count($deviceType)==0) {
58 return TRUE;
59 }
60
61 $detect=new TCMP_Mobile_Detect();
62 if ($detect->isMobile()) {
63 $type=TCMP_DEVICE_TYPE_MOBILE;
64 } elseif($detect->isTablet()){
65 $type=TCMP_DEVICE_TYPE_TABLET;
66 } else { //if(!$detect->isMobile() && !$detect->isTablet()) {
67 $type=TCMP_DEVICE_TYPE_DESKTOP;
68 }
69
70 $result=FALSE;
71 if(in_array(TCMP_DEVICE_TYPE_ALL, $deviceType) || in_array($type, $deviceType)) {
72 $result=TRUE;
73 }
74 return $result;
75 }
76 public function isModeScript($snippet) {
77 global $tcmp;
78 $result=$tcmp->Utils->iget($snippet, 'trackMode', 0);
79 return ($result==0);
80 }
81 public function isModeConversion($snippet) {
82 global $tcmp;
83 $result=$tcmp->Utils->iget($snippet, 'trackMode', 0);
84 return ($result!=0);
85 }
86 public function isPageEverywhere($snippet) {
87 global $tcmp;
88 if(!$this->isModeScript($snippet)) {
89 return FALSE;
90 }
91
92 $result=$tcmp->Utils->iget($snippet, 'trackPage', 0);
93 return ($result==TCMP_TRACK_PAGE_ALL);
94 }
95 public function isPageSpecific($snippet) {
96 global $tcmp;
97 if(!$this->isModeScript($snippet)) {
98 return FALSE;
99 }
100
101 $result=$tcmp->Utils->iget($snippet, 'trackPage', 0);
102 return ($result==TCMP_TRACK_PAGE_SPECIFIC);
103 }
104
105 public function exists($name) {
106 $snippets=$this->values();
107 $result=NULL;
108 $name=strtoupper($name);
109 if (isset($snippets[$name])) {
110 $result=$snippets[$name];
111 }
112 return $result;
113 }
114
115 //get a code snippet
116 public function get($id, $new=FALSE) {
117 global $tcmp;
118
119 $snippet=$tcmp->Options->getSnippet($id);
120 if (!$snippet && $new) {
121 $snippet=array();
122 $snippet['active']=1;
123 $snippet['trackMode']=-1;
124 $snippet['trackPage']=-1;
125 }
126
127 $snippet=$this->sanitize($id, $snippet);
128 return $snippet;
129 }
130
131 public function sanitize($id, $snippet) {
132 global $tcmp;
133 if($snippet==NULL || !is_array($snippet)) return NULL;
134
135 $page=0;
136 if(isset($snippet['includeEverywhereActive'])) {
137 $page=(intval($snippet['includeEverywhereActive']==1) ? 0 : 1);
138 }
139 $defaults=array(
140 'id'=>$id
141 , 'active'=>0
142 , 'name'=>''
143 , 'code'=>''
144 , 'order'=>1000
145 , 'position'=>TCMP_POSITION_HEAD
146 , 'trackMode'=>TCMP_TRACK_MODE_CODE
147 , 'trackPage'=>$page
148 , 'includeEverywhereActive'=>0
149 , 'includeCategoriesActive'=>0
150 , 'includeCategories'=>array()
151 , 'includeTagsActive'=>0
152 , 'includeTags'=>array()
153 , 'exceptCategoriesActive'=>0
154 , 'exceptCategories'=>array()
155 , 'exceptTagsActive'=>0
156 , 'exceptTags'=>array()
157 , 'deviceType'=>TCMP_DEVICE_TYPE_ALL
158 );
159
160 $types=$tcmp->Utils->query(TCMP_QUERY_POST_TYPES);
161 foreach($types as $v) {
162 $defaults['includePostsOfType_'.$v['id'].'_Active']=0;
163 $defaults['includePostsOfType_'.$v['id']]=array();
164 $defaults['exceptPostsOfType_'.$v['id'].'_Active']=0;
165 $defaults['exceptPostsOfType_'.$v['id']]=array();
166 }
167
168 $types=$tcmp->Utils->query(TCMP_QUERY_CONVERSION_PLUGINS);
169 foreach($types as $v) {
170 //CP stands for ConversionTrackingCode
171 //$defaults['CTC_'.$v['id'].'_Active']=0;
172 $defaults['CTC_'.$v['id'].'_ProductsIds']=array();
173 $defaults['CTC_'.$v['id'].'_CategoriesIds']=array();
174 $defaults['CTC_'.$v['id'].'_TagsIds']=array();
175 }
176 $snippet=$tcmp->Utils->parseArgs($snippet, $defaults);
177
178 foreach ($snippet as $k => $v) {
179 if (stripos($k, 'active') !== FALSE) {
180 $snippet[$k]=intval($v);
181 } elseif (is_array($v)) {
182 switch ($k) {
183 /*
184 case 'includePostsTypes':
185 case 'excludePostsTypes':
186 //keys are string and not number
187 $result=$this->uarray($snippet, $k, FALSE);
188 break;
189 */
190 default:
191 //keys are number
192 $result=$this->uarray($snippet, $k, TRUE);
193 break;
194 }
195 }
196 }
197 $snippet['code']=trim($snippet['code']);
198 $snippet['position']=intval($snippet['position']);
199 if($snippet['trackMode']==='') {
200 $snippet['trackMode']=TCMP_TRACK_MODE_CODE;
201 } else {
202 $snippet['trackMode']=intval($snippet['trackMode']);
203 }
204 if($snippet['trackPage']==='') {
205 $snippet['trackPage']=$page;
206 } else {
207 $snippet['trackPage']=intval($snippet['trackPage']);
208 }
209
210 $snippet['includeEverywhereActive']=0;
211 if($snippet['trackPage']==TCMP_TRACK_PAGE_ALL) {
212 $snippet['includeEverywhereActive']=1;
213 }
214
215 $code=strtolower($snippet['code']);
216 $cnt=substr_count($code, '<iframe')+substr_count($code, '<script');
217 if($cnt<=0) {
218 $cnt=1;
219 }
220 $snippet['codesCount']=$cnt;
221 return $snippet;
222 }
223 private function uarray($snippet, $key, $isInteger=TRUE) {
224 $array=$snippet[$key];
225 if (!is_array($array)) {
226 $array=explode(',', $array);
227 }
228
229 if ($isInteger) {
230 for ($i=0; $i < count($array); $i++) {
231 $array[$i]=intval($array[$i]);
232 }
233 }
234
235 $array=array_unique($array);
236 $snippet[$key]=$array;
237 return $snippet;
238 }
239
240 //add or update a snippet (html tracking code)
241 public function put($id, $snippet) {
242 global $tcmp;
243
244 if ($id == '' || intval($id) <= 0) {
245 //if is a new code create a new unique id
246 $id=$this->getLastId() + 1;
247 $snippet['id']=$id;
248 }
249 $snippet=$this->sanitize($id, $snippet);
250 $tcmp->Options->setSnippet($id, $snippet);
251
252 $keys=$this->keys();
253 if (is_array($keys) && !in_array($id, $keys)) {
254 $keys[]=$id;
255 $this->keys($keys);
256 }
257 return $snippet;
258 }
259
260 //remove the id snippet
261 public function remove($id) {
262 global $tcmp;
263 $tcmp->Options->removeSnippet($id);
264 $keys=$this->keys();
265 $result=FALSE;
266 if (is_array($keys) && in_array($id, $keys)) {
267 $keys=array_diff($keys, array($id));
268 $this->keys($keys);
269 $result=TRUE;
270 }
271 return $result;
272 }
273
274 //verify if match with this snippet
275 private function matchSnippet($postId, $postType, $categoriesIds, $tagsIds, $prefix, $snippet) {
276 global $tcmp;
277 if(!$this->matchDeviceType($snippet)) {
278 return FALSE;
279 }
280
281 $include=FALSE;
282 $postId=intval($postId);
283 if($postId>0) {
284 $what=$prefix.'PostsOfType_'.$postType;
285 if(!$include && isset($snippet[$what.'_Active']) && isset($snippet[$what])&& $snippet[$what.'_Active'] && $tcmp->Utils->inAllArray($postId, $snippet[$what])) {
286 $tcmp->Log->debug('MATCH=%s SNIPPET=%s[%s] DUE TO POST=%s OF TYPE=%s IN [%s]'
287 , $prefix, $snippet['id'], $snippet['name'], $postId, $postType, $snippet[$what]);
288 $include=TRUE;
289 }
290 }
291
292 return $include;
293 }
294
295 public function writeCodes($position) {
296 global $tcmp;
297
298 $text='';
299 switch ($position) {
300 case TCMP_POSITION_HEAD:
301 $text='HEAD';
302 break;
303 case TCMP_POSITION_BODY:
304 $text='BODY';
305 break;
306 case TCMP_POSITION_FOOTER:
307 $text='FOOTER';
308 break;
309 case TCMP_POSITION_CONVERSION:
310 $text='CONVERSION';
311 break;
312 }
313
314 $post=$tcmp->Options->getPostShown();
315 $args=array('field'=>'code');
316 $codes=$tcmp->Manager->getCodes($position, $post, $args);
317 if(is_array($codes) && count($codes)>0) {
318 ob_start();
319 echo "\n<!--BEGIN: TRACKING CODE MANAGER BY INTELLYWP.COM IN $text//-->";
320 foreach($codes as $v) {
321 echo "\n$v";
322 }
323 echo "\n<!--END: https://wordpress.org/plugins/tracking-code-manager IN $text//-->";
324 $text=ob_get_contents();
325 ob_end_clean();
326
327 $purchase=$tcmp->Options->getEcommercePurchase();
328 if($purchase!==FALSE && intval($tcmp->Options->getLicenseSiteCount())>0) {
329 //retrieve user data
330 $purchase->userId=intval($purchase->userId);
331 if($purchase->userId>0) {
332 $user=get_user_by('id', $purchase->userId);
333 if(!is_null($user) && $user!==FALSE && get_class($user)=='WP_User') {
334 /* @var $user WP_User */
335 $purchase->email=$user->user_email;
336 $purchase->fullname=$user->user_firstname;
337 if($user->user_lastname!='') {
338 $purchase->fullname.=' '.$user->user_lastname;
339 }
340 }
341 }
342
343 $purchase->total=floatval($purchase->total);
344 $purchase->amount=floatval($purchase->amount);
345 $purchase->tax=floatval($purchase->tax);
346
347 $fields=array(
348 'ORDERID'=>$purchase->orderId
349 , 'CURRENCY'=>$purchase->currency
350 , 'FULLNAME'=>$purchase->fullname
351 , 'EMAIL'=>$purchase->email
352 , 'PRODUCTS'=>$purchase->products
353 , 'AMOUNT'=>$purchase->amount
354 , 'TOTAL'=>$purchase->total
355 , 'TAX'=>$purchase->tax
356 );
357
358 $sep='@@';
359 $buffer='';
360 $previous=0;
361 $start=strpos($text, $sep);
362 if($start===FALSE) {
363 $buffer=$text;
364 } else {
365 while($start!==FALSE) {
366 $buffer.=$tcmp->Utils->substr($text, $previous, $start);
367 $end=strpos($text, $sep, $start+strlen($sep));
368 if($end!==FALSE) {
369 $code=$tcmp->Utils->substr($text, $start+strlen($sep), $end);
370 $code=$tcmp->Utils->toArray($code);
371 if(count($code)==1) {
372 $code[]='';
373 }
374
375 $v=FALSE;
376 if(isset($fields[$code[0]])) {
377 $v=$fields[$code[0]];
378 }
379 if(is_null($v) || $v===FALSE) {
380 $v=$code[1];
381 }
382 if(is_numeric($v)) {
383 $v=floatval($v);
384 $v=round($v, 2);
385 switch ($code[0]) {
386 case 'TOTAL':
387 case 'AMOUNT':
388 case 'TAX':
389 $v=number_format($v, 2, '.', '');
390 break;
391 default:
392 $v=intval($v);
393 break;
394 }
395 } elseif(is_array($v)) {
396 $a='';
397 foreach($v as $t) {
398 $t=str_replace(',', '', $t);
399 if($a!='') {
400 $a.=',';
401 }
402 $a.=$t;
403 }
404 $v=$a;
405 }
406 $v=str_replace("'", '', $v);
407 $v=str_replace('"', '', $v);
408 $buffer.=$v;
409
410 $previous=$end+strlen($sep);
411 $start=strpos($text, $sep, $previous);
412 } else {
413 $buffer.=$tcmp->Utils->substr($text, $start);
414 $previous=FALSE;
415 $start=FALSE;
416 }
417 }
418
419 if ($previous !== FALSE && $previous < strlen($text)) {
420 $code = $tcmp->Utils->substr($text, $previous);
421 $buffer .= $code;
422 }
423 }
424 $text=$buffer;
425 }
426 echo $text;
427 }
428 }
429
430 //return snippets that match with options
431 public function getConversionSnippets($options=NULL) {
432 global $tcmp;
433
434 $defaults=array(
435 'pluginId'=>0
436 , 'categoriesIds'=>array()
437 , 'productsIds'=>array()
438 , 'tagsIds'=>array()
439 );
440 $options=$tcmp->Utils->parseArgs($options, $defaults);
441
442 $result=array();
443 $pluginId=intval($options['pluginId']);
444 $values=$this->values();
445
446 foreach($values as $snippet) {
447 $snippet['trackMode']=intval($snippet['trackMode']);
448 if($snippet && $snippet['trackMode']>0 && $snippet['trackMode']==$pluginId) {
449 $match=FALSE;
450
451 $match=($match || $this->matchConversion($snippet, $pluginId, 'ProductsIds', $options['productsIds']));
452 $match=($match && $this->matchDeviceType($snippet));
453 if(!$match) {
454 //no selected so..all match! :)
455 if(count($snippet['CTC_'.$pluginId.'_ProductsIds'])==0
456 && count($snippet['CTC_'.$pluginId.'_CategoriesIds'])==0
457 && count($snippet['CTC_'.$pluginId.'_TagsIds'])==0) {
458 $match=TRUE;
459 }
460 }
461
462 if($match) {
463 $result[]=$snippet;
464 }
465 }
466 }
467 return $result;
468 }
469 private function matchConversion($snippet, $pluginId, $suffix, $currentIds) {
470 global $tcmp;
471
472 $settingsIds='CTC_'.$pluginId.'_'.$suffix;
473 if(isset($snippet[$settingsIds])) {
474 $settingsIds=$snippet[$settingsIds];
475 } else {
476 $settingsIds=array();
477 }
478
479 $result=$tcmp->Utils->inAllArray($currentIds, $settingsIds);
480 return $result;
481 }
482
483 //from a post retrieve the html code that is needed to insert into the page code
484 public function getCodes($position, $post, $args=array()) {
485 global $tcmp;
486
487 $defaults=array('field'=>'code');
488 $args=$tcmp->Utils->parseArgs($args, $defaults);
489
490 $postId=0;
491 $postType='page';
492 $tagsIds=array();
493 $categoriesIds=array();
494 if($post) {
495 $postId=$tcmp->Utils->get($post, 'ID', FALSE);
496 if($postId===FALSE) {
497 $postId=$tcmp->Utils->get($post, 'post_ID');
498 }
499 $postType=$tcmp->Utils->get($post, 'post_type');
500
501 $options=array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'ids');
502 if(isset($post->ID)) {
503 $tagsIds=wp_get_post_tags($post->ID, $options);
504 $categoriesIds=wp_get_post_categories($post->ID);
505 } else {
506 $tagsIds=array();
507 $categoriesIds=array();
508 }
509 }
510
511 $tcmp->Options->clearSnippetsWritten();
512 if($position==TCMP_POSITION_CONVERSION) {
513 //write snippets previously appended
514 $ids=$tcmp->Options->getConversionSnippetIds();
515 if($ids!==FALSE && count($ids)>0) {
516 foreach($ids as $id) {
517 $snippet=$tcmp->Manager->get($id);
518 if($snippet) {
519 $tcmp->Options->pushSnippetWritten($snippet);
520 }
521 }
522 }
523 } else {
524 $snippets=$this->values();
525 foreach ($snippets as $v) {
526 if(!$v || ($position>-1 && $v['position']!=$position) || $v['code']=='' || !$v['active']) {
527 continue;
528 }
529 if ($v['trackMode']!=TCMP_TRACK_MODE_CODE) {
530 continue;
531 }
532 if($tcmp->Options->hasSnippetWritten($v)) {
533 $tcmp->Log->debug('SKIPPED SNIPPET=%s[%s] DUE TO ALREADY WRITTEN', $v['id'], $v['name']);
534 continue;
535 }
536
537 $match=FALSE;
538 if (!$match && ($v['trackPage']==TCMP_TRACK_PAGE_ALL || $v['includeEverywhereActive'])) {
539 $tcmp->Log->debug('INCLUDED SNIPPET=%s[%s] DUE TO EVERYWHERE', $v['id'], $v['name']);
540 $match=TRUE;
541 }
542 if(!$match && $postId>0 && $this->matchSnippet($postId, $postType, $categoriesIds, $tagsIds, 'include', $v)) {
543 $match=TRUE;
544 }
545
546 if($match && $postId>0) {
547 if($this->matchSnippet($postId, $postType, $categoriesIds, $tagsIds, 'except', $v)) {
548 $tcmp->Log->debug('FOUND AT LEAST ON EXCEPT TO EXCLUDE SNIPPET=%s [%s]', $v['id'], $v['name']);
549 $match=FALSE;
550 }
551 }
552
553 if ($match) {
554 $tcmp->Options->pushSnippetWritten($v);
555 }
556 }
557 }
558
559 //obtain result as snippets or array of one field (tipically "id")
560 $result=$tcmp->Options->getSnippetsWritten();
561 if ($args['field']!='all') {
562 $array=array();
563 foreach($result as $k=>$v) {
564 $k=$args['field'];
565 if(isset($v[$k])) {
566 $array[]=$v[$k];
567 } else {
568 $tcmp->Log->error('SNIPPET=%s [%s] WITHOUT FIELD=%s', $v['id'], $v['name'], $k);
569 }
570 }
571 $result=$array;
572 }
573 return $result;
574 }
575
576 //ottiene o salva tutte le chiavi dei tracking code utilizzati ordinati per id
577 public function keys($keys=NULL) {
578 global $tcmp;
579
580 if (is_array($keys)) {
581 $tcmp->Options->setSnippetList($keys);
582 $result=$keys;
583 } else {
584 $result=$tcmp->Options->getSnippetList();
585 }
586
587 if (!is_array($result)) {
588 $result=array();
589 } else {
590 sort($result);
591 }
592 return $result;
593 }
594
595 //ottiene il conteggio attuale dei tracking code
596 public function count() {
597 $result=count($this->keys());
598 return $result;
599 }
600 public function codesCount() {
601 $result=0;
602 $ids=$this->keys();
603 foreach($ids as $id) {
604 $snippet=$this->get($id);
605 if($snippet) {
606 $result+=1;
607 /*
608 if($snippet['codesCount']>0) {
609 $result+=intval($snippet['codesCount']);
610 } else {
611 $result+=1;
612 }
613 */
614 }
615 }
616 return $result;
617 }
618 public function getLastId() {
619 $result=0;
620 $list=$this->keys();
621 foreach ($list as $v) {
622 $v=intval($v);
623 if ($v>$result) {
624 $result=$v;
625 }
626 }
627 return $result;
628 }
629
630 public function values() {
631 $keys=$this->keys();
632 $array=array();
633 foreach ($keys as $k) {
634 $v=$this->get($k);
635 $array[]=$v;
636 }
637 usort($array, array($this, 'values_Compare'));
638
639 $result=array();
640 foreach($array as $v) {
641 $id=$v['id'];
642 $result[$id]=$v;
643 }
644 return $result;
645 }
646 public function values_Compare($o1, $o2) {
647 global $tcmp;
648 $v1=$tcmp->Utils->iget($o1, 'order', FALSE);
649 $v2=$tcmp->Utils->iget($o2, 'order', FALSE);
650 $result=($v1-$v2);
651 if($result==0) {
652 $v1=$tcmp->Utils->get($o1, 'name', FALSE);
653 $v2=$tcmp->Utils->get($o2, 'name', FALSE);
654 $result=strcasecmp($v1, $v2);
655 }
656 return $result;
657 }
658 }