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 +493 -176 2.1.32.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 *
@@ -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
@@ -324,15 +393,14 @@
324 393 */
325 394 public $stickyBarPages = [];
326 395
327 396 /**
328 - * Show sticky bar on all pages.
397 + * Where to display the sticky bar option
329 398 *
330 399 * @var string
331 400 */
332 - public $stickyBarShowOnAllPages = C::YES;
401 + public $stickyBarDisplayOn = 'all_pages';
333 402
334 -
335 403 /**
336 404 * Show sticky bar close button?
337 405 *
338 406 * @var string
@@ -338,9 +406,8 @@
338 406 * @var string
339 407 */
340 408 public $stickyBarDismissible = C::YES;
341 409
342 -
343 410 /**
344 411 * CTA settings.
345 412 *
346 413 * @var array
@@ -396,12 +463,14 @@
396 463 * @var integer
397 464 */
398 465 public $campaignYPadding = 10;
399 466
400 - public function __construct($id)
467 + public function __construct( $id )
401 468 {
402 -
403 - $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;
404 473 }
405 474
406 475 /**
407 476 * Returns compaign post iD.
@@ -412,41 +481,69 @@
412 481 {
413 482 return $this->id;
414 483 }
415 484
416 - public function showStickyBarOn($pageId)
485 + public function showStickyBarOn( $pageId )
417 486 {
418 - if ($this->enableSticky === C::NO) {
487 + if ( $this->enableSticky === C::NO ) {
419 488 return true;
420 489 }
421 - if ($this->getStickyBarShowOnAllPages() === C::YES) {
422 - return true;
490 +
491 + $can_show = false;
492 + switch ( $this->getStickyBarDisplayOn() ) {
493 + case 'all_pages':
494 + $can_show = true;
495 + break;
496 + case 'wc_products_pages':
497 + if ( function_exists( 'is_product' ) && is_product() ) {
498 + $wc_campaign = new WCCampaign();
499 + $can_show = ( $wc_campaign->hasActiveCampaign( $this, $pageId ) );
500 + } else {
501 + $can_show = false;
502 + }
503 + break;
504 + case 'specific_pages':
505 + $pages = $this->getStickyBarPages();
506 + $can_show = in_array( $pageId, $pages );
507 + break;
423 508 }
424 509
425 - if ($pageId <= 0) {
426 - return false;
427 - }
510 + return apply_filters( 'hurryt_show_sticky_bar', $can_show, $this->getId() );
428 511
429 - $pages = $this->getStickyBarPages();
512 + }
430 513
431 - foreach ($pages as $page) {
432 - if ($page == $pageId) {
433 - return true;
434 - }
435 - }
514 + public function getStickyBarPages()
515 + {
516 + $pagesIds = $this->getRawSetting( 'sticky_bar_pages' );
436 517
437 - return false;
518 + return array_filter( array_map( 'intval', $pagesIds ) );
519 + }
438 520
521 + public function setStickyBarPages( $value )
522 + {
523 + $this->storeRawSetting( 'sticky_bar_pages', $value );
439 524 }
440 525
441 - public function getStickyBarPages()
526 + public function getStickyBarDisplayOn()
442 527 {
443 - return $this->getRawSetting('sticky_bar_pages');
528 +
529 + // backward compat.
530 + $all_pages = $this->getRawSetting( 'sticky_bar_show_on_all_pages', false );
531 +
532 + if ( $all_pages === C::YES ) {
533 + $this->setStickyBarDisplayOn( 'all_pages' );
534 + }
535 +
536 + $this->deleteRawSetting( 'sticky_bar_show_on_all_pages' );
537 +
538 + $value = $this->getRawSetting( '_hurryt_sticky_bar_display_on', false );
539 +
540 + return ! empty( $value ) ? $value : $this->stickyBarDisplayOn;
444 541 }
445 542
446 - public function getStickyBarShowOnAllPages()
543 + public function setStickyBarDisplayOn( $value )
447 544 {
448 - return $this->getRawSetting('sticky_bar_show_on_all_pages');
545 + $this->storeRawSetting( '_hurryt_sticky_bar_display_on', $value );
449 546 }
450 547
451 548 /**
452 549 * Get raw setting.
@@ -455,25 +552,30 @@
455 552 * @param boolean
456 553 *
457 554 * @return mixed
458 555 */
459 - public function getRawSetting($name, $useDefault = true)
556 + public function getRawSetting( $name, $useDefault = true )
460 557 {
461 - $value = get_post_meta($this->id, $name, true);
462 - if ( ! empty($value)) {
558 + $value = get_post_meta( $this->id, $name, true );
559 + if ( ! empty( $value ) ) {
463 560 return $value;
464 561 }
465 - if ($useDefault) {
466 - return $this->{Helpers::snakeToCamelCase($name)};
562 + if ( $useDefault ) {
563 + return $this->{Helpers::snakeToCamelCase( $name )};
467 564 }
468 565
469 566 return $value;
470 567 }
471 568
472 - public function storeRawSetting($name, $value)
569 + public function storeRawSetting( $name, $value )
473 570 {
474 - $value = ! empty($value) ? $value : $this->{Helpers::snakeToCamelCase($name)};
475 - 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 );
476 578 }
477 579
478 580 /**
479 581 * Bulk save for compaign settings.
@@ -479,30 +581,46 @@
479 581 * Bulk save for compaign settings.
480 582 *
481 583 * @param $data
482 584 */
483 - public function storeSettings($data)
585 + public function storeSettings( $data )
484 586 {
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);
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 );
492 593 }
493 594 }
494 -
595 + if ( ! isset( $data[ 'wc_conditions' ] ) ) {
596 + $this->setWcConditions( [] );
597 + }
598 + if ( ! isset( $data[ 'sticky_bar_pages' ] ) ) {
599 + $this->setStickyBarPages( [] );
600 + }
495 601 self::buildCss();
496 602 }
497 603
604 + //removeIf(pro)
498 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 + /**
499 617 * Build User CSS,
500 618 * Then merge it with base one.
501 619 *
502 620 * @return void
503 621 */
504 - static public function buildCss()
622 + public static function buildCss()
505 623 {
506 624
507 625 // Build and return CSS
508 626 ob_start();
@@ -507,32 +625,35 @@
507 625 // Build and return CSS
508 626 ob_start();
509 627 include HURRYT_DIR . 'includes/css.php';
510 628 $css = ob_get_clean();
511 - $base = file_get_contents(HURRYT_DIR . '/assets/css/base.css');
629 + $base = file_get_contents( HURRYT_DIR . '/assets/css/base.css' );
512 630
513 631 // Merge built and base css.
514 632 $base .= $css;
515 633
516 634 // Save to public file.
517 - file_put_contents(HURRYT_DIR . '/assets/css/hurrytimer.css', $base);
635 + file_put_contents( HURRYT_DIR . '/assets/css/hurrytimer.css', $base );
518 636 }
519 637
520 - /**
521 - * Load settings from database,
522 - * and set object props according their matched name.
523 - */
638 + private function setRecurringDates( $dates )
639 + {
640 + foreach ( $dates as $d ) {
641 + add_post_meta( $this->getId(), '_hurryt_recurring_dates', $d );
642 + }
643 + }
644 +
524 645 public function loadSettings()
525 646 {
526 647
527 - $reflection = new \ReflectionObject($this);
528 - foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
648 + $reflection = new \ReflectionObject( $this );
649 + foreach ( $reflection->getProperties( \ReflectionProperty::IS_PUBLIC ) as $prop ) {
529 650 $name = $prop->getName();
530 - $method = 'get' . ucfirst($name);
531 - if (method_exists($this, $method)) {
651 + $method = 'get' . ucfirst( $name );
652 + if ( method_exists( $this, $method ) ) {
532 653 $this->{$name} = $this->$method();
533 654 } else {
534 - $this->{$name} = $this->getRawSetting(Helpers::camelToSnakeCase($name));
655 + $this->{$name} = $this->getRawSetting( Helpers::camelToSnakeCase( $name ) );
535 656 }
536 657 }
537 658 }
538 659
@@ -542,14 +663,14 @@
542 663 * @return mixed
543 664 */
544 665 public function getHeadline()
545 666 {
546 - return $this->id ? get_the_title($this->id) : $this->headline;
667 + return $this->id ? get_the_title( $this->id ) : $this->headline;
547 668 }
548 669
549 670 public function isPublished()
550 671 {
551 - return get_post_status($this->id) === "publish";
672 + return get_post_status( $this->id ) === "publish";
552 673 }
553 674
554 675 public function isWcEnabled()
555 676 {
@@ -562,24 +683,30 @@
562 683 * @return bool
563 684 */
564 685 public function isEvergreen()
565 686 {
566 - return $this->getRawSetting('mode') == C::MODE_EVERGREEN;
687 + return $this->getRawSetting( 'mode' ) == C::MODE_EVERGREEN;
567 688 }
568 689
569 690 public function isRegular()
570 691 {
571 - return $this->getRawSetting('mode') == C::MODE_REGULAR;
692 + return $this->getRawSetting( 'mode' ) == C::MODE_REGULAR;
572 693 }
573 694
695 + public function isRecurring()
696 + {
697 + return $this->getRawSetting( 'mode' ) == C::MODE_RECURRING;
698 +
699 + }
700 +
574 701 /**
575 702 * Save compaign endtime for regular mode.
576 703 *
577 704 * @param $date
578 705 */
579 - public function setEndDatetime($date)
706 + public function setEndDatetime( $date )
580 707 {
581 - $this->storeRawSetting('end_datetime', $date ?: $this->defaultEndDatetime());
708 + $this->storeRawSetting( 'end_datetime', $date ?: $this->defaultEndDatetime() );
582 709 }
583 710
584 711 /**
585 712 * Returns default end datetime for regular mode.
@@ -587,9 +714,9 @@
587 714 * @return false|string
588 715 */
589 716 private function defaultEndDatetime()
590 717 {
591 - return date('Y-m-d h:i A', strtotime('+1 week'));
718 + return date( 'Y-m-d h:i A', strtotime( '+1 week' ) );
592 719 }
593 720
594 721 /**
595 722 * Save digit color.
@@ -595,11 +722,11 @@
595 722 * Save digit color.
596 723 *
597 724 * @param $color
598 725 */
599 - public function setDigitColor($color)
726 + public function setDigitColor( $color )
600 727 {
601 - $this->storeRawSetting('digit_color', $color);
728 + $this->storeRawSetting( 'digit_color', $color );
602 729 }
603 730
604 731 /**
605 732 * Returns headline visibility.
@@ -610,22 +737,23 @@
610 737 public function getHeadlineVisibility()
611 738 {
612 739 $meta = 'headline_visibility';
613 740
614 - $value = $this->getRawSetting($meta, false);
615 - if ($value === C::NO || $value === C::YES) {
741 + $value = $this->getRawSetting( $meta, false );
742 + if ( $value === C::NO || $value === C::YES ) {
616 743 return $value;
617 744 }
618 745
619 746 $legacyMeta = 'display_headline';
620 - $legacyValue = filter_var($this->getLegacySetting($legacyMeta), FILTER_VALIDATE_BOOLEAN);
747 + $legacyValue = filter_var( $this->getLegacySetting( $legacyMeta ),
748 + FILTER_VALIDATE_BOOLEAN );
621 749 $legacyValue = $legacyValue ? C::YES : C::NO;
622 750
623 - $this->storeRawSetting($meta, $legacyValue);
751 + $this->storeRawSetting( $meta, $legacyValue );
624 752
625 - $this->deleteRawSetting($legacyMeta);
753 + $this->deleteRawSetting( $legacyMeta );
626 754
627 - return $this->getRawSetting($meta);
755 + return $this->getRawSetting( $meta );
628 756 }
629 757
630 758 /**
631 759 * Returns digit color.
@@ -634,31 +762,31 @@
634 762 * @return mixed
635 763 */
636 764 public function getDigitColor()
637 765 {
638 - $value = $this->getRawSetting('digit_color', false);
639 - if ($value) {
766 + $value = $this->getRawSetting( 'digit_color', false );
767 + if ( $value ) {
640 768 return $value;
641 769 }
642 770
643 - $legacy = $this->getLegacySetting('text_color');
644 - if ( ! $legacy) {
771 + $legacy = $this->getLegacySetting( 'text_color' );
772 + if ( ! $legacy ) {
645 773 return $this->digitColor;
646 774 }
647 775
648 - $this->setDigitColor($legacy);
649 - if ( ! $this->getRawSetting('label_color', false)) {
650 - $this->setLabelColor($legacy);
776 + $this->setDigitColor( $legacy );
777 + if ( ! $this->getRawSetting( 'label_color', false ) ) {
778 + $this->setLabelColor( $legacy );
651 779 }
652 780
653 - $this->deleteRawSetting('text_color');
781 + $this->deleteRawSetting( 'text_color' );
654 782
655 - return $this->getRawSetting('digit_color');
783 + return $this->getRawSetting( 'digit_color' );
656 784 }
657 785
658 - public function setLabelColor($color)
786 + public function setLabelColor( $color )
659 787 {
660 - $this->storeRawSetting('label_color', $color);
788 + $this->storeRawSetting( 'label_color', $color );
661 789 }
662 790
663 791 /**
664 792 * Get label color
@@ -666,30 +794,29 @@
666 794 * @return mixed
667 795 */
668 796 public function getLabelColor()
669 797 {
670 - $value = $this->getRawSetting('label_color', false);
671 - if ($value) {
798 + $value = $this->getRawSetting( 'label_color', false );
799 + if ( $value ) {
672 800 return $value;
673 801 }
674 802
675 - $legacy = $this->getLegacySetting('text_color');
676 - if ( ! $legacy) {
803 + $legacy = $this->getLegacySetting( 'text_color' );
804 + if ( ! $legacy ) {
677 805 return $this->labelColor;
678 806 }
679 807
680 - $this->setLabelColor($legacy);
681 - if ( ! $this->getRawSetting('digit_color', false)) {
682 - $this->setDigitColor($legacy);
808 + $this->setLabelColor( $legacy );
809 + if ( ! $this->getRawSetting( 'digit_color', false ) ) {
810 + $this->setDigitColor( $legacy );
683 811 }
684 812
685 - $this->deleteRawSetting('text_color');
813 + $this->deleteRawSetting( 'text_color' );
686 814
687 - return $this->getRawSetting('label_color');
815 + return $this->getRawSetting( 'label_color' );
688 816
689 817 }
690 818
691 -
692 819 /**
693 820 *
694 821 * Return timer digit size.
695 822 * Backward comapt. with older versions.
@@ -699,23 +826,23 @@
699 826 */
700 827 public function getDigitSize()
701 828 {
702 829 $meta = 'digit_size';
703 - $value = $this->getRawSetting($meta, false);
704 - if ( ! empty($value)) {
830 + $value = $this->getRawSetting( $meta, false );
831 + if ( ! empty( $value ) ) {
705 832 return $value;
706 833 }
707 - $legacy = $this->getLegacySetting('text_size');
834 + $legacy = $this->getLegacySetting( 'text_size' );
708 835
709 - if (empty($legacy)) {
836 + if ( empty( $legacy ) ) {
710 837 return $this->digitSize;
711 838 }
712 839
713 - $this->setDigitSize($legacy);
840 + $this->setDigitSize( $legacy );
714 841
715 - $this->deleteRawSetting('text_size');
842 + $this->deleteRawSetting( 'text_size' );
716 843
717 - return $this->getRawSetting('digit_size');
844 + return $this->getRawSetting( 'digit_size' );
718 845 }
719 846
720 847 /**
721 848 * Save timer digit size.
@@ -721,11 +848,11 @@
721 848 * Save timer digit size.
722 849 *
723 850 * @param $size
724 851 */
725 - public function setDigitSize($size)
852 + public function setDigitSize( $size )
726 853 {
727 - $this->storeRawSetting('digit_size', $size);
854 + $this->storeRawSetting( 'digit_size', $size );
728 855 }
729 856
730 857 /**
731 858 * Save timer label size.
@@ -731,16 +858,16 @@
731 858 * Save timer label size.
732 859 *
733 860 * @param $size
734 861 */
735 - public function setLabelSize($size)
862 + public function setLabelSize( $size )
736 863 {
737 - $this->storeRawSetting('label_size', $size);
864 + $this->storeRawSetting( 'label_size', $size );
738 865 }
739 866
740 - public function setHeadlineSize($size)
867 + public function setHeadlineSize( $size )
741 868 {
742 - $this->storeRawSetting('headline_size', $size);
869 + $this->storeRawSetting( 'headline_size', $size );
743 870 }
744 871
745 872 /**
746 873 * Returns evergreen duration.
@@ -748,20 +875,46 @@
748 875 * @return array
749 876 */
750 877 public function getDuration()
751 878 {
752 - $default = [0, 0, 0, 0];
753 - $duration = $this->getRawSetting('duration');
754 - if (is_array($duration)) {
755 - $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 );
756 883
757 - return array_map('intval', $duration);
884 + return array_map( 'intval', $duration );
758 885 }
759 886
760 887 return $default;
761 888 }
762 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 +
763 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 + /**
764 917 * Returns actions array.
765 918 *
766 919 * @return array
767 920 */
@@ -766,28 +919,28 @@
766 919 * @return array
767 920 */
768 921 public function getActions()
769 922 {
770 - $legacy = $this->getLegacySetting('end_action');
771 - if ($legacy) {
772 - $redirectUrl = $this->getLegacySetting('redirect_url');
923 + $legacy = $this->getLegacySetting( 'end_action' );
924 + if ( $legacy ) {
925 + $redirectUrl = $this->getLegacySetting( 'redirect_url' );
773 926 $actions = [
774 927 [
775 - 'id' => intval($legacy),
928 + 'id' => intval( $legacy ),
776 929 'redirectUrl' => $redirectUrl,
777 930 ],
778 931 ];
779 - $this->storeRawSetting('actions', $actions);
780 - $this->deleteRawSetting('end_action');
781 - $this->deleteRawSetting('redirect_url');
932 + $this->storeRawSetting( 'actions', $actions );
933 + $this->deleteRawSetting( 'end_action' );
934 + $this->deleteRawSetting( 'redirect_url' );
782 935
783 - return $this->mergeActions($actions);
936 + return $this->mergeActions( $actions );
784 937 }
785 938
786 - return $this->mergeActions($this->getRawSetting('actions'));
939 + return $this->mergeActions( $this->getRawSetting( 'actions' ) );
787 940 }
788 941
789 - private function mergeActions($actions)
942 + private function mergeActions( $actions )
790 943 {
791 944 $defaults = [
792 945 [
793 946 'id' => C::ACTION_NONE,
@@ -796,27 +949,29 @@
796 949 'wcStockStatus' => '',
797 950 ],
798 951 ];
799 952
800 - if (count($actions) === 0) {
953 + if ( count( $actions ) === 0 ) {
801 954 return $defaults;
802 955 }
803 956
804 - return array_map(function ($action) use ($defaults) {
805 - $action['id'] = (int)$action['id'];
957 + return array_map( function ( $action ) use ( $defaults ) {
958 + $action[ 'id' ] = (int)$action[ 'id' ];
806 959
807 - return array_merge($defaults[0], $action);
808 - }, $actions);
960 + return array_merge( $defaults[ 0 ], $action );
961 + }, $actions );
809 962 }
810 963
811 964 /**
812 965 * Returns evergreen duration in seconds.
813 966 *
967 + * @param array $duration
968 + *
814 969 * @return int
815 970 */
816 - public function getDurationInSeconds()
971 + public function durationArrayToSeconds( $duration = [] )
817 972 {
818 - list($d, $h, $m, $s) = $this->getDuration();
973 + list( $d, $h, $m, $s ) = empty( $duration ) ? $this->getDuration() : $duration;
819 974
820 975 return $d * DAY_IN_SECONDS +
821 976 $h * HOUR_IN_SECONDS +
822 977 $m * MINUTE_IN_SECONDS +
@@ -829,12 +984,147 @@
829 984 * @return string
830 985 */
831 986 public function getEndDatetime()
832 987 {
833 - return $this->getRawSetting('end_datetime') ?: $this->defaultEndDatetime();
988 + return $this->getRawSetting( 'end_datetime' ) ?: $this->defaultEndDatetime();
834 989 }
835 990
836 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 + /**
837 1127 * Returns compaign publish datetime.
838 1128 *
839 1129 * @return mixed
840 1130 */
@@ -839,9 +1129,9 @@
839 1129 * @return mixed
840 1130 */
841 1131 public function getStartTimestamp()
842 1132 {
843 - return get_the_date($this->id);
1133 + return get_the_date( $this->id );
844 1134 }
845 1135
846 1136 /**
847 1137 * Returns position in WooCommerce product page.
@@ -849,22 +1139,22 @@
849 1139 * @return mixed
850 1140 */
851 1141 public function getWcPosition()
852 1142 {
853 - $legacy = $this->getLegacySetting('position');
1143 + $legacy = $this->getLegacySetting( 'position' );
854 1144
855 - if ( ! $legacy) {
856 - return $this->getRawSetting('wc_position');
1145 + if ( ! $legacy ) {
1146 + return $this->getRawSetting( 'wc_position' );
857 1147 }
858 - $this->storeRawSetting('wc_position', $legacy);
859 - $this->deleteRawSetting('position');
1148 + $this->storeRawSetting( 'wc_position', $legacy );
1149 + $this->deleteRawSetting( 'position' );
860 1150
861 1151 return $legacy;
862 1152 }
863 1153
864 - private function getLegacySetting($name)
1154 + private function getLegacySetting( $name )
865 1155 {
866 - return get_post_meta($this->id, $name, true);
1156 + return get_post_meta( $this->id, $name, true );
867 1157 }
868 1158
869 1159 /**
870 1160 * Returns true if WooCommerce integration is enabled.
@@ -872,22 +1162,22 @@
872 1162 * @return mixed
873 1163 */
874 1164 public function getWcEnable()
875 1165 {
876 - $value = $this->getRawSetting('wc_enable', false);
1166 + $value = $this->getRawSetting( 'wc_enable', false );
877 1167
878 - if ($value === C::YES || $value === C::NO) {
1168 + if ( $value === C::YES || $value === C::NO ) {
879 1169 return $value;
880 1170 }
881 1171
882 - $legacy = filter_var($value, FILTER_VALIDATE_BOOLEAN);
883 - if ($legacy) {
884 - $this->storeRawSetting('wc_enable', C::YES);
1172 + $legacy = filter_var( $value, FILTER_VALIDATE_BOOLEAN );
1173 + if ( $legacy ) {
1174 + $this->storeRawSetting( 'wc_enable', C::YES );
885 1175 } else {
886 - $this->storeRawSetting('wc_enable', C::NO);
1176 + $this->storeRawSetting( 'wc_enable', C::NO );
887 1177 }
888 1178
889 - return $this->getRawSetting('wc_enable');
1179 + return $this->getRawSetting( 'wc_enable' );
890 1180 }
891 1181
892 1182 /**
893 1183 * Returns true if label should be displayed.
@@ -896,19 +1186,20 @@
896 1186 */
897 1187 public function getLabelVisibility()
898 1188 {
899 1189 $meta = 'label_visibility';
900 - $value = $this->getRawSetting($meta, false);
901 - if ($value === C::NO || $value === C::YES) {
1190 + $value = $this->getRawSetting( $meta, false );
1191 + if ( $value === C::NO || $value === C::YES ) {
902 1192 return $value;
903 1193 }
904 1194
905 - $legacy = filter_var($this->getLegacySetting('display_labels'), FILTER_VALIDATE_BOOLEAN);
1195 + $legacy = filter_var( $this->getLegacySetting( 'display_labels' ),
1196 + FILTER_VALIDATE_BOOLEAN );
906 1197 $legacy = $legacy ? C::YES : C::NO;
907 - $this->storeRawSetting($meta, $legacy);
908 - $this->deleteRawSetting('display_labels');
1198 + $this->storeRawSetting( $meta, $legacy );
1199 + $this->deleteRawSetting( 'display_labels' );
909 1200
910 - return $this->getRawSetting($meta);
1201 + return $this->getRawSetting( $meta );
911 1202
912 1203 }
913 1204
914 1205 /**
@@ -918,9 +1209,9 @@
918 1209 * @return int
919 1210 */
920 1211 public function getRestart()
921 1212 {
922 - return $this->getRawSetting('restart') ?: C::RESTART_NONE;
1213 + return $this->getRawSetting( 'restart' ) ?: C::RESTART_NONE;
923 1214 }
924 1215
925 1216 /**
926 1217 * Returns WooCommerce products selection type.
@@ -928,14 +1219,14 @@
928 1219 * @return string
929 1220 */
930 1221 public function getWcProductsSelectionType()
931 1222 {
932 - $legacy = $this->getLegacySetting('products_type');
933 - if ( ! $legacy) {
934 - return $this->getRawSetting('wc_products_selection_type');
1223 + $legacy = $this->getLegacySetting( 'products_type' );
1224 + if ( ! $legacy ) {
1225 + return $this->getRawSetting( 'wc_products_selection_type' );
935 1226 }
936 - $this->storeRawSetting('wc_products_selection_type', $legacy);
937 - $this->deleteRawSetting('products_type');
1227 + $this->storeRawSetting( 'wc_products_selection_type', $legacy );
1228 + $this->deleteRawSetting( 'products_type' );
938 1229
939 1230 return $legacy;
940 1231 }
941 1232
@@ -943,11 +1234,11 @@
943 1234 * Delete setting item.
944 1235 *
945 1236 * @param $name
946 1237 */
947 - public function deleteRawSetting($name)
1238 + public function deleteRawSetting( $name )
948 1239 {
949 - delete_post_meta($this->id, $name);
1240 + delete_post_meta( $this->id, $name );
950 1241 }
951 1242
952 1243 /**
953 1244 * Returns products selection IDs.
@@ -955,14 +1246,14 @@
955 1246 * @return array
956 1247 */
957 1248 public function getWcProductsSelection()
958 1249 {
959 - $legacy = $this->getLegacySetting('products');
960 - if ( ! $legacy) {
961 - return $this->getRawSetting('wc_products_selection');
1250 + $legacy = $this->getLegacySetting( 'products' );
1251 + if ( ! $legacy ) {
1252 + return $this->getRawSetting( 'wc_products_selection' );
962 1253 }
963 - $this->storeRawSetting('wc_products_selection', $legacy);
964 - $this->deleteRawSetting('products');
1254 + $this->storeRawSetting( 'wc_products_selection', $legacy );
1255 + $this->deleteRawSetting( 'products' );
965 1256
966 1257 return $legacy;
967 1258 }
968 1259
@@ -970,12 +1261,12 @@
970 1261 * Store custom labels.
971 1262 *
972 1263 * @param $labels
973 1264 */
974 - public function setLabels($labels)
1265 + public function setLabels( $labels )
975 1266 {
976 - $labels = array_merge($this->labels, array_filter($labels));
977 - update_post_meta($this->id, 'labels', $labels);
1267 + $labels = array_merge( $this->labels, array_filter( $labels ) );
1268 + update_post_meta( $this->id, 'labels', $labels );
978 1269 }
979 1270
980 1271 /**
981 1272 * Returns true if compaign can be published.
@@ -983,9 +1274,10 @@
983 1274 * @return bool
984 1275 */
985 1276 public function isActive()
986 1277 {
987 - 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";
988 1280
989 1281 }
990 1282
991 1283 /**
@@ -994,28 +1286,42 @@
994 1286 * @return bool
995 1287 */
996 1288 public function isScheduled()
997 1289 {
998 - 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;
999 1304 }
1000 1305
1001 1306 /**
1002 1307 * Returns true if fixed campaign datetime is expired.
1003 1308 *
1309 + * @param string|null $date
1310 + *
1004 1311 * @return bool
1005 1312 */
1006 - public function isExpired()
1313 + public function isExpired( $date = null )
1007 1314 {
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");
1315 + $endDate = $date === null ? date_create( $this->endDatetime ) : $date;
1316 + $today = date_create( current_time( "mysql" ) );
1010 1317
1011 1318 return $endDate < $today;
1012 1319 }
1013 1320
1014 -
1015 - public function isDismissed()
1321 + public function isStickyDismissed()
1016 1322 {
1017 - return isset($_COOKIE[CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed'])
1323 + return isset( $_COOKIE[ CookieDetection::COOKIE_PREFIX . $this->getId() . '_dismissed' ] )
1018 1324 && $this->stickyBarDismissible === C::YES
1019 1325 && $this->enableSticky === C::YES;
1020 1326 }
1021 1327
@@ -1025,13 +1331,13 @@
1025 1331 * @param string
1026 1332 *
1027 1333 * @return string
1028 1334 */
1029 - public function wrapTemplate($content)
1335 + public function wrapTemplate( $content )
1030 1336 {
1031 - $campaignBuilder = new CampaignBuilder($this);
1337 + $campaignBuilder = new CampaignBuilder( $this );
1032 1338
1033 - return $campaignBuilder->build($content);
1339 + return $campaignBuilder->build( $content );
1034 1340 }
1035 1341
1036 1342 /*
1037 1343 * Returns campaign template content.
@@ -1037,9 +1343,20 @@
1037 1343 * Returns campaign template content.
1038 1344 */
1039 1345 public function buildTemplate()
1040 1346 {
1041 - $campaignBuilder = new CampaignBuilder($this);
1347 + $campaignBuilder = new CampaignBuilder( $this );
1042 1348
1043 1349 return $campaignBuilder->template();
1044 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 +
1045 1362 }