PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 2.6.0
Tracking Code Manager v2.6.0
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 / utils / Options.php
tracking-code-manager / includes / classes / utils Last commit date
Cron.php 3 years ago Ecommerce.php 3 years ago Language.php 3 years ago Logger.php 3 years ago MobileDetect.php 1 year ago Options.php 1 year ago Plugin.php 3 years ago Properties.php 3 years ago Tracking.php 3 years ago Utils.php 2 years ago
Options.php
527 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 class TCMP_Options {
7
8 private $request_data;
9
10 public function __construct() {
11 $this->request_data = array();
12 }
13
14 //Cache
15 private function getCacheName( $array ) {
16 if ( ! is_array( $array ) ) {
17 $array = array( $array );
18 }
19 $result = 'Cache';
20 foreach ( $array as $v ) {
21 if ( is_object( $v ) ) {
22 $v = get_class( $v );
23 } elseif ( is_array( $v ) ) {
24 $v = $v[0];
25 if ( is_object( $v ) ) {
26 $v = get_class( $v );
27 }
28 }
29 $result .= '_' . $v;
30 }
31 return $result;
32 }
33 public function getCache( $name, $callable = null ) {
34 $key = $this->getCacheName( $name );
35 $result = $this->getRequest( $key, false );
36 if ( false === $result && $callable && is_callable( $callable ) ) {
37 $result = $callable();
38 $this->setCache( $name, $result );
39 }
40 return $result;
41 }
42 public function setCache( $name, $value ) {
43 $key = $this->getCacheName( $name );
44 $this->setRequest( $key, $value );
45 }
46
47 //always add a prefix to avoid conflicts with other plugins
48 private function get_key( $key ) {
49 return 'TCM_' . $key;
50 }
51 //option
52 private function removeOption( $key ) {
53 $key = $this->get_key( $key );
54 delete_option( $key );
55 }
56 private function getOption( $key, $default = false ) {
57 $key = $this->get_key( $key );
58 $result = get_option( $key, $default );
59 if ( is_string( $result ) ) {
60 $result = trim( $result );
61 }
62 return $result;
63 }
64 private function setOption( $key, $value ) {
65 $key = $this->get_key( $key );
66 if ( is_bool( $value ) ) {
67 $value = ( $value ? 1 : 0 );
68 }
69 update_option( $key, $value );
70 }
71
72 //$_SESSION
73 private function removeSession( $key ) {
74 global $wp_session;
75
76 $key = $this->get_key( $key );
77 if ( isset( $wp_session[ $key ] ) ) {
78 unset( $wp_session[ $key ] );
79 }
80 }
81 private function getSession( $key, $default = false ) {
82 global $wp_session;
83
84 $key = $this->get_key( $key );
85 $result = $default;
86 if ( isset( $wp_session[ $key ] ) ) {
87 $result = $wp_session[ $key ];
88 }
89 if ( is_string( $result ) ) {
90 $result = trim( $result );
91 }
92 return $result;
93 }
94 private function setSession( $key, $value ) {
95 global $wp_session;
96
97 $key = $this->get_key( $key );
98 $wp_session[ $key ] = $value;
99 }
100
101 //$_REQUEST
102 //However WP enforces its own logic - during load process wp_magic_quotes() processes variables to emulate magic quotes setting and enforces $_REQUEST to contain combination of $_GET and $_POST, no matter what PHP configuration says.
103 private function removeRequest( $key ) {
104 $key = $this->get_key( $key );
105 if ( isset( $_POST[ $key ] ) ) {
106 unset( $_POST[ $key ] );
107 }
108 }
109 private function getRequest( $key, $default = false ) {
110 $result = $default;
111 if ( isset($this->request_data[ $key ]) ) {
112 if ( is_object( $this->request_data[ $key ] ) ) {
113 $result = clone $this->request_data[ $key ];
114 } else {
115 $result = $this->request_data[ $key ];
116 }
117 $result = $this->recursive_wp_kses( $result );
118 } else {
119 $key = $this->get_key( $key );
120 if ( isset( $_POST[ $key ] ) ) {
121 if ( is_object( $_POST[ $key ] ) ) {
122 $result = clone $_POST[ $key ];
123 } else {
124 $result = $_POST[ $key ];
125 }
126 $result = $this->recursive_wp_kses( $result );
127 }
128 }
129 return $result;
130 }
131
132 public function recursive_wp_kses( $array ) {
133 global $tcmp_allowed_html_tags;
134 foreach ( $array as $key => &$value ) {
135 if ( is_array( $value ) ) {
136 $value = $this->recursive_wp_kses( $value );
137 } elseif ( 'code' === $key ) {
138 if ( ! $this->getSkipCodeSanitization() ) {
139 $value = wp_kses( $value, $tcmp_allowed_html_tags );
140 }
141 } elseif ( is_string( $value ) ) {
142 $value = wp_kses( $value, $tcmp_allowed_html_tags );
143 } else {
144 // do nothing ... could be a video or graphics object
145 }
146 }
147 return $array;
148 }
149
150 private function setRequest( $key, $value ) {
151 $this->request_data[ $key ] = $value;
152 }
153
154 public function isPluginFirstInstall() {
155 return $this->getOption( 'PluginFirstInstall', false );
156 }
157 public function setPluginFirstInstall( $value ) {
158 $this->setOption( 'PluginFirstInstall', $value );
159 }
160 public function isShowActivationNotice() {
161 return $this->getOption( 'ShowActivationNotice', false );
162 }
163 public function setShowActivationNotice( $value ) {
164 $this->setOption( 'ShowActivationNotice', $value );
165 }
166
167 public function getShowWhatsNewSeenVersion() {
168 return intval( $this->getOption( 'ShowWhatsNewSeenVersion', 0 ) );
169 }
170 public function setShowWhatsNewSeenVersion( $value ) {
171 $this->setOption( 'ShowWhatsNewSeenVersion', $value );
172 }
173
174 //ShowWhatsNew
175 public function isShowWhatsNew() {
176 $result = intval( $this->getOption( 'ShowWhatsNew', true ) );
177 if ( $result ) {
178 $v = $this->getShowWhatsNewSeenVersion();
179 if ( TCMP_WHATSNEW_VERSION == $v ) {
180 $result = false;
181 $this->getOption( 'ShowWhatsNew', false );
182 }
183 }
184 return $result;
185 }
186 public function setShowWhatsNew( $value ) {
187 $this->setOption( 'ShowWhatsNew', $value );
188 }
189
190 //TrackingEnable
191 public function isTrackingEnable() {
192 return $this->getOption( 'TrackingEnable', 0 );
193 }
194 public function setTrackingEnable( $value ) {
195 $this->setOption( 'TrackingEnable', $value );
196 }
197 //TrackingNotice
198 public function isTrackingNotice() {
199 return $this->getOption( 'TrackingNotice', 1 );
200 }
201 public function setTrackingNotice( $value ) {
202 $this->setOption( 'TrackingNotice', $value );
203 }
204
205 public function getTrackingLastSend() {
206 return $this->getOption( 'TrackingLastSend[' . TCMP_PLUGIN_SLUG . ']', 0 );
207 }
208 public function setTrackingLastSend( $value ) {
209 $this->setOption( 'TrackingLastSend[' . TCMP_PLUGIN_SLUG . ']', $value );
210 }
211 public function getPluginInstallDate() {
212 return $this->getOption( 'PluginInstallDate[' . TCMP_PLUGIN_SLUG . ']', 0 );
213 }
214 public function setPluginInstallDate( $value ) {
215 $this->setOption( 'PluginInstallDate[' . TCMP_PLUGIN_SLUG . ']', $value );
216 }
217 public function getPluginUpdateDate() {
218 return $this->getOption( 'PluginUpdateDate[' . TCMP_PLUGIN_SLUG . ']', 0 );
219 }
220 public function setPluginUpdateDate( $value ) {
221 $this->setOption( 'PluginUpdateDate[' . TCMP_PLUGIN_SLUG . ']', $value );
222 }
223
224 //LicenseKey
225 public function getLicenseKey() {
226 return $this->getOption( 'LiceseKey', '' );
227 }
228 public function setLicenseKey( $value ) {
229 $this->setOption( 'LiceseKey', $value );
230 }
231 //LicenseStatus
232 public function isLicenseSuccess() {
233 return $this->getOption( 'LicenseSuccess', 0 );
234 }
235 public function setLicenseSuccess( $value ) {
236 $this->setOption( 'LicenseSuccess', $value );
237 }
238 //License
239 public function getLicense() {
240 return $this->getOption( 'License', false );
241 }
242 public function setLicense( $value ) {
243 $this->setOption( 'License', $value );
244 }
245 //LicenseSiteCount
246 public function getLicenseSiteCount() {
247 return $this->getOption( 'LicenseSiteCount', false );
248 }
249 public function setLicenseSiteCount( $value ) {
250 $this->setOption( 'LicenseSiteCount', $value );
251 }
252 //LicenseLastCheck
253 public function getLicenseLastCheck() {
254 return intval( $this->getOption( 'LicenseLastCheck', 0 ) );
255 }
256 public function setLicenseLastCheck( $value ) {
257 $this->setOption( 'LicenseLastCheck', intval( $value ) );
258 }
259
260 //LoggerEnable
261 public function isLoggerEnable() {
262 return ( $this->getOption( 'LoggerEnable', false ) || ( defined( 'TCMP_LOGGER' ) && TCMP_LOGGER ) );
263 }
264 public function setLoggerEnable( $value ) {
265 $this->setOption( 'LoggerEnable', $value );
266 }
267
268 //Snippet
269 public function getSnippet( $id ) {
270 return $this->getOption( 'Snippet_' . $id, null );
271 }
272 public function setSnippet( $id, $value ) {
273 $this->setOption( 'Snippet_' . $id, $value );
274 }
275 public function remove_snippet( $id ) {
276 $this->removeOption( 'Snippet_' . $id );
277 }
278 //SnippetList
279 public function getSnippetList() {
280 return $this->getOption( 'SnippetList', array() );
281 }
282 public function setSnippetList( $value ) {
283 $this->setOption( 'SnippetList', $value );
284 }
285 public function removeSnippetList() {
286 $this->removeOption( 'SnippetList' );
287 }
288
289 public function pushConversionSnippets( $options, TCMP_EcommercePurchase $purchase ) {
290 global $tcmp;
291 $this->setRequest( 'EcommercePurchase', $purchase );
292 $snippets = $tcmp->manager->get_conversion_snippets( $options );
293 foreach ( $snippets as $v ) {
294 $id = $v['id'];
295 $tcmp->options->pushConversionSnippetId( $id );
296 }
297 }
298 public function pushConversionSnippetId( $id ) {
299 $array = $this->getRequest( 'ConversionSnippetIds', array() );
300 $array[] = $id;
301 $array = array_unique( $array );
302 $this->setRequest( 'ConversionSnippetIds', $array );
303 }
304 public function get_conversion_snippet_ids() {
305 return $this->getRequest( 'ConversionSnippetIds', false );
306 }
307 public function getEcommercePurchase() {
308 /* @var $result TCMP_EcommercePurchase */
309 $result = $this->getRequest( 'EcommercePurchase', false );
310 return $result;
311 }
312
313 public function hasSnippetWritten( $snippet ) {
314 //check also the md5 of code so if the user create 2 different snippets with
315 //the same tracking code we will not insert into 2 times inside the html
316 $id = $snippet['id'];
317 $md5 = md5( $snippet['code'] );
318
319 $listIds = $this->getRequest( 'SnippetsWrittenIds', array() );
320 $listMd5 = $this->getRequest( 'SnippetsWrittenMd5', array() );
321
322 $result = ( in_array( $id, $listIds ) || in_array( $md5, $listMd5 ) );
323 return $result;
324 }
325 public function pushSnippetWritten( $snippet ) {
326 $md5 = md5( $snippet['code'] );
327 $id = $snippet['id'];
328 $listIds = $this->getRequest( 'SnippetsWrittenIds', array() );
329 $listMd5 = $this->getRequest( 'SnippetsWrittenMd5', array() );
330
331 $listIds[ $id ] = $snippet;
332 $listMd5[ $md5 ] = $id;
333 $this->setRequest( 'SnippetsWrittenIds', $listIds );
334 $this->setRequest( 'SnippetsWrittenMd5', $listMd5 );
335 }
336 public function getSnippetsWritten() {
337 return $this->getRequest( 'SnippetsWrittenIds', array() );
338 }
339 public function clearSnippetsWritten() {
340 $this->setRequest( 'SnippetsWrittenIds', array() );
341 $this->setRequest( 'SnippetsWrittenMd5', array() );
342 }
343
344 //PostShown
345 public function getPostShown() {
346 return $this->getRequest( 'PostShown' );
347 }
348 public function setPostShown( $post ) {
349 $this->setRequest( 'PostShown', $post );
350 }
351
352 private function hasGenericMessages( $type ) {
353 $result = $this->getRequest( $type . 'Messages', null );
354 return ( is_array( $result ) && count( $result ) > 0 );
355 }
356 private function pushGenericMessage( $type, $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
357 global $tcmp;
358 $array = $this->getRequest( $type . 'Messages', array() );
359 $array[] = $tcmp->lang->L( $message, $v1, $v2, $v3, $v4, $v5 );
360 $this->setRequest( $type . 'Messages', $array );
361 }
362 private function writeGenericMessages( $type, $clean = true ) {
363 $type = sanitize_text_field( $type );
364 $result = false;
365 $array = $this->getRequest( $type . 'Messages', array() );
366 // These messages are built by the plugin and have been already sanitized.
367 // Trying to sanitize them again will break plugin functionality since some of the messages contain html.
368 if ( is_array( $array ) && count( $array ) > 0 ) {
369 $result = true;
370 ?>
371 <div class="tcmp-box-<?php echo strtolower( $type ); ?>"><?php echo wpautop( implode( "\n", $array ) ); ?></div>
372 <?php
373 }
374 if ( $clean ) {
375 $this->removeRequest( $type . 'Messages' );
376 }
377 return $result;
378 }
379
380 //WarningMessages
381 public function hasWarningMessages() {
382 return $this->hasGenericMessages( 'Warning' );
383 }
384 public function pushWarningMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
385 return $this->pushGenericMessage( 'Warning', $message, $v1, $v2, $v3, $v4, $v5 );
386 }
387 public function writeWarningMessages( $clean = true ) {
388 return $this->writeGenericMessages( 'Warning', $clean );
389 }
390 //SuccessMessages
391 public function hasSuccessMessages() {
392 return $this->hasGenericMessages( 'Success' );
393 }
394 public function pushSuccessMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
395 return $this->pushGenericMessage( 'Success', $message, $v1, $v2, $v3, $v4, $v5 );
396 }
397 public function writeSuccessMessages( $clean = true ) {
398 return $this->writeGenericMessages( 'Success', $clean );
399 }
400 //InfoMessages
401 public function hasInfoMessages() {
402 return $this->hasGenericMessages( 'Info' );
403 }
404 public function pushInfoMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
405 return $this->pushGenericMessage( 'Info', $message, $v1, $v2, $v3, $v4, $v5 );
406 }
407 public function writeInfoMessages( $clean = true ) {
408 return $this->writeGenericMessages( 'Info', $clean );
409 }
410 //ErrorMessages
411 public function hasErrorMessages() {
412 return $this->hasGenericMessages( 'Error' );
413 }
414 public function pushErrorMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
415 return $this->pushGenericMessage( 'Error', $message, $v1, $v2, $v3, $v4, $v5 );
416 }
417 public function writeErrorMessages( $clean = true ) {
418 return $this->writeGenericMessages( 'Error', $clean );
419 }
420
421 public function writeMessages( $clean = true ) {
422 $result = false;
423 if ( $this->writeInfoMessages( $clean ) ) {
424 $result = true;
425 }
426 if ( $this->writeSuccessMessages( $clean ) ) {
427 $result = true;
428 }
429 if ( $this->writeWarningMessages( $clean ) ) {
430 $result = true;
431 }
432 if ( $this->writeErrorMessages( $clean ) ) {
433 $result = true;
434 }
435
436 return $result;
437 }
438 public function pushMessage( $success, $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
439 if ( $success ) {
440 $this->pushSuccessMessage( $message . 'Success', $v1, $v2, $v3, $v4, $v5 );
441 } else {
442 $this->pushErrorMessage( $message . 'Error', $v1, $v2, $v3, $v4, $v5 );
443 }
444 }
445
446 public function getFeedbackEmail() {
447 return $this->getOption( 'FeedbackEmail', get_bloginfo( 'admin_email' ) );
448 }
449 public function setFeedbackEmail( $value ) {
450 $this->setOption( 'FeedbackEmail', $value );
451 }
452
453 //MetaboxPostTypes
454 public function getMetaboxPostTypes( $create = true ) {
455 global $tcmp;
456 $result = $this->getOption( 'MetaboxPostTypes', array() );
457 if ( $create ) {
458 $types = $tcmp->utils->query( TCMP_QUERY_POST_TYPES );
459 foreach ( $types as $v ) {
460 $v = $v['id'];
461 if ( ! isset( $result[ $v ] ) ) {
462 $result[ $v ] = ( in_array( $v, array( 'post', 'page' ) ) ? 1 : 0 );
463 }
464 }
465 }
466 return $result;
467 }
468 public function setMetaboxPostTypes( $values ) {
469 $this->setOption( 'MetaboxPostTypes', $values );
470 }
471
472 // Add additional recognized tags and attributes
473 public function getAdditionalRecognizedTags() {
474 return $this->getOption( 'additionalRecognizedTags', '' );
475 }
476 public function getAdditionalRecognizedAttributes() {
477 return $this->getOption( 'additionalRecognizedAttributes', '' );
478 }
479 public function setAdditionalRecognizedTags( $text ) {
480 if (is_string($text)) {
481 $this->setOption( 'additionalRecognizedTags', $text );
482 }
483 }
484 public function setAdditionalRecognizedAttributes( $text ) {
485 if (is_string($text)) {
486 $this->setOption( 'additionalRecognizedAttributes', $text );
487 }
488 }
489
490 // Modify Superglobal Variable
491 public function getModifySuperglobalVariable() {
492 return $this->getOption( 'ModifySuperglobalVariable', false );
493 }
494
495 public function setModifySuperglobalVariable( $value ) {
496 global $tcmp;
497 if ( $tcmp->utils->isTrue( $value ) ) {
498 $this->setOption( 'ModifySuperglobalVariable', true );
499 } else {
500 $this->setOption( 'ModifySuperglobalVariable', false );
501 }
502 }
503
504 //hook priority
505 public function getHookPriority() {
506 return $this->getOption( 'HookPriority', TCMP_HOOK_PRIORITY_DEFAULT );
507 }
508
509 public function setHookPriority( $value ) {
510 $this->setOption( 'HookPriority', $value );
511 }
512
513 // Skip Code Sanitization
514 public function getSkipCodeSanitization() {
515 return $this->getOption( 'SkipCodeSanitization', false );
516 }
517
518 public function setSkipCodeSanitization( $value ) {
519 global $tcmp;
520 if ( $tcmp->utils->isTrue( $value ) ) {
521 $this->setOption( 'SkipCodeSanitization', true );
522 } else {
523 $this->setOption( 'SkipCodeSanitization', false );
524 }
525 }
526 }
527