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