PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.2.16
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.2.16
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
← All changes | includes/Campaign.php +509 -211 2.1.62.2.16 View file →
@@ -1,9 +1,11 @@
1 1 <?php
2 2
3 3 namespace Hurrytimer;
4 4
5 -use \Hurrytimer\Utils\Helpers;
5 +use Carbon\Carbon;
6 +use Carbon\CarbonPeriod;
7 +use Hurrytimer\Utils\Helpers;
6 8
7 9 class Campaign
8 10 {
9 11
@@ -32,8 +34,73 @@
32 34 */
33 35 public $duration;
34 36
35 37 /**
38 + * Recurrence duration
39 + *
40 + * @var int
41 + */
42 + public $recurringDuration;
43 +
44 + /**
45 + * Recurrence start date/time
46 + *
47 + * @var string
48 + */
49 + public $recurringStartTime;
50 +
51 + /**
52 + * Recurrence end option
53 + *
54 + * @var int
55 + */
56 + public $recurringEnd = C::RECURRING_END_NEVER;
57 +
58 + /**
59 + * Recurrence frequency
60 + *
61 + * @see C
62 + * @var string
63 + */
64 + public $recurringFrequency = C::RECURRING_DAILY;
65 +
66 + /**
67 + * Recurrence interval
68 + *
69 + * @var int
70 + */
71 + public $recurringInterval = 1;
72 +
73 + /**
74 + * Recurrence count
75 + *
76 + * @var int
77 + */
78 + public $recurringCount = 2;
79 +
80 + /**
81 + * Recurrence end date/time
82 + *
83 + * @var
84 + */
85 + public $recurringUntil;
86 +
87 + /**
88 + * Recurrence allowed days
89 + *
90 + * @var array
91 + */
92 + public $recurringDays = [ 0, 1, 2, 3, 4, 5, 6 ];
93 +
94 + /**
95 + * Recurrence timezone
96 + *
97 + * @todo Not used
98 + * @var mixed|string|void
99 + */
100 + public $recurringTimezone;
101 +
102 + /**
36 103 * Restart option after expiration.
37 104 *
38 105 * @see \Hurrytimer\CampaignRestart
39 106 *
@@ -125,14 +192,14 @@
125 192 *
126 193 * @var array
127 194 */
128 195 public $labels
129 - = [
130 - 'days' => 'days',
131 - 'hours' => 'hrs',
132 - 'minutes' => 'mins',
133 - 'seconds' => 'secs',
134 - ];
196 + = [
197 + 'days' => 'days',
198 + 'hours' => 'hrs',
199 + 'minutes' => 'mins',
200 + 'seconds' => 'secs',
201 + ];
135 202
136 203 /**
137 204 * Digit color.
138 205 *
@@ -202,8 +269,10 @@
202 269 * @var int
203 270 */
204 271 public $wcProductsSelectionType;
205 272
273 + public $wcConditions;
274 +
206 275 /**
207 276 * Timer block border color.
208 277 *
209 278 * @var string
@@ -343,21 +412,21 @@
343 412 *
344 413 * @var array
345 414 */
346 415 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 - ];
416 + = [
417 + 'enabled' => C::NO,
418 + 'new_tab' => C::NO,
419 + 'url' => '',
420 + 'text' => 'Learn More',
421 + 'text_size' => 15,
422 + 'text_color' => '#fff',
423 + 'bg_color' => '#000',
424 + 'y_padding' => 10,
425 + 'x_padding' => 15,
426 + 'border_radius' => 3,
427 + 'spacing' => 5,
428 + ];
360 429
361 430 /**
362 431 * Campaign elements display
363 432 * values: block,inline
@@ -394,12 +463,14 @@
394 463 * @var integer
395 464 */
396 465 public $campaignYPadding = 10;
397 466
398 - public function __construct($id)
467 + public function __construct( $id )
399 468 {
400 -
401 - $this->id = $id;
469 + // Default recurring timezone.
470 + $this->recurringTimezone = Utils\Helpers::getSiteTimezone();
471 + $this->recurringStartTime = Carbon::now( hurryt_tz() )->format( 'Y-m-d h:i A' );
472 + $this->id = $id;
402 473 }
403 474
404 475 /**
405 476 * Returns compaign post iD.
@@ -410,64 +481,69 @@
410 481 {
411 482 return $this->id;
412 483 }
413 484
414 - public function showStickyBarOn($pageId)
485 + public function showStickyBarOn( $pageId )
415 486 {
416 -
417 -
418 - if ($this->enableSticky === C::NO) {
487 + if ( $this->enableSticky === C::NO ) {
419 488 return true;
420 489 }
421 490
422 - switch ($this->getStickyBarDisplayOn()) {
491 + $can_show = false;
492 + switch ( $this->getStickyBarDisplayOn() ) {
423 493 case 'all_pages':
424 - return true;
494 + $can_show = true;
495 + break;
425 496 case 'wc_products_pages':
426 -
427 - if (function_exists('is_product') && is_product()) {
497 + if ( function_exists( 'is_product' ) && is_product() ) {
428 498 $wc_campaign = new WCCampaign();
429 - return ($wc_campaign->hasActiveCampaign($this, $pageId));
499 + $can_show = ( $wc_campaign->hasActiveCampaign( $this, $pageId ) );
500 + } else {
501 + $can_show = false;
430 502 }
431 - return false;
432 -
503 + break;
433 504 case 'specific_pages':
434 - $pages = $this->getStickyBarPages();
435 - foreach ($pages as $page) {
436 - if ($page == $pageId) {
437 - return true;
438 - }
439 - }
505 + $pages = $this->getStickyBarPages();
506 + $can_show = in_array( $pageId, $pages );
507 + break;
440 508 }
441 509
442 - return true;
510 + return apply_filters( 'hurryt_show_sticky_bar', $can_show, $this->getId() );
443 511
444 512 }
445 513
446 514 public function getStickyBarPages()
447 515 {
448 - return $this->getRawSetting('sticky_bar_pages');
516 + $pagesIds = $this->getRawSetting( 'sticky_bar_pages' );
517 +
518 + return array_filter( array_map( 'intval', $pagesIds ) );
449 519 }
450 520
521 + public function setStickyBarPages( $value )
522 + {
523 + $this->storeRawSetting( 'sticky_bar_pages', $value );
524 + }
525 +
451 526 public function getStickyBarDisplayOn()
452 527 {
453 528
454 529 // backward compat.
455 - $all_pages = $this->getRawSetting('sticky_bar_show_on_all_pages', false);
530 + $all_pages = $this->getRawSetting( 'sticky_bar_show_on_all_pages', false );
456 531
457 - if ($all_pages === C::YES) {
458 - $this->setStickyBarDisplayOn('all_pages');
532 + if ( $all_pages === C::YES ) {
533 + $this->setStickyBarDisplayOn( 'all_pages' );
459 534 }
460 535
461 - $this->deleteRawSetting('sticky_bar_show_on_all_pages');
536 + $this->deleteRawSetting( 'sticky_bar_show_on_all_pages' );
462 537
463 - $value = $this->getRawSetting('_hurryt_sticky_bar_display_on', false);
464 - return !empty($value) ?: $this->stickyBarDisplayOn;
538 + $value = $this->getRawSetting( '_hurryt_sticky_bar_display_on', false );
539 +
540 + return ! empty( $value ) ? $value : $this->stickyBarDisplayOn;
465 541 }
466 542
467 - public function setStickyBarDisplayOn($value)
543 + public function setStickyBarDisplayOn( $value )
468 544 {
469 - $this->storeRawSetting('_hurryt_sticky_bar_display_on', $value);
545 + $this->storeRawSetting( '_hurryt_sticky_bar_display_on', $value );
470 546 }
471 547
472 548 /**
473 549 * Get raw setting.
@@ -476,25 +552,30 @@
476 552 * @param boolean
477 553 *
478 554 * @return mixed
479 555 */
480 - public function getRawSetting($name, $useDefault = true)
556 + public function getRawSetting( $name, $useDefault = true )
481 557 {
482 - $value = get_post_meta($this->id, $name, true);
483 - if (!empty($value)) {
558 + $value = get_post_meta( $this->id, $name, true );
559 + if ( ! empty( $value ) ) {
484 560 return $value;
485 561 }
486 - if ($useDefault) {
487 - return $this->{Helpers::snakeToCamelCase($name)};
562 + if ( $useDefault ) {
563 + return $this->{Helpers::snakeToCamelCase( $name )};
488 564 }
489 565
490 566 return $value;
491 567 }
492 568
493 - public function storeRawSetting($name, $value)
569 + public function storeRawSetting( $name, $value )
494 570 {
495 - $value = !empty($value) ? $value : $this->{Helpers::snakeToCamelCase($name)};
496 - update_post_meta($this->id, $name, $value);
571 + $value = ! empty( $value ) ? $value : '';
572 + $getterMethod = Helpers::snakeToCamelCase( $name );
573 + if ( empty( $value ) && method_exists( $this, $getterMethod ) ) {
574 + $value = $this->{$getterMethod}();
575 + }
576 +
577 + update_post_meta( $this->id, $name, $value );
497 578 }
498 579
499 580 /**
500 581 * Bulk save for compaign settings.
@@ -500,24 +581,40 @@
500 581 * Bulk save for compaign settings.
501 582 *
502 583 * @param $data
503 584 */
504 - public function storeSettings($data)
585 + public function storeSettings( $data )
505 586 {
506 -
507 - foreach ($data as $prop => $value) {
508 - $method = Helpers::snakeToCamelCase("set_{$prop}");
509 - if (method_exists($this, $method)) {
510 - $this->$method($value ?: $this->$prop);
511 - } elseif (property_exists(__CLASS__, Helpers::snakeToCamelCase($prop))) {
512 - $this->storeRawSetting($prop, $value);
587 + foreach ( $data as $prop => $value ) {
588 + $method = Helpers::snakeToCamelCase( "set_{$prop}" );
589 + if ( method_exists( $this, $method ) ) {
590 + $this->$method( $value ?: $this->$prop );
591 + } elseif ( property_exists( __CLASS__, Helpers::snakeToCamelCase( $prop ) ) ) {
592 + $this->storeRawSetting( $prop, $value );
513 593 }
514 594 }
515 -
595 + if ( ! isset( $data[ 'wc_conditions' ] ) ) {
596 + $this->setWcConditions( [] );
597 + }
598 + if ( ! isset( $data[ 'sticky_bar_pages' ] ) ) {
599 + $this->setStickyBarPages( [] );
600 + }
516 601 self::buildCss();
517 602 }
518 603
604 + //removeIf(pro)
519 605 /**
606 + * @param int $mode
607 + */
608 + public function setMode($mode)
609 + {
610 + if($mode == C::MODE_RECURRING){
611 + return;
612 + }
613 + $this->storeRawSetting('mode',$mode);
614 + }
615 + //endRemoveIf(pro)
616 + /**
520 617 * Build User CSS,
521 618 * Then merge it with base one.
522 619 *
523 620 * @return void
@@ -527,33 +624,36 @@
527 624
528 625 // Build and return CSS
529 626 ob_start();
530 627 include HURRYT_DIR . 'includes/css.php';
531 - $css = ob_get_clean();
532 - $base = file_get_contents(HURRYT_DIR . '/assets/css/base.css');
628 + $css = ob_get_clean();
629 + $base = file_get_contents( HURRYT_DIR . '/assets/css/base.css' );
533 630
534 631 // Merge built and base css.
535 632 $base .= $css;
536 633
537 634 // Save to public file.
538 - file_put_contents(HURRYT_DIR . '/assets/css/hurrytimer.css', $base);
635 + file_put_contents( HURRYT_DIR . '/assets/css/hurrytimer.css', $base );
539 636 }
540 637
541 - /**
542 - * Load settings from database,
543 - * and set object props according their matched name.
544 - */
638 + private function setRecurringDates( $dates )
639 + {
640 + foreach ( $dates as $d ) {
641 + add_post_meta( $this->getId(), '_hurryt_recurring_dates', $d );
642 + }
643 + }
644 +
545 645 public function loadSettings()
546 646 {
547 647
548 - $reflection = new \ReflectionObject($this);
549 - foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
550 - $name = $prop->getName();
551 - $method = 'get' . ucfirst($name);
552 - if (method_exists($this, $method)) {
648 + $reflection = new \ReflectionObject( $this );
649 + foreach ( $reflection->getProperties( \ReflectionProperty::IS_PUBLIC ) as $prop ) {
650 + $name = $prop->getName();
651 + $method = 'get' . ucfirst( $name );
652 + if ( method_exists( $this, $method ) ) {
553 653 $this->{$name} = $this->$method();
554 654 } else {
555 - $this->{$name} = $this->getRawSetting(Helpers::camelToSnakeCase($name));
655 + $this->{$name} = $this->getRawSetting( Helpers::camelToSnakeCase( $name ) );
556 656 }
557 657 }
558 658 }
559 659
@@ -563,14 +663,14 @@
563 663 * @return mixed
564 664 */
565 665 public function getHeadline()
566 666 {
567 - return $this->id ? get_the_title($this->id) : $this->headline;
667 + return $this->id ? get_the_title( $this->id ) : $this->headline;
568 668 }
569 669
570 670 public function isPublished()
571 671 {
572 - return get_post_status($this->id) === "publish";
672 + return get_post_status( $this->id ) === "publish";
573 673 }
574 674
575 675 public function isWcEnabled()
576 676 {
@@ -583,24 +683,30 @@
583 683 * @return bool
584 684 */
585 685 public function isEvergreen()
586 686 {
587 - return $this->getRawSetting('mode') == C::MODE_EVERGREEN;
687 + return $this->getRawSetting( 'mode' ) == C::MODE_EVERGREEN;
588 688 }
589 689
590 690 public function isRegular()
591 691 {
592 - return $this->getRawSetting('mode') == C::MODE_REGULAR;
692 + return $this->getRawSetting( 'mode' ) == C::MODE_REGULAR;
593 693 }
594 694
695 + public function isRecurring()
696 + {
697 + return $this->getRawSetting( 'mode' ) == C::MODE_RECURRING;
698 +
699 + }
700 +
595 701 /**
596 702 * Save compaign endtime for regular mode.
597 703 *
598 704 * @param $date
599 705 */
600 - public function setEndDatetime($date)
706 + public function setEndDatetime( $date )
601 707 {
602 - $this->storeRawSetting('end_datetime', $date ?: $this->defaultEndDatetime());
708 + $this->storeRawSetting( 'end_datetime', $date ?: $this->defaultEndDatetime() );
603 709 }
604 710
605 711 /**
606 712 * Returns default end datetime for regular mode.
@@ -608,9 +714,9 @@
608 714 * @return false|string
609 715 */
610 716 private function defaultEndDatetime()
611 717 {
612 - return date('Y-m-d h:i A', strtotime('+1 week'));
718 + return date( 'Y-m-d h:i A', strtotime( '+1 week' ) );
613 719 }
614 720
615 721 /**
616 722 * Save digit color.
@@ -616,11 +722,11 @@
616 722 * Save digit color.
617 723 *
618 724 * @param $color
619 725 */
620 - public function setDigitColor($color)
726 + public function setDigitColor( $color )
621 727 {
622 - $this->storeRawSetting('digit_color', $color);
728 + $this->storeRawSetting( 'digit_color', $color );
623 729 }
624 730
625 731 /**
626 732 * Returns headline visibility.
@@ -631,22 +737,23 @@
631 737 public function getHeadlineVisibility()
632 738 {
633 739 $meta = 'headline_visibility';
634 740
635 - $value = $this->getRawSetting($meta, false);
636 - if ($value === C::NO || $value === C::YES) {
741 + $value = $this->getRawSetting( $meta, false );
742 + if ( $value === C::NO || $value === C::YES ) {
637 743 return $value;
638 744 }
639 745
640 - $legacyMeta = 'display_headline';
641 - $legacyValue = filter_var($this->getLegacySetting($legacyMeta), FILTER_VALIDATE_BOOLEAN);
746 + $legacyMeta = 'display_headline';
747 + $legacyValue = filter_var( $this->getLegacySetting( $legacyMeta ),
748 + FILTER_VALIDATE_BOOLEAN );
642 749 $legacyValue = $legacyValue ? C::YES : C::NO;
643 750
644 - $this->storeRawSetting($meta, $legacyValue);
751 + $this->storeRawSetting( $meta, $legacyValue );
645 752
646 - $this->deleteRawSetting($legacyMeta);
753 + $this->deleteRawSetting( $legacyMeta );
647 754
648 - return $this->getRawSetting($meta);
755 + return $this->getRawSetting( $meta );
649 756 }
650 757
651 758 /**
652 759 * Returns digit color.
@@ -655,31 +762,31 @@
655 762 * @return mixed
656 763 */
657 764 public function getDigitColor()
658 765 {
659 - $value = $this->getRawSetting('digit_color', false);
660 - if ($value) {
766 + $value = $this->getRawSetting( 'digit_color', false );
767 + if ( $value ) {
661 768 return $value;
662 769 }
663 770
664 - $legacy = $this->getLegacySetting('text_color');
665 - if (!$legacy) {
771 + $legacy = $this->getLegacySetting( 'text_color' );
772 + if ( ! $legacy ) {
666 773 return $this->digitColor;
667 774 }
668 775
669 - $this->setDigitColor($legacy);
670 - if (!$this->getRawSetting('label_color', false)) {
671 - $this->setLabelColor($legacy);
776 + $this->setDigitColor( $legacy );
777 + if ( ! $this->getRawSetting( 'label_color', false ) ) {
778 + $this->setLabelColor( $legacy );
672 779 }
673 780
674 - $this->deleteRawSetting('text_color');
781 + $this->deleteRawSetting( 'text_color' );
675 782
676 - return $this->getRawSetting('digit_color');
783 + return $this->getRawSetting( 'digit_color' );
677 784 }
678 785
679 - public function setLabelColor($color)
786 + public function setLabelColor( $color )
680 787 {
681 - $this->storeRawSetting('label_color', $color);
788 + $this->storeRawSetting( 'label_color', $color );
682 789 }
683 790
684 791 /**
685 792 * Get label color
@@ -687,26 +794,26 @@
687 794 * @return mixed
688 795 */
689 796 public function getLabelColor()
690 797 {
691 - $value = $this->getRawSetting('label_color', false);
692 - if ($value) {
798 + $value = $this->getRawSetting( 'label_color', false );
799 + if ( $value ) {
693 800 return $value;
694 801 }
695 802
696 - $legacy = $this->getLegacySetting('text_color');
697 - if (!$legacy) {
803 + $legacy = $this->getLegacySetting( 'text_color' );
804 + if ( ! $legacy ) {
698 805 return $this->labelColor;
699 806 }
700 807
701 - $this->setLabelColor($legacy);
702 - if (!$this->getRawSetting('digit_color', false)) {
703 - $this->setDigitColor($legacy);
808 + $this->setLabelColor( $legacy );
809 + if ( ! $this->getRawSetting( 'digit_color', false ) ) {
810 + $this->setDigitColor( $legacy );
704 811 }
705 812
706 - $this->deleteRawSetting('text_color');
813 + $this->deleteRawSetting( 'text_color' );
707 814
708 - return $this->getRawSetting('label_color');
815 + return $this->getRawSetting( 'label_color' );
709 816
710 817 }
711 818
712 819 /**
@@ -718,24 +825,24 @@
718 825 * @since 1.2.4
719 826 */
720 827 public function getDigitSize()
721 828 {
722 - $meta = 'digit_size';
723 - $value = $this->getRawSetting($meta, false);
724 - if (!empty($value)) {
829 + $meta = 'digit_size';
830 + $value = $this->getRawSetting( $meta, false );
831 + if ( ! empty( $value ) ) {
725 832 return $value;
726 833 }
727 - $legacy = $this->getLegacySetting('text_size');
834 + $legacy = $this->getLegacySetting( 'text_size' );
728 835
729 - if (empty($legacy)) {
836 + if ( empty( $legacy ) ) {
730 837 return $this->digitSize;
731 838 }
732 839
733 - $this->setDigitSize($legacy);
840 + $this->setDigitSize( $legacy );
734 841
735 - $this->deleteRawSetting('text_size');
842 + $this->deleteRawSetting( 'text_size' );
736 843
737 - return $this->getRawSetting('digit_size');
844 + return $this->getRawSetting( 'digit_size' );
738 845 }
739 846
740 847 /**
741 848 * Save timer digit size.
@@ -741,11 +848,11 @@
741 848 * Save timer digit size.
742 849 *
743 850 * @param $size
744 851 */
745 - public function setDigitSize($size)
852 + public function setDigitSize( $size )
746 853 {
747 - $this->storeRawSetting('digit_size', $size);
854 + $this->storeRawSetting( 'digit_size', $size );
748 855 }
749 856
750 857 /**
751 858 * Save timer label size.
@@ -751,16 +858,16 @@
751 858 * Save timer label size.
752 859 *
753 860 * @param $size
754 861 */
755 - public function setLabelSize($size)
862 + public function setLabelSize( $size )
756 863 {
757 - $this->storeRawSetting('label_size', $size);
864 + $this->storeRawSetting( 'label_size', $size );
758 865 }
759 866
760 - public function setHeadlineSize($size)
867 + public function setHeadlineSize( $size )
761 868 {
762 - $this->storeRawSetting('headline_size', $size);
869 + $this->storeRawSetting( 'headline_size', $size );
763 870 }
764 871
765 872 /**
766 873 * Returns evergreen duration.
@@ -768,20 +875,46 @@
768 875 * @return array
769 876 */
770 877 public function getDuration()
771 878 {
772 - $default = [0, 0, 0, 0];
773 - $duration = $this->getRawSetting('duration');
774 - if (is_array($duration)) {
775 - $duration = array_merge($duration, $default);
879 + $default = [ 0, 0, 0, 0 ];
880 + $duration = $this->getRawSetting( 'duration' );
881 + if ( is_array( $duration ) ) {
882 + $duration = array_merge( $duration, $default );
776 883
777 - return array_map('intval', $duration);
884 + return array_map( 'intval', $duration );
778 885 }
779 886
780 887 return $default;
781 888 }
782 889
890 + public function setRecurringDuration( $value )
891 + {
892 + $this->storeRawSetting( '_hurryt_recurring_duration', $value );
893 + }
894 +
895 + public function setRecurringPauseDuration( $value )
896 + {
897 + $this->storeRawSetting( '_hurryt_recurring_pause_duration', $value );
898 + }
899 +
783 900 /**
901 + * Returns evergreen duration.
902 + *
903 + * @return array
904 + */
905 + public function getRecurringDuration()
906 + {
907 + $default = [ 0, 0, 0, 0 ];
908 + $duration = $this->getRawSetting( '_hurryt_recurring_duration', false );
909 + if ( is_array( $duration ) ) {
910 + return array_map( 'intval', array_merge( $duration, $default ) );
911 + }
912 +
913 + return $default;
914 + }
915 +
916 + /**
784 917 * Returns actions array.
785 918 *
786 919 * @return array
787 920 */
@@ -786,62 +919,64 @@
786 919 * @return array
787 920 */
788 921 public function getActions()
789 922 {
790 - $legacy = $this->getLegacySetting('end_action');
791 - if ($legacy) {
792 - $redirectUrl = $this->getLegacySetting('redirect_url');
793 - $actions = [
923 + $legacy = $this->getLegacySetting( 'end_action' );
924 + if ( $legacy ) {
925 + $redirectUrl = $this->getLegacySetting( 'redirect_url' );
926 + $actions = [
794 927 [
795 - 'id' => intval($legacy),
928 + 'id' => intval( $legacy ),
796 929 'redirectUrl' => $redirectUrl,
797 930 ],
798 931 ];
799 - $this->storeRawSetting('actions', $actions);
800 - $this->deleteRawSetting('end_action');
801 - $this->deleteRawSetting('redirect_url');
932 + $this->storeRawSetting( 'actions', $actions );
933 + $this->deleteRawSetting( 'end_action' );
934 + $this->deleteRawSetting( 'redirect_url' );
802 935
803 - return $this->mergeActions($actions);
936 + return $this->mergeActions( $actions );
804 937 }
805 938
806 - return $this->mergeActions($this->getRawSetting('actions'));
939 + return $this->mergeActions( $this->getRawSetting( 'actions' ) );
807 940 }
808 941
809 - private function mergeActions($actions)
942 + private function mergeActions( $actions )
810 943 {
811 944 $defaults = [
812 945 [
813 - 'id' => C::ACTION_NONE,
814 - 'redirectUrl' => '',
815 - 'message' => '',
946 + 'id' => C::ACTION_NONE,
947 + 'redirectUrl' => '',
948 + 'message' => '',
816 949 'wcStockStatus' => '',
817 950 ],
818 951 ];
819 952
820 - if (count($actions) === 0) {
953 + if ( count( $actions ) === 0 ) {
821 954 return $defaults;
822 955 }
823 956
824 - return array_map(function ($action) use ($defaults) {
825 - $action['id'] = (int) $action['id'];
957 + return array_map( function ( $action ) use ( $defaults ) {
958 + $action[ 'id' ] = (int)$action[ 'id' ];
826 959
827 - return array_merge($defaults[0], $action);
828 - }, $actions);
960 + return array_merge( $defaults[ 0 ], $action );
961 + }, $actions );
829 962 }
830 963
831 964 /**
832 965 * Returns evergreen duration in seconds.
833 966 *
967 + * @param array $duration
968 + *
834 969 * @return int
835 970 */
836 - public function getDurationInSeconds()
971 + public function durationArrayToSeconds( $duration = [] )
837 972 {
838 - list($d, $h, $m, $s) = $this->getDuration();
973 + list( $d, $h, $m, $s ) = empty( $duration ) ? $this->getDuration() : $duration;
839 974
840 975 return $d * DAY_IN_SECONDS +
841 - $h * HOUR_IN_SECONDS +
842 - $m * MINUTE_IN_SECONDS +
843 - $s;
976 + $h * HOUR_IN_SECONDS +
977 + $m * MINUTE_IN_SECONDS +
978 + $s;
844 979 }
845 980
846 981 /**
847 982 * Returns end datetime.
@@ -849,12 +984,147 @@
849 984 * @return string
850 985 */
851 986 public function getEndDatetime()
852 987 {
853 - return $this->getRawSetting('end_datetime') ?: $this->defaultEndDatetime();
988 + return $this->getRawSetting( 'end_datetime' ) ?: $this->defaultEndDatetime();
854 989 }
855 990
856 991 /**
992 + * Return true if the recurrence is expired.
993 + *
994 + * @return bool
995 + */
996 + public function isRecurringExpired()
997 + {
998 + $deadline = $this->getRecurringDeadline();
999 + if ( ! ( $deadline instanceof Carbon ) ) {
1000 + return false;
1001 + }
1002 +
1003 + $now = Carbon::now( hurryt_tz() );
1004 +
1005 + return $now->isAfter( $deadline );
1006 + }
1007 +
1008 + function getTimeToNextRecurrence()
1009 + {
1010 + $this->loadSettings();
1011 + $interval = absint( $this->recurringInterval );
1012 +
1013 + if ( $this->recurringFrequency === C::RECURRING_DAILY ) {
1014 + $frequencyInSeconds = DAY_IN_SECONDS;
1015 + } elseif ( $this->recurringFrequency === C::RECURRING_WEEKLY ) {
1016 + $frequencyInSeconds = WEEK_IN_SECONDS;
1017 +
1018 + } elseif ( $this->recurringFrequency === C::RECURRING_HOURLY ) {
1019 + $frequencyInSeconds = HOUR_IN_SECONDS;
1020 +
1021 + } else {
1022 + $frequencyInSeconds = MINUTE_IN_SECONDS;
1023 + }
1024 +
1025 + $duration = $this->durationArrayToSeconds( $this->getRecurringDuration() );
1026 +
1027 + return max( 0, ( $frequencyInSeconds * $interval ) - $duration );
1028 + }
1029 +
1030 + /**
1031 + * Return true if the current campaign can recur today.
1032 + *
1033 + * @return bool
1034 + */
1035 + public function canRecurToday()
1036 + {
1037 + $this->loadSettings();
1038 + $day = absint( Carbon::now( hurryt_tz() )->format( 'w' ) );
1039 + $recur = in_array( $day, array_map( 'absint', $this->recurringDays ) );
1040 +
1041 + return apply_filters( 'hurryt_recur_today', $recur, $this->getId() );
1042 + }
1043 +
1044 + /**
1045 + * Return current recurrence date
1046 + *
1047 + * @return \Carbon\CarbonInterface|null
1048 + */
1049 + public function getRecurringCurrentDate()
1050 + {
1051 + $this->loadSettings();
1052 + $dtStart = Carbon::parse( $this->recurringStartTime, hurryt_tz() );
1053 + $now = Carbon::now( hurryt_tz() );
1054 +
1055 + $range = CarbonPeriod::since( $dtStart );
1056 + $interval = absint( $this->recurringInterval );
1057 +
1058 + if ( $this->recurringFrequency === C::RECURRING_DAILY ) {
1059 + $range->days( $interval );
1060 + } elseif ( $this->recurringFrequency === C::RECURRING_WEEKLY ) {
1061 + $range->weeks( $interval );
1062 + } elseif ( $this->recurringFrequency === C::RECURRING_HOURLY ) {
1063 + $range->hours( $interval );
1064 + } else {
1065 + $range->minutes( $interval );
1066 + }
1067 +
1068 + // Recurs forever
1069 + if ( absint( $this->recurringEnd ) === C::RECURRING_END_NEVER ) {
1070 + $range->until( $now );
1071 + // Recurs until
1072 + } elseif ( absint( $this->recurringEnd ) === C::RECURRING_END_TIME ) {
1073 + $range->until( Carbon::parse( $this->recurringUntil, hurryt_tz() ) );
1074 +
1075 + $range->addFilter( function ( $date ) use ( $now ) {
1076 + /**
1077 + * @var Carbon $date
1078 + */
1079 + return $date->isBefore( $now );
1080 + } );
1081 +
1082 + // Recurs N times.
1083 + } elseif ( absint( $this->recurringEnd ) === C::RECURRING_END_OCCURRENCES ) {
1084 +
1085 + // Virtual endDate
1086 + $range->until( Carbon::now( hurryt_tz() )->addCenturies( 1 ) );
1087 +
1088 + // Limit to the number of occurences in `$this->recurringCount`
1089 + $range->setRecurrences( absint( $this->recurringCount ) );
1090 +
1091 + // Set the endDate to the last occurence date
1092 + $range->setEndDate( $range->last() );
1093 + $range->addFilter( function ( $date ) use ( $now ) {
1094 +
1095 + /**
1096 + * @var Carbon $date
1097 + */
1098 + return $date->isBefore( $now );
1099 + } );
1100 +
1101 + }
1102 +
1103 + return $range->last();
1104 + }
1105 +
1106 + /**
1107 + * Get the next/current deadline based on the current date.
1108 + *
1109 + * @return null|Carbon
1110 + */
1111 + public function getRecurringDeadline()
1112 + {
1113 + $date = $this->getRecurringCurrentDate();
1114 + if ( ! $date ) {
1115 + return null;
1116 + }
1117 + $date = clone Carbon::instance( $date );
1118 +
1119 + $duration = $this->durationArrayToSeconds( $this->getRecurringDuration() );
1120 +
1121 + $date->addSeconds( $duration );
1122 +
1123 + return $date;
1124 + }
1125 +
1126 + /**
857 1127 * Returns compaign publish datetime.
858 1128 *
859 1129 * @return mixed
860 1130 */
@@ -859,9 +1129,9 @@
859 1129 * @return mixed
860 1130 */
861 1131 public function getStartTimestamp()
862 1132 {
863 - return get_the_date($this->id);
1133 + return get_the_date( $this->id );
864 1134 }
865 1135
866 1136 /**
867 1137 * Returns position in WooCommerce product page.
@@ -869,22 +1139,22 @@
869 1139 * @return mixed
870 1140 */
871 1141 public function getWcPosition()
872 1142 {
873 - $legacy = $this->getLegacySetting('position');
1143 + $legacy = $this->getLegacySetting( 'position' );
874 1144
875 - if (!$legacy) {
876 - return $this->getRawSetting('wc_position');
1145 + if ( ! $legacy ) {
1146 + return $this->getRawSetting( 'wc_position' );
877 1147 }
878 - $this->storeRawSetting('wc_position', $legacy);
879 - $this->deleteRawSetting('position');
1148 + $this->storeRawSetting( 'wc_position', $legacy );
1149 + $this->deleteRawSetting( 'position' );
880 1150
881 1151 return $legacy;
882 1152 }
883 1153
884 - private function getLegacySetting($name)
1154 + private function getLegacySetting( $name )
885 1155 {
886 - return get_post_meta($this->id, $name, true);
1156 + return get_post_meta( $this->id, $name, true );
887 1157 }
888 1158
889 1159 /**
890 1160 * Returns true if WooCommerce integration is enabled.
@@ -892,22 +1162,22 @@
892 1162 * @return mixed
893 1163 */
894 1164 public function getWcEnable()
895 1165 {
896 - $value = $this->getRawSetting('wc_enable', false);
1166 + $value = $this->getRawSetting( 'wc_enable', false );
897 1167
898 - if ($value === C::YES || $value === C::NO) {
1168 + if ( $value === C::YES || $value === C::NO ) {
899 1169 return $value;
900 1170 }
901 1171
902 - $legacy = filter_var($value, FILTER_VALIDATE_BOOLEAN);
903 - if ($legacy) {
904 - $this->storeRawSetting('wc_enable', C::YES);
1172 + $legacy = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
1173 + if ( $legacy ) {
1174 + $this->storeRawSetting( 'wc_enable', C::YES );
905 1175 } else {
906 - $this->storeRawSetting('wc_enable', C::NO);
1176 + $this->storeRawSetting( 'wc_enable', C::NO );
907 1177 }
908 1178
909 - return $this->getRawSetting('wc_enable');
1179 + return $this->getRawSetting( 'wc_enable' );
910 1180 }
911 1181
912 1182 /**
913 1183 * Returns true if label should be displayed.
@@ -915,20 +1185,21 @@
915 1185 * @return mixed
916 1186 */
917 1187 public function getLabelVisibility()
918 1188 {
919 - $meta = 'label_visibility';
920 - $value = $this->getRawSetting($meta, false);
921 - if ($value === C::NO || $value === C::YES) {
1189 + $meta = 'label_visibility';
1190 + $value = $this->getRawSetting( $meta, false );
1191 + if ( $value === C::NO || $value === C::YES ) {
922 1192 return $value;
923 1193 }
924 1194
925 - $legacy = filter_var($this->getLegacySetting('display_labels'), FILTER_VALIDATE_BOOLEAN);
1195 + $legacy = filter_var( $this->getLegacySetting( 'display_labels' ),
1196 + FILTER_VALIDATE_BOOLEAN );
926 1197 $legacy = $legacy ? C::YES : C::NO;
927 - $this->storeRawSetting($meta, $legacy);
928 - $this->deleteRawSetting('display_labels');
1198 + $this->storeRawSetting( $meta, $legacy );
1199 + $this->deleteRawSetting( 'display_labels' );
929 1200
930 - return $this->getRawSetting($meta);
1201 + return $this->getRawSetting( $meta );
931 1202
932 1203 }
933 1204
934 1205 /**
@@ -938,9 +1209,9 @@
938 1209 * @return int
939 1210 */
940 1211 public function getRestart()
941 1212 {
942 - return $this->getRawSetting('restart') ?: C::RESTART_NONE;
1213 + return $this->getRawSetting( 'restart' ) ?: C::RESTART_NONE;
943 1214 }
944 1215
945 1216 /**
946 1217 * Returns WooCommerce products selection type.
@@ -948,14 +1219,14 @@
948 1219 * @return string
949 1220 */
950 1221 public function getWcProductsSelectionType()
951 1222 {
952 - $legacy = $this->getLegacySetting('products_type');
953 - if (!$legacy) {
954 - return $this->getRawSetting('wc_products_selection_type');
1223 + $legacy = $this->getLegacySetting( 'products_type' );
1224 + if ( ! $legacy ) {
1225 + return $this->getRawSetting( 'wc_products_selection_type' );
955 1226 }
956 - $this->storeRawSetting('wc_products_selection_type', $legacy);
957 - $this->deleteRawSetting('products_type');
1227 + $this->storeRawSetting( 'wc_products_selection_type', $legacy );
1228 + $this->deleteRawSetting( 'products_type' );
958 1229
959 1230 return $legacy;
960 1231 }
961 1232
@@ -963,11 +1234,11 @@
963 1234 * Delete setting item.
964 1235 *
965 1236 * @param $name
966 1237 */
967 - public function deleteRawSetting($name)
1238 + public function deleteRawSetting( $name )
968 1239 {
969 - delete_post_meta($this->id, $name);
1240 + delete_post_meta( $this->id, $name );
970 1241 }
971 1242
972 1243 /**
973 1244 * Returns products selection IDs.
@@ -975,14 +1246,14 @@
975 1246 * @return array
976 1247 */
977 1248 public function getWcProductsSelection()
978 1249 {
979 - $legacy = $this->getLegacySetting('products');
980 - if (!$legacy) {
981 - return $this->getRawSetting('wc_products_selection');
1250 + $legacy = $this->getLegacySetting( 'products' );
1251 + if ( ! $legacy ) {
1252 + return $this->getRawSetting( 'wc_products_selection' );
982 1253 }
983 - $this->storeRawSetting('wc_products_selection', $legacy);
984 - $this->deleteRawSetting('products');
1254 + $this->storeRawSetting( 'wc_products_selection', $legacy );
1255 + $this->deleteRawSetting( 'products' );
985 1256
986 1257 return $legacy;
987 1258 }
988 1259
@@ -990,12 +1261,12 @@
990 1261 * Store custom labels.
991 1262 *
992 1263 * @param $labels
993 1264 */
994 - public function setLabels($labels)
1265 + public function setLabels( $labels )
995 1266 {
996 - $labels = array_merge($this->labels, array_filter($labels));
997 - update_post_meta($this->id, 'labels', $labels);
1267 + $labels = array_merge( $this->labels, array_filter( $labels ) );
1268 + update_post_meta( $this->id, 'labels', $labels );
998 1269 }
999 1270
1000 1271 /**
1001 1272 * Returns true if compaign can be published.
@@ -1003,9 +1274,10 @@
1003 1274 * @return bool
1004 1275 */
1005 1276 public function isActive()
1006 1277 {
1007 - return get_post_status($this->id) === "publish" || get_post_status($this->id) === "future";
1278 + return get_post_status( $this->id ) === "publish"
1279 + || get_post_status( $this->id ) === "future";
1008 1280
1009 1281 }
1010 1282
1011 1283 /**
@@ -1014,29 +1286,44 @@
1014 1286 * @return bool
1015 1287 */
1016 1288 public function isScheduled()
1017 1289 {
1018 - return get_post_status($this->getId()) === "future";
1290 + $scheduled = get_post_status( $this->getId() ) === "future";
1291 + if ( $scheduled ) {
1292 + return true;
1293 + }
1294 +
1295 + if ( $this->isRecurring() ) {
1296 + $start_date = Carbon::parse( $this->recurringStartTime, hurryt_tz() );
1297 + $now = Carbon::now( hurryt_tz() );
1298 + if ( $now->isBefore( $start_date ) ) {
1299 + return true;
1300 + }
1301 + }
1302 +
1303 + return $scheduled;
1019 1304 }
1020 1305
1021 1306 /**
1022 1307 * Returns true if fixed campaign datetime is expired.
1023 1308 *
1309 + * @param string|null $date
1310 + *
1024 1311 * @return bool
1025 1312 */
1026 - public function isExpired()
1313 + public function isExpired( $date = null )
1027 1314 {
1028 - $endDate = date_create($this->endDatetime)->format("Y-m-d H:i");
1029 - $today = date_create(current_time("mysql"))->format("Y-m-d H:i");
1315 + $endDate = $date === null ? date_create( $this->endDatetime ) : $date;
1316 + $today = date_create( current_time( "mysql" ) );
1030 1317
1031 1318 return $endDate < $today;
1032 1319 }
1033 1320
1034 - public function isDismissed()
1321 + public function isStickyDismissed()
1035 1322 {
1036 - return isset($_COOKIE[CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed'])
1037 - && $this->stickyBarDismissible === C::YES
1038 - && $this->enableSticky === C::YES;
1323 + return isset( $_COOKIE[ CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed' ] )
1324 + && $this->stickyBarDismissible === C::YES
1325 + && $this->enableSticky === C::YES;
1039 1326 }
1040 1327
1041 1328 /**
1042 1329 * Returns compaign template wrapper.
@@ -1044,13 +1331,13 @@
1044 1331 * @param string
1045 1332 *
1046 1333 * @return string
1047 1334 */
1048 - public function wrapTemplate($content)
1335 + public function wrapTemplate( $content )
1049 1336 {
1050 - $campaignBuilder = new CampaignBuilder($this);
1337 + $campaignBuilder = new CampaignBuilder( $this );
1051 1338
1052 - return $campaignBuilder->build($content);
1339 + return $campaignBuilder->build( $content );
1053 1340 }
1054 1341
1055 1342 /*
1056 1343 * Returns campaign template content.
@@ -1056,9 +1343,20 @@
1056 1343 * Returns campaign template content.
1057 1344 */
1058 1345 public function buildTemplate()
1059 1346 {
1060 - $campaignBuilder = new CampaignBuilder($this);
1347 + $campaignBuilder = new CampaignBuilder( $this );
1061 1348
1062 1349 return $campaignBuilder->template();
1063 1350 }
1351 +
1352 + public function setWcConditions( $value )
1353 + {
1354 + $this->storeRawSetting( '_hurryt_wc_conditions', ! empty( $value ) ? $value : [] );
1355 + }
1356 +
1357 + public function getWcConditions()
1358 + {
1359 + return $this->getRawSetting( '_hurryt_wc_conditions', false );
1360 + }
1361 +
1064 1362 }