PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 2.7.0
Tracking Code Manager v2.7.0
2.8.0 2.7.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 4 years ago Ecommerce.php 4 years ago Language.php 4 years ago Logger.php 1 month ago MobileDetect.php 1 month ago Options.php 1 month ago Plugin.php 1 month ago Properties.php 4 years ago Tracking.php 1 month ago Utils.php 1 month ago
Options.php
532 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 if ( is_array( $array ) && count( $array ) > 0 ) {
367 $result = true;
368 // Escape at the sink so safety no longer depends on every caller
369 // pre-escaping its message arguments (see F-04). wp_kses_post keeps
370 // the intentional markup (links, styled spans) some notices embed
371 // while neutralising anything dangerous. It now wraps the wpautop()
372 // result rather than each message, so the escaping is applied to
373 // everything that is actually echoed.
374 $html = wpautop( implode( "\n", $array ) );
375 ?>
376 <div class="tcmp-box-<?php echo esc_attr( strtolower( $type ) ); ?>"><?php echo wp_kses_post( $html ); ?></div>
377 <?php
378 }
379 if ( $clean ) {
380 $this->removeRequest( $type . 'Messages' );
381 }
382 return $result;
383 }
384
385 //WarningMessages
386 public function hasWarningMessages() {
387 return $this->hasGenericMessages( 'Warning' );
388 }
389 public function pushWarningMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
390 return $this->pushGenericMessage( 'Warning', $message, $v1, $v2, $v3, $v4, $v5 );
391 }
392 public function writeWarningMessages( $clean = true ) {
393 return $this->writeGenericMessages( 'Warning', $clean );
394 }
395 //SuccessMessages
396 public function hasSuccessMessages() {
397 return $this->hasGenericMessages( 'Success' );
398 }
399 public function pushSuccessMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
400 return $this->pushGenericMessage( 'Success', $message, $v1, $v2, $v3, $v4, $v5 );
401 }
402 public function writeSuccessMessages( $clean = true ) {
403 return $this->writeGenericMessages( 'Success', $clean );
404 }
405 //InfoMessages
406 public function hasInfoMessages() {
407 return $this->hasGenericMessages( 'Info' );
408 }
409 public function pushInfoMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
410 return $this->pushGenericMessage( 'Info', $message, $v1, $v2, $v3, $v4, $v5 );
411 }
412 public function writeInfoMessages( $clean = true ) {
413 return $this->writeGenericMessages( 'Info', $clean );
414 }
415 //ErrorMessages
416 public function hasErrorMessages() {
417 return $this->hasGenericMessages( 'Error' );
418 }
419 public function pushErrorMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
420 return $this->pushGenericMessage( 'Error', $message, $v1, $v2, $v3, $v4, $v5 );
421 }
422 public function writeErrorMessages( $clean = true ) {
423 return $this->writeGenericMessages( 'Error', $clean );
424 }
425
426 public function writeMessages( $clean = true ) {
427 $result = false;
428 if ( $this->writeInfoMessages( $clean ) ) {
429 $result = true;
430 }
431 if ( $this->writeSuccessMessages( $clean ) ) {
432 $result = true;
433 }
434 if ( $this->writeWarningMessages( $clean ) ) {
435 $result = true;
436 }
437 if ( $this->writeErrorMessages( $clean ) ) {
438 $result = true;
439 }
440
441 return $result;
442 }
443 public function pushMessage( $success, $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) {
444 if ( $success ) {
445 $this->pushSuccessMessage( $message . 'Success', $v1, $v2, $v3, $v4, $v5 );
446 } else {
447 $this->pushErrorMessage( $message . 'Error', $v1, $v2, $v3, $v4, $v5 );
448 }
449 }
450
451 public function getFeedbackEmail() {
452 return $this->getOption( 'FeedbackEmail', get_bloginfo( 'admin_email' ) );
453 }
454 public function setFeedbackEmail( $value ) {
455 $this->setOption( 'FeedbackEmail', $value );
456 }
457
458 //MetaboxPostTypes
459 public function getMetaboxPostTypes( $create = true ) {
460 global $tcmp;
461 $result = $this->getOption( 'MetaboxPostTypes', array() );
462 if ( $create ) {
463 $types = $tcmp->utils->query( TCMP_QUERY_POST_TYPES );
464 foreach ( $types as $v ) {
465 $v = $v['id'];
466 if ( ! isset( $result[ $v ] ) ) {
467 $result[ $v ] = ( in_array( $v, array( 'post', 'page' ) ) ? 1 : 0 );
468 }
469 }
470 }
471 return $result;
472 }
473 public function setMetaboxPostTypes( $values ) {
474 $this->setOption( 'MetaboxPostTypes', $values );
475 }
476
477 // Add additional recognized tags and attributes
478 public function getAdditionalRecognizedTags() {
479 return $this->getOption( 'additionalRecognizedTags', '' );
480 }
481 public function getAdditionalRecognizedAttributes() {
482 return $this->getOption( 'additionalRecognizedAttributes', '' );
483 }
484 public function setAdditionalRecognizedTags( $text ) {
485 if (is_string($text)) {
486 $this->setOption( 'additionalRecognizedTags', $text );
487 }
488 }
489 public function setAdditionalRecognizedAttributes( $text ) {
490 if (is_string($text)) {
491 $this->setOption( 'additionalRecognizedAttributes', $text );
492 }
493 }
494
495 // Modify Superglobal Variable
496 public function getModifySuperglobalVariable() {
497 return $this->getOption( 'ModifySuperglobalVariable', false );
498 }
499
500 public function setModifySuperglobalVariable( $value ) {
501 global $tcmp;
502 if ( $tcmp->utils->isTrue( $value ) ) {
503 $this->setOption( 'ModifySuperglobalVariable', true );
504 } else {
505 $this->setOption( 'ModifySuperglobalVariable', false );
506 }
507 }
508
509 //hook priority
510 public function getHookPriority() {
511 return $this->getOption( 'HookPriority', TCMP_HOOK_PRIORITY_DEFAULT );
512 }
513
514 public function setHookPriority( $value ) {
515 $this->setOption( 'HookPriority', $value );
516 }
517
518 // Skip Code Sanitization
519 public function getSkipCodeSanitization() {
520 return $this->getOption( 'SkipCodeSanitization', false );
521 }
522
523 public function setSkipCodeSanitization( $value ) {
524 global $tcmp;
525 if ( $tcmp->utils->isTrue( $value ) ) {
526 $this->setOption( 'SkipCodeSanitization', true );
527 } else {
528 $this->setOption( 'SkipCodeSanitization', false );
529 }
530 }
531 }
532