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