Cron.php
1 year ago
Ecommerce.php
1 year ago
Language.php
1 year ago
Logger.php
1 year ago
MobileDetect.php
1 year ago
Options.php
1 year ago
Plugin.php
1 year ago
Properties.php
1 year ago
Tracking.php
1 year ago
Utils.php
1 year ago
Options.php
516 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 | global $tcmp_allowed_html_tags; |
| 122 | foreach ( $array as $key => &$value ) { |
| 123 | if ( is_array( $value ) ) { |
| 124 | $value = $this->recursive_wp_kses( $value ); |
| 125 | } elseif ( 'code' === $key ) { |
| 126 | if ( ! $this->getSkipCodeSanitization() ) { |
| 127 | $value = wp_kses( $value, $tcmp_allowed_html_tags ); |
| 128 | } |
| 129 | } elseif ( is_string( $value ) ) { |
| 130 | $value = wp_kses( $value, $tcmp_allowed_html_tags ); |
| 131 | } else { |
| 132 | // do nothing ... could be a video or graphics object |
| 133 | } |
| 134 | } |
| 135 | return $array; |
| 136 | } |
| 137 | |
| 138 | private function setRequest( $key, $value ) { |
| 139 | $key = $this->get_key( $key ); |
| 140 | $_POST[ $key ] = $value; |
| 141 | } |
| 142 | |
| 143 | public function isPluginFirstInstall() { |
| 144 | return $this->getOption( 'PluginFirstInstall', false ); |
| 145 | } |
| 146 | public function setPluginFirstInstall( $value ) { |
| 147 | $this->setOption( 'PluginFirstInstall', $value ); |
| 148 | } |
| 149 | public function isShowActivationNotice() { |
| 150 | return $this->getOption( 'ShowActivationNotice', false ); |
| 151 | } |
| 152 | public function setShowActivationNotice( $value ) { |
| 153 | $this->setOption( 'ShowActivationNotice', $value ); |
| 154 | } |
| 155 | |
| 156 | public function getShowWhatsNewSeenVersion() { |
| 157 | return intval( $this->getOption( 'ShowWhatsNewSeenVersion', 0 ) ); |
| 158 | } |
| 159 | public function setShowWhatsNewSeenVersion( $value ) { |
| 160 | $this->setOption( 'ShowWhatsNewSeenVersion', $value ); |
| 161 | } |
| 162 | |
| 163 | //ShowWhatsNew |
| 164 | public function isShowWhatsNew() { |
| 165 | $result = intval( $this->getOption( 'ShowWhatsNew', true ) ); |
| 166 | if ( $result ) { |
| 167 | $v = $this->getShowWhatsNewSeenVersion(); |
| 168 | if ( TCMP_WHATSNEW_VERSION == $v ) { |
| 169 | $result = false; |
| 170 | $this->getOption( 'ShowWhatsNew', false ); |
| 171 | } |
| 172 | } |
| 173 | return $result; |
| 174 | } |
| 175 | public function setShowWhatsNew( $value ) { |
| 176 | $this->setOption( 'ShowWhatsNew', $value ); |
| 177 | } |
| 178 | |
| 179 | //TrackingEnable |
| 180 | public function isTrackingEnable() { |
| 181 | return $this->getOption( 'TrackingEnable', 0 ); |
| 182 | } |
| 183 | public function setTrackingEnable( $value ) { |
| 184 | $this->setOption( 'TrackingEnable', $value ); |
| 185 | } |
| 186 | //TrackingNotice |
| 187 | public function isTrackingNotice() { |
| 188 | return $this->getOption( 'TrackingNotice', 1 ); |
| 189 | } |
| 190 | public function setTrackingNotice( $value ) { |
| 191 | $this->setOption( 'TrackingNotice', $value ); |
| 192 | } |
| 193 | |
| 194 | public function getTrackingLastSend() { |
| 195 | return $this->getOption( 'TrackingLastSend[' . TCMP_PLUGIN_SLUG . ']', 0 ); |
| 196 | } |
| 197 | public function setTrackingLastSend( $value ) { |
| 198 | $this->setOption( 'TrackingLastSend[' . TCMP_PLUGIN_SLUG . ']', $value ); |
| 199 | } |
| 200 | public function getPluginInstallDate() { |
| 201 | return $this->getOption( 'PluginInstallDate[' . TCMP_PLUGIN_SLUG . ']', 0 ); |
| 202 | } |
| 203 | public function setPluginInstallDate( $value ) { |
| 204 | $this->setOption( 'PluginInstallDate[' . TCMP_PLUGIN_SLUG . ']', $value ); |
| 205 | } |
| 206 | public function getPluginUpdateDate() { |
| 207 | return $this->getOption( 'PluginUpdateDate[' . TCMP_PLUGIN_SLUG . ']', 0 ); |
| 208 | } |
| 209 | public function setPluginUpdateDate( $value ) { |
| 210 | $this->setOption( 'PluginUpdateDate[' . TCMP_PLUGIN_SLUG . ']', $value ); |
| 211 | } |
| 212 | |
| 213 | //LicenseKey |
| 214 | public function getLicenseKey() { |
| 215 | return $this->getOption( 'LiceseKey', '' ); |
| 216 | } |
| 217 | public function setLicenseKey( $value ) { |
| 218 | $this->setOption( 'LiceseKey', $value ); |
| 219 | } |
| 220 | //LicenseStatus |
| 221 | public function isLicenseSuccess() { |
| 222 | return $this->getOption( 'LicenseSuccess', 0 ); |
| 223 | } |
| 224 | public function setLicenseSuccess( $value ) { |
| 225 | $this->setOption( 'LicenseSuccess', $value ); |
| 226 | } |
| 227 | //License |
| 228 | public function getLicense() { |
| 229 | return $this->getOption( 'License', false ); |
| 230 | } |
| 231 | public function setLicense( $value ) { |
| 232 | $this->setOption( 'License', $value ); |
| 233 | } |
| 234 | //LicenseSiteCount |
| 235 | public function getLicenseSiteCount() { |
| 236 | return $this->getOption( 'LicenseSiteCount', false ); |
| 237 | } |
| 238 | public function setLicenseSiteCount( $value ) { |
| 239 | $this->setOption( 'LicenseSiteCount', $value ); |
| 240 | } |
| 241 | //LicenseLastCheck |
| 242 | public function getLicenseLastCheck() { |
| 243 | return intval( $this->getOption( 'LicenseLastCheck', 0 ) ); |
| 244 | } |
| 245 | public function setLicenseLastCheck( $value ) { |
| 246 | $this->setOption( 'LicenseLastCheck', intval( $value ) ); |
| 247 | } |
| 248 | |
| 249 | //LoggerEnable |
| 250 | public function isLoggerEnable() { |
| 251 | return ( $this->getOption( 'LoggerEnable', false ) || ( defined( 'TCMP_LOGGER' ) && TCMP_LOGGER ) ); |
| 252 | } |
| 253 | public function setLoggerEnable( $value ) { |
| 254 | $this->setOption( 'LoggerEnable', $value ); |
| 255 | } |
| 256 | |
| 257 | //Snippet |
| 258 | public function getSnippet( $id ) { |
| 259 | return $this->getOption( 'Snippet_' . $id, null ); |
| 260 | } |
| 261 | public function setSnippet( $id, $value ) { |
| 262 | $this->setOption( 'Snippet_' . $id, $value ); |
| 263 | } |
| 264 | public function remove_snippet( $id ) { |
| 265 | $this->removeOption( 'Snippet_' . $id ); |
| 266 | } |
| 267 | //SnippetList |
| 268 | public function getSnippetList() { |
| 269 | return $this->getOption( 'SnippetList', array() ); |
| 270 | } |
| 271 | public function setSnippetList( $value ) { |
| 272 | $this->setOption( 'SnippetList', $value ); |
| 273 | } |
| 274 | public function removeSnippetList() { |
| 275 | $this->removeOption( 'SnippetList' ); |
| 276 | } |
| 277 | |
| 278 | public function pushConversionSnippets( $options, TCMP_EcommercePurchase $purchase ) { |
| 279 | global $tcmp; |
| 280 | $this->setRequest( 'EcommercePurchase', $purchase ); |
| 281 | $snippets = $tcmp->manager->get_conversion_snippets( $options ); |
| 282 | foreach ( $snippets as $v ) { |
| 283 | $id = $v['id']; |
| 284 | $tcmp->options->pushConversionSnippetId( $id ); |
| 285 | } |
| 286 | } |
| 287 | public function pushConversionSnippetId( $id ) { |
| 288 | $array = $this->getRequest( 'ConversionSnippetIds', array() ); |
| 289 | $array[] = $id; |
| 290 | $array = array_unique( $array ); |
| 291 | $this->setRequest( 'ConversionSnippetIds', $array ); |
| 292 | } |
| 293 | public function get_conversion_snippet_ids() { |
| 294 | return $this->getRequest( 'ConversionSnippetIds', false ); |
| 295 | } |
| 296 | public function getEcommercePurchase() { |
| 297 | /* @var $result TCMP_EcommercePurchase */ |
| 298 | $result = $this->getRequest( 'EcommercePurchase', false ); |
| 299 | return $result; |
| 300 | } |
| 301 | |
| 302 | public function hasSnippetWritten( $snippet ) { |
| 303 | //check also the md5 of code so if the user create 2 different snippets with |
| 304 | //the same tracking code we will not insert into 2 times inside the html |
| 305 | $id = $snippet['id']; |
| 306 | $md5 = md5( $snippet['code'] ); |
| 307 | |
| 308 | $listIds = $this->getRequest( 'SnippetsWrittenIds', array() ); |
| 309 | $listMd5 = $this->getRequest( 'SnippetsWrittenMd5', array() ); |
| 310 | |
| 311 | $result = ( in_array( $id, $listIds ) || in_array( $md5, $listMd5 ) ); |
| 312 | return $result; |
| 313 | } |
| 314 | public function pushSnippetWritten( $snippet ) { |
| 315 | $md5 = md5( $snippet['code'] ); |
| 316 | $id = $snippet['id']; |
| 317 | $listIds = $this->getRequest( 'SnippetsWrittenIds', array() ); |
| 318 | $listMd5 = $this->getRequest( 'SnippetsWrittenMd5', array() ); |
| 319 | |
| 320 | $listIds[ $id ] = $snippet; |
| 321 | $listMd5[ $md5 ] = $id; |
| 322 | $this->setRequest( 'SnippetsWrittenIds', $listIds ); |
| 323 | $this->setRequest( 'SnippetsWrittenMd5', $listMd5 ); |
| 324 | } |
| 325 | public function getSnippetsWritten() { |
| 326 | return $this->getRequest( 'SnippetsWrittenIds', array() ); |
| 327 | } |
| 328 | public function clearSnippetsWritten() { |
| 329 | $this->setRequest( 'SnippetsWrittenIds', array() ); |
| 330 | $this->setRequest( 'SnippetsWrittenMd5', array() ); |
| 331 | } |
| 332 | |
| 333 | //PostShown |
| 334 | public function getPostShown() { |
| 335 | return $this->getRequest( 'PostShown' ); |
| 336 | } |
| 337 | public function setPostShown( $post ) { |
| 338 | $this->setRequest( 'PostShown', $post ); |
| 339 | } |
| 340 | |
| 341 | private function hasGenericMessages( $type ) { |
| 342 | $result = $this->getRequest( $type . 'Messages', null ); |
| 343 | return ( is_array( $result ) && count( $result ) > 0 ); |
| 344 | } |
| 345 | private function pushGenericMessage( $type, $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 346 | global $tcmp; |
| 347 | $array = $this->getRequest( $type . 'Messages', array() ); |
| 348 | $array[] = $tcmp->lang->L( $message, $v1, $v2, $v3, $v4, $v5 ); |
| 349 | $this->setRequest( $type . 'Messages', $array ); |
| 350 | } |
| 351 | private function writeGenericMessages( $type, $clean = true ) { |
| 352 | $type = sanitize_text_field( $type ); |
| 353 | $result = false; |
| 354 | $array = $this->getRequest( $type . 'Messages', array() ); |
| 355 | // These messages are built by the plugin and have been already sanitized. |
| 356 | // Trying to sanitize them again will break plugin functionality since some of the messages contain html. |
| 357 | if ( is_array( $array ) && count( $array ) > 0 ) { |
| 358 | $result = true; |
| 359 | ?> |
| 360 | <div class="tcmp-box-<?php echo strtolower( $type ); ?>"><?php echo wpautop( implode( "\n", $array ) ); ?></div> |
| 361 | <?php |
| 362 | } |
| 363 | if ( $clean ) { |
| 364 | $this->removeRequest( $type . 'Messages' ); |
| 365 | } |
| 366 | return $result; |
| 367 | } |
| 368 | |
| 369 | //WarningMessages |
| 370 | public function hasWarningMessages() { |
| 371 | return $this->hasGenericMessages( 'Warning' ); |
| 372 | } |
| 373 | public function pushWarningMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 374 | return $this->pushGenericMessage( 'Warning', $message, $v1, $v2, $v3, $v4, $v5 ); |
| 375 | } |
| 376 | public function writeWarningMessages( $clean = true ) { |
| 377 | return $this->writeGenericMessages( 'Warning', $clean ); |
| 378 | } |
| 379 | //SuccessMessages |
| 380 | public function hasSuccessMessages() { |
| 381 | return $this->hasGenericMessages( 'Success' ); |
| 382 | } |
| 383 | public function pushSuccessMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 384 | return $this->pushGenericMessage( 'Success', $message, $v1, $v2, $v3, $v4, $v5 ); |
| 385 | } |
| 386 | public function writeSuccessMessages( $clean = true ) { |
| 387 | return $this->writeGenericMessages( 'Success', $clean ); |
| 388 | } |
| 389 | //InfoMessages |
| 390 | public function hasInfoMessages() { |
| 391 | return $this->hasGenericMessages( 'Info' ); |
| 392 | } |
| 393 | public function pushInfoMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 394 | return $this->pushGenericMessage( 'Info', $message, $v1, $v2, $v3, $v4, $v5 ); |
| 395 | } |
| 396 | public function writeInfoMessages( $clean = true ) { |
| 397 | return $this->writeGenericMessages( 'Info', $clean ); |
| 398 | } |
| 399 | //ErrorMessages |
| 400 | public function hasErrorMessages() { |
| 401 | return $this->hasGenericMessages( 'Error' ); |
| 402 | } |
| 403 | public function pushErrorMessage( $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 404 | return $this->pushGenericMessage( 'Error', $message, $v1, $v2, $v3, $v4, $v5 ); |
| 405 | } |
| 406 | public function writeErrorMessages( $clean = true ) { |
| 407 | return $this->writeGenericMessages( 'Error', $clean ); |
| 408 | } |
| 409 | |
| 410 | public function writeMessages( $clean = true ) { |
| 411 | $result = false; |
| 412 | if ( $this->writeInfoMessages( $clean ) ) { |
| 413 | $result = true; |
| 414 | } |
| 415 | if ( $this->writeSuccessMessages( $clean ) ) { |
| 416 | $result = true; |
| 417 | } |
| 418 | if ( $this->writeWarningMessages( $clean ) ) { |
| 419 | $result = true; |
| 420 | } |
| 421 | if ( $this->writeErrorMessages( $clean ) ) { |
| 422 | $result = true; |
| 423 | } |
| 424 | |
| 425 | return $result; |
| 426 | } |
| 427 | public function pushMessage( $success, $message, $v1 = null, $v2 = null, $v3 = null, $v4 = null, $v5 = null ) { |
| 428 | if ( $success ) { |
| 429 | $this->pushSuccessMessage( $message . 'Success', $v1, $v2, $v3, $v4, $v5 ); |
| 430 | } else { |
| 431 | $this->pushErrorMessage( $message . 'Error', $v1, $v2, $v3, $v4, $v5 ); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | public function getFeedbackEmail() { |
| 436 | return $this->getOption( 'FeedbackEmail', get_bloginfo( 'admin_email' ) ); |
| 437 | } |
| 438 | public function setFeedbackEmail( $value ) { |
| 439 | $this->setOption( 'FeedbackEmail', $value ); |
| 440 | } |
| 441 | |
| 442 | //MetaboxPostTypes |
| 443 | public function getMetaboxPostTypes( $create = true ) { |
| 444 | global $tcmp; |
| 445 | $result = $this->getOption( 'MetaboxPostTypes', array() ); |
| 446 | if ( $create ) { |
| 447 | $types = $tcmp->utils->query( TCMP_QUERY_POST_TYPES ); |
| 448 | foreach ( $types as $v ) { |
| 449 | $v = $v['id']; |
| 450 | if ( ! isset( $result[ $v ] ) ) { |
| 451 | $result[ $v ] = ( in_array( $v, array( 'post', 'page' ) ) ? 1 : 0 ); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return $result; |
| 456 | } |
| 457 | public function setMetaboxPostTypes( $values ) { |
| 458 | $this->setOption( 'MetaboxPostTypes', $values ); |
| 459 | } |
| 460 | |
| 461 | // Add additional recognized tags and attributes |
| 462 | public function getAdditionalRecognizedTags() { |
| 463 | return $this->getOption( 'additionalRecognizedTags', '' ); |
| 464 | } |
| 465 | public function getAdditionalRecognizedAttributes() { |
| 466 | return $this->getOption( 'additionalRecognizedAttributes', '' ); |
| 467 | } |
| 468 | public function setAdditionalRecognizedTags( $text ) { |
| 469 | if (is_string($text)) { |
| 470 | $this->setOption( 'additionalRecognizedTags', $text ); |
| 471 | } |
| 472 | } |
| 473 | public function setAdditionalRecognizedAttributes( $text ) { |
| 474 | if (is_string($text)) { |
| 475 | $this->setOption( 'additionalRecognizedAttributes', $text ); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Modify Superglobal Variable |
| 480 | public function getModifySuperglobalVariable() { |
| 481 | return $this->getOption( 'ModifySuperglobalVariable', false ); |
| 482 | } |
| 483 | |
| 484 | public function setModifySuperglobalVariable( $value ) { |
| 485 | global $tcmp; |
| 486 | if ( $tcmp->utils->isTrue( $value ) ) { |
| 487 | $this->setOption( 'ModifySuperglobalVariable', true ); |
| 488 | } else { |
| 489 | $this->setOption( 'ModifySuperglobalVariable', false ); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | //hook priority |
| 494 | public function getHookPriority() { |
| 495 | return $this->getOption( 'HookPriority', TCMP_HOOK_PRIORITY_DEFAULT ); |
| 496 | } |
| 497 | |
| 498 | public function setHookPriority( $value ) { |
| 499 | $this->setOption( 'HookPriority', $value ); |
| 500 | } |
| 501 | |
| 502 | // Skip Code Sanitization |
| 503 | public function getSkipCodeSanitization() { |
| 504 | return $this->getOption( 'SkipCodeSanitization', false ); |
| 505 | } |
| 506 | |
| 507 | public function setSkipCodeSanitization( $value ) { |
| 508 | global $tcmp; |
| 509 | if ( $tcmp->utils->isTrue( $value ) ) { |
| 510 | $this->setOption( 'SkipCodeSanitization', true ); |
| 511 | } else { |
| 512 | $this->setOption( 'SkipCodeSanitization', false ); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 |