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

868 lines 17.4 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;
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 = 16;
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 /**
80 * Control labels visibility.
81 *
82 * @var string
83 */
84 public $labelVisibility = C::YES;
85
86 /**
87 * Show/hide days block.
88 *
89 * @var string
90 */
91 public $daysVisibility = C::YES;
92
93 /**
94 * Show/hide hours block.
95 *
96 * @var string
97 */
98 public $hoursVisibility = C::YES;
99
100 /**
101 * Show/hide minutes block.
102 *
103 * @var string
104 */
105 public $minutesVisibility = C::YES;
106
107 /**
108 * Show/hide seconds block.
109 *
110 * @var string
111 */
112 public $secondsVisibility = C::YES;
113
114 /**
115 * Regular compaign end datetime.
116 *
117 * @var string
118 */
119 public $endDatetime;
120
121 /**
122 * Labels texts.
123 *
124 * @var array
125 */
126 public $labels
127 = [
128 'days' => 'days',
129 'hours' => 'hrs',
130 'minutes' => 'mins',
131 'seconds' => 'secs',
132 ];
133
134 /**
135 * Digit color.
136 *
137 * @var string
138 */
139 public $digitColor = "#000";
140
141 /**
142 * Digit size.
143 *
144 * @var int
145 */
146 public $digitSize = 30;
147
148 /**
149 * Redirect action url.
150 *
151 * @var string
152 */
153 public $redirectUrl;
154
155 /**
156 * Label size.
157 *
158 * @var int
159 */
160 public $labelSize = 13;
161
162 /**
163 * Label color.
164 *
165 * @var string
166 */
167 public $labelColor = "#000";
168
169 /**
170 * End action.
171 *
172 * @var array
173 */
174 public $actions = [];
175
176 /**
177 * WooCommerce compaign position in product page.
178 *
179 * @var int
180 */
181 public $wcPosition = C::WC_POSITION_ABOVE_TITLE;
182
183 /**
184 * Enable/disable woocommerce integration.
185 *
186 * @var string
187 */
188 public $wcEnable = C::YES;
189
190 /**
191 * WooCommerce products selection.
192 *
193 * @var array
194 */
195 public $wcProductsSelection;
196
197 /**
198 * WooCommerce products selection type.
199 *
200 * @var int
201 */
202 public $wcProductsSelectionType;
203
204 /**
205 * Timer block border color.
206 *
207 * @var string
208 */
209 public $blockBorderColor = "";
210
211 /**
212 * Timer Block border width.
213 *
214 * @var int
215 */
216
217 public $blockBorderWidth = 0;
218
219 /**
220 * Timer block radius.
221 *
222 * @var int
223 */
224 public $blockBorderRadius = 0;
225
226 /**
227 * Block size.
228 *
229 * @var int
230 */
231
232 public $blockSize = 50;
233
234 /**
235 * Block background color.
236 *
237 * @var string
238 */
239 public $blockBgColor = '';
240
241 /**
242 * Block spacing.
243 *
244 * @var int
245 */
246 public $blockSpacing = 5;
247
248 /**
249 * Block padding.
250 *
251 * @var int
252 */
253 public $blockPadding = 0;
254
255 /**
256 * Block spearator visibility.
257 *
258 * @var boolean
259 */
260 public $blockSeparatorVisibility = C::YES;
261
262 /**
263 * Label case
264 *
265 * @var string
266 */
267 public $labelCase = C::TRANSFORM_LOWERCASE;
268
269 public $customCss = '';
270
271 public function __construct($id)
272 {
273
274 $this->id = $id;
275 //$this->loadSettings();
276 }
277
278 /**
279 * Returns compaign post iD.
280 *
281 * @return int
282 */
283 public function getId()
284 {
285 return $this->id;
286 }
287
288 /**
289 * Get raw setting.
290 *
291 * @param string $name
292 * @param boolean
293 *
294 * @return mixed
295 */
296 public function getRawSetting($name, $useDefault = true)
297 {
298 $value = get_post_meta($this->id, $name, true);
299 if (!empty($value)) {
300 return $value;
301 }
302 if ($useDefault) {
303 return $this->{Helpers::snakeToCamelCase($name)};
304 }
305
306 return $value;
307 }
308
309 public function storeRawSetting($name, $value)
310 {
311 $value = !empty($value) ? $value : $this->{Helpers::snakeToCamelCase($name)};
312 update_post_meta($this->id, $name, $value);
313 }
314
315 /**
316 * Bulk save for compaign settings.
317 *
318 * @param $data
319 */
320 public function storeSettings($data)
321 {
322 foreach ($data as $prop => $value) {
323 $method = Helpers::snakeToCamelCase("set_{$prop}");
324 if (method_exists($this, $method)) {
325 $this->$method($value ?: $this->$prop);
326 } elseif (property_exists(__CLASS__, Helpers::snakeToCamelCase($prop))) {
327 $this->storeRawSetting($prop, $value);
328 }
329 }
330
331 self::buildCss();
332 }
333
334 /**
335 * Build CSS file,
336 * Then merge it with default one.
337 *
338 * @return void
339 */
340 static public function buildCss()
341 {
342
343 ob_start();
344 include HURRYT_DIR . 'includes/css.php';
345 $css = ob_get_clean();
346 $base = file_get_contents(HURRYT_DIR . '/assets/css/base.css');
347 $base .= $css;
348 file_put_contents(HURRYT_DIR . '/assets/css/hurrytimer.css', $base);
349 }
350
351 /**
352 * Load settings from database,
353 * and set object props according their matched name.
354 */
355 public function loadSettings()
356 {
357
358 $reflection = new \ReflectionObject($this);
359 foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
360 $name = $prop->getName();
361 $method = 'get' . ucfirst($name);
362 if (method_exists($this, $method)) {
363 $this->{$name} = $this->$method();
364 } else {
365 $this->{$name} = $this->getRawSetting(Helpers::camelToSnakeCase($name));
366 }
367 }
368 }
369
370 /**
371 * Returns compaign headline.
372 *
373 * @return mixed
374 */
375 public function getHeadline()
376 {
377 return get_the_title($this->id);
378 }
379
380 public function isPublished()
381 {
382 return get_post_status($this->id) === "publish";
383 }
384
385 public function isWcEnabled()
386 {
387 return $this->getWcEnable() === C::YES;
388 }
389
390 /**
391 * Check current countdown timer mode
392 *
393 * @return bool
394 */
395 public function isEvergreen()
396 {
397 return $this->getRawSetting('mode') == C::MODE_EVERGREEN;
398 }
399
400 public function isRegular()
401 {
402 return $this->getRawSetting('mode') == C::MODE_REGULAR;
403 }
404
405 /**
406 * Save compaign endtime for regular mode.
407 *
408 * @param $date
409 */
410 public function setEndDatetime($date)
411 {
412 $this->storeRawSetting('end_datetime', $date ?: $this->defaultEndDatetime());
413 }
414
415 /**
416 * Returns default end datetime for regular mode.
417 *
418 * @return false|string
419 */
420 private function defaultEndDatetime()
421 {
422 return date('Y-m-d h:i A', strtotime('+1 week'));
423 }
424
425 /**
426 * Save digit color.
427 *
428 * @param $color
429 */
430 public function setDigitColor($color)
431 {
432 $this->storeRawSetting('digit_color', $color);
433 }
434
435 /**
436 * Returns headline visibility.
437 *
438 * @return mixed
439 */
440
441 public function getHeadlineVisibility()
442 {
443 $meta = 'headline_visibility';
444
445 $value = $this->getRawSetting($meta, false);
446 if ($value === C::NO || $value === C::YES) {
447 return $value;
448 }
449
450 $legacyMeta = 'display_headline';
451 $legacyValue = filter_var($this->getLegacySetting($legacyMeta), FILTER_VALIDATE_BOOLEAN);
452 $legacyValue = $legacyValue ? C::YES : C::NO;
453
454 $this->storeRawSetting($meta, $legacyValue);
455
456 $this->deleteRawSetting($legacyMeta);
457
458 return $this->getRawSetting($meta);
459 }
460
461 /**
462 * Returns digit color.
463 * Backward compat with older versions.
464 *
465 * @return mixed
466 */
467 public function getDigitColor()
468 {
469 $value = $this->getRawSetting('digit_color', false);
470 if ($value) {
471 return $value;
472 }
473
474 $legacy = $this->getLegacySetting('text_color');
475 if (!$legacy) {
476 return $this->digitColor;
477 }
478
479 $this->setDigitColor($legacy);
480 if (!$this->getRawSetting('label_color', false)) {
481 $this->setLabelColor($legacy);
482 }
483
484 $this->deleteRawSetting('text_color');
485
486 return $this->getRawSetting('digit_color');
487 }
488
489 public function setLabelColor($color)
490 {
491 $this->storeRawSetting('label_color', $color);
492 }
493
494 /**
495 * Get label color
496 *
497 * @return mixed
498 */
499 public function getLabelColor()
500 {
501 $value = $this->getRawSetting('label_color', false);
502 if ($value) {
503 return $value;
504 }
505
506 $legacy = $this->getLegacySetting('text_color');
507 if (!$legacy) {
508 return $this->labelColor;
509 }
510
511 $this->setLabelColor($legacy);
512 if (!$this->getRawSetting('digit_color', false)) {
513 $this->setDigitColor($legacy);
514 }
515
516 $this->deleteRawSetting('text_color');
517
518 return $this->getRawSetting('label_color');
519
520 }
521
522
523 /**
524 *
525 * Return timer digit size.
526 * Backward comapt. with older versions.
527 *
528 * @since 1.2.4
529 * @return int
530 */
531 public function getDigitSize()
532 {
533 $meta = 'digit_size';
534 $value = $this->getRawSetting($meta, false);
535 if (!empty($value)) {
536 return $value;
537 }
538 $legacy = $this->getLegacySetting('text_size');
539
540 if (empty($legacy)) {
541 return $this->digitSize;
542 }
543
544 $this->setDigitSize($legacy);
545
546 $this->deleteRawSetting('text_size');
547
548 return $this->getRawSetting('digit_size');
549 }
550
551 /**
552 * Save timer digit size.
553 *
554 * @param $size
555 */
556 public function setDigitSize($size)
557 {
558 $this->storeRawSetting('digit_size', $size);
559 }
560
561 /**
562 * Save timer label size.
563 *
564 * @param $size
565 */
566 public function setLabelSize($size)
567 {
568 $this->storeRawSetting('label_size', $size);
569 }
570
571 public function setHeadlineSize($size)
572 {
573 $this->storeRawSetting('headline_size', $size);
574 }
575
576 /**
577 * Returns evergreen duration.
578 *
579 * @return array
580 */
581 public function getDuration()
582 {
583 $default = [0, 0, 0, 0];
584 $duration = $this->getRawSetting('duration');
585 if (is_array($duration)) {
586 $duration = array_merge($duration, $default);
587
588 return array_map('intval', $duration);
589 }
590
591 return $default;
592 }
593
594 /**
595 * Returns actions array.
596 *
597 * @return array
598 */
599 public function getActions()
600 {
601 $legacy = $this->getLegacySetting('end_action');
602 if ($legacy) {
603 $redirectUrl = $this->getLegacySetting('redirect_url');
604 $actions = [
605 [
606 'id' => intval($legacy),
607 'redirectUrl' => $redirectUrl,
608 ],
609 ];
610 $this->storeRawSetting('actions', $actions);
611 $this->deleteRawSetting('end_action');
612 $this->deleteRawSetting('redirect_url');
613
614 return $this->mergeActions($actions);
615 }
616
617 return $this->mergeActions($this->getRawSetting('actions'));
618 }
619
620 private function mergeActions($actions)
621 {
622 $defaults = [
623 [
624 'id' => C::ACTION_NONE,
625 'redirectUrl' => '',
626 'message' => '',
627 'wcStockStatus' => '',
628 ],
629 ];
630
631 if (count($actions) === 0) {
632 return $defaults;
633 }
634
635 return array_map(function ($action) use ($defaults) {
636 $action['id'] = (int) $action['id'];
637
638 return array_merge($defaults[0], $action);
639 }, $actions);
640 }
641
642 /**
643 * Returns evergreen duration in seconds.
644 *
645 * @return int
646 */
647 public function getDurationInSeconds()
648 {
649 list($d, $h, $m, $s) = $this->getDuration();
650
651 return $d * DAY_IN_SECONDS +
652 $h * HOUR_IN_SECONDS +
653 $m * MINUTE_IN_SECONDS +
654 $s;
655 }
656
657 /**
658 * Returns end datetime.
659 *
660 * @return string
661 */
662 public function getEndDatetime()
663 {
664 return $this->getRawSetting('end_datetime') ?: $this->defaultEndDatetime();
665 }
666
667 /**
668 * Returns compaign publish datetime.
669 *
670 * @return mixed
671 */
672 public function getStartTimestamp()
673 {
674 return get_the_date($this->id);
675 }
676
677 /**
678 * Returns position in WooCommerce product page.
679 *
680 * @return mixed
681 */
682 public function getWcPosition()
683 {
684 $legacy = $this->getLegacySetting('position');
685
686 if (!$legacy) {
687 return $this->getRawSetting('wc_position');
688 }
689 $this->storeRawSetting('wc_position', $legacy);
690 $this->deleteRawSetting('position');
691
692 return $legacy;
693 }
694
695 private function getLegacySetting($name)
696 {
697 return get_post_meta($this->id, $name, true);
698 }
699
700 /**
701 * Returns true if WooCommerce integration is enabled.
702 *
703 * @return mixed
704 */
705 public function getWcEnable()
706 {
707 $value = $this->getRawSetting('wc_enable', false);
708
709 if ($value === C::YES || $value === C::NO) {
710 return $value;
711 }
712
713 $legacy = filter_var($value, FILTER_VALIDATE_BOOLEAN);
714 if ($legacy) {
715 $this->storeRawSetting('wc_enable', C::YES);
716 } else {
717 $this->storeRawSetting('wc_enable', C::NO);
718 }
719
720 return $this->getRawSetting('wc_enable');
721 }
722
723 /**
724 * Returns true if label should be displayed.
725 *
726 * @return mixed
727 */
728 public function getLabelVisibility()
729 {
730 $meta = 'label_visibility';
731 $value = $this->getRawSetting($meta, false);
732 if ($value === C::NO || $value === C::YES) {
733 return $value;
734 }
735
736 $legacy = filter_var($this->getLegacySetting('display_labels'), FILTER_VALIDATE_BOOLEAN);
737 $legacy = $legacy ? C::YES : C::NO;
738 $this->storeRawSetting($meta, $legacy);
739 $this->deleteRawSetting('display_labels');
740
741 return $this->getRawSetting($meta);
742
743 }
744
745 /**
746 *
747 * Returns restart type.
748 *
749 * @return int
750 */
751 public function getRestart()
752 {
753 return $this->getRawSetting('restart') ?: C::RESTART_NONE;
754 }
755
756 /**
757 * Returns WooCommerce products selection type.
758 *
759 * @return string
760 */
761 public function getWcProductsSelectionType()
762 {
763 $legacy = $this->getLegacySetting('products_type');
764 if (!$legacy) {
765 return $this->getRawSetting('wc_products_selection_type');
766 }
767 $this->storeRawSetting('wc_products_selection_type', $legacy);
768 $this->deleteRawSetting('products_type');
769
770 return $legacy;
771 }
772
773 /**
774 * Delete setting item.
775 *
776 * @param $name
777 */
778 public function deleteRawSetting($name)
779 {
780 delete_post_meta($this->id, $name);
781 }
782
783 /**
784 * Returns products selection IDs.
785 *
786 * @return array
787 */
788 public function getWcProductsSelection()
789 {
790 $legacy = $this->getLegacySetting('products');
791 if (!$legacy) {
792 return $this->getRawSetting('wc_products_selection');
793 }
794 $this->storeRawSetting('wc_products_selection', $legacy);
795 $this->deleteRawSetting('products');
796
797 return $legacy;
798 }
799
800 /**
801 * Store custom labels.
802 *
803 * @param $labels
804 */
805 public function setLabels($labels)
806 {
807 $labels = array_merge($this->labels, array_filter($labels));
808 update_post_meta($this->id, 'labels', $labels);
809 }
810
811 /**
812 * Returns true if compaign can be published.
813 *
814 * @return bool
815 */
816 public function isActive()
817 {
818 return get_post_status($this->id) === "publish" || get_post_status($this->id) === "future";
819
820 }
821
822 /**
823 * Returns trus if the compaign is scheduled.
824 *
825 * @return bool
826 */
827 public function isScheduled()
828 {
829 return get_post_status($this->getId()) === "future";
830 }
831
832 /**
833 * Returns true if fixed campaign datetime is expired.
834 *
835 * @return bool
836 */
837 public function isExpired()
838 {
839 $endDate = date_create($this->endDatetime)->format("Y-m-d H:i");
840 $today = date_create(current_time("mysql"))->format("Y-m-d H:i");
841
842 return $endDate < $today;
843 }
844
845 /**
846 * Returns compaign template wrapper.
847 *
848 * @param string
849 *
850 * @return string
851 */
852 public function wrapTemplate($content)
853 {
854 $campaignBuilder = new CampaignBuilder($this);
855 return $campaignBuilder->build($content);
856 }
857
858 /*
859 * Returns campaign template content.
860 */
861 public function buildTemplate()
862 {
863 $campaignBuilder = new CampaignBuilder($this);
864
865 return $campaignBuilder->template();
866 }
867 }
868