PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 1.4
Tracking Code Manager v1.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 / class-TCM-manager.php
tracking-code-manager / includes Last commit date
admin 11 years ago actions.php 11 years ago class-TCM-check.php 11 years ago class-TCM-cron.php 11 years ago class-TCM-form.php 11 years ago class-TCM-language.php 11 years ago class-TCM-logger.php 11 years ago class-TCM-manager.php 11 years ago class-TCM-options.php 11 years ago class-TCM-tracking.php 11 years ago class-TCM-utils.php 11 years ago core.php 11 years ago install.php 11 years ago uninstall.php 11 years ago
class-TCM-manager.php
320 lines
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 class TCM_Manager {
7
8 public function __construct() {
9
10 }
11
12 public function exists($name) {
13 $snippets = $this->values();
14 $result = NULL;
15 if (isset($snippets[$name])) {
16 $result=$snippets[$name];
17 }
18 return $result;
19 }
20
21 //get a code snippet
22 public function get($id, $new = FALSE) {
23 global $tcm;
24
25 $snippet=$tcm->Options->getSnippet($id);
26 if (!$snippet && $new) {
27 $snippet=array();
28 $snippet['active']=1;
29 $snippet['includeEverywhereActive']=1;
30 }
31
32 $snippet=$this->sanitize($id, $snippet);
33 return $snippet;
34 }
35
36 private function sanitize($id, $snippet) {
37 global $tcm;
38 if($snippet==NULL || !is_array($snippet)) return;
39
40 $defaults=array(
41 'id'=>$id
42 , 'active'=>0
43 , 'name'=>''
44 , 'code'=>''
45 , 'position'=>TCM_POSITION_HEAD
46 , 'includeEverywhereActive'=>0
47 );
48
49 $types=$tcm->Utils->query(TCM_QUERY_POST_TYPES);
50 foreach($types as $v) {
51 $defaults['includePostsOfType_'.$v['name'].'_Active']=0;
52 $defaults['includePostsOfType_'.$v['name']]=array();
53 $defaults['exceptPostsOfType_'.$v['name'].'_Active']=0;
54 $defaults['exceptPostsOfType_'.$v['name']]=array();
55 }
56 $snippet=$tcm->Utils->parseArgs($snippet, $defaults);
57 //$snippet['includeLastPosts'] = intval($snippet['includeLastPosts']);
58
59 foreach ($snippet as $k => $v) {
60 if (stripos($k, 'active') !== FALSE) {
61 $snippet[$k]=intval($v);
62 } elseif (is_array($v)) {
63 switch ($k) {
64 /*
65 case 'includePostsTypes':
66 case 'excludePostsTypes':
67 //keys are string and not number
68 $result = $this->uarray($snippet, $k, FALSE);
69 break;
70 */
71 default:
72 //keys are number
73 $result = $this->uarray($snippet, $k, TRUE);
74 break;
75 }
76 }
77 }
78 $snippet['code']=trim($snippet['code']);
79 $snippet['position']=intval($snippet['position']);
80
81 $code=strtolower($snippet['code']);
82 $cnt=substr_count($code, '<iframe')+substr_count($code, '<script');
83 if($cnt<=0) {
84 $cnt=1;
85 }
86 $snippet['codesCount']=$cnt;
87 return $snippet;
88 }
89 private function uarray($snippet, $key, $isInteger = TRUE) {
90 $array = $snippet[$key];
91 if (!is_array($array)) {
92 $array = explode(',', $array);
93 }
94
95 if ($isInteger) {
96 for ($i = 0; $i < count($array); $i++) {
97 $array[$i] = intval($array[$i]);
98 }
99 }
100
101 $array = array_unique($array);
102 $snippet[$key] = $array;
103 return $snippet;
104 }
105
106 public function rc() {
107 global $tcm;
108 $result = 6-$this->codesCount();
109 return $result;
110 }
111
112 //add or update a snippet (html tracking code)
113 public function put($id, $snippet) {
114 global $tcm;
115
116 if ($id == '' || intval($id) <= 0) {
117 //if is a new code create a new unique id
118 $id = $this->getLastId() + 1;
119 $snippet['id'] = $id;
120 }
121 $snippet=$this->sanitize($id, $snippet);
122 $tcm->Options->setSnippet($id, $snippet);
123
124 $keys = $this->keys();
125 if (is_array($keys) && !in_array($id, $keys)) {
126 $keys[] = $id;
127 $this->keys($keys);
128 }
129 return $snippet;
130 }
131
132 //remove the id snippet
133 public function remove($id) {
134 global $tcm;
135 $tcm->Options->removeSnippet($id);
136 $keys=$this->keys();
137 $result = FALSE;
138 if (is_array($keys) && in_array($id, $keys)) {
139 $keys = array_diff($keys, array($id));
140 $this->keys($keys);
141 $result = TRUE;
142 }
143 return $result;
144 }
145
146 //verify if match with this snippet
147 private function matchSnippet($postId, $postType, $categoriesIds, $tagsIds, $prefix, $snippet) {
148 global $tcm;
149
150 $include=FALSE;
151 $postId=intval($postId);
152 if($postId>0) {
153 $what=$prefix.'PostsOfType_'.$postType;
154 if(!$include && $snippet[$what.'_Active'] && $tcm->Utils->inArray($postId, $snippet[$what])) {
155 $tcm->Logger->debug('MATCH=%s SNIPPET=%s[%s] DUE TO POST=%s OF TYPE=%s IN [%s]'
156 , $prefix, $snippet['ID'], $snippet['name'], $postId, $postType, $snippet[$what]);
157 $include=TRUE;
158 }
159 }
160
161 return $include;
162 }
163
164 public function writeCodes($position) {
165 global $tcm;
166
167 $text='';
168 switch ($position) {
169 case TCM_POSITION_HEAD:
170 $text='HEAD';
171 break;
172 case TCM_POSITION_BODY:
173 $text='BODY';
174 break;
175 case TCM_POSITION_FOOTER:
176 $text='FOOTER';
177 break;
178 }
179
180 $post=$tcm->Options->getPostShown();
181 $args=array('field'=>'code');
182 $codes=$tcm->Manager->getCodes($position, $post, $args);
183 if(is_array($codes) && count($codes)>0) {
184 echo "\n<!--BEGIN: TRACKING CODE MANAGER BY INTELLYWP.COM IN $text//-->";
185 foreach($codes as $v) {
186 echo "\n$v";
187 }
188 echo "\n<!--END: TRACKING CODE MANAGER BY INTELLYWP.COM IN $text//-->";
189 }
190 }
191
192 //from a post retrieve the html code that is needed to insert into the page code
193 public function getCodes($position, $post, $args=array()) {
194 global $tcm;
195
196 $defaults=array('metabox'=>FALSE, 'field'=>'code');
197 $args=wp_parse_args($args, $defaults);
198
199 $postId=0;
200 $postType='page';
201 $tagsIds=array();
202 $categoriesIds=array();
203 if($post) {
204 $postId = $post->ID;
205 $postType = $post->post_type;
206 }
207
208 $tcm->Options->clearSnippetsWritten();
209 $keys=$this->keys();
210 foreach ($keys as $id) {
211 $v=$this->get($id);
212 if(!$v || ($position>-1 && $v['position']!=$position) || $v['code']=='' || (!$args['metabox'] && !$v['active'])) {
213 continue;
214 }
215 if($tcm->Options->hasSnippetWritten($v)) {
216 $tcm->Logger->debug('SKIPPED SNIPPET=%s[%s] DUE TO ALREADY WRITTEN', $v['ID'], $v['name']);
217 continue;
218 }
219
220 $match=FALSE;
221 if(!$match && $v['includeEverywhereActive']) {
222 $tcm->Logger->debug('INCLUDED SNIPPET=%s[%s] DUE TO EVERYWHERE', $v['ID'], $v['name']);
223 $match=TRUE;
224 }
225 if(!$match && $postId>0 && $this->matchSnippet($postId, $postType, $categoriesIds, $tagsIds, 'include', $v)) {
226 $match=TRUE;
227 }
228
229 if($match && $postId>0) {
230 if($this->matchSnippet($postId, $postType, $categoriesIds, $tagsIds, 'except', $v)) {
231 $tcm->Logger->debug('FOUND AT LEAST ON EXCEPT TO EXCLUDE SNIPPET=%s [%s]', $v['id'], $v['name']);
232 $match=FALSE;
233 }
234 }
235
236 if ($match) {
237 $tcm->Options->pushSnippetWritten($v);
238 }
239 }
240
241 //obtain result as snippets or array of one field (tipically "id")
242 $result=$tcm->Options->getSnippetsWritten();
243 if ($args['field']!='all') {
244 $array=array();
245 foreach($result as $k=>$v) {
246 $k=$args['field'];
247 if(isset($v[$k])) {
248 $array[]=$v[$k];
249 } else {
250 $tcm->Logger->error('SNIPPET=%s [%s] WITHOUT FIELD=%s', $v['id'], $v['name'], $k);
251 }
252 }
253 $result=$array;
254 }
255 return $result;
256 }
257
258 //ottiene o salva tutte le chiavi dei tracking code utilizzati ordinati per id
259 public function keys($keys=NULL) {
260 global $tcm;
261
262 if (is_array($keys)) {
263 $tcm->Options->setSnippetList($keys);
264 $result=$keys;
265 } else {
266 $result=$tcm->Options->getSnippetList();
267 }
268
269 if (!is_array($result)) {
270 $result = array();
271 } else {
272 sort($result);
273 }
274 return $result;
275 }
276
277 //ottiene il conteggio attuale dei tracking code
278 public function count() {
279 $result = count($this->keys());
280 return $result;
281 }
282 public function codesCount() {
283 $result=0;
284 $ids=$this->keys();
285 foreach($ids as $id) {
286 $snippet=$this->get($id);
287 if($snippet) {
288 if($snippet['codesCount']>0) {
289 $result+=intval($snippet['codesCount']);
290 } else {
291 $result+=1;
292 }
293 }
294 }
295 return $result;
296 }
297 public function getLastId() {
298 $result = 0;
299 $list = $this->keys();
300 foreach ($list as $v) {
301 $v = intval($v);
302 if ($v > $result) {
303 $result = $v;
304 }
305 }
306 return $result;
307 }
308
309 //ottiene tutti i tracking code ordinati per nome
310 public function values() {
311 $keys = $this->keys();
312 $result = array();
313 foreach ($keys as $k) {
314 $v = $this->get($k);
315 $result[strtoupper($v['name'])] = $v;
316 }
317 ksort($result);
318 return $result;
319 }
320 }