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

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