PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.1.5
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.1.5
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / Campaign.php

Campaign.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.1.5, at includes/Campaign.php

1,062 lines 21.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 use \Hurrytimer\Utils\Helpers;
6
7 class Campaign
8 {
9
10 /**
11 * Campaign custom post ID
12 *
13 * @var int
14 */
15 private $id;
16
17 /**
18 * Campaign mode.
19 *
20 * @see C.php
21 *
22 * @var int
23 */
24 public $mode = C::MODE_REGULAR;
25
26 /**
27 * Evergreen duration array.
28 *
29 * @see getDuration()
30 *
31 * @var array
32 */
33 public $duration;
34
35 /**
36 * Restart option after expiration.
37 *
38 * @see \Hurrytimer\CampaignRestart
39 *
40 * @var int
41 */
42 public $restart;
43
44 /**
45 * Headline text.
46 *
47 * @var string
48 */
49 public $headline = "Hurry Up!";
50
51 /**
52 * Headline color.
53 *
54 * @var string
55 */
56 public $headlineColor = "#000";
57
58 /**
59 * Headline size.
60 *
61 * @var int
62 */
63 public $headlineSize = 30;
64
65 /**
66 * Headline position.
67 *
68 * @var int
69 */
70 public $headlinePosition = C::HEADLINE_POSITION_ABOVE_TIMER;
71
72 /**
73 * Headline visibility.
74 *
75 * @var boolean
76 */
77 public $headlineVisibility = C::YES;
78
79 public $headlineSpacing = 5;
80
81 /**
82 * Control labels visibility.
83 *
84 * @var string
85 */
86 public $labelVisibility = C::YES;
87
88 /**
89 * Show/hide days block.
90 *
91 * @var string
92 */
93 public $daysVisibility = C::YES;
94
95 /**
96 * Show/hide hours block.
97 *
98 * @var string
99 */
100 public $hoursVisibility = C::YES;
101
102 /**
103 * Show/hide minutes block.
104 *
105 * @var string
106 */
107 public $minutesVisibility = C::YES;
108
109 /**
110 * Show/hide seconds block.
111 *
112 * @var string
113 */
114 public $secondsVisibility = C::YES;
115
116 /**
117 * Regular compaign end datetime.
118 *
119 * @var string
120 */
121 public $endDatetime;
122
123 /**
124 * Labels texts.
125 *
126 * @var array
127 */
128 public $labels
129 = [
130 'days' => 'days',
131 'hours' => 'hrs',
132 'minutes' => 'mins',
133 'seconds' => 'secs',
134 ];
135
136 /**
137 * Digit color.
138 *
139 * @var string
140 */
141 public $digitColor = "#000";
142
143 /**
144 * Digit size.
145 *
146 * @var int
147 */
148 public $digitSize = 35;
149
150 /**
151 * Redirect action url.
152 *
153 * @var string
154 */
155 public $redirectUrl;
156
157 /**
158 * Label size.
159 *
160 * @var int
161 */
162 public $labelSize = 12;
163
164 /**
165 * Label color.
166 *
167 * @var string
168 */
169 public $labelColor = "#000";
170
171 /**
172 * End action.
173 *
174 * @var array
175 */
176 public $actions = [];
177
178 /**
179 * WooCommerce compaign position in product page.
180 *
181 * @var int
182 */
183 public $wcPosition = C::WC_POSITION_ABOVE_TITLE;
184
185 /**
186 * Enable/disable woocommerce integration.
187 *
188 * @var string
189 */
190 public $wcEnable = C::NO;
191
192 /**
193 * WooCommerce products selection.
194 *
195 * @var array
196 */
197 public $wcProductsSelection;
198
199 /**
200 * WooCommerce products selection type.
201 *
202 * @var int
203 */
204 public $wcProductsSelectionType;
205
206 /**
207 * Timer block border color.
208 *
209 * @var string
210 */
211 public $blockBorderColor = "";
212
213 /**
214 * Timer Block border width.
215 *
216 * @var int
217 */
218
219 public $blockBorderWidth = 0;
220
221 /**
222 * Timer block radius.
223 *
224 * @var int
225 */
226 public $blockBorderRadius = 0;
227
228 /**
229 * Block size.
230 *
231 * @var int
232 */
233
234 public $blockSize = 50;
235
236 /**
237 * Block background color.
238 *
239 * @var string
240 */
241 public $blockBgColor = '';
242
243 /**
244 * Block spacing.
245 *
246 * @var int
247 */
248 public $blockSpacing = 5;
249
250 /**
251 * Block padding.
252 *
253 * @var int
254 */
255 public $blockPadding = 0;
256
257 /**
258 * Block spearator visibility.
259 *
260 * @var boolean
261 */
262 public $blockSeparatorVisibility = C::YES;
263
264 /**
265 * Label case
266 *
267 * @var string
268 */
269 public $labelCase = C::TRANSFORM_UPPERCASE;
270
271 /**
272 * Custom CSS
273 *
274 * @var string
275 */
276 public $customCss = '';
277
278 /**
279 * Block elements display
280 * Values: block, inline
281 *
282 * @var string
283 */
284 public $blockDisplay = 'block';
285
286 /**
287 * Enable sticky bar.
288 */
289 public $enableSticky = C::NO;
290
291 /**
292 * Sticky bar background color.
293 *
294 * @var string
295 */
296 public $stickyBarBgColor = '#eee';
297
298 /**
299 * Sticky bar close button color.
300 *
301 * @var string
302 */
303 public $stickyBarCloseBtnColor = '#fff';
304
305 /**
306 * Sticky bar position.
307 * Values: top, bottom
308 *
309 * @var string
310 */
311 public $stickyBarPosition = 'top';
312
313 /**
314 * Sticky bar padding.
315 *
316 * @var integer
317 */
318 public $stickyBarPadding = 5;
319
320 /**
321 * Sticky bar display pages.
322 *
323 * @var array
324 */
325 public $stickyBarPages = [];
326
327 /**
328 * Where to display the sticky bar option
329 *
330 * @var string
331 */
332 public $stickyBarDisplayOn = 'all_pages';
333
334 /**
335 * Show sticky bar close button?
336 *
337 * @var string
338 */
339 public $stickyBarDismissible = C::YES;
340
341 /**
342 * CTA settings.
343 *
344 * @var array
345 */
346 public $callToAction
347 = [
348 'enabled' => C::NO,
349 'new_tab' => C::NO,
350 'url' => '',
351 'text' => 'Learn More',
352 'text_size' => 15,
353 'text_color' => '#fff',
354 'bg_color' => '#000',
355 'y_padding' => 10,
356 'x_padding' => 15,
357 'border_radius' => 3,
358 'spacing' => 5,
359 ];
360
361 /**
362 * Campaign elements display
363 * values: block,inline
364 *
365 * @var string
366 */
367 public $campaignDisplay = 'block';
368
369 /**
370 * Campaign aligments
371 * values: center,left,right
372 *
373 * @var string
374 */
375 public $campaignAlign = 'center';
376
377 /**
378 * Campaign spacing.
379 *
380 * @var integer
381 */
382 public $campaignSpacing = 10;
383
384 /**
385 * Campaign horizontal padding.
386 *
387 * @var integer
388 */
389 public $campaignXPadding = 10;
390
391 /**
392 * Campaign vertical padding.
393 *
394 * @var integer
395 */
396 public $campaignYPadding = 10;
397
398 public function __construct($id)
399 {
400
401 $this->id = $id;
402 }
403
404 /**
405 * Returns compaign post iD.
406 *
407 * @return int
408 */
409 public function getId()
410 {
411 return $this->id;
412 }
413
414 public function showStickyBarOn($pageId)
415 {
416 if ($this->enableSticky === C::NO) {
417 return false;
418 }
419
420 switch ($this->getStickyBarDisplayOn()) {
421 case 'all_pages':
422 return true;
423 case 'wc_products_pages':
424
425 if (function_exists('is_product') && is_product()) {
426 $wc_campaign = new WCCampaign();
427 return ($wc_campaign->hasActiveCampaign($this, $pageId));
428 }
429 return false;
430
431 case 'specific_pages':
432 $pages = $this->getStickyBarPages();
433 foreach ($pages as $page) {
434 if ($page == $pageId) {
435 return true;
436 }
437 }
438 }
439
440 return false;
441
442 }
443
444 public function getStickyBarPages()
445 {
446 return $this->getRawSetting('sticky_bar_pages');
447 }
448
449 public function getStickyBarDisplayOn()
450 {
451
452 // backward compat.
453 $all_pages = $this->getRawSetting('sticky_bar_show_on_all_pages', false);
454
455 if ($all_pages === C::YES) {
456 $this->setStickyBarDisplayOn('all_pages');
457 }
458
459 $this->deleteRawSetting('sticky_bar_show_on_all_pages');
460
461 return $this->getRawSetting('_hurryt_sticky_bar_display_on');
462 }
463
464 public function setStickyBarDisplayOn($value)
465 {
466 $this->storeRawSetting('_hurryt_sticky_bar_display_on', $value);
467 }
468
469 /**
470 * Get raw setting.
471 *
472 * @param string $name
473 * @param boolean
474 *
475 * @return mixed
476 */
477 public function getRawSetting($name, $useDefault = true)
478 {
479 $value = get_post_meta($this->id, $name, true);
480 if (!empty($value)) {
481 return $value;
482 }
483 if ($useDefault) {
484 return $this->{Helpers::snakeToCamelCase($name)};
485 }
486
487 return $value;
488 }
489
490 public function storeRawSetting($name, $value)
491 {
492 $value = !empty($value) ? $value : $this->{Helpers::snakeToCamelCase($name)};
493 update_post_meta($this->id, $name, $value);
494 }
495
496 /**
497 * Bulk save for compaign settings.
498 *
499 * @param $data
500 */
501 public function storeSettings($data)
502 {
503
504 foreach ($data as $prop => $value) {
505 $method = Helpers::snakeToCamelCase("set_{$prop}");
506 if (method_exists($this, $method)) {
507 $this->$method($value ?: $this->$prop);
508 } elseif (property_exists(__CLASS__, Helpers::snakeToCamelCase($prop))) {
509 $this->storeRawSetting($prop, $value);
510 }
511 }
512
513 self::buildCss();
514 }
515
516 /**
517 * Build User CSS,
518 * Then merge it with base one.
519 *
520 * @return void
521 */
522 public static function buildCss()
523 {
524
525 // Build and return CSS
526 ob_start();
527 include HURRYT_DIR . 'includes/css.php';
528 $css = ob_get_clean();
529 $base = file_get_contents(HURRYT_DIR . '/assets/css/base.css');
530
531 // Merge built and base css.
532 $base .= $css;
533
534 // Save to public file.
535 file_put_contents(HURRYT_DIR . '/assets/css/hurrytimer.css', $base);
536 }
537
538 /**
539 * Load settings from database,
540 * and set object props according their matched name.
541 */
542 public function loadSettings()
543 {
544
545 $reflection = new \ReflectionObject($this);
546 foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
547 $name = $prop->getName();
548 $method = 'get' . ucfirst($name);
549 if (method_exists($this, $method)) {
550 $this->{$name} = $this->$method();
551 } else {
552 $this->{$name} = $this->getRawSetting(Helpers::camelToSnakeCase($name));
553 }
554 }
555 }
556
557 /**
558 * Returns compaign headline.
559 *
560 * @return mixed
561 */
562 public function getHeadline()
563 {
564 return $this->id ? get_the_title($this->id) : $this->headline;
565 }
566
567 public function isPublished()
568 {
569 return get_post_status($this->id) === "publish";
570 }
571
572 public function isWcEnabled()
573 {
574 return $this->getWcEnable() === C::YES;
575 }
576
577 /**
578 * Check current countdown timer mode
579 *
580 * @return bool
581 */
582 public function isEvergreen()
583 {
584 return $this->getRawSetting('mode') == C::MODE_EVERGREEN;
585 }
586
587 public function isRegular()
588 {
589 return $this->getRawSetting('mode') == C::MODE_REGULAR;
590 }
591
592 /**
593 * Save compaign endtime for regular mode.
594 *
595 * @param $date
596 */
597 public function setEndDatetime($date)
598 {
599 $this->storeRawSetting('end_datetime', $date ?: $this->defaultEndDatetime());
600 }
601
602 /**
603 * Returns default end datetime for regular mode.
604 *
605 * @return false|string
606 */
607 private function defaultEndDatetime()
608 {
609 return date('Y-m-d h:i A', strtotime('+1 week'));
610 }
611
612 /**
613 * Save digit color.
614 *
615 * @param $color
616 */
617 public function setDigitColor($color)
618 {
619 $this->storeRawSetting('digit_color', $color);
620 }
621
622 /**
623 * Returns headline visibility.
624 *
625 * @return mixed
626 */
627
628 public function getHeadlineVisibility()
629 {
630 $meta = 'headline_visibility';
631
632 $value = $this->getRawSetting($meta, false);
633 if ($value === C::NO || $value === C::YES) {
634 return $value;
635 }
636
637 $legacyMeta = 'display_headline';
638 $legacyValue = filter_var($this->getLegacySetting($legacyMeta), FILTER_VALIDATE_BOOLEAN);
639 $legacyValue = $legacyValue ? C::YES : C::NO;
640
641 $this->storeRawSetting($meta, $legacyValue);
642
643 $this->deleteRawSetting($legacyMeta);
644
645 return $this->getRawSetting($meta);
646 }
647
648 /**
649 * Returns digit color.
650 * Backward compat with older versions.
651 *
652 * @return mixed
653 */
654 public function getDigitColor()
655 {
656 $value = $this->getRawSetting('digit_color', false);
657 if ($value) {
658 return $value;
659 }
660
661 $legacy = $this->getLegacySetting('text_color');
662 if (!$legacy) {
663 return $this->digitColor;
664 }
665
666 $this->setDigitColor($legacy);
667 if (!$this->getRawSetting('label_color', false)) {
668 $this->setLabelColor($legacy);
669 }
670
671 $this->deleteRawSetting('text_color');
672
673 return $this->getRawSetting('digit_color');
674 }
675
676 public function setLabelColor($color)
677 {
678 $this->storeRawSetting('label_color', $color);
679 }
680
681 /**
682 * Get label color
683 *
684 * @return mixed
685 */
686 public function getLabelColor()
687 {
688 $value = $this->getRawSetting('label_color', false);
689 if ($value) {
690 return $value;
691 }
692
693 $legacy = $this->getLegacySetting('text_color');
694 if (!$legacy) {
695 return $this->labelColor;
696 }
697
698 $this->setLabelColor($legacy);
699 if (!$this->getRawSetting('digit_color', false)) {
700 $this->setDigitColor($legacy);
701 }
702
703 $this->deleteRawSetting('text_color');
704
705 return $this->getRawSetting('label_color');
706
707 }
708
709 /**
710 *
711 * Return timer digit size.
712 * Backward comapt. with older versions.
713 *
714 * @return int
715 * @since 1.2.4
716 */
717 public function getDigitSize()
718 {
719 $meta = 'digit_size';
720 $value = $this->getRawSetting($meta, false);
721 if (!empty($value)) {
722 return $value;
723 }
724 $legacy = $this->getLegacySetting('text_size');
725
726 if (empty($legacy)) {
727 return $this->digitSize;
728 }
729
730 $this->setDigitSize($legacy);
731
732 $this->deleteRawSetting('text_size');
733
734 return $this->getRawSetting('digit_size');
735 }
736
737 /**
738 * Save timer digit size.
739 *
740 * @param $size
741 */
742 public function setDigitSize($size)
743 {
744 $this->storeRawSetting('digit_size', $size);
745 }
746
747 /**
748 * Save timer label size.
749 *
750 * @param $size
751 */
752 public function setLabelSize($size)
753 {
754 $this->storeRawSetting('label_size', $size);
755 }
756
757 public function setHeadlineSize($size)
758 {
759 $this->storeRawSetting('headline_size', $size);
760 }
761
762 /**
763 * Returns evergreen duration.
764 *
765 * @return array
766 */
767 public function getDuration()
768 {
769 $default = [0, 0, 0, 0];
770 $duration = $this->getRawSetting('duration');
771 if (is_array($duration)) {
772 $duration = array_merge($duration, $default);
773
774 return array_map('intval', $duration);
775 }
776
777 return $default;
778 }
779
780 /**
781 * Returns actions array.
782 *
783 * @return array
784 */
785 public function getActions()
786 {
787 $legacy = $this->getLegacySetting('end_action');
788 if ($legacy) {
789 $redirectUrl = $this->getLegacySetting('redirect_url');
790 $actions = [
791 [
792 'id' => intval($legacy),
793 'redirectUrl' => $redirectUrl,
794 ],
795 ];
796 $this->storeRawSetting('actions', $actions);
797 $this->deleteRawSetting('end_action');
798 $this->deleteRawSetting('redirect_url');
799
800 return $this->mergeActions($actions);
801 }
802
803 return $this->mergeActions($this->getRawSetting('actions'));
804 }
805
806 private function mergeActions($actions)
807 {
808 $defaults = [
809 [
810 'id' => C::ACTION_NONE,
811 'redirectUrl' => '',
812 'message' => '',
813 'wcStockStatus' => '',
814 ],
815 ];
816
817 if (count($actions) === 0) {
818 return $defaults;
819 }
820
821 return array_map(function ($action) use ($defaults) {
822 $action['id'] = (int) $action['id'];
823
824 return array_merge($defaults[0], $action);
825 }, $actions);
826 }
827
828 /**
829 * Returns evergreen duration in seconds.
830 *
831 * @return int
832 */
833 public function getDurationInSeconds()
834 {
835 list($d, $h, $m, $s) = $this->getDuration();
836
837 return $d * DAY_IN_SECONDS +
838 $h * HOUR_IN_SECONDS +
839 $m * MINUTE_IN_SECONDS +
840 $s;
841 }
842
843 /**
844 * Returns end datetime.
845 *
846 * @return string
847 */
848 public function getEndDatetime()
849 {
850 return $this->getRawSetting('end_datetime') ?: $this->defaultEndDatetime();
851 }
852
853 /**
854 * Returns compaign publish datetime.
855 *
856 * @return mixed
857 */
858 public function getStartTimestamp()
859 {
860 return get_the_date($this->id);
861 }
862
863 /**
864 * Returns position in WooCommerce product page.
865 *
866 * @return mixed
867 */
868 public function getWcPosition()
869 {
870 $legacy = $this->getLegacySetting('position');
871
872 if (!$legacy) {
873 return $this->getRawSetting('wc_position');
874 }
875 $this->storeRawSetting('wc_position', $legacy);
876 $this->deleteRawSetting('position');
877
878 return $legacy;
879 }
880
881 private function getLegacySetting($name)
882 {
883 return get_post_meta($this->id, $name, true);
884 }
885
886 /**
887 * Returns true if WooCommerce integration is enabled.
888 *
889 * @return mixed
890 */
891 public function getWcEnable()
892 {
893 $value = $this->getRawSetting('wc_enable', false);
894
895 if ($value === C::YES || $value === C::NO) {
896 return $value;
897 }
898
899 $legacy = filter_var($value, FILTER_VALIDATE_BOOLEAN);
900 if ($legacy) {
901 $this->storeRawSetting('wc_enable', C::YES);
902 } else {
903 $this->storeRawSetting('wc_enable', C::NO);
904 }
905
906 return $this->getRawSetting('wc_enable');
907 }
908
909 /**
910 * Returns true if label should be displayed.
911 *
912 * @return mixed
913 */
914 public function getLabelVisibility()
915 {
916 $meta = 'label_visibility';
917 $value = $this->getRawSetting($meta, false);
918 if ($value === C::NO || $value === C::YES) {
919 return $value;
920 }
921
922 $legacy = filter_var($this->getLegacySetting('display_labels'), FILTER_VALIDATE_BOOLEAN);
923 $legacy = $legacy ? C::YES : C::NO;
924 $this->storeRawSetting($meta, $legacy);
925 $this->deleteRawSetting('display_labels');
926
927 return $this->getRawSetting($meta);
928
929 }
930
931 /**
932 *
933 * Returns restart type.
934 *
935 * @return int
936 */
937 public function getRestart()
938 {
939 return $this->getRawSetting('restart') ?: C::RESTART_NONE;
940 }
941
942 /**
943 * Returns WooCommerce products selection type.
944 *
945 * @return string
946 */
947 public function getWcProductsSelectionType()
948 {
949 $legacy = $this->getLegacySetting('products_type');
950 if (!$legacy) {
951 return $this->getRawSetting('wc_products_selection_type');
952 }
953 $this->storeRawSetting('wc_products_selection_type', $legacy);
954 $this->deleteRawSetting('products_type');
955
956 return $legacy;
957 }
958
959 /**
960 * Delete setting item.
961 *
962 * @param $name
963 */
964 public function deleteRawSetting($name)
965 {
966 delete_post_meta($this->id, $name);
967 }
968
969 /**
970 * Returns products selection IDs.
971 *
972 * @return array
973 */
974 public function getWcProductsSelection()
975 {
976 $legacy = $this->getLegacySetting('products');
977 if (!$legacy) {
978 return $this->getRawSetting('wc_products_selection');
979 }
980 $this->storeRawSetting('wc_products_selection', $legacy);
981 $this->deleteRawSetting('products');
982
983 return $legacy;
984 }
985
986 /**
987 * Store custom labels.
988 *
989 * @param $labels
990 */
991 public function setLabels($labels)
992 {
993 $labels = array_merge($this->labels, array_filter($labels));
994 update_post_meta($this->id, 'labels', $labels);
995 }
996
997 /**
998 * Returns true if compaign can be published.
999 *
1000 * @return bool
1001 */
1002 public function isActive()
1003 {
1004 return get_post_status($this->id) === "publish" || get_post_status($this->id) === "future";
1005
1006 }
1007
1008 /**
1009 * Returns trus if the compaign is scheduled.
1010 *
1011 * @return bool
1012 */
1013 public function isScheduled()
1014 {
1015 return get_post_status($this->getId()) === "future";
1016 }
1017
1018 /**
1019 * Returns true if fixed campaign datetime is expired.
1020 *
1021 * @return bool
1022 */
1023 public function isExpired()
1024 {
1025 $endDate = date_create($this->endDatetime)->format("Y-m-d H:i");
1026 $today = date_create(current_time("mysql"))->format("Y-m-d H:i");
1027
1028 return $endDate < $today;
1029 }
1030
1031 public function isDismissed()
1032 {
1033 return isset($_COOKIE[CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed'])
1034 && $this->stickyBarDismissible === C::YES
1035 && $this->enableSticky === C::YES;
1036 }
1037
1038 /**
1039 * Returns compaign template wrapper.
1040 *
1041 * @param string
1042 *
1043 * @return string
1044 */
1045 public function wrapTemplate($content)
1046 {
1047 $campaignBuilder = new CampaignBuilder($this);
1048
1049 return $campaignBuilder->build($content);
1050 }
1051
1052 /*
1053 * Returns campaign template content.
1054 */
1055 public function buildTemplate()
1056 {
1057 $campaignBuilder = new CampaignBuilder($this);
1058
1059 return $campaignBuilder->template();
1060 }
1061 }
1062