PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.1.3
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.1.3
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.3, at includes/Campaign.php

1,046 lines 20.8 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 * Show sticky bar on all pages.
329 *
330 * @var string
331 */
332 public $stickyBarShowOnAllPages = C::YES;
333
334
335 /**
336 * Show sticky bar close button?
337 *
338 * @var string
339 */
340 public $stickyBarDismissible = C::YES;
341
342
343 /**
344 * CTA settings.
345 *
346 * @var array
347 */
348 public $callToAction
349 = [
350 'enabled' => C::NO,
351 'new_tab' => C::NO,
352 'url' => '',
353 'text' => 'Learn More',
354 'text_size' => 15,
355 'text_color' => '#fff',
356 'bg_color' => '#000',
357 'y_padding' => 10,
358 'x_padding' => 15,
359 'border_radius' => 3,
360 'spacing' => 5,
361 ];
362
363 /**
364 * Campaign elements display
365 * values: block,inline
366 *
367 * @var string
368 */
369 public $campaignDisplay = 'block';
370
371 /**
372 * Campaign aligments
373 * values: center,left,right
374 *
375 * @var string
376 */
377 public $campaignAlign = 'center';
378
379 /**
380 * Campaign spacing.
381 *
382 * @var integer
383 */
384 public $campaignSpacing = 10;
385
386 /**
387 * Campaign horizontal padding.
388 *
389 * @var integer
390 */
391 public $campaignXPadding = 10;
392
393 /**
394 * Campaign vertical padding.
395 *
396 * @var integer
397 */
398 public $campaignYPadding = 10;
399
400 public function __construct($id)
401 {
402
403 $this->id = $id;
404 }
405
406 /**
407 * Returns compaign post iD.
408 *
409 * @return int
410 */
411 public function getId()
412 {
413 return $this->id;
414 }
415
416 public function showStickyBarOn($pageId)
417 {
418 if ($this->enableSticky === C::NO) {
419 return true;
420 }
421 if ($this->getStickyBarShowOnAllPages() === C::YES) {
422 return true;
423 }
424
425 if ($pageId <= 0) {
426 return false;
427 }
428
429 $pages = $this->getStickyBarPages();
430
431 foreach ($pages as $page) {
432 if ($page == $pageId) {
433 return true;
434 }
435 }
436
437 return false;
438
439 }
440
441 public function getStickyBarPages()
442 {
443 return $this->getRawSetting('sticky_bar_pages');
444 }
445
446 public function getStickyBarShowOnAllPages()
447 {
448 return $this->getRawSetting('sticky_bar_show_on_all_pages');
449 }
450
451 /**
452 * Get raw setting.
453 *
454 * @param string $name
455 * @param boolean
456 *
457 * @return mixed
458 */
459 public function getRawSetting($name, $useDefault = true)
460 {
461 $value = get_post_meta($this->id, $name, true);
462 if ( ! empty($value)) {
463 return $value;
464 }
465 if ($useDefault) {
466 return $this->{Helpers::snakeToCamelCase($name)};
467 }
468
469 return $value;
470 }
471
472 public function storeRawSetting($name, $value)
473 {
474 $value = ! empty($value) ? $value : $this->{Helpers::snakeToCamelCase($name)};
475 update_post_meta($this->id, $name, $value);
476 }
477
478 /**
479 * Bulk save for compaign settings.
480 *
481 * @param $data
482 */
483 public function storeSettings($data)
484 {
485
486 foreach ($data as $prop => $value) {
487 $method = Helpers::snakeToCamelCase("set_{$prop}");
488 if (method_exists($this, $method)) {
489 $this->$method($value ?: $this->$prop);
490 } elseif (property_exists(__CLASS__, Helpers::snakeToCamelCase($prop))) {
491 $this->storeRawSetting($prop, $value);
492 }
493 }
494
495 self::buildCss();
496 }
497
498 /**
499 * Build User CSS,
500 * Then merge it with base one.
501 *
502 * @return void
503 */
504 static public function buildCss()
505 {
506
507 // Build and return CSS
508 ob_start();
509 include HURRYT_DIR . 'includes/css.php';
510 $css = ob_get_clean();
511 $base = file_get_contents(HURRYT_DIR . '/assets/css/base.css');
512
513 // Merge built and base css.
514 $base .= $css;
515
516 // Save to public file.
517 file_put_contents(HURRYT_DIR . '/assets/css/hurrytimer.css', $base);
518 }
519
520 /**
521 * Load settings from database,
522 * and set object props according their matched name.
523 */
524 public function loadSettings()
525 {
526
527 $reflection = new \ReflectionObject($this);
528 foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
529 $name = $prop->getName();
530 $method = 'get' . ucfirst($name);
531 if (method_exists($this, $method)) {
532 $this->{$name} = $this->$method();
533 } else {
534 $this->{$name} = $this->getRawSetting(Helpers::camelToSnakeCase($name));
535 }
536 }
537 }
538
539 /**
540 * Returns compaign headline.
541 *
542 * @return mixed
543 */
544 public function getHeadline()
545 {
546 return $this->id ? get_the_title($this->id) : $this->headline;
547 }
548
549 public function isPublished()
550 {
551 return get_post_status($this->id) === "publish";
552 }
553
554 public function isWcEnabled()
555 {
556 return $this->getWcEnable() === C::YES;
557 }
558
559 /**
560 * Check current countdown timer mode
561 *
562 * @return bool
563 */
564 public function isEvergreen()
565 {
566 return $this->getRawSetting('mode') == C::MODE_EVERGREEN;
567 }
568
569 public function isRegular()
570 {
571 return $this->getRawSetting('mode') == C::MODE_REGULAR;
572 }
573
574 /**
575 * Save compaign endtime for regular mode.
576 *
577 * @param $date
578 */
579 public function setEndDatetime($date)
580 {
581 $this->storeRawSetting('end_datetime', $date ?: $this->defaultEndDatetime());
582 }
583
584 /**
585 * Returns default end datetime for regular mode.
586 *
587 * @return false|string
588 */
589 private function defaultEndDatetime()
590 {
591 return date('Y-m-d h:i A', strtotime('+1 week'));
592 }
593
594 /**
595 * Save digit color.
596 *
597 * @param $color
598 */
599 public function setDigitColor($color)
600 {
601 $this->storeRawSetting('digit_color', $color);
602 }
603
604 /**
605 * Returns headline visibility.
606 *
607 * @return mixed
608 */
609
610 public function getHeadlineVisibility()
611 {
612 $meta = 'headline_visibility';
613
614 $value = $this->getRawSetting($meta, false);
615 if ($value === C::NO || $value === C::YES) {
616 return $value;
617 }
618
619 $legacyMeta = 'display_headline';
620 $legacyValue = filter_var($this->getLegacySetting($legacyMeta), FILTER_VALIDATE_BOOLEAN);
621 $legacyValue = $legacyValue ? C::YES : C::NO;
622
623 $this->storeRawSetting($meta, $legacyValue);
624
625 $this->deleteRawSetting($legacyMeta);
626
627 return $this->getRawSetting($meta);
628 }
629
630 /**
631 * Returns digit color.
632 * Backward compat with older versions.
633 *
634 * @return mixed
635 */
636 public function getDigitColor()
637 {
638 $value = $this->getRawSetting('digit_color', false);
639 if ($value) {
640 return $value;
641 }
642
643 $legacy = $this->getLegacySetting('text_color');
644 if ( ! $legacy) {
645 return $this->digitColor;
646 }
647
648 $this->setDigitColor($legacy);
649 if ( ! $this->getRawSetting('label_color', false)) {
650 $this->setLabelColor($legacy);
651 }
652
653 $this->deleteRawSetting('text_color');
654
655 return $this->getRawSetting('digit_color');
656 }
657
658 public function setLabelColor($color)
659 {
660 $this->storeRawSetting('label_color', $color);
661 }
662
663 /**
664 * Get label color
665 *
666 * @return mixed
667 */
668 public function getLabelColor()
669 {
670 $value = $this->getRawSetting('label_color', false);
671 if ($value) {
672 return $value;
673 }
674
675 $legacy = $this->getLegacySetting('text_color');
676 if ( ! $legacy) {
677 return $this->labelColor;
678 }
679
680 $this->setLabelColor($legacy);
681 if ( ! $this->getRawSetting('digit_color', false)) {
682 $this->setDigitColor($legacy);
683 }
684
685 $this->deleteRawSetting('text_color');
686
687 return $this->getRawSetting('label_color');
688
689 }
690
691
692 /**
693 *
694 * Return timer digit size.
695 * Backward comapt. with older versions.
696 *
697 * @return int
698 * @since 1.2.4
699 */
700 public function getDigitSize()
701 {
702 $meta = 'digit_size';
703 $value = $this->getRawSetting($meta, false);
704 if ( ! empty($value)) {
705 return $value;
706 }
707 $legacy = $this->getLegacySetting('text_size');
708
709 if (empty($legacy)) {
710 return $this->digitSize;
711 }
712
713 $this->setDigitSize($legacy);
714
715 $this->deleteRawSetting('text_size');
716
717 return $this->getRawSetting('digit_size');
718 }
719
720 /**
721 * Save timer digit size.
722 *
723 * @param $size
724 */
725 public function setDigitSize($size)
726 {
727 $this->storeRawSetting('digit_size', $size);
728 }
729
730 /**
731 * Save timer label size.
732 *
733 * @param $size
734 */
735 public function setLabelSize($size)
736 {
737 $this->storeRawSetting('label_size', $size);
738 }
739
740 public function setHeadlineSize($size)
741 {
742 $this->storeRawSetting('headline_size', $size);
743 }
744
745 /**
746 * Returns evergreen duration.
747 *
748 * @return array
749 */
750 public function getDuration()
751 {
752 $default = [0, 0, 0, 0];
753 $duration = $this->getRawSetting('duration');
754 if (is_array($duration)) {
755 $duration = array_merge($duration, $default);
756
757 return array_map('intval', $duration);
758 }
759
760 return $default;
761 }
762
763 /**
764 * Returns actions array.
765 *
766 * @return array
767 */
768 public function getActions()
769 {
770 $legacy = $this->getLegacySetting('end_action');
771 if ($legacy) {
772 $redirectUrl = $this->getLegacySetting('redirect_url');
773 $actions = [
774 [
775 'id' => intval($legacy),
776 'redirectUrl' => $redirectUrl,
777 ],
778 ];
779 $this->storeRawSetting('actions', $actions);
780 $this->deleteRawSetting('end_action');
781 $this->deleteRawSetting('redirect_url');
782
783 return $this->mergeActions($actions);
784 }
785
786 return $this->mergeActions($this->getRawSetting('actions'));
787 }
788
789 private function mergeActions($actions)
790 {
791 $defaults = [
792 [
793 'id' => C::ACTION_NONE,
794 'redirectUrl' => '',
795 'message' => '',
796 'wcStockStatus' => '',
797 ],
798 ];
799
800 if (count($actions) === 0) {
801 return $defaults;
802 }
803
804 return array_map(function ($action) use ($defaults) {
805 $action['id'] = (int)$action['id'];
806
807 return array_merge($defaults[0], $action);
808 }, $actions);
809 }
810
811 /**
812 * Returns evergreen duration in seconds.
813 *
814 * @return int
815 */
816 public function getDurationInSeconds()
817 {
818 list($d, $h, $m, $s) = $this->getDuration();
819
820 return $d * DAY_IN_SECONDS +
821 $h * HOUR_IN_SECONDS +
822 $m * MINUTE_IN_SECONDS +
823 $s;
824 }
825
826 /**
827 * Returns end datetime.
828 *
829 * @return string
830 */
831 public function getEndDatetime()
832 {
833 return $this->getRawSetting('end_datetime') ?: $this->defaultEndDatetime();
834 }
835
836 /**
837 * Returns compaign publish datetime.
838 *
839 * @return mixed
840 */
841 public function getStartTimestamp()
842 {
843 return get_the_date($this->id);
844 }
845
846 /**
847 * Returns position in WooCommerce product page.
848 *
849 * @return mixed
850 */
851 public function getWcPosition()
852 {
853 $legacy = $this->getLegacySetting('position');
854
855 if ( ! $legacy) {
856 return $this->getRawSetting('wc_position');
857 }
858 $this->storeRawSetting('wc_position', $legacy);
859 $this->deleteRawSetting('position');
860
861 return $legacy;
862 }
863
864 private function getLegacySetting($name)
865 {
866 return get_post_meta($this->id, $name, true);
867 }
868
869 /**
870 * Returns true if WooCommerce integration is enabled.
871 *
872 * @return mixed
873 */
874 public function getWcEnable()
875 {
876 $value = $this->getRawSetting('wc_enable', false);
877
878 if ($value === C::YES || $value === C::NO) {
879 return $value;
880 }
881
882 $legacy = filter_var($value, FILTER_VALIDATE_BOOLEAN);
883 if ($legacy) {
884 $this->storeRawSetting('wc_enable', C::YES);
885 } else {
886 $this->storeRawSetting('wc_enable', C::NO);
887 }
888
889 return $this->getRawSetting('wc_enable');
890 }
891
892 /**
893 * Returns true if label should be displayed.
894 *
895 * @return mixed
896 */
897 public function getLabelVisibility()
898 {
899 $meta = 'label_visibility';
900 $value = $this->getRawSetting($meta, false);
901 if ($value === C::NO || $value === C::YES) {
902 return $value;
903 }
904
905 $legacy = filter_var($this->getLegacySetting('display_labels'), FILTER_VALIDATE_BOOLEAN);
906 $legacy = $legacy ? C::YES : C::NO;
907 $this->storeRawSetting($meta, $legacy);
908 $this->deleteRawSetting('display_labels');
909
910 return $this->getRawSetting($meta);
911
912 }
913
914 /**
915 *
916 * Returns restart type.
917 *
918 * @return int
919 */
920 public function getRestart()
921 {
922 return $this->getRawSetting('restart') ?: C::RESTART_NONE;
923 }
924
925 /**
926 * Returns WooCommerce products selection type.
927 *
928 * @return string
929 */
930 public function getWcProductsSelectionType()
931 {
932 $legacy = $this->getLegacySetting('products_type');
933 if ( ! $legacy) {
934 return $this->getRawSetting('wc_products_selection_type');
935 }
936 $this->storeRawSetting('wc_products_selection_type', $legacy);
937 $this->deleteRawSetting('products_type');
938
939 return $legacy;
940 }
941
942 /**
943 * Delete setting item.
944 *
945 * @param $name
946 */
947 public function deleteRawSetting($name)
948 {
949 delete_post_meta($this->id, $name);
950 }
951
952 /**
953 * Returns products selection IDs.
954 *
955 * @return array
956 */
957 public function getWcProductsSelection()
958 {
959 $legacy = $this->getLegacySetting('products');
960 if ( ! $legacy) {
961 return $this->getRawSetting('wc_products_selection');
962 }
963 $this->storeRawSetting('wc_products_selection', $legacy);
964 $this->deleteRawSetting('products');
965
966 return $legacy;
967 }
968
969 /**
970 * Store custom labels.
971 *
972 * @param $labels
973 */
974 public function setLabels($labels)
975 {
976 $labels = array_merge($this->labels, array_filter($labels));
977 update_post_meta($this->id, 'labels', $labels);
978 }
979
980 /**
981 * Returns true if compaign can be published.
982 *
983 * @return bool
984 */
985 public function isActive()
986 {
987 return get_post_status($this->id) === "publish" || get_post_status($this->id) === "future";
988
989 }
990
991 /**
992 * Returns trus if the compaign is scheduled.
993 *
994 * @return bool
995 */
996 public function isScheduled()
997 {
998 return get_post_status($this->getId()) === "future";
999 }
1000
1001 /**
1002 * Returns true if fixed campaign datetime is expired.
1003 *
1004 * @return bool
1005 */
1006 public function isExpired()
1007 {
1008 $endDate = date_create($this->endDatetime)->format("Y-m-d H:i");
1009 $today = date_create(current_time("mysql"))->format("Y-m-d H:i");
1010
1011 return $endDate < $today;
1012 }
1013
1014
1015 public function isDismissed()
1016 {
1017 return isset($_COOKIE[CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed'])
1018 && $this->stickyBarDismissible === C::YES
1019 && $this->enableSticky === C::YES;
1020 }
1021
1022 /**
1023 * Returns compaign template wrapper.
1024 *
1025 * @param string
1026 *
1027 * @return string
1028 */
1029 public function wrapTemplate($content)
1030 {
1031 $campaignBuilder = new CampaignBuilder($this);
1032
1033 return $campaignBuilder->build($content);
1034 }
1035
1036 /*
1037 * Returns campaign template content.
1038 */
1039 public function buildTemplate()
1040 {
1041 $campaignBuilder = new CampaignBuilder($this);
1042
1043 return $campaignBuilder->template();
1044 }
1045 }
1046