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